Building a Text2SQL System

Building a Text2SQL System

With the recent advancements in AI technology, there is a significant change in the way data is utilized. In the project I am working on, there is an abundance of requirements and ideas for various functions utilizing LLM.

To address the question of how to process numerous data extraction requests (Ad Hoc) pouring in from business operations more quickly and accurately, I would like to share the latest trends in 'Text2SQL' technology and the review contents for project implementation.

1. Problem: The debt that developers accumulate and the bottleneck in decision-making

There is a common scene experienced by most data utilization organizations, including our project.

- "Please write a query for how many trainees meet these conditions."

- "I need last week's training performance data; when can I expect to receive it?"

The problem is that these requests are not just one or two simple ones. As a developer, time is spent on writing simple queries rather than actual analysis and modeling, leading to a bottleneck where clients and planners have to wait for results to make decisions.

Why does this problem occur?

1. SQL Barrier: Most personnel, excluding developers within the project, either do not know SQL or find it difficult to write queries reflecting complex business logic even if they do.

2. Ambiguous Questions: A single question like "Please let me know the performance rate" can have varying conditions based on duration, department, and course specifics, leading to high communication costs.

3. Data Consistency: The reliability of queries generated can be low due to hallucinations that occur when LLM is simply integrated.

2. Solution: Natural language to SQL, Building a 'Text2SQL' service

To solve this problem, we aim to implement a Text2SQL service that generates executable SQL instantly when questions are asked in natural language.

Why Text2SQL?

Text2SQL goes beyond simply being a 'translation' technology; it acts as a 'bridge' that breaks down the linguistic barriers between data and people. We have chosen this technology as a solution due to the following attractive points.

- Realization of data democratization: Even planners or clients who do not know SQL can ask questions about data in their own language and get answers. This accelerates 'data-driven decision making' by spreading data authority concentrated in specific departments throughout the organization.

- Revolutionary improvement in development productivity: It creates an environment where developers can focus on designing more valuable business logic and improving architecture by reducing the time spent manually writing simple data extraction queries.

- Possibility of real-time communication: Without having to wait days to receive reports, you can check the results immediately after asking questions, maximizing business flexibility.

Beyond merely asking questions, the key is to combine domain knowledge specific to the project with LLM. Initially, we reviewed the DSL (Domain Specific Language) approach of defining all rules in advance, but now we are looking for a technical solution to increase accuracy by combining 'flexible structure centered on small models' and 'execution-based validation systems'.

3. Implementation Cases and Design Strategies

Let's take a look at the specific architecture and technical elements that need to be considered for actual project adoption.

3-1. System Construction: Knowledge Augmentation Based on RAG

The principle of "Garbage In, Garbage Out" is also valid in Text2SQL. We will build the following unstructured data pipeline so that LLM can understand the complex table structures of our project.

- Table Metadata Augmentation: We collect rich DDL information that includes not just column names but also the purpose of the column, characteristics, and examples of major values.

- Utilizing Few-shot SQL Examples: By providing high-quality query pairs pre-written by data analysts or developers as examples (Few-shot), we enable the model to learn the query style and business logic of our project.

3-2. Call and Integration Strategy in Java Spring Projects

Since our project is currently based on Java/Spring, it is important how to integrate the Python-based LLM ecosystem. Two main strategies can be considered.

Strategy A: Building and Calling a Python-based API Server

This is the most recommended method, where a separate microservice is built in Python (FastAPI) through the LLM library (LangChain), and this is called as a REST API in Spring Boot.

// Example of calling Python Text2SQL server from Spring Boot
@Service
public class Text2SqlService {
    private final RestClient restClient = RestClient.create();

    ...
    public String generateSql(String question) {
        return restClient.post()
                .uri("http://ai-service/generate-sql")
                .contentType(MediaType.APPLICATION_JSON)
                .body(Map.of("question", question))
                .retrieve()
                .body(String.class);
    }
}

Strategy B: Native Java Implementation using LangChain4j

If you want to integrate LLM directly within a Spring Boot project without setting up a separate server, you can use the LangChain4j library.

// Example of SQL generation in Java using LangChain4j
public interface SqlGenerator {
    @UserMessage("자연어 질문: {{it}}. 이 질문을 바탕으로 실행 가능한 SQL을 생성해줘. 테이블 정보: ...")
    String generate(String question);
}
// Service 로직
public void executeUserQuestion(String question) {
    String sql = sqlGenerator.generate(question);
    List<Map<String, Object>> result = jdbcTemplate.queryForList(sql);
    // 결과 처리...
}

3-3. Self-Correction and Validation Loop

Simultaneously verifies whether the generated SQL is syntactically correct and whether the results are business-relevant.

1. SQL Generation: The LLM writes SQL based on the questions.

2. Execution Attempt: Queries are run against an actual DB (or read-only sandbox) using Spring's `JdbcTemplate`, etc.

3. Error Handling: If a `SQLException` occurs, the error message is sent back to the LLM.

4. Correction: The LLM rewrites the query based on the error message and repeats this process until successful (up to N times).

4. Implementation Results and Future Tasks

Expected Benefits upon Implementation

- Reduced Technical Debt: Improved focus on core development tasks by automating repetitive simple extraction requests.

- Data Democratization: Allowing planners or clients who do not know SQL to explore data and derive insights directly in natural language.

- Innovating Decision-Making Speed: Shortening the waiting time from weeks to real-time responses within seconds.

Future Improvements (as of 2026)

While the latest models show high accuracy in the BIRD-SQL benchmark, they still cannot be trusted 100%.

- Semantic Layer Integration: There is a need to manage metric definitions (e.g., performance calculation methods) more rigorously by linking to the semantic model.

- Enhancing Interactive Interface: The AI should enhance the function of asking users back when questions are ambiguous to eliminate ambiguity at its source.

5. Conclusion...

To resolve the bottleneck in data analysis, we have entered an era where intelligent services like Text2SQL are essential.

Rather than waiting for perfect outcomes, it is important to first build a verifiable structure and iteratively improve it.

We will successfully integrate the reviewed Text2SQL system into our project, creating an environment where anyone can engage with data and make better decisions.

sauce0127

Site footer