A method, apparatus, and storage medium for generating SQL statements based on context enhancement.
By introducing a structured business terminology table and a large language model into the database of natural resource management, the context flow is enhanced, the semantic gap caused by low-quality metadata is solved, and highly accurate SQL statement generation is achieved.
Patent Information
- Application Number
- CN202610156804.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-02-04
- Publication Date
- 2026-05-26
AI Technical Summary
Existing Text-to-SQL technology is limited in professional fields such as natural resource management by the low-quality metadata of the underlying business database. This results in a significant semantic gap between the business intent expressed in natural language and the physical structure of the database, making it difficult to accurately understand the true business meaning of fields and affecting query accuracy.
By introducing a structured business terminology and a large language model, a semantic enhancement process is implemented to build an enhanced context. Combined with multi-step collaboration such as vector retrieval and data density preview, accurate SQL statements are generated.
It achieves accurate association between user queries and business semantics based on enhanced context, generating high-quality, executable SQL statements, and solving the problem of reliable response to complex queries in professional fields.
Smart Images

Figure CN122086916A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of artificial intelligence, and more specifically, to a method, apparatus, and storage medium for generating SQL statements based on context enhancement. Background Technology
[0002] In the digital transformation of professional fields such as natural resource management, massive amounts of business data are stored in relational databases. To enable non-technical users to directly query data using natural language, Text-to-SQL technology has emerged, the core of which is to automatically convert natural language questions into SQL query statements. Currently, zero-shot methods combined with Large Language Models (LLMs) have become the mainstream technical approach in this field. The effective application of these technologies generally relies on a fundamental premise: the underlying database metadata (such as table names, field names, and their comments) must have good semantic clarity and business readability to accurately bridge the semantic gap between natural language and database schema. Summary of the Invention
[0003] To improve metadata quality and database query accuracy, this invention provides a context-enhanced SQL statement generation method. Based on enhanced context, it integrates multi-source information to construct prompt words, driving a large language model to generate accurate and executable SQL statements, thereby achieving reliable responses to complex queries in professional fields.
[0004] As one aspect of this invention, a method for generating SQL statements based on context enhancement is provided, comprising:
[0005] The user-submitted natural language query is converted into a query vector, and a similarity search is performed in the vector database to obtain the relevant enhanced context, which includes the enhanced annotations of the data table;
[0006] Based on the enhanced context, at least one core data table most relevant to the natural language query is identified from the relevant data tables using a large language model.
[0007] Several data records with the highest data density are obtained from the core data table as data samples, wherein the data density is defined by the number of non-empty fields in the records;
[0008] By integrating the enhanced context, the structure definition of the core data table, the data sample, and the natural language query, a Text-to-SQL prompt word is constructed.
[0009] The Text-to-SQL prompts are input into the large language model to generate executable SQL statements.
[0010] In one or more optional embodiments, before converting the user-submitted natural language query into a query vector and performing similarity retrieval in a vector database to obtain relevant enhanced context, the method further includes:
[0011] A structured business terminology table is obtained by reading predefined professional terms and their corresponding explanations from a relational database.
[0012] For each field in the target database, a first prompt word is constructed based on the corresponding field name, original comments, business terminology table, and several non-empty data samples taken from each field.
[0013] The large language model is invoked to process the first prompt word, and the output is an enhanced annotation for each of the fields.
[0014] For each field's enhanced annotation, a second prompt word is constructed, and the enhanced annotation is determined by processing the second prompt word to obtain a determination result. If the determination result is to accept, the enhanced annotation is stored in a vector database, and an enhanced context is obtained based on all accepted enhanced annotations stored in the vector database.
[0015] In one or more optional embodiments, the construction of a first prompt word for each field in the target database, based on the corresponding field name, original annotation, business terminology table, and several non-empty data samples from each field, includes:
[0016] Obtain the field names and corresponding original comments of all tables in the target database as a set of field information;
[0017] For each field in the field information set, several non-empty data records for that field are obtained from its corresponding data table as data samples for that field;
[0018] For each field, the field name, the original annotation, the professional terms and explanations retrieved from the business terminology table, and the data sample are combined and spliced according to the corresponding preset template to obtain the first prompt word.
[0019] In one or more optional embodiments, the step of constructing a second prompt word for each field's enhanced annotation, and determining whether to adopt the enhanced annotation by processing the second prompt word to obtain a determination result, includes:
[0020] For each field, the original annotation, the enhanced annotation, the table annotation of the corresponding data table, and the data sample are combined to construct a second prompt word;
[0021] The large language model is invoked to process the second prompt word, and it is determined whether to adopt the enhanced annotation to obtain the judgment result.
[0022] In one or more optional embodiments, the step of reading predefined technical terms and their corresponding explanations from a relational database to obtain a structured business terminology table includes:
[0023] Read the terminology explanations corresponding to the professional terms described in the terminology table of the relational database;
[0024] The term names and term explanations are loaded into memory and constructed into a key-value pair data structure with the term names as keys and the term explanations as values, forming a structured business terminology table.
[0025] In one or more optional embodiments, the step of identifying at least one core data table most relevant to the natural language query from relevant data tables based on the enhanced context and utilizing a large language model includes:
[0026] Extract the relevant data tables from the enhanced context to form a candidate data table set;
[0027] Construct business table selection prompts, which include the natural language query, the table name of each data table in the candidate data table set, the table comments, and the enhanced comments of the fields related to the corresponding table in the enhancement context;
[0028] Input the business table selection prompts into the large language model, and output the name of the core data table most relevant to the natural language query;
[0029] Parse the output of the large language model and extract and confirm at least one core data table.
[0030] In one or more alternative embodiments, before identifying at least one core data table most relevant to the natural language query from the relevant data tables based on the enhanced context and utilizing the large language model, the method further includes:
[0031] A pre-trained language model based on the Transformer architecture is used as the basic inference engine;
[0032] In the basic inference engine, the final large language model is obtained through preset, structured instruction templates and parameter configurations.
[0033] In one or more optional embodiments, the integration of the enhanced context, the structure definition of the core data table, the data sample, and the natural language query to construct Text-to-SQL prompts includes:
[0034] Fill the enhanced context, the structure definition of the core data table, the data sample, and the natural language query into the corresponding positions of the preset template;
[0035] Combine the filled parts in the template order to generate Text-to-SQL prompts.
[0036] As a second aspect of the present invention, an apparatus for generating SQL statements based on context enhancement is provided, comprising:
[0037] The context retrieval module is used to convert the natural language query submitted by the user into a query vector, and perform similarity retrieval in the vector database to obtain relevant enhanced context, which includes enhanced annotations of the data table;
[0038] The core data table confirmation module is used to confirm at least one core data table most relevant to the natural language query from the relevant data tables based on the enhanced context and using a large language model.
[0039] The data sample acquisition module is used to obtain several data records with the highest data density from the core data table as data samples. The data density is defined by the number of non-empty fields in the records.
[0040] The SQL suggestion word construction module is used to integrate the enhanced context, the structure definition of the core data table, the data sample, and the natural language query to construct Text-to-SQL suggestion words.
[0041] The SQL generation module is used to input the Text-to-SQL prompts into the large language model and generate executable SQL statements.
[0042] As a third aspect of the present invention, a computer-readable storage medium is provided, the computer-readable storage medium storing a computer program that performs the context-enhanced SQL statement generation method.
[0043] The beneficial technical effects of this invention are as follows:
[0044] The context-enhanced SQL statement generation method provided in this invention enhances the context, accurately associates user queries with business semantics through vector retrieval, and uses a large language model to identify core business tables and filter high-density data samples. Finally, it integrates multi-source information to construct prompt words, driving the large language model to generate accurate and executable SQL statements, thus achieving a reliable response to complex queries in professional fields. Attached Figure Description
[0045] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0046] Figure 1 This is a flowchart of the SQL statement generation method based on context enhancement in an embodiment of the present invention;
[0047] Figure 2 This is a schematic diagram of the overall framework in a specific application of the present invention;
[0048] Figure 3 This is a schematic diagram illustrating the data flow in a specific application of the present invention;
[0049] Figure 4 This is a comparison chart of the accuracy rates of Experiment A and Experiment B in a specific application of this invention;
[0050] Figure 5 This is a schematic diagram of the structure of the SQL statement generation device based on context enhancement in an embodiment of the present invention. Detailed Implementation
[0051] To facilitate understanding of the technical solutions in the embodiments of this application, some terms or concepts involved in the embodiments of this application will be briefly described first:
[0052] 1. Business Terminology Table: This refers to a collection of standardized technical terms and their authoritative explanations, read from an independent relational database and stored in memory as key-value pairs, serving as an external knowledge source to enhance the semantics of database metadata.
[0053] 2. Enhanced annotations: These are new annotations that are more readable, containing explanations of domain terminology, and are generated after semantic expansion and business-oriented reconstruction of the original field annotations through a large language model. They are the core output of the semantic enhancement process.
[0054] 3. Vector Database: This refers to a dedicated database used to store the enhanced annotations and their corresponding vectorized representations. It supports efficient similarity retrieval and serves as the knowledge hub connecting the semantic enhancement process and the intelligent query generation process.
[0055] 4. Data density: refers to the number of non-empty fields in a single data record. It is used as a quantitative indicator in the data preview stage to filter high-quality data samples that can best reflect the table structure information.
[0056] Exemplary embodiments of the present disclosure will now be described in more detail with reference to the accompanying drawings. While exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the scope of the disclosure to those skilled in the art.
[0057] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numerals in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present invention. Rather, they are merely examples of apparatuses and methods consistent with some aspects of the invention as detailed in the appended claims.
[0058] It should be noted that the execution entity of each embodiment of this application can be a computing service system with data processing, network communication, and program execution functions, such as an electronic system capable of implementing the above functions, a context-enhanced SQL statement generation system, etc. The following description uses a context-enhanced SQL statement generation system (hereinafter referred to as "the system") as an example to illustrate the following embodiments.
[0059] Example 1
[0060] In practical applications, especially in specialized fields such as natural resources, the performance of existing Text-to-SQL technologies is limited by the quality of the underlying business database's metadata. These databases often prioritize storage efficiency and technical constraints in their table structure design, resulting in poor business readability of their metadata (such as table names, field names, and comments), commonly exhibiting semantic ambiguity, missing comments, or non-standardization. This creates a significant "semantic gap" between the business intent expressed in natural language and the underlying physical structure of the database, making it difficult for models to accurately understand the true business meaning of fields and becoming one of the fundamental bottlenecks restricting the improvement of query accuracy.
[0061] In their in-depth research into this problem, the inventors realized that simply relying on raw metadata or general domain knowledge is insufficient to bridge this gap. Based on the principle of "systematically enhancing the semantic representation of low-quality metadata through the injection of external domain knowledge," this invention proposes a context enhancement method, namely a semantic enhancement process. This process introduces a structured business terminology table and combines it with the semantic understanding and generation capabilities of LLM to automatically enhance and verify the consistency of raw metadata, thereby constructing a high-quality, domain-knowledge-rich structured context (enhanced context), laying a precise semantic foundation for subsequent query generation.
[0062] The inventors also discovered that even with enhanced semantic context, traditional retrieval and generation methods may still result in inaccurate SQL statements due to retrieval bias or insufficient utilization of context. To address the technical problem of "how to generate SQL accurately and reliably based on enhanced context," this invention further designs a context-enhanced SQL statement generation method, namely an intelligent query generation process. This process, through multi-step collaboration including vectorized retrieval, LLM-based context verification, and high-density data preview, ensures that enhanced context is fully utilized during SQL generation and corrects potential errors in intermediate steps, ultimately achieving accurate and reliable natural language to SQL conversion.
[0063] Reference Figure 1 As shown, this embodiment of the invention provides a method for generating SQL statements based on context enhancement, including:
[0064] S10: Convert the natural language query submitted by the user into a query vector, and perform similarity retrieval in the vector database to obtain relevant enhanced context, which includes enhanced annotations of the data table;
[0065] S20: Based on the enhanced context, use a large language model to identify at least one core data table that is most relevant to the natural language query from the relevant data tables;
[0066] S30: Obtain several data records with the highest data density from the core data table as data samples, wherein the data density is defined by the number of non-empty fields in the records;
[0067] S40: Integrate the enhanced context, the structure definition of the core data table, the data sample, and the natural language query to construct Text-to-SQL prompts;
[0068] S50: Input the Text-to-SQL prompt into the large language model to generate an executable SQL statement.
[0069] Specifically, the system receives natural language queries submitted by users and converts them into high-dimensional query vectors using a text embedding model. It then performs a similarity search in a vector database storing augmented annotation vectors to identify the most semantically relevant vectorized metadata entries, thereby obtaining the relevant augmented context, including data tables and field augmented annotations. This step precisely associates the user's question with structured semantic information rich in domain knowledge.
[0070] Based on the relevant enhanced context obtained from the retrieval, the system extracts the relevant data tables to form a candidate set. A dedicated business table is constructed to select prompt words, and an LLM (Limited Language Management) is invoked to evaluate the candidate tables. The model comprehensively considers the user's query intent and the enhanced semantic descriptions of each data table, ultimately identifying at least one core data table most relevant to the query, correcting potential biases from simple vector retrieval. Further, for the identified core data tables, the system performs data sampling. The sampling strategy uses data density as the core indicator, defined as the number of non-null fields in a single data record. The system obtains several records with the highest data density from the table as data samples reflecting the data structure and numerical characteristics within the table, providing concrete data examples for subsequent SQL generation.
[0071] In the Text-to-SQL suggestion construction step, the system integrates multi-source information, assembling the enhanced context (including enhanced table comments and field comments), the structure definition (DDL) of the core data table, the aforementioned data samples, and the original user query according to a preset engineering template to construct a Text-to-SQL suggestion containing rich and accurate context.
[0072] The constructed Text-to-SQL prompts are input into a dedicated LLM. Based on the provided strong semantic context and data instances, the model understands the user's intent and database structure, generating executable SQL statements that conform to grammar and business logic. The system then executes this SQL, completing the final conversion from natural language to a database query.
[0073] In this embodiment of the invention, before step S10, steps A10 to A50 may be included:
[0074] A10: Read predefined professional terms and their corresponding explanations from a relational database to obtain a structured business terminology table;
[0075] A20: For each field in the target database, construct a first prompt word based on the corresponding field name, original comments, business terminology table, and several non-empty data samples taken from each field;
[0076] A30: Call the large language model to process the first prompt word and obtain the output of the enhanced annotation for each field;
[0077] A40: For each field's enhanced annotation, construct a second prompt word, and determine whether to adopt the enhanced annotation by processing the second prompt word, and obtain a judgment result; A50: If the judgment result is adoption, store the enhanced annotation in the vector database, and obtain the enhanced context based on all adopted enhanced annotations stored in the vector database.
[0078] Specifically, predefined, standardized technical terms and their corresponding detailed explanations are read from a relational database to construct a structured business terminology table. This terminology table is organized in key-value pairs and serves as an authoritative source of domain knowledge for subsequent semantic enhancement, supplementing the database metadata with missing or ambiguous business semantics.
[0079] For each field in the target database to be enhanced, the system automatically retrieves its field name and original annotation, and combines this with relevant terms and explanations retrieved from the business terminology table. It also appends several non-empty data samples taken from the actual data of that field as context instances. Combining these elements, a first prompt word for requesting the LLM is constructed. The LLM is then invoked to process the first prompt word. Based on the field's original technical description, embedded business terminology definitions, and real data samples, the model performs semantic understanding and expansion, outputting a more semantically rich and business-readable enhanced annotation for that field.
[0080] It should be noted that the target database specifically refers to a relational database that stores actual business data and whose metadata requires semantic enhancement. This database is the direct processing object of the semantic enhancement process. The system extracts all field names, original comments, and other metadata from its system tables and samples its actual business data records. This database is physically or logically independent from the source database that stores the standardized "business terminology table." The former provides the original material to be enhanced, while the latter provides authoritative domain knowledge for enhancement.
[0081] For each field with enhanced annotations generated, the system constructs a second prompt for quality verification. This verification prompt integrates the original annotation for that field, the newly generated enhanced annotation, the table annotation of its corresponding data table, and the aforementioned data sample. The LLM (typically configured with low creativity parameters, such as temperature=0.3) is invoked to process this prompt, determining whether to accept the enhanced annotation and outputting the acceptance or rejection result.
[0082] For enhanced annotations that are deemed acceptable, the system associates them with the corresponding fields and table information, converts them into vector representations, and stores them in a vector database. Ultimately, all the accepted enhanced annotations in the vector database together constitute a systematic, high-quality enhanced context knowledge base, providing accurate semantic support for the downstream intelligent query generation process.
[0083] In this embodiment of the invention, step A20 may specifically include steps A201 to A203:
[0084] A201: Obtain the field names and corresponding original comments of all data tables in the target database, as a collection of field information;
[0085] A202: For each field in the field information set, retrieve several non-empty data records of that field from its corresponding data table as data samples for that field;
[0086] A203: For each field, according to the corresponding preset template, the field name, the original annotation, the professional terms and explanations retrieved from the business terminology table, and the data sample are combined and spliced to obtain the first prompt word.
[0087] Specifically, the process begins by connecting to the target business database and batch reading the physical structure information of all data tables from its system directory or metadata tables. This includes the field name of each field and its associated original comments, which are then integrated to form a set of field information to be processed. This step completes the collection of basic information for all fields in the target database.
[0088] Iterate through the above set of field information. For each field, based on its corresponding data table, execute a limited query to obtain the first N valid numerical records for that field as a data sample. For each field, perform retrieval and assembly: use the field name and its original annotation as basic input; simultaneously, using the field name or original annotation as clues, query and extract relevant professional terms and their standardized explanations from a pre-loaded structured business terminology table; then incorporate the data sample for that field obtained in the previous steps.
[0089] Finally, these multi-source information elements are combined and concatenated according to a preset template to construct a structured first prompt word rich in domain knowledge and instance context. Specifically: for each field to be enhanced, the system calls a structured preset template, which clarifies the system role (e.g., "semantic enhancement expert"), task instructions (e.g., "Please generate a more complete and business-oriented field annotation based on the following information"), and specifies a strict input format. The system fills in and concatenates the field name, original annotation, relevant terms and their explanations retrieved from the business terminology table, and a data sample of the field according to the order and syntax defined in the template (e.g., organized with headings such as "Field Name:", "Original Annotation:", "Business Terminology Background:", and "Data Example:") to generate an instruction containing multi-source information and with a well-formed format, i.e., the first prompt word.
[0090] For example, the system calls a pre-defined multi-segment instruction template for a field named `fdccrjk_mj_tb`. First, the system role defined in the template is "Database Semantic Enhancement Expert". In the core task instruction section, the template explicitly requires the model to generate a more complete and clear business-oriented field annotation. In the structured input section, the system performs formatted assembly: `fdccrjk_mj_tb` is entered after "Field Name:"; the text "Real Estate Land Supply Area (10,000 mu)" is entered after "Original Annotation:"; after "Related Business Terminology Background:", the system formats and inserts the key term "mu" and its standardized explanation "a Chinese unit of land area, 1 mu is approximately equal to 666.67 square meters" retrieved from the business terminology table; after "Field Data Example:", five specific numerical samples extracted from this field are entered, such as "125.00, 98.50, 110.30, 76.80, 142.10". Finally, the output format requirements are appended. The system uses a program to concatenate all the above parts according to the template syntax order, generating a first prompt word that is clear in instructions and has a complete context.
[0091] In this embodiment of the invention, step A40 may specifically include steps A401 to A402:
[0092] A401: For each field, the original annotation, the enhanced annotation, the table annotation of the corresponding data table, and the data sample are combined to construct a second prompt word;
[0093] A402: Call the large language model to process the second prompt word, determine whether to adopt the enhanced annotation, and obtain the judgment result.
[0094] In the consistency verification step of the semantic enhancement process, the system performs automated quality audits on the generated enhanced annotations. This process specifically includes the following two core operations:
[0095] The construction of the second prompt word is similar to that of the first prompt word. It also follows the preset evaluation template to format and assemble the different information elements, such as the original annotation, the enhanced annotation, the table annotation, and the data sample.
[0096] For example, the system operates according to a preset evaluation and review template, where the role is defined as "Quality Verification Analyst" and the task instruction is to determine whether the enhanced annotation should be adopted. The system assembles the structured input: for the same field, "Real Estate Land Supply Area (10,000 mu)" is entered after "Original Annotation:"; the enhanced result obtained in the previous step is entered after "Model-Generated Enhanced Annotation:"; the description information of the table to which the field belongs is entered after "Table Annotation of the Data Table:"; and the same numerical sample is entered again after "Field Data Example:". After filling in the above information, the system generates a structured second prompt word to request the large language model to perform a logical consistency evaluation.
[0097] Model Judgment and Result Generation: The LLM is invoked to process the second prompt word. During this process, low creativity parameters are also used to ensure the stability and consistency of the judgment. Based on multi-source information in the prompt word, the model comprehensively evaluates the accuracy of the enhanced annotation, its consistency with the original context, and the rationality of its business expression. Finally, a binary judgment result is output, deciding whether to accept or reject the enhanced annotation. This result determines whether the enhanced annotation can be included in the subsequent knowledge base (i.e., the vector database).
[0098] In this embodiment of the invention, step A10 may specifically include steps A101 to A102:
[0099] A401: Retrieve the terminology explanations corresponding to the technical terms described in the terminology table of the relational database;
[0100] A402: Load the term names and term explanations into memory, construct a key-value pair data structure with the term names as keys and the term explanations as values, and form a structured business term table.
[0101] Specifically, the system connects to a relational database (such as a terminology management system) as a knowledge source, and reads records pre-defined and maintained by domain experts from its specified business terminology table in batches. Each record contains at least two core fields: the term name (i.e., standardized professional terminology, such as "area of arable land within the red line") and its corresponding detailed terminology explanation.
[0102] After reading, the system loads this terminology data into memory for processing. In memory, the program constructs a key-value pair data structure (such as a hash table or dictionary) using the aforementioned term names as unique keys and their corresponding explanations as values. This data structure allows the system to retrieve authoritative explanations directly from term names with extremely high efficiency. This ordered set of key-value pairs in memory constitutes the structured business terminology table upon which this process relies, directly usable for fast queries. This table encapsulates standardized knowledge within the domain and serves as a crucial bridge linking ambiguous technical field names with explicit business meanings.
[0103] In this embodiment of the invention, step S20 may specifically include steps S201 to S204:
[0104] S204: Extract the relevant data tables from the enhanced context to form a candidate data table set;
[0105] S202: Construct business table selection prompts, wherein the business table selection prompts include the natural language query, the table name of each data table in the candidate data table set, the table comments, and the enhanced comments of the fields related to the corresponding table in the enhanced context;
[0106] S203: Input the business table selection prompts into the large language model and output the name of the core data table most relevant to the natural language query;
[0107] S204: Parse the output of the large language model and extract and confirm at least one core data table.
[0108] In the intelligent query generation process, the core data table confirmation step is a process of refining and deciding on the vector retrieval results:
[0109] The system parses the relevant enhanced context obtained through vector retrieval, which contains enhanced annotation information for multiple data tables and their fields. From this information, the system extracts all mentioned, unique data table identifiers (such as table names) and summarizes them to form a set of candidate data tables for this query.
[0110] For the aforementioned candidate set, the system automatically constructs a dedicated business table selection prompt. This prompt systematically integrates several key pieces of information: the original natural language query; the table name and its comments for each data table in the candidate set; and the enhanced field comments extracted from the enhanced context and associated with each candidate table. The constructed business table selection prompt is input into the LLM model. Based on its understanding of the question's semantics and by comparing the business descriptions and enhanced field comments of each candidate table, the model performs comprehensive reasoning and judgment, ultimately outputting the names of one or more core data tables that it deems most relevant to the user's query.
[0111] The system receives and parses the output text of the LLM, extracts the data table names explicitly specified by the model through rule or pattern matching, and after validating the validity of these names, the system officially confirms them as at least one core data table on which subsequent processes depend, thereby completing the transformation from fuzzy candidates to precise targets.
[0112] In this embodiment of the invention, before step S20, the following may be included:
[0113] A pre-trained language model based on the Transformer architecture is used as the basic inference engine;
[0114] In the basic inference engine, the final large language model is obtained through preset, structured instruction templates and parameter configurations.
[0115] It should be noted that the application of the large language model in this invention is manifested as a multi-task prompting engineering and dynamic parameterized reasoning framework, which constructs a multi-functional integrated system under a unified model: the same basic large language model is dynamically shaped into "virtual expert modules" with different professional functions through preset, structured instruction templates and parameter configurations.
[0116] Specifically, the model assumes the following four technical roles sequentially or in parallel:
[0117] 1. Domain Semantic Generator: In the semantic enhancement process, the model receives prompts containing business terminology definitions and data samples, and performs cross-modal semantic fusion and generation tasks;
[0118] 2. Logical Consistency Verifier: In a later stage of the same process, the model switches to a low-creativity mode to perform logical reasoning and quality assessment tasks based on multi-source information;
[0119] 3. Context Decision Maker: In the intelligent query process, the model re-analyzes the search results and performs table schema matching and path selection tasks based on business semantics;
[0120] 4. Code Synthesizer: In the final stage, the model performs the task of generating and optimizing the structured query language based on the enhanced semantic context and data instances.
[0121] This integration is achieved through hierarchical instruction fine-tuning and dynamic inference path control: the system designs strongly constrained prompt templates for each task and adjusts the model's generation strategy at runtime through parameter configuration (such as temperature coefficient and top-p sampling). For example, low-temperature deterministic inference is used in the validation phase to ensure stable results, while creativity is appropriately relaxed in the generation phase to enrich the expression.
[0122] Specifically, the construction of the large language model is achieved through the following steps: a pre-trained language model based on the Transformer architecture serves as the basic inference engine; for the four technical roles mentioned above, a set of parameterized prompt templates are designed and solidified, each template including role definition, task instructions, input format constraints, and output specifications; a dynamic scheduling module is established to select the corresponding prompt template and load the corresponding inference parameter configuration according to the current task stage during process execution, wherein the parameter configuration includes at least temperature parameters, top-p sampling parameters, and maximum generation length; through the scheduling module control, the same model instance is sequentially switched to different parameterized working states in the semantic enhancement process and the intelligent query generation process, thereby realizing the functions of domain semantic generation, logical consistency verification, context decision-making, and code synthesis in sequence.
[0123] In this embodiment of the invention, step S40 may specifically include steps S401 to S402:
[0124] S401: Fill the enhanced context, the structure definition of the core data table, the data sample, and the natural language query into the corresponding positions of the preset template;
[0125] S402: Combine the filled parts in the order specified in the template to generate Text-to-SQL prompts.
[0126] Specifically, this step assembles the context using a structured prompt engineering template. This template is pre-designed as a multi-field, multi-level parameterized framework, where each field defines not only the type of information to be filled but also the formatting standards it must follow. The system first parses and populates the enhanced context (i.e., the structured semantic description fused with business terminology and logically validated) into the dedicated "Domain Semantic Background" field in the template. Next, the structure definition of the core data table is formatted using the syntax paradigm of a standard data definition language and fully placed into the "Table Structure Specification" field. Simultaneously, the cleaned and representative data samples are filled into the "Data Instance Reference" field in a clearly labeled table format. Finally, the disambiguated and normalized natural language query is placed in the "Query Instruction" field.
[0127] After all fields are filled in, the parts are seamlessly spliced and serialized according to the combination logic and syntax connection rules embedded in the template, generating the final prompt words that integrate domain knowledge, pattern constraints, instance evidence and clear user intent, and can drive the generation of high-precision SQL from a large language model.
[0128] The following combination Figures 2-3 This document provides a detailed description of the context enhancement and context-enhanced SQL statement generation method provided in this invention, along with specific embodiments. This embodiment uses a smart query scenario of the "Annual Land Use Plan Implementation Status" database of a provincial Department of Natural Resources as an example to illustrate the specific application of this invention.
[0129] Assuming that the context enhancement method and the SQL statement generation method based on context enhancement of this invention are applied to an intelligent query system for natural resource land use statistics, the system consists of two core processes working in tandem: a semantic enhancement process and an intelligent query generation process. The system architecture and data flow correspond to... Figure 3 and Figure 4 .
[0130] 1. Implementation of the semantic enhancement process
[0131] This process is an offline, automated preprocessing procedure designed to semantically enhance low-quality metadata of the target business database (in this case, 'land_use_db', which stores land supply and land use approval records) and build a high-quality knowledge foundation.
[0132] 1.1 Loading the Business Terminology Table
[0133] The system reads predefined technical terms and their explanations from the business terminology table 'biz_glossary' of the independently maintained standardized relational database 'term_db'. For example, the explanation of the term "mu" is "a Chinese unit of land area, 1 mu is approximately equal to 666.67 square meters". This information is then constructed into a key-value pair data structure in memory, forming a structured business terminology table.
[0134] 1.2 Enhanced Field Comments
[0135] First, the system connects to the target database 'land_use_db' and retrieves the field names and original comments of all its tables. For example, it finds a field 'fdccrjk_mj_tb' in the table 'supply_land_record', whose original comment is simply "Land supply area for real estate (ten thousand mu)".
[0136] Next, sample the data for this field and obtain its first 5 non-empty data, for example: '[12.5, 8.3, 15.0,10.2, 7.8]'.
[0137] Then, based on a preset structured template, the system formats and assembles the following elements to generate the first prompt word, which integrates the following elements:
[0138] Field name: 'fdccrjk_mj_tb'
[0139] Original footer: "Land supply area for real estate (in ten thousand mu)"
[0140] Relevant business terms and explanations: Retrieve the explanation of "mu" from the business terminology table and insert it.
[0141] Data sample: the above 5 values.
[0142] The constructed prompts will request the large language model to generate a more complete and clearer business description based on this information.
[0143] 1.3 Consistency Verification and Storage
[0144] The large language model outputs enhanced annotations based on the first prompt word, such as: "This field records the approved land supply area for real estate projects in units of 'ten thousand mu'. 'Mu' is a Chinese unit of area, and 1 mu is approximately equal to 666.67 square meters."
[0145] The system then initiates a consistency check. A second prompt word is constructed, containing the original annotation for the field, the newly generated enhanced annotation, the annotation for the table 'supply_land_record' (e.g., "Annual Land Supply Ledger"), and previously sampled data. A large language model configured with a low creativity parameter ('temperature=0.3') is invoked for evaluation, and the model outputs "Accept".
[0146] For the adopted enhanced annotations, the system converts them into vectors through an embedding model and associates them with metadata such as field names and table names, storing them together in the vector database 'vector_db'. After traversing all fields, 'vector_db' stores the enhanced context knowledge base of the target database.
[0147] 2. Implementation of the intelligent query generation process
[0148] This process is an online service that responds to users' natural language queries.
[0149] 2.1 Receiving and Modifying Queries
[0150] A user entered the query: "What was the total area of existing residential land in City A last year? What was the average sales cycle?"
[0151] The system first calls the large language model to rewrite the query, eliminate ambiguity and standardize it, and outputs: "What is the total area of existing residential land in City A in 2024? What is the average sales cycle of existing residential land in City A in 2024?"
[0152] 2.2 Vector Retrieval and Contextual Verification
[0153] The rewritten query is converted into a vector, and a similarity search is performed in 'vector_db'. The search results return an enhanced set of annotations related to concepts such as "existing residential land", "area", and "sales cycle" and their respective table information, such as 'stock_land_reserve' (existing land reserve table) and 'land_supply_plan' (land supply plan table).
[0154] The system retrieves the candidate tables, their enhanced annotations, and user queries, constructs business table selection prompts, and submits them to the large language model for final analysis. After analysis, the large language model confirms that the core data table is 'stock_land_reserve'.
[0155] 2.3 Data Preview and SQL Generation
[0156] From the 'stock_land_reserve' table, the system calculates the "data density" (number of non-null fields) of each record and selects the three records with the highest density as data samples to provide examples for the model.
[0157] Subsequently, the system integrates the following key information to construct the final Text-to-SQL suggestion words, including:
[0158] Enhanced context: Enhanced post-commenting of the 'stock_land_reserve' table and its fields obtained from 'vector_db'.
[0159] Table structure definition: DDL statements for the 'stock_land_reserve' table.
[0160] Data sample: the aforementioned 3 high-density data records.
[0161] User query: Issues with the rewritten specifications.
[0162] The prompt word is input into a dedicated Text-to-SQL large language model. Based on rich context, the model generates accurate SQL statements, such as:
[0163] SELECT
[0164] SUM(area_hectare) as total_area,
[0165] AVG(depletion_cycle_year) as avg_cycle
[0166] FROM stock_land_reserve
[0167] WHERE city = 'City A' AND land_type = 'Residential Land' AND year = 2024;
[0168] 2.4 SQL Execution and Fault Tolerance
[0169] The system executes the generated SQL in 'land_use_db'. If successful, the result is returned to the user. If the execution fails (e.g., due to incorrect field names), the system sends the error message to the "context confirmation" stage, triggering the large language model to re-evaluate the core tables and start a new round of generation until successful or the retry limit is reached.
[0170] In this embodiment, the previously ambiguous field 'fdccrjk_mj_tb' has been enhanced into a description with clear business meaning. When users submit complex queries involving technical terms such as "mu" (acre) and "sales cycle," the system can accurately retrieve relevant knowledge, confirm the correct data table, and generate syntactically correct and semantically accurate SQL, ultimately successfully obtaining the statistical results of "total area of existing residential land in City A in 2024" and "average sales cycle." This verifies that the present invention, through dual-process collaboration, can effectively solve the two major problems of low-quality metadata and technical terminology understanding, significantly improving the accuracy and practicality of Text-to-SQL in the field of natural resource statistics.
[0171] To quantitatively evaluate the overall performance advantages of the proposed method and the contribution of each core component, comparative and ablation experiments were designed. The comparative experiments first constructed a baseline system that did not include the three core modules of "enhanced context," "context confirmation," and "data preview." This baseline represented a fundamental method for solving the problem of accuracy in comprehensive natural resource statistical question answering. To verify the overall performance, the proposed method was compared with the baseline scheme on a test set constructed in a real business scenario (land use), using execution accuracy as the core indicator.
[0172] Ablation experiments effectively quantify the contributions of each module in a system. Their core principle lies in adhering to the controlled variable method, establishing a causal relationship between modules and system performance by constructing a control group. Specifically, an ablation experiment starts with the complete system containing all modules; then, while keeping other conditions constant, one module to be evaluated is removed sequentially; finally, the performance differences between the ablated module and the complete system are compared. The difference can then be directly attributed to the ablated module. If the removal of a module significantly reduces system performance, it indicates that the module made a substantial contribution to maintaining or improving performance; conversely, if there is no significant performance change, it suggests that the module's contribution is limited under the current experimental settings. This method of sequential isolation allows for the deconstruction of the specific contribution of each module from the overall performance, providing a quantitative and reliable basis for system optimization. Conducting ablation experiments on the three core modules—"enhanced context," "context confirmation," and "data preview"—allows for the quantification of each module's contribution from the complete system, thereby demonstrating the rationality of the system architecture design.
[0173] Through the above research, a complete solution can be formed, from theoretical methods to technical implementation and experimental verification, providing strong support for improving the practicality of Text-to-SQL in the real world.
[0174] 1. Experimental setup
[0175] (1) Experimental subjects
[0176] The comparative experiments included the scheme of this invention (Experiment A) and the baseline scheme (Experiment B), as detailed below:
[0177] Experiment A (Solution of this invention): Includes semantic enhancement process and intelligent query generation process.
[0178] Experiment B (Baseline Scenario): A query process that does not include the three core modules of "Enhanced Context," "Contextualization," and "Data Preview." Specifically, in this experiment, executable SQL is generated based on the original database metadata through vector retrieval and Text-to-SQL prompts.
[0179] Ablation experiments include the following three experimental subjects:
[0180] Experiment C: Remove the "enhanced context" module from the intelligent query generation process.
[0181] Experiment D: Remove the "Context Confirmation" module from the intelligent query generation process.
[0182] Experiment E: Remove the "Data Preview" module from the intelligent query generation process.
[0183] (2) Dataset
[0184] The dataset used in this experiment contains 65 samples. Although small in scale, all data was collected from real-world natural resource statistics scenarios, aiming to accurately reproduce the core challenges faced by business personnel in daily data querying and analysis. The dataset covers various single-table query operations, including simple conditional filtering and projection queries, as well as complex aggregation queries involving GROUP BY, HAVING, and various aggregate functions (such as SUM, AVG, COUNT). This comprehensive coverage of query types within a single-table context ensures that the dataset can effectively evaluate the model's semantic understanding and SQL generation capabilities, from simple to complex. Its value lies in its high realism and inherent complexity, providing a high-quality benchmark for verifying the model's performance in real-world environments.
[0185] (3) Experimental indicators
[0186] Execution accuracy is defined as the percentage of queries whose generated SQL statements, when executed on the database, return results that exactly match the expected standard answer. This is one of the most stringent metrics for evaluating the performance of a Text-to-SQL system.
[0187] 2. Analysis of Experimental Results
[0188] (1) Overall performance comparison: Experiment A vs. Experiment B
[0189] The aim was to verify whether the novel process proposed in this invention (Experiment A) significantly outperformed the baseline model (Experiment B) in terms of the core metric—execution accuracy. A self-built test set was used to verify and compare the two approaches item by item.
[0190] like Figure 4 As shown, Experiment A (the solution of this invention) achieved an execution accuracy of 87%, while Experiment B (directly injecting the original annotations into the prompt words) achieved an accuracy of 65%. Experiment A showed a significant improvement in absolute performance of up to 22 percentage points compared to Experiment B.
[0191] To confirm the statistical significance of this performance difference, a chi-square test was performed (significance level α = 0.01). The results showed that the accuracy rate of Experiment A (87%) was significantly higher than that of Experiment B (65%), χ²(1) = 9.53, p = 0.002. This indicates that the performance advantage of Experiment A is highly statistically significant (p < 0.01).
[0192] (2) Module contribution analysis
[0193] To quantify the contribution of each core module in the system, an ablation experiment was designed, in which key components were removed sequentially to observe performance changes. The experimental results clearly demonstrate the necessity of each module, as shown in Table 1.
[0194] Table 1 Comparison of accuracy rates in ablation experiments
[0195] Experimental protocol Configuration Description Execution accuracy Experiment A (Complete System) Semantic enhancement process + complete intelligent query generation process (including context confirmation and data preview) 87.69% Experiment C (Removing the Enhanced Context) Without semantic enhancement, the intelligent query generation process uses original database annotations. 72.31% Experiment D (Removing Contextual Confirmation) There is a semantic enhancement process, but the "context confirmation" step has been removed from the intelligent query generation process. 83.08% Experiment E (Remove Data Preview) There is a semantic enhancement process, but the "data preview" step has been removed from the intelligent query generation process. 80.00%
[0196] The experimental results revealed the following key findings:
[0197] The semantic enhancement process is the cornerstone of this performance leap. Experiment C (removing the enhanced context) achieved an accuracy of 72.31%, forming the system's performance baseline. Introducing the semantic enhancement process (Experiment A) boosted the accuracy to 87.69%, a significant improvement of 15.38 percentage points. This demonstrates that the enhanced context, containing business semantics, constructed through the semantic enhancement process, partially bridges the semantic gap between the LLM and the original data schema, and is the primary contributor to the system's high accuracy.
[0198] The optimization module in the intelligent query generation process is key to realizing the potential of RAG. Experiments D and E achieved accuracies of 83.08% and 80.00%, respectively, significantly higher than Experiment C, but still lower than Experiment A. This indicates that:
[0199] The context verification step contributed approximately 4.61 percentage points to the performance improvement. This module effectively avoids path selection errors in multi-table join scenarios by intelligently analyzing core data tables using LLM.
[0200] The data preview step contributed approximately 7.69 percentage points to the performance improvement, even exceeding the contribution of context confirmation. Providing high-density instance samples offers valuable contextual references for LLM, enabling it to more accurately understand the actual values and business implications of fields, which is particularly crucial when handling queries involving fuzzy conditions or complex calculations.
[0201] Error analysis further corroborated the above conclusions. In Experiment D (without context confirmation), there were completely erroneous SQL queries due to the selection of the wrong core table; while in Experiment E (without data preview), the errors were mostly due to inaccurate query results caused by misunderstandings of field values.
[0202] Ablation experiments demonstrate that the performance of the system (87.69% accuracy) is a result of the synergistic effect of the semantic enhancement module and the intelligent query generation module. The semantic enhancement module is the foundational pillar, providing high-quality semantic context; while the context confirmation and data preview modules act as efficient catalysts, ensuring that the LLM can make the most of this context. These three components are indispensable, collectively forming an efficient and robust Text-to-SQL system architecture, fully demonstrating the rationality and necessity of the system design.
[0203] Example 2
[0204] Based on the same inventive concept, embodiments of the present invention also provide a context-enhanced SQL statement generation apparatus, such as... Figure 5 As shown, it includes:
[0205] The context retrieval module 10 is used to convert the natural language query submitted by the user into a query vector, and perform similarity retrieval in the vector database to obtain relevant enhanced context, which includes enhanced annotations of the data table;
[0206] Core data table confirmation module 20 is used to confirm at least one core data table most relevant to the natural language query from relevant data tables based on the enhanced context and using a large language model.
[0207] The data sample acquisition module 30 is used to obtain several data records with the highest data density from the core data table as data samples. The data density is defined by the number of non-empty fields in the records.
[0208] SQL suggestion construction module 40 is used to integrate the enhanced context, the structure definition of the core data table, the data sample and the natural language query to construct Text-to-SQL suggestion words;
[0209] SQL generation module 50 is used to input the Text-to-SQL prompts into the large language model and generate executable SQL statements.
[0210] In this embodiment of the invention, the above-mentioned context-enhanced SQL statement generation device is a device corresponding to the context-enhanced SQL statement generation method provided in Embodiment 2 above. Its specific implementation can be referred to the detailed description of the context-enhanced SQL statement generation method in Embodiment 2 above. In this embodiment of the invention, it will not be repeated here.
[0211] Example 3
[0212] Based on the same inventive concept, embodiments of the present invention also provide a computer-readable storage medium storing a computer program that executes the context-enhanced SQL statement generation method as described in Embodiment 1.
[0213] Other embodiments of this disclosure will readily occur to those skilled in the art upon consideration of the specification and practice of the disclosure herein. This application is intended to cover any variations, uses, or adaptations of this disclosure that follow the general principles of this disclosure and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this disclosure are indicated by the claims.
[0214] It should be understood that this disclosure is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this disclosure is limited only by the appended claims.
Claims
1. A method for generating SQL statements based on context enhancement, characterized by, The method comprises the following steps: transforming a natural language query submitted by a user into a query vector and performing similarity retrieval in a vector database to obtain relevant enhanced context, wherein the enhanced context comprises enhanced annotations of data tables; based on the enhanced context, using a large language model to identify at least one core data table most relevant to the natural language query from the relevant data tables; obtaining a plurality of data records with the highest data density from the core data table as data samples, wherein the data density is defined by the number of non-empty fields in the records; integrating the enhanced context, the structure definition of the core data table, the data samples and the natural language query to construct a Text-to-SQL prompt word; inputting the Text-to-SQL prompt word into the large language model to generate an executable SQL statement.
2. The contextually enhanced SQL statement generation method of claim 1, wherein, Before the step of transforming the natural language query submitted by the user into a query vector and performing similarity retrieval in a vector database to obtain relevant enhanced context, the method further comprises the following steps: reading pre-defined professional terms and term explanations corresponding to the professional terms from a relational database to obtain a structured business term table; for each field in the target database, constructing a first prompt word based on the corresponding field name, the original annotation, the business term table and a plurality of non-empty data sampled from the each field; calling a large language model to process the first prompt word to obtain an output, enhanced annotation for the each field; constructing a second prompt word based on the enhanced annotation for the each field, and determining whether to adopt the enhanced annotation by processing the second prompt word to obtain a determination result; if the determination result is to adopt, storing the enhanced annotation in a vector database, and obtaining an enhanced context based on all adopted enhanced annotations stored in the vector database. 3.The context-enhanced SQL statement generation method of claim 2, wherein, The step of constructing a first prompt word based on the corresponding field name, the original annotation, the business term table and a plurality of non-empty data sampled from the each field in the target database comprises the following steps: obtaining field names and corresponding original annotations of all data tables in the target database as a field information set; for each field in the field information set, obtaining a plurality of non-empty data of the field from the data table to which the field belongs as a data sample of the corresponding field; for the each field, combining and splicing the field name, the original annotation, professional terms and explanations retrieved from the business term table, and the data sample according to a corresponding preset template to obtain a first prompt word.
4. The method of claim 2, wherein, The step of constructing a second prompt word based on the enhanced annotation for the each field and determining whether to adopt the enhanced annotation by processing the second prompt word to obtain a determination result comprises the following steps: for the each field, combining and constructing the original annotation, the enhanced annotation, the table annotation of the corresponding data table and the data sample into a second prompt word; calling the large language model to process the second prompt word to determine whether to adopt the enhanced annotation and obtain a determination result.
5. The method of claim 2, wherein, reading the predefined professional terms and the term explanations corresponding to the professional terms from the relational database to obtain a structured business term table, comprising: reading the term names and the term explanations corresponding to the professional terms from the business term table of the relational database; loading the term names and the term explanations into the memory to construct a data structure in the form of a key-value pair with the term names as keys and the term explanations as values, forming a structured business term table. 6.The contextually enhanced SQL statement generation method of claim 1, wherein, confirming at least one core data table most relevant to the natural language query from the relevant data tables based on the enhanced context using a large language model, comprising: extracting the involved data tables from the enhanced context to form a candidate data table set; constructing a business table selection prompt word, the business table selection prompt word including the natural language query, the table name of each data table in the candidate data table set, the table annotation, and the field enhanced annotation related to the corresponding table in the enhanced context; inputting the business table selection prompt word into the large language model to output the core data table name most relevant to the natural language query; parsing the output of the large language model to extract and confirm at least one core data table.
7. The method according to claim 1, wherein before confirming at least one core data table most relevant to the natural language query from the relevant data tables based on the enhanced context using a large language model, the method further comprises: using a pre-trained language model based on a Transformer architecture as a basic reasoning engine; obtaining a final large language model in the basic reasoning engine through a pre-set, structured instruction template and parameter configuration.
8. The method according to claim 1, wherein the method further comprises: filling the enhanced context, the structure definition of the core data table, the data sample, and the natural language query into the corresponding positions of a pre-set template; combining the filled parts in the order specified by the template to generate a Text-to-SQL prompt word.
9. A context-enhanced SQL statement generation apparatus, characterized by comprising: comprising: a context retrieval module configured to convert a natural language query submitted by a user into a query vector, and perform similarity retrieval in a vector database to obtain a relevant enhanced context, the enhanced context including enhanced annotations of data tables; a core data table confirmation module configured to confirm at least one core data table most relevant to the natural language query from relevant data tables based on the enhanced context using a large language model; a data sample collection module configured to obtain a plurality of data records with the highest data density from the core data table as data samples, the data density being defined by the number of non-empty fields in the records; a SQL prompt word construction module configured to integrate the enhanced context, the structure definition of the core data table, the data sample, and the natural language query to construct a Text-to-SQL prompt word; An SQL generation module is configured to input the Text-to-SQL prompt into the large language model to generate an executable SQL statement.
10. A computer-readable storage medium, characterized in that, The computer readable storage medium stores a computer program for executing the context-enhanced SQL statement generation method according to any one of claims 1-8.