Database SQL (Structured Query Language) statement correction method and device, equipment and medium

The automated syntax and semantic correction module solves the problem of inefficient error location and correction in SQL statements, achieving efficient and accurate SQL statement correction and shortening the business delivery cycle.

CN121996686APending Publication Date: 2026-05-08TIANJIN NANKAI UNIV GENERAL DATA TECH
View PDF 5 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
TIANJIN NANKAI UNIV GENERAL DATA TECH
Filing Date
2026-04-08
Publication Date
2026-05-08

AI Technical Summary

Technical Problem

In existing technologies, users are prone to making spelling errors in keywords, violating syntax rules, and mismatches between column or table names and database metadata when writing SQL statements. This leads to inefficiency in relying on manual location and modification, and the possibility of errors in correction, thus extending the business delivery cycle.

Method used

By acquiring erroneous SQL statements, we determine whether they contain syntax errors and correct the abstract syntax tree based on the syntax error information; we also use database semantic graphs to identify and correct semantic errors, and generate corrected SQL statements, including automated processing by syntax and semantic correction modules.

Benefits of technology

It improved the efficiency and accuracy of SQL statement correction, reduced delivery delays caused by errors, and effectively shortened the business delivery cycle.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121996686A_ABST
    Figure CN121996686A_ABST
Patent Text Reader

Abstract

The invention discloses a database SQL statement correction method and device, equipment and a medium, and the method comprises the steps: obtaining an error SQL statement, and determining whether the error SQL statement has a grammar error or not; when it is determined that the wrong SQL statement has a grammar error, correcting a part of the abstract syntax tree corresponding to the wrong SQL statement based on grammar error information corresponding to the wrong SQL statement to obtain a first updated abstract syntax tree; obtaining a database semantic map corresponding to the wrong SQL statement, and determining whether the first updated abstract syntax tree has a semantic error according to the database semantic map; when it is determined that a semantic error exists in the first updated abstract syntax tree, performing semantic correction on the first updated abstract syntax tree according to a database semantic map to obtain a second updated abstract syntax tree; and according to the second updated abstract syntax tree, generating the wrong SQL statement after the grammar and the semanteme are corrected, so that the efficiency and the accuracy of correcting the wrong SQL statement are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of database technology, and in particular to a method, apparatus, device, and medium for correcting database SQL statements. Background Technology

[0002] In real-world database applications, users often encounter problems such as misspelled keywords, grammatical errors, and mismatches between column or table names and database metadata when writing SQL statements.

[0003] Current technology typically relies on database error messages to indicate SQL errors, but the actual error location and correction still need to be done manually. This is not only inefficient but also prone to errors during correction. Summary of the Invention

[0004] This invention provides a database SQL statement correction method, apparatus, device, and medium, which solves the problem that existing technologies that rely on manual location and modification of errors in SQL statements are not only inefficient but also prone to correction errors. It improves the efficiency and accuracy of correcting erroneous SQL statements, reduces delivery delays caused by SQL statement errors, and thus effectively shortens the overall business delivery cycle.

[0005] In a first aspect, embodiments of the present invention provide a database SQL statement correction method, comprising: obtaining an erroneous SQL statement and determining whether the erroneous SQL statement has a syntax error; when it is determined that the erroneous SQL statement has a syntax error, correcting a portion of the abstract syntax tree corresponding to the erroneous SQL statement based on the syntax error information corresponding to the erroneous SQL statement to obtain a first updated abstract syntax tree; obtaining a database semantic graph corresponding to the erroneous SQL statement, and determining whether the first updated abstract syntax tree has a semantic error based on the database semantic graph; when it is determined that the first updated abstract syntax tree has a semantic error, semantically correcting the first updated abstract syntax tree based on the database semantic graph to obtain a second updated abstract syntax tree; and generating an erroneous SQL statement with corrected syntax and semantics based on the second updated abstract syntax tree.

[0006] Secondly, embodiments of the present invention also provide a database SQL statement correction device, comprising: a syntax error identification module, used to acquire erroneous SQL statements and determine whether the erroneous SQL statements contain syntax errors; a syntax error correction module, used to, when it is determined that the erroneous SQL statements contain syntax errors, correct a portion of the abstract syntax tree corresponding to the erroneous SQL statements based on the syntax error information corresponding to the erroneous SQL statements to obtain a first updated abstract syntax tree; a semantic error identification module, used to acquire a database semantic graph corresponding to the erroneous SQL statements and determine whether the first updated abstract syntax tree contains semantic errors based on the database semantic graph; a semantic error correction module, used to, when it is determined that the first updated abstract syntax tree contains semantic errors, perform semantic correction on the first updated abstract syntax tree based on the database semantic graph to obtain a second updated abstract syntax tree; and a corrected statement generation module, used to generate erroneous SQL statements with corrected syntax and semantics based on the second updated abstract syntax tree.

[0007] Thirdly, embodiments of the present invention also provide an electronic device, the electronic device comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores a computer program executable by the at least one processor, the computer program being executed by the at least one processor to enable the at least one processor to execute the database SQL statement correction method provided in any embodiment of the present invention.

[0008] Fourthly, embodiments of the present invention also provide a computer-readable storage medium storing computer instructions for causing a processor to execute and implement the database SQL statement correction method provided in any embodiment of the present invention.

[0009] The technical solution of this invention solves the problem of existing technologies that rely on manual location and modification of errors in SQL statements, which are not only inefficient but also prone to errors. This improves the efficiency and accuracy of correcting erroneous SQL statements, reduces delivery delays caused by SQL statement errors, and effectively shortens the overall business delivery cycle. The solution involves determining whether an erroneous SQL statement has a syntax error; when a syntax error is found, correcting the corresponding abstract syntax tree based on the syntax error information to obtain a first updated abstract syntax tree; determining whether a semantic error exists in the first updated abstract syntax tree based on the database semantic graph; when a semantic error is found in the first updated abstract syntax tree, semantically correcting the first updated abstract syntax tree based on the database semantic graph to obtain a second updated abstract syntax tree; and generating a erroneous SQL statement with corrected syntax and semantics based on the second updated abstract syntax tree.

[0010] It should be understood that the description in this section is not intended to identify key or essential features of the embodiments of the present invention, nor is it intended to limit the scope of the invention. Other features of the invention will become readily apparent from the following description. Attached Figure Description

[0011] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0012] Figure 1 This is a flowchart of a database SQL statement correction method provided in Embodiment 1 of the present invention.

[0013] Figure 2 This is a flowchart of another database SQL statement correction method provided in Embodiment 2 of the present invention.

[0014] Figure 3 This is a schematic diagram of a database semantic graph provided according to an embodiment of the present invention.

[0015] Figure 4 This is a schematic diagram of the structure of a database SQL statement correction device provided in Embodiment 3 of the present invention.

[0016] Figure 5 This is a schematic diagram of the structure of an electronic device provided in Embodiment 4 of the present invention.

[0017] Reference numerals: Electronic device 10; Processor 11; Read-only memory 12; Random access memory 13; Bus 14; Input / output interface 15; Input unit 16; Output unit 17; Storage unit 18; Communication unit 19. Detailed Implementation

[0018] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.

[0019] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.

[0020] Example 1 Figure 1 This is a flowchart of a database SQL statement correction method according to Embodiment 1 of the present invention. This embodiment is applicable to the correction of erroneous SQL statements. The method can be executed by a database SQL statement correction device, which can be implemented in hardware and / or software and can be configured in an electronic device such as a computer.

[0021] like Figure 1 As shown, this embodiment discloses a database SQL statement correction method, including steps S110-S150.

[0022] S110. Obtain the erroneous SQL statement and determine whether the erroneous SQL statement contains a syntax error.

[0023] In this context, an erroneous SQL statement can be understood as an SQL statement that throws an error when executed in the target database.

[0024] In this step, specifically, a pre-built syntax parser can be used to parse the erroneous SQL statement, and the presence of a syntax error can be determined based on the parsing results. Alternatively, syntax errors can be filtered from the error messages returned by the target database. If a syntax error is found in the error messages returned by the target database, and this error is not caused by a false positive, then the erroneous SQL statement is determined to have a syntax error. If no syntax error is found in the error messages returned by the target database, or if the syntax error is caused by a false positive, then the erroneous SQL statement is determined not to have a syntax error. A false positive can be understood as a factor that causes the target database to falsely report a syntax error, such as database version incompatibility or improper escaping of special characters. This embodiment does not limit the specific type of false positive factor.

[0025] S120. When it is determined that the erroneous SQL statement has a syntax error, the part of the abstract syntax tree corresponding to the erroneous SQL statement is corrected based on the syntax error information corresponding to the erroneous SQL statement to obtain the first updated abstract syntax tree.

[0026] In this embodiment, syntax error information can be used to reflect the specific database syntax rules violated by the erroneous SQL statement. Syntax error information may include the location and cause of the syntax error in the erroneous SQL statement. A partial abstract syntax tree can be understood as the abstract syntax tree corresponding to the syntax-free portion of the erroneous SQL statement.

[0027] In this step, specifically, if a pre-built parser determines that an erroneous SQL statement contains syntax errors, the parser can obtain the syntax error information that caused the parsing failure, as well as the partial abstract syntax tree (API) generated before the parser interrupted parsing. Then, a syntax correction scheme can be generated based solely on the syntax error information corresponding to the erroneous SQL statement. Alternatively, a syntax correction scheme can be generated based on both the syntax error information and the partial API. Finally, the partial API can be corrected according to the syntax correction scheme to obtain the first updated API.

[0028] If a syntax error is identified in the SQL statement through syntax error reporting, the syntax error information corresponding to the syntax error is obtained, and a partial abstract syntax tree (NPS) corresponding to the erroneous SQL statement is generated based on the syntax error information. Then, based on the syntax error information corresponding to the erroneous SQL statement, the partial NPS corresponding to the erroneous SQL statement is corrected to obtain the first updated NPS.

[0029] S130. Obtain the database semantic graph corresponding to the erroneous SQL statement, and determine whether there is a semantic error in the first update abstract syntax tree based on the database semantic graph.

[0030] In this embodiment, the database semantic graph can be understood as a knowledge graph that systematically describes the semantic relationships, business meanings, and rule constraints of various elements in the target database in the form of a graph structure.

[0031] In this step, specifically, the first updated abstract syntax tree can be semantically validated using the semantic graph of the database, and based on the semantic validation results, it can be determined whether there are semantic errors in the first updated abstract syntax tree.

[0032] S140. When it is determined that there is a semantic error in the first updated abstract syntax tree, the first updated abstract syntax tree is semantically corrected according to the semantic graph of the database to obtain the second updated abstract syntax tree.

[0033] In this step, specifically, based on the semantic graph of the database and the semantic errors existing in the first updated abstract syntax tree, a semantic correction scheme corresponding to the first updated abstract syntax tree can be generated. Then, the semantic correction scheme corresponding to the first updated abstract syntax tree can be used to semantically correct the first updated abstract syntax tree to obtain the second updated abstract syntax tree.

[0034] S150. Based on the second updated abstract syntax tree, generate the erroneous SQL statement with corrected syntax and semantics.

[0035] In this step, specifically, according to the grammar specifications of the target database, a depth-first traversal of the tree nodes of the second updated abstract syntax tree can be performed to map the grammar and semantics of each tree node to the corresponding SQL text fragment, ultimately obtaining the erroneous SQL statement after correcting the syntax and semantics.

[0036] The technical solution of this embodiment obtains erroneous SQL statements and determines whether they contain syntax errors. When a syntax error is found, the abstract syntax tree corresponding to the erroneous SQL statement is corrected based on the syntax error information to obtain a first updated abstract syntax tree. A database semantic graph corresponding to the erroneous SQL statement is obtained, and the first updated abstract syntax tree is determined to contain semantic errors based on the database semantic graph. When a semantic error is found, the first updated abstract syntax tree is semantically corrected based on the database semantic graph to obtain a second updated abstract syntax tree. Based on the second updated abstract syntax tree, a grammatically and semantically corrected erroneous SQL statement is generated. This technical approach solves the problem of existing technologies that rely on manual location and modification of errors in SQL statements, which are not only inefficient but also prone to errors. It improves the efficiency and accuracy of correcting erroneous SQL statements, reduces delivery delays caused by SQL statement errors, and effectively shortens the overall business delivery cycle.

[0037] Example 2 Figure 2 This is a flowchart of another database SQL statement correction method provided by Embodiment 2 of the present invention. This embodiment is a further optimization and extension based on the above embodiments and can be combined with various optional technical solutions in the above embodiments.

[0038] like Figure 2 As shown, this embodiment discloses a database SQL statement correction method, including steps S210-S290.

[0039] S210. Obtain the erroneous SQL statement and perform syntax parsing on the erroneous SQL statement using a pre-built syntax parser.

[0040] In this embodiment, the parser can be a parser generation tool based on context-free grammar, or it can be other custom SQL parsers. This embodiment does not impose any restrictions on this.

[0041] In this step, specifically, a syntax parser can be used to parse the SQL syntax definition file corresponding to the target database to obtain the SQL syntax rules supported by the target database. Then, the syntax parser can be used to parse erroneous SQL statements according to the aforementioned SQL syntax rules.

[0042] S220. When the erroneous SQL statement is successfully parsed, determine that the erroneous SQL statement does not have a syntax error, and perform semantic checks and corrections on the erroneous SQL statement.

[0043] In this step, specifically, when the erroneous SQL statement is successfully parsed, the complete abstract syntax tree (BST) corresponding to the erroneous SQL statement is obtained. Then, based on the database semantic graph corresponding to the erroneous SQL statement, it can be determined whether the BST contains semantic errors. If the BST does not contain semantic errors, it can be assumed that the erroneous SQL statement does not actually have syntactic or semantic errors, or that the syntax or semantic checks performed on the erroneous SQL statement encountered an anomaly. In this case, the erroneous SQL statement can be re-executed in the target database, or manual processing can be performed. When the BST contains semantic errors, it is semantically corrected based on the database semantic graph to obtain a semantically corrected BST, and then the semantically corrected erroneous SQL statement is generated based on the semantically corrected BST.

[0044] S230. When parsing an erroneous SQL statement fails, determine that the erroneous SQL statement contains a syntax error, and obtain the syntax error information and a partial abstract syntax tree corresponding to the erroneous SQL statement.

[0045] In this embodiment, syntax error information can be understood as standardized feedback information thrown by the parser during the parsing of erroneous SQL statements because the SQL statements violate established syntax rules. Syntax error information may include the reason for the syntax error, the location of the syntax error, and the expected legal syntax element corresponding to the location of the syntax error. A partial abstract syntax tree can be understood as the abstract syntax tree generated before the parser's parsing process is interrupted.

[0046] S240. Based on the syntax error information and partial abstract syntax tree corresponding to the erroneous SQL statement, generate a syntax correction scheme corresponding to the erroneous SQL statement, and correct the partial abstract syntax tree according to the syntax correction scheme to obtain the first updated abstract syntax tree.

[0047] In this embodiment, there can be a variety of grammar correction schemes, such as completing missing keywords, correcting spelling errors, and adjusting grammatical structure.

[0048] In this step, specifically, the syntax correction scheme with the highest matching degree to the syntax error information and the context provided by the partial abstract syntax tree can be obtained from a predefined list of syntax correction schemes. The list of syntax correction schemes includes the correspondence between multiple sets of syntax error information, partial abstract syntax trees, and syntax correction schemes.

[0049] S250. Obtain the database semantic graph corresponding to the erroneous SQL statement, and determine whether there is a semantic error in the first update abstract syntax tree based on the database semantic graph.

[0050] Optionally, obtain the database semantic graph corresponding to the erroneous SQL statement, including: obtaining the target database corresponding to the erroneous SQL statement; and constructing the database semantic graph corresponding to the erroneous SQL statement based on the table nodes, column nodes, SQL logical nodes, and dependencies between nodes in the target database. The target database can be understood as the database that triggered the error when the erroneous SQL statement was executed. SQL logical nodes can include table alias nodes, expression nodes, function nodes, and query block nodes, etc. Dependencies between nodes can include column membership relationships, table join relationships, column dependency relationships, parameter passing relationships, and scope containment relationships, etc. Column membership relationships can be used to reflect the table nodes to which each column node belongs. Table join relationships can be used to reflect the association established between two table nodes through a join operation. Column dependency relationships can be used to reflect the column nodes that the expression evaluation depends on. Parameter passing relationships can be used to reflect the column nodes that the function execution depends on. Scope containment relationships can be used to reflect the table nodes or column nodes that the query block can operate on.

[0051] Specifically, semantic binding can be performed on column nodes in the target database to obtain an initial semantic graph corresponding to the erroneous SQL statement. The specific steps for generating the initial semantic graph are as follows: directly bind column nodes containing table name prefixes to their corresponding table nodes; determine the table nodes corresponding to column nodes without table name prefixes based on the set of visible tables corresponding to each SQL logical node, and then bind the column nodes without table name prefixes to their corresponding table nodes.

[0052] After obtaining the initial semantic graph corresponding to the erroneous SQL statement, the initial semantic graph can be optimized based on the dependencies between nodes to obtain the database semantic graph corresponding to the erroneous SQL statement.

[0053] by Figure 3Taking the database semantic graph shown as an example, in the sales model, the user table and order table can be linked through inclusion relationships. The user table, as the core business table in the sales model, carries basic user information and includes the following sub-objects: linked to the user name and user ID columns through column existence dependencies, indicating that the user table contains user name and user ID fields; and linked to the primary key index of this table based on index existence dependencies. The order table, as the business transaction table in the sales model, carries order data and includes the following sub-objects: linked to the order number and user ID columns through column existence dependencies, indicating that the order table contains the primary key column "order number" and the foreign key column "user ID". Specifically, the user ID column in the order table is linked to the user ID column in the user table through a foreign key relationship, and the user ID column in the order table references the user ID column in the user table. At the view level, dependencies are used to link to the user table and order table respectively, indicating that the query logic of this view depends on the data in the user table and order table to generate summary query results at the user dimension.

[0054] Further, in one optional implementation, determining whether the first update abstract syntax tree has a semantic error based on the database semantic graph may include: determining whether each column node in the first update abstract syntax tree corresponds to a valid table node and whether it uniquely matches the corresponding valid table node based on the database semantic graph; when each column node in the first update abstract syntax tree corresponds to a valid table node and uniquely matches the corresponding valid table node, it is determined that the first update abstract syntax tree has no semantic error; when any column node in the first update abstract syntax tree has no corresponding valid table node, or corresponds to multiple valid table nodes, it is determined that the first update abstract syntax tree has a semantic error.

[0055] Among them, a valid table node can be understood as a table node that actually exists in the semantic graph of the database.

[0056] Specifically, if each column node in the first update abstract syntax tree corresponds to a valid table node and uniquely matches its corresponding valid table node, it can be directly determined that the first update abstract syntax tree has no semantic errors. If any column node in the first update abstract syntax tree has no corresponding valid table node, it is determined that the first update abstract syntax tree has a column unbound or table non-existent error, thus determining that the first update abstract syntax tree has semantic errors. If any column node in the first update abstract syntax tree corresponds to multiple valid table nodes, it is determined that the first update abstract syntax tree has a column ambiguity error, thus determining that the first update abstract syntax tree has semantic errors.

[0057] In another optional implementation, determining whether a semantic error exists in the first updated abstract syntax tree (AST) based on the database semantic graph may include: determining whether each column node in the first AST corresponds to a valid table node and whether it uniquely matches the corresponding valid table node; when each column node in the first AST corresponds to a valid table node and uniquely matches the corresponding valid table node, further determining whether the first AST contains semantically incomplete dependencies or semantically inconsistent expression or function parameters based on the database semantic graph. If a semantically incomplete dependency error or a semantically inconsistent expression or function parameter error is determined to exist in the first AST, then a semantic error is determined to exist in the first AST. If neither a semantically incomplete dependency error nor a semantically inconsistent expression or function parameter error is determined to exist in the first AST, then a semantic error is determined to exist in the first AST.

[0058] The advantage of this setup is that by using the database semantic graph, it can determine whether there are semantic errors in the first updated abstract syntax tree. This can accurately identify hidden problems that are grammatically valid but semantically incorrect, thereby avoiding hidden faults such as data not being found or data logic errors after execution.

[0059] S260. When it is determined that there are no semantic errors in the first updated abstract syntax tree, generate the erroneous SQL statement after correcting the syntax based on the first updated abstract syntax tree.

[0060] S270. When it is determined that there is a semantic error in the first update abstract syntax tree, extract the database identifier to be corrected and the SQL statement fragments related to the database identifier to be corrected from the first update abstract syntax tree according to the database semantic graph.

[0061] In this embodiment, the identifier of the database to be corrected may include the table name to be corrected, the column name to be corrected, and the database name to be corrected.

[0062] In this step, specifically, each first database identifier in the first update abstract syntax tree is matched with each second database identifier in the database semantic graph, and the first database identifier whose matching degree with any second database identifier is lower than a preset matching degree threshold is taken as the database identifier to be corrected. Then, the syntactic structure position of the database identifier to be corrected in the first update abstract syntax tree, as well as the set of tables, columns, and aliases that the database identifier to be corrected can legally reference, can be determined. Next, based on the syntactic structure position of the database identifier to be corrected and the set of tables, columns, and aliases that can be legally referenced, the SQL statement fragments related to the database identifier to be corrected can be determined in the first update abstract syntax tree. Finally, the SQL statement fragments related to the database identifier to be corrected can be extracted from the first update abstract syntax tree.

[0063] S280. Based on the database semantic graph, generate multiple candidate semantic correction schemes corresponding to the SQL statement fragments, and determine the target semantic correction scheme from among the candidate semantic correction schemes according to the graph verification rules corresponding to the database semantic graph.

[0064] In this embodiment, candidate semantic correction schemes can be used to replace or repair database identifiers in SQL statement fragments that need correction. For example, replacing erroneous identifiers with valid objects within SQL statement fragments, completing table names or table aliases in cases of column ambiguity, and replacing them with column combinations with constraints in cases of incorrect join conditions. Specifically, candidate semantic correction schemes can include schemes for correcting column non-existence errors, column ambiguity errors, table non-existence errors, and type errors. Specifically, the scheme for correcting column non-existence errors involves calculating candidate column names similar to the erroneous column name based on the current set of columns of valid table nodes, and then recommending replacement with the candidate column name or prompting the user to correct the erroneous column name. The scheme for correcting column ambiguity errors involves automatically adding a table name prefix or table alias to ambiguous columns, or prompting the user to select the corresponding valid table node for the ambiguous column. The scheme for correcting table non-existence errors involves recommending a target table name similar to the erroneous table name, or prompting the user to supplement the table source clause or join clause. The scheme for correcting type errors involves recommending type conversion of the column, or recommending replacement with a column that meets the type requirements.

[0065] Graph validation rules include, but are not limited to, whether the structural constraints of the first updated abstract syntax tree are met, whether the database metadata constraints are met, whether new semantic ambiguities are avoided, and whether the frequency of actual use of the database object is exceeded.

[0066] In this step, specifically, the priority of each candidate semantic correction scheme can be determined based on the graph verification rules corresponding to the database semantic graph, and then the candidate semantic correction scheme with the highest priority can be selected as the target semantic correction scheme. For example, different rule scores can be assigned based on the importance of each graph verification rule, such as whether it satisfies the structural constraints of the first updated abstract syntax tree, whether it satisfies the database metadata constraints, whether it avoids introducing new semantic ambiguity, and whether it exceeds the actual usage frequency of the database object. The importance of each graph verification rule can be determined based on historical experience and user needs. Then, based on the graph verification rules satisfied by each candidate semantic correction scheme, the total rule score of each candidate semantic correction scheme can be determined, and the priority of each candidate semantic correction scheme can be determined based on the total rule score.

[0067] S290. Based on the target semantic correction scheme, perform semantic correction on the SQL statement fragments in the first updated abstract syntax tree to obtain the second updated abstract syntax tree, and generate the erroneous SQL statement after correcting the syntax and semantics based on the second updated abstract syntax tree.

[0068] In this step, specifically, after generating the erroneous SQL statement with corrected syntax and semantics, feedback can be provided to the user regarding the corrected SQL statement with corrected syntax and semantics, as well as the reasons for the syntax and semantic corrections.

[0069] In one optional implementation, when the database identifier to be corrected is an incorrect table name, and the target semantic correction scheme is to replace the incorrect table name in the SQL statement fragment, the SQL statement fragment in the first update abstract syntax tree is semantically corrected according to the target semantic correction scheme to obtain a second update abstract syntax tree. This includes: extracting database object names from the database metadata table corresponding to the first update abstract syntax tree, and determining the similarity between the incorrect table name and each database object name; taking the database object names with a similarity greater than a preset similarity threshold as target table names, and replacing the incorrect table name in the SQL statement fragment with the corresponding target table name to obtain the second update abstract syntax tree.

[0070] Specifically, the database metadata table of the target database corresponding to the erroneous SQL statement can be obtained, and the database object names can be extracted from the database metadata table. The database object names can include database names, table names, and column names, etc. Then, a predefined similarity calculation algorithm can be used to determine the similarity between the erroneous table name and each database object name. The similarity calculation algorithm can include edit distance algorithms and sequence-based algorithms, etc.

[0071] By setting the above, the target table name corresponding to the incorrect table name can be automatically determined, thereby reducing the cost of manual investigation and improving the efficiency of semantic correction.

[0072] In another optional implementation, the SQL statement fragments in the first update abstract syntax tree are semantically corrected according to the target semantic correction scheme to obtain a second update abstract syntax tree. This includes: semantically correcting the SQL statement fragments in the first update abstract syntax tree according to the target semantic correction scheme to obtain an initial update abstract syntax tree; performing semantic verification on the initial update abstract syntax tree based on the table association metadata corresponding to the first update abstract syntax tree; wherein, the table association metadata includes table ownership information, table column dependency relationships, column association relationships, table-level integrity constraints, and column-level data constraints; and using the initial update abstract syntax tree that passes the semantic verification as the second update abstract syntax tree.

[0073] Table ownership information reflects the table name and its schema. Column hierarchy reflects the relationship between tables and columns. Column association reflects the data type and nullability of columns. Table-level integrity constraints reflect primary keys, foreign keys, and uniqueness constraints. Column-level data constraints reflect the relationships between columns in join conditions.

[0074] Specifically, if the initial updated abstract syntax tree (API) passes semantic validation, it can be used as the second API. If the initial API fails semantic validation, the process returns to executing the pre-built parser to parse the erroneous SQL statement until the initial API passes semantic validation or the number of semantic validation attempts reaches a preset threshold. When the preset threshold is reached, semantic correction is considered to have failed, and the syntax and semantic correction process is sent to relevant operations and maintenance personnel for handling.

[0075] For example, based on the table associated metadata corresponding to the first update abstract syntax tree, it can be verified whether the column nodes in the initial update abstract syntax tree actually exist in the specified table node, whether there are constraint relationships or type compatibility relationships between the left and right columns of the join condition, and whether the column nodes participating in the operation in the expression node meet the data type constraints.

[0076] By setting the above parameters, the credibility of semantic correction results can be improved, while avoiding more serious business risks caused by correction operations.

[0077] The technical solution of this embodiment, when parsing an erroneous SQL statement fails, generates a syntax correction scheme corresponding to the erroneous SQL statement based on the syntax error information and a partial abstract syntax tree, and corrects the partial abstract syntax tree according to the syntax correction scheme to obtain a first updated abstract syntax tree; determines whether there is a semantic error in the first updated abstract syntax tree based on the database semantic graph; when a semantic error is determined in the first updated abstract syntax tree, extracts the database identifier to be corrected and the SQL statement fragments related to the database identifier to be corrected from the first updated abstract syntax tree according to the database semantic graph; generates multiple candidate semantic correction schemes corresponding to the SQL statement fragments according to the database semantic graph, and determines the target semantic correction scheme from each candidate semantic correction scheme according to the graph verification rules corresponding to the database semantic graph; performs semantic correction on the SQL statement fragments in the first updated abstract syntax tree according to the target semantic correction scheme to obtain a second updated abstract syntax tree, and generates the erroneous SQL statement after syntax and semantic correction based on the second updated abstract syntax tree. This technical means can quickly and accurately detect and correct the syntax and semantic errors of erroneous SQL statements, thereby effectively shortening the overall business delivery cycle.

[0078] To illustrate the database SQL statement correction method and its effects in this invention in detail, a specific embodiment is described below: Step 1: Capture the SQL statement input by the user and its feedback information after execution in the database, and identify erroneous SQL statements in the user-input SQL statement based on the feedback information. The feedback information may include syntax error information and execution exception information. Step 2: Parse the erroneous SQL statement using a pre-built syntax parser. When parsing fails, obtain the syntax error information that caused the failure, as well as the partial abstract syntax tree generated before the parser interrupted parsing. The syntax error information includes the reason for the syntax error, the location of the syntax error, and the expected legal syntax element corresponding to the syntax error location. Step 3: Using a pre-built rule engine, generate a syntax correction scheme corresponding to the erroneous SQL statement based on the syntax error information and the partial abstract syntax tree. The syntax correction scheme includes completing missing keywords, correcting spelling errors, and adjusting the syntax structure. Then, the partial abstract syntax tree can be corrected according to the syntax correction scheme to obtain a first updated abstract syntax tree. Alternatively, based on the syntax correction scheme, the erroneous SQL statement can be corrected to obtain the erroneous SQL statement after syntax correction.

[0079] Step 4: Obtain the target database corresponding to the erroneous SQL statement, and construct a database semantic graph corresponding to the erroneous SQL statement based on the table nodes, column nodes, SQL logical nodes, and dependencies between nodes in the target database. Then, based on the database semantic graph, determine whether the first updated abstract syntax tree or the corrected erroneous SQL statement contains semantic errors.

[0080] Step 5: When a semantic error is determined in the first updated abstract syntax tree or the erroneous SQL statement after syntax correction, extract the database identifier to be corrected and the SQL statement fragments related to the database identifier to be corrected from the first updated abstract syntax tree or the erroneous SQL statement after syntax correction, based on the database semantic graph. Then, based on the database semantic graph, generate multiple candidate semantic correction schemes corresponding to the SQL statement fragments. Afterwards, according to the graph verification rules corresponding to the database semantic graph, determine the target semantic correction scheme from among the candidate semantic correction schemes. Alternatively, provide feedback on each candidate semantic correction scheme to the user, and determine the target semantic correction scheme from among the candidate semantic correction schemes based on the user's selection. Finally, semantically correct the SQL statement fragments according to the target semantic correction scheme to obtain the second updated abstract syntax tree, or a preliminary corrected SQL statement. Step 6: Use the table association metadata corresponding to the target database to verify the second updated abstract syntax tree, or the preliminary corrected SQL statement. When verification fails, obtain the reason for the failure and report it to the operations and maintenance personnel for correction; when verification succeeds, based on the second updated abstract syntax tree, correct the grammatically and semantically incorrect SQL statement, or use the initially corrected SQL statement as the grammatically and semantically corrected erroneous SQL statement. Step 7: Output the grammatically and semantically corrected erroneous SQL statement, along with the correction reason and semantic verification result.

[0081] It is worth noting that the most detailed implementation described above can be achieved by a database SQL statement correction system. This system includes an input and execution result acquisition module, a syntax parsing and error detection module, a syntax correction rule module, a semantic graph construction and semantic error detection module, a correction rule engine module, a database syntax verification module, a context and environment information management module, and an output module. Specifically, the input and execution result acquisition module is used to execute step one, the syntax parsing and error detection module is used to execute step two, and the syntax correction rule module is used to execute step three. The semantic graph construction and semantic error detection module is used to execute step four, the correction rule engine module is used to execute step five, the database syntax verification module is used to execute step six, and the output module is used to execute step seven. The context and environment information management module provides the database connection information, metadata cache, and historical correction records required for executing the correction rules, ensuring scalability in multi-instance and multi-database environments.

[0082] The advantage of this setup is that by automatically detecting and correcting syntax and semantic errors in SQL statements, the efficiency and accuracy of correcting erroneous SQL statements can be improved, reducing delivery delays caused by SQL statement errors, and thus effectively shortening the overall business delivery cycle.

[0083] Example 3 Figure 4 This is a schematic diagram of a database SQL statement correction device according to Embodiment 3 of the present invention. This embodiment is applicable to the situation of correcting erroneous SQL statements. The database SQL statement correction device can be implemented in hardware and / or software and can be configured in electronic devices such as computers.

[0084] like Figure 4 As shown, the database SQL statement correction device disclosed in this embodiment includes a syntax error identification module 41, a syntax error correction module 42, a semantic error identification module 43, a semantic error correction module 44, and a correction statement generation module 45.

[0085] The syntax error identification module 41 is used to obtain erroneous SQL statements and determine whether there are syntax errors in the erroneous SQL statements.

[0086] The syntax error correction module 42 is used to correct the part of the abstract syntax tree corresponding to the erroneous SQL statement based on the syntax error information corresponding to the erroneous SQL statement when it is determined that the erroneous SQL statement has a syntax error, so as to obtain the first updated abstract syntax tree.

[0087] The semantic error identification module 43 is used to obtain the database semantic graph corresponding to the erroneous SQL statement, and to determine whether there is a semantic error in the first update abstract syntax tree based on the database semantic graph.

[0088] The semantic error correction module 44 is used to semantically correct the first updated abstract syntax tree according to the semantic graph of the database when it is determined that there is a semantic error in the first updated abstract syntax tree, so as to obtain the second updated abstract syntax tree.

[0089] The corrected statement generation module 45 is used to generate erroneous SQL statements with corrected syntax and semantics based on the second updated abstract syntax tree.

[0090] The technical solution in this embodiment, through the cooperation of the syntax error identification module 41, the syntax error correction module 42, the semantic error identification module 43, the semantic error correction module 44, and the correction statement generation module 45, solves the problem that the existing technology of relying on manual location and modification of errors in SQL statements is not only inefficient but also prone to correction errors. It improves the efficiency and accuracy of correcting erroneous SQL statements, reduces delivery delays caused by SQL statement errors, and thus effectively shortens the overall business delivery cycle.

[0091] Optionally, the syntax error identification module 41 is specifically used to: perform syntax parsing on the erroneous SQL statement using a pre-built syntax parser; determine that the erroneous SQL statement has a syntax error when the parsing of the erroneous SQL statement fails; and determine that the erroneous SQL statement does not have a syntax error when the parsing of the erroneous SQL statement succeeds.

[0092] Optionally, the syntax error correction module 42 is specifically used for: obtaining syntax error information and a partial abstract syntax tree corresponding to the erroneous SQL statement; generating a syntax correction scheme corresponding to the erroneous SQL statement based on the syntax error information and the partial abstract syntax tree; and correcting the partial abstract syntax tree according to the syntax correction scheme to obtain the first updated abstract syntax tree.

[0093] Optionally, the semantic error identification module 43 includes: a semantic graph acquisition unit, used to acquire the target database corresponding to the erroneous SQL statement; constructing a database semantic graph corresponding to the erroneous SQL statement based on the table nodes, column nodes, SQL logical nodes in the target database, and the dependencies between the nodes; and a semantic error identification unit, used to determine, based on the database semantic graph, whether each column node in the first update abstract syntax tree corresponds to a valid table node and whether it uniquely matches the corresponding valid table node; when each column node in the first update abstract syntax tree corresponds to a valid table node and uniquely matches the corresponding valid table node, it is determined that the first update abstract syntax tree does not have a semantic error; when any column node in the first update abstract syntax tree does not have a corresponding valid table node, or corresponds to multiple valid table nodes, it is determined that the first update abstract syntax tree has a semantic error.

[0094] Optionally, the semantic error correction module 44 includes: an identifier and statement fragment extraction unit, used to extract the database identifier to be corrected and the SQL statement fragments related to the database identifier to be corrected from the first update abstract syntax tree according to the database semantic graph; a candidate semantic correction scheme generation unit, used to generate multiple candidate semantic correction schemes corresponding to the SQL statement fragments according to the database semantic graph; wherein, the candidate semantic correction schemes are used to replace or repair the database identifier to be corrected in the SQL statement fragments; a target semantic correction scheme determination unit, used to determine the target semantic correction scheme from each candidate semantic correction scheme according to the graph verification rules corresponding to the database semantic graph; and a semantic correction unit, used to perform semantic correction on the SQL statement fragments in the first update abstract syntax tree according to the target semantic correction scheme to obtain a second update abstract syntax tree.

[0095] Optionally, when the database identifier to be corrected is an incorrect table name, and the target semantic correction scheme is to replace the incorrect table name in the SQL statement fragment, the semantic correction unit is specifically used to: extract the database object name from the database metadata table corresponding to the first update abstract syntax tree, and determine the similarity between the incorrect table name and each database object name; take the database object name with a similarity greater than a preset similarity threshold as the target table name, and replace the incorrect table name in the SQL statement fragment with the corresponding target table name to obtain the second update abstract syntax tree.

[0096] Optionally, the semantic correction unit is specifically used to: semantically correct the SQL statement fragments in the first update abstract syntax tree according to the target semantic correction scheme to obtain an initial update abstract syntax tree; semantically verify the initial update abstract syntax tree according to the table association metadata corresponding to the first update abstract syntax tree; wherein, the table association metadata includes table ownership information, table column dependency relationship, column association relationship, table-level integrity constraints, and column-level data constraints; and use the initial update abstract syntax tree that passes the semantic verification as the second update abstract syntax tree.

[0097] The database SQL statement correction device provided in this embodiment of the invention can execute the database SQL statement correction method provided in any embodiment of the invention, and has the corresponding functional modules and beneficial effects of the method execution. Content not described in detail in this embodiment can be referred to the description in any method embodiment of this application.

[0098] Example 4 Figure 5 A schematic diagram of the structure of an electronic device 10 that can be used to implement an embodiment of the present invention is shown.

[0099] like Figure 5As shown, the electronic device 10 includes at least one processor 11 and a memory, such as a read-only memory 12 or a random access memory 13, communicatively connected to the at least one processor 11. The memory stores computer programs executable by the at least one processor. The processor 11 can perform various appropriate actions and processes based on the computer program stored in the read-only memory 12 or loaded from storage unit 18 into the random access memory 13. The random access memory 13 may also store various programs and data required for the operation of the electronic device 10. The processor 11, read-only memory 12, and random access memory 13 are interconnected via a bus 14. An input / output interface 15 is also connected to the bus 14.

[0100] Multiple components in electronic device 10 are connected to input / output interface 15, including: input unit 16, such as keyboard, mouse, etc.; output unit 17, such as various types of monitors, speakers, etc.; storage unit 18, such as disk, optical disk, etc.; and communication unit 19, such as network card, modem, wireless transceiver, etc. Communication unit 19 allows electronic device 10 to exchange information / data with other devices through computer networks such as the Internet and / or various telecommunications networks.

[0101] Processor 11 can be a variety of general-purpose and / or special-purpose processing components with processing and computing capabilities. Some examples of processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special-purpose artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any suitable processor, controller, microcontroller, etc. Processor 11 performs the various methods and processes described above, such as database SQL statement correction methods.

[0102] In some embodiments, the database SQL statement correction method may be implemented as a computer program tangibly contained in a computer-readable storage medium, such as storage unit 18. In some embodiments, part or all of the computer program may be loaded and / or installed on electronic device 10 via read-only memory 12 and / or communication unit 19. When the computer program is loaded into random access memory 13 and executed by processor 11, one or more steps of the database SQL statement correction method described above may be performed. Alternatively, in other embodiments, processor 11 may be configured to execute the database SQL statement correction method by any other suitable means (e.g., by means of firmware).

[0103] Various embodiments of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), systems-on-a-chip (SoCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments may include implementations in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which may be a dedicated or general-purpose programmable processor, capable of receiving data and instructions from a storage system, at least one input device, and at least one output device, and transmitting data and instructions to the storage system, the at least one input device, and the at least one output device.

[0104] Computer programs used to implement the methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, such that when executed by the processor, the computer programs cause the functions / operations specified in the flowcharts and / or block diagrams to be performed. The computer programs may be executed entirely on a machine, partially on a machine, or as a standalone software package, partially on a machine and partially on a remote machine, or entirely on a remote machine or server.

[0105] In the context of this invention, a computer-readable storage medium can be a tangible medium that may contain or store a computer program for use by or in conjunction with an instruction execution system, apparatus, or device. A computer-readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination thereof. Alternatively, a computer-readable storage medium may be a machine-readable signal medium. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory, read-only memory, erasable programmable read-only memory (EPROM or flash memory), optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.

[0106] To provide interaction with a user, the systems and techniques described herein can be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user provides input to the electronic device. Other types of devices can also be used to provide interaction with the user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including sound input, voice input, or tactile input).

[0107] The systems and technologies described herein can be implemented in computing systems that include backend components (e.g., as data servers), or middleware components (e.g., application servers), or frontend components (e.g., user computers with graphical user interfaces or web browsers through which users can interact with implementations of the systems and technologies described herein), or any combination of such backend, middleware, or frontend components. The components of the system can be interconnected via digital data communication of any form or medium (e.g., communication networks). Examples of communication networks include local area networks (LANs), wide area networks (WANs), blockchain networks, and the Internet.

[0108] A computing system can include clients and servers. Clients and servers are generally located far apart and typically interact through communication networks. The client-server relationship is created by computer programs running on the respective computers and having a client-server relationship with each other. The server can be a cloud server, also known as a cloud computing server or cloud host, which is a hosting product within the cloud computing service system to address the shortcomings of traditional physical hosts and VPS services, such as high management difficulty and weak business scalability.

[0109] It should be understood that the various forms of processes shown above can be used, with steps reordered, added, or deleted. For example, the steps described in this invention can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution of this invention can be achieved, and this is not limited herein.

[0110] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this invention should be included within the scope of protection of this invention.

Claims

1. A method for correcting SQL statements in a database, characterized in that, The method includes: Obtain the erroneous SQL statement and determine whether the erroneous SQL statement contains a syntax error; When it is determined that the erroneous SQL statement has a syntax error, the abstract syntax tree corresponding to the erroneous SQL statement is corrected based on the syntax error information corresponding to the erroneous SQL statement to obtain the first updated abstract syntax tree; Obtain the database semantic graph corresponding to the erroneous SQL statement, and determine whether the first update abstract syntax tree has a semantic error based on the database semantic graph; When it is determined that the first updated abstract syntax tree has a semantic error, the first updated abstract syntax tree is semantically corrected according to the semantic graph of the database to obtain the second updated abstract syntax tree; Based on the second updated abstract syntax tree, generate erroneous SQL statements with corrected syntax and semantics.

2. The method according to claim 1, characterized in that, Determining whether the erroneous SQL statement contains a syntax error includes: The erroneous SQL statement is parsed using a pre-built syntax parser. When the parsing of the erroneous SQL statement fails, it is determined that the erroneous SQL statement contains a syntax error; When the erroneous SQL statement is successfully parsed, it is determined that the erroneous SQL statement does not contain any syntax errors.

3. The method according to claim 2, characterized in that, Based on the syntax error information corresponding to the erroneous SQL statement, the partial abstract syntax tree corresponding to the erroneous SQL statement is corrected to obtain a first updated abstract syntax tree, including: Obtain the syntax error information and partial abstract syntax tree corresponding to the erroneous SQL statement; Based on the syntax error information and partial abstract syntax tree corresponding to the erroneous SQL statement, a syntax correction scheme corresponding to the erroneous SQL statement is generated; According to the syntax correction scheme, the partial abstract syntax tree is corrected to obtain the first updated abstract syntax tree.

4. The method according to claim 1, characterized in that, Obtaining the database semantic graph corresponding to the erroneous SQL statement includes: Obtain the target database corresponding to the erroneous SQL statement; Based on the table nodes, column nodes, SQL logical nodes in the target database, and the dependencies between the nodes, construct a database semantic graph corresponding to the erroneous SQL statement; Accordingly, based on the database semantic graph, determining whether the first updated abstract syntax tree contains semantic errors includes: Based on the database semantic graph, determine whether each column node in the first update abstract syntax tree corresponds to a valid table node, and whether it uniquely matches the corresponding valid table node; When each column node in the first updated abstract syntax tree corresponds to a valid table node and is uniquely matched with the corresponding valid table node, it is determined that the first updated abstract syntax tree does not have a semantic error. When any column node in the first updated abstract syntax tree has no corresponding valid table node, or corresponds to multiple valid table nodes, it is determined that the first updated abstract syntax tree has a semantic error.

5. The method according to claim 1, characterized in that, Based on the semantic graph of the database, the first updated abstract syntax tree is semantically corrected to obtain the second updated abstract syntax tree, including: Based on the database semantic graph, extract the database identifier to be corrected from the first update abstract syntax tree, as well as the SQL statement fragments related to the database identifier to be corrected; Based on the database semantic graph, generate multiple candidate semantic correction schemes corresponding to the SQL statement fragment; The candidate semantic correction scheme is used to replace or repair the database identifier to be corrected in the SQL statement fragment; Based on the graph verification rules corresponding to the semantic graph of the database, the target semantic correction scheme is determined from each of the candidate semantic correction schemes; Based on the target semantic correction scheme, the SQL statement fragments in the first updated abstract syntax tree are semantically corrected to obtain the second updated abstract syntax tree.

6. The method according to claim 5, characterized in that, When the database identifier to be corrected is an incorrect table name, and the target semantic correction scheme is to replace the incorrect table name in the SQL statement fragment, the SQL statement fragment in the first update abstract syntax tree is semantically corrected according to the target semantic correction scheme to obtain a second update abstract syntax tree, including: Extract the database object name from the database metadata table corresponding to the first update abstract syntax tree, and determine the similarity between the error table name and each of the database object names; The database object name with a similarity greater than a preset similarity threshold is used as the target table name, and the erroneous table name in the SQL statement fragment is replaced with the corresponding target table name to obtain the second updated abstract syntax tree.

7. The method according to claim 5, characterized in that, According to the target semantic correction scheme, the SQL statement fragments in the first updated abstract syntax tree are semantically corrected to obtain a second updated abstract syntax tree, including: According to the target semantic correction scheme, the SQL statement fragments in the first updated abstract syntax tree are semantically corrected to obtain the initial updated abstract syntax tree; Based on the metadata associated with the table corresponding to the first update abstract syntax tree, semantic verification is performed on the initial update abstract syntax tree; The table association metadata includes table ownership information, inter-column dependency relationships, inter-column association relationships, table-level integrity constraints, and column-level data constraints. The initial updated abstract syntax tree that passes semantic validation is used as the second updated abstract syntax tree.

8. A database SQL statement correction device, characterized in that, The device includes: The syntax error detection module is used to obtain erroneous SQL statements and determine whether the erroneous SQL statements contain syntax errors; The syntax error correction module is used to correct the part of the abstract syntax tree corresponding to the erroneous SQL statement based on the syntax error information corresponding to the erroneous SQL statement when it is determined that the erroneous SQL statement has a syntax error, so as to obtain a first updated abstract syntax tree. The semantic error identification module is used to obtain the database semantic graph corresponding to the erroneous SQL statement, and determine whether there is a semantic error in the first update abstract syntax tree based on the database semantic graph. The semantic error correction module is used to perform semantic correction on the first updated abstract syntax tree according to the semantic graph of the database when it is determined that there is a semantic error in the first updated abstract syntax tree, so as to obtain a second updated abstract syntax tree. The corrected statement generation module is used to generate erroneous SQL statements with corrected syntax and semantics based on the second updated abstract syntax tree.

9. An electronic device, characterized in that, The electronic device includes: At least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores a computer program executable by the at least one processor, the computer program being executed by the at least one processor to enable the at least one processor to perform the database SQL statement correction method according to any one of claims 1-7.

10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions that, when executed by a processor, implement the database SQL statement correction method according to any one of claims 1-7.

Citation Information

Patent Citations

  • Method and system for automatically repairing SQL (Structured Query Language) grammar error

    CN118428355A

  • SQL (Structured Query Language) statement structure verification system based on large model and knowledge graph fusion enhancement

    CN121326958A

  • Database query system and method for converting natural language to SQL (Structured Query Language) statement based on Transform architecture

    CN121524195A

  • SQL grammar conversion method, device and equipment based on large model and storage medium

    CN121681559A

  • SQL statement generation method and system based on execution feedback iterative correction

    CN121786053A