A data processing method, apparatus, and computer program product

By constructing a database knowledge graph and aligning the training of a large language model with reinforcement learning, the problem that existing Text-to-SQL solutions cannot accurately perceive the topological structure between database tables in complex database queries is solved, achieving efficient and accurate SQL generation and improving the accuracy and executability of natural language to SQL conversion.

CN123115203APending Publication Date: 2026-11-17NEUSOFT CORP
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611330292.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-08-28
Publication Date
2026-11-17

AI Technical Summary

Technical Problem

Existing Text-to-SQL solutions cannot accurately perceive the topological structure and relationships between database tables when processing complex database queries, resulting in the generated SQL statements failing to execute correctly. This is especially true in scenarios where physical foreign keys are missing or there are a large number of business tables, which can easily lead to illusions and syntax errors.

Method used

A database knowledge graph (DKG) containing the physical structure of the database and the semantics of business rules is constructed. By dynamically retrieving the optimal connectivity graph in the DKG, the model is provided with accurate table structure context. Reinforcement learning is used to align and train the large language model (LLM) to generate syntactically sound, logically correct, and efficient SQL statements.

Benefits of technology

It improves the accuracy and executability of natural language to SQL statements, enhances adaptability in complex scenarios, reduces the number of table connection paths that the model fabricates, and generates SQL statements with efficient and reliable execution capabilities.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123115203A_ABST
    Figure CN123115203A_ABST
Patent Text Reader

Abstract

This application discloses a data processing method, apparatus, and computer program product. Based on the metadata and business rules of the target database, a database knowledge graph (DKG) containing inter-table relationships is constructed. The graph structure fully preserves the relationships and business rule information between tables and fields, providing a reasonable and computable data foundation for subsequent retrieval and preventing the model from fabricating non-existent connectivity paths. The DKG searches for natural language questions from users to obtain the optimal connectivity graph. Then, combining the optimal connectivity graph and the natural language questions, prompt words are obtained, and structured query statements (SQL) are derived from the prompt words. Instead of using pre-generated static connectivity paths, the optimal connectivity graph is explicitly injected into the prompt words, providing the model with an accurate search strategy, reducing context length and computational overhead while improving the accuracy and executability of SQL generation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of data processing technology, and in particular to a data processing method, apparatus, and computer program product. Background Technology

[0002] Text-to-Structured Query Language (Text-to-SQL) technology automatically converts users' natural language questions into executable SQL statements, enabling natural language interaction with relational databases. This technology is currently widely used in intelligent question answering, database retrieval, and data service systems.

[0003] In existing technologies, Text-to-SQL solutions mostly rely on large language models and employ Retrieval-Augmented Generation (RAG) technology to vectorize database table metadata and calculate similarity with user queries to retrieve relevant data. However, RAG can only achieve literal or shallow semantic matching and cannot preserve complex relationships between tables, resulting in low accuracy in real-world business scenarios. Summary of the Invention

[0004] To address the aforementioned issues, this application provides a data processing method, apparatus, and computer program product to improve the accuracy of Text-to-SQL solutions.

[0005] This application discloses a data processing method, the method comprising: Based on the metadata and business rules of the target database, a database knowledge graph (DKG) containing inter-table relationships is constructed. The optimal connectivity graph is obtained by searching for natural language questions from users in the DKG. By combining the optimal connectivity graph with the natural language problem, prompt words are obtained; The structured query statement (SQL) is obtained through the prompt words.

[0006] Optionally, the step of constructing a database knowledge graph (DKG) containing inter-table relationships based on the target database's metadata and business rules includes: Connect to the target database and extract the metadata and business rules corresponding to each table in the target database; Based on the metadata and the business rules, entity nodes corresponding to each table and directed edges between the entity nodes are constructed in the graph database to obtain the DKG; the directed edges are established based on the physical foreign key associations explicitly declared in the target database and / or soft associations formed according to the business configuration.

[0007] Optionally, the step of searching for the natural language question from the user in the DKG to obtain the optimal connected graph includes: Based on the natural language problem, locate the set of anchor entity nodes in the DKG; A connected path is obtained starting from each anchor entity node in the set of anchor entity nodes to form a candidate connected graph. The optimal connected graph is determined from each of the candidate connected graphs.

[0008] Optionally, determining the optimal connected graph in each of the candidate connected graphs includes: Based on the number of hops and edge weights of each connected path in the candidate connected graph, and the similarity between the candidate connected graph and the natural language problem, a comprehensive score is obtained for each candidate connected graph. The candidate connected graph whose comprehensive score meets the preset requirements is determined as the optimal connected graph; the preset requirements are that the score is greater than the preset score or the ranking is higher than the preset rank.

[0009] Optionally, locating the set of anchor entity nodes in the DKG based on the natural language question includes: The proper nouns in the natural language problem are matched with the entity nodes in the DKG to obtain a set of matching nodes; Extract the text vectors from the natural language problem, and calculate the similarity between the text vectors and the description vectors of the entity nodes in the DKG to obtain a semantic node set; The set of anchor entity nodes is obtained by combining the set of matching nodes and the set of semantic nodes.

[0010] Optionally, combining the optimal connected graph and the natural language problem to obtain prompt words includes: The optimal connectivity graph is converted into text data; the text data includes at least one of business concept mapping relationships and field value constraints, as well as the structural information and connectivity paths of the optimal connectivity graph; The text data is combined with the natural language question to obtain the prompt word.

[0011] Optionally, obtaining the structured query statement SQL through the prompt words includes: The prompt words are input into the pre-trained model to obtain the SQL output by the pre-trained model; the reward function of the pre-trained model during the training process includes execution result consistency reward, syntax standardization reward, illusion entity penalty, connectivity path rationality reward, execution efficiency reward and output format standardization reward.

[0012] Optionally, after obtaining the structured query statement (SQL) through the prompt words, the method further includes: Execute tests on multiple SQL statements, remove SQL statements that report errors, and obtain the result set returned by SQL statements that do not report errors; The confidence level of each result set is determined based on the frequency of occurrence of each result set. Based on the confidence level and the execution cost of each of the SQL statements that did not report errors, the target SQL statement is determined from among the SQL statements that did not report errors. The target SQL is used as the response to the natural language question.

[0013] Based on the above-mentioned data processing method, this application also discloses a data processing apparatus, including: a construction unit, a search unit, a combination unit, and an acquisition unit; The construction unit is used to construct a database knowledge graph (DKG) containing inter-table relationships based on the metadata and business rules of the target database. The search unit is used to search for natural language questions from users in the DKG to obtain the optimal connected graph; The combining unit is used to combine the optimal connectivity graph and the natural language question to obtain prompt words; The acquisition unit is used to acquire the structured query statement (SQL) through the prompt words.

[0014] Optionally, the building unit includes: An extraction subunit is used to connect to the target database and extract the metadata and business rules corresponding to each table in the target database. A subunit is established to construct entity nodes corresponding to each table in the graph database, as well as directed edges between the entity nodes, based on the metadata and the business rules, to obtain the DKG; the directed edges are established based on the physical foreign key associations explicitly declared in the target database and / or soft associations formed according to the business configuration.

[0015] Optionally, the search unit includes: A positioning subunit is used to locate the set of anchor entity nodes in the DKG based on the natural language question. A sub-unit is formed to obtain a connected path starting from each anchor entity node in the set of anchor entity nodes, and to form a candidate connected graph. A sub-unit is determined for identifying the optimal connected graph among the candidate connected graphs.

[0016] Optionally, the determining subunit includes: The comprehensive score acquisition subunit is used to acquire a comprehensive score for each candidate connected graph based on the number of hops and edge weights of each connected path in the candidate connected graph, as well as the similarity between the candidate connected graph and the natural language problem. The optimal determination subunit is used to determine the candidate connected graph whose comprehensive score meets the preset requirements as the optimal connected graph; the preset requirements are that the score is greater than the preset score or the ranking is higher than the preset rank.

[0017] Optionally, the positioning subunit includes: The matching subunit is used to match the proper nouns in the natural language problem with the entity nodes in the DKG to obtain a set of matching nodes; The computational subunit is used to extract text vectors from the natural language problem and calculate the similarity between the text vectors and the description vectors of the entity nodes in the DKG to obtain a semantic node set. The combined subunit is used to combine the matching node set and the semantic node set to obtain the anchor point entity node set.

[0018] Optionally, the connecting unit includes: A transformation subunit is used to convert the optimal connected graph into text data; the text data includes at least one of business concept mapping relationships and field value constraints, as well as the structural information and connection paths of the optimal connected graph; The combination subunit is used to combine the text data with the natural language question to obtain the prompt word.

[0019] Optionally, the acquisition unit includes: The output subunit is used to input the prompt word into the pre-trained model and obtain the SQL output by the pre-trained model; the reward function of the pre-trained model during the training process includes execution result consistency reward, syntax standardization reward, illusion entity penalty, connectivity path rationality reward, execution efficiency reward and output format standardization reward.

[0020] Optionally, the device further includes: The testing unit is used to perform execution tests on multiple SQL statements, remove SQL statements that report errors, and obtain the result set returned by SQL statements that do not report errors. A confidence level acquisition unit is used to determine the confidence level of each result set based on the number of times each result set appears. The SQL determination unit is used to determine the target SQL from among the non-error-reporting SQL based on the confidence level and the execution cost of each of the non-error-reporting SQLs; The response unit is used to provide the target SQL as a response to the natural language question.

[0021] Based on the above-described data processing method, this application also discloses a computer program product, which includes a computer program that, when run by a processor, is used to implement the above-described method.

[0022] Based on the above-described data processing method, this application also discloses a storage medium for storing computer program instructions, which, when executed by a central processing unit, can implement the steps of the above-described method.

[0023] This application discloses a data processing method, apparatus, and computer program product. Based on the metadata and business rules of the target database, a database knowledge graph (DKG) containing inter-table relationships is constructed. The graph structure fully preserves the relationships and business rule information between tables and fields, providing a reasonable and computable data foundation for subsequent retrieval and preventing the model from fabricating non-existent connectivity paths. The DKG searches for natural language questions from users to obtain the optimal connectivity graph. Then, combining the optimal connectivity graph and the natural language questions, prompt words are obtained, and structured query statements (SQL) are retrieved using these prompt words. Instead of using pre-generated static inter-table connectivity paths to form a fixed and inaccurate connectivity graph, the optimal connectivity graph calculated by the DKG is explicitly injected into the prompt words, providing the model with an accurate search strategy. This reduces context length and computational overhead while improving the accuracy and executability of SQL generation. Attached Figure Description

[0024] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of this application. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.

[0025] Figure 1 This is a flowchart illustrating a data processing method disclosed in an embodiment of this application; Figure 2 This is a flowchart illustrating another data processing method disclosed in an embodiment of this application; Figure 3 This is a schematic diagram of the structure of a data processing apparatus disclosed in an embodiment of this application. Detailed Implementation

[0026] Existing Text-to-SQL technologies largely rely on Large Language Models (LLMs) and vector retrieval to convert natural language into SQL statements. However, they have significant limitations when handling complex database queries. Firstly, vector retrieval can only achieve shallow semantic matching of table and field names, failing to perceive the complete topological structure and relationships between tables. In scenarios with missing physical foreign keys or a large number of business tables, the model is prone to misinterpreting data, fabricating incorrect inter-table connectivity paths, resulting in invalid SQL statements that cannot be executed correctly. Secondly, existing solutions often employ supervised fine-tuning to train models, only allowing them to mimic SQL writing patterns at the text level. They cannot truly learn the logic between database tables and the underlying execution constraints. When faced with complex nested queries outside the training set, they are prone to generating invalid SQL statements with syntax errors and empty results, leading to a low execution success rate.

[0027] To address the aforementioned technical challenges and improve the accuracy, executability, and adaptability to complex scenarios of Natural Language to SQL (NLP) statements, this application proposes a Text-to-SQL solution based on a Database Knowledge Graph (DKG). By constructing a DKG that includes the physical structure of the database and the semantics of business rules, it enables the explicit expression of relationships between tables. Furthermore, it dynamically retrieves the optimal connectivity graph from the DKG based on the user's question, providing the model with accurate table structure context. Finally, it employs reinforcement learning to align and train the LLM (Local Level Model), enabling the model to accurately grasp the logic between tables and fields, as well as database execution constraints, generating syntactically sound, logically correct, and efficiently executed SQL statements.

[0028] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0029] Example 1: This application discloses a data processing method.

[0030] For details, please refer to Figure 1 The data processing method disclosed in this embodiment includes the following steps: Step 101: Based on the metadata and business rules of the target database, construct a database knowledge graph (DKG) containing relationships between tables.

[0031] In the method of this embodiment, a target database can be connected to extract metadata and business rules corresponding to each table. The target database can be a relational database (such as PostgreSQL), and the metadata can specifically include physical metadata such as the database structure, field definitions, primary keys, foreign keys, and indexes of each table, as well as business rules such as business terminology, numerical mapping relationships, and business association rules, providing basic data support for graph construction.

[0032] After obtaining metadata and business rules, entity nodes corresponding to each table can be built in a graph database such as Neo4j based on this data, thereby comprehensively depicting the physical structure and business characteristics of each table in the database. As an implementable method, the types of entity nodes constructed can specifically include database entities, table entities, column entities, primary key entities, foreign key entities, index entities, business concept entities, value pattern entities, and entity profile entities.

[0033] Database entities can be used to represent database instances or logical libraries, serving as the highest-level namespace. Table entities can be used to represent physical tables or views, while field entities can be used to represent specific fields in a table, including field names, data types, and field comments. Primary key entities can be used to represent primary key constraints in a table, while foreign key entities can be used to represent physical foreign key constraints between tables. Index entities can be used to represent indexes created to improve query efficiency, while business concept entities can be used to represent business terms that exist independently of the physical table structure, such as "orders," "high-value users," "merchants," and "core business districts." Value schema entities can be used to represent deduplicated, enumerated, or typical values ​​of low-cardinality numeric fields (such as status bits and type codes) to characterize the mapping relationship between natural language conditions and database stored values ​​(e.g., "success" maps to "status=1"). Entity profile entities can be used to represent indexes of high-cardinality business entities, such as specific "merchant profiles" and "product profiles," to support name matching for high-cardinality business entities.

[0034] In the method of this embodiment, after obtaining the entity nodes, directed edges can be further established between the entity nodes in the graph database based on the explicitly declared physical foreign key associations in the target database and the soft associations formed according to the business configuration, to obtain a complete DKG. As an implementable solution, the established directed edges can correspond to various topological relationships. As an example, the method of this embodiment proposes several core topological relationships, which may include database containing tables (HAS_TABLE), table containing fields (HAS_COLUMN), table containing primary keys (HAS_PK), table containing foreign keys (HAS_FK), table containing indexes (HAS_INDEX), field belonging to table (BELONGS_TO_TABLE), foreign key referencing table (REFERENCES_TABLE), field joinable field (JOINABLE_WITH), table joinable table (TABLE_JOINABLE), business concept mapping to table (CONCEPT_MAPS_TO_TABLE), and business concept mapping to field (CONCEPT_MAPS_TO_COLUMN).

[0035] Therefore, the directed edges that can be constructed according to the above example can specifically include Database->Table (corresponding to a database containing a table), Table->Column (corresponding to a table containing a field), Table->PrimaryKey (corresponding to a table containing a primary key), Table->ForeignKey (corresponding to a table containing a foreign key), Table->Index (corresponding to a table containing an index), Column->Table (corresponding to a field belonging to a table), ForeignKey->Table (corresponding to a foreign key referencing a table), Column->Column (corresponding to a field that can be joined with another field), Table->Table (corresponding to a table that can be joined with another table), BusinessConcept->Table (corresponding to a business concept mapping to a table), and BusinessConcept->Column (corresponding to a business concept mapping to a field).

[0036] As an feasible solution, this step can be performed offline. Offline operation means that the graph construction is completed independently during the non-real-time query stage without occupying the computing resources of the online business database, and will not affect the performance of normal database query and transaction processing. At the same time, it can support batch, scheduled and full data extraction and structure update, ensuring the stability and consistency of DKG.

[0037] In the method of this embodiment, by constructing the aforementioned entities and topological relationships, the DKG can fully express the physical structure, business semantics, and inferable association paths between tables in each database, providing a structural foundation for subsequent dynamic retrieval. Even if no physical foreign keys are set in the target database, connectable relationships between fields or tables can be established using business rules. The DKG constructed in this way can simultaneously express the physical structure, business semantics, and table join relationships that can be used for SQL generation in the target database.

[0038] To better understand the content of this step, let's assume that the target database 'a' contains two core business tables: the merchant base table t_shop_base and the delivery area network table t_delivery_grid. This hypothetical scenario will be used as an example to provide a unified introduction to the content of this step.

[0039] On one hand, node instantiation is performed on the target database 'a', constructing corresponding entity nodes based on the physical structure and business semantics of the target database 'a'. The resulting table entities are t_shop_base and t_delivery_grid, and the field entities include shop_id, shop_category, and grid_id for t_shop_base, and grid_id and grid_type for t_delivery_grid. The business concept entities are BusinessConcept ("merchant") and BusinessConcept ("delivery grid"), and the value pattern entities are ValuePattern ("fast food") and ValuePattern ("core area").

[0040] On the other hand, directed edges representing topological relationships are established for each entity node in the target database a. Specifically, a directed edge BusinessConcept->CONCEPT_MAPS_TO_TABLE->t_shop_base representing the relationship between "business concept and table" can be established, that is, the business concept entity "merchant" is mapped to the "merchant base table" through the relationship between "business concept and table".

[0041] Subsequently, a mapping relationship can be established from value schema to field: ValuePattern->shop_category, mapping the value schema entity "fast food" to the field entity "shop_category". Simultaneously, inter-table relationships are established, parsing the physical schema or business configuration, and creating a JOINABLE_WITH edge between the field "grid_id" corresponding to the table entity "t_shop_base" and the field "grid_id" corresponding to the table entity "t_delivery_grid". This edge represents the relationship between fields that can be connected, including both physical foreign key associations and soft associations formed by business configuration. Finally, the DKG for the target database 'a' is obtained.

[0042] Step 102: Search for natural language questions from users in the DKG to obtain the optimal connected graph.

[0043] As an feasible approach, this step can be performed by a knowledge graph retrieval system, which aims to receive natural language questions input by users, dynamically find the physical tables, fields, and join paths between tables that best match the query intent in the DKG, and assemble the join paths into an optimal connectivity graph as the schema context for subsequent model inference.

[0044] In the method of this embodiment, anchor entity nodes are first located in the Data KG based on natural language problems. As an feasible approach, the establishment of anchor entity nodes can be accomplished by a dual-path entity linking method, namely an exact matching path and a semantic mapping path. The search results of these two paths are combined to form the final set of anchor entity nodes.

[0045] The exact matching path involves string matching of proper nouns in the natural language question with entity nodes in the Data Store Graph (DKG) to obtain a set of matching nodes. Specifically, entity extraction or string matching algorithms can be used to directly match proper nouns in the question with entity profiles and value patterns in the graph. For example, from the natural language question "Help me find fast food merchants," the ValuePattern ("fast food") node can be matched.

[0046] The semantic mapping approach extracts text vectors from natural language questions and calculates their similarity with the description vectors of entity nodes in the Data KG (DKG) to obtain a set of semantic nodes. Specifically, the question is input into a lightweight text vector model (Embedding Model) to obtain a vector representation, which is then compared with the description vectors of business concept entities (BusinessConcept), table entities (Table), and column entities (Column) in the graph. Nodes with similarity scores higher than a preset value are selected. For example, the semantic term "merchant" is matched to the BusinessConcept ("merchant") node.

[0047] In the method of this embodiment, the combination of the matching node set and the semantic node set can be performed as a union operation, ultimately yielding the anchor entity node set. Based on the example above, combining the two paths, the anchor entity nodes for "Help me find fast food merchants" are the ValuePattern ("Fast Food") node and the BusinessConcept ("Merchant") node.

[0048] After determining the set of anchor entity nodes, connection paths can be obtained starting from these anchor entity nodes to form a candidate connected graph. In the method of this embodiment, the graph does not pre-define static table connection paths, but relies on the graph computing engine for dynamic route derivation. Specifically, starting from the anchor entity nodes in the graph database, and limiting the maximum number of hops (e.g., N=3), a breadth-first traversal or depth-first traversal is performed along core directed edges such as CONCEPT_MAPS_TO_TABLE, BELONGS_TO_TABLE, JOINABLE_WITH, and HAS_FK to find structures that can connect all or most nodes in the graph database, thereby forming a set of candidate connected graphs. Each candidate connected graph essentially corresponds to a multi-table JOIN scheme that can be used by SQL.

[0049] In the method of this embodiment, after obtaining the candidate connected graphs, the optimal connected graph can be determined from among the connected graphs based on the similarity between the candidate connected graphs and the natural language problem, as well as the number of hops and edge weights of the connected paths. As an feasible solution, a multi-dimensional comprehensive scoring method can be used to rank the candidate connected graphs, with the scoring formula comprehensively considering three indicators: semantic fit, path simplification, and edge type weight.

[0050] Semantic fit is used to characterize the overall similarity between the descriptive text (table names, field names, etc.) contained in the candidate connected graph and the user's natural language question. Path conciseness is determined by the number of hops in the connected path; fewer hops mean fewer tables involved in the connection, a lower risk of introducing illusions, and a higher score. Edge type weighting is used to distinguish the importance of different relation edges; physical foreign key edges (HAS_FK) have a higher weight than business soft association edges (JOINABLE_WITH). If the path passes through high-confidence business concept mapping edges, additional rewards are given. The three indicators are weighted according to preset weights α, β, and γ to obtain a comprehensive score. The connected graphs ranked first or among the top few are selected as the optimal connected graphs.

[0051] As a feasible solution, the scoring formula can be expressed as follows: (1) In the formula, Score(G) i ) is a candidate connected graph G i The score, Sim(Q, G) i ) is a candidate connected graph G i The text includes all table names and field names, describing the overall similarity between the text and the natural language question Q. Length (G) i ) is a candidate connected graph G i The number of hops in the connection edges between tables, E i For candidate connected graph G i The set of edges in, Add points to the edge type weight.

[0052] To better understand this step, we'll use a DataKG (DKG) containing the merchant base table `t_shop_base` and the delivery area network table `t_delivery_grid` as an example. When a user inputs a natural language question like "Query the core delivery grid associated with fast food merchants," the system first locates anchor entity nodes through two-way entity links. The exact matching path yields `ValuePattern` ("Fast Food"), while the semantic mapping path yields `BusinessConcept` ("Merchant") and `BusinessConcept` ("Delivery Grid"), which are then combined to form the anchor entity node set. Starting from this anchor entity node set, the system dynamically traverses the DKG along predefined directed edges, resulting in a candidate connected graph containing `t_shop_base`, `t_delivery_grid`, related fields, and `JOINABLE_WITH` associated edges. Then, based on semantic fit, path simplicity, and edge weight, a score is calculated and ranked to select the most concise and reliable connected graph. This connected graph includes the merchant table, the delivery grid table, the related fields, and the correct field connections. This is the final optimal connected graph, which can be directly used for subsequent prompt word construction and SQL generation. This dynamic method of obtaining the connected graph ensures the flexibility of multi-table joins while avoiding the huge overhead of static path maintenance.

[0053] Step 103: Combine the optimal connected graph with the natural language problem to obtain prompt words.

[0054] In the method of this embodiment, this step aims to convert the structural information corresponding to the optimal connected graph into a text form that LLM can understand, and to structurally combine it with the user question and constraint rules to form a standardized, formatted prompt that can be directly used for SQL reasoning.

[0055] As an feasible solution, the optimal connectivity graph can be converted into text data. This process, also known as serialization, can be used to restore the optimal connectivity graph structure into a compact, standardized, and easily understood text format. The generated text not only contains basic structural information such as table names, field names, field types, and comments, but also explicitly declares the inter-table connectivity paths, business concept mapping relationships, and field value constraints dynamically calculated by DKG, thus providing a clear and unambiguous structural basis for subsequent SQL generation.

[0056] After serialization, the resulting text data is combined with a natural language question to obtain prompt words. As a feasible approach, structured assembled prompt words typically include a system instruction segment, a dynamic schema segment, an output format segment, and a user question segment. The system instruction segment defines the LLM's role and task objectives. The dynamic schema segment carries the optimal connectivity graph information after serialization, including table structure information, field definitions, inter-table connectivity paths, business concept mapping relationships, and field value pattern mapping relationships. The output format segment constrains the model's output format, clarifying the presentation specifications of the inference process and SQL statements. The user question segment carries the original natural language query content. Through this structured combination, complete and standardized prompt words can be formed.

[0057] In this embodiment, the optimal connected graph is dynamically obtained by scoring candidate connected graphs. This, combined with serialization to generate prompts, replaces the fixed connected graphs generated by traditional vector blind searching or static path enumeration. This improves retrieval coverage while reducing the introduction of invalid tables and redundant information. It not only provides the LLM with usable table and field information but also directly clarifies how JOIN operations should be performed between tables and which filtering conditions should be applied to fields through graph theory algorithms. This provides reliable contextual support for generating complex nested SQL queries with zero illusions and direct execution capabilities for a single model.

[0058] To better understand this step, we'll continue using the optimal connectivity graph formed by the merchant base table `t_shop_base` and the delivery area network table `t_delivery_grid` as an example. First, a serialization operation is performed on the optimal connectivity graph, converting the table structure, field comments, dynamic connectivity paths, and business mapping relationships into text. For example, it declares that an equi-join is possible between `t_shop_base.grid_id` and `t_delivery_grid.grid_id`, and notes that `shop_category` can take the value "fast food" and `grid_type` can take the value "core area". Second, the serialized text is structurally concatenated with system instructions, output format constraints, and the user question "Query the core delivery grid associated with fast food merchants" to form a complete prompt. This prompt can be directly input into the LLM to execute subsequent SQL generation.

[0059] As an example, the prompt words in this embodiment can be as follows: <System_Instruction> You are a seasoned PostgreSQL database expert. Your task is to translate the user's natural language questions into syntactically correct and efficient SQL queries based on the provided dynamic database schema context.

[0060] < / System_Instruction>

[0061] <Dynamic_Schema>

[0062] Table Structure and Field Comments

[0063] CREATE TABLE t_shop_base(

[0064] shop_id VARCHAR PRIMARY KEY COMMENT 'Unique Merchant Identifier' shop_category VARCHAR COMMENT'merchant category' grid_id VARCHAR COMMENT'Grid ID' ); CREATE TABLE t_delivery_grid( grid_id VARCHAR PRIMARY KEY COMMENT 'Unique identifier for the grid' grid_type VARCHAR COMMENT 'Grid type, such as core area, edge area' ); [Dynamic Connectivity Paths and Business Rules in Knowledge Graphs] - Recommended connection path: t_shop_base.grid_id=t_delivery_grid.grid_id(type: INNERJOIN) - Business concept mapping: The "merchant" mentioned by the user corresponds to t_shop_base, and the "delivery grid" corresponds to t_delivery_grid.

[0065] - Field value pattern mapping: "Fast food" corresponds to t_shop_base.shop_category='Fast food'; "Core" corresponds to t_delivery_grid.grid_type='Core area'.

[0066] < / Dynamic_Schema>

[0067] <Output_Format>

[0068] Please strictly adhere to the following output format: 1. First of all <think>Write your reasoning process within the tags, including which tables you need to use, how to perform JOIN operations between tables according to the provided rules, and which WHERE filtering conditions to apply.

[0069] 2. Then <sql>The tag should output the final executable PostgreSQL statement; do not output any unnecessary explanations.

[0070] < / Output_Format>

[0071] <User_Question>

[0072] Query the core delivery grid associated with fast food merchants

[0073] < / User_Question>

[0074] Step 104: Obtain the structured query statement (SQL) through the prompt words.

[0075] In the method of this embodiment, this step is executed collaboratively by the reinforcement learning aligned LLM and the corresponding reinforcement learning training framework Group Relative Policy Optimization (GRPO) algorithm engine. It aims to abandon the traditional supervised fine-tuning mode that relies solely on static text, and complete the model training by relying on the sandbox database environment and multi-dimensional reward mechanism. During the inference phase, multiple candidate SQLs are generated, and the final target SQL is determined through execution verification and comprehensive scoring, outputting a stable, efficient, and directly executable query statement.

[0076] As a feasible approach, inputting the prompt words into a pre-trained model yields the SQL output by that model. This pre-trained model is an LLM trained with reinforcement learning alignment. After receiving the prompt words from step 103, it completes inference through forward propagation via a single network. The LLM outputs a table association inference chain containing labels in a fixed format first, followed by the SQL output from the pre-trained model. <sql>The SQL statements are wrapped in tags, which clearly show the complete derivation process of table selection, join method, filtering conditions, etc.

[0077] The reward function of the pre-trained model during training can specifically include rewards for consistency of execution results, grammatical regularity, illusion entity penalties, connectivity path rationality, execution efficiency, and output format regularity. These rewards together constitute the total reward function, and by configuring preset weight hyperparameters for different reward items, precise constraints can be achieved on the model's generation behavior.

[0078] Specifically, the execution result consistency reward compares the execution result of the generated SQL statement with the manually set standard result. A perfect match earns the highest positive reward (e.g., +1.0), while inconsistency or an empty result earns 0 reward or a penalty. The syntax compliance reward determines whether the SQL statement conforms to the syntax rules of the target database such as PostgreSQL; a basic reward is awarded if there are no underlying syntax errors. The illusion entity penalty, also known as the schema consistency reward, verifies whether table names and field names originate from the optimal connected graph context using a static parser; fabricating non-existent entities triggers a severe penalty (e.g., -1.0). The connectivity path rationality reward verifies whether JOIN conditions and ON clauses conform to the physical foreign key and business soft association rules in the DKG, prohibiting illegal join methods such as Cartesian products. The execution efficiency reward obtains the estimated execution cost based on the database EXPLAIN or EXPLAINANALYZE command, penalizing inefficient writing methods such as full table scans and encouraging the generation of high-performance SQL statements. The output format compliance reward verifies whether the model strictly adheres to... <sql>Tag-formatted output ensures standardized output structure. Through the aforementioned reward mechanism, the model can deeply understand the constraints of the database table structure and the logical relationships between tables, generating high-quality, executable SQL statements in one go without external error correction.

[0079] In the method of this embodiment, the reward function can be defined as follows: (2) In the formula, R represents the total reward, and w1, w2, w3, w4, w5, and w6 are the preset weight hyperparameters for each reward dimension. exec As a reward for consistent execution results, R valid As a reward for grammatical standardization, R schema As punishment for hallucinatory entities, R join R is a reward for the rationality of connected paths. eff To implement efficiency-based rewards, R format Rewards are given for conforming to output format standards.

[0080] In the method of this embodiment, when the model outputs multiple SQL statements, further execution tests can be performed on these multiple SQL statements to remove SQL statements that report errors and obtain the result set returned by SQL statements that do not report errors. As an feasible solution, trial execution can be completed using a sandbox database that is isomorphic to the real environment. During the inference phase, multiple candidate SQL statements are generated in parallel by controlling the model with different temperature coefficients. After parsing and extraction, these statements are sent to the sandbox database for execution. Invalid SQL statements with syntax errors, execution exceptions, or other error issues are automatically filtered out, while valid SQL statements that can run normally and return data are retained. At the same time, the result set corresponding to each valid SQL statement is recorded.

[0081] After obtaining valid result sets, the confidence level of each result set is determined based on the frequency of its occurrence. As an feasible approach, the confidence level is determined by the result set consistency voting score, which is calculated using a majority voting mechanism. If SQL statements with different syntaxes return the same result set, the higher the frequency of occurrence of the corresponding result set, the higher the confidence and reliability. The score is positively correlated with the frequency, making it the core evaluation metric for ranking candidate SQL statements.

[0082] After determining the confidence level, the target SQL is selected from among the non-error-reporting SQL queries based on the confidence level and the execution cost of each non-error-reporting SQL query. As a feasible approach, a comprehensive scoring formula can be used to calculate the final score of the candidate SQL queries. The score is obtained by weighting the result set confidence score with an execution cost penalty term. The execution cost is obtained through parsing by the database query optimizer and is used to penalize time-consuming and inefficient SQL queries. During the calculation, the system prioritizes result set consistency while also considering execution efficiency, ultimately selecting the SQL query with the highest score as the target SQL query.

[0083] In this embodiment, the target SQL is used as the response to a natural language question. Through a complete process of multi-candidate generation, sandbox verification, confidence voting, and execution cost filtering, the accuracy and stability of natural language to SQL conversion can be further improved, ensuring that the returned target SQL is correct, syntactically correct, logically sound, and efficiently executed, enabling it to be directly used for database queries and return the data required by the user.

[0084] The method described in this embodiment effectively solves the problem of traditional vector retrieval lacking structural topology awareness by using dual-path entity links and knowledge graph-constrained multi-hop traversal. It can accurately identify implicit bridging relationships and soft business associations between tables, providing complete and reliable inter-table connectivity paths for large models and fundamentally suppressing the illusion of models fabricating JOIN conditions out of thin air. Simultaneously, relying on a reinforcement learning mechanism based on real database execution feedback, it guides the model to conduct deep logical deduction rather than shallow text imitation. Combined with inference chain output constraints, it improves the inherent logic of SQL generation and execution success rate, outperforming traditional supervised fine-tuning schemes in scenarios with multiple nested tables and complex conditional queries. Furthermore, the method in this embodiment has strong business migration capabilities and security stability. When the database structure or business rules change, adaptation can be completed simply by updating the knowledge graph, without retraining the large model. Through multi-SQL generation, sandbox execution verification, and comprehensive sorting and filtering mechanisms, erroneous statements can be automatically intercepted within the system, ensuring that the final output is syntactically correct, logically sound, and efficiently executed optimal SQL, further improving the overall reliability and resilience of the solution.

[0085] Example 2: This application discloses another data processing method; please refer to [link / reference]. Figure 2 This embodiment describes the method for converting natural language into structured query language.

[0086] Step 201: Connect to the target database and extract the metadata and business rules corresponding to each table.

[0087] Step 202: Construct entity nodes in the graph database based on metadata and business rules, and establish directed edges between each entity node to obtain the DKG.

[0088] Step 203: Extract proper nouns and text vectors from natural language problems.

[0089] Step 204: Match proper nouns with entity nodes in the DKG to obtain a set of matching nodes. At the same time, calculate the similarity between the text vector and the description vector of the entity node in the DKG to obtain a set of semantic nodes.

[0090] Step 205: Take the union of the matching node set and the semantic node set to obtain the anchor entity node set.

[0091] Step 206: Starting from each anchor entity node in the set of anchor entity nodes, obtain the connected paths in the graph database to form a connected graph.

[0092] Step 207: Determine the optimal connected graph based on the similarity between each connected graph and the natural language problem, as well as the number of hops and edge weights of each connected path in each connected graph.

[0093] Step 208: Convert the optimal connected graph into text data and combine it with a natural language question to obtain prompt words.

[0094] Step 209: Input the prompt words into the LLM aligned by reinforcement learning to obtain multiple candidate SQLs.

[0095] Step 210: Execute tests on multiple candidate SQL statements, remove SQL statements that report errors, and obtain the result set returned by SQL statements that do not report errors.

[0096] Step 211: Determine the confidence level of each result set based on the frequency of occurrence of each result set.

[0097] Step 212: Determine the target SQL based on the confidence level and the execution cost of each SQL that did not report an error.

[0098] Step 213: Reply to the target SQL as a natural language question.

[0099] Based on the data processing method disclosed in the above embodiments, this embodiment correspondingly discloses a data processing apparatus. Please refer to... Figure 3 The data processing apparatus includes: a construction unit 301, a search unit 302, a combination unit 303, and an acquisition unit 304; The construction unit 301 is used to construct a database knowledge graph (DKG) containing inter-table relationships based on the metadata and business rules of the target database. The search unit 302 is used to search for natural language questions from the user in the DKG to obtain the optimal connected graph; The combining unit 303 is used to combine the optimal connectivity graph and the natural language question to obtain prompt words; The acquisition unit 304 is used to acquire the structured query statement SQL through the prompt words.

[0100] Optionally, the building unit 301 includes: An extraction subunit is used to connect to the target database and extract the metadata and business rules corresponding to each table in the target database. A subunit is established to construct entity nodes corresponding to each table in the graph database, as well as directed edges between the entity nodes, based on the metadata and the business rules, to obtain the DKG; the directed edges are established based on the physical foreign key associations explicitly declared in the target database and / or soft associations formed according to the business configuration.

[0101] Optionally, the search unit 302 includes: A positioning subunit is used to locate the set of anchor entity nodes in the DKG based on the natural language question. A sub-unit is formed to obtain a connected path starting from each anchor entity node in the set of anchor entity nodes, and to form a candidate connected graph. A sub-unit is determined for identifying the optimal connected graph among the candidate connected graphs.

[0102] Optionally, the determining subunit includes: The comprehensive score acquisition subunit is used to acquire a comprehensive score for each candidate connected graph based on the number of hops and edge weights of each connected path in the candidate connected graph, as well as the similarity between the candidate connected graph and the natural language problem. The optimal determination subunit is used to determine the candidate connected graph whose comprehensive score meets the preset requirements as the optimal connected graph; the preset requirements are that the score is greater than the preset score or the ranking is higher than the preset rank.

[0103] Optionally, the positioning subunit includes: The matching subunit is used to match the proper nouns in the natural language problem with the entity nodes in the DKG to obtain a set of matching nodes; The computational subunit is used to extract text vectors from the natural language problem and calculate the similarity between the text vectors and the description vectors of the entity nodes in the DKG to obtain a semantic node set. The combined subunit is used to combine the matching node set and the semantic node set to obtain the anchor point entity node set.

[0104] Optionally, the connecting unit 303 includes: A transformation subunit is used to convert the optimal connected graph into text data; the text data includes at least one of business concept mapping relationships and field value constraints, as well as the structural information and connection paths of the optimal connected graph; The combination subunit is used to combine the text data with the natural language question to obtain the prompt word.

[0105] Optionally, the acquisition unit 304 includes: The output subunit is used to input the prompt word into the pre-trained model and obtain the SQL output by the pre-trained model; the reward function of the pre-trained model during the training process includes execution result consistency reward, syntax standardization reward, illusion entity penalty, connectivity path rationality reward, execution efficiency reward and output format standardization reward.

[0106] Optionally, the device further includes: The testing unit is used to perform execution tests on multiple SQL statements, remove SQL statements that report errors, and obtain the result set returned by SQL statements that do not report errors. A confidence level acquisition unit is used to determine the confidence level of each result set based on the number of times each result set appears. The SQL determination unit is used to determine the target SQL from among the non-error-reporting SQL based on the confidence level and the execution cost of each of the non-error-reporting SQLs; The response unit is used to provide the target SQL as a response to the natural language question.

[0107] Based on the above-described data processing method, this application also discloses a computer program product, which includes a computer program that, when run by a processor, is used to implement the above-described method.

[0108] Based on the above-described data processing method, this application also discloses a storage medium for storing computer program instructions, which, when executed by a central processing unit, can implement the steps of the above-described method.

[0109] The embodiments in this specification are described in a progressive manner. For the apparatus disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the description is relatively simple; relevant details can be found in the method section.

[0110] It should also be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0111] The steps of the methods or algorithms described in conjunction with the embodiments disclosed herein can be implemented directly by hardware, a software module executed by a processor, or a combination of both. The software module can be located in random access memory (RAM), main memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, removable disk, CD-ROM, or any other form of storage medium known in the art.

[0112] The features described in the embodiments of this specification can be substituted for or combined with each other, so that those skilled in the art can implement or use this application.

[0113] The above description of the disclosed embodiments enables those skilled in the art to make or use this application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of this application. Therefore, this application is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.< / sql> < / sql> < / sql> < / think>

Claims

1. A method of data processing, characterized by, include: Based on the metadata and business rules of the target database, a database knowledge graph (DKG) containing inter-table relationships is constructed. The optimal connectivity graph is obtained by searching for natural language questions from users in the DKG. By combining the optimal connectivity graph with the natural language problem, prompt words are obtained; The structured query statement (SQL) is obtained through the prompt words.

2. The method of claim 1, wherein, The construction of a database knowledge graph (DKG) containing inter-table relationships based on the target database's metadata and business rules includes: Connect to the target database and extract the metadata and business rules corresponding to each table in the target database; Based on the metadata and the business rules, entity nodes corresponding to each table and directed edges between the entity nodes are constructed in the graph database to obtain the DKG; the directed edges are established based on the physical foreign key associations explicitly declared in the target database and / or soft associations formed according to the business configuration.

3. The method of claim 1, wherein, The process of searching for natural language questions from users in the DKG to obtain the optimal connected graph includes: Based on the aforementioned natural language problem, locate the set of anchor entity nodes in the DKG; A connected path is obtained starting from each anchor entity node in the set of anchor entity nodes to form a candidate connected graph. The optimal connected graph is determined from each of the candidate connected graphs.

4. The method of claim 3, wherein, Determining the optimal connected graph from among the candidate connected graphs includes: Based on the number of hops and edge weights of each connected path in the candidate connected graph, and the similarity between the candidate connected graph and the natural language problem, a comprehensive score is obtained for each candidate connected graph. The candidate connected graph whose comprehensive score meets the preset requirements is determined as the optimal connected graph; the preset requirements are that the score is greater than the preset score or the ranking is higher than the preset rank.

5. The method of claim 3, wherein, The method for locating the set of anchor entity nodes in the DKG based on the natural language problem includes: The proper nouns in the natural language problem are matched with the entity nodes in the DKG to obtain a set of matching nodes; Extract the text vectors from the natural language problem, and calculate the similarity between the text vectors and the description vectors of the entity nodes in the DKG to obtain a semantic node set; The set of anchor entity nodes is obtained by combining the set of matching nodes and the set of semantic nodes.

6. The method of claim 1, wherein, The process of combining the optimal connected graph with the natural language problem to obtain prompt words includes: The optimal connectivity graph is converted into text data; the text data includes at least one of business concept mapping relationships and field value constraints, as well as the structural information and connectivity paths of the optimal connectivity graph; The text data is combined with the natural language question to obtain the prompt word.

7. The method according to claim 1, characterized in that, The step of obtaining the structured query statement SQL through the prompt words includes: The prompt words are input into the pre-trained model to obtain the SQL output by the pre-trained model; the reward function of the pre-trained model during the training process includes execution result consistency reward, syntax standardization reward, illusion entity penalty, connectivity path rationality reward, execution efficiency reward and output format standardization reward.

8. The method according to claim 1, characterized in that, After obtaining the structured query statement (SQL) through the prompt words, the method further includes: Execute tests on multiple SQL statements, remove SQL statements that report errors, and obtain the result set returned by SQL statements that do not report errors; The confidence level of each result set is determined based on the frequency of occurrence of each result set. Based on the confidence level and the execution cost of each of the SQL statements that did not report errors, the target SQL statement is determined from among the SQL statements that did not report errors. The target SQL is used as the response to the natural language question.

9. A data processing apparatus, characterized in that, include: Constructing units, searching units, combining units, and retrieving units; The construction unit is used to construct a database knowledge graph (DKG) containing inter-table relationships based on the metadata and business rules of the target database. The search unit is used to search for natural language questions from users in the DKG to obtain the optimal connected graph; The combining unit is used to combine the optimal connectivity graph and the natural language question to obtain prompt words; The acquisition unit is used to acquire the structured query statement (SQL) through the prompt words.

10. A computer program product, characterized in that, The computer program product includes a computer program that, when executed by a processor, is used to implement the method described in any one of claims 1-8.