Graph-Reward-SQL: Execution-Free Reinforcement Learning for Text-to-SQL via Graph Matching and Stepwise Reward

Han Weng, Ping Wu, Cui Longjie, Yi Zhan, Boyi Liu, Yuanfeng Song, Dun Zeng, Yaqin Yang, Qianru Zhang, Dong Huang, Xiaoming Yin, Yang Sun, Xing Chen2025 · DOI

Abstract

Reinforcement Learning (RL) has become a cornerstone for fine-tuning Large Language Models (LLMs) in code generation tasks. However, in the Text-to-SQL domain, existing reward models face a trilemma: execution-based rewards are slow and environment-dependent, LLM-based rewards are computationally expensive, and syntax-based rewards often fail to capture semantic equivalence. We present Graph-Reward-SQL, a framework that utilizes Graph Matching Networks (GMN) and stepwise rewards for subqueries. Our approach is execution-free, lightweight, and achieves state-of-the-art performance on major benchmarks like Spider and BIRD, while significantly reducing latency and memory overhead.

The Challenge of SQL Reward Modeling

Training LLMs for Text-to-SQL typically involves RL to align model outputs with desired query logic. Current reward mechanisms suffer from significant drawbacks:

  1. Execution-based (EX): Requires a live database, introducing high latency and environment complexity.
  2. LLM-based (BTRM): Uses massive models as judges, leading to high GPU memory consumption.
  3. Syntax-based (AST): Relies on structural matching, which often penalizes semantically correct but syntactically different queries (false negatives).

The goal of this research is to provide a reward signal that is semantically aware like an LLM, but as fast and lightweight as syntax matching.

Methodology: GMNScore and StepRTM

The Graph-Reward-SQL framework introduces two core components to address these challenges:

GMNScore: Semantic Outcome Reward

Instead of raw text or abstract syntax trees, GMNScore represents SQL queries as Relational Operator Trees (ROT). These trees capture the logical flow of relational algebra. A Graph Matching Network (GMN) then calculates the similarity between the generated query's ROT and the reference ROT. By learning graph embeddings, GMNScore can identify logical equivalence even when the syntax differs, effectively reducing false negatives and positives.

StepRTM: Stepwise Reward for Complex Queries

For complex queries involving Common Table Expressions (CTEs) or nested subqueries, providing a single reward at the end of generation is often insufficient. StepRTM decomposes the SQL into sub-logical units and provides intermediate rewards. This "partial credit" system helps the model learn multi-step reasoning and improves the interpretability of the generated code.

Overview of the Graph-Reward-SQL framework, illustrating the GMNScore and StepRTM components.

Experimental Evaluation

The framework was tested on the Spider and BIRD benchmarks using DeepSeek-Coder models (1.3B and 6.7B).

Performance and Efficiency

Graph-Reward-SQL demonstrates a superior balance of accuracy and resource efficiency. On the Spider benchmark, the method reached 81.62% Execution Accuracy, outperforming traditional execution-based RL and multi-agent systems.

Comparison of various reward models across modeling basis, granularity, time cost, and GPU usage.

Key efficiency metrics include:

  • Latency: GMNScore averages 0.023s per sample, a 47x speedup over execution-based rewards (1.088s).
  • Memory: The model requires only 83MB of GPU memory, compared to over 9GB for LLM-based reward models.
  • Reliability: It achieved a 97.62% AUC on Spider, showing high alignment with actual database execution results.

Key Insights

  • Relational Logic over Strings: Moving from text-matching to graph-matching allows the model to understand the underlying relational algebra, making it more robust to stylistic variations in SQL.
  • Dense Reward Signals: Stepwise rewards are crucial for long-form SQL generation. By rewarding correct sub-results, the model converges faster on complex tasks.
  • Execution-Free Training: Removing the dependency on a live database environment simplifies the training pipeline significantly without sacrificing the quality of the learned policy.

Limitations and Future Directions

While highly effective for SQL, the current ROT-based representation is domain-specific. Extending this "execution-free" graph matching approach to general-purpose languages like Python would require new graph schemas. Additionally, while GMNScore is robust, it still relies on the quality of reference queries provided during training. Future work will focus on generalizing these graph-based rewards to broader programming contexts.

Summary updated

BibTeX
@misc{weng2025graph,
  author = {Han Weng and Ping Wu and Cui Longjie and Yi Zhan and Boyi Liu and Yuanfeng Song and Dun Zeng and Yaqin Yang and Qianru Zhang and Dong Huang and Xiaoming Yin and Yang Sun and Xing Chen},
  title = {{Graph-Reward-SQL: Execution-Free Reinforcement Learning for Text-to-SQL via Graph Matching and Stepwise Reward}},
  year = {2025},
  doi = {10.18653/v1/2025.findings-emnlp.694},
}

Loading related content...

Figure