Text-to-SQL (Structured Query Language) complex query statement generation method and system based on pre-training large model

Sentence-BERT filters relevant explanations and builds a hierarchical database pattern diagram, and uses the hypergraph neural network to update node features, solving the accuracy and performance problems of the existing Text-to-SQL method in complex query scenarios, achieving efficient SQL generation.

CN120492458APending Publication Date: 2025-08-15SHENYANG UNIVERSITY OF TECHNOLOGY
View PDF 0 Cites 4 Cited by

Patent Information

Application Number
CN202510595814.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-09
Publication Date
2025-08-15

AI Technical Summary

Technical Problem

When handling complex query scenarios, the existing Text-to-SQL method is difficult to effectively capture the complex associations and multi-level query logic between multiple tables, resulting in high error rates of generated SQL statements and lack of filtering of extended information, resulting in noise interference.

Method used

Using a method based on pre-trained large model, Sentence-BERT is used to calculate semantic similarity screening related explanations, build a hierarchical database pattern diagram, and update node features through the hypergraph neural network model to generate structured query statements.

Benefits of technology

Improves the accuracy and performance of SQL generation in complex query scenarios, reduces noise interference, better understands the database schema and generates SQL queries that comply with syntax rules.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120492458A_ABST
    Figure CN120492458A_ABST
Patent Text Reader

Abstract

The invention relates to a Text-to-SQL (Structured Query Language) complex query statement generation method and system based on a pre-trained large model. The method comprises the following steps: acquiring an input sequence comprising a user question and dictionary interpretation of table names and column names in a database; respectively inputting the user question and the dictionary explanation into a Sension-BERT model, obtaining Top-K explanations most related to the user question, and obtaining an enhanced input sequence according to the Top-K explanations most related to the user question; encoding the enhanced input sequence to generate an enhanced semantic vector, and constructing a hierarchical database mode pattern according to the enhanced semantic vector; inputting the hierarchical database mode graph into a hypergraph neural network model to update node features, and obtaining optimized node features; and obtaining a structured query statement according to the optimized node features. According to the method, complex association and multi-level query logic among multiple tables in a complex query scene can be better processed, and the performance in the complex query scene is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the fields of natural language processing, database management, and machine learning technology, and in particular to a method and system for generating complex Text-to-SQL query statements based on a pre-trained large model. Background Art

[0002] In various modern data-driven application scenarios, from enterprise business data analysis to data mining in scientific research, a large amount of valuable information is stored in databases. Structured Query Language (SQL), as the standard language for operating relational databases, can efficiently extract the required data from databases. However, SQL syntax is complex and requires professional knowledge to use proficiently, which creates a high threshold for non-technical users when performing data query and analysis. Therefore, Text-to-SQL technology came into being. Its core is to use natural language processing technology to automatically convert natural language queries entered by users into executable SQL query statements, thereby lowering the threshold for database use and allowing non-technical personnel to easily obtain data.

[0003] The key to implementing Text-to-SQL lies in accurately understanding the semantics of natural language and effectively matching and converting it to the database schema. Currently, the ExSQL method is a Text-to-SQL method that uses a dictionary to expand database schema information. However, it has the following problems:

[0004] Lack of filtering of extended information leads to noise interference: The ExSQL method does not filter the extended information for table and column names from the domain-wide dictionary, which inevitably introduces noise. For example, the word "mouse" has different meanings in databases from different domains, and this method cannot distinguish them. This may include irrelevant interpretations in the model input, interfering with the model's understanding of the database schema and affecting the accuracy of SQL translation.

[0005] Limited performance in complex query scenarios: When processing complex SQL queries, the ExSQL method only extends database schema information, making it difficult to fully grasp the complex relationships between multiple tables and the multi-level query logic involved. Faced with complex scenarios involving multi-table joins, nested subqueries, and aggregation operations, it lacks the ability to deeply model complex relationships, effectively capturing complex logical relationships in natural language and accurately converting them into SQL queries. In some scenarios requiring complex table join conditions and nested hierarchies, the SQL statements generated by the ExSQL method are prone to errors, making it difficult to meet the accuracy and efficiency requirements of complex query scenarios.

[0006] To solve the above technical problems, the present invention provides a method and system for generating complex Text-to-SQL query statements based on a pre-trained large model. Summary of the Invention

[0007] The purpose of the present invention is to provide a method and system for generating complex Text-to-SQL query statements based on a pre-trained large model, so as to better handle the complex associations between multiple tables and multi-level query logic in complex query scenarios, and improve the performance in complex query scenarios.

[0008] To achieve the above object, the present invention provides the following solutions:

[0009] The Text-to-SQL complex query generation method based on a pre-trained large model includes:

[0010] Obtaining an input sequence, wherein the input sequence includes a user question and a dictionary explanation of a table name and a column name in a database;

[0011] Input the user question and the dictionary explanation into the Sentence-BERT model respectively, obtain the top-K explanations most relevant to the user question, and obtain an enhanced input sequence based on the top-K explanations;

[0012] Encoding the enhanced input sequence to generate an enhanced semantic vector, and constructing a hierarchical database schema according to the enhanced semantic vector;

[0013] Inputting the hierarchical database pattern graph into a hypergraph neural network model to update node features and obtain optimized node features;

[0014] A structured query statement is obtained according to the optimized node features.

[0015] Optionally, obtaining the top-K explanations most relevant to the user question includes:

[0016] Inputting the user question into the Sentence-BERT model to obtain a first semantic vector;

[0017] Inputting the dictionary interpretation into the Sentence-BERT model to obtain a second semantic vector;

[0018] Obtaining the similarity between the user question and each dictionary explanation by using cosine similarity based on the first semantic vector and the second semantic vector;

[0019] According to the similarity, the top-K explanations most relevant to the user question are obtained.

[0020] Optionally, obtaining the enhanced input sequence includes: concatenating the most relevant Top-K interpretations with the input sequence according to a preset format to obtain the enhanced input sequence.

[0021] Optionally, the hierarchical database includes: a column layer, a query layer, and a pattern layer;

[0022] The hierarchical database schema diagram includes: a column layer, a query layer and a schema layer;

[0023] Each column is used as a column node, and the column nodes of the same table are connected to construct the column layer, wherein the initial features of the column node include the column representation vector and the word embedding vector after T5 encoding;

[0024] Each database table is treated as a table node, and a question node is introduced. The columns contained in each database table are connected, and the question node is connected to the table node to construct the query layer, wherein the connection weight is obtained according to the foreign key association degree, the initial features of the table node are obtained by aggregation operation of the column node features contained therein, and the initial features of the question node include the question representation vector after T5 encoding;

[0025] The structural information between all tables in the database is integrated to construct a global database schema node, and each database table is connected to construct the schema layer, wherein the initial features of the global database schema node are obtained by semantic encoding of the text representation after the serialization of the entire database structure.

[0026] Optionally, inputting the hierarchical database schema into the hypergraph neural network model to update node features includes: transmitting features of the column nodes of the column layer to the query layer, the table nodes of the query layer receiving the features of the column nodes, and integrating schema information of the table nodes themselves to obtain table-level features;

[0027] The table-level features are passed to the pattern layer. The global database pattern node of the pattern layer fuses the information of all tables, obtains the global representation of the entire database pattern, and updates the node features. During the node feature update process, the attention score of each neighbor node to the current node is calculated through the attention mechanism, and the features of the current neighbor nodes are aggregated to update the features of the current node.

[0028] Optionally, obtaining a structured query statement according to the optimized node features includes: inputting the optimized node feature representation into a T5 decoder to generate the structured query statement.

[0029] Optionally, after generating the structured query statement, the method includes: performing syntax verification on the structured query statement, and checking whether the table name and column name in the structured query statement exist in the database.

[0030] The present invention also provides a Text-to-SQL complex query statement generation system based on a pre-trained large model, including:

[0031] A pattern information expansion and dynamic filtering module is used to obtain an input sequence, wherein the input sequence includes a user question and dictionary explanations of table names and column names in a database, input the user question and the dictionary explanations into the Sentence-BERT model respectively, obtain the top-K explanations most relevant to the user question, and obtain an enhanced input sequence based on the top-K explanations;

[0032] A semantic encoding and hierarchical graph construction module, configured to encode the enhanced input sequence, generate an enhanced semantic vector, and construct a hierarchical database schema according to the enhanced semantic vector;

[0033] A hierarchical graph input and HGNN processing module is used to update the node features of the hierarchical database pattern graph to obtain optimized node features;

[0034] The SQL generation module is used to obtain a structured query statement according to the optimized node features.

[0035] The beneficial effects of the present invention are as follows: on the one hand, the present invention uses the Sentence-BERT model to calculate semantic similarity, dynamically screens database pattern information, retains only explanations closely related to user query intentions, and splices them into input sequences according to a specific structure, thereby enhancing the model's understanding of database patterns; on the other hand, it constructs an HGNN architecture comprising a column layer, a query layer, and a pattern layer, combines it with a relationship-aware attention mechanism, dynamically allocates weights horizontally based on relationship types, and avoids information attenuation vertically through a gating mechanism, thereby effectively modeling the hierarchical semantics of database patterns and improving complex query processing capabilities. BRIEF DESCRIPTION OF THE DRAWINGS

[0036] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0037] Figure 1 This is a flow chart of pattern information expansion and dynamic screening according to an embodiment of the present invention;

[0038] Figure 2 A flowchart of semantic coding and hierarchical graph construction according to an embodiment of the present invention;

[0039] Figure 3This is a flowchart of a method for generating complex Text-to-SQL query statements based on a pre-trained large model according to an embodiment of the present invention. DETAILED DESCRIPTION

[0040] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the present invention.

[0041] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the present invention is further described in detail below with reference to the accompanying drawings and specific embodiments.

[0042] Example 1:

[0043] like Figure 3 As shown, this embodiment provides a method for generating complex Text-to-SQL query statements based on a pre-trained large model, including:

[0044] Obtaining an input sequence, wherein the input sequence includes a user question and a dictionary explanation of a table name and a column name in a database;

[0045] Input the user question and dictionary explanations into the Sentence-BERT model respectively to obtain the top-K explanations most relevant to the user question. Based on the top-K explanations, obtain the enhanced input sequence;

[0046] Encode the enhanced input sequence to generate enhanced semantic vectors, and construct a hierarchical database schema based on the enhanced semantic vectors;

[0047] Input the hierarchical database pattern graph into the hypergraph neural network model to update the node features and obtain the optimized node features;

[0048] Obtain structured query statements based on the optimized node features.

[0049] Furthermore, obtaining the top-K explanations most relevant to the user's question includes:

[0050] Input the user question into the Sentence-BERT model to obtain the first semantic vector;

[0051] Input the dictionary interpretation into the Sentence-BERT model to obtain the second semantic vector;

[0052] According to the first semantic vector and the second semantic vector, the similarity between the user question and each dictionary explanation is obtained by using cosine similarity;

[0053] Based on the similarity, obtain the top-K explanations most relevant to the user's question.

[0054] Furthermore, obtaining the enhanced input sequence includes: concatenating the most relevant Top-K explanations with the input sequence according to a preset format to obtain the enhanced input sequence.

[0055] Furthermore, the hierarchical database schema diagram includes: column layer, query layer and schema layer;

[0056] Treat each column as a column node and connect the column nodes of the same table to build a column layer. The initial features of the column nodes include the column representation vector and word embedding vector after T5 encoding.

[0057] Each database table is treated as a table node, and question nodes are introduced. The columns contained in each database table are connected, and the question nodes are connected to the table nodes to build a query layer. The connection weight is obtained based on the foreign key association degree. The initial features of the table node are obtained by aggregating the features of the contained column nodes. The initial features of the question node include the question representation vector after T5 encoding.

[0058] The structural information between all tables in the database is integrated to build a global database schema node, and each database table is connected to build a schema layer. The initial features of the global database schema node are obtained by semantic encoding through the text representation of the serialized structure of the entire database.

[0059] Specifically, based on the physical storage structure of the database schema, a three-level hierarchical graph structure is constructed, corresponding to the column layer (L0), query layer (L1) and schema layer (L2).

[0060] The first layer (L0) is the column layer. Each column is represented as a node. All columns belonging to the same table are connected to each other to capture the co-occurrence of fields within the table. For example, in the Student table, the columns "name", "age", and "gender" are connected to each other to simulate the inherent connections between the columns. The initial features of each column node are composed of the T5 encoding result and the word embedding vector, combining semantic and structural information.

[0061] The second layer (L1) is the query layer, where each database table acts as a graph node and establishes connections with the columns it contains. The role of this layer is to integrate column-level information so that table-level nodes can extract comprehensive features from their subordinate columns. For example, the Student table aggregates features of columns such as name, age, and gender to build a more complete table-level representation. At the same time, question nodes are also located in this layer. For specific queries, table nodes can better understand the query requirements based on the information of their subordinate columns. In addition, foreign key information is explicitly modeled at this layer, and the connection weight is adjusted by calculating the relevance of foreign keys.

[0062] The third layer (L2) is the schema layer, which introduces a global database schema node and establishes connections with all table nodes. The main function of this layer is to provide global information for the overall schema of the database, so that the model can more accurately select relevant tables and columns when generating SQL. In this layer, the relational attention mechanism is used to calculate the importance of each table to the global schema, so that key tables can receive higher attention.

[0063] Furthermore, inputting the hierarchical database schema into the hypergraph neural network model to update node features includes:

[0064] The features of the column nodes of the column layer are passed to the query layer. The table nodes of the query layer receive the features of the column nodes and integrate the schema information of the table nodes themselves to obtain table-level features.

[0065] The table-level features are passed to the pattern layer. The global database pattern node of the pattern layer fuses the information of all tables to obtain the global representation of the entire database pattern for node feature update. During the node feature update process, the attention score of each neighbor node to the current node is calculated through the attention mechanism, and the features of the current neighbor nodes are aggregated to update the features of the current node.

[0066] Specifically, an attention mechanism is introduced in the process of updating node features. The role of this mechanism is to dynamically adjust the information contribution of adjacent nodes by calculating the influence of each neighboring node on the current node (i.e., the attention score). In the propagation from column nodes to table nodes, the model assigns different weights to column nodes based on the importance of the query task. For example, some columns may be more critical in the query, and these columns should contribute more to the table node, while other columns that are not related to the query can be weakened through the attention mechanism. Similarly, in the propagation from table to pattern, the contribution of different tables to the global pattern will be dynamically adjusted according to the query content to ensure that the representation of the final pattern layer can reflect the most critical database information.

[0067] Furthermore, obtaining a structured query statement according to the optimized node features includes: inputting the optimized node features into a T5 decoder to generate a structured query statement.

[0068] Furthermore, after the structured query statement is generated, the following steps are included: performing syntax verification on the structured query statement, and checking whether the table name and column name in the structured query statement exist in the database.

[0069] Specifically, the SQL execution optimization module is not only the terminal link of the entire system, but also provides feedback to the preceding SQL generation module. The database can analyze SQL execution efficiency through query logs, identify inefficient query patterns, and feed this information back to the SQL generation module to optimize subsequent SQL generation strategies.

[0070] Example 2:

[0071] This embodiment provides a Text-to-SQL complex query statement generation system based on a pre-trained large model, including:

[0072] The pattern information expansion and dynamic filtering module is used to obtain an input sequence, which includes user questions and dictionary explanations of table and column names in the database. The user question and dictionary explanations are input into the Sentence-BERT model respectively to obtain the top-K most relevant explanations of the user question. Based on the top-K most relevant explanations, an enhanced input sequence is obtained.

[0073] Specifically, let's take a specific scenario as an example: the user enters a natural language question "View the names of males under the age of 25", and the database contains a table called Student, with columns including name, age, and gender.

[0074] First, split the table names and column names in the database into independent words. For example, the table name Student does not need to be split and can be treated as a word directly; the column names age and gender are also treated as independent words. Next, query the complete explanation of each word through the Oxford Dictionary. For example: the dictionary explanation of the table name Student is: "a person who is studying at a school or college", "someone who learns from a teacher"; the explanation of the column name age is: "the length of time that a person has lived", "a particular period of history"; the explanation of the column name gender is: "the state of being male or female", "a grammatical category of nouns";

[0075] The second step is to select the k most relevant explanations from all explanations. The Sentence-BERT model is used to calculate the semantic similarity between the question and each explanation. If the similarity is greater than a threshold, the top-K most relevant explanations are used as augmented information and spliced into the input sequence according to a preset format. If the similarity is less than the threshold, the user question and database schema are used as input without adding augmented information. The specific steps are as follows:

[0076] Input the user question "Look for the names of men under 25 years old" into the Sentence-BERT model and obtain the first semantic vector V Q , input the dictionary interpretation of each table name / column name into the model to obtain the corresponding first semantic vector

[0077] The relevance between the question and each explanation is measured by cosine similarity, which is:

[0078]

[0079] For example, the question has a similarity of 0.85 with the first explanation of the Student table and 0.45 with the second explanation; a similarity of 0.92 with the first explanation of the age column and 0.30 with the second explanation; and a similarity of 0.65 with the first explanation of the gender column and 0.10 with the second explanation. For each table or column name, the top explanation with the highest similarity (adjustable parameter) is retained. Finally, the explanation "a person who is studying..." is retained for the Student table, "the length of time..." for the age column, and "the state of being male..." for the gender column.

[0080] Finally, the filtered explanations are spliced into the input sequence in a fixed format to form the structure of "[CLS] question [TAB] table name explanation [COL] column name explanation [SEP]". The process is as follows Figure 1 shown.

[0081] The semantic encoding and hierarchical graph construction module is used to encode the enhanced input sequence, generate enhanced semantic vectors, and construct a hierarchical database schema according to the enhanced semantic vectors;

[0082] The core task of this module is to build a hierarchical database schema representation and combine it with semantic encoding methods to enable the model to accurately capture the hierarchical structure of the database schema to improve the accuracy of SQL generation. The main processes of this module include text encoding, hierarchical graph structure construction, and cross-layer information interaction to ensure that database schema information can be efficiently transmitted and integrated between different levels. The process is as follows: Figure 2 shown.

[0083] First, the enhanced database schema information (including the user question, table name, column name, and their filtered semantic extensions) is encoded. This process is based on the T5 encoder, which uses its multi-layer Transformer architecture to deeply model the input sequence and generate context-aware semantic representations. The encoded vector generated by T5 not only includes the original database schema information but also incorporates dictionary definitions filtered by Sentence-BERT to ensure semantic consistency between the database schema information and the user query. Let the input sequence be X. The output after T5 encoding is represented as:

[0084] H=T5 encoder (X)

[0085] Where H is the hidden state matrix of all tokens, and each row represents the semantic vector of the corresponding token in the context. This encoding vector serves as the basic feature representation for the subsequent hierarchical graph structure construction.

[0086] Based on the physical storage structure of the database model, a three-level hierarchical graph structure is constructed, corresponding to the column layer

[0087] (L0), query layer (L1) and pattern layer (L2).

[0088] Before building the graph structure, the model input includes the natural language question, database table and column names, and the top-K interpretations or expanded explanations related to the question retrieved via Sentence-BERT. These together constitute the enhanced input sequence. After semantic encoding through the T5 encoder, the context vector of each token is obtained, which serves as the initial node feature of the graph neural network.

[0089] The first layer (L0) is the column layer. Each column is represented as a node. All columns belonging to the same table are connected to each other to capture the co-occurrence of fields within the table. For example, in the Student table, the columns "name", "age", and "gender" are connected to each other to simulate the inherent connections between the columns. The initial features of each column node are composed of the column representation vector and the word embedding vector of the T5 encoding result, combining semantic and structural information.

[0090] The second layer (L1) is the query layer, in which each database table is treated as a table node and connected to the columns it contains. The role of this layer is to integrate column-level information so that table-level nodes can extract comprehensive features from their subordinate columns. For example, the Student table aggregates the features of columns such as name, age, and gender to construct a more complete table-level representation. The initial features of a table node are obtained by aggregation operations on the features of its column nodes. Common aggregation methods include average pooling, weighted sum, or attention mechanisms. The features of each column node are derived from the output of the first layer, integrating the semantic representation and structural embedding of the column, so that the table node can inherit this information during the feature aggregation process. In addition, to improve the ability to identify structures, the table node features can also be combined with structural information such as the embedded representation of the table name, the number of fields, and the statistics of primary and foreign keys to further enrich the expressive power of the table-level representation.

[0091] In addition to table nodes, this layer also introduces question nodes, which represent the natural language query statements posed by the current user. Question nodes serve as semantic bridges, with their features initialized using the question representation vector encoded using T5. They establish connections with all table nodes. Connection weights, determined by the semantic similarity between question and table nodes, are used to model the strength of the semantic association between query statements and individual tables. This enables the model to identify the tables most relevant to the query semantics during training and inference, further guiding downstream SQL generation. Furthermore, foreign key information is explicitly modeled at this layer. Connection weights are adjusted by calculating the relevance of foreign keys, strengthening information interaction between tables with structural dependencies.

[0092] Question nodes are also located at this layer. For specific queries, table nodes can better understand the query requirements based on the information of their subordinate columns. In addition, foreign key information is explicitly modeled at this layer, and the connection weights are adjusted by calculating the relevance of foreign keys.

[0093] The third layer (L2) is the schema layer. This layer introduces a global database schema node to model a global view of the entire database structure. This node does not represent a specific table or column, but rather abstractly integrates the structural information across all tables in the database. The initial representation of this node is semantically encoded from the serialized text representation of the entire database structure. By establishing connections with all table nodes, it captures the dependencies and interactions between tables in a global context.

[0094] The model layer uses a relational attention mechanism to calculate the importance of each table relative to the global structure, giving key tables greater attention. During backpropagation, this global information is fed back to table-level nodes, guiding the model to more accurately understand the semantic role of each table in the current query task, thereby improving the ability to select structures when generating SQL.

[0095] After completing the construction of the graph structure, the graph neural network (HGNN) module performs multi-layer updates on the node representations in the graph to generate optimized column and table representations as input to the subsequent SQL generation module. During the training process, the entire system adopts an end-to-end supervised learning approach. Specifically, the node features output by the graph neural network are input into the SQL generator (T5 decoder) together with the semantic vector of the language model to gradually generate the target SQL query statement. The training goal is to minimize the sequence difference between the generated SQL and the real SQL, and the cross-entropy loss function is usually used for optimization. The training data includes natural language questions, database schema, Top-K explanation information related to the question, and standard SQL label statements to ensure that the model can be aligned in both structural and semantic dimensions, enhance structural perception and improve SQL generation accuracy.

[0096] After the hierarchical database schema graph is constructed, information is propagated bidirectionally between layers to enhance representational capabilities. Column-level information is transferred to the table level through an aggregation mechanism, enabling table-level features to integrate information from different columns. Table-level features are further transferred to the schema layer, enabling the global representation of the database schema to perceive the importance of each table. During backpropagation, global information from the schema layer is fed back to the query layer via gated residual connections, adjusting table features to better align with the overall database structure. Similarly, table-level features are further propagated to the column layer, enabling column nodes to perceive global semantic information.

[0097] Cross-layer information propagation is a key component of hierarchical graph neural networks, enabling the network to transfer information between different layers (columns, tables, and patterns). In order to effectively fuse features from different layers, an aggregation mechanism and gated residual connections are used.

[0098] During information propagation, information is first aggregated from the column level (L0) to the query level (L1). Each table-level node is average-pooled over its column node features to obtain a comprehensive table-level representation. This approach aggregates column-level information, reduces redundancy, and preserves the hierarchical nature of the database schema.

[0099] To further optimize cross-layer information propagation and prevent information attenuation within the hierarchical structure, gated residual connections are used to dynamically adjust the strength of information interaction between layers. This mechanism uses a gating network to calculate the weights for cross-layer information transmission, ensuring that when pattern-layer information propagates to the query layer, it has a more significant impact on relevant tables, rather than being indiscriminately distributed to all table-level nodes. This mechanism ensures that when pattern information propagates downward, it does not excessively shift table-level node features, while allowing global information to have a stronger impact on specific table-level nodes, thereby improving the model's ability to select key SQL tables.

[0100] Hierarchical graph input and HGNN processing module, used to update node features of the hierarchical database pattern graph and obtain optimized node features;

[0101] This module focuses on how to use graph neural networks to process hierarchical database schemas, so that the relationships between nodes can be effectively modeled and ultimately form table and column representations that can be used for SQL generation. The input of this module is the hierarchical database schema graph constructed in the previous stage. In the previous stage, the database schema has been converted into a directed graph with a three-layer structure consisting of columns, tables, and schemas. The features of each node are composed of the semantic vector of the T5 encoder and contextual information. At the same time, the connection weights between nodes have also been initialized through the attention mechanism. After entering this stage, the model needs to further update the information of these nodes so that their features are not limited to their own information, but can also combine features from neighboring nodes to achieve cross-level information fusion.

[0102] In this module, information propagation begins. During the information transfer from column nodes to table nodes, table nodes not only receive features from column nodes but also integrate their own schema information to form more expressive table-level features. In this way, local information at the column level is gradually transferred to the query level. Subsequently, information at the query level is further transferred to the schema level. Schema nodes at the schema level fuse information from all tables to form a global representation of the entire database schema. This way, local information at lower levels is gradually transferred upward, ultimately helping the model develop a deeper understanding of the database's overall schema.

[0103] The core of information updating lies in the application of graph neural networks. GNNs employ a message-passing framework, aggregating information from neighboring nodes to update the current node's features. In each iteration, a node's features are updated not only based on its initial features but also based on the features of its adjacent nodes (i.e., directly connected tables, columns, or schema nodes). This process helps nodes gain more global context through multi-level information interaction, ensuring that node features accurately reflect their position and importance within the database schema.

[0104] In the process of updating node features, an attention mechanism is introduced. The role of this mechanism is to dynamically adjust the information contribution of adjacent nodes by calculating the influence of each neighboring node on the current node (i.e., the attention score). In the propagation from column nodes to table nodes, the model assigns different weights to column nodes based on the importance of the query task. For example, some columns may be more critical in the query, and these columns should contribute more to the table node, while other columns that are not relevant to the query can be weakened through the attention mechanism. Similarly, in the propagation from table to pattern, the contribution of different tables to the global pattern will be dynamically adjusted according to the query content, ensuring that the representation of the final pattern layer can reflect the most critical database information.

[0105] After multiple rounds of information propagation and node updates, the final generated node features not only contain the original semantic information, but also integrate contextual information from adjacent nodes and different levels.

[0106] The SQL generation module is used to obtain structured query statements based on the optimized node feature representation.

[0107] The core task of this module is to generate SQL queries using the generative model T5, based on the optimized table and column features obtained from the HGNN processing module. By accurately capturing the database's structural information and the user's query intent, the model automatically constructs SQL statements that conform to the database's grammatical rules, completing the transformation from natural language questions to structured queries.

[0108] First, the node features optimized by the HGNN are passed as input to the SQL generation module. To ensure that the generated SQL query accurately reflects the user's intent, the model performs context-aware processing based on these features, combining them with the user's natural language query input to generate a suitable SQL structure. The input query can include selection operations (SELECT), filtering conditions (WHERE), and sorting (ORDER BY). The generation module then constructs an appropriate SQL query based on these conditions.

[0109] During the SQL generation phase, the system adopts an autoregressive generation mechanism based on the T5 decoder architecture, and constructs SQL statements word by word. The decoder uses the semantic vector obtained in the encoding phase as conditional input, integrates the user's natural language query intention and the hierarchical structure information of the database model, and gradually generates each clause from left to right according to the structural order of the SQL statement. To improve the generation accuracy and grammatical rationality, the system introduces the Beam Search strategy in the autoregressive generation process to retain multiple candidate paths, and at the same time combines the structure guidance mechanism to dynamically constrain the generated words to make them conform to the SQL syntax and the logical relationship within the clause. In addition, the generation process adopts a clause staged construction strategy, first predicting and determining the main clauses of SQL (such as SELECT, FROM, WHERE, etc.), and then generating the internal content of each clause separately, thereby effectively reducing the error propagation in long sequence generation and improving the accuracy and readability of the overall SQL generation.

[0110] In the specific generation process, the T5 decoder receives the word generated at the previous time step t-1 as input at each time step t, and combines the context information H output by the encoder to predict the target word y at the current time step tThis mechanism ensures that the decoder is historically dependent, meaning that the generation of each token is not only influenced by all previous tokens but also constrains the generation of subsequent tokens. For example, the column names in the SELECT clause restrict the fields available in the WHERE clause; the table specified in the FROM clause determines the validity of subsequent fields and filter conditions; and the use of aggregate functions (such as AVG and MAX) affects the structure of the GROUP BY clause.

[0111] These dependencies are effectively modeled through the positional encoding mechanism and self-attention mechanism of the T5 decoder, enabling the model to dynamically capture the semantic and structural relationships between previous and subsequent tokens during the generation process, thereby generating SQL statements that conform to logical and grammatical constraints.

[0112] To further improve generation quality and accuracy, the model introduces grammatical structure constraints and database schema guidance mechanisms. First, by integrating grammatical priors, the model can dynamically restrict the type and position of candidate tokens, avoiding the generation of grammatically illegal structures. Second, combined with the information encoded in the structure-aware graph, the decoder can focus on the columns, tables, or foreign key information most relevant to the current context, thereby improving the accuracy of generated tokens. For example, when the user question implies aggregation semantics, the model will prioritize relevant functions and locate the appropriate fields.

[0113] In addition, the model also supports a decoding strategy based on the abstract syntax tree (AST) structure. By maintaining a syntax state stack, it dynamically determines the SQL component categories currently allowed to be generated during the generation process, further improving syntax consistency and execution.

[0114] The final output SQL statement not only meets syntactic correctness, but also maintains strong semantic consistency with the user's natural language query, ensuring that it can be correctly parsed by the downstream database system and return the expected results.

[0115] During the output phase, the generated SQL query statements undergo post-processing for optimization and correction. This includes schema validation (ensuring the correctness of table and column names), semantic correction (such as converting "gender='male'" to "gender='M'" and other standardization processes), and query optimization (such as adding index hints to queries). This ensures that the generated SQL statements can be executed efficiently and accurately meet user query requirements.

[0116] After this series of processing, the final output SQL query statement becomes the core instruction for executing database operations. This process not only relies on accurate table and column representation, but also requires the integration of contextual information and grammatical rules to ensure that the generated SQL query not only meets the user's intent but also runs efficiently in the database system.

[0117] SQL execution and optimization module:

[0118] The SQL execution and optimization module's primary task is to ensure that generated SQL queries are correctly parsed and efficiently executed. It also leverages database optimization techniques to improve query performance, making the query process smoother and more efficient. In the previous SQL generation module, the model generated SQL statements based on the user's natural language input and database schema information. This module's primary task is to receive these SQL statements, execute them in the database system, and optimize the query structure to ensure that queries return results quickly.

[0119] During the execution process, the SQL statement is first verified to ensure its syntax is correct and to check whether the table and column names involved in the SQL exist in the database schema. After the SQL is verified, the database management system parses the query statement and generates an execution plan. Optimizing the execution plan is a key task of the database query optimizer. It selects the optimal query execution path based on factors such as database statistics, index structure, and data storage methods. For example, in a multi-table query, the optimizer determines the order in which the tables should be joined and selects an appropriate join method (such as hash joins, nested loop joins, etc.) to reduce computational overhead and improve query efficiency. Furthermore, the optimizer can automatically select indexes to avoid unnecessary full table scans, further improving data retrieval speed.

[0120] This module is closely integrated with the previous SQL generation module to ensure query accuracy and execution efficiency. The output of the SQL generation module directly affects the optimization effect of this module. High-quality SQL statements can reduce the computational cost during execution and improve query efficiency. For example, during the SQL generation phase, if the generated query statement can prioritize the use of index fields for filtering, this module can directly use the index for efficient query execution when executing the query without scanning the entire data table, thereby reducing I / O overhead. Conversely, if the SQL statement contains redundant nested queries or unoptimized join operations, even if the database optimizer can perform partial optimization, it may result in longer query execution time. Therefore, the optimization strategy during the SQL generation phase directly affects the performance of SQL execution.

[0121] The SQL execution optimization module is not only the terminal link of the entire system, but also provides feedback information to the previous SQL generation module. The database can analyze the efficiency of SQL execution through query logs, identify inefficient query patterns, and feed back relevant information to the SQL generation module to optimize subsequent SQL generation strategies. For example, if it is found that certain types of queries occupy a large amount of computing resources when executed, the SQL generation module can adjust the strategy to tend to generate query structures that are more in line with the database optimization strategy, thereby improving query performance. In this collaborative optimization model, the module is not only responsible for the execution of SQL, but also plays a key role in the query optimization process of the entire system, ensuring that the Text-to-SQL system can run efficiently and stably.

[0122] The system of this embodiment has the following effects:

[0123] Solving the problem of unfiltered extended information: Sentence-BERT is used to calculate semantic similarity for a dynamic semantic filtering step, reducing the interference of irrelevant interpretation information on the model. After obtaining the dictionary interpretations of the table and column names, the semantic similarity between the user query and each interpretation is calculated, and only the top-K interpretations that best match the query intent are retained. For example, when processing queries containing "budget," relevant interpretations such as "future investment plan" are filtered out, and irrelevant interpretations such as "historical financial records" are excluded, reducing the redundant information processed by the model. This allows the model to focus more on information relevant to the query in subsequent processing, improving the accuracy of its understanding of the database schema, and thereby improving the accuracy of SQL statement generation.

[0124] Solve the problem of limited performance in complex query scenarios: By building a three-level hierarchical graph structure (column layer, query layer, and schema layer), the model's ability to model complex relationships in the database schema is enhanced. At the column layer, each database column is represented as a node and connections are established between columns in the same table to capture the co-occurrence relationship of fields within the table. For example, in the "Student" table, connections are established between the "name", "age", and "gender" columns, so that the model can understand the internal connection between columns; the query layer integrates column-level features and adjusts the connection weights through foreign key information, allowing table-level nodes to extract comprehensive features from subordinate columns and clarify the association between tables; the schema layer introduces global database schema nodes, calculates the importance of each table to the global schema, and assists the model in accurately selecting related tables and columns. This series of hierarchical modeling steps enables the model to more clearly grasp the relationship between tables when dealing with complex scenarios such as multi-table joins and nested queries, thereby improving the accuracy of SQL generation.

[0125] The embodiments described above are merely descriptions of preferred embodiments of the present invention and are not intended to limit the scope of the present invention. Without departing from the spirit of the present invention, various modifications and improvements made to the technical solutions of the present invention by persons skilled in the art should fall within the scope of protection defined by the claims of the present invention.

Claims

1. A method for generating complex query statements in Text-to-SQL based on a pre-trained large model, characterized in that: include: Obtaining an input sequence, wherein the input sequence includes a user question and a dictionary explanation of a table name and a column name in a database; Input the user question and the dictionary explanation into the Sentence-BERT model respectively, obtain the top-K explanations most relevant to the user question, and obtain an enhanced input sequence based on the top-K explanations; Encoding the enhanced input sequence to generate an enhanced semantic vector, and constructing a hierarchical database schema according to the enhanced semantic vector; Inputting the hierarchical database schema into a hypergraph neural network model to update node features and obtain optimized node features, wherein the hypergraph neural network model is trained by a training set, and the training set includes historical natural language questions, databases, top-K explanation information related to the questions, and standard SQL tag statements; A structured query statement is obtained according to the optimized node features.

2. The method for generating complex query statements using Text-to-SQL based on a pre-trained large model according to claim 1, characterized in that: Obtaining the top-K most relevant explanations for the user's question includes: Inputting the user question into the Sentence-BERT model to obtain a first semantic vector; Inputting the dictionary interpretation into the Sentence-BERT model to obtain a second semantic vector; Obtaining the similarity between the user question and each dictionary explanation by using cosine similarity based on the first semantic vector and the second semantic vector; According to the similarity, the top-K explanations most relevant to the user question are obtained.

3. The method for generating complex query statements using Text-to-SQL based on a pre-trained large model according to claim 1, characterized in that: Acquiring the enhanced input sequence includes: concatenating the most relevant Top-K interpretations with the input sequence according to a preset format to acquire the enhanced input sequence.

4. The method for generating complex query statements using Text-to-SQL based on a pre-trained large model according to claim 1, characterized in that: The hierarchical database schema diagram includes: a column layer, a query layer and a schema layer; Each column is used as a column node, and the column nodes of the same table are connected to construct the column layer, wherein the initial features of the column node include the column representation vector and the word embedding vector after T5 encoding; Each database table is treated as a table node, and a question node is introduced. The columns contained in each database table are connected, and the question node is connected to the table node to construct the query layer, wherein the connection weight is obtained according to the foreign key association degree, the initial features of the table node are obtained by aggregation operation of the column node features contained therein, and the initial features of the question node include the question representation vector after T5 encoding; The structural information between all tables in the database is integrated to construct a global database schema node, and each database table is connected to construct the schema layer, wherein the initial features of the global database schema node are obtained by semantic encoding of the text representation after the serialization of the entire database structure.

5. The method for generating complex query statements using Text-to-SQL based on a pre-trained large model according to claim 4, characterized in that: Inputting the hierarchical database pattern graph into the hypergraph neural network model to update node features includes: The features of the column nodes of the column layer are transferred to the query layer. The table nodes of the query layer receive the features of the column nodes and integrate the schema information of the table nodes themselves to obtain table-level features. The table-level features are passed to the pattern layer. The global database pattern node of the pattern layer fuses the information of all tables, obtains the global representation of the entire database pattern, and updates the node features. During the node feature update process, the attention score of each neighbor node to the current node is calculated through the attention mechanism, and the features of the current neighbor nodes are aggregated to update the features of the current node.

6. The method for generating complex query statements using Text-to-SQL based on a pre-trained large model according to claim 1, characterized in that: Acquiring a structured query statement according to the optimized node features includes: inputting the optimized node feature representation into a T5 decoder to generate the structured query statement.

7. The method for generating complex query statements using Text-to-SQL based on a pre-trained large model according to claim 1, characterized in that: After the structured query statement is generated, the following steps are performed: syntax verification is performed on the structured query statement, and a check is made as to whether the table name and column name in the structured query statement exist in the database.

8. A system for implementing the method for generating complex Text-to-SQL query statements based on a pre-trained large model according to any one of claims 1 to 7, characterized in that: include: A pattern information expansion and dynamic filtering module is used to obtain an input sequence, wherein the input sequence includes a user question and dictionary explanations of table names and column names in a database, input the user question and the dictionary explanations into the Sentence-BERT model respectively, obtain the top-K explanations most relevant to the user question, and obtain an enhanced input sequence based on the top-K explanations; A semantic encoding and hierarchical graph construction module, configured to encode the enhanced input sequence, generate an enhanced semantic vector, and construct a hierarchical database schema according to the enhanced semantic vector; A hierarchical graph input and HGNN processing module is used to update the node features of the hierarchical database pattern graph to obtain optimized node features; The SQL generation module is used to obtain a structured query statement according to the optimized node features.

Citation Information

Cited By

  • Enhanced application method and device of semantic analysis model, equipment and storage medium

    CN120832889A

  • Enhanced application method, device and equipment of semantic parsing model, and storage medium

    CN120832889B

  • Social big data-oriented multi-modal content aggregation and expression normalization method and device

    CN121278085A

  • Natural language access system and method based on large language model and business metadata

    CN121935273A