SQL-to-Text Generation with Graph-to-Sequence Model

Kun Xu, Lingfei Wu, Zhiguo Wang, Yansong Feng, Vadim Sheinin

Abstract

Previous work approaches the SQL-to-text generation task using vanilla Seq2Seq models, which may not fully capture the inherent graph-structured information in SQL query. In this paper, we first introduce a strategy to represent the SQL query as a directed graph and then employ a graph-to-sequence model to encode the global structure information into node embeddings. This model can effectively learn the correlation between the SQL query pattern and its interpretation. Experimental results on the WikiSQL dataset and Stackoverflow dataset show that our model significantly outperforms the Seq2Seq and Tree2Seq baselines, achieving the state-of-the-art performance.

Introduction

For non-experts, complex SQL queries can be difficult to parse and understand. The goal of SQL-to-text generation is to automatically produce human-like descriptions that explain the logic of a query. For instance, a query like SELECT company WHERE assets > val0 AND sales > val0 should ideally be translated to: "Which company has both market value and assets higher than val0?"

Existing approaches often fall short of this goal. Template-based methods are rigid and produce robotic text. Sequence-to-Sequence (Seq2Seq) models treat SQL like a simple linear sentence, ignoring the structural dependencies between tables, columns, and operators. While Tree-to-Sequence models attempt to capture some structure, they often lose critical "ancestor" context during the encoding process. This paper proposes a Graph-to-Sequence (Graph2Seq) model to address these limitations by treating SQL as a directed graph.

Methodology

The Graph2Seq architecture consists of three primary stages designed to preserve the structural integrity of the SQL query.

1. SQL-to-Graph Transformation

Instead of processing the SQL query as a string, it is transformed into a directed graph. Nodes represent clauses (e.g., SELECT, WHERE), columns (e.g., assets, sales), and operators (e.g., >, AND). Edges represent the logical relationships between these elements. For example, an AND operator node connects to the specific column nodes it modifies, explicitly mapping the query's logic.

Directed graph representation of a SQL query, capturing structural dependencies between clauses, columns, and operators.

2. Bidirectional Graph Encoder

The core innovation is the bidirectional graph encoder used to learn node embeddings. Unlike standard graph neural networks that may ignore edge direction, this model employs a bidirectional aggregator:

  • Forward Aggregator: Collects information from nodes that the current node points to.
  • Backward Aggregator: Collects information from nodes pointing at the current node.

By concatenating these perspectives over $K$ iterations (hops), each node learns about its global neighborhood. To synthesize these individual node meanings into a single graph vector, the model uses max-pooling across all nodes. This approach solves the "Ancestor Problem" found in tree models; the $K$-hop approach ensures every node can "see" its context in all directions, preserving the root context throughout the network.

3. Sequence Decoder

An RNN-based decoder takes the final graph vector and utilizes an attention mechanism to look back at specific nodes in the graph while generating the natural language sentence word-by-word.

Experimental Results

The model was evaluated on two major benchmarks: WikiSQL (87,726 pairs) and StackOverflow (32,337 pairs). The Graph2Seq model significantly outperformed all baselines across multiple metrics.

Performance comparison of Graph2Seq against various baselines on the WikiSQL dataset.

On the WikiSQL dataset, the Graph2Seq-PGE model achieved a BLEU-4 score of 38.97, a substantial improvement over the previous state-of-the-art Tree2Seq (26.67) and standard Seq2Seq (24.12). Human evaluation further confirmed these results, with the Graph2Seq model achieving a 79.2% correctness score, compared to just 62.1% for Seq2Seq. The model proved particularly effective at handling complex queries with multiple conditions where columns had implicit relationships.

Conclusion

By recognizing that SQL logic is inherently non-linear and graph-structured, the Graph2Seq model provides a more accurate and human-like interpretation of database queries. While current work focuses on SELECT and WHERE clauses, future research will aim to extend the graph construction strategy to handle more complex syntax such as JOIN, GROUP BY, and ORDER BY. Additionally, experiments with hop size suggest a "sweet spot" for global context (peaking at 6 hops), beyond which the structural signal may become diluted.

Summary updated

BibTeX
@inproceedings{xu2018sql,
  author = {Kun Xu and Lingfei Wu and Zhiguo Wang and Yansong Feng and Vadim Sheinin},
  title = {{SQL-to-Text Generation with Graph-to-Sequence Model}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2018},
  url = {https://openreview.net/forum?id=r1-_pbzd-r},
}
Figure