Skip to content

Incremental models

Incremental models read a bounded batch, keep one row per key, and generate an upsert into an existing target table.

selectjoin.toml
[windows.daily]
days = 1
overlap_hours = 6
timezone = "UTC"
timestamp_type = "TIMESTAMPTZ"

run.window("daily") resolves the last complete calendar day in that timezone plus six hours before it. The start is inclusive and the end exclusive. For --as-of 2026-10-10, that’s October 8 at 18:00 through October 10 at 00:00 UTC. For a backfill, set exact start and end, which override days and overlap.

Use timestamp_type = "TIMESTAMP" for wall-clock times in the configured timezone, TIMESTAMPTZ for instants.

from selectjoin import Context, Incremental, model, select, source
from selectjoin.components import latest_per_key
@model(
grain=("event_id", "occurred_at"),
relation="analytics.events",
materialization=Incremental(
key=("event_id", "occurred_at"),
strategy="merge",
partition_by="occurred_at",
window="daily",
),
)
def build(run: Context):
events = source("events")
batch = select(
events.event_id, events.occurred_at, events.updated_at,
events.event_type, events.amount,
).from_(events)
if run.is_incremental:
batch = batch.where(run.window("daily").predicate(events.occurred_at))
incoming = batch.cte("incoming_events")
return latest_per_key(
incoming,
by=[incoming.event_id, incoming.occurred_at],
order_by=[incoming.updated_at.desc(), incoming.event_type.asc(), incoming.amount.desc()],
)

Apply the window filter in the model, before cleaning or deduplication, so the source stays reusable by models that need more history.

  • key identifies rows for the merge. Only merge is supported.
  • partition_by and window are an optional pair that bound both incoming rows and target matching. The partition column must be part of the key. They do not create physical partitions.
Terminal window
selectjoin check --as-of 2026-10-10
selectjoin compile stg_events --as-of 2026-10-10
selectjoin compile stg_events --materialize --as-of 2026-10-10
selectjoin compile stg_events --materialize --full-refresh
Mode Output
compile SELECT
compile --materialize MERGE (DuckDB and PostgreSQL)
compile --materialize --full-refresh CREATE TABLE AS over full history

run.is_incremental describes the compilation mode; it doesn’t check whether the target exists. --full-refresh makes it false, so guarded filters are omitted. Full refresh doesn’t replace an existing table.

Overlap only covers late data inside the selected range; use backfills for older arrivals. This strategy doesn’t handle deletes, key changes, persisted watermarks, or schema changes. The generated SQL is for review — SelectJoin never executes it or verifies key uniqueness.

Try it: selectjoin init /tmp/incremental-demo --template incremental.