Abstract
The sequence-to-sequence paradigm employed by neural text-to-SQL models typically performs token-level decoding and does not consider generating SQL hierarchically from a grammar. Grammar-based decoding has shown significant improvements for other semantic parsing tasks, but SQL and other general programming languages have complexities not present in logical formalisms that make writing hierarchical grammars difficult. We introduce techniques to handle these complexities, showing how to construct a schema-dependent grammar with minimal over-generation. We analyze these techniques on ATIS and Spider, two challenging text-to-SQL datasets, demonstrating that they yield 14--18% relative reductions in error.
Introduction
The task of Text-to-SQL—translating natural language questions into executable SQL queries—is a cornerstone of accessible data interaction. Traditional approaches have largely relied on sequence-to-sequence (seq2seq) models, which treat SQL as a flat string of tokens. However, this paradigm often fails to respect the inherent hierarchical structure of programming languages, leading to two primary issues: syntactic invalidity (e.g., mismatched parentheses) and semantic over-generation (e.g., referencing non-existent columns).
While grammar-based decoding has been successful in simpler semantic parsing tasks, applying it to SQL is challenging due to the language's complexity, including features like table aliases and deep schema dependencies. This paper proposes a type-constrained semantic parser that leverages a hierarchical grammar to ensure both syntactic and semantic correctness.
Methodology: Type-Constrained Semantic Parsing
The proposed model moves away from token-level prediction toward the generation of an abstract syntax tree (AST) through a sequence of production rules.
Model Architecture
The architecture consists of a Bi-LSTM encoder and an LSTM-based decoder enhanced with an attention mechanism. The encoder processes the input question alongside "link embeddings" that identify potential matches between words in the query and database entities. The decoder, instead of predicting tokens, predicts production rules from a predefined grammar.
Key Innovations
- Shallow Grammar-based Decoding: The model uses a "shallow" grammar tailored to the dataset. This design keeps derivation paths short, making it easier for the neural network to learn the mapping from natural language to SQL structure.
- Dynamic Schema Rules: To ground the model in the specific database, the grammar is dynamically updated for each query. Rules for table and column names are injected based on the schema, preventing the model from selecting invalid identifiers.
- Run-time Constraints: The decoder maintains a state to handle context-sensitive SQL features like JOINs. By tracking used tables, it can prune invalid grammar rules during the search process.
- Identifier Linking: A heuristic matching module links question tokens to database values, providing the model with critical information about the "type" of entities mentioned in the query.
Experimental Evaluation
The model was evaluated on two benchmarks: ATIS, a flight-interaction dataset, and Spider, a complex, cross-domain dataset requiring generalization to unseen schemas.
Results
The grammar-based approach demonstrated superior performance over existing state-of-the-art models. On the ATIS dataset, the model achieved a denotation accuracy of 73.7%, a 4.5% absolute improvement over previous best results. The gains were even more pronounced on the challenging Spider dataset.
As shown in the table above, the model achieved 33.8% accuracy on the Spider test set, representing a massive 14.1% absolute improvement over prior work using similar supervision. Ablation studies further highlighted that grounding the grammar in the database (value consistency) is vital, as its removal led to a nearly 10% drop in ATIS accuracy.
Discussion and Future Work
The results underscore that treating code as a tree rather than a string is fundamentally more robust. By narrowing the search space to only potentially valid queries through schema-aware grammars, the model avoids common pitfalls of seq2seq approaches.
However, challenges remain. The current reliance on heuristic string matching for entity linking is a bottleneck; a learned neural linking module could better handle ambiguity. Additionally, the sensitivity of performance to grammar structure suggests that automated grammar induction could be a fruitful area for future research. Currently, error analysis indicates that most failures stem from linking errors or confusion between similar tables, pointing toward the need for better disambiguation strategies.
Summary updated