针对当前基于大语言模型(Large Language Model,LLM)的自然语言转结构化查询语句(Text-to-Structured Query Language,Text-to-SQL)方法,存在隐蔽语义错误难以闭环验证、执行准确率与执行效率难以兼顾的核心痛点,提出一种基于反向验证增强的SQL优化生成方法。该方法通过反向验证增强机制,以正向查询结果为已知条件构建双向语义验证闭环,能够有效识别符合SQL语法但语义偏离的隐蔽错误;同时设计查询范式演进机制,通过相似查询逻辑结构树的复用降低推理开销,优化验证流程的执行效率。实验结果表明,在Spider-test和BIRD Mini-dev数据集上,本文方法分别将各基线模型执行准确率最高提升5.92个百分点和8.60个百分点;其核心的查询范式演进机制,在100%命中率场景下可减少57%的反向验证耗时。基于该方法构建的股票信息查询智能Agent与智能选股平台,验证了方法的落地实用性与场景适配能力。
Abstract
Current large language model (LLM)-based Text-to-Structured Query Language (Text-to-SQL) methods face two core challenges: difficulty in closed-loop verification of hidden semantic errors, and the inability to balance execution accuracy and efficiency. To address these gaps, we propose a SQL optimization generation method based on enhanced reverse validation, with a reverse validation mechanism that constructs a bidirectional semantic verification loop using forward query results as known conditions to effectively identify hidden errors that conform to SQL syntax but deviate semantically, and a query paradigm evolution mechanism to reduce the extra inference overhead from reverse validation. Experimental results on the Spider-test and BIRD Mini-dev datasets show that the proposed method improves the execution accuracy of various baseline models by up to 5.92 and 8.60 percentage points, respectively. Its core query paradigm evolution mechanism can reduce reverse verification time by 57% under a 100% hit rate scenario. The intelligent agent for stock information queries and the intelligent stock selection platform built on this method have demonstrated practical applicability and scenario adaptability in real-world deployments.
随着关系型数据库在各行各业的广泛部署,如何降低非专业用户的数据查询门槛成为了人机交互领域的重要课题。Text-to-SQL(又称NL2SQL,即自然语言转结构化查询语言)技术,支持用户用日常语言直接与数据库交互,是实现数据普惠访问的核心技术。近年来,大语言模型(Large Language Model,LLM)凭借其强大的语义理解与代码生成能力,已逐渐取代传统的基于规则或小模型的方法,成为Text-to-SQL任务的主流实现范式。然而,在复杂业务场景(如金融科技、智能投顾等)下,面对海量数据与高度复杂的数据库模式,如何确保LLM生成SQL的准确性,仍是当前亟待突破的关键技术。
LIUX Y, SHENS Y, LIB Y, et al. A survey of text-to-SQL in the era of LLMs: Where are we, and where are we going [J]. IEEE Transactions on Knowledge and Data Engineering, 2025, 37(10): 5735-5754. DOI:10.1109/TKDE.2025.3592032 .
[2]
POURREZAM, LIH L, SUNR X, et al. CHASE-SQL: Multi-path reasoning and preference optimized candidate selection in text-to-SQL[EB/OL]. 2024: arXiv: 2410.01943.
[3]
LIUY F, ZHUY, GAOY Q, et al. XiYan-SQL: A novel multi-generator framework for text-to-SQL[J]. IEEE Transactions on Knowledge and Data Engineering, 2026, 38(4): 2474-2487. DOI:10.1109/TKDE.2026.3657851 .
[4]
POURREZAM, RAFIEID. DIN-SQL: Decomposed in-context learning of text-to-SQL with self-correction[C]//Advances in Neural Information Processing Systems 36. New Orleans: Neural Information Processing Systems Foundation, Inc. (NeurIPS), 2023: 36339-36348. DOI:10.52202/075280-1577 .
[5]
GUZ H, FANJ, TANGN, et al. Interleaving pre-trained language models and large language models for zero-shot NL2SQL generation[EB/OL]. 2023: arXiv: 2306.08891. DOI: 10.14778/3681954.3681960 .
[6]
LIUX Y, SHENS Y, LIB Y, et al. NL2SQL-BUGs: A benchmark for detecting semantic errors in NL2SQL translation[C]//Proceedings of the 31st ACM SIGKDD Conference on Knowledge Discovery and Data Mining V.2. Toronto ON Canada. New York: ACM, 2025: 5662-5673. DOI:10.1145/3711896.3737427 .
[7]
WANGX Z, WEIJ, SCHUURMANSD, et al. Self-consistency improves chain of thought reasoning in language models[EB/OL]. 2022: arXiv: 2203.11171.
[8]
GAOD W, WANGH B, LIY L, et al. Text-to-SQL empowered by large language models: A benchmark evaluation[J]. Proceedings of the VLDB Endowment, 2024, 17(5): 1132-1145. DOI:10.14778/3641204.3641221 .
[9]
ZHANGC, MAOY R, FANY J, et al. FinSQL: Model-agnostic LLMs-based text-to-SQL framework for financial analysis[C]//Companion of the 2024 International Conference on Management of Data. New York: ACM, 2024: 93-105. DOI:10.1145/3626246.3653375 .
[10]
RENZEM. The effect of sampling temperature on problem solving in large language models[C]//Findings of the Association for Computational Linguistics: EMNLP 2024. Cambridge: Association for Computational Linguistics, 2024: 7346-7356. DOI:10.18653/v1/2024.findings-emnlp.432 .
[11]
LIZ S, WANGX, ZHAOJ J, et al. PET-SQL: A prompt-enhanced two-round refinement of Text-to-SQL with cross-consistency[C]//Database Systems for Advanced Applications. Singapore: Springer, 2026: 193-208. DOI:10.1007/978-981-95-3830-0_13 .
[12]
TALAEIS, POURREZAM, CHANGY C, et al. CHESS: Contextual harnessing for efficient SQL synthesis[EB/OL]. 2024: arXiv: 2405.16755.
[13]
LIH Y, ZHANGJ, LIUH B, et al. CodeS: Towards building open-source language models for text-to-SQL[J]. Proceedings of the ACM on Management of Data, 2024, 2(3): 1-28. DOI:10.1145/3654930 .
[14]
XIANGY Z, ZHANGQ W, ZHANGX, et al. G³R: A graph-guided generate-and-rerank framework for complex and cross-domain text-to-SQL generation[C]//Findings of the Association for Computational Linguistics: ACL 2023. Cambridge: Association for Computational Linguistics, 2023: 338-352.10.18653/v1/2023.findings-acl.23. DOI: 10.18653/v1/2023.findings-acl.23 .
[15]
ZHANGK, LINX X, WANGY Z, et al. ReFSQL: A retrieval-augmentation framework for text-to-SQL generation[C]//Findings of the Association for Computational Linguistics: EMNLP 2023. Singapore: Association for Computational Linguistics, 2023: 664-673. DOI:10.18653/v1/2023.findings-emnlp.48 .
[16]
LIF, JAGADISHH V. NaLIR: An interactive natural language interface for querying relational databases[C]//Proceedings of the 2014 ACM SIGMOD International Conference on Management of Data. New York. ACM, 2014: 709-712. DOI:10.1145/2588555.2594519 .
[17]
YUT, ZHANGR, YANGK, et al. Spider: A large-scale human-labeled dataset for complex and cross-domain semantic parsing and text-to-SQL task[C]//Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing. Cambridge: Association for Computational Linguistics, 2018: 3911-3921. DOI:10.18653/v1/D18-1425 .
[18]
CHANGK, CHENGR, CHENHAOM, et al. Can LLM already serve as a database interface A big bench for large-scale database grounded text-to-SQLs[C]//Advances in Neural Information Processing Systems 36. New Orleans: Neural Information Processing Systems Foundation, Inc. (NeurIPS), 2023: 42330-42357. DOI:10.52202/075280-1835 .
[19]
YAOZ W, SUNG H, BORCHMANNL, et al. Arctic-Text2SQL-R1: Simple rewards, strong reasoning in Text-to-SQL[EB/OL]. 2025: arXiv: 2505.20315. DOI: 10.18653/v1/2026.findings-acl.1345 .
[20]
SHENGL, SHUAIX S. CSC-SQL: Corrective self-consistency in text-to-SQL via reinforcement learning[C]//Proceedings of the 14th International Joint Conference on Natural Language Processing and the 4th Conference of the Asia-Pacific Chapter of the Association for Computational Linguistics. Cambridge: Association for Computational Linguistics, 2025: 1473-1496. DOI:10.18653/v1/2025.findings-ijcnlp.91 .
[21]
LIB Y, ZHANGJ Y, FANJ, et al. Alpha-SQL: Zero-shot text-to-SQL using Monte Carlo tree search[EB/OL]. 2025: arXiv: 2502.17248.