Skip to content

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.

Terminal window
selectjoin export dbt --output path/to/dbt_project

This 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.sql

Each 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"
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.

  • Incremental models are exported as materialized='incremental' with their unique_key, but SelectJoin date windows are not. The exported model reprocesses its whole input on every run until you add an is_incremental() filter. The file carries a comment saying so.
  • Data checks from checks.py are 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.

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" # optional

Every 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 fn
from 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.

SelectJoin validates columns offline only when it knows a model’s full schema. It takes it from:

  1. catalog.json, when you set catalog. Run dbt docs generate to create it.
  2. 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.

  • 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.

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.