SlowGuess/ACL_2020_OCR huggingface dataset:
RAT-SQL: Relation-Aware Schema Encoding and Linking for Text-to-SQL Parsing
Bailin Wang*†  
University of Edinburgh  
bailin.wang@ed.ac.uk
Richard Shin*†  
UC Berkeley  
ricshin@cs.berkeley.edu
Xiaodong Liu Oleksandr Polozov Matthew Richardson  
Microsoft Research, Redmond  
{xiaodl, polozov, matri}@microsoft.com
When translating natural language questions into SQL queries to answer questions from a database, contemporary semantic parsing models struggle to generalize to unseen database schemas. The generalization challenge lies in (a) encoding the database relations in an accessible way for the semantic parser, and (b) modeling alignment between database columns and their mentions in a given query. We present a unified framework, based on the relation-aware self-attention mechanism, to address schema encoding, schema linking, and feature representation within a text-to-SQL encoder. On the challenging Spider dataset this framework boosts the exact match accuracy to  $57.2\%$ , surpassing its best counterparts by  $8.7\%$  absolute improvement. Further augmented with BERT, it achieves the new state-of-the-art performance of  $65.6\%$  on the Spider leaderboard. In addition, we observe qualitative improvements in the model's understanding of schema linking and alignment. Our implementation will be open-sourced at https://github.com/Microsoft/rat-sql.
The ability to effectively query databases with natural language (NL) unlocks the power of large datasets to the vast majority of users who are not proficient in query languages. As such, a large body of research has focused on the task of translating NL questions into SQL queries that existing database software can execute.
The development of large annotated datasets of questions and the corresponding SQL queries has catalyzed progress in the field. In contrast to prior semantic parsing datasets (Finegan-Dollak et al.,
2018), new tasks such as MongoDB (Zhong et al., 2017) and Spider (Yu et al., 2018b) pose the real-life challenge of generalization to unseen database schemas. Every query is conditioned on a multitable database schema, and the databases do not overlap between the train and test sets.
Schema generalization is challenging for three interconnected reasons. First, any text-to-SQL parsing model must encode the schema into representations suitable for decoding a SQL query that might involve the given columns or tables. Second, these representations should encode all the information about the schema such as its column types, foreign key relations, and primary keys used for database joins. Finally, the model must recognize NL used to refer to columns and tables, which might differ from the referential language seen in training. The latter challenge is known as schema linking - aligning entity references in the question to the intended schema columns or tables.
While the question of schema encoding has been studied in recent literature (Bogin et al., 2019a), schema linking has been relatively less explored. Consider the example in Figure 1. It illustrates the challenge of ambiguity in linking: while "model" in the question refers to car_names.model rather than model_list.model, "cars" actually refers to both cars_data and car_names (but not car_makers) for the purpose of table joining. To resolve the column/table references properly, the semantic parser must take into account both the known schema relations (e.g. foreign keys) and the question context.
Prior work (Bogin et al., 2019a) addressed the schema representation problem by encoding the directed graph of foreign key relations in the schema with a graph neural network (GNN). While effective, this approach has two important shortcomings. First, it does not contextualize schema encoding with the question, thus making reasoning about
*Equal contribution. Order decided by a coin toss.† Work done during an internship at Microsoft Research.‡ Work done while partly affiliated with Microsoft Research. Now at Microsoft: ricshin@microsoft.com.
Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics, pages 7567-7578 July 5 - 10, 2020. ©2020 Association for Computational Linguistics
Figure 1: A challenging text-to-SQL task from the Spider dataset.
schema linking difficult after both the column representations and question word representations are built. Second, it limits information propagation during schema encoding to the predefined graph of foreign key relations. The advent of self-attentional mechanisms in NLP (Vaswani et al., 2017) shows that global reasoning is crucial to effective representations of relational structures. However, we would like any global reasoning to still take into account the aforementioned schema relations.
In this work, we present a unified framework, called RAT-SQL, for encoding relational structure in the database schema and a given question. It uses relation-aware self-attention to combine global reasoning over the schema entities and question words with structured reasoning over predefined schema relations. We then apply RAT-SQL to the problems of schema encoding and schema linking. As a result, we obtain  $57.2\%$  exact match accuracy on the Spider test set. At the time of writing, this result is the state of the art among models unaugmented with pretrained BERT embeddings – and further reaches to the overall state of the art  $(65.6\%)$  when RAT-SQL is augmented with BERT. In addition, we experimentally demonstrate that RAT-SQL enables the model to build more accurate internal representations of the question's true alignment with schema columns and tables.
Semantic parsing of NL to SQL recently surged in popularity thanks to the creation of two new multi-table datasets with the challenge of schema generalization – WikiSQL (Zhong et al., 2017) and Spider (Yu et al., 2018b). Schema encoding is not as challenging in WikiSQL as in Spider because it lacks multi-table relations. Schema linking is relevant for both tasks but also more challenging in Spider due to the richer NL expressiveness and less restricted SQL grammar observed in it. The state of the art semantic parser on WikiSQL (He et al.,
2019) achieves a test set accuracy of  $91.8\%$ , significantly higher than the state of the art on Spider.
The recent state-of-the-art models evaluated on Spider use various attentional architectures for question-schema encoding and AST-based structural architectures for query decoding. IRNet (Guo et al., 2019) encodes the question and schema separately with LSTM and self-attention respectively, augmenting them with custom type vectors for schema linking. They further use the AST-based decoder of Yin and Neubig (2017) to decode a query in an intermediate representation (IR) that exhibits higher-level abstractions than SQL. Bogan et al. (2019a) encode the schema with a GNN and a similar grammar-based decoder. Both works emphasize schema encoding and schema linking, but design separate featurization techniques to augment word vectors (as opposed to relations between words and columns) to resolve it. In contrast, the RAT-SQL framework provides a unified way to encode arbitrary relational information among inputs.
Concurrently with this work, Bogin et al. (2019b) published Global-GNN, a different approach to schema linking for Spider, which applies global reasoning between question words and schema columns/tables. Global reasoning is implemented by gating the GNN that encodes the schema using the question token representations. This differs from RAT-SQL in two important ways: (a) question word representations influence the schema representations but not vice versa, and (b) like in other GNN-based encoders, message propagation is limited to the schema-induced edges such as foreign key relations. In contrast, our relation-aware transformer mechanism allows encoding arbitrary relations between question words and schema elements explicitly, and these representations are computed jointly over all inputs using self-attention.
We use the same formulation of relation-aware self-attention as Shaw et al. (2018). However, they only apply it to sequences of words in the context of machine translation, and as such, their relation
$^{1}$ Relation-Aware Transformer.
7568
types only encode the relative distance between two words. We extend their work and show that relation-aware self-attention can effectively encode more complex relationships within an unordered set of elements (in our case, columns and tables within a database schema as well as relations between the schema and the question). To the best of our knowledge, this is the first application of relation-aware self-attention to joint representation learning with both predefined and softly induced relations in the input structure. Hellendoorn et al. (2020) develop a similar model concurrently with this work, where they use relation-aware self-attention to encode data flow structure in source code embeddings.
Sun et al. (2018) use a heterogeneous graph of KB facts and relevant documents for open-domain question answering. The nodes of their graph are analogous to the database schema nodes in RAT-SQL, but RAT-SQL also incorporates the question in the same formalism to enable joint representation learning between the question and the schema.
3 Relation-Aware Self-Attention
First, we introduce relation-aware self-attention, a model for embedding semi-structured input sequences in a way that jointly encodes pre-existing relational structure in the input as well as induced "soft" relations between sequence elements in the same embedding. Our solutions to schema embedding and linking naturally arise as features implemented in this framework.
Consider a set of inputs  $X = \{\pmb{x}_i\}_{i=1}^n$  where  $\pmb{x}_i \in \mathbb{R}^{d_x}$ . In general, we consider it an unordered set, although  $\pmb{x}_i$  may be imbued with positional embeddings to add an explicit ordering relation. A self-attention encoder, or Transformer, introduced by Vaswani et al. (2017), is a stack of self-attention layers where each layer (consisting of  $H$  heads) transforms each  $\pmb{x}_i$  into  $\pmb{y}_i \in \mathbb{R}^{d_x}$  as follows:
$$
\begin{array}{l} e _ {i j} ^ {(h)} = \frac {\boldsymbol {x} _ {\boldsymbol {i}} W _ {Q} ^ {(h)} (\boldsymbol {x} _ {\boldsymbol {j}} W _ {K} ^ {(h)}) ^ {\top}}{\sqrt {d _ {z} / H}}; \quad \alpha_ {i j} ^ {(h)} = \operatorname * {s o f t m a x} _ {j} \left\{e _ {i j} ^ {(h)} \right\} \\ \boldsymbol {z} _ {i} ^ {(h)} = \sum_ {j = 1} ^ {n} \alpha_ {i j} ^ {(h)} \left(\boldsymbol {x} _ {\boldsymbol {j}} W _ {V} ^ {(h)}\right); \quad \boldsymbol {z} _ {i} = \operatorname {C o n c a t} \left(\boldsymbol {z} _ {i} ^ {(1)}, \dots , \boldsymbol {z} _ {i} ^ {(H)}\right) \\ \tilde {\boldsymbol {y}} _ {i} = \operatorname {L a y e r N o r m} \left(\boldsymbol {x} _ {i} + \boldsymbol {z} _ {i}\right) \\ \boldsymbol {y} _ {i} = \operatorname {L a y e r N o r m} \left(\tilde {\boldsymbol {y}} _ {i} + \mathrm {F C} (\operatorname {R e L U} \left(\mathrm {F C} \left(\tilde {\boldsymbol {y}} _ {i}\right)\right)\right) \tag {1} \\ \end{array}
$$
where FC is a fully-connected layer, LayerNorm is layer normalization (Ba et al., 2016),  $1 \leq h \leq H$ , and  $W_{Q}^{(h)}, W_{K}^{(h)}, W_{V}^{(h)} \in \mathbb{R}^{d_{x} \times (d_{x} / H)}$ .
One interpretation of the embeddings computed by a Transformer is that each head of each layer
computes a learned relation between all the input elements  $x_{i}$ , and the strength of this relation is encoded in the attention weights  $\alpha_{ij}^{(h)}$ . However, in many applications (including text-to-SQL parsing) we are aware of some preexisting relational features between the inputs, and would like to bias our encoder model toward them. This is straightforward for non-relational features (represented directly in each  $x_{i}$ ). We could limit the attention computation only to the "hard" edges where the preexisting relations are known to hold. This would make the model similar to a graph attention network (Veličković et al., 2018), and would also impede the Transformer's ability to learn new relations. Instead, RAT provides a way to communicate known relations to the encoder by adding their representations to the attention mechanism.
Shaw et al. (2018) describe a way to represent relative position information in a self-attention layer by changing Equation (1) as follows:
$$
\begin{array}{l} e _ {i j} ^ {(h)} = \frac {\boldsymbol {x} _ {i} W _ {Q} ^ {(h)} \left(\boldsymbol {x} _ {j} W _ {K} ^ {(h)} + \boldsymbol {r} _ {i j} ^ {K}\right) ^ {\top}}{\sqrt {d _ {z} / H}} \tag {2} \\ \boldsymbol {z} _ {i} ^ {(h)} = \sum_ {j = 1} ^ {n} \alpha_ {i j} ^ {(h)} (\boldsymbol {x _ {j}} W _ {V} ^ {(h)} + \boldsymbol {r} _ {i j} ^ {V}). \\ \end{array}
$$
Here the  $r_{ij}$  terms encode the known relationship between the two elements  $\pmb{x}_i$  and  $\pmb{x}_j$  in the input. While Shaw et al. used it exclusively for relative position representation, we show how to use the same framework to effectively bias the Transformer toward arbitrary relational information.
Consider  $R$  relational features, each a binary relation  $\mathcal{R}^{(s)}\subseteq X\times X$ $(1\leq s\leq R)$ . The RAT framework represents all the pre-existing features for each edge  $(i,j)$  as  $r_{ij}^K = r_{ij}^V =$ $\mathrm{Concat}\bigl (\pmb{\rho}_{ij}^{(1)},\dots,\pmb{\rho}_{ij}^{(R)}\bigr)$  where each  $\rho_{ij}^{(s)}$  is either a learned embedding for the relation  $\mathcal{R}^{(s)}$  if the relation holds for the corresponding edge (i.e. if  $(i,j)\in \mathcal{R}^{(s)})$ , or a zero vector of appropriate size. In the following section, we will describe the set of relations our RAT-SQL model uses to encode a given database schema.
4 RAT-SQL
We now describe the RAT-SQL framework and its application to the problems of schema encoding and linking. First, we formally define the text-to-SQL semantic parsing problem and its components. In the rest of the section, we present our implementation of schema linking in the RAT framework.
7569
Type of x	Type of y	Edge label	Description
Column	Column	SAME-TABLE	x and y belong to the same table.
FOREIGN-KEY-COL-F	x is a foreign key for y.
FOREIGN-KEY-COL-R	y is a foreign key for x.
Column	Table	PRIMARY-KEY-F	x is the primary key of y.
BELONGS-TO-F	x is a column of y (but not the primary key).
Table	Column	PRIMARY-KEY-R	y is the primary key of x.
BELONGS-TO-R	y is a column of x (but not the primary key).
Table	Table	FOREIGN-KEY-TAB-F	Table x has a foreign key column in y.
FOREIGN-KEY-TAB-R	Same as above, but x and y are reversed.
FOREIGN-KEY-TAB-B	x and y have foreign keys in both directions.
Table 1: Description of edge types present in the directed graph  $\mathcal{G}$  created to represent the schema. An edge exists from source node  $x\in S$  to target node  $y\in S$  if the pair fulfills one of the descriptions listed in the table, with the corresponding label. Otherwise, no edge exists from  $x$  to  $y$ .
Figure 2: An illustration of an example schema as a graph  $\mathcal{G}$ . We do not depict all the edges and label types of Table 1 to reduce clutter.
Given a natural language question  $Q$  and a schema  $\mathcal{S} = \langle \mathcal{C},\mathcal{T}\rangle$  for a relational database, our goal is to generate the corresponding SQL  $P$ . Here the question  $Q = q_{1}\ldots q_{|Q|}$  is a sequence of words, and the schema consists of columns  $\mathcal{C} = \{c_1,\dots ,c_{|\mathcal{C}|}\}$  and tables  $\mathcal{T} = \{t_1,\dots ,t_{|\mathcal{T}|}\}$ . Each column name  $c_{i}$  contains words  $c_{i,1},\ldots ,c_{i,|c_i|}$  and each table name  $t_i$  contains words  $t_{i,1},\ldots ,t_{i,|t_i|}$ . The desired program  $P$  is represented as an abstract syntax tree  $T$  in the context-free grammar of SQL.
Some columns in the schema are primary keys, used for uniquely indexing the corresponding table, and some are foreign keys, used to reference a primary key column in a different table. In addition, each column has a type  $\tau \in \{\text{number}, \text{text}\}$ .
Formally, we represent the database schema as a directed graph  $\mathcal{G} = \langle \mathcal{V},\mathcal{E}\rangle$ . Its nodes  $\mathcal{V} = \mathcal{C}\cup \mathcal{T}$  are the columns and tables of the schema, each labeled with the words in its name (for columns, we pretend their type  $\tau$  to the label). Its edges  $\mathcal{E}$  are defined by the pre-existing database relations, described in Table 1. Figure 2 illustrates an example graph (with a subset of actual edges and labels).
Figure 3: One RAT layer in the schema encoder.
While  $\mathcal{G}$  holds all the known information about the schema, it is insufficient for appropriately encoding a previously unseen schema in the context of the question  $Q$ . We would like our representations of the schema  $\mathcal{S}$  and the question  $Q$  to be joint, in particular for modeling the alignment between them. Thus, we also define the question-contextualized schema graph  $\mathcal{G}_Q = \langle \mathcal{V}_Q, \mathcal{E}_Q \rangle$  where  $\mathcal{V}_Q = \mathcal{V} \cup Q = \mathcal{C} \cup \mathcal{T} \cup Q$  includes nodes for the question words (each labeled with a corresponding word), and  $\mathcal{E}_Q = \mathcal{E} \cup \mathcal{E}_{Q \leftrightarrow S}$  are the schema edges  $\mathcal{E}$  extended with additional special relations between the question words and schema members, detailed in the rest of this section.
For modeling text-to-SQL generation, we adopt the encoder-decoder framework. Given the input as a graph  $\mathcal{G}_Q$ , the encoder  $f_{\mathrm{enc}}$  embeds it into joint representations  $\pmb{c}_i$ ,  $\pmb{t}_i$ ,  $\pmb{q}_i$  for each column  $c_i \in \mathcal{C}$ , table  $t_i \in \mathcal{T}$ , and question word  $q \in Q$  respectively. The decoder  $f_{\mathrm{dec}}$  then uses them to compute a distribution  $\operatorname*{Pr}(P \mid \mathcal{G}_Q)$  over the SQL programs.
4.2 Relation-Aware Input Encoding
Following the state-of-the-art NLP literature, our encoder first obtains the initial representations  $c_{i}^{\mathrm{init}}$ ,  $t_{i}^{\mathrm{init}}$  for every node of  $\mathcal{G}$  by (a) retrieving a pre
trained Glove embedding (Pennington et al., 2014) for each word, and (b) processing the embeddings in each multi-word label with a bidirectional LSTM (BiLSTM) (Hochreiter and Schmidhuber, 1997). It also runs a separate BiLSTM over the question  $Q$  to obtain initial word representations  $q_{i}^{\mathrm{init}}$ .
The initial representations  $c_{i}^{\mathrm{init}}$ ,  $t_{i}^{\mathrm{init}}$ , and  $q_{i}^{\mathrm{init}}$  are independent of each other and devoid of any relational information known to hold in  $\mathcal{E}_Q$ . To produce joint representations for the entire input graph  $\mathcal{G}_Q$ , we use the relation-aware self-attention mechanism (Section 3). Its input  $X$  is the set of all the node representations in  $\mathcal{G}_Q$ :
$$
X = (c _ {1} ^ {\mathrm {i n i t}}, \dots , c _ {| \mathcal {C} |} ^ {\mathrm {i n i t}}, t _ {1} ^ {\mathrm {i n i t}}, \dots , t _ {| \mathcal {T} |} ^ {\mathrm {i n i t}}, q _ {1} ^ {\mathrm {i n i t}}, \dots , q _ {| Q |} ^ {\mathrm {i n i t}}).
$$
The encoder  $f_{\mathrm{enc}}$  applies a stack of  $N$  relation-aware self-attention layers to  $X$ , with separate weight matrices in each layer. The final representations  $\pmb{c}_i, \pmb{t}_i, \pmb{q}_i$  produced by the  $N^{\mathrm{th}}$  layer constitute the output of the whole encoder.
Alternatively, we also consider pre-trained BERT (Devlin et al., 2019) embeddings to obtain the initial representations. Following (Huang et al., 2019; Zhang et al., 2019), we feed  $X$  to the BERT and use the last hidden states as the initial representations before proceeding with the RAT layers.
Importantly, as detailed in Section 3, every RAT layer uses self-attention between all elements of the input graph  $\mathcal{G}_Q$  to compute new contextual representations of question words and schema members. However, this self-attention is biased toward some pre-defined relations using the edge vectors  $r_{ij}^{K}, r_{ij}^{V}$  in each layer. We define the set of used relation types in a way that directly addresses the challenges of schema embedding and linking. Occurrences of these relations between the question and the schema constitute the edges  $\mathcal{E}_{Q \leftrightarrow S}$ . Most of these relation types address schema linking (Section 4.3); we also add some auxiliary edges to aid schema encoding (see Appendix A).
4.3 Schema Linking
Schema linking relations in  $\mathcal{E}_{Q\leftrightarrow S}$  aid the model with aligning column/table references in the question to the corresponding schema columns/tables. This alignment is implicitly defined by two kinds of information in the input: matching names and matching values, which we detail in order below.
Name-Based Linking Name-based linking refers to exact or partial occurrences of the column/table names in the question, such as the occurrences of "cylinders" and "cars" in the question in Figure 1. Textual matches are the most explicit evidence of question-schema alignment and as such, one might expect them to be directly beneficial to the encoder. However, in all our experiments the representations produced by vanilla self-attention were insensitive to textual matches even though their initial representations were identical. Brunner et al. (2020) suggest that representations produced by Transformers mix the information from different positions and cease to be directly interpretable after  $2+$  layers, which might explain our observations. Thus, to remedy this phenomenon, we explicitly encode name-based linking using RAT relations.
Specifically, for all n-grams of length 1 to 5 in the question, we determine (1) whether it exactly matches the name of a column/table (exact match); or (2) whether the n-gram is a subsequence of the name of a column/table (partial match). Then, for every  $(i,j)$  where  $x_{i}\in Q$ ,  $x_{j}\in S$  (or vice versa), we set  $r_{ij}\in \mathcal{E}_{Q\leftrightarrow S}$  to QUESTION-COLUMN-M, QUESTION-TABLE-M, COLUMN-QUESTION-M or TABLE-QUESTION-M depending on the type of  $x_{i}$  and  $x_{j}$ . Here M is one of EXACTMATCH, PARTIALMATCH, or NOMATCH.
Value-Based Linking Question-schema alignment also occurs when the question mentions any values that occur in the database and consequently participate in the desired SQL, such as "4" in Figure 1. While this example makes the alignment explicit by mentioning the column name "cylinders", many real-world questions do not. Thus, linking a value to the corresponding column requires background knowledge.
The database itself is the most comprehensive and readily available source of knowledge about possible values, but also the most challenging to process in an end-to-end model because of the privacy and speed impact. However, the RAT framework allows us to outsource this processing to the database engine to augment  $\mathcal{G}_Q$  with potential value-based linking without exposing the model itself to the data. Specifically, we add a new COLUMN-VALUE relation between any word  $q_{i}$  and column name  $c_{j}$  s.t.  $q_{i}$  occurs as a value
2In this case, the initial representations  $c_{i}^{\mathrm{init}}$ ,  $t_{i}^{\mathrm{init}}$ ,  $q_{i}^{\mathrm{init}}$  are not strictly independent although still yet uninfluenced by  $\mathcal{E}$ .
This procedure matches that of Guo et al. (2019), but we use the matching information differently in RAT.
7571
(or a full word within a value) of  $c_{j}$ . This simple approach drastically improves the performance of RAT-SQL (see Section 5). It also directly addresses the aforementioned DB challenges: (a) the model is never exposed to database content that does not occur in the question, (b) word matches are retrieved quickly via DB indices & textual search.