Structured query statement rewriting method and device based on large language model and medium

By rewriting the structured query statement twice—first using efficient rules and then leveraging the semantic understanding of a large language model—the accuracy problem of SQL evaluation was solved, and the recall rate and reliability of SQL evaluation were improved.

CN121542285AActive Publication Date: 2026-02-17BEIJING VOLCANO ENGINE TECH CO LTD
View PDF 7 Cites 0 Cited by

Patent Information

Application Number
CN202511882917.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-12-12
Publication Date
2026-02-17
Estimated Expiration
2045-12-12

AI Technical Summary

Technical Problem

In existing technologies, accurately determining whether the SQL generated by the code generation model is qualified is an urgent problem to be solved. Traditional methods suffer from false positives and false negatives, leading to inaccurate evaluations.

Method used

Two rewriting methods are used to rewrite structured query statements equivalently. First, an efficient rule-based rewriting method is used, and then the semantic understanding capability of a large language model is used to rewrite complex semantics, ensuring semantic integrity and improving the SQL recall rate.

Benefits of technology

By using equivalent rewriting, misjudgments caused by semantic differences are avoided, the recall rate of qualified SQL generated by the code generation model is improved, a reliable evaluation basis is provided, and the impact of misjudgments is reduced.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121542285A_ABST
    Figure CN121542285A_ABST
Patent Text Reader

Abstract

The invention discloses a structured query statement rewriting method and device based on a large language model and a medium, and at least relates to the technical field of large language models.The method comprises the steps that a first structured query statement generated by a code generation model based on a natural language is obtained; equivalently rewriting the first structured query statement by adopting a first rewriting mode to obtain a second structured query statement, and equivalently rewriting the second structured query statement by adopting a second rewriting mode to obtain a third structured query statement, therefore, the situation that the third structured query statement is misjudged to be an unqualified structured query statement due to non-semantic difference is avoided; besides, equivalent rewriting is carried out by using a first rewriting mode with higher rewriting efficiency to realize efficient equivalent rewriting of the SQL, and then equivalent rewriting is carried out by using a second rewriting mode with higher rewriting complexity, so that the limitation of a traditional rule is broken through, equivalent rewriting of complex semantics is realized, the generalization of equivalent rewriting is improved, and the efficiency of equivalent rewriting is improved. And the recall rate of the qualified SQL is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This disclosure relates to the fields of large language models and computer technology, and more specifically, to a method, apparatus, and medium for rewriting structured query statements based on large language models. Background Technology

[0002] With the development of large language models, code generation models based on large language model architectures have been widely used. For example, users can describe what they want to query in a database using natural language, and the code generation model can generate corresponding SQL (Structured Query Language) based on the natural language description. This allows users without technical backgrounds to interact with the database using natural language to perform queries, and the reliability and accuracy of the code generation model have become an important foundation for obtaining accurate query results.

[0003] In related technologies, it is possible to recall and analyze unqualified SQL generated by the code generation model in order to enhance or evaluate the generation capability of the code generation model. However, how to accurately determine whether the SQL is qualified is a technical problem that urgently needs to be solved. Summary of the Invention

[0004] This summary section is provided to briefly introduce the concepts, which will be described in detail in the detailed description section below. This summary section is not intended to identify key or essential features of the claimed technical solution, nor is it intended to limit the scope of the claimed technical solution.

[0005] Firstly, this disclosure provides a method for rewriting structured query statements based on a large language model. Obtain the first structured query statement, which is generated by the code generation model based on natural language; The first structured query statement is equivalently rewritten using the first rewriting method to obtain the second structured query statement, and the second structured query statement is equivalently rewritten using the second rewriting method to obtain the third structured query statement. Among them, the rewriting efficiency of the first rewriting method is higher than that of the second rewriting method, the complexity of the structured query statements that the second rewriting method can rewrite is higher than that of the structured query statements that the first rewriting method can rewrite, and the second rewriting method is based on the semantic understanding capability of the large language model to achieve rewriting.

[0006] Secondly, this disclosure provides a structured query statement rewriting device based on a large language model, comprising: The acquisition module is used to acquire a first structured query statement, which is generated by a code generation model based on natural language. The rewriting module is used to rewrite the first structured query statement using a first rewriting method to obtain a second structured query statement, and then rewrite the second structured query statement using a second rewriting method to obtain a third structured query statement. Among them, the rewriting efficiency of the first rewriting method is higher than that of the second rewriting method, the complexity of the structured query statements that the second rewriting method can rewrite is higher than that of the structured query statements that the first rewriting method can rewrite, and the second rewriting method is based on the semantic understanding capability of the large language model to achieve rewriting.

[0007] Thirdly, this disclosure provides a computer-readable medium having a computer program stored thereon, which, when executed by a processing device, implements the steps of the method described in the first aspect.

[0008] Fourthly, this disclosure provides an electronic device, comprising: A storage device on which computer programs are stored; A processing device for executing the computer program in the storage device to implement the steps of the method described in the first aspect.

[0009] Fifthly, this disclosure provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the method described in the first aspect.

[0010] Since equivalent rewriting refers to rewriting without changing the semantics, using the first and second rewriting methods to perform equivalent rewriting on the first structured query statement ensures that when evaluating whether the third structured query statement matches the standard structured query statement, it will not be mistakenly judged as an unqualified structured query statement due to non-semantic differences. Furthermore, by first using the more efficient first rewriting method to achieve efficient equivalent rewriting of SQL, and then using the second rewriting method, which can rewrite more complex SQL statements, the semantic understanding capabilities of the large language model are leveraged to overcome the limitations of traditional rules, achieving equivalent rewriting of complex semantics and improving the generalization of equivalent rewriting. This increases the recall rate of qualified SQL generated by the code generation model, providing a reliable data foundation for the evaluation or enhancement of the code generation model and reducing the impact of misjudged SQL on the evaluation or enhancement of the code generation model.

[0011] Other features and advantages of this disclosure will be described in detail in the following detailed description section. Attached Figure Description

[0012] The above and other features, advantages, and aspects of the embodiments of this disclosure will become more apparent from the accompanying drawings and the following detailed description. Throughout the drawings, the same or similar reference numerals denote the same or similar elements. It should be understood that the drawings are schematic, and the originals and elements are not necessarily drawn to scale. In the drawings: Figure 1 This is a flowchart illustrating a method for rewriting structured query statements based on a large language model, according to an embodiment of this disclosure. Figure 2 This is an architecture diagram of a structured query statement rewriting method based on a large language model, as shown in an embodiment of this disclosure. Figure 3 This is a block diagram illustrating a structured query rewriting apparatus based on a large language model, according to an embodiment of this disclosure. Figure 4 This is a schematic diagram of the structure of an electronic device according to an embodiment of the present disclosure. Detailed Implementation

[0013] Embodiments of this disclosure will now be described in more detail with reference to the accompanying drawings. While some embodiments of this disclosure are shown in the drawings, it should be understood that this disclosure can be implemented in various forms and should not be construed as limited to the embodiments set forth herein. Rather, these embodiments are provided to provide a more thorough and complete understanding of this disclosure. It should be understood that the accompanying drawings and embodiments of this disclosure are for illustrative purposes only and are not intended to limit the scope of protection of this disclosure.

[0014] It should be understood that the steps described in the method embodiments of this disclosure may be performed in different orders and / or in parallel. Furthermore, the method embodiments may include additional steps and / or omit the steps shown. The scope of this disclosure is not limited in this respect.

[0015] The term "comprising" and its variations as used herein are open-ended inclusions, meaning "including but not limited to". The term "based on" means "at least partially based on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". Definitions of other terms will be given in the description below.

[0016] It should be noted that the concepts of "first" and "second" mentioned in this disclosure are used only to distinguish different devices, modules or units, and are not used to limit the order of functions performed by these devices, modules or units or their interdependencies.

[0017] It should be noted that the terms "a" and "a plurality of" used in this disclosure are illustrative rather than restrictive, and those skilled in the art should understand that, unless otherwise expressly indicated in the context, they should be understood as "one or more".

[0018] The names of messages or information exchanged between multiple devices in the embodiments of this disclosure are for illustrative purposes only and are not intended to limit the scope of such messages or information.

[0019] It is understood that before using the technical solutions disclosed in the various embodiments of this disclosure, users should be informed of the types, scope of use, and usage scenarios of the personal information involved in this disclosure in an appropriate manner in accordance with relevant laws and regulations, and user authorization should be obtained.

[0020] For example, upon receiving a user's active request, a prompt message is sent to the user to explicitly inform them that the requested operation will require the acquisition and use of the user's personal information. This allows the user to independently choose whether to provide personal information to the software or hardware, such as the electronic device, application, server, or storage medium performing the operations of this disclosed technical solution, based on the prompt message.

[0021] As an optional but non-limiting implementation, in response to a user's active request, sending a prompt message to the user can be done via a pop-up window, where the prompt message can be presented in text format. Furthermore, the pop-up window can also include a selection control allowing the user to choose "agree" or "disagree" to provide personal information to the electronic device.

[0022] It is understood that the above notification and user authorization process are merely illustrative and do not constitute a limitation on the implementation of this disclosure. Other methods that comply with relevant laws and regulations may also be applied to the implementation of this disclosure.

[0023] Meanwhile, it is understood that the data involved in this technical solution (including but not limited to the data itself, the acquisition or use of the data) shall comply with the requirements of relevant laws, regulations and related provisions.

[0024] In related technologies, the validity of an SQL statement is generally determined based on the execution result of the SQL statement and string matching.

[0025] SQL execution results can produce false positives (i.e., invalid SQL is evaluated as valid SQL). For example, if there are no records in the database showing an age greater than 30, SQL1: (Structured query statements generated by the code generation model) and SQL2: If the execution results of the standard structured query statements are the same, then SQL1 and SQL2 are considered equivalent, and the structured query statements generated by the code generation model are considered qualified. However, since the database may be updated, this method of relying on execution results is not entirely accurate.

[0026] Therefore, to address the drawbacks of SQL-based execution results, string matching can be chosen. However, string matching suffers from false negatives (i.e., valid SQL is evaluated as invalid). It relies heavily on keywords and syntactic structure. For complex SQL statements, even if the SQL generated by the code generation model has the same semantics as standard SQL, it may still be mistakenly judged as invalid simply because of its structural difference.

[0027] In view of this, embodiments of this disclosure provide a method, apparatus, medium, electronic device, and program product for rewriting structured query statements based on a large language model. Since equivalent rewriting refers to rewriting without semantic changes, using a first rewriting method and a second rewriting method to perform equivalent rewriting on the first structured query statement ensures that when evaluating whether the third structured query statement matches the standard structured query statement, the non-semantic differences between the two will not lead to the misjudgment of the third structured query statement as an unqualified structured query statement. Furthermore, by first using the more efficient first rewriting method to perform equivalent rewriting, efficient equivalent rewriting of SQL is achieved. On this basis, the second rewriting method, which can rewrite more complex SQL statements, is then used for equivalent rewriting. That is, based on the semantic understanding capability of the large language model, the limitations of traditional rules are overcome to achieve equivalent rewriting of complex semantics, improving the generalization of equivalent rewriting. This improves the recall rate of qualified SQL generated by the code generation model, thereby providing a reliable data foundation for the evaluation or enhancement of the code generation model and reducing the impact of misjudged SQL on the evaluation or enhancement of the code generation model.

[0028] The following explanation and description of this disclosure are provided in conjunction with the accompanying drawings.

[0029] Figure 1 This is a flowchart illustrating a method for rewriting structured query statements based on a large language model, according to an embodiment of this disclosure. This method can be applied to electronic devices. Furthermore, the method can be executed by a device for rewriting structured query statements based on a large language model, which can be implemented in software and / or hardware, and the software and / or hardware can be configured in an electronic device. (Refer to...) Figure 1The method for rewriting structured query statements based on a large language model may include steps 110 and 120.

[0030] In step 110, a first structured query statement is obtained, which is generated by the code generation model based on natural language.

[0031] The first structured query statement can be a structured query statement generated by the code generation model that is determined to require equivalent rewriting. After the code generation model generates the corresponding structured query statement based on natural language, technical personnel with a coding background or a preset evaluation mechanism can evaluate whether the structured query statement needs equivalent rewriting. If equivalent rewriting is required, then the structured query statement is used as the first structured query statement. This saves resources that would otherwise be used to perform equivalent rewriting on structured query statements that do not require it.

[0032] As an example, for the method of using a preset evaluation mechanism to evaluate whether the structured query statement needs to be equivalently rewritten, the preset evaluation mechanism is used to analyze whether there are differences between the structured query statement and the standard structured query statement. If it is determined that there are differences, the structured query statement is determined to be a structured query statement that needs to be equivalently rewritten.

[0033] In step 120, the first structured query statement is equivalently rewritten using the first rewriting method to obtain the second structured query statement, and the second structured query statement is equivalently rewritten using the second rewriting method to obtain the third structured query statement. The rewriting efficiency of the first rewriting method is higher than that of the second rewriting method. The complexity of the structured query statements that can be rewritten by the second rewriting method is higher than that of the structured query statements that can be rewritten by the first rewriting method. The second rewriting method is based on the semantic understanding capability of the large language model to achieve rewriting.

[0034] The first rewriting method can be a rule-based equivalent rewriting method, such as an equivalent rewriting method based on AST (abstract syntax tree), or an equivalent rewriting method based on regular expressions. Since these equivalent rewriting methods do not require deep semantic understanding, the rewriting efficiency of the first rewriting method is higher than that of the second rewriting method based on a large language model.

[0035] Third-order structured queries are used to evaluate or enhance the code generation capabilities of a code generation model. For example, if a third-order structured query still does not match a given standard structured query, such third-order or first-order structured queries can be recalled. These recalled structured queries can then be analyzed to identify problems in the code generation model and improve it. As an example, string matching can be used to determine whether a third-order structured query matches a standard structured query. Because of the equivalent SQL rewriting, the drawbacks of string matching related to syntax structure and keywords can be avoided.

[0036] Since equivalent rewriting refers to rewriting without changing the semantics, using the first and second rewriting methods to perform equivalent rewriting on the first structured query statement ensures that when evaluating whether the third structured query statement matches the standard structured query statement, it will not be mistakenly judged as an unqualified structured query statement due to non-semantic differences. Furthermore, by first using the more efficient first rewriting method to achieve efficient equivalent rewriting of SQL, and then using the more complex second rewriting method, the semantic understanding capabilities of the large language model are leveraged to overcome the limitations of traditional rules, achieving equivalent rewriting of complex semantics and improving the generalization of equivalent rewriting. This increases the recall rate of qualified SQL generated by the code generation model, providing a reliable data foundation for the evaluation or enhancement of the code generation model and reducing the impact of misjudged SQL on the evaluation or enhancement of the code generation model.

[0037] In some embodiments, the steps of equivalently rewriting the first structured query statement using the first rewriting method to obtain the second structured query statement, and then equivalently rewriting the second structured query statement using the second rewriting method to obtain the third structured query statement, can be implemented as follows: equivalently rewriting the first structured query statement using the first rewriting method to obtain the second structured query statement; evaluating whether the second structured query statement meets the conditions for continued rewriting; if the second structured query statement meets the conditions for continued rewriting, equivalently rewriting the second structured query statement using the second rewriting method to obtain the third structured query statement.

[0038] As an example, the step of evaluating whether the second structured query statement meets the conditions for continued rewriting can be implemented as follows: determine whether there is a difference between the second structured query statement and the standard structured query statement. If there is a difference, the second structured query statement meets the conditions for continued rewriting; if there is no difference, the second structured query statement does not meet the conditions for continued rewriting. Specifically, the string-based matching method described above can be used to determine whether there is a difference between the second structured query statement and the standard structured query statement. If there is no difference, it can be directly proven that the second structured query statement generated by the code generation model is qualified.

[0039] For some simple SQL queries, the first rewriting method can achieve an equivalent rewrite. In this case, there is no need to use the second rewriting method for further equivalent rewriting, thus saving resources used in the large language model's equivalent rewriting process. Therefore, after obtaining the second structured query statement, it can be evaluated. If the second structured query statement meets the conditions for further rewriting, then the second rewriting method can be used to perform an equivalent rewrite, thereby obtaining the third structured query statement.

[0040] In some embodiments, the steps of equivalently rewriting the first structured query statement using the first rewriting method to obtain the second structured query statement, and equivalently rewriting the second structured query statement using the second rewriting method to obtain the third structured query statement, can be implemented in the following manner: obtaining the rewriting type of the code position in the first structured query statement that needs to be equivalently rewritten, wherein the rewriting type is either the first rewriting type corresponding to the first rewriting method or the second rewriting type corresponding to the second rewriting method; using the first rewriting method corresponding to the first rewriting type, equivalently rewriting the code position in the first structured query statement that belongs to the first rewriting type to obtain the second structured query statement; using the second rewriting method corresponding to the second rewriting type, equivalently rewriting the code position in the second structured query statement that belongs to the second rewriting type to obtain the third structured query statement.

[0041] The rewrite types here can be categorized based on the rewrite types that the first and second rewrite methods can handle, respectively. For example, based on an understanding of SQL domain knowledge, we can manually categorize the rewrite types that the first and second rewrite methods can handle. The first rewrite method can handle equivalent rewrites with simple logic, such as case rewriting and alias rewriting; the second rewrite method can handle equivalent rewrites with complex semantic logic, such as multi-level nested subqueries and combinations of multiple conditions.

[0042] As another example, in the process of achieving equivalent rewriting using the following steps: "Using the first rewriting method to perform equivalent rewriting on the first structured query statement to obtain the second structured query statement; evaluating whether the second structured query statement meets the conditions for continued rewriting; if the second structured query statement meets the conditions for continued rewriting, using the second rewriting method to perform equivalent rewriting on the second structured query statement to obtain the third structured query statement," the first, second, and third structured query statements in this process can be analyzed in conjunction with the standard structured query statement. The purpose of this analysis is to analyze, in conjunction with the actual equivalent rewriting, which rewriting types can be implemented by the first rewriting method and which rewriting types must be implemented by the second rewriting method. This updates or determines the rewriting types that the first and second rewriting methods can handle respectively, reducing the possibility of incorrectly classifying the rewriting types that each rewriting method can handle due to subjective interpretation and inconsistent understanding, which could lead to equivalent rewriting failures. In this way, the recall rate of qualified first structured query statements can be further improved, and the reliability and accuracy of relying on third structured query statements to evaluate or enhance code generation models can be enhanced.

[0043] Figure 2 This is an architecture diagram of a structured query statement rewriting method based on a large language model, as shown in the embodiments of this disclosure. (Refer to...) Figure 2 Rewrite types can include general rewrite types, alias rewrite types, time format rewrite types, symbol-based simplification rewrite types, value type rewrite types, conditional statement rewrite types, window function rewrite types, subquery / CTE (Common Table Expression) structure rewrite types, and domain-specific rewrite types. These rewrite types are equivalently rewritten using AST-based rewrite methods and large language model-based rewrite methods.

[0044] The rewriting types include: general rewriting types such as string case conversion, comment removal, and field replacement; time format rewriting types such as date format optimization, eliminating redundant year / month functions, and eliminating redundant today; symbol-based simplification rewriting types such as removing redundant parentheses, tabs, and spaces; value type rewriting types such as changing INT value types to Str value types; conditional statement rewriting types such as rewriting sum / count equivalently to average, merging multiple conditions into an in statement, splitting an in statement into multiple conditions, changing open intervals to closed intervals, changing in conditions to or conditions, rewriting between…and to >=…<=, etc.; window function rewriting; subquery / CTE structure rewriting types such as changing a subquery to a CTE structure, and changing a CTE structure to a subquery; and domain-specific rewriting. It is understood that all the above rewriting types are equivalent rewriting types, and the SQL execution results before and after the rewriting are consistent.

[0045] By analyzing the code locations in the first structured query statement that require equivalent rewriting and determining their rewriting types, the corresponding code locations are rewritten using a rewriting method that corresponds to the rewriting type. This approach eliminates the need to evaluate the second structured query statement, allowing for rapid equivalent rewriting.

[0046] In some embodiments, the step of obtaining the rewrite type of the code location in the first structured query statement that needs to be equivalently rewritten can be implemented in the following way: obtaining the evaluation result of the first structured query statement, the evaluation result being used to describe the code location of the difference code between the first structured query statement and the fourth structured query statement, the fourth structured query statement being a standard structured query statement, the difference code being the code that needs to be equivalently rewritten; identifying the key fields in the difference code of the code location to obtain the rewrite type of the code location.

[0047] The evaluation result of the first structured query statement can be achieved using the string-based matching method described above, to locate the difference codes between the first and fourth structured query statements. It can be understood that the fourth structured query statement serves as label data to evaluate whether the code generation model can generate qualified query statements. If the first and fourth structured query statements are completely identical, then the first structured query statement is considered qualified.

[0048] Taking time format rewriting type as an example, if the code contains a key field such as date, then the rewriting type of that code location can be considered to be time format rewriting type.

[0049] In some embodiments, when the first structured query statement is equivalently rewritten using the first rewriting method, the equivalent rewriting is performed in the following manner: obtaining the abstract syntax tree of the first structured query statement; locating the nodes in the abstract syntax tree that need to be rewritten; and performing equivalent rewriting on the nodes based on preset rewriting rules.

[0050] An AST (Abstract Syntax Tree) represents the syntax structure of SQL using nodes and hierarchical relationships. Each node corresponds to a syntax element in the SQL, and the relationships between nodes reflect the logical connections between syntax elements. Taking conditional statement rewriting as an example, consider the first structured query "select id from table where date>'20240101' and date<= '20240110'". If the GT (Greater Than) node (representing the "greater than" condition node) in the first structured query is located, it is the node that needs to be rewritten. The default rewriting rules include changing the GT node to the GTE (Greater Than or Equal to) node (representing the "greater than or equal to" condition node). Therefore, the first structured query can be equivalently rewritten as "select id from table where date>='20240102' and date<= '20240110'".

[0051] In some embodiments, when the second structured query statement is equivalently rewritten using the second rewriting method, the equivalent rewriting is performed as follows: constructing target prompts, which are used to guide the large language model to perform equivalent rewriting of the second structured query statement; performing semantic understanding on the target prompts using the large language model; and performing equivalent rewriting of the second structured query statement based on the results of the semantic understanding.

[0052] In some embodiments, the step of constructing target prompt words can be implemented in the following manner: based on the query intent, the second structured query statement, the abstract syntax tree of the second structured query statement, and the interpretation of the rewrite type, the corresponding placeholders in the preset prompt word template are filled, the query intent is represented by the above natural language, and the code generation model generates the first structured query statement based on the natural language.

[0053] Here, the preset prompt word template has placeholders set in advance. The query intent, the second structured query statement, the abstract syntax tree of the second structured query statement, and the interpretation of each rewrite type can be filled into the corresponding placeholders to construct the target prompt words.

[0054] In this embodiment, the rewrite type can refer to the above-mentioned related embodiments, which will not be described in this embodiment. The definition of the rewrite type is used to explain each rewrite type through natural language. When constructing prompt words, the definitions of all rewrite types can be filled into the corresponding placeholders.

[0055] In some embodiments, the step of constructing target prompt words can be implemented in the following manner: based on the first structured query statement, the abstract syntax tree of the first structured query statement, the query intent, the second structured query statement, the abstract syntax tree of the second structured query statement, and the interpretation of the rewrite type, the corresponding placeholders in the preset prompt word template are filled to construct target prompt words.

[0056] Here, when constructing target prompts, a first structured query statement and its abstract syntax tree can be additionally introduced to enhance the semantic understanding results of the large language model. Furthermore, explanations and descriptions of other features can be found in the aforementioned related embodiments, and will not be repeated here.

[0057] In some embodiments, the step of constructing target prompt words can be implemented as follows: based on a first structured query statement, an abstract syntax tree of the first structured query statement, a query intent, a second structured query statement, an abstract syntax tree of the second structured query statement, a definition of the rewrite type, and a rewrite example, the corresponding placeholders in the preset prompt word template are filled to construct target prompt words. The rewriting examples here include the structured query statement to be rewritten and the rewritten structured query statement corresponding to the structured query statement to be rewritten. Additional rewriting examples can be introduced when constructing target prompts, which can enhance the semantic understanding results of the large language model. Furthermore, explanations and descriptions of other features can be found in the relevant embodiments described above, and will not be repeated here.

[0058] In some embodiments, when constructing target prompt words, additional rewrite examples corresponding to the rewrite types that the large language model needs to rewrite can be introduced, which can reduce the interference of irrelevant information.

[0059] Figure 3 This is a block diagram illustrating a structured query rewriting apparatus based on a large language model according to an embodiment of this disclosure, with reference to... Figure 3 The structured query rewriting device based on a large language model may include: The acquisition module 301 is used to acquire a first structured query statement, which is generated by a code generation model based on natural language. The rewriting module 302 is used to rewrite the first structured query statement using a first rewriting method to obtain a second structured query statement, and to rewrite the second structured query statement using a second rewriting method to obtain a third structured query statement. Among them, the rewriting efficiency of the first rewriting method is higher than that of the second rewriting method, the complexity of the structured query statements that the second rewriting method can rewrite is higher than that of the structured query statements that the first rewriting method can rewrite, and the second rewriting method is based on the semantic understanding capability of the large language model to achieve rewriting.

[0060] Optionally, the rewriting module 302 includes: The first rewriting submodule is used to rewrite the first structured query statement using a first rewriting method to obtain a second structured query statement; The evaluation submodule is used to evaluate whether the second structured query statement meets the conditions for continuing to rewrite; The second rewriting submodule is used to rewrite the second structured query statement using a second rewriting method to obtain a third structured query statement when the second structured query statement meets the conditions for continued rewriting.

[0061] Optionally, the rewriting module 302 includes: The acquisition submodule is used to acquire the rewrite type of the code position in the first structured query statement that needs to be equivalently rewritten, wherein the rewrite type is a first rewrite type corresponding to the first rewrite method or a second rewrite type corresponding to the second rewrite method; The third rewriting submodule is used to perform equivalent rewriting on the code at the code position belonging to the first rewriting type in the first structured query statement using the first rewriting method corresponding to the first rewriting type, so as to obtain the second structured query statement. The fourth rewriting submodule is used to perform equivalent rewriting on the code at the code position belonging to the second rewriting type in the second structured query statement by adopting the second rewriting method corresponding to the second rewriting type, so as to obtain the third structured query statement.

[0062] Optionally, the acquisition submodule is further configured to: Obtain the evaluation result of the first structured query statement. The evaluation result is used to describe the code position of the difference code between the first structured query statement and the fourth structured query statement. The fourth structured query statement is a standard structured query statement corresponding to the first structured query statement. The difference code is the code that needs to be equivalently rewritten. Identify the key fields in the differing code at the code location to determine the rewrite type of the code location.

[0063] Optionally, when the first structured query statement is equivalently rewritten using the first rewriting method, the equivalent rewriting is performed in the following way: Obtain the abstract syntax tree of the first structured query statement; Locate the nodes in the abstract syntax tree that need to be rewritten; Based on preset rewriting rules, the nodes are rewritten equivalently.

[0064] Optionally, when the second structured query statement is equivalently rewritten using the second rewriting method, the equivalent rewriting is performed in the following way: Construct target prompt words, which are used to guide the large language model to perform equivalent rewriting of the second structured query statement; The target prompt words are semantically understood using the large language model, and the second structured query statement is equivalently rewritten based on the results of the semantic understanding.

[0065] Optionally, the target prompt words can be constructed in the following way: Based on the query intent, the second structured query statement, the abstract syntax tree of the second structured query statement, and the interpretation of the rewrite type, the corresponding placeholders in the preset prompt word template are filled to construct the target prompt word, and the query intent is represented by the natural language.

[0066] The implementation methods of each module in the above-mentioned structured query statement rewriting device 300 based on a large language model can refer to the above-mentioned related embodiments, and will not be repeated here.

[0067] This disclosure also provides a computer-readable medium storing a computer program that, when executed by a processing device, implements the steps of the above-described method for rewriting structured query statements based on a large language model.

[0068] This disclosure also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the above-described method for rewriting structured query statements based on a large language model.

[0069] This disclosure also provides an electronic device, including: A storage device on which computer programs are stored; A processing device is configured to execute the computer program in the storage device to implement the steps of the above-described method for rewriting structured query statements based on a large language model.

[0070] The following is for reference. Figure 4 This diagram illustrates a structural schematic of an electronic device 400 suitable for implementing embodiments of the present disclosure. The terminal devices in these embodiments may include, but are not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), in-vehicle terminals (e.g., in-vehicle navigation terminals), and fixed terminals such as digital TVs and desktop computers. Figure 4 The electronic device shown is merely an example and should not be construed as limiting the functionality and scope of the embodiments disclosed herein.

[0071] like Figure 4 As shown, electronic device 400 may include a processing device (e.g., a central processing unit, a graphics processor, etc.) 401, which can perform various appropriate actions and processes according to a program stored in read-only memory (ROM) 402 or a program loaded from storage device 408 into random access memory (RAM) 403. RAM 403 also stores various programs and data required for the operation of electronic device 400. Processing device 401, ROM 402, and RAM 403 are interconnected via bus 404. Input / output (I / O) interface 405 is also connected to bus 404.

[0072] Typically, the following devices can be connected to I / O interface 405: input devices 406 including, for example, touchscreens, touchpads, keyboards, mice, cameras, microphones, accelerometers, gyroscopes, etc.; output devices 407 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage devices 408 including, for example, magnetic tapes, hard disks, etc.; and communication devices 409. Communication device 409 allows electronic device 400 to communicate wirelessly or wiredly with other devices to exchange data. Although Figure 4 An electronic device 400 with various devices is shown; however, it should be understood that it is not required to implement or possess all of the devices shown. More or fewer devices may be implemented or possessed alternatively.

[0073] In particular, according to embodiments of this disclosure, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of this disclosure include a computer program product comprising a computer program carried on a non-transitory computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via communication device 409, or installed from storage device 408, or installed from ROM 402. When the computer program is executed by processing device 401, it performs the functions defined in the methods of embodiments of this disclosure.

[0074] It should be noted that the computer-readable medium described in this disclosure can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this disclosure, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In this disclosure, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium can be any computer-readable medium other than a computer-readable storage medium, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wires, optical fibers, RF (radio frequency), etc., or any suitable combination thereof.

[0075] In some implementations, electronic devices can communicate using any currently known or future-developed network protocol, such as HTTP (Hypertext Transfer Protocol), and can interconnect with digital data communications (e.g., communication networks) of any form or medium. Examples of communication networks include local area networks (“LANs”), wide area networks (“WANs”), the Internet (e.g., the Internet of Things), and peer-to-peer networks (e.g., ad hoc peer-to-peer networks), as well as any currently known or future-developed networks.

[0076] The aforementioned computer-readable medium may be included in the aforementioned electronic device; or it may exist independently and not assembled into the electronic device.

[0077] The aforementioned computer-readable medium carries one or more programs. When the aforementioned one or more programs are executed by the electronic device, the electronic device causes the following: to obtain a first structured query statement, wherein the first structured query statement is generated by a code generation model based on natural language; to perform an equivalent rewrite of the first structured query statement using a first rewrite method to obtain a second structured query statement, and to perform an equivalent rewrite of the second structured query statement using a second rewrite method to obtain a third structured query statement; wherein the rewrite efficiency of the first rewrite method is higher than that of the second rewrite method, the complexity of the structured query statement that the second rewrite method can rewrite is higher than the complexity of the structured query statement that the first rewrite method can rewrite, and the second rewrite method is implemented based on the semantic understanding capability of a large language model.

[0078] Computer program code for performing the operations of this disclosure can be written in one or more programming languages ​​or a combination thereof, including but not limited to object-oriented programming languages ​​such as Java, Smalltalk, and C++, as well as conventional procedural programming languages ​​such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a local area network (LAN) or a wide area network (WAN)—or can be connected to an external computer (e.g., via the Internet using an Internet service provider).

[0079] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this disclosure. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.

[0080] The modules described in the embodiments of this disclosure can be implemented in software or in hardware. The name of a module does not necessarily limit the module itself; for example, an acquisition module can also be described as "a module for acquiring a first structured query statement".

[0081] The functions described above in this document can be performed at least in part by one or more hardware logic components. For example, exemplary types of hardware logic components that can be used, without limitation, include: field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), system-on-a-chip (SoCs), complex programmable logic devices (CPLDs), and so on.

[0082] In the context of this disclosure, a machine-readable medium can be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, apparatus, or device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can be, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. 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 (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.

[0083] The above description is merely a preferred embodiment of this disclosure and an explanation of the technical principles employed. Those skilled in the art should understand that the scope of this disclosure is not limited to technical solutions formed by specific combinations of the above-described technical features, but should also cover other technical solutions formed by arbitrary combinations of the above-described technical features or their equivalents without departing from the above-described concept. For example, technical solutions formed by substituting the above features with (but not limited to) technical features disclosed in this disclosure that have similar functions.

[0084] Furthermore, while the operations are described in a specific order, this should not be construed as requiring these operations to be performed in the specific order shown or in a sequential order. In certain environments, multitasking and parallel processing may be advantageous. Similarly, while several specific implementation details are included in the above discussion, these should not be construed as limiting the scope of this disclosure. Certain features described in the context of individual embodiments may also be implemented in combination in a single embodiment. Conversely, various features described in the context of a single embodiment may also be implemented individually or in any suitable sub-combination in multiple embodiments.

[0085] Although the subject matter has been described using language specific to structural features and / or methodological logic, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. Rather, the specific features and actions described above are merely illustrative forms of implementing the claims. Regarding the apparatus in the above embodiments, the specific manner in which the various modules perform their operations has been described in detail in the embodiments relating to the method, and will not be elaborated upon here.

Claims

1. A method for rewriting a structured query statement based on a large language model, characterized in that, The method comprises the following steps: obtaining a first structured query statement, wherein the first structured query statement is generated by a code generation model based on natural language; equivalently rewriting the first structured query statement by using a first rewriting mode to obtain a second structured query statement, and equivalently rewriting the second structured query statement by using a second rewriting mode to obtain a third structured query statement; wherein the rewriting efficiency of the first rewriting mode is higher than that of the second rewriting mode, the complexity of the structured query statement that can be rewritten by the second rewriting mode is higher than that of the structured query statement that can be rewritten by the first rewriting mode, and the second rewriting mode is based on the semantic understanding ability of a large language model to realize rewriting.

2. The method of claim 1, wherein, The method comprises the following steps: equivalently rewriting the first structured query statement by using a first rewriting mode to obtain a second structured query statement, and equivalently rewriting the second structured query statement by using a second rewriting mode to obtain a third structured query statement; equivalently rewriting the first structured query statement by using a first rewriting mode to obtain a second structured query statement; evaluating whether the second structured query statement meets a continuous rewriting condition; 3. The method of claim 1, wherein, in the case that the second structured query statement meets the continuous rewriting condition, equivalently rewriting the second structured query statement by using a second rewriting mode to obtain a third structured query statement. The method comprises the following steps: obtaining a rewriting type to which a code position needing equivalent rewriting in the first structured query statement belongs, wherein the rewriting type is a first rewriting type corresponding to the first rewriting mode or a second rewriting type corresponding to the second rewriting mode; equivalently rewriting the code of the code position belonging to the first rewriting type in the first structured query statement by using the first rewriting mode corresponding to the first rewriting type to obtain a second structured query statement; 4. The method of claim 3, wherein, equivalently rewriting the code of the code position belonging to the second rewriting type in the second structured query statement by using the second rewriting mode corresponding to the second rewriting type to obtain a third structured query statement. The method comprises the following steps: obtaining an evaluation result of the first structured query statement, wherein the evaluation result is used to describe a code position of a difference code between the first structured query statement and a fourth structured query statement, the fourth structured query statement is a standard structured query statement corresponding to the first structured query statement, and the difference code is a code needing equivalent rewriting; identifying a key field in the difference code of the code position to obtain a rewriting type to which the code position belongs.

5. The method according to any one of claims 1 to 4, characterized in that, In the case of equivalent rewriting of the first structured query statement by the first rewriting mode, the equivalent rewriting is performed by the following manner: An abstract syntax tree of the first structured query statement is obtained; A node in the abstract syntax tree that needs to be rewritten is located; The node is equivalently rewritten based on a preset rewriting rule.

6. The method according to any one of claims 1 to 4, characterized in that, In the case of equivalent rewriting of the second structured query statement by the second rewriting mode, the equivalent rewriting is performed by the following manner: A target prompt word is constructed, which is used to guide the large language model to equivalently rewrite the second structured query statement; The target prompt word is semantically understood by the large language model, and the second structured query statement is equivalently rewritten according to the result of semantic understanding.

7. The method of claim 6, wherein, The construction of the target prompt word includes: Based on the query intent, the second structured query statement, the abstract syntax tree of the second structured query statement, and the interpretation of the rewriting type, the corresponding placeholder in the preset prompt word template is filled to construct the target prompt word, and the query intent is represented by the natural language. 8.A device for rewriting a structured query statement based on a large language model, characterized by, It includes: An acquisition module is configured to acquire a first structured query statement, which is generated by a code generation model based on a natural language; A rewriting module is configured to equivalently rewrite the first structured query statement by a first rewriting mode to obtain a second structured query statement, and equivalently rewrite the second structured query statement by a second rewriting mode to obtain a third structured query statement; Wherein, the rewriting efficiency of the first rewriting mode is higher than that of the second rewriting mode, the complexity of the structured query statement that can be rewritten by the second rewriting mode is higher than that of the structured query statement that can be rewritten by the first rewriting mode, and the second rewriting mode is based on the semantic understanding ability of the large language model to realize rewriting.

9. A computer readable medium having stored thereon a computer program, characterized in that The computer program is executed by the processing device to realize the steps of the method of any one of claims 1-7.

10. An electronic device, comprising: It includes: A storage device having a computer program stored thereon; A processing device configured to execute the computer program in the storage device to realize the steps of the method of any one of claims 1-7.

11. A computer program product comprising a computer program, characterized in that, The computer program is executed by the processor to realize the steps of the method of any one of claims 1-7. The computer program is executed by the processor to realize the steps of the method of any one of claims 1-7.

Citation Information

Patent Citations

  • Text-to-structured query language conversion method based on large language model

    CN119415546A

  • Method and device for adaptively generating structural query statements in natural language

    CN120144605A

  • Structured query language rewriting method and device, storage medium and electronic equipment

    CN120407593A

  • Model evaluation method and device, computer equipment, storage medium and program product

    CN120492481A

  • Automatic SQL tuning method and system combining large language model and database optimizer, computer equipment and storage medium

    CN120670464A