Knowledge Base Construction for Knowledge-Augmented Text-to-SQL

Jinheon Baek, Horst Samulowitz, Oktie Hassanzadeh, Dharmashankar Subramanian, Sola Shirai, Alfio Gliozzo, Debarun Bhattacharjya

Introduction

Translating natural language queries into structured SQL (Text-to-SQL) is a critical capability for making data accessible to non-technical users. While Large Language Models (LLMs) have demonstrated remarkable proficiency in general coding tasks, they frequently encounter significant hurdles when applied to specialized domains such as finance, medicine, or proprietary corporate databases. The primary challenge lies in the "knowledge gap": while an LLM understands SQL syntax, it lacks the specific domain logic—such as complex financial formulas or obscure column mappings—required to generate accurate queries for a specific database.

To address this, researchers have traditionally relied on manual knowledge annotation or "on-the-fly" generation. However, these methods are either unscalable or prone to hallucination. This paper introduces KAT-SQL (Knowledge-Augmented Text-to-SQL), an automated framework designed to build a comprehensive, reusable knowledge base (KB) that empowers LLMs to handle complex, domain-specific queries with higher precision and consistency.

The Challenge of Domain-Specific Text-to-SQL

LLMs rely on parametric knowledge acquired during pre-training. While this allows them to understand general concepts, it does not provide the specific context of a private data lakehouse. For instance, an LLM might know how to perform a JOIN operation but may not know that the "WBC" column in a medical database refers to "White Blood Cell count," or that a specific "Profit" calculation requires a multi-step subtraction of various tax and operational cost columns.

Existing solutions typically fall into two categories, both of which have significant drawbacks:

  1. Manual Annotation: Human experts must provide "evidence" or hints for every query. This is prohibitively expensive and fails to scale to large, evolving databases.
  2. On-the-fly Generation: Some systems attempt to generate the necessary knowledge at the moment of the query. This often results in inaccurate information and misses the opportunity to reuse insights across similar queries within the same domain.

The KAT-SQL Framework

KAT-SQL shifts the paradigm from query-specific generation to a structured retrieval-and-refinement pipeline. The framework operates in three distinct stages:

1. Automated Knowledge Base Construction

Rather than waiting for a user query, KAT-SQL proactively builds a KB. It utilizes existing query-schema pairs and leverages an LLM to generate descriptive knowledge entries. To ensure the KB is both diverse and comprehensive, the authors employ an iterative expansion strategy. By permuting few-shot examples, the system prompts the LLM to explore various "contextual nuances," ensuring that the resulting KB covers a wide array of potential domain logic.

2. Contrastive Retrieval

When a new query is received, the system does not rely on guesswork. It employs a dense retriever (MPNet) trained via contrastive learning to identify and extract the most relevant entries from the constructed KB. This ensures that the model is provided with contextually appropriate information tailored to the specific database schema in question.

3. Refinement and SQL Generation

Simply providing raw retrieved text can sometimes distract the model with irrelevant details. KAT-SQL introduces a "refinement" stage where an LLM tailors the retrieved knowledge to the specific query and schema. This produces a "gold nugget" of information—a concise, highly relevant hint—which is then fed into a high-capacity LLM (such as Llama-3 70B) to generate the final SQL statement.

Experimental Evaluation

The researchers evaluated KAT-SQL across three diverse environments to test its robustness and generalizability:

  • BIRD: A large-scale benchmark featuring human-annotated evidence. KAT-SQL was tested in both "Overlap" (seen databases) and "Non-Overlap" (unseen databases) scenarios.
  • Spider: A standard Text-to-SQL benchmark that lacks human-annotated knowledge, testing the framework's ability to generate its own utility from scratch.
  • CSTINSIGHT: A real-world dataset involving customer queries over a data lakehouse with 34 tables and no human-provided hints, representing a "cold start" scenario.

Key Results

KAT-SQL demonstrated substantial performance improvements over baselines that used no external knowledge or simple on-the-fly generation:

  • BIRD (Non-Overlap): Execution Accuracy (EX) increased significantly from 20.66% (baseline) to 41.07%.
  • Spider: Accuracy improved from 70.99% to 74.56%.
  • CSTINSIGHT: In the most challenging real-world test, accuracy nearly tripled, rising from 4.76% to 14.29%.
  • Efficiency: The system remains highly practical for production environments; the retrieval process accounts for only 2% of total generation time, with the entire pipeline typically completing in under 5 seconds per query.

Discussion: Reusability and Generalization

A primary insight from this research is that domain knowledge is inherently reusable. A formula for "profit" or a definition of a specific column generated for one query is often applicable to many others within the same domain. By storing this in a KB, KAT-SQL achieves a level of consistency that on-the-fly systems lack.

Furthermore, the study found that a KB built for one dataset (e.g., BIRD) can improve performance on entirely different datasets (e.g., Spider). This suggests that the "domain logic" captured by the framework transcends specific database schemas, capturing broader logic patterns that are useful across the field.

Conclusion and Future Work

KAT-SQL represents a significant step toward automated, scalable knowledge augmentation for Text-to-SQL tasks. By replacing manual effort with an automated construction and refinement pipeline, it bridges the gap between general-purpose LLMs and specialized database requirements.

Despite these gains, a performance gap remains between automated systems and those provided with "Oracle Knowledge" (perfect human hints). Future research will focus on improving KB coverage to capture 100% of required domain logic, implementing dynamic updates based on user feedback, and addressing privacy concerns to ensure sensitive database metadata is not inadvertently exposed through the reusable knowledge base.

Summary updated

BibTeX
@inproceedings{baek2025knowledge,
  author = {Jinheon Baek and Horst Samulowitz and Oktie Hassanzadeh and Dharmashankar Subramanian and Sola Shirai and Alfio Gliozzo and Debarun Bhattacharjya},
  title = {{Knowledge Base Construction for Knowledge-Augmented Text-to-SQL}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure