Text-to-SQL: Natural Language to Database Queries
Module: Natural Language Processing | Difficulty: Advanced
Text-to-SQL Task
Schema Linking
Evaluation
| Model | Spider Accuracy |
|---|---|
| Seq2Seq | 35.2 |
| SyntaxSQLNet | 45.6 |
| BERT-SQL | 58.1 |
| GPT-3 | 72.3 |
import torch
import torch.nn as nn
class TextToSQL(nn.Module):
def __init__(self, encoder, decoder, schema_encoder):
super().__init__()
self.encoder = encoder
self.decoder = decoder
self.schema_encoder = schema_encoder
def forward(self, question_ids, schema_ids):
q_repr = self.encoder(question_ids)
s_repr = self.schema_encoder(schema_ids)
sql = self.decoder(q_repr, s_repr)
return sql
Research Insight: Schema linking is crucial for Text-to-SQL because the model must ground natural language mentions to database schema elements. Pre-trained language models improved Spider accuracy by 20+ points by better understanding natural language and SQL syntax.