E3-Rewrite: Learning to Rewrite SQL for Executability, Equivalence, and Efficiency

Dongjie Xu, Weijie Shi, Yue Cui, Qingzhi Ma, Wenwen Xia, Ruiyuan Zhang, Shimin Di, Jia Zhu, Kai Zheng, Jiajie Xu


Abstract
SQL query rewriting reformulates queries into more efficient forms while preserving equivalence. Most existing methods rely on predefined rules that struggle to generalize beyond fixed query patterns and cannot express complex rewriting strategies. Large language models (LLMs) can reason over complex structures and generalize to diverse patterns, but their limited execution awareness and semantic grounding often result in non-equivalent or inefficient rewrites. To this end, we propose E3-Rewrite, an LLM-based framework that generates executable, equivalent, and efficient SQL rewrites. It features two key designs: (1) an execution-aware context construction that integrates query plans and retrieved examples to expose structural bottlenecks; and (2) a reinforcement learning optimization with executability, equivalence, and efficiency rewards, trained under a correctness-to-efficiency curriculum for stable convergence. Experiments on multiple benchmarks show that E3-Rewrite-Rewrite reduces execution time by up to 25.6% over leading baselines and delivers 7.3% more equivalent rewrites, extending SQL rewriting to query patterns previously beyond rule-based methods.
Anthology ID:
2026.emnlp-main.1493
Volume:
Proceedings of the 2026 Conference on Empirical Methods in Natural Language Processing
Month:
October
Year:
2026
Address:
Budapest, Hungary
Editors:
Hung-Yi Lee, Sunipa Dev, Jessy Li, Desmond Elliott
Venue:
EMNLP
SIG:
SIGDAT
Publisher:
Association for Computational Linguistics
Note:
Pages:
34121–34135
Language:
URL:
https://aclanthology.org/2026.emnlp-main.1493/
DOI:
Bibkey:
Cite (ACL):
Dongjie Xu, Weijie Shi, Yue Cui, Qingzhi Ma, Wenwen Xia, Ruiyuan Zhang, Shimin Di, Jia Zhu, Kai Zheng, and Jiajie Xu. 2026. E3-Rewrite: Learning to Rewrite SQL for Executability, Equivalence, and Efficiency. In Proceedings of the 2026 Conference on Empirical Methods in Natural Language Processing, pages 34121–34135, Budapest, Hungary. Association for Computational Linguistics.
Cite (Informal):
E3-Rewrite: Learning to Rewrite SQL for Executability, Equivalence, and Efficiency (Xu et al., EMNLP 2026)
Copy Citation:
PDF:
https://aclanthology.org/2026.emnlp-main.1493.pdf