Use with dbt
SelectJoin and dbt fit together in two directions. You can write models in SelectJoin and let dbt run them, and you can build SelectJoin models on top of models dbt already owns. SelectJoin still never connects to a warehouse: both directions work from files.
Export a project to dbt
Section titled “Export a project to dbt”selectjoin export dbt --output path/to/dbt_projectThis writes one dbt model per source and model into models/selectjoin/ of the dbt project:
models/selectjoin/├── _selectjoin__models.yml├── _selectjoin__sources.yml├── sources/orders.sql├── staging/stg_orders.sql└── marts/mart_customer_revenue.sqlEach file holds that unit’s own query. References to other units become ref() calls, so dbt sees the same dependency graph SelectJoin does:
{{ config(materialized='view') }}
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 {{ ref('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"What maps to what
Section titled “What maps to what”| SelectJoin | dbt |
|---|---|
| Model | A model in models/selectjoin/<layer>/, with the same materialization (view, table, ephemeral) |
| Source | An ephemeral model in models/selectjoin/sources/, so dbt inlines it as SelectJoin does |
| Physical table inside a source | {{ source('schema', 'table') }}, declared in _selectjoin__sources.yml |
| Reference to another unit | {{ ref('name') }} |
| Output columns and types | columns with data_type in _selectjoin__models.yml |
| Description | description |
| Column description | description on the column |
| Single-column grain | unique and not_null data tests on that column |
| Composite grain | not_null on each column |
relation="schema.name" |
alias='name' |
Files that already exist are never replaced unless you pass --force. --dialect overrides the SQL dialect of the output. Treat the exported files as generated: change the Python and export again.
Limits
Section titled “Limits”- Incremental models are exported as
materialized='incremental'with theirunique_key, but SelectJoin date windows are not. The exported model reprocesses its whole input on every run until you add anis_incremental()filter. The file carries a comment saying so. - Data checks from
checks.pyare not exported yet. - Composite grains are not tested for uniqueness, because that needs a dbt package.
- A physical table declared without a schema is left as a plain table name instead of a
source()call. - The YAML uses
data_tests, which needs dbt 1.8 or later.
Build on an existing dbt project
Section titled “Build on an existing dbt project”Declare the dbt project as a package in selectjoin.toml, pointing at the manifest dbt writes to its target/ folder:
[packages.analytics]type = "dbt"manifest = "../analytics/target/manifest.json"catalog = "../analytics/target/catalog.json" # optionalEvery model, seed, and snapshot the dbt project owns becomes a source named <package>.<model>. Reference it like any other unit:
from selectjoin import functions as fnfrom selectjoin import model, ref, select, source
@model(grain="region")def build(): revenue = ref("analytics.mart_customer_revenue") regions = source("regions") return ( select(regions.region, fn.sum(revenue.net_revenue).as_("net_revenue")) .from_(revenue) .join(regions, on=revenue.customer_id == regions.customer_id) .group_by(regions.region) )The compiled SQL selects from the table dbt builds, using the relation name in the manifest. selectjoin list, inspect, deps, and the lineage explorer all show package models.
Column types
Section titled “Column types”SelectJoin validates columns offline only when it knows a model’s full schema. It takes it from:
catalog.json, when you setcatalog. Rundbt docs generateto create it.- Otherwise the manifest, when every documented column of the model has a
data_type.
Without either, the model is still usable, but its columns are not checked.
Column descriptions documented in dbt are imported with the types, and show up in selectjoin inspect.
What is skipped
Section titled “What is skipped”- Ephemeral dbt models, because they have no table to select from.
- Models that belong to dbt packages the project installs, such as
dbt_utils. - Older versions of a versioned model. Only the latest version is imported.
The manifest is read when the project loads. Run dbt parse or dbt compile again after the dbt project changes.
Both together
Section titled “Both together”A SelectJoin model built on a dbt package exports back as a reference to the real dbt model:
FROM {{ ref('analytics', 'mart_customer_revenue') }} AS "analytics.mart_customer_revenue"So a team can keep its dbt project, write new models in SelectJoin against it, and export them into the same dbt project.