Abstract
Machine translation is going through a radical revolution, driven by the explosive development of deep learning techniques using Convolutional Neural Network (CNN) and Recurrent Neural Network (RNN). In this paper, we consider a special case in machine translation problems, targeting to convert natural language into Structured Query Language (SQL) for data retrieval over relational database. Although generic CNN and RNN learn the grammar structure of SQL when trained with sufficient samples, the accuracy and training efficiency of the model could be dramatically improved, when the translation model is deeply integrated with the grammar rules of SQL. We present a new encoder-decoder framework, with a suite of new approaches, including new semantic features fed into the encoder, grammar-aware states injected into the memory of decoder, as well as recursive state management for sub-queries. These techniques help the neural network better focus on understanding semantics of operations in natural language and save the efforts on SQL grammar learning. The empirical evaluation on real world database and queries show that our approach outperform state-of-the-art solution by a significant margin.
Introduction
Translating natural language to SQL (NL2SQL) is significantly more complex than standard machine translation tasks. While a generic neural network can learn to mimic SQL syntax given enough data, it often fails in two critical ways: grammatical invalidity and schema inconsistency. A model might "hallucinate" syntax that violates SQL standards or attempt to query columns that do not exist in the specified table.
Earlier research often bypassed SQL generation entirely, mapping questions directly to data records. However, such methods do not scale to massive databases and break whenever the data is updated. This paper argues that the most robust solution is to generate reusable SQL by deeply integrating the rigid rules of the database language into the model's architecture.
Proposed Methodology
The framework builds upon a standard LSTM-based Encoder-Decoder architecture, enhanced with domain-specific intelligence at both ends to ensure syntactical and logical correctness.
Semantic-Aware Encoder
Instead of feeding raw word embeddings into the encoder, the model utilizes Conditional Random Fields (CRFs) to pre-label words. Words are tagged as derived columns, table references, or value expressions. These tags are appended as binary bits to the input vectors, assisting the model in recognizing which parts of the natural language sentence refer to specific database components.
Grammar-Aware Decoder and Dual-Masking
The decoder tracks its position within a Backus-Naur Form (BNF) representation of SQL-92. This allows for a dual-masking mechanism that filters out invalid word choices during generation:
- Short-term Dependency Masking: Based on SQL grammar rules. For example, after a
SELECTkeyword, the model is restricted to outputting a column name or a function. - Long-term Dependency Masking: Based on the database schema. If the model has already selected a specific table, the mask prevents it from selecting columns that belong to a different table.
Additionally, the framework employs Recursive State Management to handle nested sub-queries. By maintaining a stack of grammatical states, the model can correctly navigate into a sub-query and return to the main query context.
Experimental Evaluation
The framework was evaluated on three distinct datasets: Geo880 (US geography), Academic (MAS) (a complex 17-table Microsoft Academic Search dataset), and IMDB. For the Academic and IMDB sets, new SQL workloads were generated, and natural language descriptions were provided by volunteers to create a robust testing ground.
Key Results
The proposed framework significantly outperformed state-of-the-art baselines, including standard Neural Machine Translation (NMT) and Semantic Parsing with Feedback (SPF):
- Academic Dataset: The model achieved 47.9% query accuracy, outperforming the NMT baseline of 43.8%.
- IMDB Dataset: It reached an impressive 97.2% accuracy.
- Geo880: It achieved 82.8% accuracy, a notable improvement over the 75% achieved by standard NMT.
Ablation studies confirmed that the inclusion of "Grammar States" provided the single largest boost in performance, validating that structural constraints are essential for reliable SQL generation.
Conclusion
By treating SQL as a formal language with strict constraints rather than a creative writing task, this framework effectively prunes the search space for the neural network. While currently focused on SELECT and JOIN operations, the authors suggest this grammar-aware approach could be extended to other structured tasks, such as generating executable code from natural language descriptions. Future work will focus on expanding the scope of supported SQL operations and reducing the reliance on manual labeling for the semantic tagger.
Summary updated