Source-linked AI summary

C3: Zero-shot Text-to-SQL with ChatGPT

Xuemei Dong, Chao Zhang, Yuhang Ge, Yuren Mao, Yunjun Gao, lu Chen, Jinshu Lin, Dongfang Lou

arXiv:2307.07306v1cs.CLcs.AI

TL;DR

Text-to-SQL requires methods that avoid costly labeled pairs, while few-shot prompting can impose substantial token costs and zero-shot approaches may underperform. C3 uses ChatGPT with Clear Prompting, Calibration with Hints, and Consistent Output, achieving state-of-the-art zero-shot performance on Spider at 82.3% execution accuracy. The study systematically examines prompting, bias calibration, and output consistency in this setting.

  • Problem

    Text-to-SQL methods face expensive text-SQL pair collection, while few-shot prompting requires substantial tokens and existing zero-shot methods have inferior performance.

  • Method

    C3 is a ChatGPT-based zero-shot Text-to-SQL method combining Clear Prompting, Calibration with Hints, and Consistent Output for model input, bias, and output.

  • Results

    82.3% execution accuracy on Spider’s holdout test set makes C3 state-of-the-art among zero-shot Text-to-SQL methods.

  • Takeaways & Limitations

    C3 provides a systematic treatment of zero-shot Text-to-SQL from the perspectives of model input, model bias, and model output.

  • Takeaways & Limitations

    The study focuses on zero-shot prompting because adding in-context examples increases manual and API token costs.

Abstract

from arXiv · show

This paper proposes a ChatGPT-based zero-shot Text-to-SQL method, dubbed C3, which achieves 82.3\% in terms of execution accuracy on the holdout test set of Spider and becomes the state-of-the-art zero-shot Text-to-SQL method on the Spider Challenge. C3 consists of three key components: Clear Prompting (CP), Calibration with Hints (CH), and Consistent Output (CO), which are corresponding to the model input, model bias and model output respectively. It provides a systematic treatment for zero-shot Text-to-SQL. Extensive experiments have been conducted to verify the effectiveness and efficiency of our proposed method.

1 Introduction

Text-to-SQL methods face expensive data requirements, token costs, and performance limitations in few-shot and zero-shot settings. C3 addresses these issues with a ChatGPT-based zero-shot approach that uses three components and reaches 82.3% execution accuracy on Spider.

  • Fine-tuning-based Text-to-SQL methods require costly text-SQL training pairs and may overfit, reducing transferability.These constraints motivate alternatives that use fewer labeled examples.
  • Few-shot prompting can outperform fine-tuning but requires more than 10,000 tokens per query, while zero-shot methods save tokens yet perform worse than fine-tuning.The two prompting regimes therefore involve a trade-off between efficiency and performance.
  • C3 combines Clear Prompting, Calibration with Hints, and Consistent Output to address model input, bias, and output consistency.The components respectively improve prompting, calibrate model biases, and reduce randomness and uncertainty in generated SQL.
  • 82.3% execution accuracy on Spider’s holdout test set makes C3 a state-of-the-art zero-shot Text-to-SQL method.The reported result is on the widely used Spider dataset.
  • C3 uses approximately 1,000 tokens per query and outperforms state-of-the-art fine-tuning-based approaches by 2.4% execution accuracy.Both figures are reported for the Spider holdout test set.
  • The authors position C3’s experimental findings as potentially contributing to a new trend in GPT-based Text-to-SQL research.

2 Related Work

Text-to-SQL research has progressed from manual rules and encoder-based models toward fine-tuning and in-context learning with large language models. This work demonstrates that ChatGPT can generate SQL without demonstrations when given proper instructions.

  • Rule-based Text-to-SQL methods rely heavily on manual rule design, limiting their scalability and generalizability across domains.They can perform well in specific cases but are difficult to transfer.
  • Seq2Seq models based on bidirectional LSTMs and CNNs improve flexibility but struggle to integrate database structural information seamlessly.Graph neural networks were explored to represent database schemas as graphs and better integrate structural information.
  • Large language models outperform fine-tuning models on many NLP tasks in few-shot or zero-shot settings through in-context learning.Prompt design strongly influences large-language-model output quality.
  • Few-shot GPT-4 Text-to-SQL achieves state-of-the-art performance but requires handcrafted demonstrations and numerous tokens.Those requirements are described as time-consuming and resource-intensive.
  • ChatGPT can effectively generate SQL without demonstrations when provided with proper instructions, supporting its use as a zero-shot SQL writer.

3 Preliminaries

Text-to-SQL parsing generates an executable SQL query from a natural-language question and database schema. In the zero-shot setting, a prompted language model estimates query probabilities and produces SQL token by token without example demonstrations.

  • Given question Q and schema S, Text-to-SQL parsing aims to generate SQL query Y corresponding to the question.The schema contains tables T, columns C, and foreign-key relations R.
  • Recent Text-to-SQL approaches formulate parsing as generation using prompt P to guide a language model M.The prompt, schema, and question condition SQL-query generation.
  • At position i, the model assigns conditional probability to token Y_i given the query prefix, prompt, schema, and question.Y_<i denotes the prefix before token i, and P_M denotes the model’s conditional probability.
  • This study focuses on zero-shot prompting because adding in-context examples increases manual and API token costs.The passage identifies zero-shot prompting as the chosen setting rather than claiming examples are ineffective.

4 Proposed Approach

C3 is a ChatGPT-based zero-shot Text-to-SQL framework with three components addressing prompt input, model bias, and output consistency. It combines clear prompting, bias-calibrating hints, and consistency-based SQL selection.

  • 4 Proposed Approach: C3 combines Clear Prompting, Calibration with Hints, and Consistent Output to address model input, bias, and output, respectively.The framework is presented as a systematic zero-shot Text-to-SQL approach based on ChatGPT.
  • Clear Prompting: Clear Prompting uses a clear layout and schema linking to provide focused instructions, questions, tables, columns, and foreign-key context.Schema linking recalls relevant tables and columns, while foreign-key information specifies columns needed for JOIN operations.
  • Clear Prompting: 7.0% execution accuracy separates clear from complicated prompt layouts, supporting the adoption of sharp symbols and an instruction excluding unrequested columns.The clear layout separates instruction, schema context, and questions; the refined instruction addresses redundant columns in generated SQL.
  • Calibration of Model Bias: Calibration with Hints targets extra-column selection and misuse of LEFT JOIN, OR, and IN through contextual prompts containing debiasing guidance.The hints encourage necessary columns, alternative SQL constructs, and DISTINCT or LIMIT when appropriate to avoid repetitive execution results.
  • Consistency Output: Less than 65% of SQL statements are written correctly across thirty independent experiments under each evaluated prompt, motivating consistency enhancement.The analysis compares ChatGPT-SQL, Clear Prompt, and Clear Prompt plus Calibration with Hints on dev-set correctness distributions.

5 Experiments

Experiments evaluate C3 on Spider using execution accuracy and compare it with zero-shot, few-shot, and fine-tuning baselines. Ablations and error analyses assess prompt design, calibration, self-consistency, and their effects on performance and errors.

  • Overall Performance: C3 uses approximately 10% of DIN-SQL’s token count, and GPT-3.5 costs significantly less than DIN-SQL’s GPT-4 engine.
  • Prompt Design: 7.0% higher execution accuracy for clear layouts than complicated layouts shows that prompt organization materially affects ChatGPT-based zero-shot Text-to-SQL.
  • Ablation Studies: Removing table-and-column recall lowers performance by 2.3%, removing foreign-key specification lowers it by 2.6%, and removing calibration lowers execution accuracy by 1.5%.
  • Error Analysis: C3 reduces total errors by approximately 34% versus ChatGPT-SQL, with reductions linked to calibration, schema recall, foreign-key specification, and output consistency.

6 Conclusion

The paper presents C3, a ChatGPT-based zero-shot Text-to-SQL method that treats model input, bias, and output systematically. It reports state-of-the-art zero-shot performance and identifies C3 as a potential direction for GPT-based Text-to-SQL research.

  • C3 is a ChatGPT-based zero-shot Text-to-SQL method designed as a systematic treatment of model input, model bias, and model output.
  • The paper reports that C3 achieves state-of-the-art zero-shot Text-to-SQL performance.
  • The authors suggest that C3 has potential to forge a new trend in GPT-based Text-to-SQL research.

A Clear Prompting

Clear Prompting uses structured prompt layouts and explicit instructions to guide ChatGPT toward more precise and consistent Text-to-SQL outputs, while recognizing that prompting alone is not sufficient.

  • A Clear Prompting: ChatGPT indicates that structured prompt styles can benefit Text-to-SQL by clarifying query intent and database schema.The paper presents this response as support for using clear prompt layouts.
  • A Clear Prompting: Structured prompts specify SQL components and guide generation toward more structured and predictable queries.They can explicitly organize SELECT, WHERE, JOIN, table, and column information.
  • A Clear Prompting: Explicit prompt instructions and constraints help reduce ambiguity and verbosity in generated SQL.This can reduce syntactically correct queries that are semantically incorrect or incomplete.
  • A Clear Prompting: Structured prompts improve performance but are not a complete solution for complex Text-to-SQL tasks.Additional training, fine-tuning, or post-processing may still be required for optimal performance.

B.1 The example of table recall prompt

The table recall prompt first asks the model to rank all database tables by their relevance to the question, then to verify that every table was considered and output in the specified order.

  • B.1 The example of table recall prompt: The prompt ranks all tables by their likelihood of being used in the SQL query.Tables or columns matching the question words more closely should receive higher priority.
  • B.1 The example of table recall prompt: The prompt requires checking whether all tables were considered before producing the output list.The output must contain every table in the order established by the ranking step.
  • B.1 The example of table recall prompt: The example schema contains continents, countries, car_makers, model_list, car_names, and cars_data tables.The schema is paired with a question about car makers that produced a car in 1970.

B.2 The example of column recall prompt

The column recall prompt ranks columns within each table using question-word matches and foreign-key relevance, explains the choices, and returns all columns in JSON order.

  • B.2 The example of column recall prompt: The prompt ranks columns in each table by their relevance to the question and by foreign-key relationships.Columns matching the question words or serving as foreign keys are placed ahead.
  • B.2 The example of column recall prompt: The model must explain why each column was selected and ordered.This explanation accompanies the requested JSON representation.
  • B.2 The example of column recall prompt: The output is a JSON object containing all columns for every table in the explained order.Each table maps to an ordered array of column names.
  • B.2 The example of column recall prompt: The example links car_makers, model_list, car_names, and cars_data through explicitly listed foreign keys.The schema is paired with the question about car makers producing a car in 1970.

C The examples of prompts

The prompt examples contrast layouts and illustrate C3’s use of role-based instructions, SQL-writing tips, and constraints on query construction and output. Together, they emphasize structured prompting for clearer and more consistent SQL generation.

  • C.1 Clear Layout: The clear-layout example requests only a completed SQLite query for counting singers and presents tables with inline properties.It explicitly prohibits explanation in the output.
  • C.2 Complicated Layout: The complicated-layout example also requests a singer count but distributes schema properties across a less compact table listing.The prompt still ends with a request to complete the SQL query.
  • C.3 C3 Prompt: The C3 prompt establishes system-level instructions for an SQL writer to remember tips and avoid repeating mistakes.This frames subsequent user tips and query tasks as persistent guidance.
  • C.3 C3 Prompt: One C3 tip instructs the model to omit COUNT(*) from SELECT when the question asks only for a field, while using it in ORDER BY.The example addresses questions asking which A has the most B.
  • C.3 C3 Prompt: Another tip recommends INTERSECT or EXCEPT instead of IN, OR, or LEFT JOIN in cases where those alternatives might produce extra results.It also advises using DISTINCT or LIMIT when necessary.
  • C.3 C3 Prompt: The C3 task prompt requires SQLite SQL only, forbids explanations and unrequested columns, and supplies table properties and join relationships.The example asks how many singers there are.
Loading 2307.07306v1…