SQL statement generation method and device, electronic equipment and storage medium

Through the mutual cooperation of generation, correction and verification models, the problem of high error rate when converting SQL statements in text is solved, and more accurate and efficient SQL statement generation is achieved, meeting the user's query needs.

CN119884157AInactive Publication Date: 2025-04-25IFLYTEK (SUZHOU) TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510371568.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-03-27
Publication Date
2025-04-25
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

In the prior art, wrong SQL statements often appear when text conversion SQL statements are converted, resulting in users being unable to obtain the correct query results. How to effectively generate more accurate SQL statements has become an urgent problem in the industry.

Method used

By entering the query request text and database information into the generation model, candidate SQL statements are generated; the model is used to correct candidate SQL statements that cannot be executed; the executable candidate SQL statements are input into the SQL verification model to generate the target SQL statement. This method improves the accuracy of SQL statements through the mutual cooperation of generation, correction and verification models.

Benefits of technology

It significantly improves the accuracy and practicality of text-converted SQL statements, ensures that the final generated target SQL statement can accurately meet user needs, reduces the workload of manual annotation, and improves the accuracy of the model.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119884157A_ABST
    Figure CN119884157A_ABST
Patent Text Reader

Abstract

The invention provides an SQL statement generation method and device, electronic equipment and a storage medium, and relates to the technical field of data processing, and the method comprises the following steps: inputting a query request text and database information into a generation model to obtain at least one candidate SQL statement corresponding to the query request text; according to an SQL statement correction model, the candidate SQL statements which cannot be executed are corrected, and all the candidate SQL statements which can be executed are obtained; inputting each executable candidate SQL statement into an SQL verification model, and generating a target SQL statement corresponding to the query request text; wherein the SQL statement correction model and the SQL verification model are both obtained through training based on verification training data generated by a temporary generation model, the temporary generation model is obtained through training based on N-1 pieces of generation training data, and N is a positive integer larger than 1.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of data processing technology, and in particular to a method, device, electronic device and storage medium for generating SQL statements. Background Art

[0002] Since database queries require the queryer to be familiar with the database structure and master the method of writing SQL statements, non-professional users cannot interact with the database directly. Text-to-SQL statements are a technology that converts a natural language text of a database query into an executable SQL statement. By converting the user's natural language requirements into SQL statements, it provides users with a more intuitive and easy-to-use database interaction method.

[0003] In the related art, erroneous SQL statements often occur when converting text to SQL statements, resulting in users being unable to obtain correct query results. Therefore, how to effectively generate more accurate SQL statements has become a problem that needs to be solved urgently in the industry. Summary of the invention

[0004] The present invention provides a SQL statement generation method, device, electronic device and storage medium, which are used to solve the problem of how to effectively generate more accurate SQL statements in the prior art.

[0005] The present invention provides a method for generating a SQL statement, comprising the following steps: Inputting the query request text and database information into the generation model to obtain at least one candidate SQL statement corresponding to the query request text; According to the SQL statement correction model, the candidate SQL statements that cannot be executed are corrected to obtain executable candidate SQL statements; Inputting each executable candidate SQL statement into an SQL verification model to generate a target SQL statement corresponding to the query request text; The SQL statement correction model and the SQL verification model are both obtained by training based on verification training data generated by a temporary generation model, and the temporary generation model is obtained by training based on N-1 generation training data, where N is a positive integer greater than 1.

[0006] According to a method for generating SQL statements provided by the present invention, the method corrects the unexecutable candidate SQL statements according to the SQL statement correction model to obtain executable candidate SQL statements, including: Execute each of the candidate SQL statements, and determine the candidate SQL statements that cannot be executed according to the execution results; Inputting the unexecutable candidate SQL statements into the SQL statement correction model, and outputting the corrected candidate SQL statements; Continue to execute the corrected candidate SQL statements, determine the unexecutable candidate SQL statements according to the execution results, and continue to input the unexecutable candidate SQL statements into the SQL statement correction model until all candidate SQL statements are executable, thereby obtaining executable candidate SQL statements.

[0007] According to a method for generating SQL statements provided by the present invention, before the step of correcting the unexecutable candidate SQL statements according to the SQL statement correction model to obtain executable candidate SQL statements, the method further includes: Based on N-1 generated training data, a temporary generated model is trained to obtain a temporary generated model corresponding to the i-th target generated training data; wherein the N-1 generated training data are obtained by removing the i-th target generated training data from the N generated training data; each generated training data includes: a query request text sample and a database information sample carrying a correct SQL statement label; i is a positive integer; The i-th target generation training data is used as test data, and input into a temporary generation model corresponding to the i-th target generation training data, to obtain verification training data corresponding to the i-th target generation training data; Traverse N different target-generated training data, input each target-generated training data into the corresponding temporary generation model as test data, and obtain multiple verification training data.

[0008] According to a SQL statement generation method provided by the present invention, the verification training data includes: correct SQL statement samples, unexecutable SQL statement samples, and logically incorrect SQL statement samples.

[0009] According to a SQL statement generation method provided by the present invention, the training method of the SQL statement correction model specifically includes: Based on the unexecutable SQL statement samples carrying correct SQL labels, model training is performed to obtain a SQL statement correction model; The SQL statement correction model is used to modify an input SQL statement that cannot be executed into an executable SQL statement.

[0010] According to a SQL statement generation method provided by the present invention, the training method of the SQL verification model specifically includes: Based on the correct SQL statement samples carrying the correct SQL tags and the logically incorrect SQL statement samples, model training is performed to obtain the SQL verification model; The SQL verification model is used to select a correct target SQL statement from multiple input SQL statements.

[0011] According to a method for generating an SQL statement provided by the present invention, the step of inputting each executable candidate SQL statement into an SQL verification model to generate a target SQL statement corresponding to the query request text includes: Back-translate each of the candidate SQL statements into a natural language text to obtain a natural language interpretation text of each of the candidate SQL statements; The natural language interpretation text of each candidate SQL statement and each candidate SQL statement are input into the SQL verification model to obtain the target SQL statement corresponding to the query request text.

[0012] The present invention also provides a SQL statement generating device, comprising the following modules: An input module, used to input the query request text and database information into the generation model to obtain at least one candidate SQL statement corresponding to the query request text; A correction module, used to correct the unexecutable candidate SQL statements according to the SQL statement correction model to obtain executable candidate SQL statements; A generation module, used for inputting each executable candidate SQL statement into an SQL verification model to generate a target SQL statement corresponding to the query request text; The SQL statement correction model and the SQL verification model are both obtained by training based on verification training data generated by a temporary generation model, and the temporary generation model is obtained by training based on N-1 generation training data, where N is a positive integer greater than 1.

[0013] According to the SQL statement generating device provided by the present invention, the device is also used for: Execute each of the candidate SQL statements, and determine the candidate SQL statements that cannot be executed according to the execution results; Inputting the unexecutable candidate SQL statements into the SQL statement correction model, and outputting the corrected candidate SQL statements; Continue to execute the corrected candidate SQL statements, determine the unexecutable candidate SQL statements according to the execution results, and continue to input the unexecutable candidate SQL statements into the SQL statement correction model until all candidate SQL statements are executable, thereby obtaining executable candidate SQL statements.

[0014] According to the SQL statement generating device provided by the present invention, the device is also used for: Based on N-1 generated training data, a temporary generated model is trained to obtain a temporary generated model corresponding to the i-th target generated training data; wherein the N-1 generated training data are obtained by removing the i-th target generated training data from the N generated training data; each generated training data includes: a query request text sample and a database information sample carrying a correct SQL statement label; i is a positive integer; The i-th target generation training data is used as test data, and input into a temporary generation model corresponding to the i-th target generation training data, to obtain verification training data corresponding to the i-th target generation training data; Traverse N different target-generated training data, input each target-generated training data into the corresponding temporary generation model as test data, and obtain multiple verification training data.

[0015] According to the SQL statement generation device provided by the present invention, the verification training data includes: correct SQL statement samples, unexecutable SQL statement samples, and logically incorrect SQL statement samples.

[0016] According to the SQL statement generating device provided by the present invention, the device is also used for: Based on the correct SQL statement samples carrying the correct SQL tags and the logically incorrect SQL statement samples, model training is performed to obtain the SQL verification model; The SQL verification model is used to select a correct target SQL statement from multiple input SQL statements.

[0017] According to the SQL statement generating device provided by the present invention, the device is also used for: Back-translate each of the candidate SQL statements into a natural language text to obtain a natural language interpretation text of each of the candidate SQL statements; The natural language interpretation text of each candidate SQL statement and each candidate SQL statement are input into the SQL verification model to obtain the target SQL statement corresponding to the query request text.

[0018] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, any one of the above-mentioned SQL statement generation methods is implemented.

[0019] The present invention also provides a non-transitory computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the method for generating an SQL statement as described above is implemented.

[0020] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements any of the above-mentioned SQL statement generation methods.

[0021] The SQL statement generation method, device, electronic device and storage medium provided by the present invention are that the SQL statement correction model and the verification model are both trained based on the verification training data generated by the temporary generation model, and the temporary generation model is trained based on N-1 generation training data. By using the target generation training data that is not used to train the temporary generation model in the N generation training data as test data and inputting it into the temporary generation model, the verification training data is obtained, which can be directly used as the supervised learning data of the verification model, reducing the workload of manual annotation, and through the training of the open source large language model, the accuracy of the SQL statement correction model and the SQL verification model can be effectively guaranteed, and the target generation training data that is not used to train the temporary generation model is input into the temporary generation model as test data, so as to obtain the verification training data. The user's query request text and related database information are used as input to generate one or more candidate SQL statements. The correction model will identify the candidate SQL statements that cannot be executed in the candidate SQL statements, and try to correct these problems to make them SQL statements that can be executed normally. The multiple executable SQL statements after correction will be sent to the SQL verification model. The verification model can accurately determine whether the execution result of the SQL statement meets the user's query intention. In the present invention, the accuracy and practicality of the text-to-SQL statement conversion are significantly improved through the mutual cooperation of the generation model, the SQL statement correction model and the SQL verification model, ensuring that the target SQL statement finally generated can accurately meet the user's needs. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0023] Figure 1 It is a flow chart of the SQL statement generation method provided by the present invention; Figure 2 A schematic diagram of the verification training data construction process provided by the present invention; Figure 3 The overall flow chart provided by the present invention; Figure 4 A schematic diagram of the structure of the SQL statement generating device provided by the present invention; Figure 5 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION

[0024] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0025] In related technologies, text-to-SQL statements are mostly based on large language models (LLMs) to understand the user's demand text and generate SQL statements. In order to adapt these large language models to specific application fields, training data sets in related fields are usually prepared to fine-tune existing open source large language models.

[0026] However, even so, these models still have certain limitations and problems in practical applications. For example, when processing complex query requirements, the model may generate SQL statements that do not conform to actual syntax or logic, resulting in query results that do not meet user needs. Therefore, how to improve the accuracy of generated SQL statements has become a key challenge for the further development and improvement of text-to-SQL statement technology.

[0027] For example, using a closed-source large language model has obvious problems in terms of cost and data security. Every time you call the API interface of a closed-source model, a certain fee will be incurred, which will incur huge costs for an application that needs to be used for a long time. In addition, when calling the API to generate the modified SQL, it is necessary to upload database information, including: database structure, sample data of the database, etc. If the database involves confidential or private data, it will lead to the leakage of important data.

[0028] Figure 1 It is a flow chart of the SQL statement generation method provided by the present invention, such as Figure 1 As shown, the method includes the following: Step 110, inputting the query request text and database information into the generation model to obtain at least one candidate SQL statement corresponding to the query request text; In the present invention, the query request text specifically refers to the database query demand expressed by the user in natural language. For example, the user may enter "find all customers whose sales in 2023 exceed 1 million".

[0029] In the present invention, database information may refer to information about the database structure, including table names, column names, data types, relationships between tables, etc. This information helps the generation model understand how to convert natural language queries into SQL statements.

[0030] In the present invention, the generative model is a trained large language model specifically used to convert natural language queries into SQL statements. The generative model generates one or more candidate SQL statements by understanding the query request text and database information.

[0031] More specifically, the generative model is trained by query request text samples and database information samples with correct SQL statement labels, and the data is divided into training set, validation set and test set. The model initialization phase uses the pre-trained large language model as the base model, and fine-tunes the model according to task requirements to adapt it to the Text-to-SQL task.

[0032] During the training process, natural language query request text and database information samples are spliced ​​as model inputs. The model generates candidate SQL statements and compares the differences between the generated subsequent SQL statements and the correct SQL statements through the cross-entropy loss function to optimize the model. After the training is completed, the final performance of the model is evaluated on the test set to ensure that the model performs well on unseen data. Indicators such as execution accuracy and grammatical correctness are used to evaluate the quality of generated SQL statements, and finally a generative model is obtained. The generative model can gradually learn to accurately convert natural language queries into SQL statements.

[0033] In the present invention, candidate SQL statements are multiple possible SQL statements generated by the generation model according to the query request text and database information. These candidate SQL statements may differ in syntax, logic or execution results, and the subsequent steps will verify and select these candidate SQL statements.

[0034] Step 120, correcting the unexecutable candidate SQL statements according to the SQL statement correction model to obtain executable candidate SQL statements; In the present invention, the SQL statement correction model is a model that identifies errors in candidate SQL statements (such as syntax errors, logic errors, or errors that do not match the database schema) and corrects them so that they can be executed correctly.

[0035] In the present invention, the unexecutable candidate SQL statement is an SQL statement that cannot be successfully executed in the database due to a syntax error or an error occurring during execution.

[0036] The correction model analyzes the error type of the candidate SQL statement and modifies the SQL statement based on the database information. The correction may include fixing syntax errors, adjusting query logic, and completing missing information.

[0037] For example, to fix a syntax error: replace SELECT FROM Customers WHERE Sales>1000000 is changed to SELECT FROM Customers WHERE Sales>1000000 AND Year = 2023.

[0038] Complete the missing information: Replace SELECT FROM Customers WHERE Sales>? is modified to SELECT FROM Customers WHERE Sales>1000000.

[0039] The correction model outputs the corrected SQL statements, which are syntactically and logically correct, match the database schema, and can be successfully executed in the database.

[0040] Step 130, inputting each executable candidate SQL statement into an SQL verification model to generate a target SQL statement corresponding to the query request text; The SQL statement correction model and the SQL verification model are both obtained by training based on verification training data generated by a temporary generation model, and the temporary generation model is obtained by training based on N-1 generation training data, where N is a positive integer greater than 1.

[0041] In the present invention, the temporary generation model is an intermediate generation model used to generate verification training data. The temporary generation model can convert natural language query requests into SQL statements by learning these training data. Since N-1 data are used in each training, the remaining 1 data can be used as a verification set to generate verification training data.

[0042] The target generated training data that is not used to train the temporary generation model among the N generated training data is used as test data. These test data are input into the temporary generation model, and the temporary generation model generates multiple candidate SQL statements. These candidate SQL statements are executed to determine whether they are executable. In this way, a series of unexecutable SQL statements and executable SQL statements can be obtained, and these data are used as verification training data to train the SQL statement correction model and the SQL verification model.

[0043] In the present invention, the SQL verification model evaluates the input candidate SQL statements and selects the SQL statement that best meets the intent of the query request text as the target SQL statement.

[0044] In the present invention, executable candidate SQL statements are SQL statements that can be successfully executed in the database after being processed by the modified model. These SQL statements are correct in syntax and logic and match the database schema.

[0045] In the present invention, executable candidate SQL statements and query request text are input into the SQL verification model. The query request text is used to help the model understand the user's query intention. Through the screening of the SQL verification model, it is ensured that the target SQL statement finally generated can not only be executed normally, but also can accurately meet the user's query intention, thereby improving the accuracy and practicality of the text-to-SQL statement and enhancing the semantic understanding and intelligent decision-making capabilities of the system.

[0046] In the present invention, both the SQL statement correction model and the verification model are trained based on the verification training data generated by the temporary generation model, and the temporary generation model is trained based on N-1 generation training data. By using the target generation training data that is not used to train the temporary generation model in the N generation training data as test data and inputting it into the temporary generation model, the verification training data is obtained, which can be directly used as the supervised learning data of the verification model, thereby reducing the workload of manual annotation and effectively ensuring the accuracy of the SQL statement correction model and the SQL verification model. In addition, the generation model receives the user's query request text and related database information as input to generate one or more candidate SQL statements. The correction model will identify the candidate SQL statements that cannot be executed in the candidate SQL statements, and try to correct these problems to make them SQL statements that can be executed normally. After correction, the multiple executable SQL statements will be sent to the SQL verification model, and the verification model can accurately determine whether the SQL statement execution result meets the user's query intention. In the present invention, the mutual cooperation of the generation model, the SQL statement correction model and the SQL verification model significantly improves the accuracy and practicality of the text-converted SQL statement, ensuring that the target SQL statement finally generated can accurately meet the user's needs.

[0047] Optionally, the step of correcting the unexecutable candidate SQL statements according to the SQL statement correction model to obtain executable candidate SQL statements includes: Execute each of the candidate SQL statements, and determine the candidate SQL statements that cannot be executed according to the execution results; Inputting the unexecutable candidate SQL statements into the SQL statement correction model, and outputting the corrected candidate SQL statements; Continue to execute the corrected candidate SQL statements, determine the unexecutable candidate SQL statements according to the execution results, and continue to input the unexecutable candidate SQL statements into the SQL statement correction model until all candidate SQL statements are executable, thereby obtaining executable candidate SQL statements.

[0048] In the present invention, the candidate SQL statements generated by the model output are executed in the target database to observe the execution results. If the SQL statement can be successfully executed and return results, it is marked as executable; if the execution fails, such as syntax errors or execution errors that do not match the database schema, it is marked as unexecutable.

[0049] Based on the execution results, select candidate SQL statements that cannot be executed. These SQL statements may fail to execute due to syntax errors, logical errors, or incompatibility with the database schema.

[0050] In the present invention, a syntax error refers to an SQL statement that does not conform to the syntax rules. This type of syntax error can be found without execution or database information, and is therefore the easiest error to find and modify. For example, if there are special symbols such as '-' in the table name or column name, or the table name or column name is a SQL reserved keyword, such as "order", it needs to be wrapped in quotation marks.

[0051] Runtime errors refer to errors in the SQL itself when it is executed on a given database, even though there are no syntax errors in the SQL itself. The discovery and modification of this type of error needs to be determined based on the given database structure. Since the error message when executing SQL will indicate the cause of the runtime error, this type of error is easy to find and relatively easy to modify. For example, the table name or column name in the SQL query does not exist.

[0052] The candidate SQL statements that cannot be executed are input into the SQL statement correction model. The correction model corrects the SQL statement by analyzing the error type of the SQL statement and combining the database information (such as table name, column name, data type, etc.).

[0053] The SQL statement correction model outputs the corrected candidate SQL statement. Corrections may include fixing syntax errors, adjusting query logic, completing missing information, etc., so that the SQL statement can be executed correctly. Execute the corrected candidate SQL statement again in the target database and observe the execution results. If the SQL statement still cannot be executed, continue to input it into the SQL statement correction model for correction.

[0054] In the present invention, the candidate SQL statements that cannot be executed are gradually corrected into executable SQL statements through repeated execution and correction. The process is iterated continuously until all candidate SQL statements can be successfully executed. The SQL statement correction model can gradually correct the candidate SQL statements that cannot be executed into executable SQL statements, thereby improving the practicality and reliability of the generated model.

[0055] Optionally, before the step of correcting the unexecutable candidate SQL statements according to the SQL statement correction model to obtain executable candidate SQL statements, the step further includes: Based on N-1 generated training data, a temporary generated model is trained to obtain a temporary generated model corresponding to the i-th target generated training data; wherein the N-1 generated training data are obtained by removing the i-th target generated training data from the N generated training data; each generated training data includes: a query request text sample and a database information sample carrying a correct SQL statement label; i is a positive integer; The i-th target generation training data is used as test data, and input into a temporary generation model corresponding to the i-th target generation training data, to obtain verification training data corresponding to the i-th target generation training data; Traverse N different target-generated training data, input each target-generated training data into the corresponding temporary generation model as test data, and obtain multiple verification training data.

[0056] In the present invention, the generated training data is a data set used to train the generated model. Each generated training data carries a query request text sample with a correct SQL statement label, and a database information sample related to the query request, including table name, column name, data type, etc.

[0057] In the present invention, before using the SQL statement correction model to correct the candidate SQL statements that cannot be executed, a temporary generation model needs to be trained first. Specifically, the temporary generation model is trained based on N-1 generated training data to obtain a temporary generation model corresponding to the i-th target generated training data. The N-1 generated training data mentioned here refers to the data remaining after removing the i-th target generated training data from the total N generated training data. Each generated training data contains a query request text sample carrying a correct SQL statement label and a corresponding database information sample, where i is a positive integer.

[0058] Figure 2 The schematic diagram of the verification training data construction process provided by the present invention is as follows: Figure 2As shown in the figure, it is assumed that there are 3 (N=3) generated training data, namely training set 1, training set 2 and training set 3. When i=1, remove training set 1, use training set 2 and training set 3 to train the temporary generation model, that is, the temporary generator, and obtain the temporary generation model corresponding to training set 1; when i=2, remove training set 2, use training set 1 and training set 3 to train the temporary generation model, and obtain the temporary generation model corresponding to training set 2; when i=3, remove training set 3, use training set 1 and training set 2 to train the temporary generation model, and obtain the temporary generation model corresponding to training set 3.

[0059] The i-th target generated training data is used as test data and input into the corresponding temporary generation model, so as to obtain the verification training data corresponding to the i-th target generated training data. Then, all N different target generated training data are traversed, and each target generated training data is input into the corresponding temporary generation model in turn, and finally multiple verification training data are obtained.

[0060] For example, according to the above example, after obtaining the temporary generation models corresponding to training set 1, training set 2, and training set 3, respectively, training set 1, training set 2, and training set 3 are respectively input into the corresponding temporary generation models as test data. Assume that after training set 1 is input into the corresponding temporary generation model as test data, verification training data A is obtained; after training set 2 is input into the corresponding temporary generation model as test data, verification training data B is obtained; after training set 3 is input into the corresponding temporary generation model as test data, verification training data C is obtained. In this way, by traversing 3 target generation training data, 3 sets of verification training data A, B, and C are obtained.

[0061] In the present invention, by training a temporary generation model based on N-1 generation training data and using the model to obtain verification training data, it is possible to fully utilize the existing generation training data without using additional data, thereby improving data utilization efficiency and reducing data waste. In addition, the temporary generation model trained using different target generation training data can learn useful information from different angles and data distributions, thereby generating more diverse and representative verification training data. When these verification training data are used to train the SQL statement correction model and the verification model, the model can learn richer features and patterns, thereby improving the performance and generalization ability of the model.

[0062] Optionally, the verification training data includes: correct SQL statement samples, unexecutable SQL statement samples, and logically incorrect SQL statement samples.

[0063] In the present invention, the correct SQL statement sample is an SQL statement that can be correctly executed and the result meets the expectation, and can be used to train the verification model so that it can identify and select the correct SQL statement.

[0064] In the present invention, the unexecutable SQL statement samples are SQL statements that cannot be executed due to syntax errors or runtime errors, and can be used to train the SQL statement correction model to enable it to identify and correct these errors so that the SQL statement can be executed normally.

[0065] In the present invention, the logical error SQL statement sample is an SQL statement that can be executed but the result does not meet the user's query intention, that is, there is a logical error. It can be used to train the verification model so that it can identify and exclude these logical error SQL statements and select the SQL statement that best meets the user's query intention.

[0066] Optionally, the training method of the SQL statement correction model specifically includes: Based on the unexecutable SQL statement samples carrying correct SQL labels, model training is performed to obtain a SQL statement correction model; The SQL statement correction model is used to modify an input SQL statement that cannot be executed into an executable SQL statement.

[0067] In the present invention, an unexecutable SQL statement refers to a SQL statement that has a syntax error or a runtime error, or cannot be normally executed in the database. The correct SQL statement label is a SQL statement that corresponds to the unexecutable SQL statement and can be correctly executed and meets the query requirements.

[0068] In the present invention, the SQL statements that cannot be executed are used as the input of the model, and the correct SQL statement labels are used as the output of the model to perform model training.

[0069] Through a large number of training samples, the model learns how to modify the input SQL statements that cannot be executed into executable SQL statements. The model automatically learns the grammatical structure of SQL statements, database constraints and other knowledge, so that it can identify and correct errors in input SQL statements.

[0070] By optimizing the algorithm, the model parameters are continuously adjusted to minimize the difference between the model's predicted output and the correct SQL statement label, thereby improving the model's correction accuracy.

[0071] In an optional embodiment, for the training prompt word template of the large language model, it can be specifically as follows: you are a SQL modifier, and you need to modify a given unexecutable SQL statement sample into an executable SQL statement based on given database information and user query request text.

[0072] In the present invention, the SQL statement correction model obtained through training can automatically correct unexecutable SQL statements into executable SQL statements, thereby improving the executability of SQL statements and reducing query failures caused by syntax errors or runtime errors.

[0073] Optionally, the training method of the SQL verification model specifically includes: Based on the correct SQL statement samples carrying the correct SQL tags and the logically incorrect SQL statement samples, model training is performed to obtain the SQL verification model; The SQL verification model is used to select a correct target SQL statement from multiple input SQL statements.

[0074] In the present invention, a correct SQL statement sample refers to an SQL statement that can be correctly executed and whose result meets expectations, and has a correct SQL tag.

[0075] Logically incorrect SQL statement samples refer to SQL statements that can be executed but whose results do not meet the user's query intent and have incorrect SQL tags.

[0076] In the present invention, multiple SQL statements are used as the input of the model, and the correct SQL tags are used as the output of the model to perform model training.

[0077] Through a large number of training samples, the model learns how to select the correct target SQL statement from multiple input SQL statements. The model automatically learns the grammatical structure of SQL statements, database constraints, and common types of logical errors, so that it can identify and select the SQL statement that best meets the user's query intent.

[0078] During the training process, the model parameters are continuously adjusted through the optimization algorithm to minimize the difference between the model's predicted output and the correct SQL label, thereby improving the model's selection accuracy.

[0079] In an optional embodiment, the training prompt word template for the large language model may specifically be: you are a SQL verifier, and you need to select the correct SQL as the output result from the candidate SQLs based on the given database information and user query questions.

[0080] In the present invention, the SQL verification model obtained through training can accurately select the correct SQL statement that meets the user's query intention from multiple candidate SQL statements, thereby improving the accuracy of SQL statement selection and reducing the problem of inaccurate query results caused by logical errors.

[0081] Optionally, the step of inputting each executable candidate SQL statement into a SQL verification model to generate a target SQL statement corresponding to the query request text includes: Back-translate each of the candidate SQL statements into a natural language text to obtain a natural language interpretation text of each of the candidate SQL statements; The natural language interpretation text of each candidate SQL statement and each candidate SQL statement are input into the SQL verification model to obtain the target SQL statement corresponding to the query request text.

[0082] In the present invention, a back-translation module is used to translate each candidate SQL statement back to the user's query question. The back-translation module is a trained model that can interpret the user's query intention based on database information and a given SQL statement and generate a corresponding natural language text.

[0083] Each executable candidate SQL statement is translated back into a natural language text to obtain a natural language interpretation text of each candidate SQL statement. This can better understand the actual meaning of each candidate SQL statement so that the subsequent verification model can more accurately determine whether it meets the user's query intent.

[0084] The natural language interpretation text of each candidate SQL statement, as well as each candidate SQL statement itself, are input into the SQL verification model together.

[0085] The SQL validation model will comprehensively consider the natural language interpretation text of the candidate SQL statements and the SQL statements themselves to determine which SQL statement best meets the user's query intent, thereby selecting the correct target SQL statement.

[0086] In the present invention, after the judgment and selection of the verification model, a target SQL statement corresponding to the query request text is finally generated. This target SQL statement can not only be executed correctly, but also its query result can accurately meet the query requirements of the user.

[0087] In the present invention, the verification is performed by combining the natural language interpretation text and the SQL statement itself, which can more comprehensively evaluate whether the candidate SQL statement meets the user's query intention, improve the selection accuracy of the verification model, and ensure that the target SQL statement finally generated can accurately meet the user's needs.

[0088] Figure 3 The overall flow chart provided by the present invention is as follows: Figure 3As shown in the figure, in the training phase, the training data set includes the generated training data of multiple training sets (training set 1, training set 2, and training set 3). One training set (such as training set 1) is removed from the multiple training sets, and the remaining training sets (training set 2 and training set 3) are used to train the temporary generation model. The removed training set (training set 1) is used as test data and input into the trained temporary generation model to generate multiple candidate SQL statements.

[0089] Verifier training,classifies the candidate SQL statements generated by the temporary generation model,,including data of syntax errors, execution errors, and logic errors,,as training data for the verification model.

[0090] The SQL verification model uses the verifier training data to train the verification model, so that it can select the correct target SQL statement from multiple candidate SQL statements.

[0091] The SQL statement correction model uses training data containing syntax errors and execution errors to train the correction model, so that it can correct unexecutable SQL statements into executable SQL statements.

[0092] In the generation phase, the generation model receives the user's query request text and database information and generates multiple candidate SQL statements; The execution module executes the generated candidate SQL statements to determine whether they can run normally. If the SQL statement cannot be executed, it is marked as a running error.

[0093] Input the candidate SQL statements that cannot be executed into the correction model, and output the corrected executable SQL statements. Continue to execute the corrected SQL statements. If there are still running errors, continue to correct them until all candidate SQL statements are executable.

[0094] The back-translation module back-translates the executable candidate SQL statements into natural language texts to obtain natural language interpretation texts of each candidate SQL statement.

[0095] The SQL verification model inputs the natural language interpretation text of each candidate SQL statement, as well as each candidate SQL statement itself, into the verification model, and selects the SQL statement that best meets the user's query intent as the final target SQL statement.

[0096] The SQL statement generating device provided by the present invention is described below. The SQL statement generating device described below and the SQL statement generating method described above can be referenced to each other.

[0097] Figure 4 The structure diagram of the SQL statement generating device provided by the present invention is as follows: Figure 4 As shown, including: The input module 410 is used to input the query request text and database information into the generation model to obtain at least one candidate SQL statement corresponding to the query request text; The correction module 420 is used to correct the unexecutable candidate SQL statements according to the SQL statement correction model to obtain executable candidate SQL statements; The generation module 430 is used to input each executable candidate SQL statement into the SQL verification model to generate a target SQL statement corresponding to the query request text; The SQL statement correction model and the SQL verification model are both obtained by training based on verification training data generated by a temporary generation model, and the temporary generation model is obtained by training based on N-1 generation training data, where N is a positive integer greater than 1.

[0098] According to the SQL statement generating device provided by the present invention, the device is also used for: Execute each of the candidate SQL statements, and determine the candidate SQL statements that cannot be executed according to the execution results; Inputting the unexecutable candidate SQL statements into the SQL statement correction model, and outputting the corrected candidate SQL statements; Continue to execute the corrected candidate SQL statements, determine the unexecutable candidate SQL statements according to the execution results, and continue to input the unexecutable candidate SQL statements into the SQL statement correction model until all candidate SQL statements are executable, thereby obtaining executable candidate SQL statements.

[0099] According to the SQL statement generating device provided by the present invention, the device is also used for: Based on N-1 generated training data, a temporary generated model is trained to obtain a temporary generated model corresponding to the i-th target generated training data; wherein the N-1 generated training data are obtained by removing the i-th target generated training data from the N generated training data; each generated training data includes: a query request text sample and a database information sample carrying a correct SQL statement label; i is a positive integer; The i-th target generation training data is used as test data, and input into a temporary generation model corresponding to the i-th target generation training data, to obtain verification training data corresponding to the i-th target generation training data; Traverse N different target-generated training data, input each target-generated training data into the corresponding temporary generation model as test data, and obtain multiple verification training data.

[0100] According to the SQL statement generation device provided by the present invention, the verification training data includes: correct SQL statement samples, unexecutable SQL statement samples, and logically incorrect SQL statement samples.

[0101] According to the SQL statement generating device provided by the present invention, the device is also used for: Based on the correct SQL statement samples carrying the correct SQL tags and the logically incorrect SQL statement samples, model training is performed to obtain the SQL verification model; The SQL verification model is used to select a correct target SQL statement from multiple input SQL statements.

[0102] According to the SQL statement generating device provided by the present invention, the device is also used for: Back-translate each of the candidate SQL statements into a natural language text to obtain a natural language interpretation text of each of the candidate SQL statements; The natural language interpretation text of each candidate SQL statement and each candidate SQL statement are input into the SQL verification model to obtain the target SQL statement corresponding to the query request text.

[0103] In the present invention, both the SQL statement correction model and the verification model are trained based on the verification training data generated by the temporary generation model, and the temporary generation model is trained based on N-1 generation training data. By using the target generation training data that is not used to train the temporary generation model in the N generation training data as test data and inputting it into the temporary generation model, the verification training data is obtained, which can be directly used as the supervised learning data of the verification model, thereby reducing the workload of manual annotation and effectively ensuring the accuracy of the SQL statement correction model and the SQL verification model. In addition, the generation model receives the user's query request text and related database information as input to generate one or more candidate SQL statements. The correction model will identify the candidate SQL statements that cannot be executed in the candidate SQL statements, and try to correct these problems to make them SQL statements that can be executed normally. After correction, the multiple executable SQL statements will be sent to the SQL verification model, and the verification model can accurately determine whether the SQL statement execution result meets the user's query intention. In the present invention, the mutual cooperation of the generation model, the SQL statement correction model and the SQL verification model significantly improves the accuracy and practicality of the text-converted SQL statement, ensuring that the target SQL statement finally generated can accurately meet the user's needs.

[0104] Figure 5 is a schematic diagram of the structure of the electronic device provided by the present invention, such as Figure 5As shown, the electronic device may include: a processor 510, a communication interface 520, a memory 530 and a communication bus 540, wherein the processor 510, the communication interface 520 and the memory 530 communicate with each other through the communication bus 540. The processor 510 may call the logic instructions in the memory 530 to execute the SQL statement generation method, which includes: inputting the query request text and the database information into the generation model to obtain at least one candidate SQL statement corresponding to the query request text; According to the SQL statement correction model, the candidate SQL statements that cannot be executed are corrected to obtain executable candidate SQL statements; Inputting each executable candidate SQL statement into an SQL verification model to generate a target SQL statement corresponding to the query request text; The SQL statement correction model and the SQL verification model are both obtained by training based on verification training data generated by a temporary generation model, and the temporary generation model is obtained by training based on N-1 generation training data, where N is a positive integer greater than 1.

[0105] In addition, the logic instructions in the above-mentioned memory 530 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.

[0106] On the other hand, the present invention further provides a computer program product, the computer program product includes a computer program, the computer program can be stored in a non-transitory computer-readable storage medium, when the computer program is executed by a processor, the computer can execute the SQL statement generation method provided by the above methods, the method comprising: inputting a query request text and database information into a generation model, obtaining at least one candidate SQL statement corresponding to the query request text; According to the SQL statement correction model, the candidate SQL statements that cannot be executed are corrected to obtain executable candidate SQL statements; Inputting each executable candidate SQL statement into an SQL verification model to generate a target SQL statement corresponding to the query request text; The SQL statement correction model and the SQL verification model are both obtained by training based on verification training data generated by a temporary generation model, and the temporary generation model is obtained by training based on N-1 generation training data, where N is a positive integer greater than 1.

[0107] In another aspect, the present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the method for generating SQL statements provided by the above methods is implemented, the method comprising: inputting a query request text and database information into a generation model to obtain at least one candidate SQL statement corresponding to the query request text; According to the SQL statement correction model, the candidate SQL statements that cannot be executed are corrected to obtain executable candidate SQL statements; Inputting each executable candidate SQL statement into an SQL verification model to generate a target SQL statement corresponding to the query request text; The SQL statement correction model and the SQL verification model are both obtained by training based on verification training data generated by a temporary generation model, and the temporary generation model is obtained by training based on N-1 generation training data, where N is a positive integer greater than 1.

[0108] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0109] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0110] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A method for generating a SQL statement, characterized in that: include: Inputting the query request text and database information into the generation model to obtain at least one candidate SQL statement corresponding to the query request text; According to the SQL statement correction model, the unexecutable candidate SQL statements are corrected to obtain executable candidate SQL statements; Inputting each executable candidate SQL statement into an SQL verification model to generate a target SQL statement corresponding to the query request text; The SQL statement correction model and the SQL verification model are both obtained by training based on verification training data generated by a temporary generation model, and the temporary generation model is obtained by training based on N-1 generation training data, where N is a positive integer greater than 1.

2. The SQL statement generation method according to claim 1, characterized in that: The step of correcting the unexecutable candidate SQL statements according to the SQL statement correction model to obtain executable candidate SQL statements includes: Execute each of the candidate SQL statements, and determine the candidate SQL statements that cannot be executed according to the execution results; Inputting the unexecutable candidate SQL statements into the SQL statement correction model, and outputting the corrected candidate SQL statements; Continue to execute the corrected candidate SQL statements, determine the unexecutable candidate SQL statements according to the execution results, and continue to input the unexecutable candidate SQL statements into the SQL statement correction model until all candidate SQL statements are executable, thereby obtaining executable candidate SQL statements.

3. The SQL statement generation method according to claim 1, characterized in that: Before the step of correcting the unexecutable candidate SQL statements according to the SQL statement correction model to obtain executable candidate SQL statements, the method further includes: Based on N-1 generated training data, a temporary generated model is trained to obtain a temporary generated model corresponding to the i-th target generated training data; wherein the N-1 generated training data are obtained by removing the i-th target generated training data from the N generated training data; each generated training data includes: a query request text sample and a database information sample carrying a correct SQL statement label; i is a positive integer; The i-th target generation training data is used as test data, and input into a temporary generation model corresponding to the i-th target generation training data, to obtain verification training data corresponding to the i-th target generation training data; Traverse N different target-generated training data, input each target-generated training data into the corresponding temporary generation model as test data, and obtain multiple verification training data.

4. The SQL statement generation method according to claim 3, characterized in that: The verification training data includes: correct SQL statement samples, unexecutable SQL statement samples, and logically incorrect SQL statement samples.

5. The SQL statement generation method according to claim 4, characterized in that: The training method of the SQL statement correction model specifically includes: Based on the unexecutable SQL statement samples carrying correct SQL labels, model training is performed to obtain a SQL statement correction model; The SQL statement correction model is used to modify an input SQL statement that cannot be executed into an executable SQL statement.

6. The SQL statement generation method according to claim 4, characterized in that: The training method of the SQL verification model specifically includes: Based on the correct SQL statement samples carrying the correct SQL tags and the logically incorrect SQL statement samples, model training is performed to obtain the SQL verification model; The SQL verification model is used to select a correct target SQL statement from multiple input SQL statements.

7. The SQL statement generation method according to claim 1, characterized in that: The step of inputting each executable candidate SQL statement into a SQL verification model to generate a target SQL statement corresponding to the query request text includes: Back-translate each of the candidate SQL statements into a natural language text to obtain a natural language interpretation text of each of the candidate SQL statements; The natural language interpretation text of each candidate SQL statement and each candidate SQL statement are input into the SQL verification model to obtain the target SQL statement corresponding to the query request text.

8. A SQL statement generating device, characterized in that: include: An input module, used to input the query request text and database information into the generation model to obtain at least one candidate SQL statement corresponding to the query request text; A correction module, used to correct the unexecutable candidate SQL statements according to the SQL statement correction model to obtain executable candidate SQL statements; A generation module, used for inputting each executable candidate SQL statement into an SQL verification model to generate a target SQL statement corresponding to the query request text; The SQL statement correction model and the SQL verification model are both obtained by training based on verification training data generated by a temporary generation model, and the temporary generation model is obtained by training based on N-1 generation training data, where N is a positive integer greater than 1.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the SQL statement generating method according to any one of claims 1 to 7 is implemented.

10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method for generating an SQL statement according to any one of claims 1 to 7 is implemented.

Citation Information

Patent Citations

  • SQL (Structured Query Language) statement verification method and device based on large model

    CN118779229A

  • SQL (Structured Query Language) generation and error correction method based on multi-stage feedback

    CN119474129A