Architectural Overview
A single-pass multi-dimensional OLAP aggregation engine implementing conditional FILTER (WHERE ...), GROUPING SETS / ROLLUP subtotals with bitmask GROUPING() flags, and exact/interpolated percentiles.
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 Multi-Dimensional Revenue, Cohort & Funnel Aggregation Suite from first principles with deterministic self-test invariants.
Guided Implementation Walkthrough (4 Progressive Steps)
Step 1: Single-Pass Conditional Aggregation (FILTER Clause)
- Core Database Concept: Pivoting Funnel & Revenue Metrics in One Table Scan
- Target Module:
index.js(OlapAggregationSuite.conditionalAggregate) - Engineering Specification: Implement
conditionalAggregate(orders, groupCol)to computetotal_orders,paid_orders,paid_revenue_cents, andrefunded_revenue_centsper group in a single table scan without self-joins. - Implementation Hint:
Accumulate totalOrders, paidOrders, paidRevenue, and refundedRevenue in a single loop per group key. - Verification Invariant: deterministic assertion verified in
test.jsstep 1
Step 2: Hierarchical ROLLUP & Bitmask GROUPING() Indicator
- Core Database Concept: Generating Prefix Subtotals and Distinguishing Super-Aggregate NULLs
- Target Module:
index.js(OlapAggregationSuite.rollupRevenue) - Engineering Specification: Implement
rollupRevenue(orders, dim1, dim2)to compute hierarchical subtotals across(dim1, dim2),(dim1, null), and the grand total(null, null), settinggrouping_id(0,1, or3) to distinguish realNULLkeys from rollup super-aggregates. - Implementation Hint:
Emit groupings for (region, plan), (region, null) with groupingMask=1, and (null, null) with groupingMask=3. - Verification Invariant: deterministic assertion verified in
test.jsstep 2
Step 3: Continuous Linear-Interpolated Percentile (PERCENTILE_CONT)
- Core Database Concept: Order-Set Aggregate Interpolation Between Adjacent Ranks
- Target Module:
index.js(OlapAggregationSuite.percentileCont) - Engineering Specification: Implement
percentileCont(values, p)to ignorenull/undefinedinputs, sort the remaining numeric values, and linearly interpolate at fractional indexpos = p * (n - 1). - Implementation Hint:
Sort non-null values ascending; compute pos = p * (n - 1), lo = Math.floor(pos), hi = Math.ceil(pos). - Verification Invariant: deterministic assertion verified in
test.jsstep 3
Step 4: HAVING Post-Aggregation Group Filter
- Core Database Concept: Filtering Aggregated Buckets After Group Collapse
- Target Module:
index.js(OlapAggregationSuite.filterHaving) - Engineering Specification: Implement
filterHaving(groups, minPaidRevenueCents, minConversionRate)to retain only groups meeting post-aggregation business thresholds. - Implementation Hint:
Filter the aggregated summary array using havingPredicate(groupSummary). - Verification Invariant: deterministic assertion verified in
test.jsstep 4
Relational & Storage Engine Guarantees
- Curriculum Domain: Aggregation & Grouping Sets
- Difficulty & Time Budget: Intermediate (~35 minutes)
- Key Architectural Highlights:
• Single-pass conditional aggregation equivalent to COUNT/SUM(...) FILTER (WHERE ...)
• Hierarchical ROLLUP and GROUPING SETS generator with bitmask GROUPING(col) metadata
• Exact discrete (PERCENTILE_DISC) and continuous interpolated (PERCENTILE_CONT) quantiles - Core Primitives & Tags:
GROUPING SETS,ROLLUP,Conditional Aggregation,FILTER Clause,Percentiles
Related Curriculum Units
- aggregate functions internals
- group by execution mechanics
- having vs where pipeline
- conditional aggregation filter
- string json array aggregation
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.