System and method for automatically executing and correcting SQL (Structured Query Language) statement based on large model
By introducing large-model-based detection and correction mechanisms in NL2SQL system, the challenges in accuracy, security and logical consistency of SQL statements generated by NL2SQL system are solved, and higher SQL generation accuracy and security are achieved.
Patent Information
- Application Number
- CN202510184323.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-19
- Publication Date
- 2025-05-30
AI Technical Summary
The existing NL2SQL system lacks effective verification and correction mechanisms, resulting in the generated SQL statements that may not accurately reflect the user's query intention or pose security risks.
Provide a system based on large models, including intention detection module, logic detection module, security setting module, security detection module, output module and correction module. Through the coordinated work of these modules, SQL statements generated by the NL2SQL system are automatically detected and corrected.
Automatic detection of SQL statements generated by NL2SQL system is realized to ensure its accuracy, security and logical consistency, and to improve the accuracy and security of NL2SQL generation of SQL.
Smart Images

Figure CN120067143A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and specifically to a system and method for automatically executing and correcting SQL statements based on a large model. Background Art
[0002] Currently, the technology of converting natural language to SQL (NL2SQL) has been able to achieve basic query conversion, but there are still challenges in terms of accuracy, security, and logical consistency. Existing systems often lack effective verification and correction mechanisms, resulting in the generated SQL statements may not accurately reflect the user's query intent or have security risks.
[0003] How to automatically detect and correct the SQL statements generated by the NL2SQL system is a technical problem that needs to be solved. Summary of the Invention
[0004] The technical task of the present invention is to address the above deficiencies and provide a system and method for automatically executing and correcting SQL statements based on a large model to solve the technical problem of how to automatically detect and correct the SQL statements generated by the NL2SQL system.
[0005] In a first aspect, a system for automatically executing and correcting SQL statements based on a large model according to the present invention includes an intent detection module, a logic detection module, a security setting module, a security detection module, an output module, and a correction module;
[0006] The security setting module is used to set a security level for the generated SQL statement, and the security level is used to limit the execution permission of the security detection module for the SQL statement;
[0007] The intent detection module is used to call the large model to determine whether the generated SQL statement conforms to the user's intent. If it conforms to the user's intent, it calls the logic detection module and passes the SQL statement to the logic detection module. If it does not conform to the user's intent, it fine-tunes the large model or calls the correction module and passes the SQL statement to the correction module; correspondingly, the correction module is used to correct the SQL statement that does not conform to the user's intent and return the corrected SQL statement to the intent detection module. For the SQL statements that have not been corrected after exceeding the predetermined number of times, it calls the output module to output the uncorrected result;
[0008] The logic detection module is used to call the large model to detect whether the syntax used in the generated SQL statement is correct, and query whether the conditions and their values used in the SQL statement match the question raised by the user. If the logic detection passes, it calls the security detection module and passes the SQL statement to the security detection module. If the logic detection fails, it calls the correction module and passes the SQL statement to the correction module. Correspondingly, the correction module is used to correct the SQL statement that fails the logic detection and return the corrected SQL statement to the logic detection module. For SQL statements that have not been corrected after exceeding the predetermined number of times, it calls the output module to output the uncorrected result.
[0009] The security detection module is used to perform security detection on the generated SQL statement based on the security level set by the security setting module. If the security detection passes, it calls the output module and passes the SQL statement to the output module. If the security detection fails, it terminates the execution of the SQL statement.
[0010] The output module is used to execute the SQL statement and return the execution result of the SQL statement. For uncorrected SQL statements, it is used to output the uncorrected result.
[0011] Preferably, the intent detection module is used to perform the following operations:
[0012] By constructing a Prompt to call the large model to determine whether the generated SQL statement can answer the user's question. If it can answer, it is determined that the generated SQL statement meets the user's intent. If it cannot answer, it is determined that the generated SQL statement does not meet the user's intent. If the SQL statement does not meet the user's intent, it calls the correction module to correct the SQL statement and inputs the corrected SQL statement into the intent detection module. For SQL statements that have not been corrected after exceeding the predetermined number of times, it calls the output module to output the uncorrected result.
[0013] Execute the SQL by pre-executing the SELECT instruction, construct a Prompt based on the obtained query result, input the Prompt into the large model, and determine whether the generated SQL statement can answer the user's question through the large model. If it can answer, it is determined that the generated SQL statement meets the user's intent. If it cannot answer, it is determined that the generated SQL statement does not meet the user's intent. If the SQL statement does not meet the user's intent, it calls the correction module to correct the SQL statement and inputs the corrected SQL statement into the intent detection module. For SQL statements that have not been corrected after exceeding the predetermined number of times, it calls the output module to output the uncorrected result.
[0014] Extract entities and intents in the user's question through a large model, and then use a text similarity comparison model to determine whether the extracted user intent matches the true user intent. If the SQL statement does not conform to the user intent, call the correction module to correct the SQL statement, and input the corrected SQL statement into the intent detection module. For SQL statements that have not been corrected after exceeding the predetermined number of times, call the output module to output the uncorrected result.
[0015] Preferably, the logic detection module is used to perform the following operations:
[0016] Build a knowledge base: Import the knowledge required to generate SQL statements into the knowledge base;
[0017] Pre-set knowledge base retrieval questions: According to the knowledge imported when building the knowledge base, set retrieval questions, and the content of the retrieval questions includes feature syntax and syntax newly added in the version;
[0018] For the input SQL statement, obtain the database data to be tested according to the business scenario, construct a Prompt based on the obtained database data and the SQL statement to be detected, input the Prompt into the large model for syntax judgment. If the verification is correct, call the security detection module and pass the SQL statement to the security detection module. If the logic detection fails, call the correction module and pass the SQL statement to the correction module.
[0019] Preferably, the security levels set by the security setting module include only query, modification, and deletion, and the corresponding statement execution permissions include select, insert, update, and delete;
[0020] Correspondingly, the security detection module is used to perform the following operations:
[0021] Based on the security levels set by the security setting module, perform security detection on the generated SQL statement. For disabled statement execution permissions, prohibit the use of disabled statement execution permissions when executing the SQL statement;
[0022] For SQL statements with injection attacks, construct a Prompt through the content filtered by rules, input the Prompt into the large model, and perform a secondary check on the security risk of the SQL statement through the large model.
[0023] Preferably, the correction module is used to perform the following:
[0024] For correction requests initiated for semantic reasons, construct a prompt based on the incoming modification opinions and the SQL statement, and input the Prompt into the large model to regenerate the SQL statement;
[0025] For the problems detected by logical detection, replace the original conditions or values according to the incoming modification opinions in a regular expression manner, and modify them into new SQL statements.
[0026] In a second aspect, a method for automatically executing and correcting SQL statements based on a large model automatically executes and corrects SQL statements through a system for automatically executing and correcting SQL statements according to any one of the first aspects. The method includes the following steps:
[0027] Security setting: Set a security level for the generated SQL statement, and the security level is used to limit the execution permission of the security detection module for the SQL statement;
[0028] Intention detection: Call the large model to determine whether the generated SQL statement conforms to the user's intention. If it conforms to the user's intention, perform a logical detection operation on the SQL statement. If it does not conform to the user's intention, correct the SQL statement that does not conform to the user's intention, and perform an intention detection operation on the corrected SQL statement. For SQL statements that have not been corrected after exceeding a predetermined number of times, perform an output operation to output the uncorrected result;
[0029] Logical detection: Call the large model to detect whether the syntax used in the generated SQL statement is correct, and query whether the conditions and their values used in the SQL statement match the user's question. If the logical detection passes, perform a security detection operation on the SQL statement. If the logical detection fails, correct the SQL statement that fails the logical detection, and perform logical detection on the corrected SQL statement. For SQL statements that have not been corrected after exceeding a predetermined number of times, perform an output operation to output the uncorrected result;
[0030] Security detection: Based on the security level set by the security setting operation, perform security detection on the generated SQL statement. If the security detection passes, perform an output operation on the SQL statement. If the security detection fails, terminate the execution of the SQL statement;
[0031] Output: Execute the SQL statement and return the execution result of the SQL statement. For uncorrected SQL statements, it is used to output the uncorrected result.
[0032] Preferably, the intention detection includes operations:
[0033] Call the large model by constructing a Prompt to determine whether the generated SQL statement can answer the user's question. If it can answer, it is determined that the generated SQL statement conforms to the user's intention. If it cannot answer, it is determined that the generated SQL statement does not conform to the user's intention. If the SQL statement does not conform to the user's intention, when correcting the SQL, perform an intention detection operation on the corrected SQL statement. For SQL statements that have not been corrected after exceeding a predetermined number of times, perform an output operation to output the uncorrected result;
[0034] Execute SQL by pre-executing a SELECT instruction, construct a Prompt based on the obtained query results, input the Prompt into a large model, and determine whether the generated SQL statement can answer the user's question through the large model. If it can answer, it is determined that the generated SQL statement meets the user's intention; if it cannot answer, it is determined that the generated SQL statement does not meet the user's intention. If the SQL statement does not meet the user's intention, correct the SQL, perform an intention detection operation on the corrected SQL statement, and for SQL statements that have not been corrected after exceeding a predetermined number of times, perform an output operation to output the uncorrected result;
[0035] Extract entities and intentions in the user's question through a large model, and then determine whether the extracted user intention matches the true user intention through a text similarity comparison model. If the SQL statement does not meet the user's intention, correct the SQL, perform intention detection on the corrected SQL statement, and for SQL statements that have not been corrected after exceeding a predetermined number of times, perform an output operation to output the uncorrected result.
[0036] Preferably, the logical detection includes the following operations:
[0037] Build a knowledge base: Import the knowledge required to generate SQL statements into the knowledge base;
[0038] Pre-set knowledge base retrieval questions: According to the knowledge imported when building the knowledge base, set retrieval questions, and the content of the retrieval questions includes characteristic syntax and newly added syntax in the version;
[0039] For the input SQL statement, obtain the database data to be tested according to the business scenario, construct a Prompt based on the obtained database data and the SQL statement to be detected, input the Prompt into the large model for syntax judgment. If the verification is correct, perform a security detection operation on the SQL statement. If the logical detection fails, perform a correction operation on the SQL statement.
[0040] Preferably, the security levels set through the security setting operation include only query, modification, and deletion, and the corresponding statement execution permissions include select, insert, update, and delete;
[0041] Correspondingly, the security detection includes the following operations:
[0042] Based on the security level set by the security setting module, perform security detection on the generated SQL statement. For disabled statement execution permissions, prohibit the use of disabled statement execution permissions when executing the SQL statement;
[0043] For the SQL statements of injection attacks, construct a Prompt based on the content filtered by the rules, input the Prompt into the large model, and perform a secondary check on the security risks of the SQL statements through the large model.
[0044] Preferably, the correction operation includes the following steps:
[0045] For the correction requests initiated due to semantic reasons, construct a prompt based on the incoming modification opinions and the SQL statements, and input the Prompt into the large model to regenerate the SQL statements;
[0046] For the problems found by logical detection, replace the original conditions or values in a regular manner according to the incoming modification opinions, and modify them into new SQL statements.
[0047] The system and method for automatically executing and correcting SQL statements based on a large model of the present invention have the following advantages:
[0048] 1. It can automatically detect whether the SQL statements generated by the NL2SQL system accurately reflect the user's query intent, whether there are logical errors, and whether there may be security risks, and automatically correct them;
[0049] 2. It improves the accuracy and security of SQL generation by nl2sql;
[0050] 3. The system has scalability and can adapt to different databases and business scenarios. BRIEF DESCRIPTION OF THE DRAWINGS
[0051] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following will briefly introduce the drawings required for use in the embodiments or the description of the prior art. Obviously, the following drawings are only some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on these drawings.
[0052] The present invention will be further described below with reference to the drawings.
[0053] Figure 1 FIG. 33 is a structural block diagram of a system for automatically executing and correcting SQL statements based on a large model in Embodiment 1;
[0054] Figure 2 FIG. 37 is a working principle block diagram of an intent detection module and a logical detection module in a system for automatically executing and correcting SQL statements based on a large model in Embodiment 1;
[0055] Figure 3 FIG. 41 is a working principle block diagram of a security detection module in a system for automatically executing and correcting SQL statements based on a large model in Embodiment 1. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0056] The present invention will be further described below in conjunction with the accompanying drawings and specific embodiments, so that those skilled in the art can better understand the present invention and be able to implement it. However, the embodiments given are not intended to limit the present invention. Without conflict, the embodiments of the present invention and the technical features in the embodiments can be combined with each other.
[0057] The embodiments of the present invention provide a system and method for automatically executing and correcting SQL statements based on a large model, which are used to solve the technical problem of how to automatically detect and correct SQL statements generated by an NL2SQL system.
[0058] Embodiment 1:
[0059] A system for automatically executing and correcting SQL statements based on a large model according to the present invention includes an intention detection module, a logic detection module, a security setting module, a security detection module, an output module, and a correction module.
[0060] The security setting module is used to set a security level for the generated SQL statement, and the security level is used to limit the execution permission of the security detection module for the SQL statement.
[0061] In this embodiment, the security levels set by the security setting module include only query, modification, and deletion, and the corresponding statement execution permissions include select, insert, update, and delete.
[0062] The intention detection module is used to call the large model to determine whether the generated SQL statement conforms to the user's intention. If it conforms to the user's intention, it calls the logic detection module and passes the SQL statement to the logic detection module. If it does not conform to the user's intention, it fine-tunes the large model or calls the correction module and passes the SQL statement to the correction module. Correspondingly, the correction module is used to correct the SQL statement that does not conform to the user's intention and return the corrected SQL statement to the intention detection module. For SQL statements that have not been corrected after exceeding a predetermined number of times, it calls the output module to output the uncorrected result.
[0063] The intention detection module of this embodiment is used to detect whether the generated SQL conforms to the problem raised by the user and whether it is consistent with the user's intention. Different from the process of generating SQL from natural language, this process is more like a reverse engineering. This module can be verified using one or more of the following schemes:
[0064] (1) Call the large model by constructing a Prompt to determine whether the generated SQL statement can answer the user's question. If it can answer, it is determined that the generated SQL statement meets the user's intention; if it cannot answer, it is determined that the generated SQL statement does not meet the user's intention. If the SQL statement does not meet the user's intention, call the correction module to correct the SQL statement, and input the corrected SQL statement into the intention detection module. For SQL statements that have not been corrected after exceeding the predetermined number of times, call the output module to output the uncorrected result;
[0065] (2) Execute the SQL by pre-executing the SELECT instruction, construct a Prompt based on the obtained query result, input the Prompt into the large model, and call the large model to determine whether the generated SQL statement can answer the user's question. If it can answer, it is determined that the generated SQL statement meets the user's intention; if it cannot answer, it is determined that the generated SQL statement does not meet the user's intention. If the SQL statement does not meet the user's intention, call the correction module to correct the SQL statement, and input the corrected SQL statement into the intention detection module. For SQL statements that have not been corrected after exceeding the predetermined number of times, call the output module to output the uncorrected result;
[0066] (3) Extract the entities and intentions in the user's question through the large model, and then use the text similarity comparison model to determine whether the extracted user intention matches the real user intention. If the SQL statement does not meet the user's intention, call the correction module to correct the SQL statement, and input the corrected SQL statement into the intention detection module. For SQL statements that have not been corrected after exceeding the predetermined number of times, call the output module to output the uncorrected result.
[0067] In the above solutions, in solution (1), a Prompt is constructed and then the large model is called to determine whether the generated SQL can answer the user's question. For example, "Please determine whether [SQL] can answer [question], and please answer yes or no". This solution requires a certain amount of data for fine-tuning. The fine-tuning targets are mainly instruction fine-tuning following instructions and fine-tuning for specific databases. A pre-trained base model with instruction fine-tuning can be used to decide whether to further perform fine-tuning on the database content according to its performance in actual business.
[0068] Solution (2) verifies whether the generation is correct by pre-executing the SQL and based on the execution result. This method is limited to the SELECT instruction. By executing the SQL, obtaining the query result, and then constructing a Prompt to be judged by the large model. The calling and fine-tuning methods are similar to those described in a, and can be referred to solution a for execution.
[0069] Solution (3) extracts entities and intents from the user's question through a large model, and then makes a judgment through a text similarity comparison model. Extracting entities and intents is to reduce the interference items of irrelevant text in natural language. For the accuracy of the results, the entity and similarity comparison models used need to be fine-tuned specifically to meet the scenarios of business requirements.
[0070] Finally, the SQL that passes the intent detection enters the next module for detection, and the SQL that fails the detection has the problems found by the large model and enters the correction module.
[0071] The logic detection module is used to call the large model to detect whether the syntax used in the generated SQL statement is correct, and query whether the conditions and their values used in the SQL statement match the user's question. If the logic detection passes, call the security detection module and pass the SQL statement to the security detection module. If the logic detection fails, call the correction module and pass the SQL statement to the correction module; correspondingly, the correction module is used to correct the SQL statement that fails the logic detection and return the corrected SQL statement to the logic detection module. For SQL statements that have not been corrected after exceeding the predetermined number of times, call the output module to output the uncorrected result.
[0072] The logic detection module in this embodiment is used to detect whether the syntax used in the generated SQL is correct and query whether the conditions and their values used match the question. For common boundary problems, for example, whether the XX within a week includes the boundary day. Syntax problems mainly focus on the confusion of syntax between different databases and the support of some syntax by different versions of the same database.
[0073] As a specific implementation of the logic detection module, this module is used to perform the following operations:
[0074] (1) Build a knowledge base: Import the knowledge required to generate SQL statements into the knowledge base. For example, if the SQL used in this solution for detection is MySQL, and the version ranges from 8.0 - 8.3, then import the technical documents of the corresponding version of MySQL into the knowledge base, or only save the key information of MySQL and its version, such as the characteristic syntax of MySQL and the new syntax updated in different versions;
[0075] (2) Pre-set knowledge base retrieval questions: According to the knowledge imported when building the knowledge base, set retrieval questions, and the content of the retrieval questions includes characteristic syntax and newly added syntax in the version;
[0076] (3) For the input SQL statement, obtain the database data to be verified according to the business scenario, construct a Prompt based on the obtained database data and the SQL statement to be detected, input the Prompt into the large model for syntax judgment. If the verification is correct, call the security detection module and pass the SQL statement into the security detection module. If the logical detection fails, call the correction module and pass the SQL statement into the correction module.
[0077] The security detection module is used to perform security detection on the generated SQL statement based on the security level set by the security setting module. If the security detection is passed, call the output module and pass the SQL statement into the output module. If the security detection fails, terminate the execution of the SQL statement.
[0078] As a specific implementation of the security detection module, this module is used to perform the following operations:
[0079] (1) Perform security detection on the generated SQL statement based on the security level set by the security setting module. For the disabled statement execution permissions, prohibit the use of the disabled statement execution permissions when executing the SQL statement;
[0080] (2) For the SQL statement of injection attack, construct a Prompt through the content filtered by the rules, input the Prompt into the large model, and perform a secondary check on the security risk of the SQL statement through the large model.
[0081] In this embodiment, the security detection module performs security detection on the SQL statement passed into this module according to the permissions set in the security setting module. This module uses the method of keyword combination to ensure strict execution of security detection. For example, if the user sets "delete" as disabled in the security setting module, then all statements containing "delete" are prohibited from passing here. If the update operation on table A of database DB is prohibited, then statements containing both "update" and "DB.A" will be prohibited from execution. Similarly, high-risk statements such as "Droptable" and "db" will also be prohibited from passing.
[0082] Statements other than those in the security settings may also have possible injection attacks, such as inserting executable statements in fields, common injection attacks such as code / scripts and links. Some can be filtered out by rules, such as common website addresses and file formats. For the content filtered by the rules, use the prompt technology to judge through the large model, ask the large model whether the SQL statement has security risks, and perform a secondary check to ensure the accuracy of risk judgment.
[0083] If a risk is detected, terminate this process and return the information and reason for the failure of security detection. If it passes smoothly, enter the final output module.
[0084] The output module is used to execute SQL statements and return the execution results of the SQL statements. For uncorrected SQL statements, it is used to output uncorrected results.
[0085] In this embodiment, the correction module is used to perform the following:
[0086] (1) For correction requests initiated for semantic reasons (problems detected and called by the intent detection module), construct a prompt based on the incoming modification opinions and the SQL statement, and input the prompt into the large model to regenerate the SQL statement;
[0087] (2) For problems found by logical detection (problems detected and called by the logical detection module), replace the original conditions or values in a regular expression manner according to the incoming modification opinions to modify into a new SQL statement.
[0088] The system of this embodiment uses a large model (LLM) to self-verify the correctness of the generated SQL in the natural language to SQL (NL2SQL) scenario and correct the execution.
[0089] Embodiment 2:
[0090] A method for automatically executing and correcting SQL statements based on a large model according to the present invention automatically executes and corrects SQL statements through the system for automatically executing and correcting SQL statements disclosed in Embodiment 1. This method includes six operations: intent detection, logical detection, security setting, security detection, output, and correction.
[0091] Operation 1, security setting: Set a security level for the generated SQL statement, and the security level is used to limit the execution permission of the security detection module for the SQL statement.
[0092] The security levels set in the security setting operation in this embodiment include only query, modification, and deletion, and the corresponding statement execution permissions include select, insert, update, and delete.
[0093] Operation 2, intent detection: Call the large model to determine whether the generated SQL statement conforms to the user's intent. If it conforms to the user's intent, perform the logical detection operation on the SQL statement. If it does not conform to the user's intent, correct the SQL statement that does not conform to the user's intent, and perform the intent detection operation on the corrected SQL statement. For SQL statements that have not been corrected after exceeding the predetermined number of times, perform the output operation to output the uncorrected results.
[0094] The intent detection in this embodiment detects whether the generated SQL conforms to the question raised by the user and whether it is consistent with the user's intent. Different from the process of generating SQL from natural language, this process is more like a reverse engineering. This operation can be verified using one or more of the following schemes:
[0095] (1) Determine whether the generated SQL statement can answer the user's question by constructing a Prompt to call the large model. If it can answer, it is determined that the generated SQL statement meets the user's intention. If it cannot answer, it is determined that the generated SQL statement does not meet the user's intention. When correcting the SQL statement that does not meet the user's intention, perform an intention detection operation on the corrected SQL statement. For the SQL statement that has not been corrected after exceeding the predetermined number of times, perform an output operation to output the uncorrected result;
[0096] (2) Execute the SQL by pre-executing the SELECT instruction, construct a Prompt based on the obtained query result, input the Prompt into the large model, and determine whether the generated SQL statement can answer the user's question through the large model. If it can answer, it is determined that the generated SQL statement meets the user's intention. If it cannot answer, it is determined that the generated SQL statement does not meet the user's intention. When the SQL statement does not meet the user's intention, correct the SQL, perform an intention detection operation on the corrected SQL statement, and for the SQL statement that has not been corrected after exceeding the predetermined number of times, perform an output operation to output the uncorrected result;
[0097] (3) Extract the entities and intentions in the user's question through the large model, and then determine whether the extracted user intention matches the real user intention through the text similarity comparison model. When the SQL statement does not meet the user's intention, correct the SQL, perform intention detection on the corrected SQL statement, and for the SQL statement that has not been corrected after exceeding the predetermined number of times, perform an output operation to output the uncorrected result.
[0098] In the above solutions, in solution (1), determine whether the generated SQL can answer the user's question by constructing a Prompt and then calling the large model, such as "Please determine whether [SQL] can answer [question], please answer yes or no". This solution requires preparing a certain amount of data for fine-tuning. The fine-tuning targets are mainly instruction fine-tuning following the instruction and fine-tuning for a specific database. A pre-trained base model with instruction fine-tuning can be determined whether to further perform fine-tuning on the database content according to its performance in actual business.
[0099] In solution (2), verify whether the generation is correct by pre-executing the SQL and based on the execution result. This method is limited to the SELECT instruction. By executing the SQL, obtaining the query result and then constructing a Prompt to be judged by the large model. The calling and fine-tuning methods are similar to those described in a, and can be referred to solution a for execution.
[0100] Solution (3) extracts entities and intents from the user's question through a large model, and then makes a judgment through a text similarity comparison model. Extracting entities and intents is to reduce the interference items of irrelevant text in natural language. For the accuracy of the results, the entity and similarity comparison models used need to be fine-tuned specifically to meet the scenarios of business requirements.
[0101] Operation 3. Logic detection: Call the large model to detect whether the syntax used in the generated SQL statement is correct, and query whether the conditions and their values used in the SQL statement match the question raised by the user. If the logic detection passes, perform a security detection operation on the SQL statement. If the logic detection fails, correct the SQL statement that fails the logic detection, and perform logic detection on the corrected SQL statement. For SQL statements that are not corrected after exceeding the predetermined number of times, perform an output operation to output the uncorrected result.
[0102] In this embodiment, the logic detection operation detects whether the syntax used in the generated SQL is correct, and queries whether the conditions and their values used match the question. For common boundary problems, whether the XX within a week includes the boundary day is queried. The syntax problems mainly focus on the confusion of syntax between different databases and the support problems of some syntax in different versions of the same database.
[0103] As a specific implementation of the logic detection operation, this operation includes the following steps:
[0104] (1) Build a knowledge base: Import the knowledge required to generate the SQL statement into the knowledge base. For example, if the SQL used in this solution for detection is MySQL, and the version ranges from 8.0 to 8.3, then import the technical documents of the corresponding version of MySQL into the knowledge base, or only save the key information of MySQL and its version, such as the characteristic syntax of MySQL and the new syntax updated in different versions;
[0105] (2) Pre-set the knowledge base retrieval questions: According to the knowledge imported when building the knowledge base, set the retrieval questions, and the content of the retrieval questions includes characteristic syntax and the syntax newly added in the version;
[0106] (3) For the input SQL statement, obtain the database data to be tested according to the business scenario, construct a Prompt based on the obtained database data and the SQL statement to be detected, input the Prompt into the large model for syntax judgment. If the verification is correct, perform a security detection operation on the SQL statement. If the logic detection fails, perform a correction operation on the SQL statement.
[0107] Operation Four, Security Detection: Based on the security level set by the security settings operation, perform security detection on the generated SQL statement. If the security detection is passed, perform the output operation on the SQL statement. If the security detection fails, terminate the execution of the SQL statement.
[0108] As a specific implementation of the security detection operation, this operation includes the following steps:
[0109] (1) Based on the security level set by the security settings module, perform security detection on the generated SQL statement. For the disabled statement execution permissions, prohibit the use of the disabled statement execution permissions when executing the SQL statement.
[0110] (2) For SQL statements with injection attacks, construct a Prompt through the content filtered by the rules, input the Prompt into the large model, and perform a secondary check on the security risk of the SQL statement through the large model.
[0111] In this embodiment, the security detection performs security detection on the incoming SQL statement according to the permissions set in the security settings operation. This operation uses a keyword combination method to ensure strict execution of the security detection. For example, if the user sets "delete" to be disabled in the security settings operation, then all statements containing "delete" are prohibited from passing here. If the update operation on table A of database DB is prohibited in the settings, then statements containing both "update" and "DB.A" will be prohibited from execution. Similarly, high-risk statements such as "Droptable" and "db" will also be prohibited from passing.
[0112] Statements other than those in the security settings may also have possible injection attacks, such as inserting executable statements, code / scripts, links, etc. into fields. Some common injection attacks can be filtered out by rules, such as common website URLs and file formats. For the content filtered by the rules, use the prompt technology to judge through the large model, ask the large model whether the SQL statement has security risks, and perform a secondary check to ensure the accuracy of the risk judgment.
[0113] If a risk is detected, terminate this process and return the information and reason for the failure of the security detection. If it passes smoothly, perform the output operation.
[0114] Operation Five, Output: Execute the SQL statement and return the execution result of the SQL statement. For uncorrected SQL statements, use them to output uncorrected results.
[0115] In this embodiment, the correction operation includes the following steps:
[0116] (1) For the correction request initiated for semantic reasons (a problem detected and invoked by the intent detection module), construct a prompt based on the incoming modification opinions and SQL statements, and input the prompt into the large model to regenerate the SQL statement;
[0117] (2) For the problems detected by logical detection (problems detected and invoked by the logical detection module), replace the original conditions or values in a regular expression manner according to the incoming modification opinions, and modify them into new SQL statements.
[0118] The method of this embodiment can automatically detect whether the SQL statement generated by the NL2SQL system accurately reflects the user's query intent, whether there are logical errors, and whether there may be security risks, and automatically correct them.
[0119] The above has introduced in detail the system and method for automatically executing and correcting SQL statements based on a large model provided by the present invention. Specific examples are used in this article to elaborate on the principle and implementation manner of the present invention. The description of the above embodiments is only used to help understand the method and its core idea of the present invention; at the same time, for those of ordinary skill in the art, according to the idea of the present invention, there will be changes in the specific implementation manner and application scope. In summary, the content of this specification should not be construed as a limitation to the present invention.
Claims
1. A system for automatically executing and correcting SQL statements based on a large model, characterized in that: It includes an intention detection module, a logic detection module, a security setting module, a security detection module, an output module and a correction module; The security setting module is used to set a security level for the generated SQL statement, and the security level is used to limit the execution authority of the security detection module on the SQL statement; The intention detection module is used to call the big model to determine whether the generated SQL statement meets the user's intention. If it meets the user's intention, the logic detection module is called and the SQL statement is passed to the logic detection module. If it does not meet the user's intention, the big model is fine-tuned or the correction module is called and the SQL statement is passed to the correction module. Correspondingly, the correction module is used to correct the SQL statement that does not meet the user's intention and return the corrected SQL statement to the intention detection module. For SQL statements that have not been corrected for more than a predetermined number of times, the output module is called to output the uncorrected result. The logic detection module is used to call the large model to detect whether the syntax used in the generated SQL statement is correct, and to query whether the conditions and their values used in the SQL statement match the questions raised by the user. If the logic detection passes, the security detection module is called and the SQL statement is passed to the security detection module. If the logic detection fails, the correction module is called and the SQL statement is passed to the correction module. Correspondingly, the correction module is used to correct the SQL statement that fails the logic detection, and return the corrected SQL statement to the logic detection module. For the SQL statement that has not been corrected for more than a predetermined number of times, the output module is called to output the uncorrected result. The security detection module is used to perform security detection on the generated SQL statement based on the security level set by the security setting module. If the security detection is passed, the output module is called and the SQL statement is passed to the output module. If the security detection is not passed, the execution of the SQL statement is terminated. The output module is used to execute SQL statements and return the execution results of the SQL statements. For uncorrected SQL statements, the output module is used to output the uncorrected results.
2. The system for automatically executing and correcting SQL statements based on a large model according to claim 1, characterized in that: The intent detection module is used to perform the following operations: By building a prompt, the big model is called to determine whether the generated SQL statement can answer the user's question. If it can answer, it is determined that the generated SQL statement meets the user's intention. If it cannot answer, it is determined that the generated SQL statement does not meet the user's intention. If the SQL statement does not meet the user's intention, the correction module is called to correct the SQL statement, and the corrected SQL statement is input into the intention detection module. For SQL statements that have not been corrected for more than a predetermined number of times, the output module is called to output the uncorrected results. Execute SQL by pre-executing SELECT instructions, build Prompt based on the query results obtained, input Prompt into the big model, and use the big model to determine whether the generated SQL statement can answer the user's question. If it can answer, it is determined that the generated SQL statement meets the user's intention. If it cannot answer, it is determined that the generated SQL statement does not meet the user's intention. If the SQL statement does not meet the user's intention, call the correction module to correct the SQL statement, and input the corrected SQL statement into the intention detection module. For SQL statements that have not been corrected for more than a predetermined number of times, call the output module to output the uncorrected results; The entities and intents in the user questions are extracted through the large model, and then the text similarity comparison model is used to determine whether the extracted user intent matches the actual user intent. If the SQL statement does not meet the user intent, the correction module is called to correct the SQL statement, and the corrected SQL statement is input into the intention detection module. For SQL statements that have not been corrected for more than a predetermined number of times, the output module is called to output the uncorrected results.
3. The system for automatically executing and correcting SQL statements based on a large model according to claim 1, characterized in that: The logic detection module is used to perform the following operations: Build a knowledge base: import the knowledge required to generate SQL statements into the knowledge base; Prefabricated knowledge base search questions: Set search questions based on the knowledge imported when building the knowledge base. The search questions include feature syntax and syntax added in the version. For the input SQL statement, the database data that needs to be checked is obtained according to the business scenario, and a prompt is built based on the obtained database data and the SQL statement to be checked. The prompt is input into the big model for syntax judgment. If the verification is correct, the security detection module is called and the SQL statement is passed to the security detection module. If the logic detection fails, the correction module is called and the SQL statement is passed to the correction module.
4. The system for automatically executing and correcting SQL statements based on a large model according to claim 1, characterized in that: The security levels set by the security setting module include query only, modification and deletion, and the corresponding statement execution permissions include select, insert, update and delete; Correspondingly, the security detection module is used to perform the following operations: Based on the security level set by the security setting module, the generated SQL statements are security checked. For disabled statement execution permissions, the disabled statement execution permissions are prohibited from being used when executing SQL statements. For SQL statements of injection attacks, a prompt is constructed through rule-filtered content, and the prompt is input into the big model, which then performs a secondary check on the security risks of the SQL statements.
5. The system for automatically executing and correcting SQL statements based on a large model according to claim 1, characterized in that: The correction module is used to perform the following: For modification requests initiated due to semantic reasons, a prompt is built based on the incoming modification opinions and SQL statements, and the prompt is input into the big model to regenerate the SQL statement; For problems found in logic detection, the original conditions or values are replaced by regular expressions based on the incoming modification suggestions and modified into new SQL statements.
6. A method for automatically executing and correcting SQL statements based on a large model, characterized in that: The method of automatically executing and correcting SQL statements by using a system for automatically executing and correcting SQL statements based on a large model as described in any one of claims 1 to 5 comprises the following steps: Security settings: Set the security level for the generated SQL statement. The security level is used to limit the execution authority of the security detection module on the SQL statement; Intent detection: Call the big model to determine whether the generated SQL statement meets the user's intention. If it meets the user's intention, perform a logic detection operation on the SQL statement. If it does not meet the user's intention, correct the SQL statement that does not meet the user's intention, and perform an intent detection operation on the corrected SQL statement. For SQL statements that have not been corrected for more than a predetermined number of times, perform an output operation to output the uncorrected results. Logical test: Call the big model to check whether the syntax used in the generated SQL statement is correct, and check whether the conditions and values used in the SQL statement match the questions raised by the user. If the logical test passes, perform security test operations on the SQL statement. If the logical test fails, correct the SQL statement that fails the logical test, and perform logical test on the corrected SQL statement. For SQL statements that have not been corrected for more than a predetermined number of times, perform output operations to output the uncorrected results. Security check: Based on the security level set by the security setting operation, the generated SQL statement is security checked. If it passes the security check, the SQL statement is output. If it fails the security check, the execution of the SQL statement is terminated. Output: Executes SQL statements and returns the execution results of SQL statements. For uncorrected SQL statements, it is used to output the uncorrected results.
7. The method for automatically executing and correcting SQL statements based on a large model according to claim 6, characterized in that: Intent detection includes operations: By building a prompt, the big model is called to determine whether the generated SQL statement can answer the user's question. If it can answer, it is determined that the generated SQL statement meets the user's intention. If it cannot answer, it is determined that the generated SQL statement does not meet the user's intention. If the SQL statement does not meet the user's intention, when correcting the SQL, the intention detection operation is performed on the corrected SQL statement. For SQL statements that have not been corrected for more than a predetermined number of times, the output operation is performed to output the uncorrected result. Execute SQL by pre-executing SELECT instructions, build Prompt based on the query results, input Prompt into the big model, and use the big model to determine whether the generated SQL statement can answer the user's question. If it can answer, it is determined that the generated SQL statement meets the user's intention. If it cannot answer, it is determined that the generated SQL statement does not meet the user's intention. If the SQL statement does not meet the user's intention, correct the SQL, perform the intention detection operation on the corrected SQL statement, and perform the output operation to output the uncorrected result for the SQL statement that has not been corrected for more than a predetermined number of times; The entities and intents in user questions are extracted through a large model, and then the text similarity comparison model is used to determine whether the extracted user intent matches the actual user intent. If the SQL statement does not meet the user intent, the SQL is corrected and intent detection is performed on the corrected SQL statement. For SQL statements that have not been corrected for more than a predetermined number of times, an output operation is performed to output the uncorrected results.
8. The method for automatically executing and correcting SQL statements based on a large model according to claim 6, characterized in that: Logical testing includes the following operations: Build a knowledge base: import the knowledge required to generate SQL statements into the knowledge base; Prefabricated knowledge base search questions: Set search questions based on the knowledge imported when building the knowledge base. The search questions include feature syntax and syntax added in the version. For the input SQL statement, the database data that needs to be verified is obtained according to the business scenario, and a prompt is built based on the obtained database data and the SQL statement to be tested. The prompt is input into the big model for syntax judgment. If the verification is correct, a security check operation is performed on the SQL statement. If the logic check fails, a correction operation is performed on the SQL statement.
9. The method for automatically executing and correcting SQL statements based on a large model according to claim 6, characterized in that: The security levels set through the security settings operation include query only, modify, and delete, and the corresponding statement execution permissions include select, insert, update, and delete; Correspondingly, security detection includes the following operations: Based on the security level set by the security setting module, the generated SQL statements are security checked. For disabled statement execution permissions, the disabled statement execution permissions are prohibited from being used when executing SQL statements. For SQL statements of injection attacks, a prompt is constructed through rule-filtered content, and the prompt is input into the big model, which then performs a secondary check on the security risks of the SQL statements.
10. The method for automatically executing and correcting SQL statements based on a large model according to claim 6, characterized in that: The corrective action includes the following steps: For modification requests initiated due to semantic reasons, a prompt is built based on the incoming modification opinions and SQL statements, and the prompt is input into the big model to regenerate the SQL statement; For problems found in logic detection, the original conditions or values are replaced by regular expressions based on the incoming modification suggestions and modified into new SQL statements.
Citation Information
Cited By
Model-based SQL code generation method and device, storage medium and equipment
CN121092567A