Architectural Overview
A Kimball dimensional warehouse pipeline implementing automated Slowly Changing Dimension (SCD Type 2) effective date range versioning, surrogate key assignment, and point-in-time historical fact-to-dimension joins.
In production relational and analytical database systems—spanning PostgreSQL, SQLite, MySQL/InnoDB, and DuckDB—understanding how query clauses, schema constraints, index structures, and storage engines execute under the hood separates surface-level query authors from staff-level database engineers. This capstone build lab guides you through implementing Star Schema with Automated SCD Type 2 Dimension Pipeline from first principles with deterministic self-test invariants.
Guided Implementation Walkthrough (4 Progressive Steps)
Step 1: Initial Dimension Seed & Surrogate Key Allocation
- Core Database Concept: Decoupling Warehouse Surrogate Keys from Operational Natural Keys
- Target Module:
index.js(ScdType2Pipeline.applyChange) - Engineering Specification: Implement the initial insert path in
applyChange(naturalKey, attrs, changeTs)to allocate a monotonically increasingsk(surrogate key) and initialize[valid_from, "9999-12-31")withis_current: true. - Implementation Hint:
If no current row exists for naturalKey, insert a new version with valid_from = changeTs, valid_to = "9999-12-31", is_current = true. - Verification Invariant: deterministic assertion verified in
test.jsstep 1
Step 2: SCD Type 2 Attribute Diffing & Prior Row Expiration
- Core Database Concept: Closing valid_from, changeTs) on Attribute Drift and Appending a New Current Version
- Target Module:
index.js(ScdType2Pipeline.applyChange) - Engineering Specification: Complete
applyChange(naturalKey, attrs, changeTs)so that when tracked attributes change, the existing current version is expired (valid_to = changeTs,is_current = false) and a new current version is appended; if attributes are unchanged, perform a no-op. - Implementation Hint:
If current row attributes match attrs, do nothing (idempotent); otherwise set current.valid_to = changeTs, current.is_current = false, and insert new version. - Verification Invariant: deterministic assertion verified in
test.jsstep 2
Step 3: Point-in-Time Historical Surrogate Key Lookup
- Core Database Concept: Half-Open Interval Containment: valid_from <= eventTs < valid_to
- Target Module:
index.js(ScdType2Pipeline.lookupSurrogateKeyAsOf) - Engineering Specification: Implement
lookupSurrogateKeyAsOf(naturalKey, eventTs)using half-open interval logic (valid_from <= eventTs && eventTs < valid_to) so boundary timestamps match exactly one version. - Implementation Hint:
Find the dimension version for naturalKey where row.valid_from <= eventTs && eventTs < row.valid_to. - Verification Invariant: deterministic assertion verified in
test.jsstep 3
Step 4: As-Was Historical Fact Table Star Join & Aggregation
- Core Database Concept: Attributing Fact Revenue to the Dimension Attribute Active at Event Time
- Target Module:
index.js(ScdType2Pipeline.enrichAndAggregateFacts) - Engineering Specification: Implement
enrichAndAggregateFacts(facts)to join each fact event to the dimension version that was active atfact.eventTsand aggregate revenue by historical tier. - Implementation Hint:
Resolve each fact’s historical dimension row via lookupSurrogateKeyAsOf and sum amount_cents by the historical tier. - Verification Invariant: deterministic assertion verified in
test.jsstep 4
Relational & Storage Engine Guarantees
- Curriculum Domain: Dimensional Modeling & Kimball OLAP
- Difficulty & Time Budget: Advanced (~40 minutes)
- Key Architectural Highlights:
• Automated SCD Type 2 change detection expiring prior version rows and inserting new surrogate keys
• Half-open [valid_from, valid_to) temporal interval invariants with is_current boolean flags
• As-was point-in-time historical fact table enrichment resolving surrogate keys by event timestamp - Core Primitives & Tags:
Star Schema,SCD Type 2,Surrogate Keys,Point-in-Time Join,Kimball OLAP
Related Curriculum Units
- [oltp vs olap workload physics
- star schema facts dimensions
- snowflake vs star vs obt
- slowly changing dimensions scd
- bridge tables junk role dimensions
Production Database Context & Verification
This from-scratch relational engine component is engineered with zero external dependencies and deterministic verification harnesses. Every internal database invariant—from three-valued logic (TRUE / FALSE / UNKNOWN), referential integrity, and fan-out-free join pre-aggregation to B+Tree page splits, MVCC snapshot visibility (xmin / xmax), slotted-page compaction, and ARIES write-ahead log crash recovery—is asserted directly in test.js. Use the interactive browser workbench above to complete the 4 guided steps in Guided Lab mode or inspect and benchmark the complete reference architecture in Full Solution mode.