A method and apparatus for NL2SQL statement transformation based on a multi-level inference framework
By combining a multi-layered inference framework with large-parameter and small-parameter language models, the accuracy and efficiency issues of existing NL2SQL technology in complex queries and multi-table joins are solved, achieving efficient SQL statement generation and domain adaptability, and improving the accuracy and response speed of enterprise data queries.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-29
- Publication Date
- 2026-04-03
AI Technical Summary
Existing NL2SQL technology has limitations in handling complex queries, multi-table joins, and implicit constraint identification. It is difficult to deeply integrate business knowledge, differentiate task complexity, and achieve efficient collaborative reasoning, resulting in insufficient accuracy and usability of the generated SQL statements in enterprise environments.
We adopt a multi-level inference framework approach, combining large-parameter and small-parameter language models, and through a three-level inference framework of table-field-data, we collaboratively optimize database element identification, thereby improving domain adaptability and SQL statement accuracy.
Through collaborative optimization of a multi-layered inference framework, the accuracy and domain adaptability of SQL statements are improved, solving the problems of information overload, resource waste and long context modeling in existing technologies, and meeting the high-concurrency real-time query needs of enterprises.
Smart Images

Figure CN121413786B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, particularly to the field of artificial intelligence technology, and especially to a statement conversion method and apparatus based on a multi-layered reasoning framework for NL2SQL. Background Technology
[0002] Natural Language to Structured Query Language (NL2SQL) technology is a key technology for realizing intelligent data query systems. Its goal is to automatically transform user query intentions expressed in natural language into executable SQL statements that conform to syntax and semantics. This technology significantly lowers the technical barrier to database use, improves the efficiency of self-service data analysis for non-technical users, and promotes data-driven decision-making processes in enterprises. Early NL2SQL methods mainly relied on rule templates and semantic parsers, using manually designed rules and syntax trees for transformation. However, their generalization ability was limited, making it difficult to cope with the diversity and ambiguity of natural language, especially performing poorly in complex query scenarios.
[0003] With the development of neural network technology, especially the widespread application of pre-trained language models (PLMs), end-to-end neural network-based methods have gradually become mainstream. These methods typically concatenate user queries with database schema information (including table names, column names, types, key constraints, etc.), inputting the data into a single large-scale model to directly generate SQL queries, demonstrating superior performance on multiple standard evaluation datasets. End-to-end PLM methods learn the mapping relationship between language and SQL through large-scale pre-training, avoiding the complexity of manual rule design and achieving high versatility.
[0004] However, in real-world enterprise environments, due to the complexity of database structures, the strong implicitness of business logic, and the variability of query intent, existing end-to-end large-scale modeling methods still face a series of significant challenges, limiting their accuracy and usability in practical applications:
[0005] 1. Single models struggle to adapt to multi-granularity NL2SQL tasks: NL2SQL tasks encompass multiple cognitive levels, including keyword recognition, semantic parsing, table join inference, and aggregate function generation. The complexity of their subtasks varies significantly, ranging from simple entity matching to complex query construction requiring multi-step reasoning and embedded business logic. Using a single-scale model to handle all subtasks makes it difficult to balance performance and efficiency: large models have low resource utilization when handling simple subtasks, while small models struggle with complex reasoning, leading to generation errors and resource waste.
[0006] 2. Lack of Domain Knowledge Integration Mechanism: Enterprise databases contain a large number of business-specific terms, indicator definitions, and constraint rules (such as deducting refunds from "sales revenue" and requiring "active users" to meet a certain number of logins). Existing end-to-end models lack an effective mechanism to embed such dynamic and domain-related knowledge, relying solely on general pre-trained data. This results in SQL queries with biases at the business semantic level, making them unsuitable for direct production use.
[0007] 3. Long Context Modeling and Information Loss: Faced with large database schemas containing multiple table joins and numerous columns, the length of the input sequence easily exceeds the maximum context limit of the pre-trained model. Existing research shows that even if the cognitive complexity of the task itself remains unchanged, simply increasing the length of the input context leads to a significant and non-uniform decrease in the performance of large models. This performance degradation is not solely due to the physical truncation of information, but is also closely related to the inherent modeling challenges in long contexts:
[0008] 3.1 Attention Dilution and Semantic Interference: As the context grows, key pattern information (such as specific table names and column names) is submerged in a large amount of irrelevant or similar information, leading to a distraction of the model's attention. Reference studies show that the model's ability to retrieve and utilize key information in long contexts decreases sharply due to the increase in distracting items and semantic ambiguity.
[0009] 3.2 Structural Sensitivity: The contextual structure of the input (such as logically coherent text versus randomly shuffled sentences) can non-uniformly affect model performance. In some cases, maintaining logical coherence may actually increase the difficulty of model recognition by increasing the "integration" of key information.
[0010] 3.3 The Combined Burden of Retrieval and Inference: In NL2SQL scenarios, the model must first retrieve relevant tables / columns from the long schema information above, and then perform SQL generation and inference. Research has shown (Cornell University paper LongMemEval: Benchmarking Chat Assistants on Long-Term Interactive Memory) that when the model needs to perform both "retrieval" and "inference" steps simultaneously within a lengthy context, its performance is significantly lower than when it only needs to perform "inference" on refined information. This is directly analogous to the combined challenge in NL2SQL where the model needs to locate relevant elements from a lengthy schema and generate correct SQL.
[0011] 3.4 High computational overhead and latency: Large-scale parametric model inference requires a large amount of computational resources and has a high response latency, making it difficult to meet the service requirements of high-concurrency real-time queries, and also resulting in high deployment costs for enterprises.
[0012] To alleviate the aforementioned problems, recent studies have proposed several improvement strategies, such as introducing Retrieval Enhancement Generation (RAG) mechanisms to assist semantic linking, employing chain-of-thought cues for step-by-step reasoning, and designing multi-module pipelines to handle different subtasks. However, these methods typically suffer from loose coupling between modules, significant error propagation, and a lack of systematic allocation of model size based on the complexity differences of subtasks, thus failing to achieve an effective balance between performance, efficiency, and domain adaptability. Specifically:
[0013] 1. End-to-end generation methods have problems such as mismatch between complex tasks and model capacity, loss of key pattern information due to long sequence inputs, and lack of explicit fusion mechanism for business rules.
[0014] 2. Although the Search Enhanced Generation (RAG) method narrows the context scope through retrieval, retrieval errors are easily propagated to the generation stage, leading to SQL deviations, and noise in long contexts will reduce the generation quality.
[0015] 3. Methods based on unified business metrics (such as mapping natural language to predefined metric rules) rely on semantic layer construction, which has high maintenance costs, rigid rules are difficult to handle complex queries, and the semantic layer is strongly coupled with the database schema, resulting in poor adaptability.
[0016] In summary, existing NL2SQL technologies still have significant limitations in handling complex queries, multi-table joins, and implicit constraint identification. There is an urgent need for a new generation framework that can deeply integrate business knowledge, differentiate task complexity, and achieve efficient collaborative reasoning. This invention aims to address these pain points by proposing an efficient generation framework and method that combines PLMs of different parameter scales and supports multi-level task reasoning. Summary of the Invention
[0017] One objective of this invention is to provide a statement conversion method for NL2SQL based on a multi-level inference framework. This method injects a database schema hierarchy into a PLM (Portable Logic Model) to avoid information overload. It uses a three-level inference process (table-field-data) to locate database elements relevant to user intent. Collaborative optimization of large-parameter and small-parameter language models improves domain adaptability, thereby further enhancing the accuracy of SQL statements. Another objective of this invention is to provide a statement conversion device for NL2SQL based on a multi-level inference framework. A further objective of this invention is to provide a computer-readable medium. A final objective of this invention is to provide a computer device.
[0018] To achieve the above objectives, this invention discloses a statement transformation method for NL2SQL based on a multi-layer inference framework, comprising:
[0019] Receive user questions input by the user;
[0020] Based on a pre-trained language model, multi-level reasoning is performed on user questions and element alignment is performed to generate aligned database elements. The pre-trained language model includes a large parameter language model and a small parameter language model.
[0021] Using a large-parameter language model, statements are generated based on user questions and aligned database elements to obtain the target SQL statement.
[0022] Preferably, the aligned database elements include target table elements, target field elements, and target basic data elements;
[0023] Based on a pre-trained language model, multi-level reasoning is performed on user questions, and element alignment is carried out to generate aligned database elements, including:
[0024] By using large-parameter language models and small-parameter language models, the user's question is decomposed into elements, generating rewritten question and element recognition results;
[0025] Using a small-parameter language model and following a three-level reasoning framework of table-field-data, collaborative reasoning is performed on the rewriting problem and the element identification results to generate target table elements, target field elements, and target basic data elements.
[0026] Preferably, the user question is decomposed into elements using both large-parameter and small-parameter language models to generate rewritten question and element recognition results, including:
[0027] Semantic matching and business term extraction are performed on user questions to obtain the corresponding business knowledge base and business terms;
[0028] By using a large parameter language model, the problem is rewritten based on the user's question and the corresponding business knowledge base to generate a rewritten question;
[0029] By using a small-parameter language model, we can identify hierarchical information in user questions.
[0030] Using a small-parameter language model, element identification is performed based on the rewritten question, business terms, and hierarchical information to generate element identification results.
[0031] Preferably, using a small-parameter language model and following a three-level reasoning framework of table-field-data, collaborative reasoning is performed on the rewriting problem and feature identification results to generate target table elements, target field elements, and target basic data elements, including:
[0032] Using a small-parameter language model, the main table is inferred and aligned based on the feature recognition results to generate target table elements.
[0033] Using a small-parameter language model, the target field elements and target basic data elements are obtained by inferring and aligning the corresponding fields and basic data based on the rewriting question and the target table elements.
[0034] Preferably, the method further includes:
[0035] Execute the target SQL statement;
[0036] If execution fails, rewrite the target SQL statement to obtain the rewritten target SQL statement, and repeat the steps of executing the target SQL statement.
[0037] If the execution is successful, a chart will be generated based on the user's question, the target SQL statement, and the successful execution result information using a small-parameter language model, producing visual chart data.
[0038] Preferably, the target SQL statement is rewritten to obtain the rewritten target SQL statement, including:
[0039] Using a large parameter language model, the target SQL statement is rewritten based on the target SQL statement, execution failure results, user questions, and aligned database elements to obtain the rewritten target SQL statement.
[0040] This invention also discloses a statement conversion device for NL2SQL based on a multi-layer inference framework, comprising:
[0041] The receiving unit is used to receive user input questions;
[0042] The multi-level inference unit is used to perform multi-level inference on user questions and align elements based on pre-trained language models to generate aligned database elements. The pre-trained language models include large parameter language models and small parameter language models.
[0043] The statement generation unit is used to generate the target SQL statement based on the user's question and aligned database elements using a large parameter language model.
[0044] The present invention also discloses a computer-readable medium having a computer program stored thereon, which, when executed by a processor, implements the method described above.
[0045] The present invention also discloses a computer device, including a memory and a processor, wherein the memory is used to store information including program instructions, and the processor is used to control the execution of the program instructions, wherein the processor executes the program to implement the method described above.
[0046] The present invention also discloses a computer program product, including a computer program / instruction, which, when executed by a processor, implements the method described above.
[0047] This invention receives user input questions; based on a pre-trained language model, it performs multi-level reasoning and element alignment on the user questions to generate aligned database elements. The pre-trained language model includes a large-parameter language model and a small-parameter language model; using the large-parameter language model, it generates a statement based on the user questions and aligned database elements to obtain the target SQL statement. It injects a layered PLM based on the importance of the database schema to avoid information overload. It locates database elements related to user intent through a three-level reasoning process of table-field-data. The large-parameter language model and the small-parameter language model are collaboratively optimized to improve domain adaptability, thereby further improving the accuracy of the SQL statement. Attached Figure Description
[0048] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art 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.
[0049] Figure 1 A flowchart illustrating a statement transformation method for NL2SQL based on a multi-layer inference framework, provided for an embodiment of the present invention;
[0050] Figure 2 A flowchart illustrating another NL2SQL statement conversion method based on a multi-layer inference framework provided in this embodiment of the invention;
[0051] Figure 3 A flowchart illustrating another NL2SQL statement conversion method based on a multi-layer inference framework provided in this embodiment of the invention;
[0052] Figure 4 A schematic diagram of the structure of a statement conversion device based on a multi-layer inference framework for NL2SQL provided in an embodiment of the present invention;
[0053] Figure 5 This is a schematic diagram of the structure of a computer device provided in an embodiment of the present invention. Detailed Implementation
[0054] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0055] It should be noted that the statement conversion method and apparatus based on a multi-layer inference framework for NL2SQL disclosed in this application can be used in the field of artificial intelligence technology, or in any field other than artificial intelligence technology. The application field of the statement conversion method and apparatus based on a multi-layer inference framework for NL2SQL disclosed in this application is not limited.
[0056] To facilitate understanding of the technical solution provided in this application, the relevant content of the technical solution will be explained below. The context information relied upon by the NL2SQL process includes relevant business knowledge bases, table and field schema information bases, and basic databases. It uses a combination of coarse ranking based on the vector model (bge-base-v1.5) and fine ranking based on the reranking model (bge-reranker-v2-m3) to semantically match relevant records. It also combines the combined invocation of large language models (large parameter language model Qwen3-32B and small parameter language model Qwen3-8B) to complete the inference and recognition of intent-related data elements.
[0057] The following uses an NL2SQL statement conversion device based on a multi-level inference framework as an example to illustrate the implementation process of the NL2SQL statement conversion method based on a multi-level inference framework provided in this embodiment of the invention. It is understood that the execution entity of the NL2SQL statement conversion method based on a multi-level inference framework provided in this embodiment of the invention includes, but is not limited to, an NL2SQL statement conversion device based on a multi-level inference framework.
[0058] Figure 1 A flowchart of a statement transformation method for NL2SQL based on a multi-layer inference framework provided for embodiments of the present invention is shown below. Figure 1 As shown, the method includes:
[0059] Step 101: Receive user input regarding user questions.
[0060] In this embodiment of the invention, the user question is in natural language and represents the user's intent.
[0061] Step 102: Based on the pre-trained language model, perform multi-level reasoning on the user question and align the elements to generate aligned database elements.
[0062] In this embodiment of the invention, the pre-trained language model includes a large-parameter language model and a small-parameter language model. The small-parameter language model is a lightweight machine learning or deep learning model with a trainable parameter scale of hundreds of millions to billions (far lower than the large-parameter language model with hundreds of billions of parameters). As an optional solution, the large-parameter language model is Qwen3-32B, and the small-parameter language model is Qwen3-8B.
[0063] In this embodiment of the invention, the aligned database elements include target table elements, target field elements, and target basic data elements.
[0064] In this embodiment of the invention, based on the relevance of each element in the database schema to the user's question, elements are injected into the model in stages according to their importance. A three-level reasoning process—table-field-data—is performed on the user's question, accurately locating database elements related to the user's intent step by step. This is combined with a lightweight auxiliary model and PLM for joint optimization, improving domain adaptability. The schema includes structural information such as tables, fields, and constraints.
[0065] Step 103: Using a large parameter language model, generate the target SQL statement based on the user's question and the aligned database elements.
[0066] Specifically, the user question and aligned database elements are input into a large-parameter language model to generate the target SQL statement and its corresponding interpretation.
[0067] In the technical solution provided by this invention, a user question is received from the user; based on a pre-trained language model, multi-level reasoning is performed on the user question and element alignment is performed to generate aligned database elements. The pre-trained language model includes a large-parameter language model and a small-parameter language model; through the large-parameter language model, a statement is generated based on the user question and the aligned database elements to obtain the target SQL statement; PLM is injected hierarchically based on the importance of the database schema to avoid information overload; database elements related to user intent are located through three-level reasoning of table-field-data; the large-parameter language model and the small-parameter language model are optimized collaboratively to improve domain adaptability, thereby further improving the accuracy of the SQL statement.
[0068] Figure 2 A flowchart of another NL2SQL statement transformation method based on a multi-layer inference framework provided in this embodiment of the invention is shown below. Figure 2 As shown, the method includes:
[0069] Step 201: Receive user input regarding user questions.
[0070] In this embodiment of the invention, each step is performed by a statement conversion device based on a multi-layer inference framework NL2SQL.
[0071] In this embodiment of the invention, the user question is a natural language statement input by the user that needs to be converted into an SQL statement.
[0072] Step 202: Decompose the user's question into elements using a large-parameter language model and a small-parameter language model, and generate rewritten question and element recognition results.
[0073] In this embodiment of the invention, step 202 specifically includes:
[0074] Step 2021: Perform semantic matching and business term extraction on user questions to obtain the corresponding business knowledge base and business terms.
[0075] In this embodiment of the invention, vector retrieval technology (such as coarse ranking using the vector model bge-base-v1.5 and fine ranking using the reranking model bge-reranker-v2-m3) is used to semantically match records related to the user's question from a pre-set business knowledge base, table and field schema information base, and basic database.
[0076] Semantic matching involves calculating the similarity between user questions and knowledge in the business knowledge base, and filtering out relevant business rules, terminology definitions, etc. (For example, "data assets" in a user question may correspond to the "total data assets / total assets calculation" rule in the business knowledge base).
[0077] Business term extraction involves extracting key business terms from the matching results, such as table names, field names, and data values (for example, extracting business terms such as "total data assets" and "total assets").
[0078] Step 2022: Using a large parameter language model, rewrite the user's question based on the corresponding business knowledge base to generate a rewritten question.
[0079] In this embodiment of the invention, a large parameter language model is used to rewrite and enrich user questions in order to incorporate business knowledge and solve the problem of ambiguity of pronouns.
[0080] Specifically, the user's original question, matching business knowledge, user login information, current time, and other information are input into the large parameter language model. The large parameter language model embeds the business knowledge into the question according to the prompt word requirements (such as "integration of relevant knowledge" and "conversion of pronouns") and outputs the rewritten question in the format of "content type|content description" (content type: query, knowledge, display, calculation, interpretation, export, other).
[0081] For example, the user question "Calculate the percentage of data assets of each subordinate unit by asset category" was rewritten as "Calculate the percentage of data assets of each subordinate unit by asset category (calculated by dividing the total data assets by the total assets)".
[0082] Step 2023: Identify the hierarchical information in the user's question using a small-parameter language model.
[0083] In this embodiment of the invention, a small-parameter language model is used to identify the implicit hierarchical relationship information in user questions, which is then used for subsequent data filtering and SQL generation.
[0084] Specifically, the user's question is input into the small-parameter language model, which parses the hierarchical relationship in the question and outputs the superior and subordinate information.
[0085] For example: The user's question is: To calculate the percentage of data assets held by each subordinate unit according to asset category. The corresponding identified hierarchical information is: {
[0086] "has_level": "Yes",
[0087] "object_type": "unit",
[0088] "base_name": "User's current organization",
[0089] "level_name": "subordinate",
[0090] "has_current": "No",
[0091] "level_depth": -1
[0092] }
[0093] The output parameters are explained below:
[0094] has_level: Whether there are hierarchical conditions (value is "yes" or "no"). This is a required field. If there are no hierarchical conditions, there is no need to identify subsequent key-value pairs.
[0095] object_type: Queries the dimensions or entity types of the parent and child levels.
[0096] base_name: The name of the current level on which the query is based.
[0097] level_name: Queries whether the current level is a parent or a child (value is "parent" or "child").
[0098] has_current: Whether to include the current level (value is "yes" or "no").
[0099] level_depth: The number of levels to query (-1 means query all levels, 1 means query one level, 2 means query two levels, and so on).
[0100] Step 2024: Using a small-parameter language model, perform element identification based on the rewritten question, business terms, and hierarchical information, and generate element identification results.
[0101] In this embodiment of the invention, the small-parameter language model relies on a zero-shot learning mechanism to fully utilize the semantic understanding and generalization capabilities acquired through pre-training, without depending on task-specific labeled data, to perform deep feature parsing of the input content. The feature recognition process structurally decomposes the "query block" content, identifying and outputting key elements including query fields, source tables, and filter bars. All recognition results are output in a standardized JSON array format, ensuring a clear structure and ease of subsequent processing.
[0102] Specifically, the rewrite question, business terms, and hierarchical information are input into a small-parameter language model. The small-parameter language model identifies the hierarchical information in the rewrite question, specifically the query fields, source table, and filter conditions, and outputs the feature recognition results in JSON array format. The feature recognition results are as follows:
[0103] Query fields: Column names of the query results data | Dimensions / Metrics.
[0104] Source table: Infer possible table names.
[0105] Filtering criteria: explicit / implicit data filtering criteria.
[0106] For example, the problem can be rewritten as: "Calculate the percentage of data assets for each subordinate unit by asset category (derived from the ratio of total data assets to total assets)"; the extracted business terms are: "Data value: ['Data']; Table name: ['Asset category']; Field name: ['Total assets']". The corresponding element breakdown results are:
[0107] {
[0108] "Query field": [
[0109] "Asset Classification | Dimensions"
[0110] "Total Data Assets | Metrics"
[0111] "Total Assets | Indicators",
[0112] "Data Asset Ratio | Indicator"
[0113] ],
[0114] "Source Table": [
[0115] "Asset Classification"
[0116] ],
[0117] "Grouping field": [
[0118] "Asset Classification"
[0119] ],
[0120] "Filter criteria": [
[0121] "Asset Category = 'Data'" ]
[0123] }
[0124] Step 203: Using a small-parameter language model, and following a three-level reasoning framework of table-field-data, perform collaborative reasoning on the rewriting problem and the element identification results to generate target table elements, target field elements, and target basic data elements.
[0125] In this embodiment of the invention, a multi-level factor reasoning method based on reasoning (distillation) is adopted. Through a small parameter language model, deep collaborative reasoning is performed on the rewriting problem and the factor identification results to accurately generate target table elements, target field elements and target basic data elements.
[0126] In this embodiment of the invention, step 203 specifically includes:
[0127] Step 2031: Using a small-parameter language model, perform main table reasoning and alignment based on the feature recognition results to generate target table elements.
[0128] In this embodiment of the invention, the element identification result includes query fields, source table, and filtering conditions.
[0129] Specifically, the query fields, source table, filter conditions, and tables selected by graph-based indexing (GBI) are input into a small-parameter language model. The small-parameter language model identifies and determines the main table that best suits the user's query intent. It then performs semantic matching between the decomposed elements (dimensions, metrics, filter values) and the actual record values in the database to establish a preliminary association mapping and output the target table elements, which include the main table name.
[0130] In this embodiment of the invention, the relevant query metrics are improved based on user knowledge by rewriting the question; and the source table, dimensions, and metrics are inferred by element decomposition. After semantic matching to relevant tables, table alignment is performed to complete the reasoning for querying the main table. Specifically, a vector retrieval model is used to filter candidate tables that are semantically related to the user's question from all database tables; a small-parameter language is used to determine the main table based on the matching degree between fields in the table and query elements, the coreness of the table in the business logic, the data integrity and timeliness of the table, and the complexity of the relationships between tables, and outputs the main table name.
[0131] For example, the problem can be rewritten as: "Calculate the percentage of data assets for each subordinate unit by asset category (calculated by dividing the total data assets by the total assets)"; the query fields, source table, filter conditions, and GBI selection table are shown in Table 1.
[0132] Table 1
[0133]
[0134] The corresponding main table for identification results is: Asset Basic Information Table.
[0135] Furthermore, the table alignment mechanism includes: quantitatively scoring each candidate table according to predefined multi-dimensional evaluation rules, considering factors including but not limited to: the matching degree between fields in the table and query elements, the coreness of the table in the business logic, the data integrity and timeliness of the table, the complexity of the relationships between tables, and the strength of the association between the relevant record values matched by the elements and the table.
[0136] Furthermore, the table alignment mechanism also includes large model voting alignment: the small parameter language model, based on the comprehensive scores of each candidate table and its own semantic understanding ability, makes a final inference judgment and votes to determine the main table that best suits the user's query intent.
[0137] Step 2032: Using a small-parameter language model, perform field and basic data reasoning and alignment based on the rewriting question and the corresponding fields and basic data of the target table elements to obtain the target field elements and target basic data elements.
[0138] In this embodiment of the invention, field / basic data alignment and secondary alignment of inferred elements are performed based on the rewrite problem and the corresponding fields and basic data of the target table elements to achieve accurate mapping at the field level and data level.
[0139] Specifically, the corresponding fields and basic data of the rewritten problem and target table elements are input into the small parameter language model. The small parameter language model performs field inference alignment, basic data inference alignment, and derived indicator identification, and outputs the target field elements and target basic data elements.
[0140] Field inference alignment includes: the small parameter language model maps business terms in user questions (such as "total data assets") to specific database fields (such as "INIT_ASSET_VAL") based on the field ranges of the main table and related tables; identifies the role of fields in SQL: query fields, grouping fields, sorting fields, aggregation fields, etc.; and handles the mapping of field aliases and business terms (such as mapping "revenue" to "revenue").
[0141] Basic data reasoning alignment includes: mapping numerical conditions and enumeration values in user questions to specific data values in the database; handling hierarchical relationships in tree-structured data (such as organizational hierarchy and classification hierarchy); and generating data filtering sub-statements (such as classifying assets as "data" and all its sub-categories).
[0142] Derived indicator identification includes: identifying calculation-related requirements in user questions, such as derived indicator types like "percentage" and "year-on-year"; and determining the numerator and denominator fields and calculation logic.
[0143] It is worth noting that derived indicator types are derived from basic aggregation results (such as sum, average) through mathematical operations (such as division, subtraction) to obtain new indicator types, such as "percentage" and "year-on-year".
[0144] Field reasoning is based on the field range of the main table and related tables. It performs semantic matching of business terms, dimensions and indicator fields, and uses PLM to locate the actual fields involved in querying, filtering, grouping, aggregation and sorting, thus completing the field reasoning.
[0145] Data reasoning is based on the table's scope and the information of the corresponding actual fields. It performs basic data matching on the data values extracted from the user's question and the element values for which no corresponding field was found. It also uses PLM to align the basic data and combines the identified hierarchical information, basic data table information, and corresponding field information to generate data filtering SQL sub-statements to complete the data reasoning.
[0146] For example, the problem could be rewritten as: "Statistically calculate the percentage of assets held by each subordinate unit according to asset category (calculated by dividing the total assets by the total assets)." The corresponding fields include: Asset Quantity; Total Assets; Asset Category Code; Total Non-Governmental Appropriation Assets (Total Other Funded Assets); Total Self-Raised Assets; Total Government-Appropriated Assets; Unit Code (Unit to which the Asset belongs); Asset Category; Asset Category Name; Abbreviated Asset Category; Asset Name; Asset Status Name; Asset Purpose Name; Abbreviated Asset Status; Original Asset Value (Amount Recorded); Net Value; Total Depreciation; Asset Primary Key; Value Type Name. The basic data is shown in Table 2.
[0147] Table 2
[0148]
[0149] The aligned target field element and target basic data element are as follows:
[0150] {
[0151] "Grouping field": ["Asset classification code", "Unit code"],
[0152] "Filter criteria": [
[0153] "Asset Class Name = 'Data' {Asset Class | Data Asset}"
[0154] ],
[0155] Query fields: ["Total Assets", "Total Assets Not Funded by Government Appropriations", "Total Assets Funded by Government Appropriations", "Total Self-Raised Assets"],
[0156] "Aggregate function": ["SUM(Total Assets)"],
[0157] "Derived indicator type": ["Percentage"],
[0158] Sort field: ["Asset category code", "Unit code"]
[0159] }
[0160] Furthermore, for elements that fail to find a match in the first alignment, a second inference and matching process is performed to reduce contextual information and improve alignment accuracy. Specifically, the elements with inferred / no corresponding actual fields / basic data, the rewrite problem, and the corresponding fields and basic data of the target table elements are input into a small-parameter language model. The small-parameter language model performs a second field inference alignment and a second basic data inference alignment, outputting the target field element and the target basic data element.
[0161] It is worth noting that the output format of secondary inference alignment is the same as that of primary inference alignment. Only a single feature type (the feature type that has been inferred) needs to be output, which reduces contextual information and improves the accuracy of alignment.
[0162] Step 204: Using a large parameter language model, generate the target SQL statement based on the user's question and the aligned database elements.
[0163] In this embodiment of the invention, the aligned database elements include target table elements (main table and related relationships), target field elements (query fields, grouping fields, filtering conditions, etc.), and target basic data elements (data filtering sub-statements).
[0164] In this embodiment of the invention, during the SQL generation process, the model also incorporates predefined special business SQL fragments to achieve accurate code generation that conforms to business specifications: For queries involving hierarchical relationships (such as asset classification), the model will directly embed pre-generated tree-structured filtering sub-statements to ensure the integrity of data queries at different levels; for typical business calculation scenarios such as "percentage" and "year-on-year comparison," the model calls pre-set complex SQL calculation templates. For example, when generating percentage calculations, the template ensures automatic handling of division by zero errors and uses the ROUND function to standardize decimal places, ensuring the rigor of the calculation logic.
[0165] Specifically, the user question, target table elements, target field elements, and target basic data elements are input into a large parameter language model. The large parameter language model organizes the aligned database elements into a structured context of prompt words. Since the previous steps have already completed element alignment, the number of input tokens in this step can be controlled between 300 and 500. For specific types of queries (such as "percentage"), the model will refer to predefined SQL templates to generate the query, ensuring the accuracy of the calculation logic (e.g., handling division by zero errors, using the ROUND function to retain decimals, etc.). During the generation process, the logical correctness of the SQL is implicitly verified, including: the correctness of table relationships (JOIN conditions), the matching of grouping fields and aggregation fields, and the correctness of the syntax of filtering conditions (tree / hierarchical queries). Finally, the target SQL statement and its corresponding explanation are output.
[0166] In this embodiment of the invention, the explanation corresponding to the target SQL statement is a natural language description used to explain the generated SQL intent and key components.
[0167] It is worth noting that the basic unit of text input and output in the Token large model is used to break down the input or output text into segments that the model can understand. The calculation method varies depending on the tokenizer.
[0168] For example, the question could be rewritten as: "Calculate the percentage of data assets for each subordinate unit by asset category (calculated by dividing the total data assets by the total assets)."
[0169] The target table elements are:
[0170] Main table: BAS_ASSET_INFO (Basic Asset Information Table) AS t1 LEFT JOIN BAS_ASSET_EXT (Extended Asset Information Table) AS t2 ON t2.ASSET_ID = t1.ASSET_ID.
[0171] The associated basic data tables are shown in Table 3:
[0172] Table 3
[0173]
[0174] The target field elements include:
[0175] Dimension fields of table "t1":
[0176] Card status: t1.CARD_STATE;
[0177] Data starting with 0 represents assets on the books and existing assets, while other data represents assets disposed of, cancelled assets, assets reduced due to splitting, etc.
[0178] Asset classification code: t1.FIXED_ASSET_TYPE_CODE.
[0179] Table “t1” indicator field:
[0180] Total assets funded by government appropriations (Government Appropriation Assets): (CASE WHEN MOF_APP IS NOT NULL THENSUM(MOF_APP) ELSE 0 END);
[0181] Total Assets: SUM(INIT_ASSET_VAL);
[0182] Total non-fiscal appropriation assets (Total other funds assets, non-fiscal appropriation assets, other funds assets): (CASE WHEN NON_MOF_APP IS NOT NULL THEN SUM(NON_MOF_APP) ELSE 0 END);
[0183] Total self-raised assets: (CASE WHEN MOF_APP=0 AND NON_MOF_APP!=0 AND THEN SUM(NON_MOF_APP) ELSE 0 END).
[0184] Table "t2" dimension fields:
[0185] Unit code: t2.AGENCY_CODE;
[0186] The entity to which the assets belong.
[0187] Dimension fields of table “ELE_VD03001”:
[0188] Asset classification code: ELE_VD03001.CODE;
[0189] Asset Classification Name: ELE_VD03001.NAME.
[0190] The target basic data elements include:
[0191] Field default filter (t1):
[0192] Audit status t1.AUDIT_STATE: t1.AUDIT_STATE=2;
[0193] Is it latest (1: latest) t1.IS_LASTEST: t1.IS_LASTEST=1;
[0194] Delete t1.IS_DELETED: t1.IS_DELETED<>1;
[0195] Card status t1.CARD_STATE: t1.CARD_STATE LIKE '0%'.
[0196] The SQL template for querying the derived indicator "percentage" is as follows:
[0197] SELECT
[0198] [{AUTO_GROUP_BY},]
[0199] {AGGRE_FUNC}({INDEX_FIELD_NAME})AS"Total{Name of the denominator indicator used in the percentage calculation}",
[0200] {AGGRE_FUNC}({NUMERATOR_INDEX_FIELD})AS"{Name of the numerator index used in the percentage calculation}",
[0201] (CASE WHEN{AGGRE_FUNC}({INDEX_FIELD_NAME}) > 0 THEN
[0202] ROUND({AGGRE_FUNC}({NUMERATOR_INDEX_FIELD})*100.0 / {AGGRE_FUNC}({INDEX_FIELD_NAME}),2)
[0203] ELSE 0 END) AS "{USE_DEF_CONDITION_NAME}'s share"
[0204] FROM
[0205] {AUTO_FROM_TABLE}
[0206] [WHERE {DEFAULT_WHERE_CONDITION}]
[0207] [GROUP BY {AUTO_GROUP_BY}]
[0208] [ORDER BY {AUTO_ORDER_BY}]
[0209] [LIMIT {AUTO_LIMIT}]
[0210] The template variables are explained as follows:
[0211] Related tables: The base data tables of the main table, tables whose fields do not need to be used in SQL, and tables that do not need to have JOIN relationships added to FROM sub-statements.
[0212] Relevant filtering conditions: For the dimension or basic data filtering mentioned in the user's question, the "filter sub-statement" (including sub-statements of tree / superior and inferior queries, time range, etc.) should be used first. If there is no corresponding filter sub-statement, a fuzzy matching filter condition (i.e., LIKE query) will be generated.
[0213] To avoid division by zero, we need to determine the proportion only when the denominator is not equal to zero;
[0214] The sub-statements enclosed in square brackets "[]" are optional (can be removed if not specified by the user). The area enclosed in curly braces "{}" represents the variable part in the template and needs to be rewritten as the corresponding SQL fragment (removing the curly braces). The variable correspondence is explained below:
[0215] AUTO_FROM_TABLE: The default generated main table and related tables that mention relevant fields (in the "related metrics" scenario, multiple or subquery results may need to be identified via UNION; the JOIN relationship can be removed from tables where fields are not used).
[0216] AUTO_GROUP_BY: The default grouping field, the statistical dimension field mentioned by the user, which includes asset classification code and unit code. If the field is associated with the CODE field of the base data table, the name (NAME) field is also required.
[0217] AGGRE_FUNC: Aggregate functions involved in user queries, including SUM (total assets), SUM (total self-raised assets), SUM (total non-government-funded assets), and SUM (total government-funded assets).
[0218] USE_DEF_WHERE_CONDITION: The filter condition mentioned by the user (filter sub-statement - user-defined).
[0219] DEFAULT_WHERE_CONDITION: Default filter condition (in the filter sub-statement - default filter).
[0220] INDEX_FIELD_NAME: The aggregate field involved in the user query. When counting, the field can use constants such as "*" or "0".
[0221] NUMERATOR_INDEX_FIELD: The numerator index column, which may be the direct name of the corresponding index column (if the corresponding index column exists); or it may be a conditional index in the format `CASE WHEN {USE_DEF_WHERE_CONDITION} THEN {INDEX_FIELD_NAME} END` (if the corresponding filter sub-statement exists).
[0222] AUTO_ORDER_BY: The default sorting field.
[0223] AUTO_LIMIT: The default number of records to generate, with a default limit of 100 records to be retrieved.
[0224] The basic data alignment results generate corresponding filter sub-statements based on the type of the underlying data table (list / tree):
[0225] 1. The "Percentage of Data Assets" in the user's question needs to be filtered for the relevant values of "Data (A08060200)" and "Data Assets (A08060200)" in the "Asset Classification" table. The corresponding filter sub-statement is: SELECT NAME,CODE FROM ELE_VD03001WHERE CONCAT(' / ', PARENTS) like '% / A08060200%' OR CONCAT(' / ', PARENTS) like '% / A08060200%'.
[0226] 2. The SQL statement to query the codes of its subordinate (all levels) is: SELECT CODE,NAME FROM MD_ORGWHERE CONCAT(' / ', PARENTS) like '% / CS04 / %'.
[0227] The generated explanation is as follows:
[0228] Based on the user's question, it is necessary to calculate the percentage of "data assets" in each "subordinate unit" under the "asset classification" dimension, that is, the "total data assets" divided by the "total assets".
[0229] Related tables: BAS_ASSET_INFO(t1), BAS_ASSET_EXT(t2), ELE_VD03001 (Asset Classification Table).
[0230] Related fields:
[0231] Numerator: Total amount of data assets (filtered by the asset category being data assets);
[0232] Denominator: Total assets;
[0233] Dimension fields: Unit code (t2.AGENCY_CODE), Asset category code (t1.FIXED_ASSET_TYPE_CODE), Asset category name (ELE_VD03001.NAME).
[0234] Filtering criteria:
[0235] The assets are classified as "Data Assets" (A08060200) and its subcategories;
[0236] The default filter criteria are listed.
[0237] Grouping fields: Unit code (t2.AGENCY_CODE), Asset classification code (t1.FIXED_ASSET_TYPE_CODE), Asset classification name (ELE_VD03001.NAME).
[0238] Aggregate function: SUM(INIT_ASSET_VAL).
[0239] The target SQL is generated as follows:
[0240] SELECT
[0241] t2.AGENCY_CODE AS "Unit Code",
[0242] ELE_VD03001.NAME AS "Asset Classification Name",
[0243] SUM(t1.INIT_ASSET_VAL) AS "Total Assets",
[0244] SUM(
[0245] CASE
[0246] WHEN ELE_VD03001.CODE IN (
[0247] SELECT CODE
[0248] FROM ELE_VD03001
[0249] WHERE CONCAT(' / ', PARENTS) LIKE '% / A08060200%' )
[0251] THEN t1.INIT_ASSET_VAL
[0252] ELSE 0
[0253] END
[0254] AS "Total Data Assets",
[0255] CASE
[0256] WHEN SUM(t1.INIT_ASSET_VAL) > 0 THEN
[0257] ROUND
[0258] SUM(
[0259] CASE
[0260] WHEN ELE_VD03001.CODE IN (
[0261] SELECT CODE
[0262] FROM ELE_VD03001 [[ID=1�]]
[0263] WHERE CONCAT(' / ', PARENTS) LIKE '% / A08060200%' )
[0265] THEN t1.INIT_ASSET_VAL
[0266] ELSE 0
[0267] END
[0268] ) * 100.0 / SUM(t1.INIT_ASSET_VAL), 2 )
[0271] ELSE 0
[0272] END AS "Proportion of Data Assets"
[0273] FROM
[0274] BAS_ASSET_INFO t1
[0275] LEFT JOIN BAS_ASSET_EXT t2
[0276] ON t2.ASSET_ID = t1.ASSET_ID <000056�>LEFT JOIN ELE_VD03001
[0278] ON t1.FIXED_ASSET_TYPE_CODE = ELE_VD03001.CODE
[0279] WHERE
[0280] t1.AUDIT_STATE = 2
[0281] AND t1.IS_LASTEST = 1 <00005>AND t1.IS_DELETED <> 1
[0283] AND t1.CARD_STATE LIKE '0%'
[0284] GROUP BY
[0285] t2.AGENCY_CODE,
[0286] ELE_VD03001.NAME,
[0287] ELE_VD03001.CODE
[0288] ORDER BY
[0289] t2.AGENCY_CODE,
[0290] ELE_VD03001.CODE
[0291] LIMIT 100.
[0292] Step 205: Execute the target SQL statement. If the execution fails, proceed to step 206; if the execution succeeds, proceed to step 207.
[0293] In this embodiment of the invention, the target SQL statement is submitted to the target database for execution. If the execution fails, it indicates that the model may be unstable, causing the target SQL to be generated incorrectly. Then, step 206 is executed. If the execution succeeds, it indicates that the target SQL is correct in both syntax and semantics. Then, step 207 is executed.
[0294] Step 206: Rewrite the target SQL statement to obtain the rewritten target SQL statement, and repeat step 205.
[0295] Specifically, using a large-parameter language model, the target SQL statement is rewritten based on the target SQL statement, execution failure results, user questions, and aligned database elements to obtain the rewritten target SQL statement.
[0296] In this embodiment of the invention, a self-correcting loop is constructed, utilizing the reasoning capabilities of a large-parameter language model to accurately correct SQL based on execution error information. The large-parameter language model analyzes the error information, diagnoses the root cause of the error (e.g., spelling errors, logical errors, type mismatches, etc.), and rewrites the problematic SQL fragments in conjunction with the original correct context.
[0297] Step 207: Using a small-parameter language model, draw charts based on the user's question, the target SQL statement, and the successful execution result information to generate visual chart data.
[0298] Specifically, the user's question, the target SQL statement, and the successful execution result information are input into the small-parameter language model. Based on the question intent, data characteristics, and business conventions, the small-parameter language model generates a description of the result data and recommends appropriate display formats and columns, generating visual chart data.
[0299] For example: The display result format is:
[0300] {
[0301] "description": "histogram",
[0302] "expression": "line_box",
[0303] "dim_fields": ["Unit Code", "Asset Category Name"],
[0304] "index_fields": ["Data asset percentage"]
[0305] }
[0306] This invention effectively decomposes the relevant context information of the SQL generation task. It decomposes and infers the query elements of the user's question through PLM, and finds the relevant actual tables / fields / basic data through vector retrieval and reordering models. It dynamically generates the PLM call context for the element alignment and SQL generation stages, reducing a large amount of irrelevant table and field information. The longest context information is in the SQL generation stage, and the input context information is 300~500 tokens, which can effectively ensure the accuracy of query element alignment and SQL generation.
[0307] It's worth noting that the three stages of this invention—question rewriting, SQL generation, and SQL rewriting—involve business domain knowledge and require relatively complex SQL logic understanding and syntax. Implementing RAG (Regional Analysis Group) combined with a business knowledge base, and generating SQL based on SQL templates and substatements, necessitates a large model with significant parameters. The other six stages—element decomposition, hierarchical information recognition, table alignment, field / basic data alignment, secondary alignment of inferred elements, and recommendation chart display—only require the user question, business terms, and corresponding semantically matched related words. The input is less extensive and of higher quality (less noisy data), allowing for collaborative completion using a large model with smaller parameters. Furthermore, these six stages can be effectively improved in vertical domains through efficient parameter fine-tuning methods. Fine-tuning for simple tasks is less difficult, consumes fewer resources, and offers better performance, quickly and effectively improving the model's accuracy and stability at each stage. Therefore, this process, through the collaboration of small-parameter models, effectively enhances domain applicability.
[0308] The purpose of this invention, a multi-level reasoning NL2SQL general generation framework, is to achieve business understanding of query text questions through multi-level reasoning. It combines inferred elements with multiple alignments of relevant tables / fields / basic data to ultimately generate accurate SQL. The interface allows configuration of implicit business knowledge of the query question, business definitions of tables and fields, and subqueries (view tables) for complex queries. Through multi-level reasoning (inference + recall + alignment), it effectively improves the accuracy of table alignment, field / basic data alignment, and SQL generation.
[0309] With over 100 tables and over 1000 fields, a 32-byte large-scale model was used to test and verify the text-to-SQL generation. The results are as follows:
[0310] 1) The NL2SQL generation process, which does not involve multi-layered inference, directly retrieves tables, fields, and basic data from user questions, achieving a maximum accuracy of 50% (retrieving relevant tables / fields / basic data, with the SQL generation context taking the optimal value). However, adding SQL rule descriptions (corresponding SQL templates) increases the number of input tokens by 3-5 times, and this increases exponentially when the question involves many elements. Furthermore, given that the accuracy of the basic model is affected by the length of the context, the accuracy will significantly decrease.
[0311] 2) Through a multi-layered inference framework, the number of input tokens in key stages is reduced to around 300, and the accuracy is improved to 93.5%. While improving performance, resource consumption is also reduced. Deploying small-parameter models (such as 8B models) using AI accelerator cards only requires 1×24G of video memory, while deploying large-parameter language models (such as 32B models) requires 2×48G of video memory, reducing resource consumption by 4 times.
[0312] Figure 3 A flowchart of another NL2SQL statement transformation method based on a multi-layer inference framework provided in this embodiment of the invention is shown below. Figure 3 As shown, the method is as follows:
[0313] This process breaks down the NL2SQL process into several main modules: Zero-shot element decomposition with business knowledge, multi-level element reasoning and alignment based on reasoning (distillation), SQL generation incorporating special business SQL fragments, and unified data access. The reasoning process is divided into a multi-level hierarchical reasoning scheme that combines dimensional indicator reasoning, data reasoning, table reasoning, and query SQL.
[0314] The detailed process is as follows:
[0315] I. Breakdown of Zero-Shot's Business-Knowledge-Integrated Elements:
[0316] By employing Retrieval Enhancement Generation (RAG) technology, user questions are supplemented with semantically relevant business knowledge context. Relevant business terms are extracted through word segmentation, and then, based on business knowledge, a business vocabulary, and basic dimensions and metrics, the user question is decomposed into elements, including question-related dimensions, metrics, and other data elements, while preserving the relationships between these elements. This step utilizes the Zero-shot technique, transferring existing semantic embedding knowledge from a large model to the understanding of relationships within the current user question, thus enabling attribute reasoning.
[0317] 1) Business knowledge supplementation issue: forming a complete query that integrates business knowledge.
[0318] Input: User question, user knowledge, login information, current time.
[0319] Processing: By semantically matching the user's question with the business knowledge base, relevant knowledge is matched out. Then, the question is rewritten using a large parameter (Qwen3-32B) model. The prompt words "relevant knowledge should be incorporated" and "pronoun conversion" are used to break down and output information such as query content, relevant knowledge, and display format requirements.
[0320] Output: Feature recognition results, in the format of "Content Type | Content Description" (Content Type: Query, Knowledge, Display, Calculation, Interpretation, Export, Other).
[0321] For example, the question "Statistics on the data assets of each subordinate unit according to asset classification" should be rewritten as "Statistics on the data assets of each subordinate unit according to asset classification (including original asset value, net asset value, and asset quantity)".
[0322] 2) Content type breakdown: Based on the content types involved in the user's question, content blocks of different types are broken down. Content types are categorized as: query, display, calculation, interpretation, export, and others.
[0323] For example, if the question is "Query the sales figures for each department last month", the corresponding breakdown is shown in Table 4:
[0324] Table 4
[0325]
[0326] 3) Enhanced decomposition and fine-tuning of business terms based on dimensions / indicators: The “query” content block in the previous decomposition block is decomposed into elements, and the relevant dimensions (dims), indicators (indices), other data elements (bd_vals), and element relationships (conditinos) in the user’s question are decomposed based on the known basic dimension indicators.
[0327] It is known that the basic dimensions and basic metrics are selected based on the Chinese names / aliases configured for the actual table fields.
[0328] For example, if the question is "Statistics on the quantity of data assets for each unit according to asset category", the dimensions involved are "unit (organization)" and "asset category", and the indicator is "asset quantity". The word segmentation result is "asset category, data, asset quantity". Based on the above information, the large model can break down the corresponding element information as follows:
[0329] {
[0330] "dims": {
[0331] "Unit (Organizational Structure)": "Grouping Dimension",
[0332] Asset Classification: Grouping Dimensions
[0333] },
[0334] "indices": [
[0335] "Amount of Assets"
[0336] ],
[0337] "bd_vals": [
[0338] "data"
[0339] ],
[0340] "conditions": []
[0341] }
[0342] II. Multi-level element reasoning and alignment based on reasoning (distillation).
[0343] We acquire dimension, indicator, other element data and element relationship information, use vectorization technology (Embedding) to retrieve relevant data sources, and improve accuracy through reranking. We perform multi-level data element alignment on non-dimensional indicator data to find the element type and element information corresponding to each data element. The element types are divided into: fields and basic data. We use rules-based main table voting and large model to determine the voting results.
[0344] During the initial element inference alignment, this solution semantically matches relevant business table fields, basic data table names, and record values based on the dimensions, metrics, filtering dimensions / metrics, and filtering values derived from the element decomposition. The matched element-related results are then provided to the large model from the perspectives of dimensions, corresponding dimension values, and metrics. This allows the large model to combine the matched contextual information to perform further element inference on the user's question, breaking it down into elements with actual field names and data record values.
[0345] Based on the actual field names and data record values from the initial inference, we can find the corresponding actual tables in the database information. By analyzing the relationships between these actual tables, we can obtain a list of matching fields for each table. Finally, we can calculate the score for each table by summing the scores of the matching fields for each element. For example, in the user question "Calculate the original value of fixed assets by asset type," the matching elements are: asset type, fixed assets, and original value. The corresponding table matching results are shown in Table 5.
[0346] Table 5
[0347]
[0348] Note: In the above results, the Business Table (Basic Asset Information), the Competent Authority's Summary of Asset Disposal Table, and the Asset Disposal Execution Sub-Table are all related to the Asset Classification Table (Basic Data Table). Therefore, the "Fixed Assets" record value is also a relevant element in these main tables and will be weighted by a score.
[0349] Based on the above-mentioned dimensions, indicators, and basic data records, we enable the large model to select the main query table according to the business meaning and completeness of each table's elements, thus accurately inferring the relevant tables that the user should query.
[0350] After aligning to the main query table, we limit the table range for field location. However, some query elements may not find corresponding actual fields. Therefore, we perform a round of element inference and alignment on the inferred data elements, further matching relevant dimension fields and their descriptions, related basic data records, and having the large model regenerate the filtering conditions for that data element. For example, for the data element "last three years," we provide the "business time" field and its description "in yyyy-MM-dd format" to the large model. The large model will then infer the alignment result as follows:
[0351] {
[0352] "filter_type": "field",
[0353] "object_name": "Business Date",
[0354] "value": [
[0355] "2022-10-24",
[0356] "2025-10-24"
[0357] ],
[0358] "relation": "BETWEEN"
[0359] }
[0360] Description of result fields:
[0361] 1) filter_type: Filter type, the value may be field / basic data / unknown / none.
[0362] If the term has a business meaning but there is no corresponding basic data record or related field information, the filter type will be "unknown".
[0363] If the word has no business meaning or no filtering semantics, the filter type is "None".
[0364] 2) object_name: The English name of the entity to be filtered, which may be the table name or field name of the underlying data table.
[0365] 3) value: The value to be filtered. It may be the CODE of the base data table or a fixed value. If there are multiple values or time ranges, please use an array.
[0366] 4) Relation: The filtering relationship, conforming to the filter symbols in the SQL WHERE filter statement, such as (=, BETWEEN, etc.). In summary, this solution identifies the main dimensions (dimensional fields, underlying data tables), indicators (indicator fields, underlying data record values), and underlying data record values through initial inference and alignment of the decomposed elements, while simultaneously generating constant values as initial inferred elements. Then, based on the tables, dimensions, indicators, and corresponding underlying data are organized for table inference and alignment to identify the main query table. Finally, based on the range of the main table, the actual fields and underlying data values are located. Finally, inference and alignment of the filter type are performed on the inferred data elements to generate supplementary filter information, thus obtaining all relevant SQL generation fragments.
[0367] III. SQL generation incorporating special business SQL fragments.
[0368] In the SQL generation stage, conventional text-based SQL generation solutions provide the user question and complete table schema information for the large model to generate SQL. However, in this invention's SQL generation stage, in addition to the user question, the table information is provided in the form of SQL fragments, including:
[0369] 1. Tree-based basic data filtering sub-statements:
[0370] 1.1. If the question asks "Statistical analysis of the original value of fixed assets by asset category", the "Original value of fixed assets" needs to be filtered for the relevant values of "Fixed Assets (1601)" in the "Financial Accounting Subject Code" table. The corresponding filter sub-statement is: 'SELECT NAME,CODEFROM ELE_VD08124 WHERE CODE = '1601''.
[0371] 2. Complex calculations are templated using SQL:
[0372] 2.1. When asked for "percentage", the SQL template used is:
[0373] SELECT [{AUTO_GROUP_BY}, ]
[0374] {AGGRE_FUNC}({INDEX_FIELD_NAME}) AS "Total {name of denominator indicator used in percentage calculation}",
[0375] {AGGRE_FUNC}({NUMERATOR_INDEX_FIELD}) AS "{Name of the numerator index used in the percentage calculation}",
[0376] (CASE WHEN {AGGRE_FUNC}({INDEX_FIELD_NAME}) > 0 THEN
[0377] ROUND({AGGRE_FUNC}({NUMERATOR_INDEX_FIELD}) * 100.0 / {AGGRE_FUNC}({INDEX_FIELD_NAME}), 2),
[0378] ELSE 0 END) AS "{USE_DEF_CONDITION_NAME}'s share"
[0379] 3. Default filter fields are dynamically generated:
[0380] 3.1. For example, the default query is_deleted=1.
[0381] 4. Custom SQL metrics:
[0382] 4.1. If the "Asset Status" is configured as a custom indicator, it can be used to query three indicators: original asset value, net asset value, and asset quantity.
[0383] By providing SQL sub-statements, many related prompts are eliminated when generating SQL from large models (such as "keep two decimal places when calculating ratios, exclude null values from field values, and the denominator cannot be 0"), effectively reducing the length of prompts and generating more accurate SQL.
[0384] 5. Integrate data permissions into SQL parsing: Add access control logic when generating SQL to ensure data security.
[0385] IV. Unified access to business data:
[0386] Data visualization presents the results of SQL execution to users in a visual manner. The implementation path includes:
[0387] Unified business data access: Access all data services through a unified interface.
[0388] SQL execution: Running the generated SQL within the data platform.
[0389] Data platform: As an intermediate layer, it integrates data from different systems.
[0390] Data services: Provide API or report outputs for front-end display.
[0391] Process summary and results explanation:
[0392] The above process systematically decomposes the NL2SQL task into multi-layered inference subtasks and innovatively combines the collaborative invocation of large and small parameter pre-trained language models (PLMs) to achieve efficient and accurate SQL generation. The core advantage of this framework is:
[0393] Layered task design and model adaptation: By breaking down the complex NL2SQL process into three main modules—feature decomposition, feature alignment, and SQL generation—and dynamically allocating high-parameter models (such as Qwen3-32B) and low-parameter models (such as Qwen3-8B) based on the complexity of the sub-tasks, the reasoning depth of key links (such as business knowledge integration and complex SQL generation) is guaranteed, while the overall computational efficiency is improved and resource waste is avoided.
[0394] Contextual Enhancement and Multi-level Reasoning: By fully utilizing contextual information such as business knowledge bases and dimensional indicator libraries, combined with Retrieval Enhancement Generation (RAG) and distilled multi-level feature alignment, the accuracy and robustness of feature identification are significantly improved. A two-level semantic matching mechanism, employing coarse ranking using a vector model and fine ranking using a re-ranking model, effectively resolves data feature ambiguity and reduces the false matching rate.
[0395] SQL generation optimization: Special business SQL fragments (such as tree-structured filtering sub-statements and templated calculation expressions) are introduced as the generation context, which simplifies the design of prompt words, reduces the burden on large models, and makes the generated SQL more in line with business rules and performance requirements, while supporting complex query scenarios (such as percentage calculation and multi-table joins).
[0396] This method achieves a balance between accuracy, efficiency, and versatility in NL2SQL tasks through a modular and scalable architecture, making it particularly suitable for the complex query needs of enterprise-level business databases. Experiments show that, compared to traditional end-to-end methods, this framework improves SQL generation accuracy by approximately 15% on multiple benchmark datasets, while optimizing inference speed by over 20%, demonstrating significant practical value and promising prospects for wider application.
[0397] It is worth noting that the acquisition, storage, use, and processing of data in the technical solution of this application all comply with relevant laws and regulations. The user information in the embodiments of this application was obtained through legal and compliant means, and the acquisition, storage, use, and processing of user information have been authorized and agreed upon by the client.
[0398] It is worth noting that the information collected in this application is information and data authorized by the user or fully authorized by all parties, and the collection, storage, use, processing, transmission, provision, disclosure and application of the relevant data all comply with the relevant laws, regulations and standards of the relevant countries and regions, necessary confidentiality measures have been taken, and they do not violate public order and good morals. Corresponding operation portals are provided for users to choose to authorize or refuse.
[0399] It is worth noting that the technical solution provided in this application provides users with a corresponding operation entry point, allowing users to choose to agree to or reject the automated decision-making result; if the user chooses to reject, the process will proceed to the expert decision-making process.
[0400] The technical solution of the NL2SQL statement conversion method based on a multi-level inference framework provided in this invention embodiment receives user input questions; based on a pre-trained language model, multi-level inference is performed on the user questions and element alignment is performed to generate aligned database elements. The pre-trained language model includes a large-parameter language model and a small-parameter language model; through the large-parameter language model, a statement is generated based on the user questions and aligned database elements to obtain the target SQL statement; PLM is injected hierarchically based on the importance of the database schema to avoid information overload; database elements related to user intent are located through a three-level inference of table-field-data; the large-parameter language model and the small-parameter language model are optimized collaboratively to improve domain adaptability, thereby further improving the accuracy of the SQL statement.
[0401] Figure 4 This is a schematic diagram of a statement conversion device for NL2SQL based on a multi-layer inference framework, provided in an embodiment of the present invention. This device is used to execute the aforementioned statement conversion method for NL2SQL based on a multi-layer inference framework, such as... Figure 4 As shown, the device includes: a receiving unit 11, a multi-layer reasoning unit 12, and a statement generation unit 13.
[0402] The receiving unit 11 is used to receive user questions input by the user.
[0403] The multi-level inference unit 12 is used to perform multi-level inference on user questions and align elements based on a pre-trained language model to generate aligned database elements. The pre-trained language model includes a large parameter language model and a small parameter language model.
[0404] The statement generation unit 13 is used to generate the target SQL statement by using a large parameter language model based on the user question and aligned database elements.
[0405] In this embodiment of the invention, the aligned database elements include target table elements, target field elements, and target basic data elements; the multi-layer inference unit 12 is specifically used to decompose the user problem into elements through a large parameter language model and a small parameter language model, and generate rewriting problems and element identification results; through the small parameter language model, according to the three-level inference framework of table-field-data, collaborative inference is performed on the rewriting problems and element identification results to generate target table elements, target field elements, and target basic data elements.
[0406] In this embodiment of the invention, the multi-layer inference unit 12 is specifically used to perform semantic matching and business term extraction on user questions to obtain corresponding business knowledge bases and business terms; rewrite user questions based on the user questions and corresponding business knowledge bases using a large parameter language model to generate rewritten questions; identify hierarchical information in user questions using a small parameter language model; and perform element identification based on rewritten questions, business terms, and hierarchical information using a small parameter language model to generate element identification results.
[0407] In this embodiment of the invention, the multi-layer inference unit 12 is specifically used to perform main table inference and alignment based on the feature recognition results using a small parameter language model to generate target table elements; and to perform field and basic data inference and alignment based on the rewrite question and the corresponding fields and basic data of the target table elements using a small parameter language model to obtain target field elements and target basic data elements.
[0408] In this embodiment of the invention, the device further includes an execution unit 14, a rewriting unit 15, and a drawing unit 16.
[0409] Execution unit 14 is used to execute the target SQL statement.
[0410] If execution fails, the rewriting unit 15 rewrites the target SQL statement to obtain the rewritten target SQL statement and triggers the execution unit 14 to repeat the execution of the target SQL statement.
[0411] The drawing unit 16 is used to draw charts and generate visual chart data based on the user's question, the target SQL statement and the execution success result information through a small parameter language model if the execution is successful.
[0412] In this embodiment of the invention, the rewriting unit 15 is specifically used to rewrite the target SQL statement by using a large parameter language model, based on the target SQL statement, execution failure result information, user questions, and aligned database elements, to obtain the rewritten target SQL statement.
[0413] In this embodiment of the invention, a user question is received from the user. Based on a pre-trained language model, multi-level reasoning is performed on the user question, and element alignment is performed to generate aligned database elements. The pre-trained language model includes a large-parameter language model and a small-parameter language model. Through the large-parameter language model, a statement is generated based on the user question and the aligned database elements to obtain the target SQL statement. The PLM is injected hierarchically based on the importance of the database schema to avoid information overload. The database elements related to the user intent are located through a three-level reasoning of table-field-data. The large-parameter language model and the small-parameter language model are optimized collaboratively to improve domain adaptability, thereby further improving the accuracy of the SQL statement.
[0414] The systems, devices, modules, or units described in the above embodiments can be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer device, specifically, a computer device can be, for example, a personal computer, a laptop computer, a cellular phone, a camera phone, a smartphone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or any combination of these devices.
[0415] This invention provides a computer device, including a memory and a processor. The memory is used to store information including program instructions, and the processor is used to control the execution of the program instructions. When the program instructions are loaded and executed by the processor, they implement the steps of the above-described embodiment of the NL2SQL statement conversion method based on a multi-level inference framework. For a detailed description, please refer to the above-described embodiment of the NL2SQL statement conversion method based on a multi-level inference framework.
[0416] The following is for reference. Figure 5 It shows a schematic diagram of the structure of a computer device 600 suitable for implementing the embodiments of this application.
[0417] like Figure 5 As shown, the computer device 600 includes a central processing unit (CPU) 601, which can perform various appropriate tasks and processes based on programs stored in read-only memory (ROM) 602 or programs loaded from storage section 608 into random access memory (RAM) 603. The RAM 603 also stores various programs and data required for the operation of the computer device 600. The CPU 601, ROM 602, and RAM 603 are interconnected via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0418] The following components are connected to I / O interface 605: an input section 606 including a keyboard, mouse, etc.; an output section 607 including a cathode ray tube (CRT), liquid crystal feedback (LCD), etc., and speakers, etc.; a storage section 608 including a hard disk, etc.; and a communication section 609 including a network interface card such as a LAN card, modem, etc. The communication section 609 performs communication processing via a network such as the Internet. A drive 610 is also connected to I / O interface 605 as needed. A removable medium 611, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on drive 610 as needed so that computer programs read from it can be installed in storage section 608 as needed.
[0419] In particular, according to embodiments of the present invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of the present invention include a computer program product comprising a computer program tangibly embodied on a machine-readable medium, the computer program including program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via communication section 609, and / or installed from removable medium 611.
[0420] Computer-readable media includes both permanent and non-permanent, removable and non-removable media that can store information by any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.
[0421] For ease of description, the above devices are described separately by function as various units. Of course, in implementing this application, the functions of each unit can be implemented in one or more software and / or hardware.
[0422] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0423] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0424] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0425] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0426] The acquisition, storage, use, and processing of data in this application all comply with the relevant provisions of national laws and regulations.
[0427] It should be noted that in the embodiments of this application, certain software, components, models and other existing solutions in the industry may be mentioned. These should be regarded as exemplary and are only intended to illustrate the feasibility of implementing the technical solution of this application. However, it does not mean that the applicant has used or necessarily used the solution.
[0428] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0429] This application can be described in the general context of computer-executable instructions, such as program modules, that are executed by a computer. Generally, program modules include routines, programs, objects, components, data structures, etc., that perform a specific task or implement a specific abstract data type. This application can also be practiced in distributed computing environments where tasks are performed by remote processing devices connected via a communication network. In distributed computing environments, program modules can reside in local and remote computer storage media, including storage devices.
[0430] The various embodiments in this specification are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the system embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions in the method embodiments.
[0431] The above description is merely an embodiment of this application and is not intended to limit this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principle of this application should be included within the scope of the claims of this application.
Claims
1. A statement transformation method for NL2SQL based on a multi-layer inference framework, characterized in that, The method includes: Receive user questions input by the user; Based on a pre-trained language model, multi-level reasoning is performed on the user question and element alignment is performed to generate aligned database elements. The pre-trained language model includes a large parameter language model and a small parameter language model. Using the large parameter language model, a statement is generated based on the user question and aligned database elements. During the statement generation process, a predefined business SQL fragment is incorporated as the generation context to obtain the target SQL statement. The aligned database elements include target table elements, target field elements, and target basic data elements; The process of performing multi-level reasoning and element alignment on the user question based on a pre-trained language model to generate aligned database elements includes: The user question is decomposed into elements using the large-parameter language model and the small-parameter language model to generate a rewritten question and element recognition results. Using the small-parameter language model, and following the three-level reasoning framework of table-field-data, collaborative reasoning is performed on the rewriting problem and the element identification results to generate the target table element, target field element, and target basic data element. The step of decomposing the user question into elements using the large-parameter language model and the small-parameter language model to generate a rewritten question and element recognition results includes: The user questions are semantically matched and business terms are extracted to obtain the corresponding business knowledge base and business terms. Using the large parameter language model, the user question is rewritten based on the corresponding business knowledge base to generate a rewritten question; The hierarchical information in the user's question is identified using the small-parameter language model. Using the small-parameter language model, element identification is performed based on the rewriting question, business terms, and hierarchical information to generate the element identification result. The process involves using the small-parameter language model and following a three-level reasoning framework of table-field-data to perform collaborative reasoning on the rewriting problem and feature identification results, generating the target table elements, target field elements, and target basic data elements, including: Using the small-parameter language model, the main table is inferred and aligned based on the feature recognition results to generate target table elements; Using the small-parameter language model, the target field elements and target basic data elements are obtained by inferring and aligning the fields and basic data according to the rewriting problem and the corresponding fields and basic data of the target table elements.
2. The statement transformation method for NL2SQL based on a multi-layer inference framework according to claim 1, characterized in that, The method further includes: Execute the target SQL statement; If the execution fails, the target SQL statement is rewritten to obtain the rewritten target SQL statement, and the step of executing the target SQL statement is repeated. If the execution is successful, the small-parameter language model is used to draw charts based on the user question, the target SQL statement, and the successful execution result information, generating visual chart data.
3. The statement transformation method for NL2SQL based on a multi-layer inference framework according to claim 2, characterized in that, The step of rewriting the target SQL statement to obtain the rewritten target SQL statement includes: Using the large parameter language model, the target SQL statement is rewritten based on the target SQL statement, execution failure result information, user questions, and aligned database elements to obtain the rewritten target SQL statement.
4. A statement conversion device for NL2SQL based on a multi-layer inference framework, characterized in that, The device includes: The receiving unit is used to receive user input questions; A multi-level inference unit is used to perform multi-level inference on the user question based on a pre-trained language model and perform element alignment to generate aligned database elements. The pre-trained language model includes a large parameter language model and a small parameter language model. The statement generation unit is used to generate a statement based on the user question and aligned database elements using the large parameter language model. During the statement generation process, a predefined business SQL fragment is incorporated as the generation context to obtain the target SQL statement. The aligned database elements include target table elements, target field elements, and target basic data elements; The multi-layer reasoning unit is specifically used to decompose the user question into elements using the large-parameter language model and the small-parameter language model, generating a rewritten question and element identification results; and to perform collaborative reasoning on the rewritten question and element identification results using the small-parameter language model according to the three-level reasoning framework of table-field-data, generating the target table element, target field element, and target basic data element. The multi-layer inference unit is further specifically used to perform semantic matching and business term extraction on the user question to obtain the corresponding business knowledge base and business terms; to rewrite the user question based on the user question and the corresponding business knowledge base using the large parameter language model to generate a rewritten question; to identify the hierarchical information in the user question using the small parameter language model; and to perform element recognition based on the rewritten question, business terms, and hierarchical information using the small parameter language model to generate the element recognition result. The multi-layer inference unit is also specifically used to perform main table inference and alignment based on the element recognition results using the small parameter language model to generate target table elements; and to perform field and basic data inference and alignment based on the rewrite question and the corresponding fields and basic data of the target table elements using the small parameter language model to obtain target field elements and target basic data elements.
5. A computer-readable medium having a computer program stored thereon, characterized in that, When executed by a processor, the program implements the statement transformation method of NL2SQL based on a multi-level inference framework as described in any one of claims 1 to 3.
6. A computer device comprising a memory and a processor, the memory for storing information including program instructions, and the processor for controlling the execution of the program instructions, characterized in that, When the program instructions are loaded and executed by the processor, they implement the statement transformation method of NL2SQL based on a multi-level inference framework as described in any one of claims 1 to 3.
7. A computer program product comprising a computer program / instructions, characterized in that, When the computer program / instruction is executed by the processor, it implements the statement transformation method of NL2SQL based on a multi-level inference framework as described in any one of claims 1 to 3.
Citation Information
Patent Citations
SQL (Structured Query Language) statement generation method based on large model, related device and storage medium
CN119166666A
Intelligent discriminant optimization system and method based on large language model
CN119759539A