SlowGuess/NAACL_2021_OCR huggingface dataset:
Torsten Scholak*, Raymond Li*, Dzmitry Bahdanau, Harm de Vries, Chris Pal Element AI, a ServiceNow company
Recent neural text-to-SQL models can effectively translate natural language questions to corresponding SQL queries on unseen databases. Working mostly on the Spider dataset, researchers have proposed increasingly sophisticated solutions to the problem. Contrary to this trend, in this paper we focus on simplifications. We begin by building DuoRAT, a re-implementation of the state-of-the-art RAT-SQL model that unlike RAT-SQL is using only relation-aware or vanilla transformers as the building blocks. We perform several ablation experiments using DuoRAT as the baseline model. Our experiments confirm the usefulness of some techniques and point out the redundancy of others, including structural SQL features and features that link the question with the schema<sup>1</sup>.
Language user interfaces to databases allow nonspecialists to retrieve and process information that might otherwise not be easily available to them. Much of the recent research in this area has focused on neural models that can generalize to new relational databases without any human intervention. Given a relational database schema (and often also content), such models translate the user's question directly into an SQL query (Zhong et al., 2017; Yu et al., 2018a; Boin et al., 2019). Such cross-database text-to-SQL research was spurred by the introduction of large datasets such as WikiSQL (Zhong et al., 2017) and Spider (Yu et al., 2018b) that feature utterance-query pairs for hundreds or even thousands of databases.
State-of-the-art text-to-SQL models employ many sophisticated techniques. These include, but are not limited to, grammar-constrained models
and recurrent neural networks with parent feeding (Yin and Neubig, 2018), intermediate meaning representations (Guo et al., 2019; Suhr et al., 2020), relation-aware attention (Wang et al., 2020), schema linking and table joining heuristics (Guo et al., 2019), slot filling (Choi et al., 2020) and re-ranking (Kelkar et al., 2020) models. The high complexity of these models raises the barrier of entry and can slow down text-to-SQL research.
In this work, we attempt to distill the essence of high-performing text-to-SQL systems. We start with a transformer-only reimplementation of the state-of-the-art RAT-SQL model (Wang et al., 2020). Importantly, our resulting DuoRAT model trains three times faster than RAT-SQL. We then systematically study how DuoRAT can be simplified without losing performance. Our ablation study confirms the usefulness of many but not all techniques employed in RAT-SQL. For example, we show that the benefits of explicit matching of question spans with the column or table names (name-based schema linking, NBSL) become marginal when a pretrained transformer (Devlin et al., 2018) is used to jointly encode the question and the schema. By contrast, we confirm the benefit of using a grammar to constrain the inference to only produce well-formed queries. These and other findings of our work bring much-needed insight of what enables higher performance in modern text-to-SQL models.
Our base model, DuoRAT, is a reimplementation of RAT-SQL (Wang et al., 2020). It is an encoder-decoder model with attention and a pointer-network copy mechanism, Fig. 1. Contrary to RAT-SQL, both the encoder and the decoder are relation-aware transformers (Shaw et al., 2018). The input is modelled as a labelled directed graph, where the nodes are the input tokens and the edges are the so-called relations, see below.
*Equal contribution, order was determined by a quantum random number draw.  
1Code available at https://github.com/ElementAI/duorat.
Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, pages 1313-1321 June 6-11, 2021. ©2021 Association for Computational Linguistics
Figure 1: The DuoRAT architecture. The encoder consists of a stack of BERT layers and several relation-aware self-attention layers, while the decoder is a relation-aware transformer with encoder cross-attention. The inputs to BERT are, from left to right, the question (red), each column type and name (blue), and each table name (yellow). Column and table representations are pooled (blue and yellow boxes), the question's representation is not (red boxes).
Relation-Aware Attention Compared to vanilla self-attention, relation-aware self-attention takes two additional tensor inputs, key relations  $r_{ij}^{(K)}$  and value relations  $r_{ij}^{(V)}$ , that amplify or diminish contributions in the scaled dot-product attention for each of the  $H$  attention heads:
$$
e _ {i j} ^ {(h)} = \frac {x _ {i} W _ {Q} ^ {(h)} \left(x _ {j} W _ {K} ^ {(h)} + r _ {i j} ^ {(K)}\right) ^ {\intercal}}{\sqrt {d _ {z} / H}} \tag {1}
$$
$$
\alpha_ {i j} ^ {(h)} = \frac {\exp e _ {i j} ^ {(h)}}{\sum_ {k = 1} ^ {n} \exp e _ {i k} ^ {(h)}} \tag {2}
$$
$$
z _ {i} ^ {(h)} = \sum_ {j = 1} ^ {n} \alpha_ {i j} ^ {(h)} \left(x _ {j} W _ {V} ^ {(h)} + r _ {i j} ^ {(V)}\right), \tag {3}
$$
where  $x_{i}\in \mathbb{R}^{d_{x}}$  is the  $i$ -th element of the input sequence,  $\alpha_{ij}^{(h)}$  is the attention weight coefficient for the  $h$ -th head, and  $z_{i}^{(h)}\in \mathbb{R}^{d_{z} / H}$  is the  $i$ -th output element. The indices  $i$  and  $j$  run from 1 to  $n$ , where  $n$  is the length of the sequence.  $W_{Q}^{(h)}, W_{K}^{(h)}$ , and  $W_{V}^{(h)}\in \mathbb{R}^{d_{x}\times d_{z} / H}$  are trainable weight matrices.  $r_{ij}^{(K)}\in \mathbb{R}^{d_{z} / H}$  and  $r_{ij}^{(V)}\in \mathbb{R}^{d_{z} / H}$  represent a directed labelled edge pointing from the  $i$ -th input  $x_{i}$  to the  $j$ -th input  $x_{j}$ . Following Shaw et al. (2018), we set  $r_{ij}^{(K)} = r_{ij}^{(V)} = r_{ij}$ . The relations  $r_{ij}$  are shared across all layers. Let  $R$  be the total number of relational edge labels. If the relation  $s\in \{1,\dots ,R\}$  exists between the  $i$ -th and  $j$ -th input, then we assign the  $s$ -th learned embedding  $r_{ij}^{(s)}$  to  $r_{ij}$ . Otherwise, we use padding.
Encoder The DuoRAT encoder is divided into two stages, a pretrained relation-unaware transformer stage followed by a relation-aware transformer stage that is trained from scratch. The first stage is initialized with BERT weights (Devlin
et al., 2018) and is fed embeddings of the question tokens, the table name tokens, the column name tokens, and one token for each column data type. We add [CLS] tokens between segments, cf. Fig. 1 for the input layout. The second stage has two inputs: an input sequence and the input relations corresponding to graph nodes and labelled edges, respectively. The input sequence is comprised of the BERT outputs for all question token positions and the [CLS] token position outputs for each table and each column. We use relational edge labels similar to those introduced by RAT-SQL. The labels are divided into three groups; (i) schemalinking relations, (ii) table-column relations, and (iii) foreign-key relations. (i) Schema-linking relations provide explicit alignment between the question and the schema. We distinguish between name-based schema linking (NBSL) and content-based schema linking (CBSL), where the former uses the names of tables and columns only and the latter uses the database content. An example for NBSL is when the question references a table by name, like "singer" in Fig. 1. CBSL identifies when the question references a value in a database column, e.g. the word "France" in Fig. 1. We use a common schema-linking heuristic where question n-grams are compared at the character level with names of tables and columns for NBSL and with the contents of column cells for CBSL. (ii) The table-column relations describe which columns belong to which tables, and which columns occur in the same table. Finally, (iii) the foreign-key relations indicate the foreign key constraints between columns. See Appendix A for a complete list of the encoder relations.
Decoder We have extended the TRANX framework for grammar-constrained sequence prediction
(Yin and Neubig, 2018) to relation-aware transformers. Like in the original framework, the decoder is restricted to generate only those sequences of grammar actions that encode a valid SQL abstract syntax tree (AST), see Appendix B. We consider two output grammars, one for complete SQL and another for SQL with underspecified FROM clause  $(\mathrm{SQL}^{\mathrm{UF}})$  (Suhr et al., 2020). In a  $\mathrm{SQL}^{\mathrm{UF}}$  query, the FROM clause is replaced by the UF clause that contains only the tables that were not mentioned in other clauses of the original SQL query. After decoding a  $\mathrm{SQL}^{\mathrm{UF}}$  query, we recover the FROM clause by adding tables from other clauses and joining them using the foreign-key relations.
RAT-SQL and the TRANX framework use a custom parent-feeding LSTM decoder where the LSTM is fed also its own state from a previous step at which the constructor of the current action's parent AST node was generated. By contrast, in DuoRAT's relation-aware transformer decoder, we experiment with relations that are derived from the structure of the SQL program code, see Appendix A for a list. The relations can bias the transformer decoder towards attending AST parent or sibling nodes, allowing for the model to get a sense of the AST's structure. However, it is unclear whether or not this is necessary in a model with self-attention.
The decoder is coupled to the encoder via a relation-aware memory attention mechanism. Here we use relations to indicate which tokens from the input were copied to the output, that is, either question tokens, tables, or columns, depending on the type of the literal that was produced.
For most of our experiments we use the Spider dataset (Yu et al., 2018b) and evaluate the predicted SQL with the exact-match (EM) accuracy from the official Spider evaluation script. The Spider training set contains 8,659 questions for 146 databases. The Spider development set contains 1,034 questions for 20 databases. We exclude the baseball11 questions from the training data because the schema of this database is too large. To compare DuoRAT to the models in the literature we evaluate it on the original development set as released on January 8, 2019. In all other experiments we use the corrected development set that was released on June 7, 2020.
We also test our Spider-trained models on several
System	EM (dev.)
RYANSQL (Choi et al., 2020)	70.6
RAT-SQL (Wang et al., 2020)	69.7
IRNet (Guo et al., 2019)	65.5
DuoRAT (original dev. set)	68.7 ± 0.7
DuoRAT (corrected dev. set)	69.9 ± 0.8
Table 1: Exact-match (EM) performance on the Spider development set. For DuoRAT we report results on the original and the corrected development set. For the other models only the original development set performance is available.
earlier single-database text-to-SQL datasets, see Section 3.4 for more details on that and Appendix C for details on the training procedure.
3.1 Comparing DuoRAT to Other Models
Table 1 compares DuoRAT's performance on the Spider development set to that of other state-of-the-art models. DuoRAT performs similarly to its close relative RAT-SQL and outperforms other recently proposed models. Importantly, DuoRAT training takes roughly two days compared to six days for RAT-SQL. We associate the difference in speed with replacing RAT-SQL's LSTM-with-parent-feeding decoder with a transformer.
3.2 Encoder Ablations
Schema Linking Prior work (Wang et al., 2020; Guo et al., 2019) attributes a high value to schema linking, that is, to the engineering of features that ground the user utterance in the database domain. However, this insight rests entirely on experiments without BERT. We find that, for our BERT-based DuoRAT model, name-based schema linking (NBSL) can be disabled with a negligible loss in performance (see Table 2) while content-based schema linking (CBSL) can not.
The result suggests that a BERT encoder fine-tunes to perform computation that makes heuristic NBSL redundant. To gain further understanding of whether or how BERT does this, we conduct an experiment in which the inputs to BERT are divided into two logical segments: the question and the schema. We shape the attention mask such that the question segment attends to the schema or the schema attends to the question, or both, or neither. The results are shown in Table 2. We observe that, for the best performance, BERT should
2Results taken from the Spider leaderboard at https://yale-lily.github.io/spider on October 19, 2020.
Model Variant	Exact Match (dev.)
CBSL	Q→S	S→Q	w NBSL	w/o NBSL
✓	✓	✓	69.9±0.8	68.6±1.0
✓	✓	67.5±0.6	66.7±1.0
✓	✓	64.3±0.4	63.2±0.7
✓	✓	64.4±0.9	64.5±0.8
✓	63.9±0.8	59.1±1.1
Table 2: Results with and without name-based (NBSL) or content-based schema-linking (CBSL) for various attention masks.  $\mathrm{Q} \rightarrow  \mathrm{S}$  : the question can attend to the schema.  $\mathrm{S} \rightarrow  \mathrm{Q}$  : the schema can attend to the question.
Model Variant	EM (dev.)
DuoRAT	69.9 ± 0.8
w/o AST relations	69.7 ± 0.9
w/o copied-from relations	69.2 ± 1.0
w/o any decoder relations	69.4 ± 1.1
w/o constraining during training	69.0 ± 1.1*
w/o any constraining	63.2 ± 0.9*
be jointly embedding the question and the schema. We can neither embed the question separately from the schema nor the schema separately from the question without substantial performance losses. Interestingly, once we cut all the attention connections between the question and the schema, explicit NBSL becomes essential. This confirms our hypothesis that joint BERT-based encoding of the question and the schema is the cause of the low importance of NBSL in our model.
Schema Structure Representation Various ways of encoding which columns belong to which table have been explored: Suhr et al. (2020) orders the schema elements such that each table name is followed by the names of the columns of that table, RAT-SQL (Wang et al., 2020) represents schema structure as table-column relations in the RAT encoder. Our experiments show that encoding the schema structure via encoder relations gives the best performance (first row of Table 4), and encoding it in the order of its elements (third row) is better than not encoding it at all (second row). Additional results can be found in Appendix D.
3.3 Decoder Ablations
Decoder Relations And Grammar-Based Constraining Table 3 shows results of ablation stud
Table 3: Decoder ablations. Middle: decoder relations. Bottom: grammar constraining at training or inference. *In each case one out of five jobs diverged during training and was removed from the ensemble.
Model Variant	EM (dev.)
[column] [table] + relations	69.6 ± 0.8
[columns] [table]	60.0 ± 0.7
[table[columns]]	65.8 ± 1.3
Table 4: Encoding of schema structure. Ordering of schema elements can be: (i) [column][table]: first all the column types and names, then all the table names. (ii) [table[columns]]: each table is followed by the columns of that table.
ies in which (i) different kinds of decoder relations were removed, in particular those that provide program structure information, and (ii) grammar-based constraining was deactivated during training and/or inference. The experiments provide the following insights: (i) A vanilla transformer decoder can be used without loss of performance. The relations that provide information about the AST and about which literals were copied from the input are not useful. We speculate that AST information can be more useful in deeply nested SQL expressions of which Spider contains only few. (ii) By contrast, grammar constraining at inference leads to significant performance improvements. Notably, training tends to be less stable when not using grammar constraints. The usefulness of grammar constraining can be explained by the fact that it reduces the output space and makes the decoder more data-efficient.
Output Format In this section, we examine the performance on the Spider dataset when outputting complete SQL and when outputting simplified SQL with underspecified FROM clause,  $\mathbf{SQL}^{\mathrm{UF}}$ . The results are reported in Table 6. Our first insight is that DuoRAT performance with and without  $\mathbf{SQL}^{\mathrm{UF}}$  is almost the same. Our second insight is that when the encoder does not have access to information about the foreign keys,  $\mathbf{SQL}^{\mathrm{UF}}$  brings a significant improvement. The best result is still achieved with a model that uses the foreign-key input relations.
3.4 Testing on Single-Database Datasets
A known issue of the Spider dataset is that the question wordings are unnaturally close to the respective queries (Suhr et al., 2020). To complement our studies on Spider, we perform additional experiments on single-database text-to-SQL datasets that are devoid of this issue. Suhr et al. (2020) propose a demanding cross-domain generalization evaluation whereby models are trained on Spider and
Dataset	DuoRAT + SQLUF	w/o CBSL	w/o SQLUF
GeoQuery	54.6 ± 3.9	49.2 ± 4.2	48.4 ± 4.9
Academic	31.4 ± 4.9	18.0 ± 3.9	10.3 ± 1.2
IMDB	38.5 ± 8.1	39.2 ± 5.6	20.0 ± 5.0
Yelp	33.5 ± 3.8	25.7 ± 4.7	27.8 ± 4.3
Table 5: Ablation results with the underspecified FROM (SQL $^{\text{UF}}$ ) approach on single-database text-to-SQL datasets. CBSL stands for content-based schema linking. Pink shading indicates statistically significant gap between the ablated model and the complete one.
Model Variant	Exact Match (dev.)
DuoRAT	69.9±0.8
w/o foreign-key inputs	67.6±0.7
+ SQLUF, w/o foreign-key inputs	69.0±0.8
+ SQLUF	70.5±1.2
tested on single-database datasets by comparing the execution results of the predicted and the gold queries. We follow this methodology and filter the datasets to only use those question-query pairs for which execution accuracy evaluation is appropriate (see Suhr et al. (2020) for filtering details). To focus on evaluating the query structure, we replace predicted string literals with the most similar ones from the gold query (details of this procedure can be found in Appendix E).
Of the 8 datasets that Suhr et al. (2020) consider we exclude ATIS, Advising, and Scholar for being too different from Spider and Restaurants for having just 27 examples after filtering. What remains are the SQL version of the GeoQuery dataset (Zelle and Mooney, 1996) as well as the small Academic, IMDB, and Yelp datasets (Finegan-Dollak et al., 2018). After filtering, these datasets are left with 532, 180, 107 and 54 examples, respectively. Note that these single-database datasets are partially contained in Spider. To avoid testing on training data, we train new models only on the about 7,000 examples produced by the Spider data collection effort.
In this round of analysis we focus on the impact of CBSL and the underspecified FROM clause (SQL $^{\text{UF}}$ ) technique (Suhr et al., 2020). We expect both methods to be especially useful for out-of-distribution generalization, despite the limited importance that Spider evaluation attributes to them. The results in Table 5 show that, in line with our intuition, CBSL and SQL $^{\text{UF}}$  bring performance gains on 2 and 3 out of 4 datasets, respectively.
Table 6: Results using  $\mathrm{SQL}^{\mathrm{UF}}$  and/or foreign-key inputs.
Dataset	DuoRAT + SQLUF	Suhr et al. (2020)
GeoQuery	54.59 ± 3.91	41.6
Academic	23.28 ± 2.93	8.2
IMDB	34.95 ± 5.66	24.6
Yelp	30.19 ± 3.27	19.8
Table 7: DuoRAT performance on single-database text-to-SQL datasets. The model is trained to predict  $\mathrm{SQL}^{\mathrm{UF}}$ , i.e. SQL with underspecified FROM clause.
To enable comparison with results by Suhr et al. (2020), we also report the results without literal replacement in Table 7. DuoRAT performs consistently better than the model by Suhr et al. (2020).
Our investigations have revealed several possible simplifications of relation-aware text-to-SQL transformer models. In particular, we have shown that a transformer decoder with vanilla self- and memory-attention is sufficient, and that heuristic schema linking based on table and/or column names brings only a marginal benefit. To the contrary, we confirm the importance of grammar-constrained decoding, relational schema representations, content-based schema linking. Looking forward, we believe that content-based schema-linking will remain important, while the impact of name-based schema linking will further decrease as the language models get bigger and absorb more data. This prediction is based on the fact that the mapping from an entity to the entity type that name-based schema linking effectively performs can be highly domain- and schema-specific. Last but not least, we have shown that predicting a more compact SQL version with an underspecified FROM clause improves the model's out-of-distribution performance, despite bearing little influence on the model's performance on Spider.
In future work, we will combine the successful simplifications from this paper to build the simplest yet high-performing text-to-SQL model. One promising direction for further simplification is to use a pretrained encoder-decoder pair as proposed in Raffel et al. (2020) and Lewis et al. (2019).
Ben Boin, Matt Gardner, and Jonathan Berant. 2019. Representing Schema Structure with Graph Neural Networks for Text-to-SQL Parsing. arXiv:1905.06241 [cs]. ArXiv:1905.06241.
DongHyun Choi, Myeong Cheol Shin, EungGyun Kim, and Dong Ryeol Shin. 2020. RYANSQL: Recursively Applying Sketch-based Slot Fillings for Complex Text-to-SQL in Cross-Domain Databases. arXiv:2004.03125 [cs]. ArXiv:2004.03125.
Jacob Devlin, Ming-Wei Chang, Kenton Lee, and Kristina Toutanova. 2018. BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. In Proceedings of the 2019 Conference of the North American Chapter of the Association for Computational Linguistics, NAACL 2019.
Catherine Finegan-Dollak, Jonathan K. Kummerfeld, Li Zhang, Karthik Ramanathan, Sesh Sadasivam, Rui Zhang, and Dragomir Radev. 2018. Improving Text-to-SQL Evaluation Methodology. arXiv:1806.09029 [cs]. ArXiv:1806.09029.
Jiaqi Guo, Zecheng Zhan, Yan Gao, Yan Xiao, Jian-Guang Lou, Ting Liu, and Dongmei Zhang. 2019. Towards complex text-to-SQL in cross-domain database with intermediate representation. In Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics, pages 4524-4535, Florence, Italy. Association for Computational Linguistics.
Amol Kelkar, Rohan Relan, Vaishali Bhardwaj, Saurabh Vaichal, and Peter Relan. 2020. Bertrand DR: Improving Text-to-SQL using a Discriminative Re-ranker. arXiv:2002.00557 [cs, stat]. ArXiv: 2002.00557.
Diederik P. Kingma and Jimmy Ba. 2015. Adam: A Method for Stochastic Optimization. In Proceedings of the 2015 International Conference on Learning Representations, ICLR 2015. ArXiv: 1412.6980.
Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Ves Stoyanov, and Luke Zettlemoyer. 2019. BART: Denoising Sequence-to-Sequence Pretraining for Natural Language Generation, Translation, and Comprehension. arXiv e-prints, page arXiv:1910.13461.
Colin Raffel, Noam Shazeer, Adam Roberts, Katherine Lee, Sharan Narang, Michael Matena, Yanqi Zhou, Wei Li, and Peter J. Liu. 2020. Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer. Journal of Machine Learning Research, 21(140):1-67.
Peter Shaw, Jakob Uszkoreit, and Ashish Vaswani. 2018. Self-Attention with Relative Position Representations. In Proceedings of the 2018 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies, Volume 2 (Short Papers), pages 464-468, New Orleans, Louisiana. Association for Computational Linguistics.