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

Through correlation matching and difficulty analysis with SQL statement sample in the historical database, suitable prompt words are constructed to improve the accuracy of SQL statement error correction, and the problem of low accuracy of complex SQL statement error correction in the prior art is solved.

CN119807219APending Publication Date: 2025-04-11CHINA TELECOM ARTIFICIAL INTELLIGENCE TECHNOLOGY (BEIJING) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411805104.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-09
Publication Date
2025-04-11

AI Technical Summary

Technical Problem

In the prior art, the SQL statement error correction method has a low accuracy rate for complex SQL statements.

Method used

By obtaining the correlation score between SQL statements and SQL statement samples in the historical database, select samples with high correlation as candidate samples, and select prompt samples based on the difficulty type of SQL statements, build prompt words, and input them into the model for correction.

Benefits of technology

It improves the accuracy of error correction for complex SQL statements, can dynamically adjust the construction of prompt words, and adapt to SQL statements of different difficulties, thereby improving the accuracy of correction.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119807219A_ABST
    Figure CN119807219A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides an SQL (Structured Query Language) statement processing method and device, electronic equipment and a storage medium. The method comprises the following steps: acquiring a first SQL statement; determining a correlation score between the first SQL statement and each SQL statement sample in a historical database; obtaining SQL statement samples with the correlation scores greater than a first threshold value in a historical database as candidate samples; determining a difficulty level type of the first SQL statement; according to the difficulty level type, selecting at least part of the samples from the candidate samples as prompt samples; on the basis of the prompt sample, constructing a prompt word; and inputting the first SQL statement and the cue word into the first model to obtain a second SQL statement after the first SQL statement is corrected. Therefore, according to the embodiment of the invention, the problem that an SQL statement error correction method in the prior art is relatively low in error correction accuracy on complex SQL statements can be solved.
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 processing SQL statements. Background Art

[0002] Structured Query Language (SQL) is a programming language specifically designed to manage and operate relational database management systems. It is widely used to create, query, update, and manage data in databases. SQL statements are instructions for performing these operations, and they follow specific syntax rules.

[0003] SQL error correction refers to identifying and correcting errors in SQL statements, which may be syntax errors, logic errors, or performance issues. Currently, SQL error correction can be performed using a large model of zero-shot prompt learning or a large model of static prompt learning.

[0004] For zero-shot learning, i.e., zero-shot prompt model, no examples need to be given to the large model, and the large model can directly control the output. The reason for this ability is that the large model has trained a large amount of data and has a certain reasoning ability, so it can achieve some zero-shot tasks. However, for large-model SQL error correction of zero-shot prompt learning, its effect is highly dependent on the training corpus of the large model, and its understanding and generalization ability of SQL semantics are poor, and it often performs poorly on task sets with a certain degree of difficulty.

[0005] For static prompt learning, that is, based on prompt learning, the samples given for different business inputs are fixed. However, for large-model SQL error correction in static prompt learning, the existing solutions only use simple prompt words and fixed prompt samples to prompt large models. This simple technical solution can often only solve simple SQL error correction problems, and complex syntax errors and data type errors cannot be solved.

[0006] It can be seen from this that the SQL statement error correction method in the prior art has a low error correction accuracy for complex SQL statements. Summary of the invention

[0007] The embodiments of the present application provide a method, device, electronic device and storage medium for processing SQL statements to solve the problem that the SQL statement error correction method in the prior art has a low error correction accuracy for complex SQL statements.

[0008] In a first aspect, an embodiment of the present application provides a method for processing a structured query language SQL statement, the method comprising:

[0009] Get the first SQL statement;

[0010] Determine a correlation score between the first SQL statement and each SQL statement sample in the history database;

[0011] Acquire SQL statement samples with a relevance score greater than a first threshold in the historical database as candidate samples;

[0012] Determining the difficulty level type of the first SQL statement;

[0013] According to the difficulty type, select at least some of the candidate samples as prompt samples;

[0014] Constructing a prompt word based on the prompt sample;

[0015] The first SQL statement and the prompt word are input into a first model to obtain a second SQL statement after correcting the first SQL statement.

[0016] Optionally, determining the correlation score between the first SQL statement and each SQL statement sample in the history database includes:

[0017] Acquire first parameter information of the first SQL statement, wherein the first parameter information includes syntax error information and architecture information of an associated table;

[0018] Determine a correlation score between the first SQL statement and each SQL statement sample in the historical database according to the first parameter information of the first SQL statement and the first parameter information of each SQL statement sample in the historical database.

[0019] Optionally, determining the difficulty type of the first SQL statement includes:

[0020] Inputting the first parameter information of the first SQL statement and the first SQL statement into a classification model to obtain a difficulty level type of the first SQL statement;

[0021] The first parameter information includes syntax error information and architecture information of the association table.

[0022] Optionally, selecting at least some of the candidate examples as prompt examples according to the difficulty level type includes:

[0023] When the difficulty level type is a target type and the number of the candidate examples is greater than the target predetermined number corresponding to the target type, selecting the target predetermined number of examples from the candidate examples as the prompt examples in descending order of relevance scores;

[0024] When the difficulty level type is the target type and the number of the candidate examples is less than or equal to the target predetermined number, selecting the candidate examples as the prompt examples;

[0025] Among them, different difficulty levels correspond to different reservation numbers. The greater the difficulty level corresponding to the difficulty level type, the greater the corresponding reservation number.

[0026] Optionally, constructing a prompt word based on the prompt sample includes:

[0027] Sorting the prompt samples in ascending order of relevance scores;

[0028] Based on the sorted prompt examples, construct prompt words.

[0029] Optionally, constructing prompt words based on the sorted prompt examples includes:

[0030] Constructing a prompt word based on the sorted prompt samples and the first parameter information of the first SQL statement;

[0031] The first parameter information includes syntax error information and architecture information of the association table.

[0032] Optionally, the method further comprises:

[0033] Performing syntax check on the second SQL statement;

[0034] If there is a syntax error in the second SQL statement, correct the second SQL statement;

[0035] When there is no syntax error in the second SQL statement, the first SQL statement is stored in the history database as an SQL statement sample.

[0036] In a second aspect, an embodiment of the present application provides a structured query language SQL statement processing device, the device comprising:

[0037] A first acquisition module, used to acquire a first SQL statement;

[0038] A first determination module, used to determine the correlation score between the first SQL statement and each SQL statement sample in the historical database;

[0039] A second acquisition module is used to acquire SQL statement samples with a relevance score greater than a first threshold in the historical database as candidate samples;

[0040] A second determination module, used to determine the difficulty type of the first SQL statement;

[0041] A selection module, configured to select at least some of the candidate samples as prompt samples according to the difficulty level type;

[0042] A construction module, used for constructing a prompt word based on the prompt sample;

[0043] The correction module is used to input the first SQL statement and the prompt word into the first model to obtain a second SQL statement after correcting the first SQL statement.

[0044] In a third aspect, an embodiment of the present application provides an electronic device, including a memory, a transceiver, and a processor:

[0045] A memory for storing computer programs; a transceiver for sending and receiving data under the control of the processor; and a processor for reading the computer program in the memory and executing the method for processing structured query language SQL statements described in the first aspect above.

[0046] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and the computer program is used to enable the processor to execute the method for processing structured query language SQL statements described in the first aspect above.

[0047] In an embodiment of the present application, after obtaining the first SQL statement, it is possible to determine the correlation scores between the first SQL statement and each SQL statement sample in the historical database, thereby obtaining SQL statement samples in the historical database whose correlation scores are greater than a first threshold value as candidate samples; it is also possible to determine the difficulty type of the first SQL statement, thereby selecting at least some samples from the candidate samples as prompt samples based on the difficulty type, and constructing prompt words based on the prompt samples, and then the first SQL statement and the prompt word can be input into the first model to obtain a second SQL statement that is a correction of the first SQL statement.

[0048] Among them, in the model generation task, the prompt example is an important factor in guiding the model to react and generate inputs, and different prompt examples determine completely different outputs of the model. In the embodiment of the present application, when the first model is used to correct the first SQL statement, when selecting the prompt example used to construct the prompt word, not only the relevance score with the first SQL statement is considered, but also the difficulty type of the first SQL statement is considered. In this way, the constructed prompt word has a high relevance with the first SQL statement and conforms to the difficulty of the first SQL statement, so that the prompt word can guide the first model to better correct the first SQL statement.

[0049] It can be seen that the embodiments of the present application can dynamically adjust the prompt samples required to construct the prompt words according to the relevance and difficulty type to the SQL statement to be processed. In this way, even if the SQL statement to be processed is more difficult, suitable prompt words can be constructed, thereby improving the accuracy of correcting the SQL statement. BRIEF DESCRIPTION OF THE DRAWINGS

[0050] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the description of the embodiments of the present application will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.

[0051] Figure 1 A flowchart of a method for processing a structured query language SQL statement provided in an embodiment of the present application;

[0052] Figure 2 A flowchart of a specific implementation method of a method for processing a structured query language SQL statement according to an embodiment of the present application;

[0053] Figure 3 A structural block diagram of a device for processing structured query language SQL statements provided in an embodiment of the present application;

[0054] Figure 4 A structural block diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0055] In the embodiments of the present application, the term "and / or" describes the association relationship of the associated objects, indicating that three relationships may exist. For example, A and / or B may represent: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the associated objects before and after are in an "or" relationship.

[0056] In the embodiments of the present application, the term "plurality" refers to two or more than two, and other quantifiers are similar.

[0057] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0058] The embodiments of the present application provide a method and device for processing a structured query language SQL statement, so as to solve the problem that the SQL statement error correction method in the prior art has a low error correction accuracy for complex SQL statements.

[0059] Among them, the method and the device are based on the same application concept. Since the method and the device solve the problem in a similar principle, the implementation of the device and the method can refer to each other, and the repeated parts will not be repeated.

[0060] Figure 1 A flow chart of a method for processing a structured query language SQL statement provided in an embodiment of the present application is shown. The method may include the following steps 101 to 107:

[0061] Step 101: Obtain a first SQL statement.

[0062] The first SQL statement may be a SQL statement input by a user (i.e., a SQL statement written directly by a user); or it may be a SQL statement obtained through natural language conversion (e.g., a SQL statement output by a natural language conversion SQL (Natural Language to SQL, NL2SQL) large model).

[0063] Step 102: Determine the correlation score between the first SQL statement and each SQL statement sample in the history database.

[0064] The historical database includes at least one SQL statement sample.

[0065] In addition, in step 101, it is necessary to determine the relevance score between the first SQL statement and each SQL statement sample in the historical database. That is, each SQL statement sample in the historical database has a corresponding relevance score.

[0066] In addition, the relevance score is used to indicate the similarity between the first SQL statement and the SQL statement sample; the higher the relevance score, the higher the similarity; conversely, the lower the relevance score, the lower the similarity.

[0067] Step 103: Obtain SQL statement samples with a relevance score greater than a first threshold in the historical database as candidate samples.

[0068] Among them, the SQL statement samples whose correlation scores are greater than the first threshold have a higher similarity with the first SQL statement. It can be seen that in the embodiment of the present application, SQL statement samples in the historical database with a higher similarity with the first SQL statement are selected to construct the prompt words of the model.

[0069] In addition, selecting a SQL statement sample similar to the first SQL statement to construct the prompt word can produce the following effects:

[0070] (1) The model can better understand the user’s intent and expected output format; this is because the model learns how to “think” and answer questions through these examples, thereby improving its ability to handle reasoning problems.

[0071] (2) Improve efficiency and save time: Specific prompt words can allow the model to generate output results faster. This is because the model does not need to spend time guessing the user's needs, but can generate directly based on the specific prompt words. This can avoid unnecessary repetition and modification, improve efficiency and save time.

[0072] Step 104: Determine the difficulty type of the first SQL statement.

[0073] The difficulty level of SQL statements can be pre-classified into at least two types, for example, a first type and a second type, wherein the first type may represent a simple type and the second type may represent a difficult type; or, for example, can be classified into three types, namely, a simple type, a medium type, a difficult type, etc. It can be understood that the specific classification form of the difficulty level of SQL statements is not limited to that described herein.

[0074] It can be seen that in the embodiment of the present application, the difficulty types of SQL statements can be pre-classified, and then in step 104, it is determined to which type of the pre-classified difficulty types of SQL statements the first SQL statement belongs.

[0075] Step 105: According to the difficulty type, select at least some examples from the candidate examples as prompt examples.

[0076] For simple SQL statements, only a few prompt examples are needed. Providing too many prompt examples will cause the model's attention to be distracted and the effect to deteriorate. For complex SQL (i.e., more difficult SQL statements), more prompt examples are needed so that the model can better refer to these prompt examples and generate better results.

[0077] Therefore, in an embodiment of the present application, after selecting the above-mentioned candidate examples in the historical database, we can further refer to the difficulty of the first SQL statement and select an appropriate number of prompt examples from the candidate examples, so as to improve the accuracy of the model.

[0078] Step 106: construct prompt words based on the prompt examples.

[0079] Among them, Few-Shot Learning: It is mainly used when the model cannot understand the problem or needs to output in a fixed format. In this case, it is necessary to provide guidance or certain examples in prompts to guide and fine-tune the model to meet business requirements.

[0080] In the embodiment of the present application, after obtaining the above prompt sample, it is used to construct a prompt word (Prompt) to assist the first model in correcting the first SQL statement, so that the output of the first model can meet the requirements. It can be understood that the process of constructing the prompt word based on the prompt sample can adopt existing related technologies, which will not be repeated here.

[0081] Step 107: Input the first SQL statement and the prompt word into the first model to obtain a second SQL statement that is a correction of the first SQL statement.

[0082] The first model is used to correct the syntax problems of the SQL statement, thereby outputting the corrected SQL statement. For example, the first model may be a large language model (LLM); it is understandable that the first model is not limited to LLM, and may also be other large models based on deep learning technology.

[0083] It should be noted that LLM is usually a natural language processing model with large-scale parameters and computing power. Large models are trained with a large amount of data and parameters to generate human-like text or answer natural language questions. LLM has a wide range of applications in natural language processing, text generation, and intelligent dialogue.

[0084] It can be seen from the above steps 101 to 107 that in the embodiment of the present application, after obtaining the first SQL statement, it is possible to determine the correlation score between the first SQL statement and each SQL statement sample in the historical database, thereby obtaining the SQL statement sample in the historical database whose correlation score is greater than the first threshold as a candidate sample; the difficulty type of the first SQL statement can also be determined, so that according to the difficulty type, at least some samples are selected from the candidate samples as prompt samples, and prompt words are constructed based on the prompt samples, and then the first SQL statement and the prompt word can be input into the first model to obtain a second SQL statement after correcting the first SQL statement.

[0085] Among them, in the model generation task, the prompt example is an important factor in guiding the model to react and generate inputs, and different prompt examples determine completely different outputs of the model. In the embodiment of the present application, when the first model is used to correct the first SQL statement, when selecting the prompt example used to construct the prompt word, not only the relevance score with the first SQL statement is considered, but also the difficulty type of the first SQL statement is considered. In this way, the constructed prompt word has a high relevance with the first SQL statement and conforms to the difficulty of the first SQL statement, so that the prompt word can guide the first model to better correct the first SQL statement.

[0086] It can be seen that the embodiments of the present application can dynamically adjust the prompt samples required to construct the prompt words according to the relevance and difficulty type to the SQL statement to be processed. In this way, even if the SQL statement to be processed is more difficult, suitable prompt words can be constructed, thereby improving the accuracy of correcting the SQL statement.

[0087] In addition, the method for processing the structured query language SQL statement of the embodiment of the present application can be executed before the code of the first SQL statement is executed, or it can be executed after the code of the first SQL statement is executed and an error is reported.

[0088] Optionally, in the above step 102, determining the correlation score between the first SQL statement and each SQL statement sample in the history database includes the following steps A-1 to A-2:

[0089] Step A-1: ​​obtaining first parameter information of the first SQL statement, wherein the first parameter information includes syntax error information and schema information of an associated table;

[0090] Step A-2: Determine a correlation score between the first SQL statement and each SQL statement sample in the historical database based on the first parameter information of the first SQL statement and the first parameter information of each SQL statement sample in the historical database.

[0091] The syntax error information may include syntax type, error code, error location and other information.

[0092] In addition, the associated table of the SQL statement includes, for example, a data table referenced (or called) by the SQL statement.

[0093] The associated table information of an SQL statement includes, for example, table join, group, and key information; in a database query, join is an operation used to combine rows from two or more tables based on a common field (usually a key); group usually refers to the GROUP BY clause, which allows the result set to be grouped according to one or more columns, and then aggregate functions can be used to calculate the data for each group; key refers to a primary key or a foreign key, a primary key is a field that uniquely identifies each record in a table, and a foreign key is a field in a table that points to the primary key of another table and is used to establish a relationship between the two tables.

[0094] The schema information of the SQL statement-related table can be obtained by querying the INFORMATION_SCHEMA database. INFORMATION_SCHEMA is a special database in the relational database management system (MySQL). It contains multiple tables that store metadata information about other databases, including table structure, index, character set, column details, etc.

[0095] In addition, the syntax error information of the SQL statement and the schema information of the associated table are characteristic information of the SQL statement. Based on the syntax error information and the schema information of the associated table of the first SQL statement, and the syntax error information and the schema information of the associated table of a certain SQL statement sample in the historical database, the correlation score between the two can be calculated, that is, the similarity between the two can be determined.

[0096] Optionally, in step A-2, determining a correlation score between the first SQL statement and the i-th SQL statement example according to the first parameter information of the first SQL statement and the first parameter information of the i-th SQL statement example in the historical database includes:

[0097] Calculate the similarity S1 between the syntax error information of the first SQL statement and the syntax error information of the i-th SQL statement sample;

[0098] Calculate the similarity S2 between the Schema information of the association table of the first SQL statement and the Schema information of the association table of the i-th SQL statement sample;

[0099] According to the first weight corresponding to S1 and the second weight corresponding to S2, a weighted sum is performed on S1 and S2 to obtain a correlation score between the first SQL statement and the i-th SQL statement sample.

[0100] When calculating S1, the syntax error information of the first SQL statement may be converted into a first vector, and the syntax error information of the i-th SQL statement sample may be converted into a second vector, so as to calculate the similarity between the first vector and the second vector.

[0101] Similarly, when calculating S2, the Schema information of the first SQL statement can be converted into a third vector, and the Schema information of the i-th SQL statement sample can be converted into a fourth vector, so as to calculate the similarity between the third vector and the fourth vector.

[0102] In addition, the similarity between two vectors can be calculated by a variety of methods, among which the most commonly used method is cosine similarity. Cosine similarity determines the degree of similarity between two vectors by calculating the dot product of two vectors and their modulus. The value of cosine similarity ranges from -1 to 1, where 1 means exactly the same, -1 means completely opposite, and 0 means no correlation.

[0103] Optionally, in the above step 104, determining the difficulty type of the first SQL statement includes:

[0104] Inputting the first parameter information of the first SQL statement and the first SQL statement into a classification model to obtain a difficulty level type of the first SQL statement;

[0105] The first parameter information includes syntax error information and architecture information of the association table.

[0106] In addition, the classification model may be implemented using an LLM model, or may be implemented using any other classification model in the prior art.

[0107] In addition, the more syntax error information an SQL statement contains and the more complex the schema information of the associated table is, the more difficult the SQL statement may be. Therefore, based on the error information of the SQL statement, the schema information of the associated table, and the SQL statement, its difficulty type can be identified through a classification model.

[0108] It should be noted that the classification model can be obtained in advance through training. It can be understood that the training method of the classification model can adopt the training method in the prior art, which will not be repeated here.

[0109] Optionally, in the above step 105, selecting at least some of the candidate examples as prompt examples according to the difficulty level type includes:

[0110] When the difficulty level type is a target type and the number of the candidate examples is greater than the target predetermined number corresponding to the target type, selecting the target predetermined number of examples from the candidate examples as the prompt examples in descending order of relevance scores;

[0111] When the difficulty level type is the target type and the number of the candidate examples is less than or equal to the target predetermined number, selecting the candidate examples as the prompt examples;

[0112] Among them, different difficulty levels correspond to different reservation numbers. The greater the difficulty level corresponding to the difficulty level type, the greater the corresponding reservation number.

[0113] For example, if the number of candidate samples is m=5, the target type is a simple type, and the corresponding number of prompt samples is n=2, then the m candidate samples can be sorted in descending order according to the relevance score, so that the first two candidate samples are selected as prompt samples; if the target type is a difficult type, and the corresponding number of prompt samples is n=3, then the m candidate samples can be sorted in descending order according to the relevance score, so that the first three candidate samples are selected as prompt samples;

[0114] If the number n of prompt examples corresponding to the simple type or the difficult type is greater than or equal to m, then the m candidate examples are directly used as prompt examples.

[0115] It should be noted that when m is greater than n, candidate samples with larger correlation scores are given priority, which can further improve the output effect of the model.

[0116] Optionally, in the above step 106, constructing a prompt word based on the prompt sample includes:

[0117] Sorting the prompt samples in ascending order of relevance scores;

[0118] Based on the sorted prompt examples, construct prompt words.

[0119] Among them, in various large model generation tasks, the order of prompt samples input into the large model is also very important. Because learning a large model through such prompts does not adjust parameters through gradient descent, but generates results through observation and understanding of the prompt samples. For example, in few-shot learning, the order of prompt samples will cause the large model to fluctuate between the best performance and random guessing. It can be seen that the order of prompt samples is even more important than sample selection.

[0120] In addition, in an embodiment of the present application, in the constructed prompt words, the prompt samples are sorted in order from small to large in terms of relevance scores, so that the prompt sample most relevant to the first SQL statement can be placed in the sensitive area of ​​the first model prompt word (Prompt), making the model error correction result more accurate.

[0121] Optionally, constructing prompt words based on the sorted prompt examples includes:

[0122] Constructing a prompt word based on the sorted prompt samples and the first parameter information of the first SQL statement;

[0123] The first parameter information includes syntax error information and architecture information of the association table.

[0124] It can be seen from this that in the embodiment of the present application, when constructing the above-mentioned prompt words, the syntax error information of the first SQL statement and the structure information of the associated table can also be added to assist the first model in correcting the first SQL statement, thereby further improving the correction accuracy of the SQL statement.

[0125] Optionally, the method further comprises:

[0126] Performing syntax check on the second SQL statement;

[0127] If there is a syntax error in the second SQL statement, correct the second SQL statement;

[0128] When there is no syntax error in the second SQL statement, the first SQL statement is stored in the history database as an SQL statement sample.

[0129] It can be seen that after correcting the first SQL statement to obtain the second SQL statement, it is possible to further detect whether there are any syntax errors in the second SQL statement, thereby realizing the verification of the second SQL statement. If the verification passes (i.e., there are no syntax errors), it means that the correction of the first SQL statement by the first model meets the requirements, and the first SQL statement can be stored in the historical database as a sample to assist in the correction of other subsequent SQL statements; if the verification fails (i.e., there are still syntax errors), the second SQL statement needs to be corrected.

[0130] The process of correcting the second SQL statement here is the same as the process of correcting the first SQL statement described above; alternatively, the prompt words constructed when correcting the first SQL statement can be directly used to correct the second SQL statement through the first model (that is, the above prompt words, the second SQL statement, the syntax error information of the second SQL statement and the structure information of the associated table are input into the first model to obtain the corrected SQL statement of the second SQL statement).

[0131] In summary, the specific implementation of the method for processing the structured query language SQL statement of the embodiment of the present application can be as follows: Figure 2 As shown, it includes the following steps 1 to 7:

[0132] Step 1: Input a first SQL statement into a SQL parsing module, and the SQL parsing module processes the first SQL statement; the first SQL statement is an original SQL statement;

[0133] The SQL parsing module consists of the following two parts:

[0134] (1) Pre-syntax check small model: used to perform syntax check on the first SQL statement, output SQL syntax error information (such as type, error code, syntax error location), and also output the name of the associated table of the first SQL statement.

[0135] (2) Table information recall submodule: It is used to detect the name of the associated table output by the small model through the prefix syntax, and query the database to obtain the corresponding table schema information.

[0136] Step 2: recall samples based on the information output by the SQL parsing module: that is, based on the syntax error information of the first SQL statement and the schema information of the associated table, as well as the syntax error information of the SQL statement samples in the historical database and the schema information of the associated table, calculate the correlation score of each SQL statement sample with the first SQL statement, and select SQL statement samples with a correlation score greater than a first threshold as candidate samples.

[0137] Among them, when calculating the relevance score between the first SQL statement and a certain SQL statement sample, the first similarity between the two can be calculated based on the syntax error information of the first SQL statement and the syntax error information of the SQL statement sample; based on the Schema information of the association table of the first SQL statement and the Schema information of the association table of the SQL statement sample, the second similarity between the two is calculated, and then the first similarity and the second similarity are weightedly summed to obtain the relevance score of the first SQL statement and the SQL statement sample.

[0138] It can be understood that the weight corresponding to the first similarity and the weight corresponding to the second similarity can be predetermined.

[0139] Step 3: Selecting prompt samples from candidate samples: First, determine the difficulty type of the first SQL statement, thereby determining the number of prompt samples required according to the difficulty type, and then select a corresponding number of prompt samples from the candidate samples according to the relevance score.

[0140] The first SQL statement, the syntax error information of the first SQL statement, and the Schema information of the associated table of the first SQL statement can be input into the LLM, so that the LLM can perform binary classification to determine whether the type of the first SQL statement is a simple type or a difficult type. For example, if the classification result is "simple type", the number of samples n=2 is prompted; if the classification result is "difficult type", the number of samples n=3 is prompted.

[0141] In addition, when selecting hint samples from candidate samples, the candidate samples can be first sorted in descending order of relevance scores. If the number of candidate samples m is greater than n, the first n candidate samples in the sorting are selected as hint samples; if the number of candidate samples m is less than or equal to n, the m candidate samples are directly used as hint samples.

[0142] Step 4: sort the prompt samples in reverse order, that is, sort the selected prompt samples in order of relevance scores from small to large;

[0143] Step 5: Prompt assembly: Integrate the syntax error information of the first SQL statement, the schema information of the related table, and the prompt examples in reverse order to build a prompt. The LLM is required to correct the error step by step from the perspective of a SQL expert and fix the output format.

[0144] Step 6: Correct the SQL statement based on the Prompt, that is, input the first SQL statement and the Prompt into the LLM to obtain a second SQL statement that is the result of correcting the first SQL statement.

[0145] Step 7: Verify the second SQL statement: that is, perform syntax check on the second SQL statement through the above-mentioned front-end syntax check small model. If a syntax error is detected (that is, the verification fails), the detected syntax error information, the Schema information of the associated table of the second SQL statement and the above-mentioned Prompt are input into the LLM to correct the second SQL statement; if no syntax error is detected (that is, the verification succeeds), the second SQL statement is output to the user, and the error correction (that is, the first SQL statement, the syntax error information of the first SQL statement and the Schema information of the associated table, and the second SQL statement) is written into the historical database as a new SQL statement sample.

[0146] It should be noted that the second SQL statement is obtained after correcting the first SQL statement. Therefore, the schema information of the associated table of the first SQL statement can be directly used as the schema information of the associated table of the second SQL statement; or, the schema information of the associated table of the second SQL statement can be re-obtained.

[0147] From the above, it can be seen that the embodiments of the present application design a dynamic prompt learning solution for SQL error correction tasks based on large models. By scoring the relevance of recalled prompt samples, judging the difficulty of SQL error correction tasks (determining the number of prompt samples required for the large model) and sorting the prompt samples, it can effectively improve and solve the problems of low accuracy of SQL error correction in difficult samples, single error correction task range type and invalid answers generated by large models, thereby improving the error correction accuracy of large models, and further comprehensively and effectively improving the overall effect of SQL error correction.

[0148] The above introduces the method for processing the structured query language SQL statement provided in the embodiment of the present application. The following will introduce the device for processing the structured query language SQL statement provided in the embodiment of the present application in conjunction with the accompanying drawings.

[0149] See also Figure 3 The embodiment of the present application also provides a structured query language SQL statement processing device, the device comprising:

[0150] A first acquisition module 301 is used to acquire a first SQL statement;

[0151] A first determination module 302, configured to determine a correlation score between the first SQL statement and each SQL statement sample in a historical database;

[0152] The second acquisition module 303 is used to acquire SQL statement samples with a relevance score greater than a first threshold in the historical database as candidate samples;

[0153] A second determination module 304, configured to determine the difficulty level type of the first SQL statement;

[0154] A selection module 305, configured to select at least some of the candidate examples as prompt examples according to the difficulty level type;

[0155] A construction module 306 is used to construct a prompt word based on the prompt sample;

[0156] The correction module 307 is used to input the first SQL statement and the prompt word into the first model to obtain a second SQL statement after correcting the first SQL statement.

[0157] As can be seen from the above, in the embodiment of the present application, the first SQL statement can be obtained by the first acquisition module 301 to trigger the first determination module 302 to determine the correlation score between the first SQL statement and each SQL statement sample in the historical database, so that the second acquisition module 303 obtains the SQL statement sample in the historical database whose correlation score is greater than the first threshold as a candidate sample; wherein, the difficulty type of the first SQL statement can also be determined by the second determination module 304, so that the selection module 305 can select at least part of the samples from the candidate samples as prompt samples according to the difficulty type, and then the construction module 306 can construct a prompt word based on the prompt sample, and then the first SQL statement and the prompt word are input into the first model through the correction module 307 to obtain a second SQL statement after the first SQL statement is corrected.

[0158] Among them, in the model generation task, the prompt example is an important factor in guiding the model to react and generate inputs, and different prompt examples determine completely different outputs of the model. In the embodiment of the present application, when the first model is used to correct the first SQL statement, when selecting the prompt example used to construct the prompt word, not only the relevance score with the first SQL statement is considered, but also the difficulty type of the first SQL statement is considered. In this way, the constructed prompt word has a high relevance with the first SQL statement and conforms to the difficulty of the first SQL statement, so that the prompt word can guide the first model to better correct the first SQL statement.

[0159] It can be seen that the embodiments of the present application can dynamically adjust the prompt samples required to construct the prompt words according to the relevance and difficulty type to the SQL statement to be processed. In this way, even if the SQL statement to be processed is more difficult, suitable prompt words can be constructed, thereby improving the accuracy of correcting the SQL statement.

[0160] Optionally, the first determining module 302 is specifically configured to:

[0161] Acquire first parameter information of the first SQL statement, wherein the first parameter information includes syntax error information and architecture information of an associated table;

[0162] Determine a correlation score between the first SQL statement and each SQL statement sample in the historical database according to the first parameter information of the first SQL statement and the first parameter information of each SQL statement sample in the historical database.

[0163] Optionally, the second determining module 304 is specifically configured to:

[0164] Inputting the first parameter information of the first SQL statement and the first SQL statement into a classification model to obtain a difficulty level type of the first SQL statement;

[0165] The first parameter information includes syntax error information and architecture information of the association table.

[0166] Optionally, the selection module 305 is specifically used to:

[0167] When the difficulty level type is a target type and the number of the candidate examples is greater than the target predetermined number corresponding to the target type, selecting the target predetermined number of examples from the candidate examples as the prompt examples in descending order of relevance scores;

[0168] When the difficulty level type is the target type and the number of the candidate examples is less than or equal to the target predetermined number, selecting the candidate examples as the prompt examples;

[0169] Among them, different difficulty levels correspond to different reservation numbers. The greater the difficulty level corresponding to the difficulty level type, the greater the corresponding reservation number.

[0170] Optionally, the construction module 306 is specifically used to:

[0171] Sorting the prompt samples in ascending order of relevance scores;

[0172] Based on the sorted prompt examples, construct prompt words.

[0173] Optionally, the construction module 306 constructs prompt words based on the sorted prompt examples, including:

[0174] Constructing a prompt word based on the sorted prompt samples and the first parameter information of the first SQL statement;

[0175] The first parameter information includes syntax error information and architecture information of the association table.

[0176] Optionally, the device further comprises a verification module, configured to:

[0177] Performing syntax check on the second SQL statement;

[0178] If there is a syntax error in the second SQL statement, correct the second SQL statement;

[0179] When there is no syntax error in the second SQL statement, the first SQL statement is stored in the history database as an SQL statement sample.

[0180] It should be noted that the division of units in the embodiments of the present application is schematic and is only a logical function division. There may be other division methods in actual implementation. In addition, each functional unit in each embodiment of the present application may be integrated into a processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0181] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a processor-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or 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 to enable a computer device (which can be a personal computer, server, or network device, etc.) or a processor (processor) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory, ROM), random access memory (Random Access Memory, RAM), disk or optical disk and other media that can store program codes.

[0182] It should be noted here that the above-mentioned device provided in the embodiment of the present application can implement all the method steps implemented in the above-mentioned method embodiment, and can achieve the same technical effect. The parts and beneficial effects of this embodiment that are the same as those in the method embodiment will not be described in detail here.

[0183] The embodiment of the present application also provides an electronic device, such as Figure 4 As shown, the electronic network device includes a memory 420, a transceiver 410, and a processor 400;

[0184] Memory 420, used for storing computer programs;

[0185] The transceiver 410 is used to receive and send data under the control of the processor 400;

[0186] The processor 400 is used to read the computer program in the memory 420 and execute the method for processing the structured query language SQL statement described in the first aspect.

[0187] Among them, Figure 4 In the embodiment, the bus architecture may include any number of interconnected buses and bridges, specifically linking together various circuits of one or more processors represented by processor 400 and memory represented by memory 420. The bus architecture may also link together various other circuits such as peripherals, voltage regulators, and power management circuits, which are well known in the art and are therefore not further described herein. The bus interface provides an interface. The transceiver 410 may be a plurality of components, namely, a transmitter and a receiver, providing a unit for communicating with various other devices on a transmission medium, which transmission medium may include a wireless channel, a wired channel, an optical cable, and the like. The processor 400 is responsible for managing the bus architecture and general processing, and the memory 420 may store data used by the processor 400 when performing operations.

[0188] The processor 400 may be a central processing unit (CPU), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or a complex programmable logic device (CPLD). The processor 400 may also adopt a multi-core architecture.

[0189] It should be noted here that the above-mentioned device provided in the embodiment of the present application can implement all the method steps implemented in the above-mentioned method embodiment, and can achieve the same technical effect. The parts and beneficial effects of this embodiment that are the same as those in the method embodiment will not be described in detail here.

[0190] An embodiment of the present application further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and the computer program is used to enable the processor to execute the above-mentioned method for processing structured query language SQL statements.

[0191] The computer-readable storage medium can be any available medium or data storage device that can be accessed by the processor, including but not limited to magnetic storage (such as floppy disks, hard disks, magnetic tapes, magneto-optical disks (Magnet-Optica, MO)), etc.), optical storage (such as Compact Disk (CD), Digital Versatile Disc (DVD), Blu-ray Disc (BD), Holographic Versatile Disc (HVD), etc.), and semiconductor memory (such as Read-Only Memory (ROM), Erasable Programmable Read-Only Memory (EPROM), Electrically Erasable Programmable Read Only Memory (EEPROM), non-volatile memory (NAND FLASH), solid-state drive (Solid State Disk or Solid State Drive, SSD)), etc.

[0192] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage and optical storage, etc.) that contain computer-usable program code.

[0193] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer executable instructions. These computer executable instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0194] These processor executable instructions may also be stored in a processor readable memory that can direct a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the processor readable memory produce an article of manufacture including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0195] These processor-executable instructions may also be loaded onto a computer or other programmable data processing device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0196] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.

Claims

1. A method for processing a structured query language SQL statement, characterized in that: The method comprises: Get the first SQL statement; Determine a correlation score between the first SQL statement and each SQL statement sample in the history database; Acquire SQL statement samples with a relevance score greater than a first threshold in the historical database as candidate samples; Determining the difficulty level type of the first SQL statement; According to the difficulty type, select at least some of the candidate samples as prompt samples; Constructing a prompt word based on the prompt sample; The first SQL statement and the prompt word are input into a first model to obtain a second SQL statement after correcting the first SQL statement.

2. The method according to claim 1, characterized in that The determining of the correlation scores between the first SQL statement and each SQL statement sample in the history database includes: Acquire first parameter information of the first SQL statement, wherein the first parameter information includes syntax error information and architecture information of an associated table; Determine a correlation score between the first SQL statement and each SQL statement sample in the historical database according to the first parameter information of the first SQL statement and the first parameter information of each SQL statement sample in the historical database.

3. The method according to claim 1, characterized in that Determining the difficulty level type of the first SQL statement includes: Inputting the first parameter information of the first SQL statement and the first SQL statement into a classification model to obtain a difficulty level type of the first SQL statement; The first parameter information includes syntax error information and architecture information of the association table.

4. The method according to claim 1, characterized in that The selecting at least some of the candidate examples as prompt examples according to the difficulty level type includes: When the difficulty level type is a target type and the number of the candidate examples is greater than the target predetermined number corresponding to the target type, selecting the target predetermined number of examples from the candidate examples as the prompt examples in descending order of relevance scores; When the difficulty level type is the target type and the number of the candidate examples is less than or equal to the target predetermined number, selecting the candidate examples as the prompt examples; Among them, different difficulty levels correspond to different reservation numbers. The greater the difficulty level corresponding to the difficulty level type, the greater the corresponding reservation number.

5. The method according to claim 1, characterized in that The step of constructing a prompt word based on the prompt sample includes: Sorting the prompt samples in ascending order of relevance scores; Based on the sorted prompt examples, construct prompt words.

6. The method according to claim 5, characterized in that The step of constructing prompt words based on the sorted prompt samples includes: Constructing a prompt word based on the sorted prompt samples and the first parameter information of the first SQL statement; The first parameter information includes syntax error information and architecture information of the association table.

7. The method according to any one of claims 1 to 6, characterized in that: The method further comprises: Performing syntax check on the second SQL statement; If there is a syntax error in the second SQL statement, correct the second SQL statement; When there is no syntax error in the second SQL statement, the first SQL statement is stored in the history database as an SQL statement sample.

8. A structured query language SQL statement processing device, characterized in that: The device comprises: A first acquisition module, used to acquire a first SQL statement; A first determination module, used to determine the correlation score between the first SQL statement and each SQL statement sample in the historical database; A second acquisition module is used to acquire SQL statement samples with a relevance score greater than a first threshold in the historical database as candidate samples; A second determination module, used to determine the difficulty type of the first SQL statement; A selection module, configured to select at least some of the candidate samples as prompt samples according to the difficulty level type; A construction module, used for constructing a prompt word based on the prompt sample; The correction module is used to input the first SQL statement and the prompt word into the first model to obtain a second SQL statement after correcting the first SQL statement.

9. An electronic device, characterized in that: Including memory, transceiver, processor: A memory for storing a computer program; a transceiver for sending and receiving data under the control of the processor; a processor for reading the computer program in the memory and executing the method for processing a structured query language SQL statement according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and the computer program is used to enable the processor to execute the method for processing a structured query language SQL statement according to any one of claims 1 to 7.