A SQL generation method for injecting optimal pattern items and historical interaction information

By incorporating pre-filtered cross-encoders and historical interaction information, combined with SQL syntax tree depth-first decoding, the accuracy and large-scale database input problems of Chinese natural language query to SQL conversion are solved, achieving high recall and accurate SQL generation.

CN117033423BActive Publication Date: 2025-10-28UNIV OF ELECTRONICS SCI & TECH OF CHINA
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202311004567.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-08-10
Publication Date
2025-10-28
Estimated Expiration
2043-08-10

AI Technical Summary

Technical Problem

Existing technologies struggle to effectively understand Chinese natural language query intent and generate complex SQL statements in real-world applications. They also face challenges such as the inability to fully input large-scale databases and performance degradation due to changes in database structure.

Method used

The optimal pattern items are extracted using a pre-screened cross encoder and historical interaction information is incorporated. SQL statements are generated through depth-first search using an SQL syntax tree. The relationships between database pattern items are calculated using a Transformer structure. Accurate SQL statements are generated by combining a focus loss function and an LSTM decoding model.

Benefits of technology

It improves the accuracy of converting Chinese natural language queries to SQL, solves the problems of large-scale database input and complex query generation, and generates SQL statements with high accuracy and recall, supporting the generation of SQL statements with complex structures.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117033423B_ABST
    Figure CN117033423B_ABST
Patent Text Reader

Abstract

This invention discloses a method for generating SQL by injecting optimal pattern terms and historical interaction information, applied to the semantic parsing field of natural language processing. It addresses the problem that existing technologies struggle to generate structured SQL from complex cross-domain Chinese natural language queries for practical business intelligence applications. This invention injects optimal pattern terms highly relevant to Chinese natural language queries into the model through a pre-selected cross-encoder. Then, it simplifies complex queries by using the user's historical queries. Finally, it proposes a complete SQL structure prediction model, generating a node sequence from the root node in depth-first order. This model integrates features from the previous time step, parent node features, context features, and node type features to maximize the probability of the correct SQL statement corresponding to the natural language problem, generating an abstract SQL syntax tree. The final SQL statement is then generated by traversing the SQL syntax tree depth-first.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of semantic parsing in natural language processing, and specifically relates to a technique for converting natural language into structured SQL statements. Background Technology

[0002] Semantic parsing is a task that maps natural language queries to corresponding machine-executable logical forms, transforming complex natural language into SQL (Structured Query Language) statements. It's a branch of semantic parsing that aims to break down the barriers between non-technical personnel and databases, shortening the distance between users and databases. The emergence of large-scale cross-domain datasets like Spider and WikiSQL has greatly promoted research into natural language to SQL. While the datasets mentioned above are all based on English, the release of Chinese-specific datasets TableQA and DuSQL, as well as the English translation dataset CSpider through competitions, strongly promotes the development and application of SQL generation technology in Chinese-speaking scenarios.

[0003] However, current research results and work cannot truly enable this technology to be applied in real-world scenarios. The problems include: (1) Complex queries in the spoken form of Chinese natural language make it difficult for machines to understand the user's true query intent. (2) The database size designed in real-world application scenarios may be very large, making it impossible to fully input information into the model. (3) Changes in database structure lead to performance degradation.

[0004] Early research often involved inputting all the information contained in the database into the model in an effort to fully grasp the information it contained, without considering the real-world application scenarios where users struggle to express their query intent in a single sentence. This made generating complex SQL statements difficult. Summary of the Invention

[0005] To address the practical application of generating structured SQL from complex cross-domain Chinese natural language in business intelligence scenarios, this invention proposes a SQL generation method that injects optimal pattern items and historical interaction information. By designing a pre-filtered cross encoder to extract the optimal pattern items and injecting historical user interaction information, the method simplifies the generation of complex queries. Finally, it generates the correct SQL statement through a depth-first approach in the form of an SQL syntax tree.

[0006] The technical solution adopted in this invention is: a SQL generation method for injecting optimal pattern items and historical interaction information, comprising:

[0007] S1. Pre-screening cross encoder: Based on the input natural language query and the corresponding database pattern items, calculate the pattern items most relevant to the natural language query.

[0008] S2. The SQL encoding module, which incorporates historical interaction information, constructs an interaction graph between natural language and historical queries based on the optimal pattern items output by the pre-filtered cross encoder and the user's historical interaction information, and encodes the resulting interaction graph.

[0009] S3. Decode the encoding result of step S2 based on the SQL structure prediction model.

[0010] The beneficial effects of this invention: By using the SQL generation technology proposed in this invention to inject optimal pattern items and historical interaction information, the following beneficial effects can be achieved:

[0011] 1. In practical application scenarios where Chinese natural language queries are converted into SQL, the database size may be too large to be fully input into the model. The present invention proposes a pre-screening cross encoder that prioritizes the calculation of the database pattern items (including tables and columns) most relevant to the natural language query. The optimal pattern items, including the first k1 database tables and the first k2 data columns, are injected into the model, ensuring high recall while preventing excessive noise injection.

[0012] 2. The generation of complex SQL statements corresponds to complex natural language statements. Therefore, this invention proposes that before users input Chinese natural language query statements, they should first input multiple simple interdependent query statements based on historical interaction information to generate the final complex query.

[0013] 3. When performing calculations based on the Transformer structure, add relationships between the segmented words of the input sequence to make the model more biased towards these predefined relationships during processing, including... Database schema items include the relationships between database tables and columns; The linking relationship between word segmentation in Chinese natural language queries and data tables and columns in the database schema; Dependencies between natural language query tokens; The relationship between the segmentation of the current natural language query and the segmentation of historical queries;

[0014] 4. The method for constructing a Chinese natural language SQL statement generation model provided by this invention generates SQL statements in depth-first order using a syntax tree decoding method. The decoding process is divided into two categories: generating syntax keywords and generating database schemas. The possible candidate options are determined based on the current node type and the parent node type. Then, an LSTM is used to calculate the probability distribution, and the candidate with the highest probability value is selected. This decoding method conforms to the structure of SQL statements and the characteristics of structured data, avoids syntax errors, supports the generation of complex SQL statements including table joins and nested queries, and the model has high accuracy. Attached Figure Description

[0015] Figure 1 These are the specific technical steps for implementing this invention;

[0016] Figure 2 A flowchart illustrating the multilingual encoding module of the pre-screened cross encoder;

[0017] Figure 3 Example diagram of the steps for calculating the probability of database pattern items for a pre-screened cross encoder;

[0018] Figure 4 Examples of possible values ​​for k1 and k2;

[0019] Figure 5 A diagram illustrating natural language queries and database schema item links;

[0020] Figure 6 This is a diagram illustrating the coreference relationships between rounds of natural language queries.

[0021] Figure 7 A flowchart illustrating the process of encoding the optimal pattern item to obtain the relation-aware encoding vector;

[0022] Figure 8 This is a schematic diagram of the SQL decoding process based on the SQL syntax tree. Detailed Implementation

[0023] To facilitate understanding of the technical content of this invention by those skilled in the art, the following description, in conjunction with the accompanying drawings, further illustrates the invention.

[0024] like Figure 1 As shown, the method of the present invention includes the following specific steps:

[0025] S1. Pre-screening cross-compilation model: This model selects the top k1 database tables and k2 database columns that strongly match the natural language description. k1 and k2 are two important hyperparameters that need to be manually set. Setting them too large may include many unnecessary pattern terms, while setting them too small may exclude some important pattern terms. To avoid the problem of large-scale databases not being fully input into the model in real-world applications, a cross-encoder is designed to take the natural language query and database pattern information as sequential input. The pre-screening cross-compilation model includes: a data preprocessing module, an encoding module, a column augmentation table embedding module, and a loss calculation module.

[0026] Specifically, it includes the following steps:

[0027] S11. The data preprocessing module includes preprocessing natural language and database schemas. User-inputted Chinese natural language may contain colloquial and non-standard expressions; for example, 2022 might be expressed as "last year," "22 years," or "two thousand two years." An amount of 100,000 might be expressed as "ten thousand," but the corresponding numerical values ​​in natural language are represented as standardized, precise values ​​in the database. Therefore, it is necessary to uniformly process numerical data types such as time, currency, and percentages to represent them as precise values. The database schema preprocessing involves using semantic names to unify the processing of database tables and columns. For example, the id column in the airports database table represents the unique identifier of an airport, but the model cannot identify its true meaning or whether it is a column associated with the natural language query. Therefore, the database schema uses semantic names (airport id) to make it closer to the true semantics, facilitating subsequent filtering.

[0028] Specifically, semantic names are used to replace database schema items, which can better identify the true meaning of database items during filtering and matching. The similarity between Chinese natural language and semantic representation of database items is calculated, and schema items are classified. The specific approach is to traverse the data tables and specific column names in the database. For data tables, comments are used to restore the true semantic names of the data tables, and for data columns, the information of the data tables and column comments are used to restore the true semantic names.

[0029] S12, Encoding Module: Encodes the preprocessed natural language queries and database pattern items into vectors. The vectors contain Chinese natural language queries, historical queries, and English database pattern representations. The concatenated input is encoded using the XLM-R (Cross-lingual Language Model RoBERTa) cross-lingual pre-trained model.

[0030] The specific implementation steps involve representing N database tables in a database as T = {t1, t2, ..., tn}. N Let C represent all columns according to the table they belong to, which represents the set of all tables.

[0031]

[0032] For database columns, Represents the i-th table t i The vector representation of the j-th column, with the column type prefixed to the column name. This indicates the data type of the j-th column in the i-th table, whether it is numeric or character. Then, the unstructured natural language query, historical queries, and the database schema represented using semantic names are concatenated into a fused feature representation. Using | as the delimiter. Further, the normalized fusion representation X is obtained. contact This serves as the input sequence for the PLM (Pre-trained Language Model). Because the input sequence contains both Chinese natural language queries and database schema items in English, this example uses the Cross-lingual Language Model RoBERTa (XLM-R) for encoding, obtaining semantic encoding vectors for the semantic names of the Chinese natural language queries and the database table and column names in the database schema. The PLM will divide schema items into multiple token identifiers (e.g., encoding "source airport" as two words, "source" and "airport"), but when used as a filter column condition, it is desirable to integrate all schema item information into a single encoding vector.

[0033] Therefore, the example uses a BiLSTM layer to obtain the encoded representation of the hidden state of the segmented pattern items. After pooling, each pattern item is represented as a whole embedding. The pooling operation aggregates the hidden state into a fixed-size representation of each pattern item. A non-linear fully connected layer is added on top of the pooled representation so that the pattern items can capture complex relationships and patterns in the data. After the above pooling operation, the table embedding representation is... Each column embedding is represented as d represents the size of the hidden layer. This represents an embedding dimension of 1×d. Detailed steps are as follows: Figure 2 As shown.

[0034] S13, Column Information Enhanced Table Embedding Module: When only a column name in a data table is explicitly mentioned in natural language without mentioning the table name, the column information is injected into the corresponding table encoding. Even when only information related to the column name is mentioned in natural language, the corresponding table can still be identified. Column information is injected into the table embedding by stacking multi-head scaling dot product attention layers, and a feature fusion layer is added to obtain the column information enhanced table embedding.

[0035] Specifically, for example in Figure 3 As described in the example, the natural language only mentions "Beijing" without mentioning the table name "airports" to which the column belongs. In this case, the "airports" table may be missed when filtering table names. Therefore, this invention proposes a method to inject column information into the table embedding, using a multi-head attention mechanism to integrate the column embedding information from the table into the table embedding. Where T i As the query matrix with shape (1xd), C : i The matrix, which serves as both key and value, is also of size (n).i ×d), n i Let be the number of columns, and h represent the number of heads. By using a different randomly initialized Q / K / V weight matrix for each head, the model focuses on different positions. The attention weights of the table embeddings for each column are calculated separately, and then a weighted sum is used to obtain the table embedding sensitive to column information obtained through multi-head attention computation. Then, a normalization function is used to obtain the column-enhanced table embedding.

[0036] S14. The loss calculation module can actually be viewed as a natural language problem and a table similarity task. Therefore, the final calculation is a loss function for a classification task. However, because the SQL statement contains fewer database tables and columns, the negative samples in the classification task are many times more than the positive samples, resulting in an imbalanced sample structure. Therefore, the focal loss function is used as the classification loss function. The loss function of the pre-selected cross-encoder is formed in a multi-task learning manner, which consists of table classification loss and column classification loss.

[0037] The detailed implementation steps involve using two different MLP (Multi-Layer Perception) modules to perform classification tasks on the enhanced table embeddings and column embeddings respectively, and calculating the relevance probability to natural language problems for each.

[0038]

[0039]

[0040] All are trainable parameters, and σ(·) represents softmax. In practical applications, the SQL queries users need to write typically only include a few tables in the database. Therefore, when filtering data tables and columns, there will be many negative samples. To address the problem of imbalanced samples, this invention uses a focus loss function. Using a task-based learning approach, we calculate the loss functions for data table classification and data column classification:

[0041]

[0042] N represents all tables, n i This represents the i-th column in the database. This represents all columns, while y i The true label y of the i-th table i =1 indicates that the table is included in the corresponding SQL statement; otherwise, it is 0. This represents the true label of the k-th column in the i-th table. If it is zero, it means that the corresponding SQL statement involves that column; otherwise, it is zero.

[0043] Finally, we calculate the relevance probabilities of natural language for different tables and columns. In the example, we filter the k1 tables with the highest probabilities and the k2 columns with the highest probabilities. k1 and k2 are two crucial hyperparameters; we need to choose appropriate values ​​for k1 and k2 to ensure high recall while preventing the introduction of excessive noise. We then restore the reordered database schema items to their original names. The subsequent SQL generation module uses these original names for linking, and the resulting optimal schema items serve as input for the subsequent SQL generation module. The steps are as follows... Figure 3 As shown.

[0044] For example, to query the sample question "What is the flight number departing from Beijing?", the tables and columns in the database are as follows: Figure 4 As shown, the parentheses represent the semantic names of the pattern items. The database tables (airlines, aiports, flights) have relevance scores of (0.02, 0.93, 0.95) respectively. The column similarity is as follows: Figure 4 As shown, the main columns involved in the SQL statement are (airports.city, aiports.airport_code, flghts.flight_number, flights.source_airport). To improve the performance of the filtering task, the values ​​of k1 and k2 are relatively high. Setting the relevant probability value to be greater than 80% of the given threshold is sufficient to use it as a relevant pattern item. The threshold can be set by the user according to the query requirements. Therefore, in this example, k1 is 2 and k2 is 4. Here, k1 and k2 are used for pre-filtering to extract the optimal pattern item. The actual pattern item involved in the SQL statement needs to be determined according to the subsequent coding part.

[0045] S2. The SQL encoding module integrating historical interaction information requires that when a user inputs a natural language query, the system prioritizes retrieving historical records that better match the intent of the current query from the historical query pool, generating the final result through multiple rounds of interaction. It encodes the relationships between database schema items and natural language, as well as between natural language and historical queries, including dependencies between schema item links and natural language queries. Existing research has shown that explicitly representing these relationships can improve the model's performance. After representing each relationship, encoding can be performed based on the relationship-aware graph. Specifically, it includes four modules: a natural language query and database schema encoding module, a schema item linking module, a historical interaction linking module, and a relationship-aware graph encoding module. Specifically, it includes the following sub-steps:

[0046] S21, Natural Language Query and Database Encoding Module:

[0047] This module can be run independently, or the output of module S1 can be used directly as the input of this module. Specifically: the pre-selected cross-encoder has obtained the initial encoding vector of natural language and database pattern items that incorporate the optimal pattern items and historical queries, encoded by the cross-language pre-trained encoder.

[0048]

[0049] Q i This represents the i-th historical query statement, where |Q i | represents the total number of word segments in the i-th historical natural language query. |k1| represents the vector representation of the j-th word in the i-th historical query natural language query, and |k1| represents the first k1 tables in the database schema that have been pre-filtered. Let represent the initial vector representation of the i-th table in the database. For a database column, This represents the vector representation of the j-th column in the i-th table, with the column type prefixed to the column name. Indicates the data type of the j-th column of the i-th table, either numeric or character. All It is the sum of the number of all columns, k2.

[0050] S22. Schema-based links primarily involve aligning links between database tables, columns, and natural language queries. These relationships include table-to-table, table-to-column, table-to-natural language terms, column-to-natural language terms, and column-to-column. The relationships between terms in the natural language query are represented as follows: The relationship between natural language queries and database schemas (including database columns and tables, and database values) is represented as follows: Relationships between database schema items, such as foreign key information, are represented as follows: The natural language and database schema are processed as nodes, and the links or relationships between natural language queries and database schema items are edges. Thus, the entire input constitutes an interaction graph, because... Syntactic dependencies encompass various relationships. For example, the SpaCy toolkit contains as many as 55 syntactic dependencies. Injecting all dependencies into the interaction heterogeneous graph may lead to overfitting. Therefore, syntactic dependencies are abstracted into three relationships: Forward, Backward, and None, which respectively represent dependencies on the word preceding the current word, dependencies on the word following the current word, and no dependency relationship. For example, in "Look, this cute kitten", "cat" is forward dependent on "cute", and "this" is backward dependent on "kitten".

[0051] More preferably, any two distinct vertices in the directed graph are denoted as vertex A and vertex B;

[0052] The relation represents the relationship between database schema items, including the relationship between database tables and columns. As shown in Table 1:

[0053] Table 1 Containment Relationship

[0054]

[0055] Furthermore, regarding the link between natural language queries and database schemas... like Figure 5 The diagram shows how to link "Beijing" and "flight number" in the natural language query to the "city" column in the `aiports` table and the "fightno" column in the `fights` table, respectively. Correctly linking the database schema items and the tokens in the natural language query is crucial for generating the SQL statement. An important value linking method is also used here: "Beijing" is a value in the "city" column, aligning with the values ​​in the schema mentioned in the question. Therefore... For the following relationships, exact matches in word segmentation and database patterns are represented by EM (exact match), while partial matches are represented by PM (partial match), as shown in Table 2:

[0056] Table 2 Containment Relationship

[0057]

[0058] The dependency relationships between natural language query tokens, particularly the syntactic dependencies of the query itself, have received little attention. While pre-trained models can handle these dependencies to some extent, experimental results show that explicitly labeling these relationships significantly improves SQL generation performance. This represents the dependencies between natural language query segments. To prevent overfitting due to an excessive number of relationships, the relationships are abstracted as follows: when both vertices A and B are natural language segments, Syntax-F indicates that A has a forward syntactic dependency on B, Syntax-R indicates that A has a backward syntactic dependency on B, and Syntax-None indicates that A and B have no syntactic dependency. The inclusion relationships are shown in Table 3:

[0059] Table 3 Containment Relationship

[0060]

[0061] Finally, the aforementioned This indicates the relationship between the segmentation of the current natural language query and the segmentation of historical queries. When a user inputs a natural language query, especially for queries involving multiple tables, it's difficult for the user to accurately describe their true query intent in a single, comprehensive query. Therefore, it's desirable for users to use a layered approach to generate more complex queries based on their past query history. This allows the model to better understand the user's true intent. For example, if a user wants to query "Which flights depart from Beijing to Shanghai, and which are operated by China Airlines?", the process of generating complex queries through multiple rounds of historical queries would be as follows: Figure 6 As shown:

[0062] The The relationships included are: vertex A is the segmentation of the current natural language query, vertex B is the segmentation of the historical query, Coref_Relation indicates that B and A have a coreference relationship, and Co_Relation indicates that the coreference relationship is regarded as a whole relationship, as shown in Table 4.

[0063] Table 4 contains the relationships in Co_Relation

[0064]

[0065] S24. Relationship-Aware Graph Encoding: Having defined a series of relationships, the next step is to enable the model to perceive these predefined relationships. The encoder initializes each edge of the preliminary heterogeneous graph constructed from the nodes and edges as a vector representation. Key relationships are then set by concatenating the features of all edges in the heterogeneous graph. Sum value relationship As shown in the formula

[0066]

[0067] i and j represent the i-th node and the j-th node respectively, Contact(·) represents the concatenation operation, p ij Representing a certain relationship, m is a trainable edge vector representation. ij `m` is a graph clipping variable. Based on the input natural language query, each edge in the heterogeneous graph is classified using a binary classification to record whether the edge should be retained. If it should not be retained, then `m` is used. ij Let the vector representation of this edge be a vector of the same dimension consisting of all zeros, where R is the total number of relation edges.

[0068] The constructed heterogeneous graph is encoded using RGAT (Relational graph attention transformers) as follows:

[0069]

[0070]

[0071]

[0072] RGAT consists of a multi-layered multi-head self-attention stack, with each layer containing a total of H heads, each head initialized with a different W. Q W K W V Focusing on different positions within a vector, a predefined relationship is added to the original self-attention mechanism, forming a new K matrix. The V matrix is W is obtained through linear transformations respectively. Q W K W V It sets a randomly initialized coefficient matrix in each head, x i x j The input sequence X init The i-th and j-th words in the text, This represents the attention score calculated for the i-th word with respect to the j-th word. This is the attention value of the i-th word to the j-th word. Finally... The attention result of the h-th attention head, after passing through H heads, is composed of multiple independent attention values ​​concatenated and multiplied by W. o The parameter matrix yields the final multi-head attention value.

[0073]

[0074] Afterwards, the first summation and normalization unit adds the multi-head attention value of the i-th word segmentation result to the composite semantic encoding vector of the i-th word segmentation result, and then normalizes it to obtain the result. The input is fed into a feedforward neural network for processing; then... After passing through a linear layer, the product is processed by the ReLU activation function, then through another linear layer to obtain the output, which is then combined with the result obtained from the first summation and normalization unit. Add them together, specifically: Each word segment is processed as described above to generate y. i y for all word segmentations i The vectors are used to construct a matrix to obtain the final relational code X. encode The overall process is as follows Figure 7 As shown.

[0075] S3, SQL decoding module

[0076] SQL decoding is achieved using the relational encoding tensor X.encode A depth-first SQL decoding model based on a syntax tree is proposed, providing a complete SQL structure prediction model. The syntax tree structure is characterized by generating a sequence of nodes in depth-first order, starting from the root node, according to SQL syntax rules. It integrates features from the previous time step, parent node features, context features, and node type features to maximize the probability of the correct SQL statement corresponding to the natural language problem. First, an abstract SQL syntax tree is generated, and then a series of decoder actions are output through an LSTM network. The formula for updating the LSTM state of the tree-structured decoder is:

[0077]

[0078] Where m t It is the LSTM cell state, h t It is the output of the LSTM at step t. This is the output of the parent node of the current node. t-1 It is an embedded representation of the previous action (the actions include APPLYRULE, SELECTTABLE, and SELECTCOLUMN). This represents the behavior of the parent node of the current node, z. t It is a context representation, p t It is the parent node of the current node in the syntax tree. This is the embedding representation of the current node type. The model's goal is to maximize the probability of predicting the correct SQL statement, i.e., Prob represents the conditional probability, given q. pre ,X encode Calculate a under the premise t The probability, a pre This represents all previous predicted behavior sequences.

[0079]

[0080] It includes the following steps:

[0081] S31. Expanding Non-Leaf Nodes: When a generated node is a non-leaf node, it is expanded into a syntax rule, becoming an APPLYRULE. APPLYRULE means applying a syntax rule; the formula is as follows: where g(.) is a 2-layer MLP with Tanh as the activation function.

[0082] Prob(a t =APPLYRULE[R]|a pre ,X encode ) = softmax(g(h t ))

[0083] softmax(·) is a normalized exponential function;

[0084] S32. Generate leaf nodes. Since the generated nodes are leaf nodes, a table name or column name is selected from the database schema, namely SELECTTABLE and SELECTCOLUMN, respectively. The formula is the same: λ j For the weight vector, Embedded representation of tables

[0085]

[0086] For example, the following SQL statement can be generated:

[0087] SELECT COUNT(*)

[0088] FROM students

[0089] WHERE name....

[0090] The generation process is as follows Figure 8 As shown, nodes are generated starting from the root node of the syntax tree in a depth-first order. "students" is a table name, and "name" is a column name in that table. Figure 8 The action of generating SQL syntax keywords or the terminator is called APPLYRULE; the actions of generating table names and column names are called SELECTTABLE and SELECTCOLUMN, respectively. The possible types of successor nodes for each node are determined by the SQL syntax. The SQL decoding process is complete when all branches end with . At this point, the syntax tree is traversed in depth-first order to obtain a unique SQL statement, which is then passed to the database execution engine to return the SQL query results.

[0091] Those skilled in the art will recognize that the embodiments described herein are intended to help the reader understand the principles of the invention, and should be understood that the scope of protection of the invention is not limited to such specific statements and embodiments. Various modifications and variations can be made to the invention by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the invention should be included within the scope of the claims of the invention.

Claims

1. A method for generating SQL that injects optimal pattern items and historical interaction information, characterized in that, include: S1. A pre-filtering cross-encoder calculates the most relevant pattern terms to the natural language query based on the input natural language query and the corresponding database pattern terms. Step S1 specifically includes: S11. Normalize the numerical items in the input natural language query and replace the database schema items with semantic names; S12. The natural language query and database pattern items processed in step S11 are concatenated and input into the cross-language pre-trained model to obtain the encoded representation of the hidden state of the segmented pattern items. The encoded representation of the hidden state of the segmented pattern items is pooled using BiLSTM, and each pattern item is represented as a whole embedding. The pooling result is input into a non-linear fully connected layer to obtain table embedding and column embedding. S13. Column embedding information is injected into table embedding by stacking multi-head scaling dot product attention layers, and a feature fusion layer is added to obtain table embedding with enhanced column embedding information. S14. Two different multilayer perceptron modules are used to perform classification tasks on column embedding and enhanced table embedding respectively, and the relevance probabilities of natural language to different columns and tables are calculated. Based on the relevance probabilities of natural language to different columns and tables, the k1 tables with the highest probabilities and the k2 columns with the highest probabilities are selected to obtain the optimal pattern item. S2, the SQL encoding module integrating historical interaction information, constructs an interaction graph of natural language and historical queries based on the optimal pattern items output by the pre-filtered cross-encoder and the user's historical interaction information, and encodes the obtained interaction graph; step S2 specifically includes: S21. Obtain the initial encoding vectors for natural language and database pattern items based on the optimal pattern items and historical queries; S22. Natural language and database schema are processed as nodes, and the links or relationships between natural language queries and database schema items are processed as edges to construct a preliminary interaction graph. S23. Set key relationships by combining the features of all edges in the interaction graph. Sum value relationship S24. Encode the heterogeneous graph processed in step S23 using a relational graph attention transformer to obtain the final relational code X. encode ; S3. Decode the encoding result of step S2 based on the SQL structure prediction model; step S3 specifically includes the following sub-steps: S31. Starting from the root node, generate a node sequence in depth-first order. If the generated node is a non-leaf node, proceed to step S32. If the generated node is a leaf node, proceed to step S33. S32. Expand non-leaf nodes into syntax rules: Prob(a t =APPLYRULE[R]|a pre ,X encode )=softmax(g(h t )) Here, g(.) is a 2-layer multilayer perceptron module with Tanh as the activation function, APPLYRULE indicates the application of grammar rules, and a pre h represents all previous predicted behavior sequences. t It is the output of the LSTM at step t, a t This represents the embedded representation of the current node; S33. Selecting a table name or column name from a database schema is called SELECTTABLE and SELECTCOLUMN respectively, using the same formula: λ j For the weight vector, It is an embedded representation of a table. S34. When all branches end with 2. The SQL generation method for injecting optimal pattern items and historical interaction information according to claim 1, characterized in that, , generate the SQL abstract syntax tree; then decode the SQL abstract syntax tree through an LSTM network.

3. The SQL generation method for injecting optimal pattern items and historical interaction information according to claim 2, characterized in that, The link or relationship between the natural language query and the database schema items in step S22 specifically includes: the relationship between the words in the natural language query is represented as follows: The relationship between natural language queries and database schemas is represented as follows: The relationship between database schema items is represented as follows:

4. The SQL generation method for injecting optimal pattern items and historical interaction information according to claim 3, characterized in that, Syntactic dependencies include: depending on the word preceding the current word, depending on the word following the current word, and having no dependency.

5. The SQL generation method for injecting optimal pattern items and historical interaction information according to claim 4, characterized in that, Step S11, which involves replacing database schema items with semantic names, specifically involves: traversing the data tables in the database and the specific column names in the tables; using comments to restore the true semantic names of the data tables; and using the information of the data tables and column comments to restore the true semantic names of the data columns. The formula for updating the state of an LSTM network is: Where, m t It is the LSTM cell state, h t It is the output of the LSTM at step t. This is the output of the parent node of the current node, a t-1 It is an embedded representation of the previous action. z represents the behavior of the parent node of the current node. t It is a context representation, p t It is the parent node of the current node in the syntax tree. It is the embedded representation of the current node type.

Citation Information

Patent Citations

  • Tree model-based complex natural language query-to-SQL method

    CN113032418A

  • Method for generating SQL (Structured Query Language) statement from Chinese natural language and cross

    CN115576984A