Method, apparatus and equipment for generating query statements based on Text2SQL

By using a pre-defined SQL statement generation model to extract features and reconstruct database schemas for natural language problems, the problem of poor statement quality generated by existing Text2SQL models in complex scenarios is solved, and efficient and accurate SQL statement generation is achieved.

CN117520375BActive Publication Date: 2026-07-17SOUTH CENTRAL UNIVERSITY FOR NATIONALITIES +1

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
SOUTH CENTRAL UNIVERSITY FOR NATIONALITIES
Filing Date
2023-10-08
Publication Date
2026-07-17

Smart Images

  • Figure CN117520375B_ABST
    Figure CN117520375B_ABST
Patent Text Reader

Abstract

This invention relates to the field of data processing, and discloses a method, apparatus, and device for generating query statements based on Text2SQL. The method includes: acquiring an input natural language question and a related database; based on the related database, converting the natural language question using a preset SQL statement generation model to obtain a target SQL query statement. The preset SQL statement generation model includes an encoding module, a decoding module, and a pattern decomposition and evaluation module. The encoding module is used to acquire feature information of the natural language question, the pattern decomposition and evaluation module is used to determine a reconstruction pattern based on the feature information of the natural language question, and the decoding module is used to perform feature matching on the feature information based on the reconstruction pattern. Because the preset model in this invention determines the database reconstruction pattern by matching reconstruction patterns based on the features of the natural language statement, it achieves high efficiency in pattern reconstruction, thereby improving the generation speed and quality of query statements.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of data processing technology, and in particular to a method, apparatus and device for generating query statements based on Text2SQL. Background Technology

[0002] In the field of natural language processing, Text2SQL is an important task, playing a crucial role in the research of intelligent question answering. Its core task is to extract the user's query intent from natural language and convert it into SQL statements, enabling users to query information from a database using natural language. Text2SQL has a promising future; it can change traditional database query methods, making them easier to use and better meeting user needs.

[0003] However, current research, both domestically and internationally, primarily focuses on improving the matching accuracy of SQL clauses, neglecting the correctness of SQL syntax generation. Therefore, in practical applications, due to the complexity of real-world scenarios and the diversity of user needs, existing Text2SQL models suffer from poor generated statement quality and low efficiency when used for SQL statement generation.

[0004] The above content is only used to help understand the technical solution of the present invention and does not represent an admission that the above content is related technology. Summary of the Invention

[0005] The main objective of this invention is to provide a method, apparatus, and device for generating query statements based on Text2SQL, which aims to solve the technical problems that existing SQL statement generation models cannot meet the complex and diverse user needs in real-world scenarios, have low statement generation efficiency, and produce poor-quality SQL query statements.

[0006] To achieve the above objectives, this invention provides a method for generating query statements based on Text2SQL, the method comprising the following steps:

[0007] Obtain input natural language questions and related databases;

[0008] Based on the relevant database, the natural language problem is transformed using a preset SQL statement generation model to obtain the target SQL query statement. The preset SQL statement generation model includes an encoding module, a decoding module, and a pattern decomposition and evaluation module. The encoding module is used to obtain the feature information of the natural language problem. The pattern decomposition and evaluation module is used to determine the reconstruction pattern based on the feature information of the natural language problem. The decoding module is used to perform feature matching based on the reconstruction pattern.

[0009] Optionally, the step of converting the natural language problem based on the relevant database using a preset SQL statement generation model to obtain the target SQL query statement includes:

[0010] The relevant database and the natural language question are input into the encoding module to extract the semantic features of the natural language question;

[0011] The schema decomposition and evaluation module performs database inverse normalization reconstruction on the relevant databases to obtain a set of database schemas and determine the database schema information.

[0012] The decoding module combines the database pattern information with the semantic features of the natural language question to generate a matching template, and obtains the target SQL query statement based on the matching template.

[0013] Optionally, the schema decomposition and evaluation module includes: a denormalization submodule and a first reconstruction submodule. The step of performing database denormalization reconstruction on the relevant database through the schema decomposition and evaluation module to obtain a database schema set and determine database schema information includes:

[0014] The foreign key relationships of the relevant databases are extracted through the denormalization submodule, the relevant databases are grouped into tables according to the foreign key relationships, and the tables in each group are denormalized to obtain the updated tables.

[0015] The updated tables are connected sequentially by the first reconstruction submodule to obtain the reconstructed related databases. Combined with the related database schemas before reconstruction, a set of database schemas and database schema information are obtained.

[0016] Optionally, the schema decomposition and evaluation module further includes: a second reconstruction submodule, which, after the step of successively connecting the updated tables through the first reconstruction submodule to obtain the reconstructed related databases, and combining the related database schemas before reconstruction to obtain the database schema set and database schema information, further includes:

[0017] The database pattern set is decomposed by the second reconstruction submodule to obtain a preset number of database pattern groups. A corresponding normalization standard is generated based on the database pattern information. Based on the corresponding normalization standard, each database pattern group is decomposed into a sub-pattern containing independent semantics. The sub-patterns containing independent semantics are connected to obtain a standard database pattern.

[0018] Accordingly, the step of combining the database pattern information with the semantic features of the natural language question through the decoding module to generate a matching template, and obtaining the target SQL query statement based on the matching template, includes:

[0019] The decoding module combines the standard database pattern with the semantic features of the natural language problem to generate a matching template, and obtains the target SQL query statement based on the matching template.

[0020] Optionally, the step of combining the standard database pattern with the semantic features of the natural language problem through the decoding module to generate a matching template, and obtaining the target SQL query statement based on the matching template, includes:

[0021] The decoding module uses a feature-based matching method to match the features in the standard database pattern with the semantic features of the natural language problem. Based on the matching results, a standard grammar template is determined, and a target SQL query statement is generated based on the standard grammar template.

[0022] Optionally, the step of inputting the relevant database and the natural language question into the encoding module to extract the semantic features of the natural language question includes:

[0023] The encoding module transforms the natural language problem into a vector sequence.

[0024] The encoding module extracts features from the transformed vector sequence based on an attention mechanism to obtain the semantic features of the natural language problem.

[0025] Optionally, the second reconstruction submodule is built based on the BERT model. After the step of successively connecting the updated tables through the first reconstruction submodule to obtain the reconstructed related databases, and combining the related database schemas before reconstruction to obtain the database schema set and database schema information, the module further includes:

[0026] The second reconstruction submodule takes the database schema set and the database schema information as input, and uses a preset schema decomposition algorithm combined with the original BERT model to determine the standard database schema for output based on the fine-grained decomposition.

[0027] Furthermore, to achieve the above objectives, the present invention also proposes a query statement generation device based on Text2SQL, the device comprising:

[0028] The data acquisition module is used to acquire the input natural language question and related databases;

[0029] The statement generation module is used to transform the natural language problem based on the relevant database using a preset SQL statement generation model to obtain a target SQL query statement. The preset SQL statement generation model includes an encoding module, a decoding module, and a pattern decomposition and evaluation module. The encoding module is used to obtain the feature information of the natural language problem. The pattern decomposition and evaluation module is used to determine the reconstruction pattern based on the feature information of the natural language problem. The decoding module is used to perform feature matching based on the reconstruction pattern and the feature information.

[0030] Furthermore, to achieve the above objectives, the present invention also proposes a Text2SQL-based query statement generation device, the device comprising: a memory, a processor, and a Text2SQL-based query statement generation program stored in the memory and executable on the processor, the Text2SQL-based query statement generation program being configured to implement the steps of the Text2SQL-based query statement generation method described above.

[0031] This invention first acquires the input natural language question and a related database. Then, based on the related database, it transforms the natural language question using a preset SQL statement generation model to obtain a target SQL query statement. The preset SQL statement generation model includes an encoding module, a decoding module, and a pattern decomposition and evaluation module. The encoding module acquires the feature information of the natural language question, the pattern decomposition and evaluation module determines a reconstruction pattern based on the feature information, and the decoding module performs feature matching on the feature information based on the reconstruction pattern. Because the preset SQL statement generation model in this invention extracts features from the input natural language statement and matches them with features in the reconstruction pattern, determining the reconstruction pattern of the related database based on the matching results, it achieves high efficiency in pattern reconstruction. By linking the natural language statement with the database reconstruction pattern, it improves the generation speed and quality of the SQL query statement. Attached Figure Description

[0032] Figure 1 This is a schematic diagram of the structure of a Text2SQL-based query statement generation device in the hardware operating environment involved in the embodiments of the present invention;

[0033] Figure 2 This is a flowchart illustrating the first embodiment of the Text2SQL-based query statement generation method of the present invention;

[0034] Figure 3 This is a flowchart illustrating the second embodiment of the Text2SQL-based query statement generation method of the present invention;

[0035] Figure 4This is a diagram illustrating the denormalized database schema in the second embodiment of the Text2SQL-based query statement generation method of the present invention;

[0036] Figure 5 This is a flowchart illustrating the third embodiment of the Text2SQL-based query statement generation method of the present invention;

[0037] Figure 6 This is a database schema link-aware representation graph in the third embodiment of the Text2SQL-based query statement generation method of the present invention;

[0038] Figure 7 This is a structural block diagram of the first embodiment of the Text2SQL-based query statement generation device of the present invention.

[0039] The realization of the objective, functional features and advantages of the present invention will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation

[0040] It should be understood that the specific embodiments described herein are for illustrative purposes only and are not intended to limit the scope of the invention.

[0041] Reference Figure 1 , Figure 1 This is a schematic diagram of the device structure for generating query statements based on Text2SQL in the hardware operating environment involved in the embodiments of the present invention.

[0042] like Figure 1 As shown, the Text2SQL-based query generation device may include: a processor 1001, such as a central processing unit (CPU), a communication bus 1002, a user interface 1003, a network interface 1004, and a memory 1005. The communication bus 1002 is used to establish communication between these components. The user interface 1003 may include a display screen or an input unit such as a keyboard; optionally, the user interface 1003 may also include a standard wired interface or a wireless interface. The network interface 1004 may optionally include a standard wired interface or a wireless interface (such as a Wireless-Fidelity (Wi-Fi) interface). The memory 1005 may be high-speed random access memory (RAM) or stable non-volatile memory (NVM), such as a disk drive. The memory 1005 may also optionally be a storage device independent of the aforementioned processor 1001.

[0043] Those skilled in the art will understand that Figure 1The structure shown does not constitute a limitation on the Text2SQL-based query generation device and may include more or fewer components than shown, or combine certain components, or have different component arrangements.

[0044] like Figure 1 As shown, the memory 1005, which serves as a storage medium, may include an operating system, a network communication module, a user interface module, and a query statement generation program based on Text2SQL.

[0045] exist Figure 1 In the Text2SQL-based query statement generation device shown, the network interface 1004 is mainly used for data communication with the network server; the user interface 1003 is mainly used for data interaction with the user; the processor 1001 and memory 1005 in the Text2SQL-based query statement generation device of the present invention can be set in the Text2SQL-based query statement generation device, and the Text2SQL-based query statement generation device calls the Text2SQL-based query statement generation program stored in the memory 1005 through the processor 1001 and executes the Text2SQL-based query statement generation method provided in the embodiment of the present invention.

[0046] This invention provides a method for generating query statements based on Text2SQL, referring to... Figure 2 , Figure 2 This is a flowchart illustrating the first embodiment of the Text2SQL-based query statement generation method of the present invention.

[0047] In this embodiment, the method for generating query statements based on Text2SQL includes the following steps:

[0048] Step S10: Obtain the input natural language question and related database.

[0049] It should be noted that the executing entity of the method in this embodiment can be a computing service device with image acquisition, data processing, model calling, network communication, and program execution functions, such as a mobile phone or personal computer. It can also be other electronic devices capable of implementing the Text2SQL-based query statement generation method, and this embodiment does not impose any limitations on this. Here, a Text2SQL-based query statement generation device (hereinafter referred to as the generation device) is selected as an example to illustrate various embodiments of the Text2SQL-based query statement generation method of the present invention.

[0050] Understandably, the natural language question can be a query or query command entered by the user. The natural language question can be a single natural language statement (Query), such as "List all singer names in concerts in year 2014".

[0051] It should be noted that the relevant database can be the database that needs to be queried for this natural language problem. The database schema and database content of the relevant database can be obtained. The database schema can include database information such as table names, column names, primary key tags, and foreign key relationships.

[0052] Step S20: Based on the relevant database, the natural language problem is transformed using a preset SQL statement generation model to obtain the target SQL query statement.

[0053] It should be noted that the preset SQL statement generation model includes an encoding module, a decoding module, and a pattern decomposition and evaluation module. The encoding module is used to obtain the feature information of the natural language problem, the pattern decomposition and evaluation module is used to determine the reconstruction pattern based on the feature information of the natural language problem, and the decoding module is used to perform feature matching on the feature information based on the reconstruction pattern.

[0054] Understandably, this pre-defined SQL statement generation model could be based on denormalization. Denormalization is a technique for optimizing and reconstructing database schemas. By extracting features from the user's natural language input and mapping them to the denormalized database schema, the syntactic accuracy of the generated SQL statements can be improved.

[0055] It should be understood that the preset SQL statement generation model can also be built based on the "encode-decode" framework. The "encode-decode" architecture is a commonly used framework for building Text2SQL models. The basic idea of ​​this framework is to encode and decode the natural language problem input and the target output respectively, thereby realizing the process of natural language understanding and generation.

[0056] It should be noted that the encoding module can transform the input natural language question and database pattern information into feature vectors. The pattern decomposition module and evaluation module can match the features of the natural language question with the features of the database pattern after inverse normalization reconstruction to determine the reconstruction pattern of the database. The decoding module can transform the extracted feature information of the natural language question based on the reconstruction pattern to obtain the SQL query statement and the corresponding query information.

[0057] Further, step S20 includes:

[0058] Step S201: Input the relevant database and the natural language question into the encoding module to extract the semantic features of the natural language question.

[0059] In a specific implementation, the natural language problem can be transformed into a vector sequence through the encoding module, and the semantic features of the natural language problem can be obtained by extracting features from the transformed vector sequence based on the attention mechanism through the encoding module.

[0060] Step S202: Perform database inverse normalization reconstruction on the relevant database through the schema decomposition and evaluation module to obtain a database schema set and determine the database schema information.

[0061] Understandably, denormalization can reconstruct the database schema, thereby reducing schema boundaries within the database and further generating queries by decomposing related queries. The reconstructed database schema can then be linked based on the model's "encode-decode" framework and extracted natural language feature information.

[0062] It should be noted that schema linking can be a Text2SQL method that generates SQL query statements based on the matching relationship between natural language questions and database schemas. Therefore, it can obtain relevant database information, organize the database metadata to form a database schema, and use inverse normalization techniques to reconstruct the database schema, obtaining a set of database schemas and determining the database schema information for subsequent matching based on the semantic features of natural language questions.

[0063] Step S203: The decoding module combines the database pattern information with the semantic features of the natural language question to generate a matching template, and obtains the target SQL query statement based on the matching template.

[0064] Understandably, upon obtaining the database schema and related database information after inverse normalization, candidate templates can be generated based on the schema information and keywords in natural language questions, such as defining rules for relationships between tables and querying data from tables. Candidate templates can then be filtered based on the semantic features of the natural language question to obtain matching templates, and finally, the target SQL query statement can be obtained based on the matching templates.

[0065] This embodiment first acquires the input natural language question and related database. Then, based on the related database, it transforms the natural language question using a preset SQL statement generation model to obtain the target SQL query statement. The preset SQL statement generation model includes an encoding module, a decoding module, and a pattern decomposition and evaluation module. The encoding module acquires the feature information of the natural language question, the pattern decomposition and evaluation module determines the reconstruction pattern based on the feature information, and the decoding module performs feature matching on the feature information based on the reconstruction pattern. Because this embodiment's preset SQL statement generation model extracts features from the input natural language statement, links the natural language statement and the database pattern, and introduces denormalization technology to optimize the database pattern, it improves pattern linking efficiency. By using a feature-based matching method to determine the reconstruction pattern, it achieves high efficiency in pattern reconstruction, thereby improving the generation speed and quality of the SQL statement.

[0066] refer to Figure 3 , Figure 3 This is a flowchart illustrating the second embodiment of the Text2SQL-based query statement generation method of the present invention.

[0067] Based on the first embodiment described above, in this embodiment, in order to perform denormalization schema reconstruction on the database to improve schema join efficiency and optimize various error problems in multi-table joins, the schema decomposition and evaluation module further includes: a denormalization submodule and a first reconstruction submodule. Step S202 includes:

[0068] Step S2021: Extract the foreign key associations of the relevant databases through the denormalization submodule, group the relevant databases into tables according to the foreign key associations, and perform denormalization operation on the tables in each group to obtain the updated tables.

[0069] Step S2022: The updated tables are connected sequentially through the first reconstruction submodule to obtain the reconstructed related databases, and combined with the related database schemas before reconstruction, a database schema set and database schema information are obtained.

[0070] Understandably, database denormalization schema reconstruction can involve grouping a database table that originally had a large number of foreign key relationships according to their relationships, and then connecting the new tables obtained from each group one by one to obtain the updated table.

[0071] It should be noted that database denormalization can improve database query efficiency and maintainability, effectively reduce database storage pressure, and enhance database performance and response speed.

[0072] refer to Figure 4 , Figure 4For an example of a denormalized database schema, in Figure 4 The relevant database contains four tables: concert, stadium, singer_in_concert, and singer. The database schema contains three sets of foreign keys: the singer table is connected to the singer_in_concert table via the foreign key Fk_singer_in_concert_singer; the singer_in_concert table is connected to the concert table via the foreign key Fk_singer_in_concert_concert; and the concert table is connected to the stadium table via the foreign key Fk_concert_stadium.

[0073] It should be noted that foreign key associations are an important indicator for denormalization order. Performing denormalized table joins according to the order of table names in the table join list yields five single tables, resulting in RN(singer_in_concert_concert), RN(singer_in_concert_singer), RN(concert_stadium), RN(singer_i-n_concert_concert_stadium), and RN(singer_singer_in_concert_concert_stadium). This leads to the reconstructed database schema, which, combined with the relevant database schemas before reconstructing, yields a set of database schemas and database schema information.

[0074] Furthermore, when performing multi-table joins, considering the instability of schema quality during denormalization schema reconstruction, a schema decomposition and evaluation optimization approach can be introduced to optimize the database schema selection. The schema decomposition and evaluation module further includes: a second reconstruction submodule, and after step S2022, it also includes:

[0075] Step S2023: The database schema set is decomposed by the second reconstruction submodule to obtain a preset number of database schema groups. A corresponding normalization standard is generated based on the database schema information. Each database schema group is decomposed into a sub-schema containing independent semantics based on the corresponding normalization standard. The sub-schemas containing independent semantics are connected to obtain a standard database schema.

[0076] It should be noted that, in order to optimize the role of denormalization, a Text2SQL model based on schema decomposition evaluation can be introduced, namely the Reversed Normalization with Schema Decomposition Evaluation (RNSDE) model. This model is based on an encoder-decoder architecture. RNSDE uses schema decomposition evaluation to find the optimal schema after denormalization, so as to enhance the table-schema alignment layer.

[0077] In practical implementation, Text2SQL tasks based on RNSDE can be formally represented as follows:

[0078]

[0079] Understandably, the input consists of a natural language question and a set of database schemas, while the output represents the target SQL query statement. The database schema set includes information such as table names, column names, primary key markers, and foreign key relationships. Furthermore, this Text2SQL task based on RNSDE focuses on maximizing task performance improvement and can be formally represented as:

[0080]

[0081] D L This is the denormalized database schema. The SQL statement generated after denormalization is RN, which is the denormalization process, and its formal representation is as follows:

[0082] D L =RN(D)

[0083] D L The denormalized database schema set is used to perform schema decomposition and evaluation on the denormalized database schema set, and the database schema with the highest availability is obtained, which is the determined standard database schema D. m Its formal expression is:

[0084] D m =SDE(D L )

[0085] It should be noted that the G function in the above formula is defined as follows:

[0086]

[0087] Among them, Z i Z' represents the comprehensive evaluation value after inverse normalization. iThis represents the comprehensive evaluation value before the denormalization process, where the function G represents the difference in precision between the SQL statement after denormalization and before denormalization. in The SQL statement generated after denormalization is formally represented as follows:

[0088]

[0089] when When, then Z i Take a positive value, when At that time, Z i Set to 0. EMA is the SQL statement accuracy evaluation function, where The provided standard SQL statement is defined as follows:

[0090]

[0091] when At this point, the denormalized SQL statement outputs correctly. At this point, the denormalized SQL statement will output an error.

[0092] It is understood that this embodiment aims to find the RNSDE model that maximizes the sum of any input function G in the above formula, that is, to optimize the quality of SQL query statement generation, and obtain the standard database schema in the process.

[0093] Accordingly, step S203 includes:

[0094] Step S203': The decoding module combines the standard database pattern with the semantic features of the natural language problem to generate a matching template, and obtains the target SQL query statement based on the matching template.

[0095] In a specific implementation, the decoding module uses a feature-based matching method to match the features in the standard database pattern with the semantic features of the natural language problem. Based on the matching results, a standard syntax template is determined, and a target SQL query statement is generated based on the standard syntax template.

[0096] This embodiment extracts foreign key associations from the relevant databases through the denormalization submodule, groups the relevant databases into tables based on these foreign key associations, and performs denormalization on the tables in each group to obtain updated tables. The first reconstruction submodule then sequentially joins the updated tables to obtain reconstructed relevant databases. Combined with the original database schema, a database schema set and database schema information are obtained. Because this embodiment can reconstruct the database schema using denormalization, it reduces schema boundaries in the database. Furthermore, it uses decomposition of join queries to generate queries, reducing the need for join operations, the number of indexes and foreign keys in the database schema, and accelerating the generation of target SQL query statements. Addressing the issue of unstable schema quality during schema optimization and reconstruction based on the denormalization model, it optimizes denormalized schema links through schema decomposition and evaluation, linking natural language statements with the database reconstruction schema. Focusing on efficient schema reconstruction, it is named RNSDE, which can further improve the generation speed and optimize the quality of generated SQL statements.

[0097] refer to Figure 5 , Figure 5 This is a flowchart illustrating the third embodiment of the Text2SQL-based query statement generation method of the present invention.

[0098] Furthermore, after step S2022, the following may also be included:

[0099] Step S2023': The second reconstruction submodule takes the database schema set and the database schema information as input, and uses a preset schema decomposition algorithm combined with the original BERT model to determine the standard database schema for output based on the fine-grained decomposition.

[0100] It should be noted that, since the redundant information generated in the database schema set obtained by denormalization has a significant impact on the query results, and the data duplication and data errors caused by excessive redundancy interfere with the data query, in order to optimize the database schema selection, a schema decomposition evaluation can be performed on the database schema set generated by denormalization. The second reconstruction submodule can adopt a database schema decomposition algorithm. The core of the database schema decomposition algorithm is to decompose the database schema based on methods such as role decomposition, aggregation decomposition, and commonality decomposition. The choice of this schema decomposition algorithm can be based on a bidirectional encoding (Bidirectional Encoder Representation from Transformers, BERT) model proposed by Google Research.

[0101] Understandably, the BERT model uses a word-piece-based encoding method, dividing the entire text into multiple word pieces. Then, BERT adds a special marker [CLS] to the beginning of the text to indicate the classification result of the entire text. Each word piece is then processed by an embedding layer into a vector, called a word piece embedding. This process uses a deep neural network model with multiple embedding layers.

[0102] It's important to note that the most crucial part of the BERT model is the Multi-Head Self-Attention mechanism. This transforms the input text sequence into a vector sequence, with each vector representing a specific type of textual information. The vector sequence obtained through this attention mechanism is processed by multiple hidden layers to extract and learn higher-level features. The output of the last hidden layer is used for the classification task. For classification problems, a fully connected layer maps each text vector to a scalar output representing its classification score. For multi-class classification problems, a fully connected layer is also used, but the number of nodes in the output layer becomes the number of classes. Finally, BERT outputs the classification scores for all texts, selects the class with the highest score as the final prediction, and chooses the optimal mode decomposition algorithm.

[0103] In the specific implementation, you can refer to Figure 6 , Figure 6 This is a database schema link-aware representation graph generated by applying a predefined schema decomposition algorithm to the database schema set obtained after denormalization.

[0104] exist Figure 6 In the process, a set of candidate database schemas S{S1, S2, S3, ..., S} is generated. m Let θ represent the basic true pattern of the pattern link distribution, and let the estimated value be represented as... Natural language embedding representation W i , with pattern element S j The link is represented as p i,j The calculation method is as follows:

[0105]

[0106] Its loss function is defined as follows:

[0107]

[0108] Where, p i,j ∈θ is each pair of W i and S j The fundamental truth value of pattern perception. Pattern-aware representation is described by the following formula:

[0109]

[0110] Here, f is the reference mechanism. Its calculation method involves connecting W... i Embedding of pattern elements S j The calculation is as follows:

[0111] Understandably, in selecting the optimal pattern decomposition algorithm, the BERT model and pattern-aware learning method can select the optimal fine-grained auxiliary pattern generation to achieve better query results and obtain standard database patterns.

[0112] In this embodiment, the database pattern set and database pattern information are taken as input by the second reconstruction submodule. A preset pattern decomposition algorithm is used in combination with the original BERT model. Based on the fine-grained decomposition, the standard database pattern of the output is determined. Since the BERT model with multi-head self-attention mechanism is selected, it can extract and learn higher-level features during the common decomposition process, analyze the commonalities and differences in the data, group and classify the data, obtain the standard database pattern, and select the optimal fine-grained decomposition to assist in pattern generation through pattern-aware learning, thereby achieving better query results.

[0113] refer to Figure 7 , Figure 7 This is a structural block diagram of the first embodiment of the Text2SQL-based query statement generation device of the present invention.

[0114] like Figure 7 As shown, the Text2SQL-based query statement generation device of the present invention includes:

[0115] Data acquisition module 701 is used to acquire input natural language questions and related databases;

[0116] The statement generation module 702 is used to convert the natural language problem based on the relevant database and through a preset SQL statement generation model to obtain a target SQL query statement. The preset SQL statement generation model includes an encoding module, a decoding module, and a pattern decomposition and evaluation module. The encoding module is used to obtain the feature information of the natural language problem. The pattern decomposition and evaluation module is used to determine the reconstruction pattern based on the feature information of the natural language problem. The decoding module is used to perform feature matching based on the reconstruction pattern and the feature information.

[0117] This embodiment first acquires the input natural language question and related database. Then, based on the related database, it transforms the natural language question using a preset SQL statement generation model to obtain the target SQL query statement. The preset SQL statement generation model includes an encoding module, a decoding module, and a pattern decomposition and evaluation module. The encoding module acquires the feature information of the natural language question, the pattern decomposition and evaluation module determines the reconstruction pattern based on the feature information, and the decoding module performs feature matching on the feature information based on the reconstruction pattern. Because the preset SQL statement generation model in this embodiment extracts features from the input natural language statement and matches them with features in the reconstruction pattern, determining the reconstruction pattern of the related database based on the matching results, it achieves high efficiency in pattern reconstruction. By linking the natural language statement with the database reconstruction pattern, it improves the generation speed and quality of the SQL statement.

[0118] Based on the first embodiment of the Text2SQL-based query statement generation device of the present invention, a second embodiment of the Text2SQL-based query statement generation device of the present invention is proposed.

[0119] In this embodiment, the statement generation module 702 is used to input the relevant database and the natural language question into the encoding module, extract the semantic features of the natural language question; perform database inverse normalization reconstruction on the relevant database through the pattern decomposition and evaluation module to obtain a database pattern set and determine the database pattern information; and combine the database pattern information with the semantic features of the natural language question through the decoding module to generate a matching template, and obtain the target SQL query statement according to the matching template.

[0120] Furthermore, since the schema decomposition and evaluation module includes a denormalization submodule and a first reconstruction submodule, the statement generation module 702 is also used to extract foreign key associations of the relevant databases through the denormalization submodule, group the relevant databases into tables according to the foreign key associations, and perform denormalization operations on the tables in each group to obtain updated tables; the updated tables are then sequentially connected through the first reconstruction submodule to obtain reconstructed relevant databases, and combined with the relevant database schemas before reconstruction, a database schema set and database schema information are obtained.

[0121] Furthermore, since the pattern decomposition and evaluation module also includes a second reconstruction submodule, the statement generation module 702 is further configured to decompose the database pattern set through the second reconstruction submodule to obtain a preset number of database pattern groups, generate corresponding normalization standards based on the database pattern information, and decompose each database pattern group into sub-patterns containing independent semantics based on the corresponding normalization standards, connect each sub-pattern containing independent semantics to obtain a standard database pattern; and combine the standard database pattern with the semantic features of the natural language problem through the decoding module to generate a matching template, and obtain the target SQL query statement based on the matching template.

[0122] Furthermore, the statement generation module 702 is also used to match the features in the standard database pattern with the semantic features of the natural language problem by using the feature-based matching method through the decoding module, determine the standard syntax template according to the matching result, and generate the target SQL query statement according to the standard syntax template.

[0123] Furthermore, the statement generation module 702 is also used to convert the natural language question into a vector sequence through the encoding module; and to extract features from the converted vector sequence based on an attention mechanism through the encoding module to obtain the semantic features of the natural language question.

[0124] Furthermore, since the second reconstruction submodule is built based on the BERT model, the statement generation module 702 is also used to take the database schema set and the database schema information as input through the second reconstruction submodule, and use a preset schema decomposition algorithm combined with the original BERT model to determine the standard database schema for output based on the fine-grained decomposition.

[0125] Other embodiments or specific implementations of the Text2SQL-based query statement generation device of the present invention can be found in the above-described method embodiments, and will not be repeated here.

[0126] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or system that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or system. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or system that includes that element.

[0127] The sequence numbers of the above embodiments of the present invention are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.

[0128] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods of the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as read-only memory / random access memory, magnetic disk, optical disk) and includes several instructions to cause a terminal device (which may be a mobile phone, computer, server, air conditioner, or network device, etc.) to execute the methods described in the various embodiments of the present invention.

[0129] The above are merely preferred embodiments of the present invention and do not limit the patent scope of the present invention. Any equivalent structural or procedural transformations made based on the content of the present invention's specification and drawings, or direct or indirect applications in other related technical fields, are similarly included within the patent protection scope of the present invention.

Claims

1. A method for generating query statements based on Text2SQL, characterized in that, The method includes: Obtain input natural language questions and related databases; Based on the relevant database, the natural language problem is transformed using a preset SQL statement generation model to obtain the target SQL query statement. The preset SQL statement generation model includes an encoding module, a decoding module, and a pattern decomposition and evaluation module. The encoding module is used to obtain the feature information of the natural language problem. The pattern decomposition and evaluation module is used to determine the reconstruction pattern based on the feature information of the natural language problem. The decoding module is used to perform feature matching based on the reconstruction pattern and the feature information. The pattern decomposition and evaluation module includes: an inverse normalization submodule, a first reconstruction submodule, and a second reconstruction submodule, wherein the second reconstruction submodule is constructed based on the BERT model; The step of converting the natural language problem based on the relevant database and using a preset SQL statement generation model to obtain the target SQL query statement includes: The relevant database and the natural language question are input into the encoding module to extract the semantic features of the natural language question; The schema decomposition and evaluation module performs database inverse normalization on the relevant databases to obtain a set of database schemas and determine the database schema information, including: The foreign key relationships of the relevant databases are extracted through the denormalization submodule, the relevant databases are grouped into tables according to the foreign key relationships, and the tables in each group are denormalized to obtain the updated tables. The updated tables are connected sequentially through the first reconstruction submodule to obtain the reconstructed related databases. Combined with the related database schemas before reconstruction, the denormalized database schema set and database schema information are obtained. The second reconstruction submodule takes the denormalized database schema set and the database schema information as input. Based on the BERT original model, it uses a preset schema decomposition algorithm to estimate the link distribution of natural language embedding representations and schema elements, obtaining a link-aware representation. Based on the link-aware representation, it selects the optimal decomposition granularity and performs schema decomposition evaluation on the denormalized database schema set to obtain a standard database schema. Wherein, the input natural language question q... i Belonging to Q, calculate the difference between the precision accuracy of the denormalized SQL statement output by the preset SQL statement generation model and the precision accuracy of the SQL statement before denormalization output by the preset SQL statement generation model, and determine the database pattern that maximizes the sum of the differences as the standard database pattern. The decoding module combines the standard database pattern with the semantic features of the natural language problem to generate a matching template, and obtains the target SQL query statement based on the matching template.

2. The query statement generation method based on Text2SQL as described in claim 1, characterized in that, The step of combining the standard database pattern with the semantic features of the natural language problem through the decoding module to generate a matching template, and obtaining the target SQL query statement based on the matching template, includes: The decoding module uses a feature-based matching method to match the features in the standard database pattern with the semantic features of the natural language problem. Based on the matching results, a standard grammar template is determined, and a target SQL query statement is generated based on the standard grammar template.

3. The query statement generation method based on Text2SQL as described in claim 1, characterized in that, The step of inputting the relevant database and the natural language question into the encoding module to extract the semantic features of the natural language question includes: The encoding module transforms the natural language problem into a vector sequence. The encoding module extracts features from the transformed vector sequence based on an attention mechanism to obtain the semantic features of the natural language problem.

4. A query statement generation device based on Text2SQL, characterized in that, The device includes: The data acquisition module is used to acquire the input natural language question and related databases; The statement generation module is used to transform the natural language problem based on the relevant database using a preset SQL statement generation model to obtain a target SQL query statement. The preset SQL statement generation model includes an encoding module, a decoding module, and a pattern decomposition and evaluation module. The encoding module is used to obtain the feature information of the natural language problem. The pattern decomposition and evaluation module is used to determine the reconstruction pattern based on the feature information of the natural language problem. The decoding module is used to perform feature matching based on the reconstruction pattern and the feature information. The pattern decomposition and evaluation module includes an inverse normalization submodule, a first reconstruction submodule, and a second reconstruction submodule. The second reconstruction submodule is constructed based on the BERT model. The statement generation module is further configured to input the relevant database and the natural language question into the encoding module, extract the semantic features of the natural language question, and perform database inverse normalization reconstruction on the relevant database through the pattern decomposition and evaluation module to obtain a database pattern set and determine the database pattern information. The statement generation module is further configured to extract foreign key associations from the relevant databases through the denormalization submodule, group the relevant databases into tables based on the foreign key associations, and perform denormalization on the tables in each group to obtain updated tables; the updated tables are sequentially joined through the first reconstruction submodule to obtain reconstructed relevant databases, and combined with the relevant database schemas before reconstruction, to obtain a set of denormalized database schemas and database schema information; the set of denormalized database schemas and the database schema information are taken as input by the second reconstruction submodule, and based on the BERT original model, a preset schema decomposition algorithm is used to estimate the link distribution between the natural language embedding representation and the schema elements to obtain a link-aware representation; the optimal decomposition fineness is selected based on the link-aware representation, and the denormalized database schema set is evaluated by schema decomposition to obtain a standard database schema; wherein, based on the input natural language question q i Belonging to Q, calculate the difference between the precision accuracy of the denormalized SQL statement output by the preset SQL statement generation model and the precision accuracy of the SQL statement before denormalization output by the preset SQL statement generation model, and determine the database pattern that maximizes the sum of the differences as the standard database pattern. The statement generation module is further configured to combine the standard database pattern with the semantic features of the natural language problem through the decoding module to generate a matching template, and obtain the target SQL query statement based on the matching template.

5. A query statement generation device based on Text2SQL, characterized in that, The device includes: a memory, a processor, and a Text2SQL-based query statement generation program stored in the memory and executable on the processor, the Text2SQL-based query statement generation program being configured to implement the steps of the Text2SQL-based query statement generation method as described in any one of claims 1 to 3.