NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Juggling Functions Inside a Database

Mahmoud Abo Khamis, Hung Q. Ngo, Atri Rudra

Introduction

Across computer science, many fundamental problems share a common mathematical structure: computing generalized sums of products. Whether multiplying a chain of matrices, finding the Maximum A Posteriori (MAP) estimate in a probabilistic graphical model, or executing a complex SQL join, the underlying task is essentially evaluating a Functional Aggregate Query (FAQ).

Historically, these fields have developed isolated, domain-specific algorithms. Data scientists often extract data from relational databases to perform complex math in specialized tools like R or Python. This paper introduces the FAQ framework to unify these diverse computational tasks under a single algebraic umbrella, allowing a standard relational database to double as a general-purpose constraint solver, linear algebra processor, and machine learning inference engine.

The InsideOut Algorithm

The authors present InsideOut, a dynamic programming algorithm designed to evaluate FAQs by rewriting them into a series of smaller, more manageable sub-queries. The framework's workflow involves transforming an initial FAQ expression through structural optimizations before execution.

Overview of the FAQ framework and the InsideOut algorithm workflow, showing the transition from expression trees to tree decompositions and final evaluation.

Key Methodological Components

  • Variable Elimination & Indicator Projections: Similar to inference in graphical models, InsideOut eliminates variables sequentially. It introduces "indicator projections" to handle sparsity; if an input matrix is sparse, these projections act as filters to ensure only non-zero combinations are processed, preventing the computational blow-up of dense intermediate factors.
  • Worst-Case Optimal Joins (WCOJ): Each sub-query is treated as a natural database join and evaluated using modern WCOJ algorithms like LeapFrog Triejoin. This ensures the runtime is strictly bounded by the theoretical maximum output size (the AGM bound), processing queries globally rather than two tables at a time.
  • Optimizing Variable Ordering: The algorithm's efficiency depends heavily on the elimination order. The authors formalize this via FAQ-width (faqw). Since finding the perfect order is NP-hard, the framework utilizes tree decompositions to derive approximately optimal orderings.
A query hypergraph (a) and its corresponding tree decomposition (b), illustrating how complex relational structures are mapped to tree-based representations for optimized processing.

Key Insights and Theoretical Results

The FAQ framework blurs the line between data storage and computation. By treating input factors as database relations, the engine natively performs complex math without exporting data.

Theoretical Speedups

InsideOut achieves a runtime of $\tilde{O}(N^{\text{faqw}(\varphi)} + Z)$, where $N$ is the input size and $Z$ is the output size. This matches or strictly beats previous best-known algorithms across multiple domains:

  • Graphical Models: For Marginal Distribution and MAP queries, InsideOut improves the runtime exponent from standard tree-width (htw) to faqw, offering significant performance gains for specific query classes.
  • Logic & CSPs: It provides a unified algorithmic bound for Quantified Conjunctive Queries (QCQ), where no non-trivial unified algorithm previously existed.

Practical Implementation

The algorithm is implemented in the LogicBlox database engine. It enables orders-of-magnitude speedups for in-database machine learning by leveraging shared dynamic programming states across massive batches of queries.

Limitations and Future Work

While powerful, the framework faces challenges with immaterialized predicates (e.g., $a + b = c$), which do not have a finite size, complicating runtime estimation via AGM bounds. Additionally, because finding the absolute best variable ordering is NP-hard, practical implementations must rely on heuristics. Future work aims to extend this framework to a broader class of machine learning models, potentially computing hundreds of thousands of simultaneous queries through shared states.

Summary updated

BibTeX
@article{khamis2017juggling,
  author = {Mahmoud Abo Khamis and Hung Q. Ngo and Atri Rudra},
  title = {{Juggling Functions Inside a Database}},
  journal = {SIGMOD Rec.},
  year = {2017},
  doi = {10.1145/3093754.3093757},
}
Figure