-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support customizing how dbt nodes are converted to Airflow (#503)
This change aims to solve the following current limitations of Cosmos 1.0: * If you want to pass arguments to just some tasks (e.g., on_warning_callback to test nodes), Cosmos has to add it to the main interface and explicitly pass it down to just those tasks * If a user wants to subclass one of the Cosmos operators and use that instead, they can't * If a user wants more granular customization over how each task is rendered, they can't use Cosmos It does this by introducing the parameter `node_converters` to `RenderConfig`, which allows users to define a custom function to convert a DbtNode into nothing or an Airflow resource (Operator or TaskGroup instances). ## Example of the feature ``` import os from datetime import datetime from pathlib import Path from airflow.operators.dummy import DummyOperator from airflow.models.dag import DAG from airflow.utils.task_group import TaskGroup from cosmos import DbtDag, ProjectConfig, ProfileConfig, RenderConfig from cosmos.constants import DbtResourceType from cosmos.dbt.graph import DbtNode DEFAULT_DBT_ROOT_PATH = Path(__file__).parent / "dbt" DBT_ROOT_PATH = Path(os.getenv("DBT_ROOT_PATH", DEFAULT_DBT_ROOT_PATH)) os.environ["DBT_SQLITE_PATH"] = str(DEFAULT_DBT_ROOT_PATH / "simple") profile_config = ProfileConfig( profile_name="simple", target_name="dev", profiles_yml_filepath=(DBT_ROOT_PATH / "simple/profiles.yml"), ) # [START custom_dbt_nodes] def convert_source(dag: DAG, task_group: TaskGroup, node: DbtNode, **kwargs): return DummyOperator(dag=dag, task_group=task_group, task_id=f"{node.name}_source") def convert_exposure(dag: DAG, task_group: TaskGroup, node: DbtNode, **kwargs): return DummyOperator(dag=dag, task_group=task_group, task_id=f"{node.name}_exposure") render_config = RenderConfig( node_converters={DbtResourceType.SOURCE: convert_source, DbtResourceType("exposure"): convert_exposure} ) example_cosmos_sources = DbtDag( # dbt/cosmos-specific parameters project_config=ProjectConfig( DBT_ROOT_PATH / "simple", ), profile_config=profile_config, render_config=render_config, operator_args={"append_env": True}, # normal dag parameters schedule_interval="@daily", start_date=datetime(2023, 1, 1), catchup=False, dag_id="example_cosmos_sources", ) # [END custom_dbt_nodes] ``` It is now rendered in Airflow: <img width="1173" alt="Screenshot 2023-10-10 at 12 04 31" src="https://github.com/astronomer/astronomer-cosmos/assets/272048/96f96261-4a8e-4418-ae67-63404fc59f77"> Before this change, there was no way for users to describe how to render source or other unsupported dbt resources. ## Related Issue(s) Closes: #427 Closes: #477
- Loading branch information
Showing
34 changed files
with
7,611 additions
and
12,853 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -220,6 +220,68 @@ jobs: | |
DATABRICKS_WAREHOUSE_ID: ${{ secrets.DATABRICKS_WAREHOUSE_ID }} | ||
DATABRICKS_CLUSTER_ID: ${{ secrets.DATABRICKS_CLUSTER_ID }} | ||
|
||
Run-Integration-Tests-Sqlite: | ||
needs: Authorize | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.10"] | ||
airflow-version: ["2.7"] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ github.event.pull_request.head.sha || github.ref }} | ||
- uses: actions/cache@v3 | ||
with: | ||
path: | | ||
~/.cache/pip | ||
.nox | ||
key: integration-sqlite-${{ runner.os }}-${{ matrix.python-version }}-${{ matrix.airflow-version }}-${{ hashFiles('pyproject.toml') }}-${{ hashFiles('cosmos/__init__.py') }} | ||
|
||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install packages and dependencies | ||
run: | | ||
python -m pip install hatch | ||
hatch -e tests.py${{ matrix.python-version }}-${{ matrix.airflow-version }} run pip freeze | ||
- name: Test Cosmos against Airflow ${{ matrix.airflow-version }} and Python ${{ matrix.python-version }} | ||
run: | | ||
hatch run tests.py${{ matrix.python-version }}-${{ matrix.airflow-version }}:test-integration-sqlite-setup | ||
hatch run tests.py${{ matrix.python-version }}-${{ matrix.airflow-version }}:test-integration-sqlite | ||
env: | ||
AIRFLOW_HOME: /home/runner/work/astronomer-cosmos/astronomer-cosmos/ | ||
AIRFLOW_CONN_AIRFLOW_DB: postgres://postgres:[email protected]:5432/postgres | ||
PYTHONPATH: /home/runner/work/astronomer-cosmos/astronomer-cosmos/:$PYTHONPATH | ||
AIRFLOW_CONN_DATABRICKS_DEFAULT: ${{ secrets.AIRFLOW_CONN_DATABRICKS_DEFAULT }} | ||
DATABRICKS_HOST: ${{ secrets.DATABRICKS_HOST }} | ||
DATABRICKS_TOKEN: ${{ secrets.DATABRICKS_TOKEN }} | ||
DATABRICKS_WAREHOUSE_ID: ${{ secrets.DATABRICKS_WAREHOUSE_ID }} | ||
DATABRICKS_CLUSTER_ID: ${{ secrets.DATABRICKS_CLUSTER_ID }} | ||
COSMOS_CONN_POSTGRES_PASSWORD: ${{ secrets.COSMOS_CONN_POSTGRES_PASSWORD }} | ||
POSTGRES_HOST: localhost | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_DB: postgres | ||
POSTGRES_SCHEMA: public | ||
POSTGRES_PORT: 5432 | ||
|
||
- name: Upload coverage to Github | ||
uses: actions/upload-artifact@v2 | ||
with: | ||
name: coverage-integration-sqlite-test-${{ matrix.python-version }}-${{ matrix.airflow-version }} | ||
path: .coverage | ||
|
||
env: | ||
AIRFLOW_HOME: /home/runner/work/astronomer-cosmos/astronomer-cosmos/ | ||
AIRFLOW_CONN_AIRFLOW_DB: postgres://postgres:[email protected]:5432/postgres | ||
PYTHONPATH: /home/runner/work/astronomer-cosmos/astronomer-cosmos/:$PYTHONPATH | ||
|
||
|
||
Code-Coverage: | ||
if: github.event.action != 'labeled' | ||
needs: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
target/ | ||
dbt_packages/ | ||
logs/ |
Oops, something went wrong.