SQL statement generation method and device

By obtaining and fusion of semantic association information of historical SQL Q&A pairs, the problem of insufficient background information quality in existing Text2SQL technology is solved, and the generation of high-quality SQL statements is realized.

CN119917528AActive Publication Date: 2025-05-02ZHUO SHI TECH (HAINAN) CO LTD
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202510402766.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-01
Publication Date
2025-05-02
Estimated Expiration
2045-04-01

AI Technical Summary

Technical Problem

Due to the poor background information quality of the existing Text2SQL technology, the generated SQL statements are of poor quality.

Method used

By obtaining semantic association information of historical SQL Q&A pairs, integrating historical SQL Q&A pairs and business data related to target query, filtering and extending background data, and generating high-quality SQL statements.

Benefits of technology

Improve the quality of generated SQL statements, provide high-quality background data, and enhance the understanding and generation accuracy of the model.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119917528A_ABST
    Figure CN119917528A_ABST
Patent Text Reader

Abstract

The invention provides an SQL statement generation method and device, and relates to the technical field of artificial intelligence. The method comprises the steps of obtaining semantic association information between historical query and historical SQL statements; fusing a first historical SQL question and answer pair related to the target query and the first business data, and reasoning a preliminary SQL statement, a target business type and reasoning analysis data of the target query; and filtering the first business data and the semantic association information by using the target business type and the inference analysis data, expanding the first historical SQL question-answer pair and the business data by using the preliminary SQL statement, and finally fusing the filtered data and the expanded data to generate a target SQL statement corresponding to the target query. According to the method, the high-quality background data can be constructed through the semantic association information of the historical SQL question and answer pairs, the filtered and expanded service data and the SQL question and answer pairs, so that the quality of SQL statements can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a method and device for generating SQL statements. Background Art

[0002] Structured Query Language (SQL) is a language used for database query and programming. It can be used to efficiently access, update and manage database systems. As a key technology for natural language interaction with databases, Text2SQL technology aims to automatically convert natural language questions into corresponding SQL statements through natural language understanding and database information parsing, thereby achieving convenient database operations.

[0003] The current Text2SQL technology focuses on optimizing the prompt words of large language models, introducing relevant background information into the prompt words to assist in generating SQL statements. However, due to the poor quality of the background information, the quality of the SQL statements is poor. Summary of the invention

[0004] In view of the above problems, the object of the present invention is to provide a method and device for generating SQL statements, which can mine high-quality background information to improve the quality of generated SQL statements.

[0005] In order to solve the above technical problems, the present invention provides the following technical solutions:

[0006] In one aspect, the present invention provides a method for generating a SQL statement, comprising:

[0007] For each historical SQL question-answer pair, obtain semantic association information between the historical query and the historical SQL statement, wherein the semantic association information includes multiple sub-semantic association information and the historical business type corresponding to the historical query, and the semantic association information is obtained by analyzing the historical SQL question-answer pair using business data;

[0008] Integrate the first historical SQL question-answer pair and the first business data related to the target query, and infer a preliminary SQL statement corresponding to the target query, a target business type of the target query, and inference analysis data;

[0009] Using the target business type and the inference analysis data, filter the first business data and the semantic association information of all historical SQL question-answer pairs to obtain the second business data and the first semantic association information;

[0010] Using the target query to expand the first business data, and using the preliminary SQL statement to expand the first historical SQL question-answer pair, to obtain expanded business data and expanded SQL question-answer pairs;

[0011] The first semantic association information, the second business data, the extended business data, the extended SQL question-answer pair, the first historical SQL question-answer pair, and the preliminary SQL statement are integrated to generate a target SQL statement corresponding to the target query.

[0012] On the other hand, the present invention also provides a SQL statement generating device, comprising:

[0013] An acquisition module, for acquiring, for each historical SQL question-answer pair, semantic association information between a historical query and a historical SQL statement, wherein the semantic association information includes a plurality of sub-semantic association information and a historical business type corresponding to the historical query, and the semantic association information is obtained by analyzing the historical SQL question-answer pair using business data;

[0014] A preliminary generation module, used to fuse the first historical SQL question-answer pair and the first business data related to the target query, and infer the preliminary SQL statement corresponding to the target query, the target business type of the target query, and the inference analysis data;

[0015] A filtering module, used to filter the first business data and the semantic association information of all historical SQL question-answer pairs based on the target business type and the inference analysis data, to obtain the second business data and the first semantic association information;

[0016] An expansion module, configured to expand the first business data using the target query, and expand the first historical SQL question-answer pair using the preliminary SQL statement, to obtain expanded business data and expanded SQL question-answer pair;

[0017] The target generation module is used to fuse the first semantic association information, the second business data, the extended business data, the extended SQL question and answer pair, the first historical SQL question and answer pair and the preliminary SQL statement to generate a target SQL statement corresponding to the target query.

[0018] On the other hand, the present invention further provides an electronic device, comprising a processor and a memory, wherein the memory stores a plurality of instructions; the processor loads instructions from the memory to execute the steps in any one of the SQL statement generation methods provided by the present invention.

[0019] On the other hand, the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a plurality of instructions, wherein the instructions are suitable for being loaded by a processor to execute the steps in any one of the SQL statement generation methods provided by the present invention.

[0020] On the other hand, the present invention further provides a computer program product, including a computer program / instruction, which implements the steps in any one of the SQL statement generation methods provided by the present invention when executed by a processor.

[0021] The beneficial effects brought about by the technical solution provided by the present invention include at least:

[0022] In an embodiment of the present invention, semantic association information between historical queries and historical SQL statements established based on business data can be obtained to enhance the comprehension of the model, and preliminary SQL statements, target business types, and inference analysis data are preliminarily analyzed using the first historical SQL question-answer pair related to the target query and the first business data; the first business data and the semantic association information are filtered using the inferred target business type and inference analysis data to obtain more matching background data; in order to enrich the background data, the target business type and preliminary SQL statements are enabled for deep mining, and finally the final target SQL statements are generated by combining the mined data, filtered data, and preliminary SQL statements, which can provide high-quality background data for the model and effectively improve the quality of the generated SQL statements. BRIEF DESCRIPTION OF THE DRAWINGS

[0023] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.

[0024] Figure 1 It is a schematic diagram of an application scenario of the SQL statement generation method provided by an embodiment of the present invention;

[0025] Figure 2 It is a flowchart of a method for generating a SQL statement provided by an embodiment of the present invention;

[0026] Figure 3 is a schematic diagram of generating sub-semantic association information provided by an embodiment of the present invention;

[0027] Figure 4 It is a schematic diagram of obtaining a preliminary SQL statement provided by an embodiment of the present invention;

[0028] Figure 5 It is a schematic diagram of filtering, expanding and generating candidate SQL provided by an embodiment of the present invention;

[0029] Figure 6 It is a structural diagram of a SQL statement generating device provided by an embodiment of the present invention;

[0030] Figure 7 It is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0031] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.

[0032] The present invention provides a method and device for generating SQL statements, which can improve the quality of background data and further improve the quality of SQL statements.

[0033] It is understandable that in the specific implementation of the present invention, data related to users, such as query statements input by users, etc., need to obtain user permission or consent, and the collection, use and processing of relevant data need to comply with relevant laws, regulations and standards of relevant countries and regions.

[0034] See also Figure 1 , showing a schematic diagram of an application scenario of the SQL statement generation method. The application scenario may include a terminal 101 and a server 102, where data can be exchanged between the terminal 101 and the server 102 via a network, and a question-and-answer related application may be installed on the terminal 101. The terminal 101 may be a mobile phone, a tablet computer, a smart Bluetooth device, a computer, a large screen device, a robot, etc.; the server 102 may be a single server or a server cluster consisting of multiple servers.

[0035] The user can send the target query to the server 102 through the terminal 101. The server 102 can obtain the semantic association information between the historical query and the historical SQL statement for each historical SQL question-answer pair, wherein the semantic association information includes multiple sub-semantic association information and the historical business type corresponding to the historical query, and the semantic association information is obtained by analyzing the historical SQL question-answer pair using the business data; the first historical SQL question-answer pair and the first business data related to the target query are integrated to infer the preliminary SQL statement corresponding to the target query, the target business type of the target query, and the inference analysis data; the first business data and the semantic association information of all historical SQL question-answer pairs are filtered by the target business type and the inference analysis data to obtain the second business data and the first semantic association information; the first business data is expanded by using the target query, and the first historical SQL question-answer pair is expanded by using the preliminary SQL statement to obtain the expanded business data and the expanded SQL question-answer pair; the first semantic association information, the second business data, the expanded business data, the expanded SQL question-answer pair, the first historical SQL question-answer pair, and the preliminary SQL statement are integrated to generate the target SQL statement corresponding to the target query.

[0036] The server 102 may then send the target SQL statement to the terminal 101 so that the terminal 101 displays the target SQL statement to the user.

[0037] In this embodiment, a method for generating a SQL statement is provided, such as Figure 2 As shown, the specific process of the SQL statement generation method can be as follows:

[0038] S110. For each historical SQL question-answer pair, obtain semantic association information between the historical query and the historical SQL statement.

[0039] Historical SQL question-answer pairs refer to SQL question-answer pairs generated within a historical time period, and may include historical queries, historical SQL statements corresponding to historical queries, and historical business types corresponding to historical queries. The historical time period refers to the time period before the current time, for example, one week before the current time, one month before the current time, etc. The specific time period can be set according to actual needs and is not specifically limited here.

[0040] The historical SQL question-answer pairs may be in a specific business scenario, and the specific business scenario may also be consistent with the scenario of the target query for which SQL is actually generated. For example, if the target query is a user query in an e-commerce business, the historical SQL question-answer pairs are also in the e-commerce business.

[0041] The historical business type is obtained by classifying the historical queries. For example, e-commerce businesses may include maintenance and sales. By strongly associating the historical business type with the historical queries and the corresponding historical SQL statements, the historical SQL question-answer pair can be obtained.

[0042] SQL statements are database-oriented statements, while user queries are natural language-based statements. In order to query the corresponding data from the database to accurately answer user queries, the user queries can be converted into SQL statements based on their semantics. That is, the semantics between historical SQL statements and historical queries are related. For each historical SQL question-answer pair, the semantic association information between the historical query and the historical SQL statement can be obtained. The semantic association information can include multiple sub-semantic association information, as well as the historical business type corresponding to the historical query.

[0043] Among them, the semantic association information of each historical SQL question-answer pair can be pre-analyzed and stored in a specified location, and can be directly obtained from the specified location when needed. The same word may have different meanings in different business scenarios. In order to ensure the accuracy of the acquired semantic association information, the business data in a specific business scenario can be used to analyze the semantic association between the two. That is, the historical SQL question-answer pairs and business data in the embodiment of the present invention are all data in the same business scenario.

[0044] As an implementation method, the semantic association information between historical queries and historical SQL statements can be obtained through the following steps: for each historical SQL question-answer pair, obtain the historical database information corresponding to the historical query in the historical SQL question-answer pair; utilize the similarity between the historical query and the business data to determine candidate business data from the business data and associate it with the historical business type corresponding to the historical query to obtain the business data to be used; utilize the similarity between the historical query and other historical queries to determine a candidate SQL question-answer pair in the historical SQL question-answer pair; combine the historical database information, the business data to be used and the candidate SQL question-answer pair to guide the third reasoning model to think about the association between the historical query and the historical SQL statement to obtain semantic association information; divide the semantic association information into multiple sub-semantic association information, and associate the sub-semantic association information with the historical business type.

[0045] See also Figure 3, showing a schematic diagram of generating sub-semantic association information. In an embodiment of the present invention, the historical SQL question-answer pair includes historical queries, historical SQL statements, and historical business types. The business data may include data in a specific business scenario, for example, business nouns, business noun definitions, synonyms of business nouns, business calculation logic, and various types of text materials generated in a specific business scenario. Optionally, business nouns, business noun definitions, synonyms of business nouns, and business calculation logic can be extracted from the business data, pre-processed, and stored in a noun vector database for subsequent use. Similarly, for various types of text materials in the business data, the text materials can be sliced ​​and stored in a semantic vector database for subsequent use.

[0046] For each historical SQL question-answer pair, the correlation between the historical query and the historical SQL statement can be analyzed through the large language model, and the corresponding semantic association information can be summarized. Among them, the large language model can use a model with strong reasoning performance. As an implementation method, prompt words can be set, and then the prompt words can be used to guide the large language model analysis to obtain semantic association information.

[0047] It is understandable that in the process of converting historical queries into historical SQL statements, the database information corresponding to the database related to the historical query needs to be used. The database information may include data table information, foreign keys, etc. in the database, which is also called schema. For ease of description, the database information here is recorded as historical database information. The expression format of historical database information can adopt mainstream design ideas, for example:

[0048] "[DB name]

[0049]

table name

[0050] (col name, col type, whether primary key, sample: [col sample value sampling 1, col sample value sampling 1, col sample value sampling 1])

[0051] (col name, col type, whether primary key, sample: [col sample value sampling 1, col sample value sampling 1, col sample value sampling 1])

[0052] (col name, col type, whether primary key, sample: [col sample value sampling 1, col sample value sampling 1, col sample value sampling 1])

[0053]

table2 name

[0055] Foreign key

[0056] table1.col1=table2.col2

[0057] table1.[col2+col3+col4]=table2.[col1+col2+col3]

[0058] ...”

[0059] In order to obtain semantic association information more accurately, for each historical SQL question-answer pair, some data can be determined from business data and other historical SQL question-answer pairs to provide relevant background data for reasoning and analyzing semantic association information. Optionally, the similarity between historical queries and business data can be used to determine candidate business data.

[0060] As mentioned above, business data can be classified and stored in a noun vector database and a semantic vector database. Historical queries can be used to perform similarity matching in the noun vector database, and the first preset number of contents can be filtered out in order of similarity from large to small. Similarly, historical queries can be used to perform similarity matching in the semantic vector database, and the second preset number of contents with high ranking can be filtered out in order of similarity from large to small.

[0061] By merging the first preset number of contents and the second preset number of contents, candidate business data can be obtained. Since each historical SQL question-answer pair contains the historical task type corresponding to the historical query, for the candidate business data, the historical task type of the corresponding historical query can be associated with each candidate business data, thereby obtaining the business data to be used.

[0062] Other historical queries refer to historical queries in other historical SQL question-answer pairs, and other historical query SQL question-answer pairs refer to historical SQL question-answer pairs other than the current historical SQL question-answer pair. For example, there are historical SQL question-answer pairs QA1, QA2, and QA3. When analyzing the semantic association information corresponding to QA1, QA2 and QA3 are other historical SQL question-answer pairs; when analyzing the semantic association information corresponding to QA2, QA1 and QA3 are other historical SQL question-answer pairs.

[0063] The current historical query is matched with the historical queries in other historical SQL question-answer pairs in similarity, and a third preset number of historical SQL question-answer pairs are selected in descending order of similarity as candidate SQL question-answer pairs. Among them, the first preset number, the second preset number, and the third preset number in the embodiment of the present invention can be set to the same or different specific values, which can be set according to actual needs.

[0064] Combining historical database information, business data to be used, and candidate SQL question-answer pairs, a third prompt word can be constructed so as to use the third prompt word to guide the third reasoning model to analyze semantic association information. The third prompt word can be obtained by filling the third template with content. The third template is a prompt word template for analyzing semantic association information, which may include table creation information slots, business data slots, question slots, and SQL statement slots. For example, the third template may be:

[0065] "

Task

[0066] Carefully analyze the [problem], combine [table creation information] and [business information], think about the relevance between [SQL] and [problem], and summarize your reply. Your reply format is ```ref\n1, (related point content)\n2, (related point content)\n...

[0067]

Table creation information

[0068] {schema_1}

[0069]

Business Information

[0070] {knowledge_1}

[0071]

question

[0072] {question_1}

[0073]

SQL

[0074] {sql_1}”

[0075] Among them, {schema_1} can be used to fill in the historical database information corresponding to the historical query; {knowledge_1} can be used to fill in the business data to be used and the candidate SQL question-answer pairs; {question_1} can be used to fill in the historical query; {sql_1} can be used to fill in the historical SQL statement corresponding to {question_1}. After the data is filled, the third prompt word can be obtained. The third prompt word is input into the third reasoning model, and the third reasoning model can analyze the historical query, and analyze the correlation between the historical query and the historical SQL statement in combination with the historical database information, the business data to be used, and the candidate historical SQL question-answer pairs, and output semantic correlation information.

[0076] The semantic association information output by the third reasoning model can be divided into multiple sub-semantic association information according to the format, and then the sub-semantic association information can be associated with the historical task type corresponding to the historical query. All historical SQL question-answer pairs are processed in the above manner and stored in the designated vector database for subsequent use. In the above manner, a designated vector database of cross-database semantic associations between historical SQL question-answer pairs and business data can be constructed, which can provide highly correlated knowledge for the subsequent generation of SQL statements, improve the comprehension of the model, and thus improve the accuracy of SQL statement generation.

[0077] S120, integrating the first historical SQL question-answer pair and the first business data related to the target query, and inferring a preliminary SQL statement corresponding to the target query, a target business type of the target query, and inference analysis data.

[0078] The target query is the text for which the SQL statement currently needs to be generated. The target query can be actively input by the user or passed in by other programs. The first historical SQL question-answer pair refers to a historical SQL question-answer pair similar to the target query, which can be obtained by the similarity between the target query and the historical queries in each historical SQL question-answer pair. The first business data refers to business data similar to the target query, which can be obtained by the similarity between the target query and each business data. It should be noted that the similarities involved in the embodiments of the present invention can all be conventional cosine similarities.

[0079] In combination with the first historical SQL question-answer pair and the first business data, the target query can be analyzed by the large language model to analyze the preliminary SQL statement of the target query and the target business type. Among them, the large language model has its corresponding thinking process, and the reasoning analysis data refers to the thinking process data of the large language model to obtain the preliminary SQL statement and the target business type.

[0080] See also Figure 4 , showing a schematic diagram of obtaining a preliminary SQL statement. Optionally, when inferring the preliminary SQL statement corresponding to the target query, the target business type and the inference analysis data, the first business data can be determined from the business data according to the similarity between the target query and each business data; the first historical SQL question-answer pair can be determined from the historical SQL question-answer pairs according to the similarity between the target query and the historical query in each historical SQL question-answer pair; the first business data, the first historical SQL question-answer pair and the target database information are filled into the first template to generate a first prompt word; the first prompt word is used to guide the first inference model to analyze and infer the target query to generate a preliminary SQL statement and the target business data of the target query; and the process data generated by the first inference model in the analysis and inference is obtained as the inference analysis data.

[0081] As described in the above embodiments, business data can be classified and stored in a noun vector database and a semantic vector database. The target query is encoded as a target vector, and the similarity between the target vector and each data in the noun vector database is calculated, and then they are sorted in order from large to small according to the similarity, and the top fourth preset number of data are extracted. Similarly, the similarity between the target vector and each data in the semantic vector database is calculated, and then the top fifth preset number of data are extracted after being sorted in order from large to small according to the similarity. The fourth preset number of data and the fifth preset number of data are merged in the form of text splicing, and a preset splicing symbol is used during splicing to obtain the first business data. Among them, the fourth preset number and the fifth preset number can be set according to actual needs, and no specific limitation is made here. The splicing symbol can also be set according to actual needs. In an embodiment of the present invention, the splicing symbol can be "\n".

[0082] When determining the first historical SQL question-answer pair, the target query can be encoded as a target vector, and the historical queries in all historical SQL question-answer pairs can be encoded as historical query vectors in the same way. The similarity between the target vector and each historical query vector is calculated, and the first six preset number of historical SQL question-answer pairs are selected in order of similarity from large to small as the first historical SQL question-answer pair.

[0083] The first template refers to a prompt word template for generating a preliminary SQL statement of a target query, and the first template can be set in advance according to actual needs. In an embodiment of the present invention, the first template can be:

[0084] "

Task

[0085] You are a {dialect} data analysis assistant who can analyze and reason based on [business data] and [similar cases]. You need to think carefully about the user's [question], understand the user's intention, and generate relevant SQL, and output it in the format of ```sql\n\n...```.

[0086] You need to label the user's question. The label needs to be selected from the label list, which may contain multiple labels. Output in the format of ```tag\n\n1, ...```.

[0087]

Table creation information

[0088] {schema_2}

[0089]

Business Information

[0090] {knowledge_2}

[0091] [Similar cases]

[0092] {cases_2}

[0093]

question

[0094] {question_2}”

[0095] Among them, the first template may include multiple slots to be filled, {dialect} is a database type slot, which is a system-level configuration variable that can define the database type, such as MySQL, Postgre, etc. {schema_2} is the target database information slot corresponding to the target query, which can be filled with the target database information corresponding to the target query; {knowledge_2} is a slot for business data, which can be filled with the first business data; {cases_2} can be filled with the first historical SQL question and answer pair; {question_2} can be used to fill the target query.

[0096] The first prompt word can be generated by filling the corresponding content into the slot of the first template. The first prompt word is input into the first reasoning model, and the first reasoning model can analyze and infer the preliminary SQL statement and the target business type corresponding to the target query according to the requirements in the first prompt word. Among them, the first reasoning model can be a language model with strong reasoning ability, and its output content can be divided into two parts. One is the internal reasoning and analysis content of the model, that is, the process data generated by the first reasoning model in the analysis and reasoning, which can be used as reasoning analysis data; the other is the formal response to the question, that is, the output preliminary SQL statement and the target business type. Combined with similar historical SQL question and answer pairs and similar business data, more background data can be provided for the first reasoning model to improve the reasoning accuracy of the preliminary SQL statement and the target business type.

[0097] S130. Filter the first business data and the semantic association information of all historical SQL question-answer pairs based on the target business type and the inference analysis data to obtain the second business data and the first semantic association information.

[0098] The semantic association information of the first business data and the historical SQL question and answer pairs are both background data related to the target query. In order to further improve the quality of the background data, after obtaining the target business type corresponding to the target query, the background data can be filtered based on the target business type to obtain the second business data and the first semantic association information that match both the target query and the target business type.

[0099] Optionally, when acquiring the second business data and the first semantic association information, the data business type corresponding to the first business data can be acquired; from the first business data, the first business data whose data business type does not include the target business type is filtered to obtain the second business data; the semantic association information consistent with the historical business type and the target business type is determined as the intermediate semantic association information; and the first semantic association information is determined based on the similarity between the intermediate semantic association information and the reasoning analysis data.

[0100] Among them, see Figure 5 , showing a schematic diagram of filtering, expanding and generating candidate SQL. When obtaining the semantic association information of the historical SQL question-answer pairs, the historical query can be used to determine the business data to be used in the business data. The business data to be used is the data associated with the historical business type. After traversing all the historical SQL question-answer pairs, there may be multiple historical business types corresponding to one business data, or there may be no corresponding historical business type. Based on the process of determining the business data to be used, the corresponding historical business type can be marked for the corresponding business data.

[0101] When obtaining the data service type of the first service data, the historical service type marked by the first service data can be directly used as its data service type. The first service data whose data service type does not include the target service type is filtered out from the first service data to obtain the second service data. In other words, the second service data is the first service data whose data service type includes the target service type.

[0102] The semantic association information of each historical SQL question-answer pair includes multiple sub-semantic association information and corresponding historical business types. The semantic association information whose historical business types do not include the target business type can be filtered out, and the remaining is the intermediate semantic association information. In other words, the intermediate semantic association information is the semantic association information whose historical business types include the target business type.

[0103] Then the similarity between the inference analysis data and the intermediate semantic association information can be used to determine the first semantic association information. In the embodiment of the present invention, all semantic association information is first filtered using the target business type, which can effectively reduce the data processing amount of subsequent similarity calculations.

[0104] In some embodiments, when determining the first semantic association information based on the similarity between the intermediate semantic association information and the reasoning analysis data, the reasoning analysis data can be segmented to obtain multiple sub-inference data; for each sub-inference data, the similarity between each sub-semantic association information in the intermediate semantic association information and the sub-inference data is calculated; for each sub-semantic association information, the target similarity corresponding to the sub-semantic association information is calculated using the similarity between all sub-inference data and the sub-semantic association information; and the first semantic association information is determined from the sub-semantic association information using the target similarity.

[0105] The acquired reasoning analysis data may be segmented, for example, the paragraphs may be segmented according to the line break character "\n" to obtain a plurality of sub-reasoning data. For each sub-reasoning data, the similarity between the sub-reasoning data and each sub-semantic association information in each intermediate semantic association information may be calculated.

[0106] For each sub-semantic association information, there is a similarity between the sub-semantic association information and each sub-reasoning data, that is, one sub-semantic association information corresponds to multiple similarities, and each similarity corresponds to the sub-reasoning data one by one. Using the multiple similarities corresponding to the sub-semantic association information, the target similarity corresponding to the sub-semantic association information can be calculated. Optionally, the multiple similarities can be directly summed up as the target similarity.

[0107] The sub-semantic association information is sorted in descending order of target similarity, and the sixth preset number of sub-semantic association information with the highest sorting order is selected as the first semantic association information. The sixth preset number can be set according to actual needs. The target business type is used to filter the redundant information in the first task data and the intermediate semantic association information to obtain highly correlated second business data and the first semantic association information.

[0108] S140: Use the target query to expand the first business data, and use the preliminary SQL statement to expand the first historical SQL question-answer pair to obtain expanded business data and expanded SQL question-answer pairs.

[0109] The first business data and the first historical SQL question-answer pair are both background data related to the target query. In order to provide the model with richer background data in the process of generating SQL, the first business data can be expanded based on the target query to obtain extended business data; and the first historical SQL question-answer pair can be expanded using the preliminary SQL statement to obtain an extended SQL question-answer pair.

[0110] As an implementation method, when expanding the first business data and the first historical SQL question and answer pair, third business data can be determined from the business data based on the similarity between the target query and each of the business data, and the number of the third business data is greater than the number of the first business data; the third business data different from the first business data is used as extended business data; the preliminary SQL statement is randomly masked to obtain the SQL statement to be used; based on the similarity between the SQL statement to be used and each historical SQL question and answer pair, a second historical SQL question and answer pair is determined, and the number of the second historical SQL question and answer pairs is greater than the number of the first historical SQL question and answer pairs; the second historical SQL question and answer pair different from the first historical SQL question and answer pair is used as an extended SQL question and answer pair.

[0111] The target query is encoded into a target vector, and the third business data is determined from the business data in the same manner as the first business data described above. The difference is that when determining the first business data, a fourth preset number of data is extracted from the noun vector database, and a fifth preset number of data is extracted from the semantic vector database, while when determining the third business data, a seventh preset number of data is extracted from the noun vector database, and an eighth preset number of data is extracted from the semantic vector database. Among them, the seventh preset number is greater than the fourth preset number, and the eighth preset number is greater than the fifth preset number. It can be set specifically according to actual needs. For example, the seventh preset number is approximately 6 times the fourth preset number, and the eighth preset number is approximately 6 times the fifth preset number.

[0112] The amount of the third business data determined in the above manner is also significantly greater than the amount of the first business data, and the third business data different from the first business data is used as extended business data.

[0113] For the preliminary SQL statement, a random masking process may be performed on the preliminary SQL statement, wherein the masking process may be performed with words as the minimum granularity to obtain the SQL statement to be used. Then, based on the similarity between the SQL statement to be used and each historical SQL question-answer pair, a second historical SQL question-answer pair may be determined.

[0114] Optionally, the SQL statement to be used may be encoded as a vector to be used, and the historical SQL statements in each historical SQL question-answer pair may be encoded as a historical SQL vector in the same manner. The similarity between the vector to be used and each historical SQL vector is calculated, and the historical SQL question-answer pairs corresponding to the historical SQL vectors are sorted in descending order of similarity, so as to select the top ninth preset number of historical SQL question-answer pairs as the second historical SQL question-answer pairs. Among them, the ninth preset number is greater than the sixth preset number, and can be specifically set according to actual needs. In an embodiment of the present invention, the ninth preset number is approximately 6 times the sixth preset number.

[0115] It should be noted that the first historical SQL question-answer pair is determined based on the similarity between the target query and the historical query, while the second historical SQL question-answer pair is determined based on the similarity between the preliminary SQL statement and the historical SQL statement. There may be repeated content in the first historical SQL question-answer pair and the second historical SQL question-answer pair, so the second historical SQL question-answer pair that is different from the first historical SQL question-answer pair can be used as an extended SQL question-answer pair.

[0116] The target query is used to expand the available business data, and the preliminary SQL statements obtained by reasoning are used to expand the historical SQL question-answer pairs, which can provide more comprehensive and effective background data for the subsequent generation of target SQL, thereby improving the accuracy of the target SQL.

[0117] S150, integrating the first semantic association information, the second business data, the extended business data, the extended SQL question-answer pair, the first historical SQL question-answer pair, and the preliminary SQL statement to generate a target SQL statement corresponding to the target query.

[0118] After fusing the aforementioned determined first semantic association information, second business data, extended business data, extended SQL question-answer pair, first historical SQL question-answer pair, and preliminary SQL statement, a target SQL statement corresponding to the target query can be generated. As an implementation method, the above data can be directly input into a prompt word template in combination with the database information of the target query to obtain a corresponding prompt word, and then the corresponding prompt word is input into a large language model for inference, so that the large language model outputs a target SQL statement corresponding to the target query.

[0119] As another implementation, in order to more accurately generate a target SQL statement, multiple intermediate business data may be generated using the first semantic association information, the second business data, and the extended business data; multiple intermediate SQL question-answer pairs may be generated using the first historical SQL question-answer pairs and the extended SQL question-answer pairs; the intermediate SQL question-answer pairs and the intermediate business data may be combined to obtain multiple intermediate data; for each intermediate data, a candidate SQL statement may be generated based on the intermediate data, the target query, and the preliminary SQL statement; and the target SQL statement may be determined based on the execution data of all the candidate SQL statements and the preliminary SQL statement.

[0120] The first semantic association information, the second business data, and the extended business data are all related to the business data, and can be used as a set of data. By integrating these three types of data, multiple intermediate business data can be generated. The first historical SQL question and answer pair and the extended SQL question and answer pair are both related to the historical SQL question and answer pair, and can be used as a set of data. By integrating these two types of data, multiple intermediate SQL questions and answers can be generated.

[0121] Optionally, when generating multiple intermediate business data, the extended business data may be shuffled to obtain processed extended business data; the processed extended business data may be divided into multiple sub-extended business data; for each sub-extended business data, the sub-extended business data, the first semantic association information and the second business data may be combined to obtain intermediate business data.

[0122] Based on the above determination of the content of the extended business data, it can be known that the extended business data is composed of a plurality of third business data, and the third business data is sorted according to the similarity with the target query. The extended business data is randomly shuffled to obtain the processed extended business data. The extended business data can then be divided into a plurality of sub-extended business data, wherein the number of sub-extended business data can be set according to actual needs, and it is ensured that the amount of data in each sub-extended business data is substantially the same. For example, the extended business data is equally divided into 5 sub-extended business data. Then, each sub-extended business data is combined with the first semantic association information and the second business data to obtain a corresponding plurality of intermediate business data.

[0123] Optionally, when generating multiple intermediate SQL question and answer pairs, the extended SQL question and answer pairs may be shuffled to obtain processed extended SQL question and answer pairs; the processed extended SQL question and answer pairs may be divided into multiple sub-extended SQL question and answer pairs; for each sub-extended SQL question and answer pair, the sub-extended SQL question and answer pair and the first historical SQL question and answer pair may be combined to obtain an intermediate SQL question and answer pair.

[0124] Based on the content of the extended SQL question and answer pairs determined above, it can be known that the extended SQL question and answer pairs are all historical SQL question and answer pairs, and the extended SQL question and answer pairs are sorted according to the similarity with the preliminary SQL statements. The extended SQL question and answer pairs are randomly shuffled to obtain the processed extended SQL question and answer pairs. The extended SQL question and answer pairs can then be divided into multiple sub-extended SQL question and answer pairs, wherein the number of sub-extended SQL question and answer pairs can be consistent with the number of sub-extended business data, and ensure that the amount of data in each sub-extended SQL question and answer pair is basically the same. For example, the extended SQL question and answer pairs are equally divided into 5 sub-extended SQL question and answer pairs. Then, each sub-extended SQL question and answer pair is combined with the first historical SQL question and answer pair to obtain a corresponding plurality of intermediate SQL question and answer pairs.

[0125] By combining the intermediate business data and the intermediate SQL question and answer pair, a plurality of intermediate data can be obtained. Among them, one intermediate business data and one intermediate SQL question and answer pair can be combined, and the number of the intermediate business data is the same as the number of the intermediate SQL question and answer pairs. Optionally, the sub-extended business data and the sub-extended SQL question and answer pairs can be numbered according to the same coding rule, and the number of the sub-extended business data is used as the number of the corresponding intermediate business data, and the number of the sub-extended SQL question and answer pair is used as the number of the corresponding intermediate SQL question and answer. The intermediate business data and the intermediate SQL question and answer pair with the same number are combined to obtain a plurality of intermediate data, wherein the number of the plurality of intermediate data is consistent with the number of the intermediate business data and the intermediate SQL question and answer pairs.

[0126] For each intermediate data, a candidate SQL statement may be generated using the intermediate data, the target query, and the preliminary SQL statement, and the number of the candidate SQL statements is consistent with the number of the intermediate data.

[0127] As an implementation method, the intermediate data, the target query, and the preliminary SQL statement can be filled into the second template to obtain the second prompt word; the second prompt word is input into the second reasoning model so that the second reasoning model outputs the candidate SQL statement. The second template is a preset prompt word template for generating the candidate SQL statement, and the second template can be as follows:

[0128] "

Task

[0129] You are a {dialect} data analysis assistant who can analyze and reason based on [business data] and [similar cases]. You need to think carefully about the user's [question], understand the user's intention, think about whether the [preliminary SQL] is correct, and re-output the SQL in the format ```sql\n\n...```.

[0130]

Table creation information

[0131] {schema_2}

[0132]

Business Information

[0133] {knowledge_filtered}

[0134] {Q_rc_rag}

[0135] {K_i}

[0136] [Similar cases]

[0137] {cases_2}

[0138] {C_i}

[0139]

question

[0140] {question_2}

[0141]

Introduction to SQL

[0142] {pred_sql}"

[0143] Among them, [Business data] is the intermediate business data in the intermediate data; {knowledge_filtered} is filled with the second business data; {Q_rc_rag} is filled with the first semantic association information; {K_i} is filled with the sub-extended business data in the intermediate business data; [Similar cases] is the intermediate SQL question-answer pair in the intermediate data; {cases_2} is filled with the first historical SQL question-answer pair; {C_i} is filled with the sub-extended SQL question-answer pair in the intermediate SQL question-answer pair. {question_2} is filled with the target query; {pred_sql} is filled with the preliminary SQL statement.

[0144] After the content is filled, a corresponding second prompt word can be generated. It can be understood that one intermediate data can generate a corresponding second prompt word. Then, based on multiple second prompt words, the second reasoning model is called in parallel, and the second prompt word is input into the second reasoning model. The output of the second reasoning model is the candidate SQL statement. In this way, multiple candidate SQL statements can be obtained, and the reasoning speed can be effectively improved by calling in parallel.

[0145] Execute the candidate SQL statements and the preliminary SQL statements, and obtain their corresponding execution data, which may include execution time, execution status, and execution results. Count the execution data with a successful execution status, and use the SQL with the largest number of overlapping execution results and the shortest execution time as the target SQL. If there is no SQL statement that meets the above conditions, the SQL statement with a successful execution status and a non-empty execution result can be screened out as the first candidate SQL statement; if the first candidate SQL statement includes the preliminary SQL statement, the preliminary SQL statement is used as the target SQL statement; if the first candidate SQL statement does not include the preliminary SQL statement, one is randomly selected as the target SQL statement.

[0146] The SQL statement generation solution provided by the embodiment of the present invention can be applied in various scenarios where SQL statements need to be generated. For example, in intelligent question and answering, the solution provided by the embodiment of the present invention can assist the large language model to convert the target query into a high-quality SQL statement based on the target query input by the user, through the target database information and background data related to the target query, so as to improve the accuracy of the question and answer.

[0147] The method provided by the embodiment of the present invention can establish semantic association information between business data and historical SQL question-answer pairs, and use the first historical SQL question-answer pair related to the target query and the first business data to generate preliminary SQL, target business type and reasoning analysis data. The target business type and the analysis and reasoning data are used to filter out redundant information in the first historical question-answer pair and the semantic association information, and then the filtered data is expanded to deeply mine the data related to the target query to ensure the quality of the background data. The background data is then used to generate the final SQL statement, which can effectively improve the quality of the SQL statement.

[0148] In order to better implement the above method, an embodiment of the present invention further provides a SQL statement generation device, which can be integrated in an electronic device, and the electronic device can be a terminal, a server, etc. Among them, the terminal can be a mobile phone, a tablet computer, a smart Bluetooth device, a laptop, a personal computer, etc.; the server can be a single server or a server cluster composed of multiple servers.

[0149] For example, in this embodiment, the method of the embodiment of the present invention is described in detail by taking the SQL statement generation device being specifically integrated in the server as an example.

[0150] For example, Figure 6 As shown, the SQL statement generating device 200 may include an acquisition module 210 , a preliminary generating module 220 , a filtering module 230 , an expansion module 240 and a target generating module 250 .

[0151] An acquisition module 210 is used to acquire, for each historical SQL question-answer pair, semantic association information between a historical query and a historical SQL statement, wherein the semantic association information includes a plurality of sub-semantic association information and a historical business type corresponding to the historical query, and the semantic association information is obtained by analyzing the historical SQL question-answer pair using business data;

[0152] A preliminary generation module 220 is used to fuse the first historical SQL question-answer pair and the first business data related to the target query, and infer the preliminary SQL statement corresponding to the target query, the target business type of the target query, and the inference analysis data;

[0153] A filtering module 230 is used to filter the first business data and the semantic association information of all historical SQL question-answer pairs by using the target business type and the inference analysis data to obtain the second business data and the first semantic association information;

[0154] An expansion module 240 is used to expand the first business data using the target query, and to expand the first historical SQL question-answer pair using the preliminary SQL statement, to obtain expanded business data and expanded SQL question-answer pair;

[0155] The target generation module 250 is used to fuse the first semantic association information, the second business data, the extended business data, the extended SQL question and answer pair, the first historical SQL question and answer pair and the preliminary SQL statement to generate a target SQL statement corresponding to the target query.

[0156] In some embodiments, the preliminary generation module 220 is specifically used to:

[0157] Determining first business data from the business data according to the similarity between the target query and each business data;

[0158] Determine a first historical SQL question-answer pair from the historical SQL question-answer pairs according to the similarity between the target query and the historical query in each historical SQL question-answer pair;

[0159] Filling the first business data, the first historical SQL question-answer pair, and target database information into a first template to generate a first prompt word;

[0160] Using the first prompt word to guide the first reasoning model, analyzing and reasoning the target query to generate a preliminary SQL statement and target business data of the target query;

[0161] The process data generated by the first reasoning model in the analysis and reasoning is obtained as the reasoning analysis data.

[0162] In some embodiments, the filtering module 230 is specifically used to:

[0163] Obtaining a data service type corresponding to the first service data;

[0164] From the first service data, filtering the first service data whose data service type does not include the target service type, to obtain second service data;

[0165] Determine the semantic association information that the historical service type is consistent with the target service type as the intermediate semantic association information;

[0166] Based on the similarity between the intermediate semantic association information and the reasoning analysis data, first semantic association information is determined.

[0167] In some embodiments, the filtering module 230 is specifically used to:

[0168] Segmenting the reasoning analysis data to obtain a plurality of sub-reasoning data;

[0169] For each of the sub-inference data, calculating the similarity between each sub-semantic association information in the intermediate semantic association information and the sub-inference data;

[0170] For each of the sub-semantic association information, using the similarities between all sub-inference data and the sub-semantic association information, calculate the target similarity corresponding to the sub-semantic association information;

[0171] The first semantic association information is determined from the sub-semantic association information according to the target similarity.

[0172] In some embodiments, the expansion module 240 is specifically used to:

[0173] Determining third business data from the business data based on the similarity between the target query and each of the business data, wherein the amount of the third business data is greater than the amount of the first business data;

[0174] using third service data different from the first service data as extended service data;

[0175] Performing random masking processing on the preliminary SQL statement to obtain a SQL statement to be used;

[0176] Determine a second historical SQL question-answer pair based on the similarity between the SQL statement to be used and each historical SQL question-answer pair, wherein the number of the second historical SQL question-answer pairs is greater than the number of the first historical SQL question-answer pairs;

[0177] A second historical SQL question-answer pair different from the first historical SQL question-answer pair is used as an extended SQL question-answer pair.

[0178] In some embodiments, the target generation module 250 is specifically used to:

[0179] Generate a plurality of intermediate business data using the first semantic association information, the second business data and the extended business data;

[0180] Generate multiple intermediate SQL question-answer pairs using the first historical SQL question-answer pairs and the extended SQL question-answer pairs;

[0181] Combining the intermediate SQL question-answer pair and the intermediate business data to obtain a plurality of intermediate data;

[0182] For each intermediate data, generating a candidate SQL statement based on the intermediate data, the target query and the preliminary SQL statement;

[0183] A target SQL statement is determined based on the execution data of all the candidate SQL statements and the preliminary SQL statement.

[0184] In some embodiments, the target generation module 250 is specifically used to:

[0185] Scrambling the extended service data to obtain processed extended service data;

[0186] Dividing the processed extended service data into a plurality of sub-extended service data;

[0187] For each sub-extended service data, the sub-extended service data, the first semantic association information and the second service data are combined to obtain intermediate service data.

[0188] In some embodiments, the target generation module 250 is specifically used to:

[0189] The extended SQL question-answer pair is shuffled to obtain a processed extended SQL question-answer pair;

[0190] Dividing the processed expanded SQL question-answer pair into a plurality of sub-expanded SQL question-answer pairs;

[0191] For each sub-expanded SQL question-answer pair, the sub-expanded SQL question-answer pair and the first historical SQL question-answer pair are combined to obtain an intermediate SQL question-answer pair.

[0192] In some embodiments, the acquisition module 210 is specifically used to:

[0193] For each historical SQL question-answer pair, obtain historical database information corresponding to the historical query in the historical SQL question-answer pair;

[0194] Determine candidate business data from the business data by using the similarity between the historical query and the business data and associate it with the historical business type corresponding to the historical query to obtain the business data to be used;

[0195] Determine candidate SQL question-answer pairs from historical SQL question-answer pairs by using the similarity between the historical query and other historical queries;

[0196] In combination with the historical database information, the business data to be used, and the candidate SQL question-answer pair, guiding the third reasoning model to think about the correlation between the historical query and the historical SQL statement to obtain semantic correlation information;

[0197] The semantic association information is divided into a plurality of sub-semantic association information, and each of the sub-semantic association information is associated with the historical business type.

[0198] In specific implementation, the above modules can be implemented as independent entities, or can be arbitrarily combined and implemented as the same or several entities. The specific implementation of the above modules can be found in the previous method embodiments, which will not be repeated here.

[0199] From the above, it can be seen that the SQL statement generation device of this embodiment can obtain the semantic association information between historical queries and historical SQL statements established based on business data to enhance the comprehension of the model, and use the first historical SQL question-answer pair related to the target query and the first business data to preliminarily analyze the preliminary SQL statement, target business type and reasoning analysis data; use the inferred target business type and reasoning analysis data to filter the first business data and semantic association information to obtain more matching background data; in order to enrich the background data, enable the target business type and preliminary SQL statement for deep mining, and finally combine the mined data, filtered data and preliminary SQL statement to generate the final target SQL statement, which can provide high-quality background data for the model, and thus effectively improve the quality of the generated SQL statement.

[0200] The embodiment of the present invention further provides an electronic device, which may be a terminal, a server, or the like. The terminal may be a mobile phone, a tablet computer, a smart Bluetooth device, a notebook computer, a personal computer, or the like; the server may be a single server or a server cluster composed of multiple servers, or the like.

[0201] In some embodiments, the SQL statement generating device may also be integrated into multiple electronic devices. For example, the SQL statement generating device may be integrated into multiple servers, and the SQL statement generating method of the present invention may be implemented by multiple servers.

[0202] In this embodiment, the electronic device of this embodiment is a server as an example for detailed description, for example, Figure 7 As shown, it shows a schematic diagram of the structure of an electronic device involved in an embodiment of the present invention, specifically:

[0203] The electronic device may include components such as a processor 310 with one or more processing cores, a memory 320 with one or more computer-readable storage media, a power supply 330, an input module 340, and a communication module 350. Those skilled in the art will appreciate that Figure 7 The electronic device structure shown in the figure does not constitute a limitation on the electronic device, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.

[0204] The processor 310 is the control center of the electronic device. It uses various interfaces and lines to connect various parts of the entire electronic device. It executes various functions of the electronic device and processes data by running or executing software programs and / or modules stored in the memory 320, and calling data stored in the memory 320. In some embodiments, the processor 310 may include one or more processing cores; in some embodiments, the processor 310 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface, and application programs, and the modem processor mainly processes wireless communications. It is understandable that the above-mentioned modem processor may not be integrated into the processor 310.

[0205] The memory 320 can be used to store software programs and modules. The processor 310 executes various functional applications and data processing by running the software programs and modules stored in the memory 320. The memory 320 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area may store data created according to the use of the electronic device, etc. In addition, the memory 320 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices. Accordingly, the memory 320 may also include a memory controller to provide the processor 310 with access to the memory 320.

[0206] The electronic device also includes a power supply 330 for supplying power to various components. In some embodiments, the power supply 330 can be logically connected to the processor 310 through a power management system, so as to manage charging, discharging, and power consumption through the power management system. The power supply 330 can also include any components such as one or more DC or AC power supplies, recharging systems, power failure detection circuits, power converters or inverters, and power status indicators.

[0207] The electronic device may further include an input module 340, which may be used to receive input digital or character information and generate keyboard, mouse, joystick, optical or trackball signal inputs related to user settings and function controls.

[0208] The electronic device may further include a communication module 350. In some embodiments, the communication module 350 may include a wireless module. The electronic device may perform short-range wireless transmission through the wireless module of the communication module 350, thereby providing the user with wireless broadband Internet access. For example, the communication module 350 may be used to help the user send and receive emails, browse web pages, and access streaming media.

[0209] Although not shown, the electronic device may further include a display unit, etc., which will not be described in detail herein. Specifically in this embodiment, the processor 310 in the electronic device will load the executable files corresponding to the processes of one or more application programs into the memory 320 according to the following instructions, and the processor 310 will run the application programs stored in the memory 320, thereby implementing the steps in the methods of the embodiments of the present invention.

[0210] The specific implementation of the above operations can be found in the previous embodiments, which will not be described in detail here.

[0211] From the above, it can be seen that the electronic device provided by the embodiment of the present invention can obtain the semantic association information between historical queries and historical SQL statements established based on business data to enhance the comprehension of the model, and use the first historical SQL question-answer pair related to the target query and the first business data to preliminarily analyze the preliminary SQL statement, the target business type and the reasoning analysis data; use the inferred target business type and the reasoning analysis data to filter the first business data and the semantic association information to obtain more matching background data; in order to enrich the background data, enable the target business type and the preliminary SQL statement for deep mining, and finally combine the mined data, filtered data and the preliminary SQL statement to generate the final target SQL statement, which can provide high-quality background data for the model, and thus effectively improve the quality of the generated SQL statement.

[0212] A person of ordinary skill in the art will appreciate that all or part of the steps in the various methods of the above embodiments may be completed by instructions, or by controlling related hardware through instructions. The instructions may be stored in a computer-readable storage medium and loaded and executed by a processor.

[0213] To this end, an embodiment of the present invention provides a computer-readable storage medium, in which a plurality of instructions are stored. The instructions can be loaded by a processor to execute the steps in any one of the SQL statement generation methods provided in the embodiments of the present invention.

[0214] The storage medium may include: a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.

[0215] According to one aspect of the present invention, a computer program product or computer program is provided, the computer program product or computer program including a computer program / instruction, the computer program / instruction being stored in a computer-readable storage medium. A processor of an electronic device reads the computer program / instruction from the computer-readable storage medium, and the processor executes the computer program / instruction, so that the electronic device executes the method provided in various optional implementations of the semantic association information acquisition aspect or SQL statement generation aspect provided in the above embodiments.

[0216] Since the instructions stored in the storage medium can execute the steps in any SQL statement generation method provided in the embodiments of the present invention, the beneficial effects that can be achieved by any SQL statement generation method provided in the embodiments of the present invention can be achieved. Please refer to the previous embodiments for details and will not be repeated here.

[0217] The above is a detailed introduction to a method and device for generating SQL statements provided in an embodiment of the present invention. Specific examples are used herein to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method and core idea of ​​the present invention. At the same time, for those skilled in the art, according to the idea of ​​the present invention, there may be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as limiting the present invention.

Claims

1. A method for generating a SQL statement, characterized in that: The method comprises: For each historical SQL question-answer pair, obtain semantic association information between the historical query and the historical SQL statement, wherein the semantic association information includes a plurality of sub-semantic association information and a historical business type corresponding to the historical query, and the semantic association information is obtained by analyzing the historical SQL question-answer pair using business data; Integrate the first historical SQL question-answer pair and the first business data related to the target query, and infer a preliminary SQL statement corresponding to the target query, a target business type of the target query, and inference analysis data; Using the target business type and the inference analysis data, filter the first business data and the semantic association information of all historical SQL question-answer pairs to obtain the second business data and the first semantic association information; Using the target query to expand the first business data, and using the preliminary SQL statement to expand the first historical SQL question-answer pair, to obtain expanded business data and expanded SQL question-answer pairs; The first semantic association information, the second business data, the extended business data, the extended SQL question-answer pair, the first historical SQL question-answer pair, and the preliminary SQL statement are integrated to generate a target SQL statement corresponding to the target query.

2. The method according to claim 1, characterized in that: The first historical SQL question-answer pair and the first business data related to the target query are integrated to infer the preliminary SQL statement corresponding to the target query, the target business type of the target query, and the inference analysis data, including: Determining first business data from the business data according to the similarity between the target query and each business data; Determine a first historical SQL question-answer pair from the historical SQL question-answer pairs according to the similarity between the target query and the historical query in each historical SQL question-answer pair; Filling the first business data, the first historical SQL question-answer pair, and target database information into a first template to generate a first prompt word; Using the first prompt word to guide the first reasoning model, analyzing and reasoning the target query to generate a preliminary SQL statement and target business data of the target query; The process data generated by the first reasoning model in the analysis and reasoning is obtained as the reasoning analysis data.

3. The method according to claim 1, characterized in that The step of filtering the first business data and the semantic association information of all historical SQL question-answer pairs based on the target business type and the inference analysis data to obtain the second business data and the first semantic association information includes: Obtaining a data service type corresponding to the first service data; From the first service data, filtering the first service data whose data service type does not include the target service type to obtain second service data; Determine the semantic association information that the historical service type is consistent with the target service type as the intermediate semantic association information; Based on the similarity between the intermediate semantic association information and the reasoning analysis data, first semantic association information is determined.

4. The method according to claim 3, characterized in that The determining the first semantic association information based on the similarity between the intermediate semantic association information and the reasoning analysis data includes: Segmenting the reasoning analysis data to obtain a plurality of sub-reasoning data; For each of the sub-inference data, calculating the similarity between each sub-semantic association information in the intermediate semantic association information and the sub-inference data; For each of the sub-semantic association information, using the similarities between all sub-inference data and the sub-semantic association information, calculate the target similarity corresponding to the sub-semantic association information; The first semantic association information is determined from the sub-semantic association information according to the target similarity.

5. The method according to claim 1, characterized in that The step of using the target query to expand the first business data and using the preliminary SQL statement to expand the first historical SQL question-answer pair to obtain the expanded business data and the expanded SQL question-answer pair includes: Determining third business data from the business data based on the similarity between the target query and each of the business data, wherein the amount of the third business data is greater than the amount of the first business data; using third service data different from the first service data as extended service data; Performing random masking processing on the preliminary SQL statement to obtain a SQL statement to be used; Determine a second historical SQL question-answer pair based on the similarity between the SQL statement to be used and each historical SQL question-answer pair, wherein the number of the second historical SQL question-answer pairs is greater than the number of the first historical SQL question-answer pairs; A second historical SQL question-answer pair different from the first historical SQL question-answer pair is used as an extended SQL question-answer pair.

6. The method according to claim 1, characterized in that The step of fusing the first semantic association information, the second business data, the extended business data, the extended SQL question-answer pair, the first historical SQL question-answer pair, and the preliminary SQL statement to generate a target SQL statement corresponding to the target query includes: Generate a plurality of intermediate business data using the first semantic association information, the second business data and the extended business data; Generate multiple intermediate SQL question-answer pairs using the first historical SQL question-answer pairs and the extended SQL question-answer pairs; Combining the intermediate SQL question-answer pair and the intermediate business data to obtain a plurality of intermediate data; For each intermediate data, generating a candidate SQL statement based on the intermediate data, the target query and the preliminary SQL statement; A target SQL statement is determined based on the execution data of all the candidate SQL statements and the preliminary SQL statement.

7. The method according to claim 6, characterized in that The generating a plurality of intermediate business data by using the first semantic association information, the second business data and the extended business data includes: Scrambling the extended service data to obtain processed extended service data; Dividing the processed extended service data into a plurality of sub-extended service data; For each sub-extended service data, the sub-extended service data, the first semantic association information and the second service data are combined to obtain intermediate service data.

8. The method according to claim 6, characterized in that The step of generating a plurality of intermediate SQL question-answer pairs using the first historical SQL question-answer pairs and the extended SQL question-answer pairs includes: The extended SQL question-answer pair is shuffled to obtain a processed extended SQL question-answer pair; Dividing the processed expanded SQL question-answer pair into a plurality of sub-expanded SQL question-answer pairs; For each sub-expanded SQL question-answer pair, the sub-expanded SQL question-answer pair and the first historical SQL question-answer pair are combined to obtain an intermediate SQL question-answer pair.

9. The method according to claim 1, characterized in that: The step of obtaining semantic association information between historical queries and historical SQL statements for each historical SQL question-answer pair includes: For each historical SQL question-answer pair, obtain historical database information corresponding to the historical query in the historical SQL question-answer pair; Determine candidate business data from the business data by using the similarity between the historical query and the business data and associate it with the historical business type corresponding to the historical query to obtain the business data to be used; Determine candidate SQL question-answer pairs from historical SQL question-answer pairs by using the similarity between the historical query and other historical queries; In combination with the historical database information, the business data to be used, and the candidate SQL question-answer pair, guiding the third reasoning model to think about the correlation between the historical query and the historical SQL statement to obtain semantic correlation information; The semantic association information is divided into a plurality of sub-semantic association information, and each of the sub-semantic association information is associated with the historical business type.

10. A device for generating SQL statements, the device being used to implement the method according to any one of claims 1 to 9, characterized in that: The device comprises: An acquisition module, for acquiring, for each historical SQL question-answer pair, semantic association information between a historical query and a historical SQL statement, wherein the semantic association information includes a plurality of sub-semantic association information and a historical business type corresponding to the historical query, and the semantic association information is obtained by analyzing the historical SQL question-answer pair using business data; A preliminary generation module, used to fuse the first historical SQL question-answer pair and the first business data related to the target query, and infer the preliminary SQL statement corresponding to the target query, the target business type of the target query, and the inference analysis data; A filtering module, used to filter the first business data and the semantic association information of all historical SQL question-answer pairs based on the target business type and the inference analysis data, to obtain the second business data and the first semantic association information; An expansion module, configured to expand the first business data using the target query, and expand the first historical SQL question-answer pair using the preliminary SQL statement, to obtain expanded business data and expanded SQL question-answer pair; The target generation module is used to fuse the first semantic association information, the second business data, the extended business data, the extended SQL question and answer pair, the first historical SQL question and answer pair and the preliminary SQL statement to generate a target SQL statement corresponding to the target query.

Citation Information

Patent Citations

  • Question and answer processing method and device, electronic equipment and storage medium

    CN113553412A

  • Text-to-SQL conversion method and system based on large pre-training model

    CN118503273A

  • Question and answer processing method and device

    CN119323262A

  • Human-machine dialog method and apparatus, and device

    US20210191952A1