Learned Graph Rewriting with Equality Saturation: A New Paradigm in Relational Query Rewrite and Beyond
George-Octavian Bărbulescu, Taiyi Wang, Zak Singh, Eiko Yoneki
TL;DR
This paper addresses the NP-hard problem of relational query rewrite by integrating equality saturation with graph reinforcement learning in a system named Aurora. By modeling equality saturation as a Markov decision process and employing a spatio-temporal RL agent that combines Graph Attention Networks with LSTMs, Aurora guides non-destructive e-graph expansion to construct many competitive query plans within practical memory and time budgets. The extraction of optimal plans is performed via ILP, with a cost model tied to end-to-end latency, enabling significant reductions in planning time and plan cost compared to state-of-the-art ES solvers and competitive performance against mainstream optimisers. The approach demonstrates strong runtime advantages on testbeds like Risinglight and PostgreSQL, and highlights RL’s pivotal role in navigating the combinatorial space of rewrite rules, offering a transferable paradigm for broader programming languages beyond SQL.
Abstract
Query rewrite systems perform graph substitutions using rewrite rules to generate optimal SQL query plans. Rewriting logical and physical relational query plans is proven to be an NP-hard sequential decision-making problem with a search space exponential in the number of rewrite rules. In this paper, we address the query rewrite problem by interleaving Equality Saturation and Graph Reinforcement Learning (RL). The proposed system, Aurora, rewrites relational queries by guiding Equality Saturation, a method from compiler literature to perform non-destructive graph rewriting, with a novel RL agent that embeds both the spatial structure of the query graph as well as the temporal dimension associated with the sequential construction of query plans. Our results show Graph Reinforcement Learning for non-destructive graph rewriting yields SQL plans orders of magnitude faster than existing equality saturation solvers, while also achieving competitive results against mainstream query optimisers.
