arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2603.16155cs.DBcs.SE
收藏

基于LLM的方言无关SQL解析

Dialect-Agnostic SQL Parsing via LLM-Based Segmentation

National University of Singapore(新加坡国立大学);Virgina Tech(弗吉尼亚理工大学)

Junwen An, Kabilan Mahathevan, Manuel Rigger

AI总结:

本文提出SQLFlex框架,结合语法解析与LLM分段技术,提升对多种SQL方言的解析能力,在SQL检查和测试用例缩减中均表现优异。

摘要:

SQL是一种广泛使用的数据查询语言,已催生各种SQL分析和重写工具。然而,由于SQL方言的多样性,这些工具在遇到不熟悉的方言特定语法时往往失效。尽管大型语言模型(LLM)在理解SQL查询方面表现出色,但其在处理层次结构和幻觉风险方面的固有局限性限制了其在解析中的直接应用。为了解决这些局限性,我们提出了SQLFlex,一种新的查询重写框架,结合基于语法的解析与基于LLM的分段技术,以稳健地解析多种SQL方言。我们的核心思想是将层次解析分解为顺序分段任务,这更符合LLM的优势,并通过验证检查提高输出可靠性。具体而言,SQLFlex使用短语级分段和表达级分段作为两种策略,将查询的不同层次元素分解开来。我们广泛评估了SQLFlex在实际应用场景和独立评估中的表现。在SQL检查中,SQLFlex在ANSI模式下的F1分数比SQLFluff高63.68%,而在方言特定模式下的表现与之相当。在测试用例缩减中,SQLFlex在简化率方面比SQLess高出多达10倍。在独立评估中,它能解析八个不同方言中91.55%至100%的查询,优于所有基线解析器。我们相信SQLFlex可以成为许多查询分析和重写应用场景的基础。

英文摘要:

SQL is a widely adopted language for querying data, which has led to the development of various SQL analysis and rewriting tools. However, due to the diversity of SQL dialects, such tools often fail when encountering unrecognized dialect-specific syntax. While Large Language Models (LLMs) have shown promise in understanding SQL queries, their inherent limitations in handling hierarchical structures and hallucination risks limit their direct applicability in parsing. To address these limitations, we propose SQLFlex, a novel query rewriting framework that integrates grammar-based parsing with LLM-based segmentation to parse diverse SQL dialects robustly. Our core idea is to decompose hierarchical parsing to sequential segmentation tasks, which better aligns with the strength of LLMs and improves output reliability through validation checks. Specifically, SQLFlex uses clause-level segmentation and expression-level segmentation as two strategies that decompose elements on different levels of a query. We extensively evaluated SQLFlex on both real-world use cases and in a standalone evaluation. In SQL linting, SQLFlex outperforms SQLFluff in ANSI mode by 63.68% in F1 score while matching its dialect-specific mode performance. In test-case reduction, SQLFlex outperforms SQLess by up to 10 times in simplification rate. In the standalone evaluation, it parses 91.55% to 100% of queries across eight distinct dialects, outperforming all baseline parsers. We believe SQLFlex can serve as a foundation for many query analysis and rewriting use cases.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑