Structured query language statement generation method and device

By parsing code logic description text, retrieving database metadata and generating natural language questions, SQL statements are automatically constructed, which solves the problems of low efficiency and high error rate of manually written SQL statements, and achieves efficient and accurate SQL statement generation.

CN118747178APending Publication Date: 2024-10-08CHINA ELECTRONICS JINXIN DIGITAL TECH GRP CO LTD
View PDF 0 Cites 3 Cited by

Patent Information

Application Number
CN202411021341.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-26
Publication Date
2024-10-08

AI Technical Summary

Technical Problem

In the existing technology, users need to manually write SQL statements to manage the database, which results in low efficiency and prone to syntax errors. In addition, development efficiency is low, code error rates are high, and test script coverage is incomplete.

Method used

By obtaining the code logic description text, parsing the requirement description information of the target SQL statement, retrieving the metadata information in the reference database, generating natural language questions and reference SQL statements associated with the data table, combining the requirement description information, metadata information and natural Language issues, automatically generate target SQL statements.

Benefits of technology

It realizes the automatic generation of SQL statements without manual writing, improves the accuracy and efficiency of SQL statement generation, ensures that the generated SQL statements meet user needs and matches the database structure, and significantly improves development efficiency and accuracy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118747178A_ABST
    Figure CN118747178A_ABST
Patent Text Reader

Abstract

The invention provides a method and device for generating a structured query language statement, and the method comprises the steps: obtaining a code logic description text, and carrying out the analysis of the code logic description text, so as to obtain the demand description information of a to-be-generated target structured query language statement; according to the demand description information, retrieving reference meta-information of a reference data table in a reference database to obtain target meta-data information matched with the demand description information; generating a plurality of natural language questions and a plurality of reference structured query language statements associated with the reference data table according to the reference meta-information; generating a target structured query language statement according to the demand description information, the target metadata information, the plurality of natural language problems and the plurality of reference structured query language statements; therefore, a reference is provided for generating the target SQL statement, so that the target SQL statement not only meets user requirements, but also is matched with the structure of the reference data table, and meanwhile, the generation accuracy and efficiency of the target SQL statement are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the technical field of data processing, and in particular to a method and device for generating a structured query language statement. Background Art

[0002] In order to meet the needs of database management and data analysis, by generating SQL (Structured Query Language) statements, various operations on the database (such as query operations, storage operations, addition and deletion operations, update operations, etc.) can be accurately defined and executed, so as to efficiently manage and utilize the data stored in the database.

[0003] In the related art, a user manually writes SQL statements to operate and manage a database according to the manually written SQL statements. However, the above-mentioned method of manually writing SQL statements is inefficient and easily leads to syntax errors. Summary of the invention

[0004] The present disclosure provides a method and device for generating a structured query language statement to at least solve one of the technical problems in the related art to a certain extent. The technical solution of the present disclosure is as follows:

[0005] According to a first aspect of an embodiment of the present disclosure, a method for generating a structured query language statement is provided, comprising: obtaining a code logic description text, and parsing the code logic description text to obtain requirement description information of a target structured query language statement to be generated, wherein the requirement description information is used to indicate the code logic information of the target structured query language statement; according to the requirement description information, retrieving reference meta information of a reference data table in a reference database to obtain target metadata information matching the requirement description information; according to the reference meta information, generating a plurality of natural language questions and a plurality of reference structured query language statements associated with the reference data table; and generating the target structured query language statement according to the requirement description information, the target metadata information, the plurality of natural language questions and the plurality of reference structured query language statements.

[0006] According to a second aspect of an embodiment of the present disclosure, a device for generating a structured query language statement is provided, comprising: an acquisition module, used to acquire a code logic description text, and parse the code logic description text to obtain requirement description information of a target structured query language statement to be generated, wherein the requirement description information is used to indicate the code logic information of the target structured query language statement; a retrieval module, used to retrieve reference meta information of a reference data table in a reference database according to the requirement description information, to obtain target metadata information matching the requirement description information; a first generation module, used to generate a plurality of natural language questions and a plurality of reference structured query language statements associated with the reference data table according to the reference meta information; and a second generation module, used to generate the target structured query language statement according to the requirement description information, the target metadata information, the plurality of natural language questions and the plurality of reference structured query language statements.

[0007] According to a third aspect of an embodiment of the present disclosure, an electronic device is provided, comprising: a processor; and a memory for storing instructions executable by the processor; wherein the processor is configured to execute the instructions to implement the method for generating a structured query language statement as described in the embodiment of the first aspect of the present disclosure.

[0008] According to a fourth aspect of an embodiment of the present disclosure, a computer-readable storage medium is provided. When instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the method for generating a structured query language statement as described in the embodiment of the first aspect of the present disclosure.

[0009] According to a fifth aspect of an embodiment of the present disclosure, a computer program product is provided, including: a computer program, which, when executed by a processor, implements the method for generating a structured query language statement as described in the embodiment of the first aspect of the present disclosure.

[0010] The technical solution provided by the embodiments of the present disclosure brings at least the following beneficial effects:

[0011] In the technical scheme, by acquiring the code logic description text and parsing the code logic description text, the requirement description information of the target structured query language statement to be generated is obtained; according to the requirement description information, the reference meta information of the reference data table in the reference database is retrieved to obtain the target metadata information matching the requirement description information; according to the reference meta information, multiple natural language questions and multiple reference structured query language statements associated with the reference data table are generated; according to the requirement description information, the target metadata information, the multiple natural language questions and the multiple reference structured query language statements, the target structured query language statement is generated, thereby, the reference meta information in the reference database is retrieved through the requirement description information of the target SQL statement, the target metadata information matching the requirement description information is obtained, and then, according to the requirement description information, the target metadata information, the multiple natural language questions and the multiple reference structured query language statements, the target structured query language statement is automatically generated, so as to provide a reference for generating the target SQL statement, so that the generated target SQL statement not only meets the user's needs, but also matches the structure of the reference data table, and no manual participation in the writing is required, thereby improving the generation accuracy and efficiency of the target SQL statement; In the method, when generating a target structured query language statement according to the requirement description information, the target metadata information, multiple natural language questions and multiple reference structured query language statements, by acquiring reference resource information similar to the requirement description information, based on the target metadata information matching the requirement description information and the reference resource information similar to the requirement description information, a target SQL statement is generated, thereby effectively combining business requirements, database structure and historical experience, and quickly and accurately building an SQL statement that meets the user's requirements. Moreover, when generating a target SQL statement based on the target metadata information matching the requirement description information and the reference resource information similar to the requirement description information, context information associated with the target SQL statement is built based on the target metadata information, combined with historical SQL statement processing scripts similar to the requirement description information, historical SQL statement requirement documents, natural language questions and reference SQL statements. In this way, the context information not only describes the structure and field information of the database in detail, but also incorporates the generation experience and pattern of historical SQL statements, thereby automatically generating a target SQL statement based on the information-rich context information, which not only meets the actual needs of users, but also significantly improves the efficiency and accuracy of SQL statement writing.

[0012] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS

[0013] The drawings herein are incorporated into and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the description are used to explain the principles of the present disclosure, and do not constitute improper limitations on the present disclosure.

[0014] Figure 1 It is a flowchart of a method for generating a structured query language statement shown in the first embodiment of the present disclosure;

[0015] Figure 2 is a flow chart of a method for generating a structured query language statement according to a second embodiment of the present disclosure;

[0016] Figure 3 is a flow chart of a method for generating a structured query language statement according to a third embodiment of the present disclosure;

[0017] Figure 4 is a flow chart of a method for generating a structured query language statement according to a fourth embodiment of the present disclosure;

[0018] Figure 5 It is a flowchart of a method for generating a structured query language statement shown in an embodiment of the present disclosure;

[0019] Figure 6 is a schematic diagram of the structure of a device for generating a structured query language statement according to a fifth embodiment of the present disclosure;

[0020] Figure 7 It is a schematic structural diagram of an electronic device shown in an exemplary embodiment of the present disclosure. DETAILED DESCRIPTION

[0021] In order to enable ordinary persons in the art to better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the accompanying drawings.

[0022] It should be noted that the terms "first", "second", etc. in the specification and claims of the present disclosure and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present disclosure described herein can be implemented in an order other than those illustrated or described herein. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.

[0023] It should be noted that in the technical solution of the present disclosure, the collection, storage, use, processing, transmission, provision and disclosure of user personal information are all carried out with the user's consent, and are in compliance with relevant laws and regulations and do not violate public order and good morals.

[0024] The main process of current data development is to start from the data processing requirements document, first design the data model, then develop the data processing script according to the design document, and then develop the data test script, and finally launch the tested processing script online; all of the above steps rely on the experience of designers and developers, and refer to the code assistance function of database metadata and SQL statement syntax rules. This approach may lead to low overall development efficiency, high code error rate, and incomplete coverage of test scripts.

[0025] Therefore, in order to solve the above problems, the present disclosure proposes a method and device for generating a structured query language statement.

[0026] The following describes a method and apparatus for generating a structured query language statement according to an embodiment of the present disclosure with reference to the accompanying drawings.

[0027] Figure 1 It is a flowchart of a method for generating a structured query language statement shown in the first embodiment of the present disclosure.

[0028] like Figure 1 As shown, the method for generating the structured query language statement includes the following steps:

[0029] Step 101 : Acquire a code logic description text, and parse the code logic description text to obtain requirement description information of a target structured query language statement to be generated.

[0030] As an example, in response to a user input operation, an input code logic description text associated with a target SQL statement to be generated is obtained, and the code logic description text is used to instruct the user to describe the code logic of the target SQL statement through natural language.

[0031] In order to accurately obtain business requirements, as an example, the code logic description text is parsed to obtain the requirement description information of the target structured query language statement, where the requirement description information includes necessary content such as data source information obtained by the target SQL, core processing logic, and output fields.

[0032] Step 102: According to the requirement description information, reference metadata of the reference data table in the reference database is retrieved to obtain target metadata information matching the requirement description information.

[0033] In order to accurately obtain data information that matches business needs, as a possible implementation method, based on the demand description information, the reference metadata of the reference data table in the reference database is retrieved to obtain the target metadata information that matches the demand description information, wherein the target metadata information includes: target data table, target data field information and target annotation information.

[0034] As an example, based on the requirement description information, a keyword search is performed on the reference meta information to obtain the target data table and target data field information that matches the requirement description information; based on the requirement description information, a fuzzy search is performed on the reference meta information to obtain the target annotation information that matches the requirement description information.

[0035] That is to say, through keyword-based retrieval methods, such as the BM25 algorithm, based on the demand description information, the reference meta-information of the reference data table in the reference database is searched to match the most relevant data table and data field information, that is, the target data table and target data field information; through fuzzy retrieval methods, such as Levenshtein distance, Jaro-Winkler distance, Jaccard similarity and other similarity algorithms, the reference meta-information in the reference database is matched. In addition to the database tables and fields, the annotation information of the database tables and fields is also required to be fuzzy matched to recall relevant data content as much as possible.

[0036] Step 103: Generate multiple natural language questions and multiple reference structured query language statements associated with the reference data table according to the reference meta information.

[0037] In order to simplify the SQL statement writing process and improve the efficiency and accuracy of SQL statement writing, as a possible implementation method, when generating a target SQL statement, multiple natural language questions and multiple reference SQL statements are provided, wherein the multiple natural language questions are used to describe the requirements for generating SQL statements from multiple perspectives, and the multiple reference SQL statements are used to provide statement templates for generating SQL statements.

[0038] As an example, a large model is used to process reference meta-information to obtain multiple natural language questions and multiple reference structured query language statements associated with the reference data table.

[0039] Step 104 : generating a target structured query language statement according to the requirement description information, the target metadata information, a plurality of natural language questions and a plurality of reference structured query language statements.

[0040] In order to accurately generate SQL statements that meet user needs, as an example, the demand description information, target metadata information, multiple natural language questions and multiple reference structured query language statements are combined to jointly generate a target SQL statement.

[0041] It should be noted that the present disclosure does not limit the execution sequence of step 102 and step 103. Step 102 and step 103 can be executed sequentially or in parallel. Figure 1 It is merely assumed that step 103 is executed after step 102 for example.

[0042] The method for generating structured query language statements of the embodiment of the present disclosure obtains the code logic description text and parses the code logic description text to obtain the requirement description information of the target structured query language statement to be generated; according to the requirement description information, the reference meta information of the reference data table in the reference database is retrieved to obtain the target metadata information matching the requirement description information; according to the reference meta information, a plurality of natural language questions and a plurality of reference structured query language statements associated with the reference data table are generated; according to the requirement description information, the target metadata information, the plurality of natural language questions and the plurality of reference structured query language statements, the target structured query language statement is generated. In summary, the reference meta information in the reference database is retrieved according to the requirement description information of the target SQL statement, and then, according to the requirement description information, the target metadata information, the plurality of natural language questions and the plurality of reference structured query language statements, the target structured query language statement is automatically generated, so that a reference is provided for generating the target SQL statement, so that the generated target SQL statement not only meets the user's requirements, but also matches the structure of the reference data table, and no manual participation in the writing is required, so as to improve the generation accuracy and efficiency of the target SQL statement.

[0043] In order to clearly illustrate how the target structured query language statement is generated according to the requirement description information, the target metadata information, multiple natural language questions and multiple reference structured query language statements in the above embodiment, the present disclosure proposes another method for generating a structured query language statement.

[0044] Figure 2 It is a flowchart of a method for generating a structured query language statement shown in the second embodiment of the present disclosure.

[0045] like Figure 2 As shown, the method for generating the structured query language statement includes the following steps:

[0046] Step 201 : Obtain a code logic description text, and parse the code logic description text to obtain requirement description information of a target structured query language statement to be generated.

[0047] The requirement description information is used to indicate the code logic information of the target structured query language statement.

[0048] Step 202: According to the requirement description information, reference metadata of the reference data table in the reference database is retrieved to obtain target metadata information matching the requirement description information.

[0049] Step 203: Generate multiple natural language questions and multiple reference structured query language statements associated with the reference data table based on the reference meta information.

[0050] Step 204 , obtaining similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements that are similar to the requirement description information.

[0051] In order to improve the efficiency and accuracy of SQL writing, as a possible implementation method, reference resource information similar to the requirement description information is obtained. The reference resource information similar to the requirement description information can provide a reference for generating the target SQL statement, thereby improving the efficiency and accuracy of the target SQL statement.

[0052] As an example, the requirement description information is vectorized to obtain a first vectorized representation; based on the first vectorized representation, a vector database is queried to obtain a query result; based on the query result, similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements that are similar to the requirement description information are determined.

[0053] That is to say, in order to accurately and quickly obtain reference resource information similar to the requirement description information, a vectorized representation based on the requirement description information can be used to query the vector database to obtain query results; wherein the query results include: vectorized representations of similar SQL statement processing scripts similar to the requirement description information, vectorized representations of similar SQL statement requirement documents similar to the requirement description information, vectorized representations of similar natural language questions similar to the requirement description information, and vectorized representations of similar SQL statements similar to the requirement description information; and then, the query results are converted to obtain similar structured query language statement processing scripts similar to the requirement description information, similar structured query language statement requirement documents, similar natural language questions, and similar structured query language statements.

[0054] The vector database includes vectorized representations of multiple historical structured query language statement processing scripts, multiple historical structured query language statement requirement documents, multiple natural language questions and multiple reference SQL statements.

[0055] It should be noted that the historical structured query language statement processing script is a script or program that was written and saved historically and is used to modify, generate or optimize SQL statements, and the historical structured query language statement requirement document is a document that was written and saved historically and describes the requirements for generating SQL statements.

[0056] Step 205 : Generate a target structured query language statement based on the target metadata information, similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements.

[0057] In order to accurately generate SQL statements that meet user needs, as an example, the target structured query language statement is jointly generated based on the target metadata information and similar structured query language statement processing scripts similar to the requirement description information, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements.

[0058] It should be noted that the execution process of steps 201 to 203 can be implemented in any manner in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.

[0059] In summary, by obtaining historical structured query language statement processing scripts, historical structured query language statement requirement documents, natural language questions and reference structured query language statements that are similar to the requirement description information; based on the target metadata information, as well as based on similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements, the target structured query language statement is generated. Thus, by obtaining reference resource information similar to the requirement description information, based on the target metadata information that matches the requirement description information and the reference resource information that is similar to the requirement description information, the target SQL statement is generated, thereby achieving an effective combination of business requirements, database structure and historical experience, and quickly and accurately constructing SQL statements that meet user needs.

[0060] In order to clearly illustrate how the target structured query language statement is generated based on the target metadata information in the above embodiment, as well as based on similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements, the present disclosure proposes another method for generating a structured query language statement.

[0061] Figure 3 It is a flowchart of a method for generating a structured query language statement shown in the third embodiment of the present disclosure.

[0062] like Figure 3 As shown, the method for generating the structured query language statement includes the following steps:

[0063] Step 301 : Acquire a code logic description text, and parse the code logic description text to obtain requirement description information of a target structured query language statement to be generated.

[0064] The requirement description information is used to indicate the code logic information of the target structured query language statement.

[0065] Step 302: According to the requirement description information, reference metadata of the reference data table in the reference database is retrieved to obtain target metadata information matching the requirement description information.

[0066] Step 303: Generate multiple natural language questions and multiple reference structured query language statements associated with the reference data table according to the reference meta information.

[0067] Step 304 , obtaining similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements that are similar to the requirement description information.

[0068] Step 305 : Generate context information associated with the target structured query language based on the target metadata information, similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements.

[0069] In order to improve the efficiency of SQL generation, as a possible implementation method, the metadata information, historical experience and actual query requirements of the database are integrated to automatically build a rich and accurate context information, so as to generate SQL statements based on the context information.

[0070] As an example, the target metadata information, similar structured query language statement processing script, similar structured query language statement requirement document, similar natural language questions and similar structured query language statements are formatted and marked using a specified syntax; context information is generated based on the formatted and marked target metadata information, the formatted and marked similar structured query language statement processing script, the formatted and marked similar structured query language statement requirement document, the formatted and marked similar natural language questions and the formatted and marked similar structured query language statements.

[0071] For example, taking the designated syntax as Markdown syntax, the target metadata information, similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements are formatted, marked and organized using Markdown syntax to generate context information.

[0072] Step 306: Generate a target structured query language statement according to the context information.

[0073] In order to improve the richness of the target SQL statements, as an example, the target SQL statements include target SQL statements of multiple statement types, and the target SQL statements of multiple statement types are generated according to the context information.

[0074] As an example, a target structured query language statement of a first statement type is generated according to context information; wherein the target structured query language statement of the first statement type is used to create a data table; the target structured query language statement of the first statement type is executed, and in response to the successful execution of the SQL statement of the first statement type, a target SQL statement of a second statement type is generated according to the context information; wherein the target structured query language statement of the second statement type is used to process the data table; the target structured query language statement of the second statement type is executed, and in response to the successful execution of the structured query language statement of the second statement type, a target structured query language statement of a third statement type is generated according to the context information; wherein the target structured query language statement of the third statement type is used to test the data of the data table.

[0075] That is, a large model is used to process the context information to obtain a target SQL statement of a first statement type, wherein the target SQL statement of the first statement type is used to create a data table; then, the target SQL statement of the first statement type is executed, and when the target structured query language statement of the first statement type is executed successfully, the large model is used to process the context information to obtain a target SQL statement of a second statement type, wherein the target SQL statement of the second statement type is used to process the data table; then, the target SQL statement of the second statement type is executed, and when the target SQL statement of the second statement type is executed successfully, the large language model is used to process the context information to obtain a target SQL statement of a third statement type, wherein the target structured query language statement of the third statement type is used to test the data of the data table; then, the target SQL statement of the third statement type is executed, and when the target SQL statement of the third statement type is executed successfully, the target SQL statement of the first statement type, the target SQL statement of the second statement type, and the target SQL statement of the third statement type are used as target SQL statements.

[0076] It should be noted that after generating a target structured query language statement of the first statement type, it is necessary to perform a syntax check on the target SQL statement of the first statement type. When the target structured query language statement of the first statement type passes the syntax check, the target SQL statement of the first statement type is executed; when the target structured query language statement of the first statement type fails the syntax check, an exception prompt information is generated, wherein the exception prompt information is used to indicate at least one of the following: a syntax error in the target structured query language statement of the first statement type; and regenerate context information, and regenerate the target structured query language statement of the first statement type based on the regenerated context information.

[0077] After generating a target structured query language statement of the second statement type, it is necessary to perform syntax check on the target SQL statement of the second statement type. When the target structured query language statement of the second statement type passes the syntax check, the target SQL statement of the second statement type is executed; when the target structured query language statement of the second statement type fails the syntax check, abnormal prompt information is generated, wherein the abnormal prompt information is used to indicate at least one of the following: a syntax error in the target structured query language statement of the second statement type; and regenerate context information, and regenerate the target structured query language statement of the second statement type based on the regenerated context information.

[0078] Similarly, after generating a target structured query language statement of the third statement type, it is necessary to perform a syntax check on the target SQL statement of the third statement type. When the target structured query language statement of the third statement type passes the syntax check, the target SQL statement of the third statement type is executed; when the target structured query language statement of the third statement type fails to pass the syntax check, an exception prompt information is generated, wherein the exception prompt information is used to indicate at least one of the following: a syntax error in the target structured query language statement of the third statement type; and regenerate context information, and regenerate the target structured query language statement of the third statement type based on the regenerated context information.

[0079] It should be noted that the execution process of steps 301 to 304 can be implemented in any manner in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.

[0080] In summary, based on the target metadata information, as well as based on similar historical structured query language statement processing scripts, historical structured query language statement requirement documents, natural language questions and reference structured query language statements, context information associated with the target structured query language is generated; based on the context information, the target structured query language statement is generated, thereby, based on the target metadata information, combined with similar SQL statement processing scripts, similar SQL statement requirement documents, similar natural language questions and similar reference SQL statements that are similar to the requirement description information, the context information associated with the target SQL statement is constructed, so that the context information not only describes the structure and field information of the database in detail, but also incorporates the generation experience and patterns of historical SQL statements, so that the target SQL statement is automatically generated based on the information-rich context information, which not only meets the actual needs of users, but also significantly improves the efficiency and accuracy of SQL statement writing.

[0081] In order to clearly explain how to generate multiple natural language questions and multiple reference structured query language statements associated with the reference data table according to the reference meta-information in the above embodiment, the present disclosure proposes another method for generating structured query language statements.

[0082] Figure 4 It is a flowchart of a method for generating a structured query language statement shown in the fourth embodiment of the present disclosure.

[0083] like Figure 4 As shown, the method for generating the structured query language statement includes the following steps:

[0084] Step 401 , obtaining a code logic description text, and parsing the code logic description text to obtain requirement description information of a target structured query language statement to be generated.

[0085] The requirement description information is used to indicate the code logic information of the target structured query language statement.

[0086] Step 402: According to the requirement description information, reference metadata of the reference data table in the reference database is retrieved to obtain target metadata information matching the requirement description information.

[0087] Step 403, obtaining a prompt template.

[0088] The prompt template is used to indicate the task information to be executed by the large model.

[0089] In order to accurately obtain multiple natural language questions and multiple reference SQL statements associated with the reference data table, as an example, a large model is used to process the reference meta information to obtain multiple natural language questions and multiple reference structured query language statements associated with the reference data table. Since the reference meta information needs to be filled into the prompt template of the large model when the large model is used to process the reference meta information, the prompt template for indicating the task information to be executed by the large model is first obtained.

[0090] Step 404: fill the prompt template with reference meta information to obtain model prompt information.

[0091] Then, the prompt template is filled with the reference meta-information to obtain the model prompt information.

[0092] For example, the model prompts for entering a large model include:

[0093] {

[0094] #Character Description

[0095] You are a professional data analyst. Based on the structure information of the following database table, design 5 BI problems that fit the actual business and generate corresponding SQL query statements for the problems. / / Task information

[0096] #Database metadata

[0097] <Data table creation statement> / / text filling position

[0098] }.

[0099] Step 405 , input the model prompt information into the large model for processing to obtain a plurality of natural language questions and a plurality of reference structured query language statements.

[0100] Furthermore, the model prompt information is input into the big model, so that the big model generates a plurality of natural language questions and a plurality of reference SQL statements based on the model prompt information.

[0101] Step 406 , generating a target structured query language statement according to the requirement description information, the target metadata information, the multiple natural language questions and the multiple reference structured query language statements.

[0102] It should be noted that the execution process of steps 401 to 402 and step 406 can be implemented in any manner in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.

[0103] In addition, it should be noted that the present disclosure performs step 401 and step 402 sequentially, and the execution sequence of step 403 and steps 401 and 402 is not limited. Step 403 may be performed before step 401, after step 402, between step 401 and step 402, or in parallel with step 401 and / or step 402. Figure 4 It is only assumed that step 403 is executed after step 402 for example.

[0104] In summary, by obtaining a prompt template; using reference meta-information to fill the prompt template to obtain model prompt information; inputting the model prompt information into a large model for processing to obtain multiple natural language questions and multiple reference structured query language statements, thereby using a large model to process the reference meta-information to obtain multiple natural language questions and multiple reference structured query language statements, thereby improving the accuracy and efficiency of generating multiple natural language questions and multiple reference structured query language statements.

[0105] Based on any embodiment of the present disclosure, Figure 5 As shown, the method for generating the structured query language statement of the embodiment of the present disclosure can also be implemented based on the following steps:

[0106] S01. Requirements identification and decomposition

[0107] The user describes the code logic of the SQL statement to be generated in natural language, that is, the code logic description text is parsed to obtain the requirement description information of the target structured query language statement to be generated, where the requirement description information includes necessary content such as the data source information obtained by the SQL statement, the core processing logic, and the output fields;

[0108] S02. Keyword search

[0109] Through keyword-based search methods, such as the BM25 algorithm, the user inputs the requirements, searches the existing data table metadata in the database, and matches the most relevant data table and data field information (i.e., the target data table and target field in the target metadata information that matches the requirement description information);

[0110] S03, fuzzy search

[0111] Through fuzzy retrieval, such as Levenshtein distance, Jaro-Winkler distance, Jaccard similarity and other classic algorithms, the meta-information in the database is matched. In addition to database tables and fields, the annotation information of database tables and fields (that is, the target annotation information in the target metadata information that matches the requirement description information) needs to be fuzzily matched to recall relevant data content as much as possible;

[0112] S04. Semantic vector retrieval

[0113] In order to improve the accuracy of SQL statements generated by large models (such as large language models), the data tables in the database are first analyzed, and the large model is allowed to generate natural language questions and corresponding reference SQL statements based on these data tables as reference information for subsequent generation of SQL statements. That is, based on the reference meta-information, multiple natural language questions and multiple reference structured query language statements associated with the reference data table are generated; for example, 5 question-answer pairs can be generated for each database table, and the prompt information input to the large model may include:

[0114] {

[0115] #Character Description

[0116] You are a professional data analyst. Based on the structure information of the following database tables, design 5 BI problems that fit the actual business and generate corresponding SQL statements for the problems.

[0117] #Database metadata

[0118] <Data table creation statement> / / text filling position

[0119] }

[0120] S05. Semantic vector retrieval

[0121] Through the embedding model, the requirements (i.e., the requirements description information) input by the user are vectorized, and various types of metadata of the database, historical SQL statement processing scripts, other data processing requirements documents, and the questions and SQL statement contents generated in S04 are vectorized in advance to form a vector database. The vectorized information input by the user is used to calculate the cosine similarity with all the data in the vector database, and the top 3 or top 5 results with the highest similarity in each category are returned; the specific number of results returned depends on the input data length acceptable to the large model;

[0122] The calculation formula for vector similarity is:

[0123] Assume that there are two vectors A and B, and their representations in n-dimensional space are A = (a1, a2, ..., an) and B = (b1, b2, ..., bn). The calculation formula of the cosine similarity between A and B is:

[0124] CosineSimilarity=A·B / ‖A‖‖B‖;

[0125] Among them, A·B represents the dot product (inner product) of vector A and vector B, and the calculation formula is expressed as: A·B = a1×b1 + a2×b2 +... + an×bn;

[0126] ‖A‖ and ‖B‖ respectively represent the Euclidean norms (lengths) of vector A and vector B;

[0127] The value range of the cosine similarity is from -1 to 1:

[0128] When two vectors are completely opposite, the cosine similarity is -1;

[0129] When two vectors are exactly the same, the cosine similarity is 1; when two vectors are orthogonal (i.e., have no similarity), the cosine similarity is 0;

[0130] S06. Organize the retrieval results obtained in S02, S03, and S05 and organize them according to the syntax of Markdown to generate context information for the large model to understand and process. Among them, the context information specifically includes:

[0131] {

[0132] # Data processing requirement description

[0133] <Requirement description text>

[0134] # Data tables involved

[0135] <Data tables and fields and annotations in each data table>

[0136] # Related code

[0137] <SQL code snippet related to this problem>

[0138] # Related documents

[0139] <Other requirement description documents related to this problem>

[0140] # Reference Q&A pairs

[0141] <Q&A pairs generated in S04>

[0142] # Other SQL generation requirements

[0143] <Such as which database SQL dialect to generate>

[0144] }

[0145] S07. Based on the context information generated by the large model in S06, generate and execute the table creation statements for the requirements; that is, generate the target structured query language statements of the first statement type according to the context information;

[0146] S08, in response to the successful execution of the table creation statement, the large language model generates and executes the required processing statement based on the context information generated in S06; that is, generates a target structured query language statement of the second statement type according to the context information;

[0147] S09, in response to the successful execution of the processed statement, the large model generates a test statement of the current requirement based on the context information generated in S06; and generates a target structured query language statement of the third statement type according to the context information;

[0148] S10, using a code checking tool, such as sqlparser or sqlglot, to perform syntax checking on the generated execution statements (table creation statements and processing statements) and test statements, and generate feedback information on the syntax checking results. If the syntax checking fails, the error information is returned to S06, requiring the large model to modify and generate the corresponding SQL statements according to the error information;

[0149] As an example, after generating a target structured query language statement of a first statement type according to context information, a syntax check is performed on the target structured query language statement of the first statement type; in response to the target structured query language statement of the first statement type passing the syntax check, the target structured query language statement of the first statement type is executed; in response to the target structured query language statement of the first statement type failing the syntax check, an exception prompt message is generated; the exception prompt message is used to indicate that the target structured query language statement of the first statement type has a syntax error; the context information is regenerated, and based on the regenerated context information, the target structured query language statement of the first statement type is regenerated. Similarly, after generating a target structured query language statement of a second statement type and generating a target structured query language statement of a third statement type, the target structured query language statement of the second statement type and the target structured query language statement of the third statement type are respectively syntax checked.

[0150] Corresponding to the method for generating a structured query language statement provided in the above embodiment, the present disclosure further provides a device for generating a structured query language statement. Since the device for generating a structured query language statement provided in the embodiment of the present disclosure corresponds to the method for generating a structured query language statement provided in the above embodiment, the implementation method of the method for generating a structured query language statement is also applicable to the device for generating a structured query language statement provided in the embodiment of the present disclosure, and will not be described in detail in the embodiment of the present disclosure.

[0151] Figure 6 It is a schematic diagram of the structure of a device for generating structured query language statements shown in the fifth embodiment of the present disclosure.

[0152] like Figure 6As shown, the structured query language statement generating device 600 includes: an acquisition module 610 , a retrieval module 620 , a first generating module 630 and a second generating module 640 .

[0153] Among them, the acquisition module 610 is used to acquire the code logic description text and parse the code logic description text to obtain the requirement description information of the target structured query language statement to be generated, wherein the requirement description information is used to indicate the code logic information of the target structured query language statement; the retrieval module 620 is used to retrieve the reference meta information of the reference data table in the reference database according to the requirement description information to obtain the target metadata information matching the requirement description information; the first generation module 630 is used to generate multiple natural language questions and multiple reference structured query language statements associated with the reference data table according to the reference meta information; the second generation module 640 is used to generate the target structured query language statement according to the requirement description information, the target metadata information, the multiple natural language questions and the multiple reference structured query language statements.

[0154] As a possible implementation method of the embodiment of the present disclosure, the second generation module 640 is used to obtain similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements that are similar to the requirement description information; based on the target metadata information, and based on the similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements, generate a target structured query language statement.

[0155] As a possible implementation method of the embodiment of the present disclosure, the second generation module 640 is used to vectorize the requirement description information to obtain a first vectorized representation; based on the first vectorized representation, query the vector database to obtain a query result; based on the query result, determine similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements that are similar to the requirement description information; wherein the vector database includes vectorized representations of multiple historical structured query language statement processing scripts, multiple historical structured query language statement requirement documents, multiple natural language questions and multiple reference SQL statements.

[0156] As a possible implementation method of the embodiment of the present disclosure, the second generation module 640 is used to generate context information associated with the target structured query language based on the target metadata information, and based on similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements; and generate the target structured query language statement according to the context information.

[0157] As a possible implementation method of the embodiment of the present disclosure, the second generation module 640 is used to format and mark the target metadata information, similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements using a specified syntax; and generate context information based on the formatted and marked target metadata information, the formatted and marked similar structured query language statement processing scripts, the formatted and marked similar structured query language statement requirement documents, the formatted and marked similar natural language questions and the formatted and marked similar structured query language statements.

[0158] As a possible implementation of the embodiment of the present disclosure, the target structured query language statement includes target structured query language statements of multiple statement types, and the second generation module 640 is used to generate a target structured query language statement of a first statement type according to the context information; wherein the target structured query language statement of the first statement type is used to create a data table; execute the target structured query language statement of the first statement type, and in response to the target structured query language statement of the first statement type being successfully executed, generate a target structured query language statement of a second statement type according to the context information; wherein the target structured query language statement of the second statement type is used to process the data table; execute the target structured query language statement of the second statement type, and in response to the target structured query language statement of the second statement type being successfully executed, generate a target structured query language statement of a third statement type according to the context information; wherein the target structured query language statement of the third statement type is used to test the data of the data table.

[0159] As a possible implementation manner of the embodiment of the present disclosure, the structured query language statement generating device 600 further includes: a verification module.

[0160] The verification module is used to perform syntax verification on the target structured query language statement of the first statement type; in response to the target structured query language statement of the first statement type passing the syntax verification, executing the target structured query language statement of the first statement type; in response to the target structured query language statement of the first statement type failing the syntax verification, generating abnormal prompt information; wherein the abnormal prompt information is used to indicate at least one of the following: the target structured query language statement of the first statement type has a syntax error; regenerate context information, and regenerate the target structured query language statement of the first statement type based on the regenerated context information.

[0161] As a possible implementation method of the embodiment of the present disclosure, the first generation module 630 is used to obtain a prompt template, wherein the prompt template is used to indicate the task information to be executed by the large model; the prompt template is filled with reference meta-information to obtain model prompt information; the model prompt information is input into the large model for processing to obtain multiple natural language questions and multiple reference structured query language statements.

[0162] As a possible implementation method of the embodiment of the present disclosure, the target metadata information includes a target data table, target data field information and target annotation information. The retrieval module 620 is used to perform a keyword search on the reference meta information according to the requirement description information to obtain the target data table and target data field information that match the requirement description information; and perform a fuzzy search on the reference meta information according to the requirement description information to obtain the target annotation information that matches the requirement description information.

[0163] The structured query language statement generation device of the embodiment of the present disclosure obtains the code logic description text and parses the code logic description text to obtain the demand description information of the target structured query language statement to be generated; according to the demand description information, the reference meta information of the reference data table in the reference database is retrieved to obtain the target metadata information matching the demand description information; according to the reference meta information, a plurality of natural language questions and a plurality of reference structured query language statements associated with the reference data table are generated; according to the demand description information, the target metadata information, the plurality of natural language questions and the plurality of reference structured query language statements, the target structured query language statement is generated, thereby, the reference meta information in the reference database is retrieved by the demand description information of the target SQL statement, and then, according to the demand description information, the target metadata information, the plurality of natural language questions and the plurality of reference structured query language statements, the target structured query language statement is automatically generated, so that a reference is provided for generating the target SQL statement, so that the generated target SQL statement not only meets the user demand, but also matches the structure of the reference data table, and no manual participation in the writing is required, thereby improving the generation accuracy and efficiency of the target SQL statement.

[0164] In an exemplary embodiment, an electronic device is also provided.

[0165] Among them, electronic equipment includes:

[0166] processor;

[0167] a memory for storing processor-executable instructions;

[0168] The processor is configured to execute instructions to implement the method for generating a structured query language statement as proposed in any of the aforementioned embodiments.

[0169] As an example, Figure 7 is a schematic diagram of the structure of an electronic device 700 shown in an exemplary embodiment of the present disclosure, such as Figure 7 As shown, the electronic device 700 may further include:

[0170] The memory 710 and the processor 720, and the bus 730 connecting different components (including the memory 710 and the processor 720), the memory 710 stores a computer program, and when the processor 720 executes the program, the method for generating a structured query language statement described in the embodiment of the present disclosure is implemented.

[0171] Bus 730 represents one or more of several types of bus structures, including a memory bus or memory controller, a peripheral bus, an accelerated graphics port, a processor or a local bus using any of a variety of bus architectures. For example, these architectures include but are not limited to Industry Standard Architecture (ISA) bus, Micro Channel Architecture (MAC) bus, Enhanced ISA bus, Video Electronics Standards Association (VESA) local bus and Peripheral Component Interconnect (PCI) bus.

[0172] The electronic device 700 typically includes a variety of electronic device readable media. These media can be any available media that can be accessed by the electronic device 700, including volatile and non-volatile media, removable and non-removable media.

[0173] The memory 710 may also include computer system readable media in the form of volatile memory, such as random access memory (RAM) 740 and / or cache memory 750. The server 700 may further include other removable / non-removable, volatile / non-volatile computer system storage media. By way of example only, the storage system 760 may be used to read and write non-removable, non-volatile magnetic media ( Figure 7 not shown, usually called a "hard drive"). Although Figure 7 Not shown, a disk drive for reading and writing to a removable non-volatile disk (e.g., a "floppy disk"), and an optical disk drive for reading and writing to a removable non-volatile optical disk (e.g., a CD-ROM, a DVD-ROM, or other optical media) may be provided. In these cases, each drive may be connected to the bus 730 via one or more data medium interfaces. The memory 710 may include at least one program product having a set (e.g., at least one) of program modules that are configured to perform the functions of the various embodiments of the present disclosure.

[0174] A program / utility 780 having a set (at least one) of program modules 770 may be stored, for example, in the memory 710, such program modules 770 including but not limited to an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment. The program modules 770 generally perform the functions and / or methods of the embodiments described in the present disclosure.

[0175] The electronic device 700 may also communicate with one or more external devices 790 (e.g., keyboard, pointing device, display 791, etc.), may also communicate with one or more devices that enable a user to interact with the electronic device 700, and / or communicate with any device that enables the electronic device 700 to communicate with one or more other computing devices (e.g., network card, modem, etc.). Such communication may be performed through an input / output (I / O) interface 792. Furthermore, the electronic device 700 may also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN) and / or public network, such as the Internet) through a network adapter 793. As shown, the network adapter 793 communicates with other modules of the electronic device 700 through a bus 730. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the electronic device 700, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.

[0176] The processor 720 executes various functional applications and data processing by running the programs stored in the memory 710 .

[0177] It should be noted that the implementation process and technical principles of the electronic device of this embodiment refer to the aforementioned explanation of the method for generating the structured query language statement of the embodiment of the present disclosure, and will not be repeated here.

[0178] In an exemplary embodiment, a computer-readable storage medium including instructions is also provided, such as a memory including instructions, and the instructions can be executed by a processor of an electronic device to complete the method for generating a structured query language statement proposed in any of the above embodiments. Optionally, the computer-readable storage medium can be a ROM, a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, an optical data storage device, etc.

[0179] In an exemplary embodiment, a computer program product is also provided, including a computer program / instruction, characterized in that when the computer program / instruction is executed by a processor, the method for generating a structured query language statement proposed in any of the above embodiments is implemented.

[0180] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. The present disclosure is intended to cover any variations, uses or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The description and examples are to be considered exemplary only, and the true scope and spirit of the present disclosure are indicated by the following claims.

[0181] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.

Claims

1. A method for generating a structured query language statement, characterized in that: include: Acquire a code logic description text, and parse the code logic description text to obtain requirement description information of a target structured query language statement to be generated; wherein the requirement description information is used to indicate code logic information of the target structured query language statement; According to the requirement description information, reference metadata of a reference data table in a reference database is retrieved to obtain target metadata information matching the requirement description information; generating a plurality of natural language questions and a plurality of reference structured query language statements associated with the reference data table according to the reference meta-information; The target structured query language statement is generated according to the requirement description information, the target metadata information, the multiple natural language questions and the multiple reference structured query language statements.

2. The method according to claim 1, characterized in that The step of generating the target structured query language statement according to the requirement description information, the target metadata information, the multiple natural language questions and the multiple reference structured query language statements includes: Obtaining similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements that are similar to the requirement description information; The target structured query language statement is generated based on the target metadata information, the similar structured query language statement processing script, the similar structured query language statement requirement document, the similar natural language question and the similar structured query language statement.

3. The method according to claim 2, characterized in that The step of obtaining similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions and similar structured query language statements that are similar to the requirement description information includes: Vectorizing the requirement description information to obtain a first vectorized representation; According to the first vectorized representation, query a vector database to obtain a query result; Determining, based on the query results, similar structured query language statement processing scripts, similar structured query language statement requirement documents, similar natural language questions, and similar structured query language statements that are similar to the requirement description information; The vector database includes a plurality of historical structured query language statement processing scripts, a plurality of historical structured query language statement requirement documents, the plurality of natural language questions and vectorized representations of the plurality of reference SQL statements.

4. The method according to claim 2, characterized in that: The generating the target structured query language statement based on the target metadata information, the similar structured query language statement processing script, the similar structured query language statement requirement document, the similar natural language question and the similar structured query language statement comprises: Generate context information associated with the target structured query language based on the target metadata information, the similar structured query language statement processing script, the similar structured query language statement requirement document, the similar natural language question and the similar structured query language statement; The target structured query language statement is generated according to the context information.

5. The method according to claim 4, characterized in that The generating of context information associated with the target structured query language based on the target metadata information, the similar structured query language statement processing script, the similar historical structured query language statement requirement document, the similar natural language question and the similar structured query language statement includes: Using a specified syntax to format and mark the target metadata information, the similar structured query language statement processing script, the similar structured query language statement requirement document, the similar natural language question, and the similar structured query language statement; The context information is generated according to the target metadata information after formatting and marking, the similar structured query language statement processing script after formatting and marking, the similar structured query language statement requirement document after formatting and marking, the similar natural language question after formatting and marking, and the similar structured query language statement after formatting and marking.

6. The method according to claim 4, characterized in that The target structured query language statement includes target structured query language statements of multiple statement types. Generating the target structured query language statement according to the context information includes: Generate a target structured query language statement of a first statement type according to the context information; wherein the target structured query language statement of the first statement type is used to create a data table; Executing a target structured query language statement of the first statement type, and in response to the target structured query language statement of the first statement type being successfully executed, generating a target structured query language statement of a second statement type according to the context information; wherein the target structured query language statement of the second statement type is used to process the data table; Execute a target structured query language statement of the second statement type, and in response to successful execution of the target structured query language statement of the second statement type, generate a target structured query language statement of a third statement type according to the context information; wherein the target structured query language statement of the third statement type is used to test the data of the data table.

7. The method according to claim 6, characterized in that After generating a target structured query language statement of the first statement type according to the context information, the method further includes: Performing syntax checking on a target structured query language statement of the first statement type; In response to a target structured query language statement of the first statement type passing syntax verification, executing the target structured query language statement of the first statement type; In response to the target structured query language statement of the first statement type failing syntax verification, generating an exception prompt message; The abnormal prompt information is used to indicate at least one of the following: The target structured query language statement of the first statement type has a syntax error; The context information is regenerated, and based on the regenerated context information, a target structured query language statement of the first statement type is regenerated.

8. The method according to claim 1, characterized in that The step of generating a plurality of natural language questions and a plurality of reference structured query language statements associated with the reference data table according to the reference meta-information comprises: Obtaining a prompt template, wherein the prompt template is used to indicate task information to be performed by the large model; Filling the prompt template with the reference meta information to obtain model prompt information; The model prompt information is input into the large model for processing to obtain the multiple natural language questions and the multiple reference structured query language statements.

9. The method according to claim 1, characterized in that: The target metadata information includes target data table, target data field information and target annotation information. According to the requirement description information, reference metadata of a reference data table in a reference database is retrieved to obtain target metadata information matching the requirement description information, including: According to the requirement description information, keyword search is performed on the reference meta information to obtain the target data table and target data field information matching the requirement description information; According to the requirement description information, the reference meta-information is fuzzily retrieved to obtain target annotation information matching the requirement description information.

10. A device for generating structured query language statements, characterized in that: include: An acquisition module, used for acquiring a code logic description text, and parsing the code logic description text to obtain requirement description information of a target structured query language statement to be generated, wherein the requirement description information is used for indicating code logic information of the target structured query language statement; A retrieval module, configured to retrieve reference metadata of a reference data table in a reference database according to the requirement description information, so as to obtain target metadata information matching the requirement description information; A first generating module, configured to generate a plurality of natural language questions and a plurality of reference structured query language statements associated with the reference data table according to the reference meta-information; The second generating module is used to generate the target structured query language statement according to the requirement description information, the target metadata information, the multiple natural language questions and the multiple reference structured query language statements.

Citation Information

Cited By

  • Query statement generation method and device, electronic equipment and medium

    CN121412257A

  • A query statement generation method and device, an electronic device, and a medium

    CN121412257B

  • Information processing device and information processing method

    WO2026042207A1