Optimally Rewriting Formulas and Database Queries: A Confluence of Term Rewriting, Structural Decomposition, and Complexity

Hubie Chen, Stefan Mengel

Abstract

Evaluating first-order (FO) logic sentences is a cornerstone of database query processing, where the efficiency of evaluation is primarily determined by the "width" of the formula—the maximum number of free variables in any subformula. While minimizing width is generally undecidable, this paper identifies a comprehensive set of syntactic rewriting rules and provides a polynomial-time algorithm (given a treewidth oracle) to find the optimal width achievable through these rules. By integrating term rewriting theory with structural graph decomposition, the authors bridge the gap between heuristic query optimization and theoretical complexity.

Introduction

In the realm of database theory, the performance of SQL queries—mathematically represented as first-order logic sentences—is heavily dependent on their structural properties. Specifically, the width of a sentence serves as a critical metric: lower width typically translates to faster evaluation times. However, finding the absolute minimum-width equivalent for an arbitrary FO sentence is a known undecidable problem.

Practitioners often rely on a standard suite of rewriting rules, such as reordering conjunctions or pushing quantifiers deeper into a formula, to reduce width. Until now, there was no formal guarantee that these manual or heuristic transformations reached the most efficient possible version of a query. This paper addresses this gap by providing an algorithmic solution that guarantees optimality within the bounds of a standard set of syntactic rules.

Theoretical Framework: Gauged Systems

The authors' approach begins by extending the theory of abstract reduction systems into what they term gauged systems. In this framework, every logical formula is associated with a "gauge" (its width). The objective is to navigate the space of equivalent formulas using rewriting rules to reach an element with the minimum possible gauge.

The Syntactic Rule Set

The study focuses on a robust set of rules commonly used in query optimization:

  • Associativity and Commutativity (A, C): Allowing the reordering and regrouping of AND and OR operations.
  • Quantifier Movement ($P\downarrow, S\downarrow, M$): Rules for pushing quantifiers down into subformulas or removing redundant quantifiers that do not bind any variables.
  • Renaming (N): Standardizing variable names to prevent clashes and simplify the formula structure.

The Two-Stage Optimization Algorithm

The paper proposes a definitive two-stage algorithm to achieve width minimization:

  1. Normalization: The algorithm first applies a set of "pushdown," "splitdown," and "removal" rules. The authors prove that this system is both convergent (it consistently reaches a unique normal form) and monotone (the width of the formula never increases during these transformations).
  2. Structural Mapping: Once the formula is in its normal form, the problem shifts from logic to graph theory. The authors demonstrate that for positive first-order formulas, minimizing width is equivalent to finding a minimum-width tree decomposition of the formula's representative hypergraph.

Key Insights: Bridging Logic and Graph Theory

A central contribution of this work is the formal link established between logical complexity and structural graph theory. The authors prove that for Boolean conjunctive queries, the minimum width obtainable through their defined rule set is exactly equal to the treewidth of the query's hypergraph plus one.

Furthermore, the paper proves that "standardizing" a formula—renaming variables to ensure uniqueness—is a safe initial step. This standardization does not prune the path to the optimal width, significantly narrowing the search space for the algorithm. While calculating the exact treewidth of a graph is an NP-hard problem, the remainder of the rewriting algorithm operates in polynomial time. This implies that with access to an efficient treewidth solver (an "oracle"), query optimization becomes highly tractable.

Results and Complexity

The primary result of the research is encapsulated in Theorem 4.1, which asserts the existence of an algorithm capable of computing the minimum possible width for any positive first-order formula within the defined rule set.

Key findings include:

  • Efficiency: The algorithm is polynomial-time relative to the treewidth oracle.
  • Fixed-Parameter Tractability: The results suggest that for classes of formulas that can be rewritten into a bounded width, query evaluation is fixed-parameter tractable (FPT), providing a clear path for optimizing complex database queries.

Limitations and Future Directions

The current research focuses on positive logic (formulas without negation). While the authors suggest that general FO logic can be handled by converting formulas to Negation Normal Form (NNF), this remains an area for further formalization.

Additionally, the rule set intentionally excludes the distributive law (e.g., $A \land (B \lor C) \equiv (A \land B) \lor (A \land C)$). Including distributivity introduces the risk of exponential "blow-up" in formula size, which would negate the polynomial-time guarantees of the current algorithm. Future work will likely explore how to incorporate distributivity or other more complex rules without sacrificing computational efficiency.

Summary updated

BibTeX
@inproceedings{chen2024optimally,
  author = {Hubie Chen and Stefan Mengel},
  title = {{Optimally Rewriting Formulas and Database Queries: A Confluence of Term Rewriting, Structural Decomposition, and Complexity}},
  booktitle = {ICDT},
  year = {2024},
  doi = {10.4230/lipics.icdt.2024.16},
}
Figure