SelectJoin
GitHubGet started

SQL models, written in Python.

Write warehouse models as Python functions, reuse components, and compile the whole graph into plain SQL.

$pip install "git+https://github.com/YuriyTrofymenko/selectjoin.git"

MIT licensed. Installs from GitHub until the PyPI release.

models/marts/mart_completed_orders.pyhover a line
from selectjoin import model, select, source

@model(grain="order_id")
def build():
    orders = source("orders")
    return (
        select(
            orders.order_id,
            (orders.amount - orders.discount).as_("net_amount"),
        )
        .from_(orders)
        .where(orders.status == "completed")
    )
$selectjoin compile mart_completed_orders --local duckdb
SELECT
  "orders"."order_id",
  "orders"."amount" - "orders"."discount" AS "net_amount"
FROM "orders"
WHERE
  "orders"."status" = 'completed'
$ selectjoin checkOK: 2 sources/models validated (duckdb).
01 The idea

SQL is where CSS was before preprocessors.

CSS moved to variables, mixins, and components, and stayed the output. Warehouse SQL is still typed by hand, with the same logic copied from file to file. SelectJoin makes SQL the output too.

By hand One rule, typed three times

stg_orders.sql
SELECT  order_id,  NULLIF(LOWER(TRIM(status)), '') AS statusFROM raw.orders
stg_refunds.sql
SELECT  refund_id,  NULLIF(LOWER(TRIM(status)), '') AS statusFROM raw.refunds
stg_returns.sqldrifted
SELECT  return_id,  LOWER(status) AS statusFROM raw.returns

With SelectJoin Written once, imported three times

components/staging.py
def clean_status(rows):    return (        rows.trim_spaces("status")        .lowercase_text("status")        .replace_empty_strings_with_null("status")    )
stg_orders.py
clean_status(  source("orders"))
stg_refunds.py
clean_status(  source("refunds"))
stg_returns.py
clean_status(  source("returns"))
Writing it by handWhat CSS gotWhat SelectJoin gives SQL
The same block copied into every fileMixins and componentsComponents and presets you import
One rule changed in twenty placesVariables and design tokensOne Python function; every model follows
Each variation typed out by handLoops and functionsModels and checks generated in a loop
Hoping nothing brokeA build that fails on errorsAn offline check with file, line, and fix
02 What changes

What generating SQL changes.

  1. 01

    Consistent SQL

    Every query is generated: the same quoting, aliases, and structure in every model.

  2. 02

    Errors before the warehouse

    A misspelled column fails an offline check, with the file, the line, and the fix.

  3. 03

    Breaking changes listed

    A diff lists every removed column and changed type, on each model downstream.

  4. 04

    Column lineage

    Column lineage traces a value through every model, back to the raw table.

  5. 05

    Smaller reviews

    A reviewer reads a short Python change and the SQL it produced.

  6. 06

    Rules written once

    A check for every model in a layer is one loop.

  7. 07

    Agents reuse your code

    Coding agents import your components and check their own work.

  8. 08

    Conventions built in

    Layers, name prefixes, and scaffolding put a new model in the right place.

  9. 09

    One place for scripts

    Audits, reports, and custom exports live in scripts/ and run with one command.

03 How it fits

What you write, and what it writes.

Pick any part to see it.

You write
selectjoincheck · compile · export
It writes
models/staging/stg_orders.pyModels →

A model is one query in one file: a Python function that returns a select. The folder it sits in is its layer: staging, intermediate, or marts.

sourcestagingintermediatemartsthe same colours as the model graph below

@model(grain="order_id")
def build():
    return clean_orders(source("orders"))
04 Use cases

Five common problems.

Pick one to see the code.

The same cleaning logic in ten models

Write it once as a Python function and import it. Presets name each data change.

# components/staging.py
def clean_status(orders):
    return (
        orders.trim_spaces("status")
        .lowercase_text("status")
        .replace_empty_strings_with_null("status")
        .named("normalized_status")
    )

# models/staging/stg_orders.py
from components.staging import clean_status

@model(grain="order_id")
def build():
    return clean_status(source("orders")).keep_first_row_per_key(
        by="order_id", order_by=["updated_at DESC"]
    )
Components and presets →

See all of it on a real project.

05 Coding agents

Agents work from your components.

  • ConsistentAn agent imports your components instead of writing the logic again.
  • Self-checkingOne command validates the project offline and answers in JSON: a pass, or the file and a fix.
  • Reports what it changedA diff lists every column removed and every type changed.
an agent checking its own change
$ selectjoin check --json
{
  "ok": false,
  "error": {
    "message": "… Unknown column: statuss Did you mean 'status'?",
    "file": "models/marts/mart_completed_orders.py",
    "line": 13,
    "suggestion": "status"
  }
}
# the agent applies the suggestion, then checks again
$ selectjoin check --json
{ "ok": true, "dialect": "duckdb", "units": 2, "checks": 0 }
06 Demo project

A real project and the SQL it compiles to.

The layered example from the package: 11 sources and models, from raw tables to a customer revenue mart.

examples/layersread-only

Hover a model to see what depends on it. Pick a column to trace it back to the raw tables.

mart_customer_revenue:
sourcestagingintermediatemarts
Click a model to open it below.
selectjoin · demo explorer

The explorer, live: each model's Python beside its SQL.

Open the explorer →
  1. 01

    Install

    A Python package with a command-line tool.

    $ pip install "git+https://github.com/YuriyTrofymenko/selectjoin.git"
  2. 02

    Create a project

    One source and one staging model.

    $ selectjoin init my_projectCreated project: my_project
  3. 03

    Add a model

    Scaffolded in the right layer.

    $ selectjoin new order_totals --layer marts --from stg_orders --grain order_idCreated mart_order_totals: models/marts/mart_order_totals.py
  4. 04

    Check and compile

    Validate offline, then read the SQL.

    $ selectjoin checkOK: 3 sources/models validated (duckdb).$ selectjoin compile mart_order_totals
What you see in your terminal
my_project
Support

Free to use. A star is all it asks.

Open source, no donations. If it saves you time, star it on GitHub.