SelectJoin
GitHubGet started
v0.1 Early release · explore the demo project →

SQL models, written in Python.

Hand-written SQL does not scale. Write warehouse models as Python functions, reuse components, and compile the whole graph into clean, reviewable SQL.

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

Open source, MIT licensed. Installs from source while the PyPI release is prepared.

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.customer_id,
            (orders.amount - orders.discount).as_("net_amount"),
        )
        .from_(orders)
        .where(orders.status == "completed")
    )
$selectjoin compile mart_completed_orders duckdb
WITH "orders" AS (
  SELECT "order_id", "customer_id", "status", "amount", "discount"
  FROM raw.orders
)
SELECT
  "orders"."order_id",
  "orders"."customer_id",
  "orders"."amount" - "orders"."discount" AS "net_amount"
FROM "orders"
WHERE "orders"."status" = 'completed';
$ selectjoin checkOK: 2 sources/models validated (duckdb).
Works with
  • Python
  • DuckDB
  • PostgreSQL
  • dbt
  • Looker
  • Airflow
  • Metabase soon
01 The idea

SQL is where CSS was before preprocessors.

Hand-written CSS stopped scaling years ago. The web moved to variables, mixins, and components, and kept CSS as the output. Warehouse SQL is still typed by hand: the same logic copied from file to file, a little different each time. 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

What you ship is still plain, readable SQL. You just stop typing it.

02 Use cases

Five problems every SQL codebase has.

Pick one to see how SelectJoin handles it.

The same cleaning logic in ten models

Write it once as a Python function and import it. Presets name every data change, so a reviewer sees exactly what happens to each column.

# 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 →
03 Built for agents

Built for the agents that write your SQL.

Ask an agent for SQL and it writes it from scratch, a little differently each time. In a SelectJoin project it builds from the same components your team uses, checks its own work, and tells you what it changed.

  • ConsistentIt imports your components instead of writing the logic again, so the tenth model cleans a column exactly like the first.
  • Self-checkingOne command validates the whole project offline and answers in JSON: a pass, or the file and a suggested fix.
  • Honest about impactBefore anything merges, it can list every column it removed and every type it 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 }
04 Demo project

Browse a real project. Read every line it compiles to.

The layered example that ships with the package: 11 sources and models, from raw tables to a customer revenue mart. Open any node to see its Python next to its SQL.

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 itself, shown live: each model's Python beside the SQL it compiles to.

Open the explorer →
05 How it fits

Python in. The files your stack already reads, out.

SelectJoin checks your project offline, then writes it in the form each tool expects. Pick an output to see the same model in that form.

You write
sources/tables and column types
models/queries, one per file
components/shared Python functions
checks.pydata quality rules
dbt manifestoptional: build on dbt models
selectjoincheck · compile · export
It writes
selectjoin compile mart_customer_revenue --localShip the SQL →
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 "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";
06 Get started

From nothing to compiled SQL in four steps.

  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

    A folder with one source and one staging model, ready to compile.

    $ selectjoin init my_projectCreated project: my_project
  3. 03

    Add a model

    Scaffolded in the right layer, ready to edit.

    $ 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 everything 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
Keep it independent

Built by developers. Funded by its users.

If SelectJoin saves you time, sponsoring keeps the Python package free, fast, and focused on the people who write the models.