CN-121996686-A - Database SQL sentence correction method, device, equipment and medium
Abstract
The invention discloses a database SQL sentence correction method, device, equipment and medium, which comprise the steps of obtaining an error SQL sentence, determining whether grammar errors exist in the error SQL sentence, correcting a part of abstract syntax trees corresponding to the error SQL sentence based on grammar error information corresponding to the error SQL sentence when the grammar errors exist in the error SQL sentence, obtaining a first updated abstract syntax tree, obtaining a database semantic graph corresponding to the error SQL sentence, determining whether the first updated abstract syntax tree has semantic errors according to the database semantic graph, carrying out semantic correction on the first updated abstract syntax tree according to the database semantic graph when the semantic errors exist in the first updated abstract syntax tree, obtaining a second updated abstract syntax tree, and generating corrected grammar and semantic-after-semantic error SQL according to the second updated abstract syntax tree, so that the correction efficiency and accuracy of the error SQL sentence are improved.
Inventors
- HAN KUN
Assignees
- 天津南大通用数据技术股份有限公司
Dates
- Publication Date
- 20260508
- Application Date
- 20260408
Claims (10)
- 1. A method for modifying a database SQL statement, the method comprising: Acquiring an error SQL sentence, and determining whether the syntax error exists in the error SQL sentence; When determining that the error SQL statement has grammar errors, correcting a part of abstract grammar tree corresponding to the error SQL statement based on grammar error information corresponding to the error SQL statement to obtain a first updated abstract grammar tree; Acquiring a database semantic graph corresponding to the wrong SQL sentence, and determining whether the first updated abstract syntax tree has semantic errors according to the database semantic graph; When determining that the first updating abstract syntax tree has semantic errors, carrying out semantic modification on the first updating abstract syntax tree according to the database semantic graph to obtain a second updating abstract syntax tree; and generating error SQL sentences after grammar correction and semantics correction according to the second updated abstract grammar tree.
- 2. The method of claim 1, wherein determining whether the erroneous SQL statement has a syntax error comprises: carrying out grammar analysis on the error SQL sentence through a pre-constructed grammar analyzer; When the analysis of the error SQL statement fails, determining that the grammar error exists in the error SQL statement; And when the analysis of the error SQL statement is successful, determining that the grammar error does not exist in the error SQL statement.
- 3. The method of claim 2, wherein modifying the partial 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 comprises: Acquiring grammar error information and a part of abstract grammar tree corresponding to the error SQL statement; Generating a grammar correction scheme corresponding to the error SQL statement according to grammar error information corresponding to the error SQL statement and a part of abstract grammar tree; And correcting the partial abstract syntax tree according to the syntax correction scheme to obtain a first updated abstract syntax tree.
- 4. The method of claim 1, wherein obtaining a database semantic graph corresponding to the incorrect SQL statement comprises: acquiring a target database corresponding to the error SQL statement; constructing a database semantic graph corresponding to the error SQL statement according to table nodes, column nodes, SQL logical nodes and dependency relations among the nodes in the target database; Correspondingly, determining whether the first updated abstract syntax tree has semantic errors according to the database semantic graph comprises the following steps: Determining whether each column of nodes in the first updated abstract syntax tree corresponds to an effective table node or not and is uniquely matched with the corresponding effective table node according to the database semantic graph; When each column node in the first updating abstract syntax tree corresponds to an effective table node and is uniquely matched with the corresponding effective table node, determining that the first updating abstract syntax tree has no semantic error; And when any column node in the first updating abstract syntax tree has no corresponding valid table node or corresponds to a plurality of valid table nodes, determining that the first updating abstract syntax tree has semantic errors.
- 5. The method of claim 1, wherein semantically modifying the first updated abstract syntax tree according to the database semantic graph to obtain a second updated abstract syntax tree, comprising: Extracting a database identifier to be corrected in the first updated abstract syntax tree and SQL sentence fragments related to the database identifier to be corrected according to the database semantic graph; generating a plurality of candidate semantic modification schemes corresponding to the SQL sentence fragments according to the database semantic map; the candidate semantic modification scheme is used for replacing or repairing the database identifier to be modified in the SQL sentence fragment; Determining a target semantic modification scheme from each candidate semantic modification scheme according to a map verification rule corresponding to the database semantic map; And carrying out semantic modification on the SQL sentence fragments in the first updating abstract syntax tree according to the target semantic modification scheme to obtain a second updating abstract syntax tree.
- 6. The method of claim 5, wherein when the database identifier to be corrected is an error table name and the target semantic modification scheme is to replace the error table name in the SQL statement fragment, performing semantic modification on the SQL statement fragment in the first update abstract syntax tree according to the target semantic modification scheme to obtain a second update abstract syntax tree, comprising: Extracting database object names from a database metadata table corresponding to the first updated abstract syntax tree, and determining the similarity between the error table names and each database object name; and taking the object names of the database with the similarity larger than the preset similarity threshold value as target table names, and replacing the error table names in the SQL sentence fragments with corresponding target table names to obtain a second updated abstract syntax tree.
- 7. The method of claim 5, wherein semantically modifying the SQL statement fragment in the first updated abstract syntax tree according to the target semantic modification scheme to obtain a second updated abstract syntax tree, comprising: According to the target semantic modification scheme, performing semantic modification on SQL sentence fragments in the first updating abstract syntax tree to obtain an initial updating abstract syntax tree; Performing semantic verification on the initial updating abstract syntax tree according to table association metadata corresponding to the first updating abstract syntax tree; Wherein the table association metadata comprises table attribution information, inter-table-column affiliation, inter-column affiliation, table-level integrity constraints, and column-level data constraints; and taking the initial updating abstract syntax tree passing the semantic verification as a second updating abstract syntax tree.
- 8. A database SQL statement modification apparatus, the apparatus comprising: the grammar error recognition module is used for acquiring an error SQL statement and determining whether the grammar error exists in the error SQL statement; The grammar error correction module is used for correcting a part of abstract grammar tree corresponding to the error SQL sentence based on grammar error information corresponding to the error SQL sentence when the grammar error exists in the error SQL sentence, so as to obtain a first updated abstract grammar tree; the semantic error recognition module is used for acquiring a database semantic graph corresponding to the error SQL sentence and determining whether the first updated abstract syntax tree has semantic errors according to the database semantic graph; The semantic error correction module is used for carrying out semantic correction on the first updating abstract syntax tree according to the database semantic graph to obtain a second updating abstract syntax tree when determining that the first updating abstract syntax tree has semantic errors; and the corrected sentence generating module is used for generating an error SQL sentence after correcting grammar and semantics according to the second updated abstract grammar tree.
- 9. An electronic device, the electronic device comprising: And a memory communicatively coupled to the at least one processor, wherein the memory stores a computer program executable by the at least one processor to enable the at least one processor to perform the database SQL statement modification method of any one of claims 1-7.
- 10. A computer readable storage medium storing computer instructions for causing a processor to implement the database SQL statement modification method of any one of claims 1-7 when executed.
Description
Database SQL sentence correction method, device, equipment and medium Technical Field The present invention relates to the field of database technologies, and in particular, to a method, an apparatus, a device, and a medium for correcting an SQL statement of a database. Background In the actual application scene of the database, when a user writes SQL sentences, the problems of misspelling of keywords, violations of grammar structures, mismatching of column names or table names with metadata of the database and the like are easy to occur. In the prior art, errors in SQL can be prompted only by means of error reporting information of a database, and specific error positioning and modification work still needs to be completed manually, so that the efficiency is low, and correction errors are easy to occur. Disclosure of Invention The invention provides a database SQL sentence correction method, device, equipment and medium, which solve the problems of low efficiency and easy error correction in the prior art relying on manual positioning and correction of errors in SQL sentences, improve the efficiency and accuracy of correcting the error SQL sentences, reduce the delivery delay caused by SQL sentence errors, and further effectively shorten the overall service delivery cycle. The embodiment of the invention provides a database SQL sentence correction method, which comprises the steps of obtaining an error SQL sentence, determining whether grammar errors exist in the error SQL sentence, correcting a part of abstract syntax trees corresponding to the error SQL sentence based on grammar error information corresponding to the error SQL sentence when the grammar errors exist in the error SQL sentence, obtaining a first updated abstract syntax tree, obtaining a database semantic graph corresponding to the error SQL sentence, determining whether the first updated abstract syntax tree has the semantic errors according to the database semantic graph, carrying out semantic correction on the first updated abstract syntax tree according to the database semantic graph when the semantic errors exist in the first updated abstract syntax tree, obtaining a second updated abstract syntax tree, and generating corrected grammar and the error SQL sentence after the semantics according to the second updated abstract syntax tree. The embodiment of the invention also provides a database SQL sentence correction device, which comprises a grammar error identification module, a grammar error correction module and a correction sentence generation module, wherein the grammar error identification module is used for acquiring an error SQL sentence and determining whether the error SQL sentence has grammar errors, the grammar error correction module is used for correcting a part of abstract grammar tree corresponding to the error SQL sentence based on grammar error information corresponding to the error SQL sentence when the error SQL sentence is determined to have grammar errors, so as to obtain a first updated abstract grammar tree, the semantic error identification module is used for acquiring a database semantic map corresponding to the error SQL sentence, determining whether the first updated abstract grammar tree has semantic errors according to the database semantic map, and carrying out semantic correction on the first updated abstract grammar tree according to the database semantic map when the first updated abstract grammar tree is determined to have semantic errors, so as to obtain a second updated abstract grammar tree, and the correction sentence generation module is used for generating corrected grammar and the semantic error SQL sentence according to the second updated abstract grammar tree. In a third aspect, an embodiment of the present invention further provides an electronic device, where the electronic device includes at least one processor, and a memory communicatively connected to the at least one processor, where the memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor, so that the at least one processor can perform the method for correcting a database SQL statement provided by any embodiment of the present invention. In a fourth aspect, an embodiment of the present invention further provides a computer readable storage medium, where the computer readable storage medium stores computer instructions, where the computer instructions are configured to cause a processor to implement the method for correcting a database SQL statement provided in any embodiment of the present invention when executed. According to the technical scheme, whether the grammar errors exist in the error SQL sentence or not is determined, when the grammar errors exist in the error SQL sentence, part of abstract grammar trees corresponding to the error SQL sentence are corrected based on grammar error information corresponding to the error SQL sentence to obtain