Learning to Generate Structured Queries from Natural Language with Indirect Supervision

Ziwei Bai, Bo Yu, Bowen Wu, Zhuoran Wang, Baoxun Wang

TL;DR

This paper introduces SQLGen, a framework that learns to translate natural language questions into SQL queries using only the answers as supervision, rather than the queries themselves. By leveraging reinforcement learning with a novel "compound point-wise reward" system, the authors eliminate the need for expensive, expert-level SQL annotations while outperforming traditional supervised baselines.

What Problem Does This Paper Address?

Translating natural language (NL) into Structured Query Language (SQL) is the "holy grail" for making databases accessible to non-technical users. However, training these models usually requires large datasets of NL-SQL pairs.

The problem? SQL annotation is hard. It requires experts who understand both the database schema and complex SQL syntax. On the other hand, question-answer (QA) pairs—like those found on forums or through simple user interactions—are abundant. The authors aim to bridge this gap by using indirect supervision: training a model to generate a query that, when executed, simply returns the correct answer.

Methods

The authors propose SQLGen, an end-to-end neural model built on three main pillars:

  1. COPYNET Architecture: The model uses an encoder-decoder structure (Bi-GRU) equipped with a "copy mechanism." Since SQL queries often contain specific values from the question (e.g., a movie title or a year), the model can choose to either generate a keyword from a fixed vocabulary or copy a word directly from the input text.
  2. Grammar Masking: To prevent the model from generating gibberish, they implement a masking mechanism based on SQL grammar. If the previous word was FROM, the mask ensures the next word must be a table name, significantly narrowing the search space.
  3. Compound Point-wise Rewards: This is the core innovation. Standard Reinforcement Learning (RL) often gives a single "scalar" reward for the whole sequence (e.g., +1 if the answer is right, 0 if not). SQLGen uses point-wise rewards, assigning specific values to individual tokens:
  4. Coverage Reward: Rewards the model for correctly copying attribute values from the question and using the right number of operators.
  5. Execution Reward: Executes the generated query. If the final result matches the ground-truth answer, the model is rewarded. If not, it executes sub-queries to identify which specific parts of the WHERE clause were likely correct or incorrect.

Datasets

The model was evaluated on three datasets, each containing roughly 10,000 QA pairs:

  • Movie-Chinese: A new dataset collected from Baidu Zhidao (QA community) and Douban (movie database). It is particularly challenging because 78% of the questions require joining multiple tables.
  • Academic: A synthetic English dataset based on records of papers, researchers, and conferences.
  • Movie (English): A synthetic English dataset based on IMDB data.

Key Insights and Ideas

  • The Credit Assignment Solution: In sequence generation, it’s often hard to tell which word caused a failure. By breaking the reward down into "Coverage" (did I pick the right data?) and "Execution" (did the logic work?), the model gets much clearer feedback during training.
  • Flexibility Over Rigidity: Interestingly, SQLGen outperformed the supervised baseline (CopyNet-SL). The authors suggest this is because supervised models penalize the model for generating a "correct" query that simply uses a different join order or condition sequence than the human annotator. SQLGen only cares if the result is right, allowing for more flexible learning.
  • The Multi-Table Challenge: Unlike the popular WikiSQL benchmark, which focuses on single tables, this work tackles multi-table joins, which are much more representative of real-world database usage.

Key Results

  • Accuracy: SQLGen achieved an accuracy of 59.8% to 70.0% across the three datasets, consistently beating the supervised CopyNet-SL (which hovered between 51% and 62%).
  • RL Baselines: It crushed standard RL approaches (like Seq2Seq-RL), which often failed to learn anything (0% accuracy) on the English datasets due to the massive search space.
  • Pre-training Boost: When the model was pre-trained on a small amount of annotated data (SQLGen-Pretrain), accuracy jumped significantly, reaching over 80% on the Movie-Chinese dataset.

Limitations and Future Work

  • Redundancy: The model has a high "redundancy" rate (68-75%). Because it is only rewarded for the final answer, it often learns to join extra, unnecessary tables just to be "safe," as long as they don't break the final result.
  • Simple Logic: The current version is limited to the = (equality) operator. The authors acknowledge that handling more complex operators (like >, <, or LIKE) and aggregations (like SUM or AVERAGE) is a necessary next step.
  • Schema Complexity: While it handles multiple tables, the experiments were limited to databases with about three tables. Scaling this to massive enterprise schemas remains an open challenge.

Summary updated

BibTeX
@misc{bai2018learning,
  author = {Ziwei Bai and Bo Yu and Bowen Wu and Zhuoran Wang and Baoxun Wang},
  title = {{Learning to Generate Structured Queries from Natural Language with Indirect Supervision}},
  year = {2018},
  eprint = {1809.03195},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1809.03195},
}
Figure