Skip to content

Use with Airflow

SelectJoin can write an Airflow DAG that builds your models in dependency order. Airflow runs the SQL; SelectJoin only writes the files.

Terminal window
selectjoin export airflow --output path/to/dags --schema analytics --schedule "@daily"

This writes a DAG file and one SQL file per model into the dags folder:

dags/
├── selectjoin.py
└── selectjoin_sql/
├── stg_orders.sql
├── int_all_orders.sql
└── mart_customer_revenue.sql

The DAG has one SQLExecuteQueryOperator task per model that builds a view or a table. Task dependencies follow the model graph, so a model runs after the models it reads.

A normal selectjoin compile inlines every upstream model as a CTE. In the exported files, a model reads the tables and views that earlier tasks built instead, so nothing is computed twice:

-- Generated by SelectJoin from models/marts/mart_customer_revenue.py.
CREATE OR REPLACE VIEW analytics.mart_customer_revenue AS
SELECT
"int_order_customer_features"."customer_id",
"int_order_customer_features"."customer_name",
COUNT(*) AS "order_count",
SUM("int_order_customer_features"."net_amount") AS "net_revenue",
SUM("int_order_customer_features"."loyalty_adjusted_amount") AS "loyalty_adjusted_revenue"
FROM analytics.int_order_customer_features AS "int_order_customer_features"
WHERE
"int_order_customer_features"."is_completed"
GROUP BY
"int_order_customer_features"."customer_id",
"int_order_customer_features"."customer_name";

Sources, ephemeral models, and package models have no task of their own, so they are inlined where they are used.

Option Default Description
--output PATH required The Airflow dags folder.
--dag-id ID selectjoin The DAG id. The DAG file and the SQL folder are named after it.
--conn-id ID warehouse The Airflow connection the tasks use.
--schedule VALUE none A cron expression or preset such as @daily. Without it the DAG runs only when triggered.
--schema NAME none The schema models are built in, when a model’s relation has none.
--dialect D project dialect SQL dialect of the output.
--force off Replace previously exported files.
SelectJoin SQL in the task
view CREATE OR REPLACE VIEW ... AS
table DROP TABLE IF EXISTS ...; then CREATE TABLE ... AS
incremental The same as table: a full rebuild on every run
ephemeral, sources No task; inlined into the models that use them

The DAG imports SQLExecuteQueryOperator from the apache-airflow-providers-common-sql package and uses the schedule argument, which needs Airflow 2.4 or later. Create a connection with the id you pass as --conn-id.

  • Incremental models rebuild fully. SelectJoin resolves date windows when it compiles, so a static SQL file cannot merge a moving window. To merge a window, run selectjoin compile NAME --materialize --as-of ... at run time instead.
  • Data checks from checks.py are not exported as tasks.
  • Dropping a table that a view depends on fails on warehouses that protect dependent views, such as PostgreSQL.
  • The exported DAG was loaded with Airflow 2.11 to confirm its tasks and dependencies. Its tasks have not been run against a warehouse.