Skip to main content
Version: 0.7.x

Slowly changing dimensions

Pattern. Turn a stream of attribute changes into SCD-2 rows: one row per version with effective_ts and next_effective_ts, ready for time-travel joins.

The query​

SELECT user, tier, effective_ts,
lead(effective_ts) OVER (PARTITION BY user ORDER BY effective_ts) AS next_effective_ts
FROM flow

lead() over the key orders the versions; the last version keeps NULL as next_effective_ts, meaning "still current". Persisting the output to an UPSERT sink keyed on (user, effective_ts) keeps the dimension table idempotent under at-least-once redelivery.

Input and output​

Input (two change events for user a):

usertiereffective_ts
asilver1000
agold2000

Output (SCD-2 rows):

usertiereffective_tsnext_effective_ts
asilver10002000
agold2000NULL

Point-in-time lookup​

With the dimension materialized, resolve "what tier was user a on at time t" with a range predicate (see temporal join):

SELECT o.*, d.tier
FROM orders o JOIN tiers d
ON o.user = d.user
AND o.ts >= d.effective_ts
AND (d.next_effective_ts IS NULL OR o.ts < d.next_effective_ts)

Runnable example​

examples/sql_scd.yaml (manifest name sql-scd).

Boundaries​

  • Per-batch scope: lead() sees one batch; versions of one key spread across batches must be merged downstream (the UPSERT sink approach handles this: each row is keyed and idempotent).
  • Late-arriving older versions insert a new range; consumers should treat overlapping ranges for the same key as a data-quality signal.