A Text2SQL implementation method and system
Through the collaborative work of the planner, retriever, and generator, combined with multi-dimensional verification and diagnostic error correction mechanisms, the adaptability and interpretability issues of traditional Text2SQL methods in professional fields are solved, and highly accurate and interpretable SQL generation is achieved.
Patent Information
- Application Number
- CN202510935875.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-08
- Publication Date
- 2025-09-19
- Estimated Expiration
- 2045-07-08
AI Technical Summary
Traditional Text2SQL methods have problems in professional fields such as weak domain adaptability, insufficient knowledge integration, and poor interpretability, making it difficult to generate accurate SQL queries in complex business scenarios.
The planner performs intent analysis and domain classification, the retriever performs multi-way parallel retrieval to obtain domain knowledge and database table structure, the generator builds the SQL framework and performs multi-dimensional verification, and the diagnosis and error correction mechanism ensures the accuracy and interpretability of the generated SQL statements.
It improves the accuracy and robustness of SQL conversion in professional scenarios, enhances the user interaction experience, and provides explainable query logic and data source information.
Smart Images

Figure CN120470020B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing, and in particular to a Text2SQL implementation method and system. Background Art
[0002] With the deepening of digital transformation, the demand for precise natural language-to-SQL conversion is surging in specialized fields like finance and healthcare. Traditional Text2SQL approaches have significant limitations: weak domain adaptability. The complex business rules and database table structures in specialized scenarios make it difficult for general-purpose models to accurately understand domain requirements. Knowledge integration is insufficient, with domain knowledge and databases not deeply coupled, resulting in SQL generation logic that deviates from business realities. Furthermore, interpretability is poor, with only query results returned, leaving users unaware of key information such as data sources and computational logic. Therefore, a Text2SQL approach that adapts to specialized scenarios, integrates domain knowledge, and offers full-process closed-loop optimization and interpretability is urgently needed. Summary of the Invention
[0003] The purpose of the present invention is to provide a Text2SQL implementation method and system.
[0004] In a first aspect, an embodiment of the present invention provides a Text2SQL implementation method, including:
[0005] receiving a natural language query input by a user;
[0006] The planner performs intent analysis and processing path planning on the natural language query to obtain query intent, domain classification results and key entity information;
[0007] Based on the domain classification results and key entity information output by the planner, a multi-path parallel search is performed by a retriever to obtain domain knowledge, entity information, database table structure and historical query patterns related to the natural language query;
[0008] Based on the search results output by the retriever, a SQL framework is constructed by a generator to generate an initial SQL statement;
[0009] The initial SQL statement is verified by the validator in terms of syntax, table fields, permissions, and logic. If the verification passes, the verified SQL statement is executed; if the verification fails, the generator is returned and the SQL generation logic is adjusted based on the verification feedback to regenerate the initial SQL statement;
[0010] When executing a verified SQL statement, if the execution is successful, the query results are obtained and the query results are sorted, field mapped, and tabulated to obtain formatted results; if the execution fails, the diagnosis and error correction mechanism is activated, a corrected SQL statement is generated, and the corrected SQL statement is returned to the validator for re-verification;
[0011] Based on the formatted results and SQL execution logic, a natural language explanation including query logic, data source and calculation method is generated and output to the user.
[0012] In a possible implementation, the planner performs intent analysis and processing path planning on the natural language query to obtain query intent, domain classification results, and key entity information, including:
[0013] Performing intent recognition on the natural language query to determine a query type, where the query type includes factual query, statistical analysis, or trend prediction, and the query type constitutes the core content of the query intent;
[0014] Performing domain classification on the natural language query based on a preset industry classification system and concept classification system to obtain a domain classification result, wherein the domain classification result includes a first-level domain and a second-level domain;
[0015] Extracting key entities from the natural language query to obtain key entity information, wherein the key entity information includes company name, time range, and indicator name;
[0016] According to the query type and the complexity of the natural language query, the calling strategy of the subsequent retriever and generator is decided; wherein, the domain classification results, key entity information and calling strategy are used together as the retrieval basis for the retriever to perform multi-way parallel retrieval.
[0017] In a possible implementation, performing multi-path parallel searches by a search engine to obtain domain knowledge, entity information, database table structure, and historical query patterns related to the natural language query includes:
[0018] Retrieving corresponding business rules and indicator calculation methods from a professional knowledge base based on the field classification results to obtain the field knowledge;
[0019] Acquire entity attributes and associated information from an entity library based on the key entity information to obtain the entity information;
[0020] Based on the first-level domain, the second-level domain and the key entity information in the domain classification result, the relevant database tables and fields are located from the database metadata layer and the relationship between the tables is obtained to obtain the table structure information;
[0021] Match historical query patterns that are semantically similar to the natural language query to obtain a reference SQL structure and obtain the query pattern information; wherein the domain knowledge, entity information, table structure information and query pattern information together serve as basic data for the generator to construct an SQL framework.
[0022] In a possible implementation, the generating of an initial SQL statement by constructing an SQL framework based on the search results output by the retriever includes:
[0023] Based on the table structure information and query intent, determine the database table to be queried and form a FROM clause of the SQL framework;
[0024] Based on the inter-table association relationship in the table structure information, generate JOIN conditions between the database tables and integrate them into the FROM clause;
[0025] According to the query intent and the determined database table, select the fields to be queried from the table structure information to form a SELECT clause of the SQL framework;
[0026] Convert the screening conditions in the natural language query into SQL conditional expressions in combination with the entity information, and construct a WHERE clause of the SQL framework;
[0027] According to the query type in the query intent, the corresponding aggregation function and grouping logic are selected, and the indicator calculation rules in the domain knowledge are integrated to supplement and improve the SELECT clause and WHERE clause to obtain the initial SQL statement.
[0028] In a possible implementation, the performing of syntax, table field, permission, and logic verification on the initial SQL statement by a validator includes:
[0029] Checking whether the initial SQL statement complies with SQL syntax specifications to implement syntax verification;
[0030] Based on the table structure information, check whether the table and field referenced in the SQL statement exist and are valid to implement table and field verification;
[0031] Confirm that the current user has the access rights to query the tables and fields to implement permission verification;
[0032] Evaluate the consistency of SQL statement logic with the query intent through semantic comparison to achieve logic verification;
[0033] Evaluate the execution resource consumption of SQL statements to avoid overly complex operations to achieve complexity verification;
[0034] When verification fails, the validator outputs a specific error type, which includes a syntax error or a non-existent field. The specific error type serves as a basis for the generator to adjust the SQL generation logic.
[0035] In one possible implementation, the process of the diagnosis and error correction mechanism includes:
[0036] Classify execution failure types into syntax errors, semantic errors, data missing errors, or execution timeouts to achieve error classification;
[0037] Locate specific issues based on the error type and the table structure information and entity information, including incorrect field names, incorrect table association conditions, or overly strict query conditions, to achieve root cause analysis;
[0038] Based on the root cause analysis results, table-level, field-level, or condition-level corrections are used to select a correction strategy. Table-level corrections refocus related tables based on table structure information, field-level corrections replace entity information with valid fields, and condition-level corrections relax screening conditions.
[0039] The erroneous part of the SQL statement is adjusted and the correct logic is retained to generate a corrected SQL statement to achieve incremental correction; wherein, the corrected SQL statement is verified by the validator and then executed again by the executor.
[0040] In a possible implementation, performing data sorting, field mapping, and tabular processing on the query results to obtain formatted results includes:
[0041] Sorting, removing duplicates and filtering out null values of the query results to achieve data organization;
[0042] Converting database field names into business terms based on the entity information to implement field mapping;
[0043] Perform unit conversion or percentage calculation based on the indicator type and the unit rules in the domain knowledge to achieve data conversion;
[0044] The processed data is formatted into a structured table to obtain the formatted result; wherein the formatted result is used as input data for generating a natural language interpretation.
[0045] In one possible implementation, generating a natural language explanation including query logic, data source, and calculation method based on the formatted result and SQL execution logic includes:
[0046] Based on SQL execution logic, use natural language to describe query targets, table association methods, and screening conditions to implement query logic interpretation;
[0047] Based on the table structure information, describe the database table and field corresponding to the result data and mark the data validity to achieve data source description;
[0048] Based on the domain knowledge, explain the calculation formulas and business implications of the complex indicators involved to achieve calculation logic elaboration, the complex indicators including net profit growth rate;
[0049] Based on the formatted results, trend analysis or abnormal value prompts are provided to implement result analysis prompts; wherein the natural language interpretation is adapted to the user's business knowledge level to avoid the use of technical terms.
[0050] In a possible implementation, the method further includes a non-intrusive fusion mechanism of expert knowledge and database schema, wherein the non-intrusive fusion mechanism includes:
[0051] Constructing a multi-level knowledge classification system, which includes an industry classification layer, a concept classification layer, a first-level domain classification layer, a second-level domain classification layer, and a table classification layer, to achieve the mapping of business concepts to database tables and provide a classification basis for the planner's domain classification;
[0052] Establishing a metadata table to store domain knowledge and data structure mapping relationships, providing metadata support for the table structure information retrieval of the retriever;
[0053] Use dynamic table fetchers to associate business concepts with newly added database tables in real time, and use fixed table fetchers to maintain a stable mapping of core table structures;
[0054] Build a professional knowledge base, integrate the domain expert rule base and industry terminology base, and realize the association reasoning between entities and indicators through the knowledge graph;
[0055] Embedded vector indexing technology is used to vectorize table field information to support retrieval based on semantic similarity. The non-intrusive fusion mechanism provides knowledge support for the multi-way parallel retrieval of the retriever and the SQL framework construction of the generator.
[0056] In a second aspect, an embodiment of the present invention provides a Text2SQL implementation system, including:
[0057] An acquisition module for receiving natural language queries input by users;
[0058] An execution module is used to perform intent analysis and processing path planning on the natural language query through a planner to obtain query intent, domain classification results and key entity information; based on the domain classification results and key entity information output by the planner, perform multi-path parallel retrieval through a retriever to obtain domain knowledge, entity information, database table structure and historical query patterns related to the natural language query; based on the retrieval results output by the retriever, build an SQL framework through a generator to generate an initial SQL statement; perform syntax, table fields, permissions and logic verification on the initial SQL statement through a validator, and if the verification passes, execute the verified SQL statement; if the verification fails, return to the generator and adjust the SQL generation logic based on the verification feedback to regenerate the initial SQL statement; when executing the verified SQL statement, if the execution is successful, obtain the query result and perform data sorting, field mapping and tabular processing on the query result to obtain a formatted result; if the execution fails, start the diagnosis and error correction mechanism, generate a corrected SQL statement and return it to the validator for reverification;
[0059] The output module is used to generate a natural language explanation including query logic, data source and calculation method based on the formatted results and SQL execution logic, and output it to the user.
[0060] Compared with existing technologies, the present invention provides the following beneficial effects: Using a Text2SQL implementation method and system disclosed in the present invention, a planner analyzes query intent, domain classification, and key entities by receiving natural language queries; a retriever acquires domain knowledge, entity information, database table structure, and historical query patterns in parallel based on the planning results; a generator constructs an SQL framework based on the search results and generates initial statements; a validator performs multi-dimensional verification of SQL syntax, table fields, permissions, and logic, and iteratively adjusts the generation logic if failure occurs; and when executing SQL, if success is achieved, the results are formatted and a natural language explanation containing the query logic, data source, and calculation method is generated; if failure occurs, a diagnostic and error correction mechanism is activated and then re-verified. This method improves the accuracy, robustness, and user interaction experience of SQL conversion in professional scenarios through deep integration of domain knowledge, full-process verification and error correction, and enhanced interpretability. BRIEF DESCRIPTION OF THE DRAWINGS
[0061] To more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly describes the drawings required for use in the embodiments. It should be understood that the following drawings illustrate only certain embodiments of the present invention and should not be construed as limiting the scope of the present invention. Those skilled in the art can, without inventive effort, derive other relevant drawings from these drawings.
[0062] Figure 1A schematic block diagram illustrating the steps of the Text2SQL implementation method provided in an embodiment of the present invention;
[0063] Figure 2 A schematic diagram of the workflow of the Text2SQL system provided in an embodiment of the present invention;
[0064] Figure 3 A schematic diagram of the Text2SQL implementation system process framework provided by an embodiment of the present invention;
[0065] Figure 4 Schematic diagram of the fusion mechanism of expert knowledge and database schema provided by an embodiment of the present invention;
[0066] Figure 5 A schematic block diagram of the structure of a Text2SQL implementation device provided in an embodiment of the present invention;
[0067] Figure 6 A schematic block diagram of the structure of a computer device provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0068] To make the objectives, technical solutions, and advantages of the embodiments of the present invention more apparent, the technical solutions of the embodiments of the present invention will be described clearly and completely below in conjunction with the accompanying drawings of the embodiments of the present invention. It should be understood that the described embodiments are only a portion of the embodiments of the present invention, not all of them. Generally, the components of the embodiments of the present invention described and illustrated in the drawings herein may be arranged and designed in a variety of different configurations.
[0069] The specific embodiments of the present invention are described in detail below with reference to the accompanying drawings.
[0070] In order to solve the technical problems in the above background technology, Figure 1 This is a flowchart of the Text2SQL implementation method provided in an embodiment of the present disclosure. The Text2SQL implementation method is introduced in detail below.
[0071] Step S201, receiving a natural language query input by a user;
[0072] Step S202: performing intent analysis and processing path planning on the natural language query through a planner to obtain query intent, domain classification results, and key entity information;
[0073] Step S203: Based on the domain classification results and key entity information output by the planner, a multi-path parallel search is performed by the retriever to obtain domain knowledge, entity information, database table structure, and historical query patterns related to the natural language query;
[0074] Step S204: Based on the search results output by the retriever, a SQL framework is constructed by a generator to generate an initial SQL statement;
[0075] Step S205: Verify the syntax, table fields, permissions, and logic of the initial SQL statement through the validator. If the verification passes, execute the verified SQL statement; if the verification fails, return to the generator and adjust the SQL generation logic based on the verification feedback to regenerate the initial SQL statement;
[0076] Step S206: When executing the SQL statement that has passed the verification, if the execution is successful, the query results are obtained and the query results are sorted, field mapped, and tabulated to obtain formatted results; if the execution fails, the diagnosis and error correction mechanism is activated, and a corrected SQL statement is generated and returned to the validator for re-verification;
[0077] Step S207: Based on the formatted result and the SQL execution logic, a natural language explanation including query logic, data source and calculation method is generated and output to the user.
[0078] In an exemplary embodiment of the present invention, a natural language query is first received from a user. This query can cover business requirements in specialized fields such as finance, such as "Changes in a certain entity's core financial indicators over the past three years." Upon receiving the query, the server invokes a planner to analyze the query intent and plan a processing path. Relying on a multi-level classification model and rule engine, the planner first identifies the query intent, determining whether it falls into categories such as factual queries and statistical analysis (e.g., the aforementioned query on financial indicators falls into the statistical analysis category). It then performs domain classification to clarify the query's domain (e.g., financial sub-sectors such as stocks and funds). Entity recognition then extracts key entities, such as the entity name, time range, and indicator name. Finally, the processing path is determined based on the query's complexity and type. Simple single-table queries can streamline the process, while complex queries involving multi-table joins require a full pipeline. For example, in the financial context of "Querying a certain entity's net profit growth rate over the past three years," the planner identifies the intent as statistical analysis and the domain as individual stock finance. It extracts entity information such as "certain entity," "over the past three years," and "net profit growth rate," and initiates the full processing logic, including multi-table search and complex SQL generation.
[0079] Based on the domain classification results and key entity information output by the planner, the server invokes the retriever to perform multiple parallel searches. The retriever employs a hybrid search strategy, simultaneously retrieval of domain knowledge, entity information, table structure, and query pattern information. During the domain knowledge search, based on query intent and keywords, it locates industry calculation rules and financial business definitions for "net profit growth rate." During the entity information search, it expands information on the "entity" to include its industry and listed sector. During the table structure search, it locates database tables and corresponding fields storing the entity's basic information and financial data (e.g., the identifier field in the entity information table, and the net profit and reporting period fields in the financial data table). During the query pattern search, it matches historical "entity + time range + financial indicator" query patterns and corresponding SQL structures. For example, in the aforementioned financial query scenario, the retriever will retrieve the domain rule that "net profit growth rate must be calculated based on consecutive annual net profits," the corresponding financial data storage table for the entity's industry, and the table association and field selection logic for historical similar queries, providing a comprehensive reference for subsequent SQL generation.
[0080] Based on the search results output by the retriever, the server invokes the generator to construct the SQL framework and generate the initial SQL statement. The generator, centered on a large-scale language model, combines search-enhanced generation technology with domain-specific prompt templates. It first determines the basic SQL structure, such as SELECT, FROM, and WHERE, based on the query intent. For multi-table queries, it generates table join logic (for example, linking the subject information table with the financial data table using the subject identifier field). It then translates natural language filter conditions (such as "the past three years" into a WHERE clause covering the reporting period) and aggregate function requirements (such as aggregation logic for growth rate calculations) into SQL clauses. It also incorporates specific generation rules for the financial sector, such as field selection logic for consolidated and quarterly reports, and industry-standard formulas for calculating financial indicators. In this financial query scenario, the SQL framework constructed by the generator specifies the identifier and name from the subject information table, the annual net profit and reporting period from the financial data table, establishes table joins using the subject identifier, sets a filter condition for the reporting period of the past three years, and selects aggregation logic for growth rate calculations. Ultimately, it generates the initial SQL statement, including table joins, conditional filters, and aggregation calculations.
[0081] After generating the initial SQL statement, the server calls the validator to perform multi-dimensional verification. The validator uses a combination of rule checking and model evaluation: syntax checking verifies whether the SQL conforms to the syntax specifications of the target database (such as MySQL, Oracle); table and field verification verifies whether the tables (such as the subject information table, financial data table) and fields (such as net profit and reporting period fields) referenced by the SQL actually exist; permission verification confirms that the query operation complies with the user's database access rights; logic verification evaluates whether the SQL logic is consistent with the original query intent (such as whether the "recent three years" time screening and "net profit growth rate" calculation logic are correctly reflected); complexity control evaluates whether the query has excessive resource consumption operations (such as deeply nested subqueries). If the verification finds a problem (such as the database name of a financial field does not match the SQL reference), the validator generates feedback including the error type, location, and correction direction, and returns it to the generator to adjust the logic and regenerate; if the verification passes, the SQL is passed to the executor for execution.
[0082] After receiving the validated SQL, the executor optimizes the query (e.g., simplifying subqueries, adjusting condition order, and adapting indexes), and then safely executes it in a controlled environment. The execution strategy is adjusted for different database types, and execution anomalies (e.g., interruption due to syntax errors, empty results due to missing data) are captured. If the execution is successful, the results are collected and passed to the formatter. If the execution fails, the diagnosis and error correction mechanism is activated. The diagnostic algorithm first classifies the errors (syntax, semantics, missing data, etc.), then locates the root cause (e.g., table-link errors due to misunderstood foreign keys, empty results due to overly strict filtering). Multi-level error correction is implemented based on the diagnosis, such as table-level error correction focusing on refactoring SQL for linked tables, field-level error correction to correct field names, and query condition error correction to relax the time range. The corrected SQL is generated and returned to the validator for reverification. For example, if the initial SQL fails due to an overly narrow range of "the past three years," the diagnosis identifies it as a missing data error. The error correction strategy adjusts the time range to "the past five years" to refactor the SQL, and the corrected SQL is returned to the validator for compliance reverification.
[0083] After successful execution, the formatter receives the results and performs user-friendly processing: data sorting, sorting the results (for example, displaying trends in ascending order by reporting period), and removing duplicates; field mapping, converting technical field names (such as "F110101") into business terms such as "net profit attributable to the parent company"; data conversion, performing unit conversions (for example, converting 10,000 yuan of net profit to 100 million yuan) and percentage calculations as needed; and tabular processing, organizing the results into a structured table to control data volume and ensure performance. For example, in the financial query scenario described above, the formatter sorts the original results, replaces technical field names, converts units, and then outputs a clear table.
[0084] Finally, the server invokes the interpreter to generate a natural language explanation based on the formatted results and SQL execution logic. Combining template generation with a large-scale language model, the interpreter explains the query logic (e.g., "This query links entity information and financial data tables, filtering data from the past three years to calculate the net profit growth rate"), the data source (e.g., "The data comes from the entity information database and the legal tables of the financial indicator database"), the calculation method (e.g., "The growth rate is calculated by dividing the difference between the current and previous year's net profits by the previous year's net profit"), and provides result analysis tips (e.g., "The results show that the entity's net profit has grown steadily over the past three years, reflecting improved profitability"). These explanations, delivered in plain language and blended with professional knowledge, help users understand the query process and the meaning of the results. Finally, the formatted results and natural language explanations are provided to users, completing the entire Text2SQL process. During this process, self-reflection and error correction mechanisms, as well as human-computer interaction feedback loops, continue to operate: self-reflection records error diagnosis and correction SQL to build an error pattern library, and optimizes strategies through result caching and manual feedback; human-computer interaction collects user reviews, error reports, and other feedback, adjusts query strategies, and updates knowledge bases and model parameters, allowing the system to continuously evolve over multiple executions, adapting to complex query requirements in professional fields and ensuring that Text2SQL conversion reaches efficient and practical levels in terms of accuracy, explainability, and robustness.
[0085] In an embodiment of the present invention, the planner performs intent analysis and processing path planning on the natural language query to obtain query intent, domain classification results and key entity information, which can be implemented through the following examples.
[0086] Performing intent recognition on the natural language query to determine a query type, where the query type includes factual query, statistical analysis, or trend prediction, and the query type constitutes the core content of the query intent;
[0087] Performing domain classification on the natural language query based on a preset industry classification system and concept classification system to obtain a domain classification result, wherein the domain classification result includes a first-level domain and a second-level domain;
[0088] Extracting key entities from the natural language query to obtain key entity information, wherein the key entity information includes company name, time range, and indicator name;
[0089] According to the query type and the complexity of the natural language query, the calling strategy of the subsequent retriever and generator is decided; wherein, the domain classification results, key entity information and calling strategy are used together as the retrieval basis for the retriever to perform multi-way parallel retrieval.
[0090] In an embodiment of the present invention, for example, when a server, acting as the execution entity, processes a natural language query, for example, "Query the year-on-year net profit growth rate of XX Company over the past five years," it first performs intent recognition to determine the query type. The server invokes a pre-trained intent classification model to analyze the "query" action and the statistical calculation objective, "year-on-year net profit growth rate," in the query, determining that the query type is statistical analysis. This type constitutes the core content of the query intent—that is, the user desires to calculate and present statistical dimensions of a specific entity's financial indicators within a specified timeframe. Next, the server performs domain classification based on a pre-set industry classification system and concept classification system. The industry classification system covers first-level domains such as finance and healthcare, with each first-level domain being subdivided into second-level domains (e.g., listed company finance and financial product transactions within the finance domain). The concept classification system associates concept sets within vertical domains such as finance and healthcare. Through text matching and semantic vector similarity calculation, the server matches keywords such as "net profit," "growth rate," and "company" with financial concepts within the financial domain, further identifying the second-level domain as listed company finance. The resulting domain classification results are "finance" for the first-level domain and "listed company finance" for the second-level domain. The server then performs key entity extraction: relying on the named entity recognition (NER) module, it identifies "XX Company" in the sentence as a company name entity, "the past five years" as a time range entity, and "year-on-year net profit growth rate" as an indicator name entity, integrating these information to form key entity information. Finally, the server determines the subsequent call strategy based on the query type and the complexity of the natural language query: the query type is statistical analysis and requires cross-linking calculations across the financial data table (storing annual net profit) and the entity information table (storing company identification), judging the complexity to be medium. Based on the complexity rule base, in medium-complexity scenarios, the decision retriever performs a full multi-channel parallel search (covering domain knowledge, entity information, table structure, and historical query patterns), and the decision generator calls an SQL generation strategy that supports multi-table joins and aggregate calculations. The above domain classification results, key entity information, and call strategy serve as the retrieval basis for the retriever to perform subsequent operations, ensuring that the retrieval and generation stages accurately match the domain attributes and business requirements of the user query.
[0091] In an embodiment of the present invention, the multi-path parallel search performed by the retriever to obtain the domain knowledge, entity information, database table structure and historical query pattern related to the natural language query can be implemented through the following examples.
[0092] Retrieving corresponding business rules and indicator calculation methods from a professional knowledge base based on the field classification results to obtain the field knowledge;
[0093] Acquire entity attributes and associated information from an entity library based on the key entity information to obtain the entity information;
[0094] Based on the first-level domain, the second-level domain and the key entity information in the domain classification result, the relevant database tables and fields are located from the database metadata layer and the relationship between the tables is obtained to obtain the table structure information;
[0095] Match historical query patterns that are semantically similar to the natural language query to obtain a reference SQL structure and obtain the query pattern information; wherein the domain knowledge, entity information, table structure information and query pattern information together serve as basic data for the generator to construct an SQL framework.
[0096] In an exemplary embodiment of the present invention, during the domain knowledge retrieval phase, the server, based on the domain classification results (the first-level domain is finance, and the second-level domain is listed company finance), conducts targeted access to a specialized knowledge base. This specialized knowledge base pre-stores business rules and indicator calculation methods for various vertical fields. Through keyword matching and semantic vector retrieval, the server locates the industry's general calculation rule for "year-on-year net profit growth rate," which requires calculation based on consecutive annual net profits using the formula "(current net profit - previous period net profit) ÷ previous period net profit × 100%." It also retrieves the time definition rule for "the past five years" in financial contexts (referring to five consecutive full fiscal years, excluding unaudited interim reporting periods). These business rules and calculation methods together constitute the domain knowledge, providing a basis for generating aggregation logic and time filtering logic in subsequent SQL statements.
[0097] Secondly, during the entity information retrieval phase, the server accesses the entity database based on key entity information (company name "XX Company," time range "Last Five Years," and indicator name "Year-on-Year Net Profit Growth Rate"). The entity database stores attributes and associated information for entities such as companies, time periods, and indicators. For "XX Company," attributes such as its industry (e.g., technology sector), listing code, and associated subsidiaries are retrieved. For "Last Five Years," the specific time period is calculated from the current year (e.g., 2024) to 2019-2023. For "Year-on-Year Net Profit Growth Rate," the database identifier (e.g., field "net_profit") and data update frequency of the underlying indicator "net profit" are linked to the database. These entity attributes and associated information are integrated into entity information, which is used to clarify the entity dimensions and data boundaries required for reference in SQL.
[0098] Next, to retrieve table structure information, the server combines the first-level domain (finance) and second-level domain (public company finance) of the domain classification with key entity information to access the database metadata layer. This metadata layer records the definitions, fields, and relationships of all database tables. For example, "Company Name" locates the "company_info" table (containing the fields company_id, name, and industry) that stores basic company information; "Net Profit" locates the "annual_finance" table (containing the fields company_id, year, net_profit, and revenue) that stores annual financial data; Foreign key constraints between tables (such as the relationship between "company_info.company_id" and "annual_finance.company_id") are used to determine the multi-table join logic; and field permissions are also verified (such as whether "net_profit" is read-only for the current user group). This ultimately creates table structure information containing table names, field lists, and inter-table association rules, providing direct reference for SQL FROM clauses and JOIN logic.
[0099] Finally, during the query pattern information retrieval phase, the server matches semantically similar requests against the historical query database. This database stores natural language queries submitted by past users and their corresponding SQL statements. Using text similarity models (such as BERT vector matching), the server identifies historical records with a semantic overlap of over 80% with the query "Query XX Company's net profit growth rate over the past five years" (e.g., "Query YY Company's revenue growth rate over the past three years"). The server then extracts the corresponding reference SQL structure. This SQL structure utilizes a multi-table self-join (the annual_finance table itself is joined by year), time range filtering (the WHERE clause limits the year interval), and aggregate function calculations (the SELECT clause nests the growth rate formula). This business-proven SQL structure serves as query pattern information, providing a template-level reference for the generator when building the SQL framework.
[0100] The above domain knowledge, entity information, table structure information, and query pattern information serve as the basic data for the generator to subsequently build the SQL framework, ensuring that the generation logic is consistent with professional field rules and adapts to database storage structure and historical practice paths.
[0101] In the embodiment of the present invention, the SQL framework is constructed by a generator based on the search results output by the retriever to generate an initial SQL statement, which can be implemented through the following examples.
[0102] Based on the table structure information and query intent, determine the database table to be queried and form a FROM clause of the SQL framework;
[0103] Based on the inter-table association relationship in the table structure information, generate JOIN conditions between the database tables and integrate them into the FROM clause;
[0104] According to the query intent and the determined database table, select the fields to be queried from the table structure information to form a SELECT clause of the SQL framework;
[0105] Convert the screening conditions in the natural language query into SQL conditional expressions in combination with the entity information, and construct a WHERE clause of the SQL framework;
[0106] According to the query type in the query intent, the corresponding aggregation function and grouping logic are selected, and the indicator calculation rules in the domain knowledge are integrated to supplement and improve the SELECT clause and WHERE clause to obtain the initial SQL statement.
[0107] In an embodiment of the present invention, for example, when the server processes a natural language query such as "Query the year-on-year growth rate of XX company's net profit in the past five years", it drives the generator to build an SQL framework and generate an initial SQL statement in steps based on the table structure information, domain knowledge, entity information, and other results output by the retriever:
[0108] First, the FROM clause is determined. The generator parses the table structure (including the "company_info" table that stores basic company information and the "annual_finance" table that records annual financial data). Combined with the query intent (cross-table association of company entities and financial data to calculate growth rates), it is clear that both tables need to be called simultaneously. Therefore, the FROM clause is constructed as "FROMcompany_info,annual_finance", anchoring the core table set of the data source.
[0109] Secondly, when generating JOIN conditions, the generator extracts the foreign key relationship between "company_info.company_id" and "annual_finance.company_id" in the table structure information, and based on this, generates the association logic of "JOIN annual_financeONcompany_info.company_id=annual_finance.company_id" and integrates it into the FROM clause to form "FROMcompany_info JOIN annual_finance ON company_info.company_id = annual_finance.company_id", ensuring that cross-table data is accurately linked according to the company's unique identifier.
[0110] Then, in the SELECT clause construction step, the generator selects the fields to be queried based on the query intent (the company name, fiscal year, net profit, and growth rate must be presented) and the table structure information ("company_info.name" stores the company name, "annual_finance.year" records the year, and "annual_finance.net_profit" stores the net profit value). The SELECT clause is initially constructed as "SELECTcompany_info.name,annual_finance.year,annual_finance.net_profit", clarifying the core data dimensions to be extracted.
[0111] Next, the WHERE clause is constructed. The generator combines entity information (the exact matching rule for "XX Company" corresponding to company_info.name, and "the past five years" is calculated to correspond to the 2019-2023 fiscal year period) to convert the natural language screening conditions into SQL expressions: "WHEREcompany_info.name='XX Company'ANDannual_finance.yearBETWEEN2019AND2023", accurately limiting the corporate entity and time range for data screening.
[0112] Finally, to supplement the aggregation and calculation logic, the generator identifies the query type as statistical analysis (needing to calculate the year-on-year growth rate). Referring to the calculation rule in the domain knowledge, "Year-on-year growth rate of net profit = (current net profit - previous net profit) ÷ previous net profit × 100%", the window function LAG is selected to implement cross-year data reference (using "LAG(annual_finance.net_profit,1)OVER(PARTITIONBYcompany_info.nameORDERBYannual_finance.year)" to obtain the previous year's net profit). This logic is then integrated into the SELECT clause: a new calculated column "(annual_finance.net_profit,1)OVER(PARTITIONBYcompany_info.nameORDERBYannual_finance.year)" is added based on the original fields. ce.net_profit - LAG(annual_finance.net_profit, 1) OVER(PARTITIONBYcompany_info.name ORDERBYannual_finance.year)) / LAG(annual_finance.net_profit, 1) OVER(PARTITIONBYcompany_info.name ORDERBYannual_finance.year) * 100ASgrowth_rate". At the same time, adjust the WHERE clause to ensure the continuity of the time series (for example, exclude the record logic of the first year without data from the previous year). Finally, integrate the clauses to generate the initial SQL statement:
[0113] {SELECT
[0114] company_info.name,
[0115] annual_finance.year,
[0116] annual_finance.net_profit,
[0117] (annual_finance.net_profit - LAG(annual_finance.net_profit, 1)OVER (PARTITION BY company_info.name ORDER BY annual_finance.year))
[0118] / LAG(annual_finance.net_profit, 1) OVER (PARTITION BY company_info.name ORDER BY annual_finance.year) * 100 AS growth_rate FROM company_info JOIN annual_finance ON company_info.company_id = annual_finance.company_id WHERE company_info.name = 'XX company' AND annual_finance.year BETWEEN 2019AND 2023;}.
[0119] This process breaks down natural language demands clause by clause and injects domain rules and table structure constraints to ensure that the initial SQL not only adapts to the database storage logic but also accurately carries the user's intention for statistical analysis queries.
[0120] In the embodiment of the present invention, the syntax, table fields, permissions and logic verification of the initial SQL statement by the validator can be implemented through the following examples.
[0121] Checking whether the initial SQL statement complies with SQL syntax specifications to implement syntax verification;
[0122] Based on the table structure information, check whether the table and field referenced in the SQL statement exist and are valid to implement table and field verification;
[0123] Confirm that the current user has the access rights to query the tables and fields to implement permission verification;
[0124] Evaluate the consistency of SQL statement logic with the query intent through semantic comparison to achieve logic verification;
[0125] Evaluate the execution resource consumption of SQL statements to avoid overly complex operations to achieve complexity verification;
[0126] When verification fails, the validator outputs a specific error type, which includes a syntax error or a non-existent field. The specific error type serves as a basis for the generator to adjust the SQL generation logic.
[0127] In an embodiment of the present invention, for example, when the server processes the initial SQL generated by "querying the year-on-year growth rate of XX company's net profit in the past five years", it calls the validator to perform multi-dimensional verification:
[0128] During syntax verification, the validator analyzes the statement structure word by word, relying on a SQL syntax rule library (covering the specifications of major database dialects). If "JOIN" in the initial SQL is mistakenly written as "JION," the syntax checker will detect the keyword typo and flag "Syntax error: JOIN keyword misspelled." If keywords, brackets, and quotation marks are used correctly in the statement (for example, in the example SQL, JOIN, ON, and WHERE are written correctly, and string quotes are enclosed), the syntax verification passes.
[0129] During table and field verification, the validator retrieves table structure information (including the "company_info" and "annual_finance" tables and field lists) to check referenced objects. If the SQL mistakenly specifies "annual_finance.net_profits" (the actual field is "net_profit"), the table and field verification module will compare the list and output "Field does not exist: annual_finance.net_profits is not a valid field." If all tables ("company_info" and "annual_finance") and fields (such as name, year, and net_profit) exist in the table structure information and are in the "available" state, table and field verification passes.
[0130] During the permissions verification phase, the validator queries the user permissions matrix (which stores user / group read and write permissions for tables and fields). If the current user group only has "Insert" permissions on "annual_finance.net_profit" and no "Query" permissions, the permissions verification module will determine "Insufficient permissions: user group does not have the SELECT permission on annual_finance.net_profit." However, if the user group has valid query permissions on the relevant tables (e.g., read permissions on all fields of "company_info") and fields (e.g., read permissions on "annual_finance.year" and "net_profit"), the permissions verification passes.
[0131] During the logic verification phase, the validator uses a semantic comparison model to deconstruct the SQL logic and the original intent. If the time range in the WHERE clause in the SQL is mistakenly written as "2018-2022" (which does not correspond to 2019-2023 for "the past five years"), the semantic model will identify the time dimension discrepancy and output "Logical inconsistency: The time range 2018-2022 does not match the 'past five years' requirement." If the table join (company_id matching), time filter (2019-2023), and growth rate calculation (window function implementing year-on-year logic) all align with the intent of "cross-table join companies, filter the past five years, and calculate annual growth rate," the logic verification passes.
[0132] During the complexity verification phase, the validator analyzes resource consumption based on a query complexity model (taking into account factors such as the number of table joins and subquery depth). For example SQL statements containing only two table joins and a single window function, the complexity score is below the threshold, and verification passes. However, if the SQL statement contains multiple nested joins, such as "FROM(SELECT*FROMAJOINB)AST1JOIN(SELECT*FROMCJOIND)," the model identifies resource risks and outputs "Complexity exceeds limit: Multiple nested table joins may cause performance degradation."
[0133] When validation fails, the validator combines the error type (e.g., "Syntax Error," "Field Does Not Exist") with the location information and returns it to the generator. The generator then adjusts its logic accordingly: if "Field Does Not Exist," it corrects the mapping rule from "Net Profit" to "net_profit." If "Syntax Error," it strengthens keyword spelling verification to ensure subsequent SQL compliance.
[0134] In the embodiment of the present invention, the process of the diagnosis and error correction mechanism can be implemented through the following examples.
[0135] Classify execution failure types into syntax errors, semantic errors, data missing errors, or execution timeouts to achieve error classification;
[0136] Locate specific issues based on the error type and the table structure information and entity information, including incorrect field names, incorrect table association conditions, or overly strict query conditions, to achieve root cause analysis;
[0137] Based on the root cause analysis results, table-level, field-level, or condition-level corrections are used to select a correction strategy. Table-level corrections refocus related tables based on table structure information, field-level corrections replace entity information with valid fields, and condition-level corrections relax screening conditions.
[0138] The erroneous part of the SQL statement is adjusted and the correct logic is retained to generate a corrected SQL statement to achieve incremental correction; wherein, the corrected SQL statement is verified by the validator and then executed again by the executor.
[0139] In an embodiment of the present invention, for example, when executing a verified SQL statement, if the execution fails, the server initiates a diagnostic and error correction mechanism. For example, if the SQL statement corresponding to "querying the year-on-year growth rate of XX company's net profit over the past five years" returns an empty result, the specific process is as follows:
[0140] During the error classification phase, the server captures the database execution response (such as "0 rows affected") and, combined with the error feature library matching rules, determines that the execution failure type is a data missing error (distinguished from syntax errors, semantic errors, execution timeouts, and other categories).
[0141] During the root cause analysis phase, the server retrieved table structure information (the "annual_finance" table stores XX Company's financial data covering only the years 2020-2023) and entity information (the "recent five years" period is estimated to correspond to the 2019-2023 time period). After cross-analysis, the specific issue was identified: because XX Company's 2019 financial data was not entered into the "annual_finance" table, the SQL query "annual_finance.yearBETWEEN2019AND2023" filter condition had no valid data matching it. In other words, the overly strict query condition was the root cause of the execution failure.
[0142] In the correction strategy selection phase, based on the root cause of "query conditions are too strict", the server selects condition-level error correction (matching this scenario in table-level error correction, field-level error correction, and condition-level error correction): It decides to relax the time filtering dimension and adjust "recent five years" to "recent four years (2020-2023)" to adapt to the actual data range.
[0143] During the incremental correction phase, the server accurately locates the time condition in the WHERE clause of the SQL statement ("annual_finance.yearBETWEEN2019AND2023") and adjusts only that segment to "annual_finance.yearBETWEEN2020AND2023." It retains the correct parts, including the table join logic ("company_infoJOINannual_financeONcompany_info.company_id=annual_finance.company_id"), the field selection in the SELECT clause, and the growth rate calculation logic ("(annual_finance.net_profit-LAG(...)) / LAG(...)*100ASgrowth_rate"), and generates the corrected SQL statement:
[0144] {SELECT
[0145] company_info.name,
[0146] annual_finance.year,
[0147] annual_finance.net_profit,
[0148] (annual_finance.net_profit - LAG(annual_finance.net_profit, 1)OVER (PARTITION BY company_info.name ORDER BY annual_finance.year))
[0149] / LAG(annual_finance.net_profit, 1) OVER (PARTITION BY company_info.name ORDER BY annual_finance.year) * 100 AS growth_rate FROM company_info JOIN annual_finance ON company_info.company_id = annual_finance.company_id WHERE company_info.name = 'XX company' AND annual_finance.year BETWEEN 2020AND 2023;}
[0150] The corrected SQL statement is passed to the validator. After passing verification of syntax compliance, table field validity, permission matching, and logical consistency, it is executed again by the executor to ensure that the year-on-year growth rate data of XX Company's net profit from 2020 to 2023 can be obtained.
[0151] If other failure scenarios occur (for example, the initial SQL mistakenly writes "annual_finance.net_profits", resulting in an "unknown field" error, which is classified as a semantic error), the server will locate the root cause of the "field name error" and perform field-level correction (replacing "net_profits" with the valid field "net_profit" based on the entity information). After modifying only the field reference, the corrected SQL is generated and then executed after verification by the validator to ensure a closed-loop process.
[0152] In the embodiment of the present invention, the query results are subjected to data sorting, field mapping, and tabular processing to obtain formatted results, which can be implemented through the following examples.
[0153] Sorting, removing duplicates and filtering out null values of the query results to achieve data organization;
[0154] Converting database field names into business terms based on the entity information to implement field mapping;
[0155] Perform unit conversion or percentage calculation based on the indicator type and the unit rules in the domain knowledge to achieve data conversion;
[0156] The processed data is formatted into a structured table to obtain the formatted result; wherein the formatted result is used as input data for generating a natural language interpretation.
[0157] In an embodiment of the present invention, for example, after executing a verified SQL statement and obtaining the query results, the server performs data sorting, field mapping, and tabular processing for the scenario of "querying the year-on-year growth rate of XX company's net profit in the past five years":
[0158] During the data sorting phase, the server first sorts the original query results in ascending order by the "annual_finance.year" field to ensure that the data for 2020, 2021, 2022, and 2023 are arranged in chronological order; then, duplicate records are checked through row hash value comparison (such as the same company-year data rows generated due to table association logical redundancy) and duplicates are automatically eliminated; finally, null values are filtered and marked - if the "net profit year-on-year growth rate" in 2020 is NULL due to the lack of 2019 data, the server will retain the row and mark the growth rate field as "no previous year data reference" to ensure the integrity of the time dimension and data traceability.
[0159] During the field mapping phase, the server calls the pre-configured "technical field-business term" mapping rules in the entity information: "company_info.name" is mapped to "company name", "annual_finance.year" is mapped to "fiscal year", "annual_finance.net_profit" is mapped to "net profit attributable to the parent company (10,000 yuan)", and "growth_rate" is mapped to "net profit year-on-year growth rate (%)". The technical field names at the database level are converted into business domain expressions that are easy for users to understand, eliminating the gap between technical and business language.
[0160] During the data conversion phase, the server processes numerical values based on the "Financial Indicator Unit Specifications" in domain knowledge: if the original net profit data storage unit is "10,000 yuan", and the industry report convention is to present core financial indicators in "100 million yuan", the server automatically performs unit conversion (such as 1 million yuan ÷ 10,000 = 0.01 billion yuan); for the growth rate field (originally in decimal form, such as 0.2), it is converted to "20%" according to the percentage display rules in the financial field to ensure that the data presentation is consistent with the reading habits of professional scenarios.
[0161] In the tabular processing stage, the server organizes the sorted, mapped, and converted data into a structured table: the first row sets the column headers (company name, fiscal year, net profit attributable to the parent company (100 million yuan), year-on-year growth rate of net profit (%)), and the subsequent rows are filled with the data of each year in sequence (such as 2020 corresponds to "XX Company, 2020, 0.01, no reference to previous year's data", 2021 corresponds to "XX Company, 2021, 0.012, 20%", etc.), and at the same time, the column width, alignment and data highlighting rules are optimized through the front-end adaptation logic to ensure the visualization friendliness of the table and the efficiency of information transmission.
[0162] The formatted results generated through the above steps not only retain the original logic of the data but also meet the readability requirements of the business scenario. They can be directly used as input data for the subsequent generation of natural language explanations, providing a clear data basis for explaining the query logic and result meaning to users.
[0163] In an embodiment of the present invention, the generation of a natural language explanation including query logic, data source, and calculation method based on the formatted result and SQL execution logic can be implemented through the following examples.
[0164] Based on SQL execution logic, use natural language to describe query targets, table association methods, and screening conditions to implement query logic interpretation;
[0165] Based on the table structure information, describe the database table and field corresponding to the result data and mark the data validity to achieve data source description;
[0166] Based on the domain knowledge, explain the calculation formulas and business implications of the complex indicators involved to achieve calculation logic elaboration, the complex indicators including net profit growth rate;
[0167] Based on the formatted results, trend analysis or abnormal value prompts are provided to implement result analysis prompts; wherein the natural language interpretation is adapted to the user's business knowledge level to avoid the use of technical terms.
[0168] In an embodiment of the present invention, for example, when the server generates a natural language explanation for the execution result of the scenario "querying the year-on-year growth rate of XX company's net profit in the past five years", a multi-dimensional explanation is provided around the formatted result and the SQL execution logic:
[0169] During the query logic interpretation phase, the server parses the core elements of the SQL execution logic and translates them into business language: "This query aims to obtain the year-on-year growth rate of net profit for Company XX over the past five years. To achieve this, the system uses the company's unique identifier (company_id) in the database to link the company_info table, which stores basic company information, with the annual_finance table, which records annual financial data. The system also limits the filter conditions to 'Company Name is Company XX' and 'Fiscal Year is 2020-2023' to ensure that only valid financial records within the specified time range for the target company are retrieved."
[0170] In the data source description section, the server clearly marks the data source and validity based on the table structure information: "The result data comes from two core database tables: the first is the company_info table, from which the 'name' field is read to confirm that the query subject is XX Company; the second is the annual_finance table, from which the 'year' (fiscal year) and 'net_profit' (net profit) fields are read to obtain financial data. It should be noted that the 2019 data for XX Company in the annual_finance table has not been fully entered, so the valid data actually involved in the calculation covers 2020-2023."
[0171] When explaining the calculation logic, the server invokes domain knowledge to break down complex indicator logic: "The year-on-year growth rate of net profit is a core financial indicator for measuring changes in a company's profitability. Its calculation formula is '(current period net profit - previous period net profit) ÷ previous period net profit × 100%'. In this query, the system uses the SQL window function LAG to automatically retrieve the previous year's net profit value after sorting by fiscal year (for example, when calculating the 2021 growth rate, the LAG function extracts the 2020 net profit). This is then substituted into the formula to complete the percentage conversion, ensuring that the growth rate calculation adheres to financial business rules."
[0172] During the result analysis prompt, the server outputs a scenario-based interpretation based on the formatted results: "The formatted table shows that XX Company's net profit attributable to the parent company increased from 10 million yuan to 15 million yuan from 2020 to 2023, with year-on-year net profit growth rates of 20% in 2021, 25% in 2022, and 20% in 2023. The overall trend is 'steady growth, slightly accelerated, and then stable continuation,' reflecting the continued improvement in the company's product profitability or market share during the reporting period. Note that no growth rate was generated for 2020 due to the lack of comparable data for 2019. The data for that year serves only as a basic reference for calculations in subsequent years."
[0173] Throughout the natural language interpretation process, the server always replaces technical terms with a business perspective (for example, expressing "JOIN association" as "inter-table association" and "window function" as "automatically obtaining previous year's data") to ensure that users with different levels of business knowledge can understand the query logic, data sources, and result meanings. Ultimately, the integrated interpretation text is fed back to the user, completing the transformation from technical process to business cognition.
[0174] In an embodiment of the present invention, the method further includes a non-invasive fusion mechanism of expert knowledge and database schema. The non-invasive fusion mechanism can be implemented through the following examples.
[0175] Constructing a multi-level knowledge classification system, which includes an industry classification layer, a concept classification layer, a first-level domain classification layer, a second-level domain classification layer, and a table classification layer, to achieve the mapping of business concepts to database tables and provide a classification basis for the planner's domain classification;
[0176] Establishing a metadata table to store domain knowledge and data structure mapping relationships, providing metadata support for the table structure information retrieval of the retriever;
[0177] Use dynamic table fetchers to associate business concepts with newly added database tables in real time, and use fixed table fetchers to maintain a stable mapping of core table structures;
[0178] Build a professional knowledge base, integrate the domain expert rule base and industry terminology base, and realize the association reasoning between entities and indicators through the knowledge graph;
[0179] Embedded vector indexing technology is used to vectorize table field information to support retrieval based on semantic similarity. The non-intrusive fusion mechanism provides knowledge support for the multi-way parallel retrieval of the retriever and the SQL framework construction of the generator.
[0180] In an exemplary embodiment of the present invention, when the server constructs a non-intrusive fusion mechanism to support the Text2SQL process, it first constructs a multi-level knowledge classification system for financial query scenarios in the financial sector: a vertically divided industry classification layer (finance), a concept classification layer (financial indicators, corporate entities), a first-level domain classification layer (finance), a second-level domain classification layer (listed company finance), and a table-level classification layer (the company_info table is associated with corporate entity concepts, and the annual_finance table is associated with annual financial indicator concepts). When the planner processes the query "XX Company's net profit growth rate over the past five years," it relies on this system to quickly match the second-level domain "listed company finance" and locate the company_info and annual_finance tables in the table-level classification layer, providing a precise basis for domain classification.
[0181] Next, a metadata table is created: This table stores the mapping between domain knowledge such as "year-on-year net profit growth rate" and data structures. For example, recording this metric requires association with company_info (using the company ID), annual_finance (using the year and net profit fields), and inter-table foreign key constraints (company_id association). When performing a table structure search, the search engine directly retrieves the table name, field list, and association rules corresponding to "year-on-year net profit growth rate" in the metadata table, quickly locating core elements such as annual_finance.net_profit and company_info.name, providing metadata support for table structure searches.
[0182] Then, we deployed dynamic table fetchers and fixed table fetchers. The dynamic table fetcher monitors new database tables in real time (for example, when the quarterly financial table "quarterly_finance" is added), automatically associating business concepts such as "quarterly net profit growth rate" to ensure that the new table structure is promptly included in the search scope. The fixed table fetcher continuously maintains the mapping relationship between core tables such as company_info and annual_finance (for example, the company_info.name field is always marked as "full company name") to ensure the stability of the mapping of core table structures. When Company XX's business expands and requires querying quarterly data, the dynamic table fetcher quickly associates the new table, allowing the searcher to obtain the full financial table structure and support multi-granularity queries.
[0183] Rebuilding the professional knowledge base: integrating domain expert rule bases (e.g., "Year-on-year net profit growth rate must exclude the impact of non-recurring gains and losses") and industry terminology ("net profit attributable to the parent company" corresponds to the technical field net_profit_parent). Using the knowledge graph, we enable reasoning about the association between entities and indicators (e.g., Company XX → listed company → financial industry → must comply with the Financial Reporting Standards for Financial Enterprises → growth rate calculation requires consolidated financial report data). When the generator constructs SQL, the knowledge graph infers that "Company XX, as a listed company, requires consolidated net profit." This guides the SQL to select the net_profit_consolidated field from the annual_finance table rather than individual report fields, ensuring that the generated logic complies with industry standards.
[0184] Finally, embedded vector indexing technology is used to convert table fields (such as annual_finance.net_profit and company_info.industry) into semantic embedding vectors to build a vector index library. When the user's natural language includes the ambiguous expression "profit growth," the search engine quickly locates relevant fields such as net_profit and growth_rate through vector similarity matching, supporting semantically based multi-path parallel searches. When the generator needs to replace fields, vector search can also find the optimal mapping between business terms and technical fields (for example, matching the "net profit attributable to parent company" vector to net_profit_parent), ensuring the semantic accuracy of generated SQL.
[0185] This non-intrusive fusion mechanism does not modify the original database schema throughout the process. Through knowledge stratification, metadata mapping, dynamic adaptation, knowledge graph and vector retrieval, it provides the searcher with precise association between domain knowledge and table structure for multi-channel parallel retrieval, and injects professional rules and semantic understanding into the generator's SQL framework construction, fully supporting the professionalism and robustness of the Text2SQL process from the knowledge level to the technical level.
[0186] In order to more clearly describe the solution provided by the embodiment of the present invention, a relatively complete implementation method is provided below.
[0187] Please refer to Figure 2 and Figure 3 , Figure 2 A schematic diagram of the workflow of the Text2SQL system provided in an embodiment of the present invention, Figure 3This is a schematic diagram of the Text2SQL implementation system process framework provided by an embodiment of the present invention. The proposed Text2SQL system utilizes a multi-stage pipeline architecture, designed to accurately convert and execute user natural language queries into SQL statements, while also providing explanations of the query logic. The entire system process, from query intent understanding to result display, forms a complete closed loop, with self-correction mechanisms implemented at key stages. The overall system architecture consists of seven core components: Planner, Retriever, Generator, Verifier, Executor, Formatter, and Explainer. These components work together in a specific order to form a processing pipeline. Furthermore, the system incorporates diagnostic and self-reflection mechanisms, as well as human-computer interaction feedback, forming an intelligent, reliable, and continuously optimizing Text2SQL conversion system. The system's processing flow is as follows: After a user submits a natural language query, the Planner first analyzes the query intent and categorizes the query. A multi-dimensional search is then performed, including industry / concept targeting, first-level domain targeting, second-level domain targeting, and table targeting. The Generator then generates the initial SQL statement based on the search results. The Verifier verifies the SQL statement. The Executor executes the SQL statement and retrieves the results. If the execution fails, the system initiates diagnostic and self-reflective error correction mechanisms. Finally, the Formatter formats the query results, and the Explainer generates an explanation of the query logic to help users understand the entire process. This precisely defined processing path ensures that each query receives the most appropriate processing.
[0188] The embodiment of the present invention realizes the efficient integration of expert knowledge and database schema. Figure 4 , Figure 4 This is a schematic diagram of the expert knowledge and database schema fusion mechanism provided by an embodiment of the present invention. Through non-invasive horizontal expansion technology, the system can adapt to heterogeneous database environments containing tens of thousands of tables.
[0189] Domain knowledge representation method:
[0190] Domain knowledge is represented using a multi-level classification system, including industry classification, concept classification, and domain classification, forming a complete knowledge representation framework: Industry classification layer: The system can identify the industries involved in the query (such as A-shares, Hong Kong stocks, Science and Technology Innovation Board, etc.), providing a basis for subsequent targeted retrieval; Concept classification layer: Identify the concepts (themes) involved in the query and understand user needs from a business perspective.
[0191] Domain classification layer: includes first-level and second-level domain classifications, which determine the business field to which the query belongs (such as "individual stock related", "fund related", "index related", etc.); table classification layer: maps the query to a specific database table.
[0192] This multi-level classification system forms a complete mapping from business concepts to data structures, enabling the system to accurately understand user queries in specialized fields and effectively locate relevant data sources. For example, when a user queries "CATL's net profit growth rate," the system identifies "CATL" as a company entity and "net profit growth rate" as a financial indicator, belonging to the "Individual Stock Related" first-level domain and the "Main Board Financial" second-level domain, thereby locating the relevant financial statements.
[0193] Non-intrusive horizontal expansion technology:
[0194] In order to achieve knowledge fusion without modifying the existing database structure, the present invention adopts a non-invasive horizontal expansion technology based on the metadata layer. The specific implementation includes:
[0195] Metadata enhancement layer: The system uses dedicated metadata tables to store domain knowledge and data structure mappings without interfering with the original database; dynamic mapping mechanism: Dynamic association between business concepts and underlying data structures is achieved through dynamic table obtainer and fixed table obtainer components; embedded vector index: Domain knowledge is vectorized to support efficient semantic retrieval; professional knowledge base: Expert knowledge base is built for specific fields to provide guidance for query understanding and SQL generation.
[0196] This design allows the system to flexibly adapt to various database environments without requiring intrusive modifications to existing systems. Especially for databases in specialized fields like finance, the system understands specific business rules and query patterns, such as distinguishing between "consolidated statements" and "quarterly statements" and recognizing specialized concepts like "export sales" and "domestic sales," significantly improving query accuracy.
[0197] Knowledge base construction and maintenance methods:
[0198] The initial construction of the knowledge base adopts a semi-automated approach, combining automatic extraction and manual verification: automatic collection of database metadata: automatically captures information such as table structure and field type; expert knowledge encoding: establishes a structured expert knowledge table to form a rule base; vectorized indexing: vectorizes and stores table field information to support semantic similarity retrieval; special scenario processing: designs special processing mechanisms for specific query scenarios (such as shareholder queries) to implement personalized query strategies.
[0199] Knowledge base maintenance utilizes an incremental update mechanism, recording query results in an intermediate table to provide a data foundation for future knowledge base optimization. The system also supports dynamic knowledge expansion through entity extraction and entity vectorization, enabling the knowledge base to continuously evolve and improve.
[0200] Detailed explanation of the core processing pipeline:
[0201] Planner: The Planner is the starting point of the entire processing process, responsible for analyzing the user's query intent and planning the subsequent processing path. Its work includes: Query intent identification: determining the query's type of question (such as fact query, statistical analysis, trend forecasting, etc.); Domain classification: determining the query's professional field (such as stocks, funds, industry analysis, etc.); Entity identification: extracting key entities in the query (such as company name, time range, indicator name, etc.); Processing path decision: determining the subsequent component calling strategy based on the query complexity and type.
[0202] Planner uses a multi-level classification model combined with a rules engine to understand intent. For the financial sector, the system has preset over ten query intent categories. Through semantic analysis, it accurately identifies the type of user query and provides guidance for subsequent processing.
[0203] Retriever: Based on the Planner's analysis results, the Retriever performs multi-way parallel searches to obtain domain knowledge and database structure information relevant to the query. This includes: Domain Knowledge Retrieval: Retrieving relevant domain concepts and rules based on query intent and keywords; Entity Information Retrieval: Expanding information on entities identified in the query (such as companies and industries); Table Structure Retrieval: Locating database tables and fields that may be involved in the query; and Query Pattern Retrieval: Matching similar historical query patterns to provide reference for SQL generation.
[0204] Retriever uses a hybrid search strategy, combining keyword matching and semantic vector search to ensure high recall and precision. For example, for a query like "CATL's net profit growth rate over the past three years," the system can simultaneously retrieve entity information about CATL, the calculation method for "net profit growth rate," and the financial statement structure that may contain relevant data.
[0205] Generator:
[0206] The Generator is responsible for generating standardized SQL statements based on the retrieved knowledge and structural information. Its core functions include: SQL framework construction: determining basic structures such as SELECT, FROM, and WHERE based on query intent; table association logic generation: automatically generating JOIN conditions based on relationships between tables; conditional clause construction: converting filter conditions in natural language into WHERE clauses; and aggregate function selection: selecting appropriate aggregate functions and grouping logic based on query requirements.
[0207] The Generator utilizes a Large Language Model (LLM) at its core, combined with Retrieval-Augmented Generation (RAG) technology. Through carefully designed prompt templates, the model generates high-quality SQL statements. The system also includes built-in SQL generation rules for specific domains, such as financial indicator calculations and time series processing, ensuring that the generated SQL meets domain-specific standards.
[0208] Verifier:
[0209] Verifier verifies the SQL generated by Generator in many aspects to ensure its legality and executableness. The main verification contents include: Syntax check: to ensure that the SQL complies with the standard syntax specification; Table field verification: to check whether the tables and fields referenced in the SQL exist; Permission verification: to confirm that the query complies with the access permission requirements; Logic verification: to evaluate whether the SQL logic is consistent with the original query. Figure 1 Complexity control: Evaluate query complexity and avoid operations that consume too many resources.
[0210] Verifier combines rule checking with model evaluation to not only verify the formal correctness of SQL statements but also assess their compatibility with user intent. When problems are discovered, Verifier generates detailed error diagnostic information to guide subsequent corrections.
[0211] Executor:
[0212] The Executor is responsible for safely and efficiently executing validated SQL statements and handling any exceptions that may arise during execution. Its functions include: query optimization: performing necessary optimizations on SQL statements to improve execution efficiency; secure execution: executing SQL statements in a controlled environment to prevent malicious operations; result acquisition: collecting query results and performing preliminary processing; and exception handling: capturing and analyzing execution errors and providing diagnostic information.
[0213] Executor supports multiple database types (such as MySQL, Oracle, PostgreSQL, etc.) and can adjust execution strategies based on the characteristics of the target database. The system also implements a query result caching mechanism to improve the response speed of repeated queries.
[0214] Formatter:
[0215] The Formatter processes the raw results returned by the Executor and converts them into a user-friendly format. Key functions include: data organization: sorting, deduplication, and filtering query results; field mapping: converting database field names into business terms; data conversion: performing unit conversions and percentage calculations as needed; tabular processing: formatting results into structured tables; and data constraints: controlling the amount of returned data to ensure responsiveness. The Formatter automatically selects the appropriate display method based on query intent and data characteristics. For example, for time series data, the system performs appropriate time sorting and formatting to make the results easier to understand.
[0216] Explainer:
[0217] The Explainer is a key innovation of this system, responsible for generating natural language explanations of query logic and enhancing the system's explainability. Its functions include: query logic explanation: explaining SQL query logic in plain language; data source explanation: explaining the source and reliability of result data; calculation logic explanation: explaining the calculation methods of complex indicators; and result analysis tips: providing basic interpretation of query results. The Explainer combines template generation with LLM to generate professional and easy-to-understand explanations based on SQL structure and domain knowledge. These explanations not only help users understand the query process but also enhance their trust in the results.
[0218] Self-reflection and error correction mechanism:
[0219] A major feature of the present invention is the introduction of a self-reflection and error correction mechanism, which can perform self-diagnosis and correction when a query fails.
[0220] Error detection and diagnosis algorithms:
[0221] The system categorizes and diagnoses errors during SQL execution: Syntax errors: Specific syntax issues are identified by parsing error messages returned by the database (such as the "invalid identifier" error code); Semantic errors: Potential misunderstandings are detected by comparing user intent with SQL semantics; Missing data errors: Empty results caused by non-existent data (such as "No rows fetched") are identified; Execution timeouts: Analyze the causes of excessive query complexity.
[0222] The diagnostic process uses different strategies depending on the error type. For example, if the execution is successful but the results may be problematic, the system will check for duplicate rows, large numbers of null values, mismatched results, or problems that violate common sense. For failed executions, the system will analyze possible causes, such as missing tables, missing data, syntax errors, or non-existent fields, and provide detailed diagnostic information to provide precise guidance for subsequent corrections.
[0223] Automatic error correction strategy implementation:
[0224] Based on the diagnostic results, the system implements a multi-level error correction strategy: Table-level error correction: When SQL execution fails, the system extracts all fields from the original SQL, identifies the tables used, and then refocuses on the relevant tables for SQL reconstruction. Field-level error correction: For errors such as "invalid identifier," the system extracts accurate information about candidate fields to ensure that the used fields exist in the corresponding tables. Query condition error correction: For queries that return empty results, the system analyzes whether the query conditions are overly restrictive (such as fuzzy query conditions or overly specific GROUP BY conditions) and automatically relaxes them appropriately. Table association error correction: The system checks the correctness of associations between tables, particularly ensuring that fields in different tables are correctly associated (such as the correspondence between company codes and stock symbols). The error correction process generates a dedicated reflection prompt, combining error information, field information, and expert knowledge to guide the generation of new SQL. The system also differentiates status codes, applying different error correction strategies for 500 errors (execution failure) and 200 errors (execution success but may require optimization). This targeted error correction approach significantly improves query success rates.
[0225] Learning optimization process:
[0226] The system continuously optimizes its performance by recording and analyzing success and failure cases during the query process: Error pattern library: collects diagnostic results and corrected SQL statements to build common error patterns and solutions; result cache mechanism: stores query results in an intermediate table to support subsequent analysis and optimization; manual feedback integration: an interruption detection mechanism is designed to allow users to interrupt excessively long processing processes at any time, from which the system can learn the user's patience threshold; incremental improvement: the diagnostic and error correction results of each execution are used to optimize subsequent queries, allowing the system to continuously improve performance with use.
[0227] This continuous learning mechanism enables the system to gain experience from each query, continuously improve its error handling capabilities, and reduce the recurrence of similar errors.
[0228] Human-computer interaction and feedback loop:
[0229] The present invention fully considers the importance of human-machine collaboration and designs a complete interaction and feedback mechanism.
[0230] User feedback collection mechanism:
[0231] The system collects user feedback through multiple channels: Query result evaluation: users can rate and comment on query results; Error reporting: collects errors and inaccuracies reported by users; Query correction: records user modifications to system-generated SQL; Usage behavior analysis: automatically analyzes user query patterns and habits.
[0232] Feedback processing and system adjustment:
[0233] Based on the collected feedback, the system implements corresponding adjustments: query strategy optimization: adjust the weights and strategies of different components according to the success rate; knowledge base update: supplement and correct the knowledge base content based on user feedback; error pattern update: update error diagnosis and correction strategies; model parameter adjustment: optimize model parameters for specific types of queries.
[0234] Incremental learning implementation:
[0235] The system supports continuous incremental learning: periodic retraining: regularly updating the model using newly collected data; online learning: supporting online parameter adjustment of some components; A / B testing: comparative testing of new strategies to select the best solution; human-assisted training: combining expert knowledge for supervised model optimization; through this closed-loop human-machine feedback mechanism, the system can continuously improve and adapt to changing business needs and data environments.
[0236] Key algorithms and technical implementations:
[0237] Query intent recognition algorithm:
[0238] The system adopts a multi-level intent recognition method, combined with rules and machine learning models: field classification: first identify the business field to which the query belongs (such as stocks, funds, industry analysis); intent classification: further analyze the specific query type (such as obtaining numerical values, comparative analysis, trend prediction); entity recognition: extract key entities and parameters in the query; relationship extraction: identify the relationship between entities and query conditions; intent recognition adopts a fine-tuning method based on a pre-trained language model, and through training with a large number of query samples in the field, it achieves accurate understanding of professional field queries.
[0239] Table positioning and association algorithm:
[0240] In response to the complex environment of tens of thousands of tables, the system has developed an efficient table location and association algorithm: multi-dimensional similarity calculation: comprehensive consideration of semantic similarity, field matching and historical usage frequency; hierarchical retrieval: first locate the field, then locate the database, and finally locate the specific table; table relationship inference: infer the relationship between tables based on foreign key constraints and historical JOIN patterns; relevance sorting: sort the candidate tables by relevance, giving priority to the most relevant tables; this algorithm can quickly locate the most relevant data source in massive tables and correctly establish associations between tables, providing an accurate structural foundation for SQL generation.
[0241] SQL generation and optimization algorithm:
[0242] SQL generation uses a template-enhanced generative approach: query framework selection: select a basic SQL template based on the intent type; field mapping: map indicators in natural language to specific database fields; condition conversion: convert natural language conditions into SQL condition expressions; aggregation logic generation: generate GROUP BY and aggregation functions based on query requirements; the generated SQL will also undergo optimization processing: query rewriting: simplify complex subqueries and improve execution efficiency; index awareness: adjust the query condition order based on the index status of the table; access path optimization: select an efficient table access method; concurrency control: control query complexity to avoid excessive resource usage.
[0243] Error diagnosis and automatic correction algorithms:
[0244] The system implements a structured error diagnosis and correction process: Error classification: Classifies errors into categories such as syntax errors, semantic errors, and execution errors; Root cause analysis: Locates the root cause of the error through a decision tree model; Correction strategy selection: Selects an appropriate correction strategy based on the error type; Incremental correction: Uses the principle of minimum modification to adjust SQL; Verification loop: Verifies again after correction until success or the upper limit of attempts is reached.
[0245] This structured error correction method can specifically address various errors and significantly improve query success rates. In summary, this invention integrates expert knowledge with database structure to build a complete processing pipeline, achieving high-quality Text2SQL conversion in complex heterogeneous data environments. It also continuously optimizes system performance through self-reflection, error correction, and human-computer collaboration, providing strong support for the intelligent utilization of enterprise data assets.
[0246] Please refer to Figure 5 , Figure 5 A Text2SQL implementation system 110 provided in an embodiment of the present invention includes:
[0247] Acquisition module 1101, for receiving a natural language query input by a user;
[0248] The execution module 1102 is used to perform intent analysis and processing path planning on the natural language query through a planner to obtain query intent, domain classification results and key entity information; based on the domain classification results and key entity information output by the planner, perform multi-path parallel retrieval through a retriever to obtain domain knowledge, entity information, database table structure and historical query patterns related to the natural language query; based on the retrieval results output by the retriever, build an SQL framework through a generator to generate an initial SQL statement; perform syntax, table fields, permissions and logic verification on the initial SQL statement through a validator, and if the verification passes, execute the verified SQL statement; if the verification fails, return to the generator and adjust the SQL generation logic based on the verification feedback to regenerate the initial SQL statement; when executing the verified SQL statement, if the execution is successful, obtain the query result and perform data sorting, field mapping and tabular processing on the query result to obtain a formatted result; if the execution fails, start the diagnosis and error correction mechanism, generate a corrected SQL statement and return it to the validator for reverification;
[0249] The output module 1103 is used to generate a natural language explanation including query logic, data source and calculation method based on the formatted result and SQL execution logic, and output it to the user.
[0250] It should be noted that the implementation principles of the aforementioned Text2SQL implementation system 110 can be referenced from the implementation principles of the aforementioned Text2SQL implementation method and will not be elaborated upon here. It should be understood that the division of the various modules of the aforementioned device is merely a division of logical functions. In actual implementation, they may be fully or partially integrated into a single physical entity or physically separated. Furthermore, these modules may be implemented entirely in software invoked by a processing element, or entirely in hardware. Alternatively, some modules may be implemented in software invoked by a processing element, while others may be implemented in hardware. For example, the Text2SQL implementation system 110 may be a separate processing element, or integrated into a chip of the aforementioned device. Furthermore, it may be stored in the form of program code in the memory of the aforementioned device, invoked by a processing element of the aforementioned device to execute the functions of the aforementioned Text2SQL implementation system 110. The implementation of the other modules is similar. Furthermore, these modules may be fully or partially integrated together, or implemented independently. The processing element described herein may be an integrated circuit with signal processing capabilities. During implementation, the steps of the aforementioned method or the modules above may be performed by hardware integrated logic circuits within the processor element or by software instructions.
[0251] For example, the above modules may be one or more integrated circuits configured to implement the above methods, such as one or more application specific integrated circuits (ASICs), one or more digital signal processors (DSPs), or one or more field programmable gate arrays (FPGAs). For another example, when a module is implemented by scheduling program code on a processing element, the processing element may be a general-purpose processor, such as a central processing unit (CPU) or other processor that can call program code. For another example, these modules may be integrated together and implemented in the form of a system-on-a-chip (SOC).
[0252] The embodiment of the present invention provides a computer device 100, which includes a processor and a non-volatile memory storing computer instructions. When the computer instructions are executed by the processor, the computer device 100 executes the aforementioned Text2SQL implementation system 110. Figure 6 As shown, Figure 6This is a block diagram of the structure of a computer device 100 provided in an embodiment of the present invention. The computer device 100 includes a Text2SQL implementation system 110, a memory 111, a processor 112, and a communication unit 113.
[0253] To enable data transmission or interaction, the memory 111, processor 112, and communication unit 113 are electrically connected to each other, directly or indirectly. For example, these components can be electrically connected via one or more communication buses or signal lines. The Text2SQL implementation system 110 includes at least one software functional module that can be stored in the form of software or firmware in the memory 111 or embedded in the operating system (OS) of the computer device 100. The processor 112 is used to execute the Text2SQL implementation system 110 stored in the memory 111, such as the software functional modules and computer programs included in the Text2SQL implementation system 110.
[0254] An embodiment of the present invention provides a readable storage medium, which includes a computer program. When the computer program is executed, the computer device where the readable storage medium is located is controlled to execute the aforementioned Text2SQL implementation system 110.
[0255] For illustrative purposes, the foregoing description has been made with reference to specific embodiments. However, the above illustrative discussion is not intended to be exhaustive or to limit the present disclosure to the precise forms disclosed. Numerous modifications and variations are possible in light of the above teachings. These embodiments have been selected and described in order to best illustrate the principles of the present disclosure and its practical application, thereby enabling those skilled in the art to best utilize the present disclosure and to utilize various embodiments with various modifications as appropriate for the specific application contemplated.
Claims
1. A Text2SQL implementation method, characterized in that: include: receiving a natural language query input by a user; The planner performs intent analysis and processing path planning on the natural language query to obtain query intent, domain classification results and key entity information; Based on the domain classification results and key entity information output by the planner, a multi-path parallel search is performed by a retriever to obtain domain knowledge, entity information, database table structure and historical query patterns related to the natural language query; Based on the search results output by the retriever, a SQL framework is constructed by a generator to generate an initial SQL statement; The initial SQL statement is verified by the validator in terms of syntax, table fields, permissions, and logic. If the verification passes, the verified SQL statement is executed; if the verification fails, the generator is returned and the SQL generation logic is adjusted based on the verification feedback to regenerate the initial SQL statement; When executing a verified SQL statement, if the execution is successful, the query results are obtained and the query results are sorted, field mapped, and tabulated to obtain formatted results; If the execution fails, the diagnosis and error correction mechanism is activated to generate a corrected SQL statement and return it to the validator for re-verification; Based on the formatted results and SQL execution logic, a natural language explanation including query logic, data source, and calculation method is generated and output to the user; The generating of a natural language explanation including query logic, data source and calculation method based on the formatted result and SQL execution logic includes: Based on SQL execution logic, use natural language to describe query targets, table association methods, and screening conditions to implement query logic interpretation; Based on the table structure information, describe the database table and field corresponding to the result data and mark the data validity to achieve data source description; Based on the domain knowledge, explain the calculation formulas and business implications of the complex indicators involved to achieve calculation logic elaboration, the complex indicators including net profit growth rate; Based on the formatted results, trend analysis or abnormal value prompts are provided to implement result analysis prompts; wherein the natural language interpretation is adapted to the user's business knowledge level to avoid the use of technical terms.
2. The method according to claim 1, characterized in that The planner performs intent analysis and processing path planning on the natural language query to obtain query intent, domain classification results and key entity information, including: Performing intent recognition on the natural language query to determine a query type, where the query type includes factual query, statistical analysis, or trend prediction, and the query type constitutes the core content of the query intent; Performing domain classification on the natural language query based on a preset industry classification system and concept classification system to obtain a domain classification result, wherein the domain classification result includes a first-level domain and a second-level domain; Extracting key entities from the natural language query to obtain key entity information, wherein the key entity information includes company name, time range, and indicator name; According to the query type and the complexity of the natural language query, the calling strategy of the subsequent retriever and generator is decided; wherein, the domain classification results, key entity information and calling strategy are used together as the retrieval basis for the retriever to perform multi-way parallel retrieval.
3. The method according to claim 2, characterized in that The multi-path parallel search is performed by the retriever to obtain domain knowledge, entity information, database table structure and historical query patterns related to the natural language query, including: Retrieving corresponding business rules and indicator calculation methods from a professional knowledge base based on the field classification results to obtain the field knowledge; Acquire entity attributes and associated information from an entity library based on the key entity information to obtain the entity information; Based on the first-level domain, the second-level domain and the key entity information in the domain classification result, the relevant database tables and fields are located from the database metadata layer and the relationship between the tables is obtained to obtain the table structure information; Match historical query patterns that are semantically similar to the natural language query to obtain a reference SQL structure and obtain the query pattern information; wherein the domain knowledge, entity information, table structure information and query pattern information together serve as basic data for the generator to construct an SQL framework.
4. The method according to claim 1, wherein The retrieval results output by the retriever are used to construct an SQL framework through a generator to generate an initial SQL statement, including: Based on the table structure information and query intent, determine the database table to be queried and form a FROM clause of the SQL framework; Based on the inter-table association relationship in the table structure information, generate JOIN conditions between the database tables and integrate them into the FROM clause; According to the query intent and the determined database table, select the fields to be queried from the table structure information to form a SELECT clause of the SQL framework; Convert the screening conditions in the natural language query into SQL conditional expressions in combination with the entity information, and construct a WHERE clause of the SQL framework; According to the query type in the query intent, the corresponding aggregation function and grouping logic are selected, and the indicator calculation rules in the domain knowledge are integrated to supplement and improve the SELECT clause and WHERE clause to obtain the initial SQL statement.
5. The method according to claim 1, wherein The validator verifies the syntax, table fields, permissions and logic of the initial SQL statement, including: Checking whether the initial SQL statement complies with SQL syntax specifications to implement syntax verification; Based on the table structure information, check whether the table and field referenced in the SQL statement exist and are valid to implement table and field verification; Confirm that the current user has the access rights to query the tables and fields to implement permission verification; Evaluate the consistency of SQL statement logic with the query intent through semantic comparison to achieve logic verification; Evaluate the execution resource consumption of SQL statements to avoid overly complex operations to achieve complexity verification; When verification fails, the validator outputs a specific error type, which includes a syntax error or a non-existent field. The specific error type serves as a basis for the generator to adjust the SQL generation logic.
6. The method according to claim 1, characterized in that The process of the diagnosis and error correction mechanism includes: Classify execution failure types into syntax errors, semantic errors, data missing errors, or execution timeouts to achieve error classification; Locate specific issues based on the error type and the table structure information and entity information, including incorrect field names, incorrect table association conditions, or overly strict query conditions, to achieve root cause analysis; Based on the root cause analysis results, table-level, field-level, or condition-level corrections are used to select a correction strategy. Table-level corrections refocus related tables based on table structure information, field-level corrections replace entity information with valid fields, and condition-level corrections relax screening conditions. The erroneous part of the SQL statement is adjusted and the correct logic is retained to generate a corrected SQL statement to achieve incremental correction; wherein, the corrected SQL statement is verified by the validator and then executed again by the executor.
7. The method according to claim 1, characterized in that The query results are processed by data sorting, field mapping and tabular processing to obtain formatted results, including: Sorting, removing duplicates and filtering out null values of the query results to achieve data organization; Converting database field names into business terms based on the entity information to implement field mapping; Perform unit conversion or percentage calculation based on the indicator type and the unit rules in the domain knowledge to achieve data conversion; The processed data is formatted into a structured table to obtain the formatted result; wherein the formatted result is used as input data for generating a natural language interpretation.
8. The method according to claim 1, characterized in that The method further includes a non-intrusive fusion mechanism of expert knowledge and database schema, wherein the non-intrusive fusion mechanism includes: Constructing a multi-level knowledge classification system, which includes an industry classification layer, a concept classification layer, a first-level domain classification layer, a second-level domain classification layer, and a table classification layer, to achieve the mapping of business concepts to database tables and provide a classification basis for the planner's domain classification; Establishing a metadata table to store domain knowledge and data structure mapping relationships, providing metadata support for the table structure information retrieval of the retriever; Use dynamic table fetchers to associate business concepts with newly added database tables in real time, and use fixed table fetchers to maintain a stable mapping of core table structures; Build a professional knowledge base, integrate the domain expert rule base and industry terminology base, and realize the association reasoning between entities and indicators through the knowledge graph; Embedded vector indexing technology is used to vectorize table field information to support retrieval based on semantic similarity. The non-intrusive fusion mechanism provides knowledge support for the multi-way parallel retrieval of the retriever and the SQL framework construction of the generator.
9. A Text2SQL implementation system, characterized in that: include: An acquisition module for receiving natural language queries input by users; An execution module is used to perform intent analysis and processing path planning on the natural language query through a planner to obtain query intent, domain classification results and key entity information; Based on the domain classification results and key entity information output by the planner, a multi-path parallel search is performed by the retriever to obtain domain knowledge, entity information, database table structure and historical query patterns related to the natural language query; based on the search results output by the retriever, an SQL framework is constructed by the generator to generate an initial SQL statement; the initial SQL statement is verified by the validator in terms of syntax, table fields, permissions and logic. If the verification passes, the verified SQL statement is executed; if the verification fails, the generator is returned and the SQL generation logic is adjusted based on the verification feedback to regenerate the initial SQL statement; when the verified SQL statement is executed, if the execution is successful, the query result is obtained and the query result is processed by data sorting, field mapping and tabulation to obtain a formatted result; If the execution fails, the diagnosis and error correction mechanism is activated to generate a corrected SQL statement and return it to the validator for re-verification; An output module is used to generate a natural language explanation including query logic, data source and calculation method based on the formatted result and SQL execution logic, and output it to the user; The output module is specifically used for: Based on the SQL execution logic, the query target, table association method and screening conditions are described in natural language to implement query logic explanation; based on the table structure information, the database table and field corresponding to the result data are explained and the data validity is marked to implement data source explanation; based on the domain knowledge, the calculation formula and business meaning of the complex indicators involved are explained to implement calculation logic explanation, and the complex indicators include net profit growth rate; Based on the formatted results, trend analysis or abnormal value prompts are provided to implement result analysis prompts; wherein the natural language interpretation is adapted to the user's business knowledge level to avoid the use of technical terms.
Citation Information
Patent Citations
SQL (Structured Query Language) generation interpretation method and device based on business rules, equipment and medium
CN117271557A
Natural language SQL (Structured Query Language) conversion method based on data platform and large language model
CN119576977A