DataJunction handles different aggregation types using specific decomposition patterns:
Simple Aggregations
SUM, COUNT, MIN, and MAX are already additive and decompose trivially.
AVG (Average)
Requires two components: sum_col and count_col.
- Accumulate:
SUM(x) and COUNT(x) - Merge: Both use
SUM to roll up. - Combiner:
SUM(sum_col) / SUM(count_col)
APPROX_COUNT_DISTINCT (HyperLogLog)
Uses probabilistic HLL sketches that can be merged.
- Accumulate:
hll_sketch_agg(x) - Merge:
hll_union - Combiner:
hll_sketch_estimate(hll_union(hll_col))
Note: DJ uses Spark function names internally and translates them to target dialects like Druid or Trino at query time.
Variance & Standard Deviation
Uses the identity VAR = E[X²] - E[X]² and requires three components:
sum_col (SUM(x))sum_sq_col (SUM(POWER(x, 2)))count_col (COUNT(x))
Covariance & Correlation
- Covariance requires:
sum_x, sum_y, sum_xy, and count. - Correlation requires 6 components:
sum_x, sum_y, sum_x_sq, sum_y_sq, sum_xy, and count.
-- Example: Decomposed measures for STDDEV(response_time)
SELECT
SUM(response_time) AS response_time_sum,
SUM(POWER(response_time, 2)) AS response_time_sum_sq,
COUNT(response_time) AS response_time_count
FROM requests