Sharing Computations for User-Defined Aggregate Functions

Chao Zhang, Farouk ToumaniEdbt 2020

Introduction

Modern data analytics increasingly relies on complex statistical functions that go beyond standard SQL aggregates like SUM or COUNT. From linear regression coefficients to skewness and custom machine learning metrics, data scientists frequently implement these as User-Defined Aggregate Functions (UDAFs). However, traditional UDAF implementations are often treated as "black boxes" by database engines, leading to significant performance bottlenecks and missed optimization opportunities.

The "Black Box" Problem

Current UDAF implementations suffer from two primary issues:

  1. Hardcoding Burden: Users must manually implement low-level routines, typically following the initialize-update-merge-evaluate (IUME) pattern. This process is error-prone and requires deep programming expertise.
  2. Optimization Blindness: Because the query engine does not understand the mathematical logic within a hardcoded function, it cannot optimize it. For instance, if one query calculates variance and another calculates standard deviation, the engine typically computes both from scratch, despite their shared underlying mathematical components.

The SUDAF Framework

To address these challenges, we introduce SUDAF (Sharing User-Defined Aggregate Functions), a framework that shifts UDAFs from imperative code to declarative mathematical expressions.

Declarative Expressions and Canonical Form

Users define UDAFs as mathematical formulas. SUDAF automatically translates these into a Canonical Form $(F, \oplus, T)$, consisting of a translating function, a commutative/associative binary operation, and a terminating function. This allows the system to decompose complex UDAFs into "partial aggregates" that can be executed using highly optimized built-in functions like sum, count, and power.

Solving the Sharing Problem

A key innovation of SUDAF is its ability to identify when one UDAF can reuse the cached results of another. While general function equivalence is undecidable, SUDAF defines a restricted set of primitive functions (Scalar, Binary, and Aggregate) that are expressive enough for real-world analytics while making the sharing problem decidable. By using symbolic representations, the system precomputes sharing relationships, avoiding expensive runtime transformations.

Evaluation and Results

The SUDAF framework was evaluated using the industry-standard TPC-DS benchmark and the Milan Dataset, a real-world telecommunications dataset containing up to 319 million rows.

Performance Gains

Even without sharing, rewriting UDAFs into built-in functions provided a 10x speedup in PostgreSQL and a 1.25x speedup in Spark SQL for linear regression queries. However, the most dramatic improvements occurred when sharing was enabled.

Execution time comparison in PostgreSQL showing significant speedups with SUDAF, especially when sharing results (b) and (c).

As shown in the PostgreSQL evaluation (Figure 4), SUDAF with sharing achieved massive gains. For example, in query Q2 (after Q1), SUDAF reduced execution time to just 0.892s, compared to nearly 150s for a standard UDAF. In query Q3, the execution time dropped from 19s to a mere 31.7ms when using the optimized shared version (RQ3').

Execution time comparison in Spark SQL demonstrating performance improvements across different query types.

Similar trends were observed in Spark SQL (Figure 3), where SUDAF consistently outperformed traditional UDAF implementations. By aggressively reusing cached partial results, SUDAF maintains low execution times even as query complexity increases.

Conclusion and Future Work

SUDAF demonstrates that by breaking the "black box" of UDAFs and treating them as declarative mathematical expressions, database engines can unlock significant optimization potential. The ability to reuse partial aggregates leads to speedups of up to two orders of magnitude.

Future work will explore extending these techniques to sharing across different data ranges (predicates) and handling more complex SQL scenarios involving joins and multiple group-by clauses. While truly "holistic" functions like median cannot be expressed exactly, SUDAF already supports the partial aggregates needed for high-quality approximations via moment sketches.

Summary updated

BibTeX
@inproceedings{zhang2020sharing,
  author = {Chao Zhang and Farouk Toumani},
  title = {{Sharing Computations for User-Defined Aggregate Functions}},
  booktitle = {Edbt},
  year = {2020},
  doi = {10.5441/002/edbt.2020.22},
}
Figure