Fixpoint semantics and optimization of recursive Datalog programs with aggregates

Carlo Zaniolo, Mohan Yang, Ariyam Das, Alexander Shkapsky, Tyson Condie, Matteo Interlandi

Introduction

Datalog has long been celebrated as a powerful declarative language for recursive queries, particularly in fields like program analysis, network routing, and graph processing. However, the integration of SQL-style aggregates—such as min, max, count, and sum—within recursive loops has historically been a significant challenge. This paper presents a formal framework for safely incorporating these aggregates into recursive Datalog programs, introducing a property called Pre-mappability (PREM) that enables dramatic performance optimizations.

The Bottleneck: Stratification and Non-Monotonicity

The primary obstacle to using aggregates in recursion is non-monotonicity. In standard Datalog logic, the system iterates until it reaches a "fixpoint" where results stop changing. Aggregates break this mathematical guarantee; for instance, adding a new path to a dataset might change the result of a min or count operation in a way that invalidates previous recursive steps.

To maintain logical consistency, most traditional Datalog systems rely on stratification. This approach mandates that all recursion must be completed before any aggregation is performed. While mathematically sound, stratification is computationally expensive for Big Data tasks. For example, in a shortest-path calculation, a stratified system must compute every possible path between nodes before finally selecting the minimum. On large-scale graphs, this leads to memory exhaustion and prohibitive execution times.

The PREM Framework

The authors propose a solution that moves beyond stratification by identifying conditions under which aggregates can be "pushed" directly into the recursive step. This is achieved through the concept of Pre-mappability (PREM).

1. The PREM Property

PREM defines formal conditions where a constraint (e.g., min(Dist) < 100) can be integrated into recursive rules without altering the final result. This allows the system to treat the aggregate as a pruning tool, discarding non-optimal data early in the computation.

2. Inflation and Deflation Preservation

The framework provides a systematic way to verify if rules are compatible with specific aggregates:

  • Inflation-preserving rules: Rules where larger inputs lead to larger outputs allow for the pushing of max constraints.
  • Deflation-preserving rules: Rules where smaller inputs lead to smaller outputs allow for the pushing of min constraints.

3. Monotonic Aggregates

For count and sum, the authors introduce specialized operators, mcount and msum. These operators are designed to grow monotonically as new data is discovered. By combining these with max constraints, the system can bridge the gap between theoretical fixpoint semantics and the practical requirements of standard SQL aggregates.

Implementation and Evaluation

The proposed techniques were implemented in DeALS, a high-performance Datalog engine. The evaluation utilized both synthetic Directed Acyclic Graphs (DAGs) and real-world datasets from the Stanford SNAP collection, including:

  • web-Stanford: 282K nodes, 2.3M arcs.
  • web-Google: 876K nodes, 5.1M arcs.
  • soc-Pokec: 1.6M nodes, 30.6M arcs.
  • soc-LiveJournal1: 4.8M nodes, 68.9M arcs.

Performance Results

The impact of PREM optimization was profound. On synthetic graphs, optimized programs (spath_prem) achieved speedups ranging from 7x to 177x compared to standard stratified versions.

In real-world scaling tests, the PREM approach successfully processed the soc-LiveJournal1 graph (68 million edges). In contrast, standard stratified Datalog failed to terminate or crashed due to memory limits, as it attempted to materialize an astronomical number of non-minimal paths. Furthermore, the PREM method proved to be 2x to 3x faster than XY-stratification, a common alternative. This efficiency stems from simpler execution plans; for instance, PREM generated an AND/OR tree with only 15 nodes, compared to the 120 nodes required by XY-stratification.

Conclusion and Future Work

By establishing a solid mathematical foundation for pushing aggregates into recursion, this research transforms Datalog's handling of complex analytics. The "Aha" moment of the paper is the realization that aggregates need not be computed at the end of a process; when they satisfy the PREM property, they serve as powerful pruning mechanisms that turn brute-force searches into efficient algorithms like Dijkstra’s.

While the current framework requires users to ensure rules meet preservation criteria, it sets the stage for more automated optimizers. Future work includes expanding the classes of PREM constraints and exploring their application in distributed environments like BigDatalog on Spark, potentially opening a new frontier for declarative Big Data analytics.

Summary updated

Abstract

A very desirable Datalog extension investigated by many researchers in the last 30 years consists in allowing the use of the basic SQL aggregates min, max, count and sum in recursive rules. In this paper, we propose a simple comprehensive solution that extends the declarative least-fixpoint semantics of Horn Clauses, along with the optimization techniques used in the bottom-up implementation approach adopted by many Datalog systems. We start by identifying a large class of programs of great practical interest in which the use of min or max in recursive rules does not compromise the declarative fixpoint semantics of the programs using those rules. Then, we revisit the monotonic versions of count and sum aggregates proposed by Mazuran et al. (2013b, The VLDB Journal 22, 4, 471–493) and named, respectively, mcount and msum. Since mcount, and also msum on positive numbers, are monotonic in the lattice of set-containment, they preserve the fixpoint semantics of Horn Clauses. However, in many applications of practical interest, their use can lead to inefficiencies, that can be eliminated by combining them with max, whereby mcount and msum become the standard count and sum. Therefore, the semantics and optimization techniques of Datalog are extended to recursive programs with min, max, count and sum, making possible the advanced applications of superior performance and scalability demonstrated by BigDatalog (Shkapsky et al. 2016. In SIGMOD. ACM, 1135–1149) and Datalog-MC (Yang et al. 2017. The VLDB Journal 26, 2, 229–248).
BibTeX
@inproceedings{zaniolo2017fixpoint,
  author = {Carlo Zaniolo and Mohan Yang and Ariyam Das and Alexander Shkapsky and Tyson Condie and Matteo Interlandi},
  title = {{Fixpoint semantics and optimization of recursive Datalog programs with aggregates}},
  booktitle = {Theory Pract. Log. Program},
  year = {2017},
  url = {https://openreview.net/forum?id=RCjkN1ExbBYy},
}
Figure