Introduction
Bridging the gap between non-technical users and relational databases is a primary goal of Natural Language to SQL (NL2SQL) research. While SQL is the standard for data manipulation, its syntax remains a barrier for many. Current state-of-the-art models often excel at predicting the structural components of a query—such as SELECT, FROM, and JOIN clauses—but frequently falter when extracting specific literal values, or "terminals," from user prompts. For instance, a model might correctly identify the need to filter by country but fail to accurately extract the specific string "Romania" from the input. This paper presents a hybrid pipeline that addresses this "terminal" extraction problem by combining structural generation with machine reading comprehension.
Methodology
The proposed system utilizes a two-stage architecture designed to leverage the specialized strengths of different transformer models. The entire workflow, from user input to final output, is designed to ensure both structural integrity and contextual accuracy.
The Structural Architect: RAT-SQL
The core of the system is the Relation-Aware Transformer SQL (RAT-SQL) model. RAT-SQL is optimized to understand complex database schemas and the relationships between tables. In this pipeline, RAT-SQL generates the "skeleton" of the SQL query. To bypass the terminal extraction issue during this phase, the model inserts a placeholder string, 'terminal', wherever a specific filter value is required.
The Contextual Refiner: RoBERTa
To resolve the placeholders, the system integrates a RoBERTa model (Base SQuAD 2) trained for question-answering. The pipeline identifies the context of each 'terminal' placeholder and generates a natural language question (e.g., "What is the city name?"). RoBERTa then treats the user's original prompt as a reference text to find the most likely answer, effectively "patching" the missing details in the query.
Post-Processing and Logic
Final query refinement involves custom logic to handle SQL-specific operators like LIKE, BETWEEN, and LIMIT. Additionally, the word2number library is employed to ensure that numerical values expressed in text (e.g., "five") are correctly converted into integers for the executable SQL statement.
Experimental Setup
The system was developed and evaluated using the Spider dataset, a benchmark for cross-domain semantic parsing. Unlike simpler datasets, Spider includes 200 distinct databases and requires models to handle complex multi-table joins and nested logic, providing a rigorous test for generalization.
Results and Discussion
The hybrid RAT-SQL and RoBERTa configuration achieved an execution accuracy between 70% and 80% on complex multi-table tasks. A qualitative comparison was also conducted against ChatGPT to evaluate the nuances of the generated queries.
While ChatGPT often produced more "readable" SQL by utilizing table aliases and selecting only essential columns, the proposed system proved equally functional in terms of data retrieval. The modular approach of using a QA model as a "patch" for entity extraction demonstrates the efficacy of task-specific model synergy.
Limitations and Future Work
Despite its success, the system faces challenges with "subjective randomness," where minor variations in user phrasing can lead to inconsistent outputs. Furthermore, the RoBERTa component can struggle with queries containing repetitive subjects, occasionally mapping the same value to multiple predicates. Future iterations will focus on providing the QA model with schema-level awareness—such as data types and column annotations—to improve mapping precision and reduce context ambiguity.
Summary updated