SQL statement generation method, device, electronic device and storage medium

By combining lightweight and heavyweight language models, the generation method is automatically selected according to the complexity of the SQL statement, solving the problem that users find it difficult to judge the complexity of SQL statements, improving the accuracy and efficiency of generation, and facilitating data management.

CN120470021BActive Publication Date: 2025-09-12CSC FINANCIAL CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510946970.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-07-10
Publication Date
2025-09-12
Estimated Expiration
2045-07-10

AI Technical Summary

Technical Problem

In the prior art, it is difficult for users to accurately judge the complexity of SQL statements, resulting in low accuracy or low efficiency in generating SQL statements. Especially in the case of high complexity, the prior methods cannot efficiently generate accurate SQL statements.

Method used

By obtaining the descriptive text entered by the user, a lightweight large language model is used to generate preliminary SQL statements, and the complexity of the statements is calculated. Based on the complexity, an appropriate method is selected to generate SQL statements. The lightweight model is used to quickly recognize text, and the heavyweight model is used to accurately generate highly complex text.

Benefits of technology

It realizes automatic selection of generation method according to the complexity of SQL statements, avoids errors caused by manual judgment by users, improves the accuracy and efficiency of SQL statement generation, and facilitates user data management.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120470021B_ABST
    Figure CN120470021B_ABST
Patent Text Reader

Abstract

The present invention provides an SQL statement generation method, device, electronic device, and storage medium, relating to the field of data management technology. In the method, a current description text input by a user describing the SQL statement to be generated is obtained; a first prompt word containing the current description text is input into a first large language model to obtain a first SQL statement; the complexity of the first SQL statement is calculated using specified parameters; the complexity is positively correlated with each of the specified parameters; if the complexity is greater than a preset threshold, an SQL statement is generated using a second prompt word containing the current description text and a second large language model; the number of model parameters of the second large language model is greater than that of the first large language model; if the complexity is not greater than the preset threshold, a target statement template matching the first SQL statement is obtained; the table name and field name of the data table indicated by the first SQL statement are filled into the target statement template to generate the SQL statement. The present invention can facilitate data management for users.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of data management, and in particular to a method, device, electronic device and storage medium for generating an SQL statement. Background Art

[0002] Users can write Structured Query Language (SQL) statements and manage data in the database through the written SQL statements.

[0003] In the prior art, database management systems can provide users with the following two automated SQL statement generation methods. In the first method, when an SQL statement needs to be generated, the electronic device fills the metadata such as the data table name and field name entered by the user into a pre-defined SQL statement template, and can quickly generate an SQL statement. However, this method simply fills the data into the part of the SQL statement template that corresponds to the type of the data. Accordingly, if there are multiple data of the same type in the data that needs to be filled in, it may cause errors in the filling positions of the multiple data. In other words, this method can only be applied to generating SQL statements with lower complexity. In the second method, the text representing the user's needs is input into a large language model so that the large language model outputs an SQL statement that can accurately realize the user's needs. With the help of the capabilities of the large language model, it can also be applied to generating SQL statements with higher complexity. However, the large language model requires a long time for reasoning, resulting in low efficiency in generating SQL statements.

[0004] Therefore, users need to manually determine the complexity of the SQL statements to be generated and select the appropriate technology to generate them. However, users generally lack professional SQL statement writing skills and are unable to accurately determine the complexity of the SQL statements to be generated. The selected method for generating SQL statements may result in less accurate or inefficient SQL statements, making data management inconvenient for users. Summary of the Invention

[0005] The purpose of the embodiments of the present invention is to provide a method, device, electronic device, and storage medium for generating SQL statements to facilitate data management for users. The specific technical solution is as follows:

[0006] In a first aspect, an embodiment of the present invention provides a method for generating an SQL statement, the method comprising:

[0007] Get the description text entered by the user, which is used to describe the SQL statement to be generated, as the current description text;

[0008] Inputting a first prompt word containing the current description text into a first large language model to obtain a first SQL statement; wherein the first prompt word is used to instruct the first large language model to generate an SQL statement that conforms to the input text;

[0009] Calculating the complexity of the first SQL statement using specified parameters of the first SQL statement; wherein the specified parameters include at least one of the following: the number of nesting levels of the first SQL statement, the number of associated data tables, the number of included window functions, and the number of included sensitive fields; and the complexity is positively correlated with each of the specified parameters;

[0010] If the complexity is greater than a preset threshold, generating an SQL statement that conforms to the current description text using a second prompt word and a second language model containing the current description text; wherein the second prompt word is used to instruct the second language model to generate an SQL statement that conforms to the input text, and the number of model parameters of the second language model is greater than the number of model parameters of the first language model;

[0011] If the complexity is not greater than the preset threshold, a target statement template that matches the first SQL statement is obtained from the pre-stored statement templates; the table name of the data table indicated by the first SQL statement and the field name in the data table are filled into the target statement template to generate an SQL statement that conforms to the current description text.

[0012] Optionally, calculating the complexity of the first SQL statement by using specified parameters of the first SQL statement includes:

[0013] A weighted sum of the parameters included in the specified parameters is calculated to obtain the complexity of the first SQL statement.

[0014] Optionally, the number of nesting levels, the number of associated data tables, the number of included window functions, and the number of included sensitive fields of the first SQL statement are determined by:

[0015] Parsing the syntax units in the first SQL statement to obtain a first syntax tree corresponding to the first SQL statement; wherein the nodes in the first syntax tree correspond one-to-one to the syntax units in the first SQL statement;

[0016] Subtract one from the maximum path depth from the root node to the leaf node in the first syntax tree to obtain the number of nesting levels of the first SQL statement;

[0017] Add one to the number of nodes of the connection type in the first syntax tree to obtain the number of data tables associated with the first SQL statement;

[0018] Counting the number of nodes whose node type is a window function type in the first syntax tree to obtain the number of window functions included in the first SQL statement;

[0019] Count the number of sensitive fields in the sentence content of the nodes whose node type is the field type in the first syntax tree.

[0020] Optionally, the method further includes:

[0021] Identify whether there are syntax errors in the latest generated SQL statement that conforms to the current description text;

[0022] If there is a syntax error, the newly generated SQL statement that conforms to the current description text is modified to obtain a new SQL statement that conforms to the current description text; and the step of identifying whether there is a syntax error in the newly generated SQL statement that conforms to the current description text is returned to execute until there is no syntax error in the newly generated SQL statement that conforms to the current description text.

[0023] Optionally, the method further includes:

[0024] If there are no syntax errors, determine whether the newly generated SQL statement that conforms to the current description text meets the preset statement rules; wherein the preset statement rules include desensitization sub-rules and efficiency improvement sub-rules; the efficiency improvement sub-rules are used to improve the execution efficiency of the SQL statement;

[0025] If the preset statement rules are not met, the newly generated SQL statement that conforms to the current description text is corrected according to the preset statement rules to obtain a new SQL statement that conforms to the current description text; and the process returns to the step of identifying whether there are syntax errors in the newly generated SQL statement that conforms to the current description text until the preset statement rules are met and the target SQL statement is obtained.

[0026] Optionally, a preset sentence template is generated based on the historical description text input by the user;

[0027] The acquiring a target statement template matching the first SQL statement from each pre-stored statement template includes:

[0028] Generate a first semantic vector representing the first SQL statement and the current description text;

[0029] Determine, from the pre-stored second semantic vectors, a semantic vector having the highest similarity to the first semantic vector; wherein one second semantic vector corresponds to one sentence template, and one second semantic vector represents the corresponding sentence template and the historical description text corresponding to the sentence template;

[0030] The sentence template corresponding to the determined semantic vector is used as the target sentence template.

[0031] Optionally, the method further includes:

[0032] If an adoption instruction for the newly generated SQL statement that conforms to the current description text is received from the user, the SQL statement that conforms to the current description text is stored as a new statement template and the current description text is stored accordingly;

[0033] The second largest language model is obtained by training using each sentence template and the historical description text corresponding to the sentence template.

[0034] In a second aspect, an embodiment of the present invention provides a device for generating an SQL statement, the device comprising:

[0035] The acquisition module is used to acquire the description text input by the user and used to describe the SQL statement to be generated as the current description text;

[0036] An input module, configured to input a first prompt word containing the current description text into a first large language model to obtain a first SQL statement; wherein the first prompt word is used to instruct the first large language model to generate an SQL statement that conforms to the input text;

[0037] a first utilizing module, configured to calculate the complexity of the first SQL statement using specified parameters of the first SQL statement; wherein the specified parameters include at least one of the following: the number of nesting levels of the first SQL statement, the number of associated data tables, the number of included window functions, and the number of included sensitive fields; and the complexity is positively correlated with each of the specified parameters;

[0038] a first generation module configured to generate an SQL statement that conforms to the current description text using a second prompt word and a second language model containing the current description text if the complexity is greater than a preset threshold; wherein the second prompt word is used to instruct the second language model to generate an SQL statement that conforms to the input text, and the number of model parameters of the second language model is greater than the number of model parameters of the first language model;

[0039] The second generation module is used to obtain a target statement template that matches the first SQL statement from the pre-stored statement templates if the complexity is not greater than the preset threshold; fill the table name of the data table indicated by the first SQL statement and the field name in the data table into the target statement template to generate an SQL statement that conforms to the current description text.

[0040] An embodiment of the present invention further provides an electronic device, comprising a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other via the communication bus;

[0041] Memory for storing computer programs;

[0042] The processor is configured to implement the SQL statement generation method provided in the first aspect when executing the program stored in the memory.

[0043] An embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the SQL statement generation method provided in the first aspect is implemented.

[0044] Beneficial effects of the embodiments of the present invention:

[0045] In an embodiment of the present invention, a description text input by a user for describing the SQL statement to be generated is obtained as the current description text; a first prompt word containing the current description text is input into a first large language model to obtain a first SQL statement. Since the designated parameters include the number of nesting levels of the first SQL statement, the number of associated data tables, the number of window functions included, and the number of sensitive fields included, each parameter in the designated parameters is positively correlated with the complexity of the SQL statement. In other words, the larger the number of nesting levels, the number of associated data tables, the number of window functions included, and the number of sensitive fields included, the more complex the SQL statement is. Thus, the complexity of the first SQL statement can be calculated using the designated parameters of the first SQL statement. When the complexity is not greater than a preset threshold, a target statement template matching the first SQL statement is obtained from each pre-stored statement template; the table name of the data table indicated by the first SQL statement and the field name in the data table are filled into the target statement template to generate an SQL statement; when the complexity is greater than the preset threshold, an SQL statement is generated using a second prompt word containing the current description text and the second large language model.

[0046] In the embodiment of the present invention, the complexity of the first SQL statement output by the first large language model is calculated. Since the number of model parameters of the second large language model is greater than the number of model parameters of the first large language model, the first large language model is a lighter-weight large language model. Therefore, during the inference process of the first large language model, the current description text can be identified in less time to obtain the first SQL statement. The complexity of the first SQL statement is calculated to determine the complexity of the SQL statement to be generated; and different methods are used to generate the SQL statement to be generated according to different complexities. In other words, in the embodiment of the present invention, the machine can use the lightweight large language model to identify the current description text and obtain a preliminary SQL statement. According to the calculated complexity of the preliminary SQL statement, different methods are used to generate the SQL statement. This can avoid the user manually judging the complexity of the SQL statement to be generated and selecting the method for generating the SQL statement. This can also avoid the problem of generating SQL statements with low accuracy or the problem of low efficiency in generating SQL statements, making it easier for users to manage data.

[0047] Of course, it is not necessary to achieve all of the advantages described above simultaneously in order to implement any product or method of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other embodiments can also be obtained based on these drawings.

[0049] Figure 1 A schematic diagram of a flow chart of a first SQL statement generation method provided in an embodiment of the present invention;

[0050] Figure 2 A schematic diagram of a syntax tree provided by an embodiment of the present invention;

[0051] Figure 3 A schematic diagram of a flow chart of a second SQL statement generation method provided in an embodiment of the present invention;

[0052] Figure 4 A schematic diagram of a flow chart of a third SQL statement generation method provided in an embodiment of the present invention;

[0053] Figure 5 A schematic diagram illustrating the principles of a method for generating SQL statements provided by an embodiment of the present invention;

[0054] Figure 6 A schematic diagram illustrating the principle of computing complexity in the SQL statement generation method provided in an embodiment of the present invention;

[0055] Figure 7 A schematic diagram illustrating the principles of verification and correction in the SQL statement generation method provided by an embodiment of the present invention;

[0056] Figure 8 A schematic diagram of the structure of an SQL statement generating device provided by an embodiment of the present invention;

[0057] Figure 9 A schematic structural diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0058] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field based on the present invention are within the scope of protection of the present invention.

[0059] To facilitate data management for users, embodiments of the present invention provide a method, device, electronic device, and storage medium for generating SQL statements.

[0060] The following first introduces a method for generating SQL statements provided by an embodiment of the present invention.

[0061] The SQL statement generation method provided in an embodiment of the present invention can be applied to an electronic device. For example, the electronic device can be a tablet computer or a desktop computer. A user can input a description text describing the SQL statement to be generated into the electronic device. The electronic device can then execute the SQL statement generation method provided in an embodiment of the present invention to generate an SQL statement that conforms to the description text.

[0062] An embodiment of the present invention provides a method for generating an SQL statement, which may include the following steps:

[0063] Get the description text entered by the user, which is used to describe the SQL statement to be generated, as the current description text;

[0064] Inputting a first prompt word containing the current description text into the first large language model to obtain a first SQL statement; wherein the first prompt word is used to instruct the first large language model to generate an SQL statement that conforms to the input text;

[0065] Calculating the complexity of the first SQL statement using specified parameters of the first SQL statement; wherein the specified parameters include at least one of the following: the number of nesting levels of the first SQL statement, the number of associated data tables, the number of included window functions, and the number of included sensitive fields; and the complexity is positively correlated with each of the specified parameters;

[0066] If the complexity is greater than a preset threshold, generating an SQL statement that conforms to the current description text using a second prompt word and a second largest language model containing the current description text; wherein the second prompt word is used to instruct the second largest language model to generate an SQL statement that conforms to the input text, and the number of model parameters of the second largest language model is greater than the number of model parameters of the first largest language model;

[0067] If the complexity is not greater than the preset threshold, a target statement template that matches the first SQL statement is obtained from the pre-stored statement templates; the table name of the data table indicated by the first SQL statement and the field name in the data table are filled into the target statement template to generate an SQL statement that conforms to the current description text.

[0068] In the embodiment of the present invention, the complexity of the first SQL statement output by the first large language model is calculated. Since the number of model parameters of the second large language model is greater than the number of model parameters of the first large language model, the first large language model is a lighter-weight large language model. Therefore, during the inference process of the first large language model, the current description text can be identified in less time to obtain the first SQL statement. The complexity of the first SQL statement is calculated to determine the complexity of the SQL statement to be generated; and different methods are used to generate the SQL statement to be generated according to different complexities. In other words, in the embodiment of the present invention, the machine can use the lightweight large language model to identify the current description text and obtain a preliminary SQL statement. According to the calculated complexity of the preliminary SQL statement, different methods are used to generate the SQL statement. This can avoid the user manually judging the complexity of the SQL statement to be generated and selecting the method for generating the SQL statement. This can also avoid the problem of generating SQL statements with low accuracy or the problem of low efficiency in generating SQL statements, making it easier for users to manage data.

[0069] The following combination Figure 1 Introducing a method for generating SQL statements provided by an embodiment of the present invention, such as Figure 1 As shown, the method may include steps S101-S105.

[0070] S101 , obtaining a description text input by a user for describing an SQL statement to be generated as a current description text.

[0071] It is understood that when a user needs to generate an SQL statement, the user can enter a description text into the electronic device, and the electronic device can obtain the description text entered by the user and use the obtained description text as the current description text. For example, the electronic device can display a text input page, which includes an input box, and the user can enter the description text in the input box on the text input page.

[0072] Among them, the description text is used to describe the SQL statement to be generated. The description text can be a natural language text. Specifically, the description text can contain specific operations and operation objects. For example, the description text can be "Count the order volume of users in Beijing", "Count" is the specific operation, and "the order volume of users in Beijing" is the operation object. The description text can also be a semi-structured text. Specifically, the description text can contain semi-structured key-value pairs. For example, the description text can be a table name key-value pair, a condition key-value pair, and a field key-value pair. Among them, in the table name key-value pair, the key is the "table name" text, and the value is the table name of the data table where the data to be operated is located; in the condition key-value pair, the key is the "condition" text, and the value is the filtering condition of the data to be operated; in the field key-value pair, the key is the "field" text, and the value is the field name of the data to be operated; for example, the description text can be "Table name: Sales table, Condition: Time = 2025-06, Field: Product name, Sales", and the SQL statement that meets the description text is used to query the sales of each product in the sales table in June 2025.

[0073] S102: Input a first prompt word containing the current description text into a first language model to obtain a first SQL statement.

[0074] The first prompt word is used to instruct the first language model to generate an SQL statement that conforms to the input text.

[0075] It is understood that the first prompt word can instruct the first language model to generate an SQL statement that matches the input text. For example, the first prompt word can include the current description text and instructive text, where the instructive text can indicate the specific task that the first language model needs to perform. For example, the instructive text can be "Please generate an SQL statement to implement the current description text."

[0076] Among them, the large language model can be a language model trained using large-scale text data. The large language model is preset to have rich language knowledge, comprehension ability and generation ability, and can recognize the semantics of the input content and generate text to respond to the input content.

[0077] The first largest language model has fewer model parameters than the second largest language model used subsequently, that is, the first largest language model is a lighter-weight large language model. The inference time of a large language model is positively correlated with the model parameters. Since the first largest language model has fewer model parameters, the amount of computation required for inference using the model parameters it has is less, so the inference time of the first largest language model is less than that of the second largest language model. The electronic device inputs the first prompt word into the first largest language model and can use fewer model parameters for inference to generate the first SQL statement described by the current description text. The lightweight large language model can quickly and accurately identify the current description text and generate the first SQL statement, so as to subsequently determine the complexity of the SQL statement to be generated.

[0078] Exemplarily, the number of model parameters of the first largest language model is less than a specified number, which may be 1 billion, 2 billion, or 7 billion; the second largest language model may have model parameters ranging from tens of billions to trillions.

[0079] Among them, since the first large language model is a lightweight large language model, the first large language model has less computational complexity when using its model parameters for inference, and the inference accuracy is low. The accuracy of the results generated by the first large language model is not high. The first SQL statement output by the first large language model can be used as a preliminary SQL statement that conforms to the current description text. The first SQL statement can be used to calculate the complexity in subsequent steps, but it is necessary to generate SQL statements in a more accurate way according to the complexity in the future.

[0080] In one implementation, if the first SQL statement can pass the verification of whether there are any grammatical problems and whether it complies with preset statement rules mentioned in subsequent embodiments, the electronic device can use the first SQL statement as a target SQL statement that complies with the current description text and provide the target SQL statement to the user.

[0081] S103: Calculate the complexity of the first SQL statement using the specified parameters of the first SQL statement.

[0082] The specified parameters include at least one of the following: the number of nesting levels of the first SQL statement, the number of associated data tables, the number of included window functions, and the number of included sensitive fields; and the complexity is positively correlated with each of the specified parameters.

[0083] It is understood that the electronic device can use the specified parameters of the first SQL statement to calculate the complexity of the first SQL statement. Since the complexity is positively correlated with each parameter in the specified parameters, the electronic device can count each parameter included in the specified parameters in the first SQL statement and calculate the complexity based on each parameter.

[0084] The number of nesting levels of an SQL statement is the nesting depth of subqueries within the SQL statement, that is, the number of levels of subqueries within subqueries; a subquery is a query statement nested within another query statement. Because more nesting levels involve more operations in the SQL statement and more complex combinations of data tables involved in each operation in the SQL statement, the SQL statement's complexity increases. Therefore, electronic devices can use the number of nesting levels of a first SQL statement to calculate the complexity of the first SQL statement.

[0085] The data tables associated with an SQL statement are the data tables linked by the join operator. Since the greater the number of associated data tables, the more complex the combinations between the data tables, and thus the higher the complexity of the SQL statement, the electronic device can calculate the complexity of the first SQL statement by using the number of data tables associated with the first SQL statement.

[0086] Window functions in an SQL statement are used to perform calculations on the data in the query results. Because window functions are used to perform further calculations on the queried data, the more window functions there are, the more complex the calculations are, and the more complex the SQL statement is. Therefore, the electronic device can calculate the complexity of the first SQL statement using the number of window functions included in the first SQL statement.

[0087] Sensitive fields are designated fields related to data security. For example, sensitive fields may include at least one of the following: mobile phone number, ID number, email address, and address. If data desensitization is required for sensitive fields, a desensitization function may be included in the SQL statement to desensitize each sensitive field. The more sensitive fields there are, the more desensitization functions there are in the SQL statement, and the more complex the SQL statement is. Therefore, the electronic device can use the number of sensitive fields included in the first SQL statement to calculate the complexity of the first SQL statement.

[0088] In one implementation, the electronic device may use the number of sensitive fields in an SQL statement to calculate the sensitive field density of the SQL statement, and then use the sensitive field density to calculate the complexity of the SQL statement.

[0089] It is understood that the number of sensitive fields is positively correlated with the number of desensitizing functions in an SQL statement. If the proportion of sensitive fields in an SQL statement is high, the proportion of desensitizing functions in the SQL statement is high, and the complexity of the SQL statement is high, then the sensitive field density is positively correlated with the complexity. For example, when calculating the sensitive field density of an SQL statement, the electronic device can calculate the ratio of the number of sensitive fields to the total number of fields in the SQL statement to obtain the sensitive field density.

[0090] In another implementation, to simplify calculations and save computing resources, the electronic device may multiply the number of sensitive fields included in the first SQL statement by a specified coefficient to obtain a parameter representing the number of sensitive fields, and then use the parameter to calculate the complexity of the first SQL statement. The specified coefficient is a preset value between 0 and 1, for example, the specified coefficient may be 0.3.

[0091] The electronic device can obtain the specified parameters in a variety of ways. In one implementation, the electronic device can use a regular expression to match the specified characters, retrieve the specified characters in the first SQL statement, and then determine the specified parameters in the first SQL statement based on the number of specified characters retrieved. Specifically, the electronic device can use a regular expression to retrieve the keyword query (SELECT) in the first SQL statement, identify functions that start with the keyword SELECT, and use the number of identified functions as the number of nested layers; the electronic device can use a regular expression to retrieve the join (JOIN) operator in the first SQL statement. JOIN can connect the table names of two data tables to indicate that the two data tables are associated data tables. That is, adding one to the number of JOINs can get the number of associated data tables. It can be understood that the electronic device can deduplicate the associated data tables to avoid counting duplicate data tables. The electronic device can use a regular expression to retrieve the preset function name of the window function in the first SQL statement (wherein the preset function name of the window function can be RANK, and the number of the retrieved function names of the window function is used as the number of window functions included in the first SQL statement; the electronic device can use a regular expression to retrieve the preset field name in the first SQL statement (wherein the preset field name can include at least one of the following: mobile phone number, ID number, email address and address), and the number of the retrieved preset field names is used as the number of sensitive fields included in the first SQL statement.

[0092] In another implementation, the electronic device can obtain each parameter in the specified parameter through the syntax tree. Specifically, the number of nesting levels, the number of associated data tables, the number of window functions included, and the number of sensitive fields included in the first SQL statement can be determined by the following steps:

[0093] A1. Parse the syntax units in the first SQL statement to obtain a first syntax tree corresponding to the first SQL statement; wherein the nodes in the first syntax tree correspond one-to-one to the syntax units in the first SQL statement.

[0094] A2: Subtract one from the maximum path depth from the root node to the leaf node in the first syntax tree to obtain the number of nesting levels of the first SQL statement; add one to the number of nodes with a connection type in the first syntax tree to obtain the number of data tables associated with the first SQL statement; count the number of nodes with a window function type in the first syntax tree to obtain the number of window functions included in the first SQL statement; and count the number of sensitive fields in the statement content of nodes with a field type in the first syntax tree.

[0095] It is understandable that the electronic device can use an SQL syntax parser to parse the first SQL statement into an Abstract Syntax Tree (AST) to obtain a first syntax tree. Exemplarily, the SQL syntax parser can be another language recognition tool (ANTLR).

[0096] The nodes in the first syntax tree correspond one-to-one to syntax units in the first SQL statement. A syntax unit in a SQL statement can be a keyword or expression in the SQL statement. For example, a syntax unit in a SQL statement can be a keyword, operator, field name, table name, or the name of a called function. The root node is the first keyword in the first SQL statement (for example, SELECT, INSERT, or UPDATE), which indicates the type of the first SQL statement. The intermediate nodes are other keywords in the first SQL statement (for example, other keywords can include the subquery keywords SELECT, FROM, and WHERE), operators (for example, operators can include JOIN), and the names of called functions (for example, the window function name RANK). The leaf nodes are specific element values ​​in the first SQL statement, for example, field names or table names.

[0097] The electronic device can traverse all nodes from the root node of the first syntax tree to the leaf node. The total number of keywords representing the operations involved in the path from the root node to the leaf node (for example, query keywords, insert keywords and update keywords) is the maximum path depth. Since the number of nested levels is the number of levels of sub-queries within the sub-queries, the root node does not belong to the sub-query. When counting the number of nested levels, the keywords of the root node are not considered. Therefore, the maximum path depth can be reduced by one to obtain the number of nested levels of the first SQL statement.

[0098] The electronic device can count the number of connection type nodes (i.e., nodes whose corresponding syntax content is the operator JOIN) in the first syntax tree. The connection type nodes indicate that two data tables are associated. Therefore, the number of connection type nodes in the first syntax tree is added by one, and the number of associated data tables in the first SQL statement can be obtained.

[0099] The electronic device may count the number of window function nodes (i.e., nodes whose grammatical content is the function name of a window function) in the first syntax tree to obtain the number of window functions included in the first SQL statement. The electronic device may also count the number of sensitive fields in the statement content of field nodes (i.e., nodes whose grammatical content is the field name) in the first syntax tree.

[0100] For example, Figure 2 A syntax tree diagram provided by an embodiment of the present invention. Figure 2 As shown in the example, the root node is the keyword query SELECT. The leaf nodes include the first field "e.emp_name" representing the employee name, the second field "d.dept_name" representing the department name, the third field "employees e" representing the employee table name, the fourth field "departments d" representing the department table name, the filter condition "e.salary>50000" to query only employees with a salary greater than 50,000, and the join condition "e.depar_id=d.dept_id" to match the employee department with the department table. The intermediate nodes include the field list, the called window function RANK, the keyword FROM, the keyword WHERE, the operator JOIN, and the operator ON. The keyword query SELECT indicates that a query action needs to be executed, the window function RANK indicates that the window function RANK is used, the keyword FROM indicates the data table to be queried, the keyword WHERE indicates the filter condition to be met during the query, the operator JOIN indicates the related data tables during the query, and the operator ON indicates that the related data in the related data tables meet the join condition. The SQL statement represented by this syntax tree can be used to query the employee table and department table for employees whose salary is greater than 50,000, and to perform statistics on the queried employees according to the method defined in the window function. For example, the queried employees are grouped by department, and the employees in each group are sorted in descending order by salary.

[0101] In determining the specified parameters of the SQL statement represented by this syntax tree, the root node is the keyword query node. There is only one root node representing the keyword operation involved from the root node to the leaf nodes, so the number of nesting levels of this SQL statement is 0. Since there is only one "JOIN" node of the connection type, the number of data tables associated with this SQL statement is 2. Since there is only one RANK node of the window function type, the number of window functions contained in this SQL statement is 1. The statement content in the field type node in this syntax tree does not contain sensitive fields, so the number of sensitive fields in this SQL statement is 0.

[0102] It is understandable that in the previous implementation, the electronic device needs to use a pre-written regular expression to match the specified characters. Since regular expressions usually need to be manually written by users with SQL statement writing skills, the threshold for using regular expression matching to determine each parameter is high. If the user's ability to write SQL statements is weak, regular expression retrieval cannot be used. In this implementation, an SQL syntax parser is used to automatically parse the first SQL statement to obtain a syntax tree. By querying the nodes in the syntax tree, the electronic device can accurately and conveniently determine each parameter.

[0103] In one implementation, the electronic device may use the sum of the parameters in the specified parameters as the complexity. For example, if the specified parameters include the number of nesting levels of a first SQL statement and the number of data tables associated with the first SQL statement, the electronic device may calculate the sum of the number of nesting levels of the first SQL statement and the number of data tables associated with the first SQL statement to obtain the complexity of the first SQL statement.

[0104] In another implementation, the electronic device may calculate a weighted sum of parameters included in the specified parameters to obtain the complexity of the first SQL statement.

[0105] It is understandable that the degree of influence of each parameter in the specified parameters on complexity varies. For example, each nesting layer in the first SQL statement may introduce new associated data tables, window functions, and sensitive fields. Therefore, the number of nesting layers has the greatest impact on complexity, and the number of nesting layers is the first level of importance. The larger the number of associated data tables, the more complex the query logic for the data in the data tables. The impact of the number of associated data tables on complexity is less than that of the number of nesting layers, and the number of associated data tables is the second level of importance. Window functions are used to perform complex calculations on the queried data, which does not affect the query logic but only increases the amount of calculation. Therefore, the impact of the number of window functions on complexity is less than that of the number of associated data tables, and the number of window functions is the third level of importance. Sensitive fields require desensitization processing, which only desensitizes sensitive fields and does not involve complex calculations on multiple queried data. Therefore, the impact of the number of included sensitive fields on complexity is less than the number of included sensitive fields, and the number of included sensitive fields is the fourth level of importance. Among them, the first level of importance, the second level of importance, the third level of importance, and the fourth level of importance represent the degree of influence on complexity in descending order. The electronic device may set a weight according to the degree of influence of each parameter on the complexity. Specifically, the weight corresponding to a parameter is positively correlated with the degree of influence of the parameter on the complexity.

[0106] Exemplarily, the electronic device may calculate the complexity of the first SQL statement according to a complexity formula. The complexity formula is: , Indicates the complexity of the first SQL statement, Indicates the number of nesting levels of the first SQL statement, Indicates the number of data tables associated with the first SQL statement. Indicates whether the first SQL statement contains a window function. Indicates the number of sensitive fields included in the first SQL statement. If the first SQL statement contains a window function, W is recorded as 1; if the first SQL statement does not contain a window function, W is recorded as 0.

[0107] In an embodiment of the present invention, each parameter can be used as part of the complexity calculation, and the impact of multiple parameters on complexity is comprehensively considered to obtain the accurate complexity of the first SQL statement, providing a basis for subsequent processing based on the complexity.

[0108] In one implementation, if an error occurs in the process of calculating the complexity of the first SQL statement using the specified parameters in the first SQL statement (for example, the specified parameters in the first SQL statement cannot be determined), the electronic device can display a prompt message to the user indicating that the SQL statement is wrong, so as to prompt the user to adjust the description text entered by the user.

[0109] S104: If the complexity is greater than a preset threshold, a SQL statement that conforms to the current description text is generated using a second prompt word and a second language model containing the current description text.

[0110] The second prompt word is used to instruct the second largest language model to generate an SQL statement that conforms to the input text, and the number of model parameters of the second largest language model is greater than the number of model parameters of the first largest language model.

[0111] It is understandable that if the complexity is greater than a preset threshold, the electronic device can determine that the complexity of the SQL statement to be generated is relatively high, and the electronic device can adopt a method for generating SQL statements with high complexity, that is, using the second largest language model to generate SQL statements. Since the number of model parameters of the second largest language model is greater than the number of model parameters of the first largest language model, the second largest language model can use more model parameters during the reasoning process to perform more accurate reasoning and generate more accurate SQL statements, avoiding the use of template filling to generate SQL statements with lower accuracy, and ensuring the generation of accurate SQL statements that conform to the current description text. Exemplarily, the complexity threshold can be a preset value, such as 0.7.

[0112] S105, if the complexity is not greater than the preset threshold, obtain a target statement template that matches the first SQL statement from the pre-stored statement templates; fill the table name of the data table indicated by the first SQL statement and the field name in the data table into the target statement template to generate an SQL statement that conforms to the current description text.

[0113] It is understandable that if the complexity is less than the preset threshold, the electronic device can determine that the complexity of the SQL statement to be generated is low, and the electronic device can adopt a method for generating SQL statements with low complexity, that is, using a template filling method to quickly and accurately generate SQL statements, avoiding the use of a large language model with a large number of model parameters to generate SQL statements with lower efficiency.

[0114] The electronic device can obtain a target statement template that matches the first SQL statement from pre-stored statement templates, then obtain the table name of the data table indicated by the first SQL statement and the field names in the data table, and finally populate the table name of the data table indicated by the first SQL statement and the field names in the data table into the target statement template to generate an SQL statement that conforms to the current description text. The statement templates can be stored in a first type of database, which can be called a case database. The field names of the data tables can be stored in a second type of database, which can be called a metadata database. The data in the metadata database can be updated based on updates to the data tables in the database.

[0115] In order to obtain a target statement template that matches the first SQL statement, the electronic device can adopt multiple methods. In one implementation, the electronic device can compare the similarity between the first SQL statement and each statement template, and obtain the statement template with the highest similarity to the first SQL statement as the target statement template. Exemplarily, the electronic device stores a semantic vector corresponding to each statement template, and the electronic device can convert the first SQL statement into a semantic vector through a text model, and then calculate the similarity between the semantic vector of the first SQL statement and the semantic vector of each statement template. When calculating the similarity of two semantic vectors, the cosine similarity of the two semantic vectors can be calculated; the text model can be a General Embedding (GE) model.

[0116] In another implementation, the preset sentence template is generated based on the historical description text input by the user.

[0117] The process of the electronic device acquiring the target sentence template may include steps B1 to B3.

[0118] B1, generating a first semantic vector representing the first SQL statement and the current description text.

[0119] B2: Determine, from the pre-stored second semantic vectors, a semantic vector having the highest similarity to the first semantic vector.

[0120] Wherein, one second semantic vector corresponds to one sentence template, and one second semantic vector represents the corresponding sentence template and the historical description text corresponding to the sentence template.

[0121] B3, using the sentence template corresponding to the determined semantic vector as the target sentence template.

[0122] It is understood that the electronic device can pre-store each statement template and the historical description text corresponding to the statement template. For example, the electronic device can obtain a historical description text and execute the SQL statement generation method provided by the embodiment of the present invention on the historical description text to obtain an SQL statement that conforms to the historical description text. If the user adopts the SQL statement, the SQL statement can be used as a statement template, and the historical description text is the historical description text corresponding to the statement template.

[0123] The electronic device may concatenate each sentence template with the historical description text corresponding to the sentence template to obtain the concatenated text corresponding to the sentence template, then convert the concatenated text corresponding to the sentence template into a semantic vector to obtain a second semantic vector, and then store the second semantic vector. Specifically, the electronic device may input the concatenated text corresponding to each sentence template into a trained text model to obtain the second semantic vector.

[0124] The electronic device may concatenate the first SQL statement and the current description text to obtain a concatenated text corresponding to the first SQL statement, and then convert the concatenated text corresponding to the first SQL statement into a semantic vector to obtain a first semantic vector. Specifically, the electronic device may use the same text model used to obtain the second semantic vector to process the concatenated text corresponding to the first SQL statement to obtain the first semantic vector. The electronic device may then calculate the similarity between the first semantic vector and each second semantic vector, determine the second semantic vector with the highest similarity, and use the statement template corresponding to the second semantic vector as the target statement template.

[0125] In an embodiment of the present invention, an SQL statement is converted into a semantic vector. Even if the grammatical structure or field name of the SQL statement is different, it can be matched with the help of the semantics represented by the semantic vector. Moreover, the semantic vector also contains a description text. With the help of the semantic supplement of the description text, a second semantic vector can be matched more accurately to obtain an accurate target statement template.

[0126] In one embodiment, Figure 1 Based on the SQL statement generation method shown, Figure 3 As shown, the method further includes steps S301-S304.

[0127] S301, identifying whether there is a syntax error in the latest generated SQL statement that conforms to the current description text.

[0128] It is understandable that after obtaining the latest generated SQL statement that conforms to the current description text, in order to ensure that the SQL statement does not make mistakes during use, the SQL statement may be verified.

[0129] The electronic device can identify whether there is a syntax error in the latest generated SQL statement that conforms to the current description text. In one implementation, the electronic device can use an SQL syntax parser to parse the latest generated SQL statement that conforms to the current description text. If the parsing fails, it can be determined that there is a syntax error in the SQL statement.

[0130] In another implementation, the electronic device may determine whether the most recently generated SQL statement that matches the current description text complies with preset grammatical rules. If not, the SQL statement is determined to have a grammatical error. The preset grammatical rules include: the left and right parentheses must appear in pairs, the CASE and END keywords must appear in pairs, the SELECT keyword appears before the FROM keyword, the FROM keyword appears before the WHERE keyword, and the WHERE keyword appears before the GROUP BY keyword.

[0131] S302: If there is a syntax error, modify the latest generated SQL statement that conforms to the current description text to obtain a new SQL statement that conforms to the current description text; return to the step of identifying whether there is a syntax error in the latest generated SQL statement that conforms to the current description text, until there is no syntax error in the latest generated SQL statement that conforms to the current description text.

[0132] It can be understood that when using an SQL syntax parser to identify syntax errors in the latest generated SQL statement that conforms to the current description text, if the SQL statement contains syntax errors, the SQL syntax parser will output the specific cause of the syntax error. The electronic device can correct the SQL statement based on the cause of the syntax error. For example, if the cause of the syntax error is the lack of a right bracket corresponding to the left bracket, the electronic device can add a right bracket in the SQL statement.

[0133] In the case of using preset grammar rules to identify grammatical errors in the latest generated SQL statement that conforms to the current description text, if the SQL statement contains a grammatical error, the electronic device can detect the preset grammar rules that the SQL statement does not conform to, and then modify the SQL statement according to the grammar rules.

[0134] Since there may still be syntax errors in the modified SQL statement, the electronic device can check the syntax of the modified SQL statement after obtaining the modified SQL statement (i.e., return to execute step S301) until there are no syntax errors in the newly generated SQL statement that conforms to the current description text.

[0135] In the embodiment of the present invention, by checking and modifying grammatical errors, it can be ensured that there are no grammatical errors in the newly generated SQL statement that conforms to the current description text, basic statement execution can be achieved, and the execution resource consumption of SQL statements with grammatical errors can be reduced.

[0136] S303: If there is no syntax error, determine whether the newly generated SQL statement that conforms to the current description text satisfies the preset statement rules.

[0137] The preset statement rules include desensitization sub-rules and efficiency improvement sub-rules; the efficiency improvement sub-rules are used to improve the execution efficiency of SQL statements.

[0138] S304: If the preset statement rules are not met, the newly generated SQL statement that conforms to the current description text is corrected according to the preset statement rules to obtain a new SQL statement that conforms to the current description text; the process returns to the step of identifying whether there are syntax errors in the newly generated SQL statement that conforms to the current description text, until the preset statement rules are met and the target SQL statement is obtained.

[0139] It is understandable that the absence of syntax errors in an SQL statement is a necessary condition for the SQL statement to run. Therefore, by first identifying and correcting syntax errors through steps S301-S302, invalid statements with syntax errors can be filtered out, avoiding subsequent processing of invalid statements and wasting processing resources. If the newly generated SQL statement that conforms to the current description text does not contain syntax errors, the electronic device can determine that the SQL statement will not produce syntax errors during execution and that the SQL statement is executable. The electronic device can then further determine whether the SQL statement meets business requirements (for example, business requirements may include at least one of the following: desensitizing sensitive fields and improving the execution efficiency of SQL statements).

[0140] The desensitization sub-rule within the preset statement rules ensures data security for sensitive fields, while the efficiency sub-rule improves the efficiency of SQL statement execution. Preset statement rules can be stored in a database. A database storing preset data rules is called a standardized database.

[0141] Specifically, the desensitization sub-rule can include: non-existent field name , and the presence of desensitizing functions for sensitive fields (for example, the MASK function). Efficiency-enhancing sub-rules are used to improve the execution efficiency of SQL statements. These sub-rules can include: querying without unindexed fields, performing partitioned queries on tables with more than a specified number of rows, and using explicit join conditions for JOIN operations. The specified number of rows can be 100 million, and the explicit join condition is to directly define the related fields through the ON clause.

[0142] In one implementation, the preset statement rules are rules manually set based on personal experience. In another implementation, the preset statement rules are derived from a large language model summarizing the desensitization reasons and efficiency improvement reasons for a specified SQL statement; wherein the specified SQL statement is a desensitized SQL statement or a SQL statement that meets the execution efficiency standard. Specifically, after the electronic device generates an SQL statement that conforms to the historical description text, it can check whether the SQL statement contains sensitive fields. If no sensitive fields are present, the electronic device can input the SQL statement and a desensitization analysis prompt into the large language model. The desensitization analysis prompt indicates the desensitization reason summarized by the large language model as the absence of sensitive fields in the input SQL statement, and the desensitization sub-rule is updated using the obtained desensitization reason. The electronic device can execute the SQL statement to obtain a response result and record the execution time. If the execution time is less than the preset time, the SQL statement is determined to be a SQL statement that meets the execution efficiency standard. The electronic device can input the SQL statement and a performance analysis prompt into the large language model. The performance analysis prompt indicates the efficiency improvement reason summarized by the large language model as the high execution efficiency improvement reason for the input SQL statement, and the efficiency improvement sub-rule is updated using the obtained efficiency improvement reason. That is to say, in the embodiment of the present invention, the data in the specification database can be updated.

[0143] If the newly generated SQL statement that conforms to the current description text does not conform to the preset statement rules, the SQL statement is corrected according to the preset statement rules. For example, if the SQL statement contains a field name , then the field name Replace with explicit fields SLECTuser_id, name; if there is no desensitizing function for the sensitive field in the SQL statement, add a desensitizing function for the sensitive field. For example, if there is no desensitizing function for the sensitive field phone, add the desensitizing function MASK(phone); if there is a query on an unindexed field, obtain the field comment of the data table to which the unindexed field belongs, and add the index in the field comment to the unindexed field; if no partition query is performed on a data table with more than the specified number of rows, obtain the partition information of the data table, set the query condition according to the partition condition in the partition information, and query by partition, where the partition condition can be partitioned by time or partitioned by data location; if the join operation does not use an explicit join condition, add an ON clause to the SQL statement.

[0144] After the electronic device corrects the latest generated SQL statement that conforms to the current description text and does not meet the preset statement rules, the corrected SQL statement may have a syntax error due to the correction operation. Therefore, the electronic device can use the modified SQL statement as the latest SQL statement that conforms to the current description text, and return to execute the step of identifying whether there is a syntax error in the latest generated SQL statement that conforms to the current description text until the preset statement rules are met and the target SQL statement is obtained.

[0145] In an embodiment of the present invention, the electronic device can first perform a syntax check to ensure that the SQL statement is usable, and then perform a statement rule check to determine that the SQL statement meets business requirements (for example, desensitization and efficiency improvement), and optimize the SQL statement at the syntax and statement rule levels to ensure that the target SQL statement obtained can be executed correctly and meets business requirements.

[0146] In one implementation, the electronic device can also standardize the coding style of the newly generated SQL statements that conform to the current description text. For example, the parameter naming in the SQL statements can be standardized (using t1 to avoid mixing t1 and t2) and the indentation format in the SQL statements can be standardized to 2 spaces or 4 spaces. In this embodiment of the present invention, the standardization of the coding style can facilitate user understanding of SQL statements.

[0147] In one implementation, the electronic device may record a correction log for each correction of the latest generated SQL statement that conforms to the current description text. Specifically, the electronic device may record the error targeted by the correction and the corrected statement.

[0148] In one implementation, after obtaining the target SQL statement, the electronic device may add a comment to the target SQL statement. The comment may include: whether the target SQL statement was generated using a template filling method or a large language model generation method, so that the user can understand how the target SQL statement was generated.

[0149] In one embodiment, Figure 1 Based on the SQL statement generation method shown, Figure 4 As shown, the method further includes step S401.

[0150] S401: If an adoption instruction for a newly generated SQL statement that matches the current description text is received from a user, the SQL statement that matches the current description text is stored as a new statement template, and the current description text is stored accordingly.

[0151] It is understood that the user can determine whether to use the newly generated SQL statement that matches the current description text, that is, determine whether to adopt the SQL statement. If the user adopts the SQL statement, the electronic device can use the SQL statement to manage the data in the database. The electronic device can receive an adoption instruction for the SQL statement. For example, after generating the SQL statement that matches the current description text, the electronic device can execute the SQL statement to obtain a response result indicating whether the response was successful, record the response result and execution time, and then display the SQL statement, response result, and execution time on a display page. The user can view the display page. If the user determines that the response result meets the expected result and execution time meets the expected time, the user can issue an adoption instruction for the SQL statement on the display page. For example, the user can score the SQL statement based on the response result and execution time on the display page. For example, a successful response result will be scored higher, while a negative response result will be scored lower. The lower the execution time, the higher the score. If the score exceeds a preset score, an adoption instruction for the SQL statement will be issued.

[0152] When receiving an adoption instruction for the SQL statement, the electronic device can store the SQL statement as a new statement template and the current description text as the corresponding historical description text to update the case database. The case database can store each statement template and its corresponding historical description text.

[0153] The electronic device can use each sentence template in the case database and its corresponding historical description text to train the second language model.

[0154] Regarding the training method of the second largest language model, electronic devices can use the Low-Rank Adaptation (LoRA) algorithm or the Quantized Low-Rank Adaptation (QLoRA) algorithm to freeze most of the model parameters in the second largest language model and adjust a small number of model parameters. By adjusting a small number of model parameters, the second largest language model can quickly reach convergence, complete training, and reduce the operating resource consumption required for training.

[0155] Specifically, the electronic device can input each historical description text into the second largest language model to be trained to obtain a predicted SQL statement, calculate the loss value between the predicted SQL statement and the template statement corresponding to the historical description text, and adjust a small number of model parameters according to the loss value until the second largest language model converges.

[0156] In one implementation, the electronic device can employ a course training approach. Specifically, simpler statement templates from a case database (e.g., SQL statements for querying a single data table) and their corresponding historical descriptions are first used as ground truth and sample text for training. Subsequently, increasingly complex statement templates (e.g., SQL statements for querying multiple related data tables) and their corresponding historical descriptions are then used as ground truth and sample text for training. In this embodiment of the present invention, by training from simple to complex samples, the second language model can first understand the basic grammatical rules of SQL statements before further learning more complex statement rules. This prevents grammatical errors from occurring later in training and improves training effectiveness. In one implementation, the electronic device can use SQL statements containing out-of-order field names as noise samples for training the second language model to improve its stability. In one implementation, the electronic device can employ an early stopping mechanism for training. Specifically, if the loss value does not decrease after repeated adjustments to the model parameters, the electronic device can stop training and prompt the trainer to review the results, thus avoiding wasting training resources.

[0157] For the training data of the second largest language model, the electronic device can record the user's operation behavior through the front-end embedding method (for example, the user's score of the generated SQL statement, the user's correction record of the generated SQL statement, the execution time of the generated SQL statement, whether it is adopted and used, etc.). Based on the user's operation behavior, the electronic device can receive the adoption instruction issued by the user for the latest generated SQL statement that conforms to the current description text.

[0158] In one implementation, based on the statement templates and corresponding historical description text contained in the case database, the training data also includes SQL statements that were not adopted by users. These SQL statements may contain errors. Based on the user's record of correcting the SQL statements during user operations, the electronic device can determine the error type of the rejected SQL statements. The electronic device can input the description text corresponding to the rejected SQL statements and their error types as negative samples into the second largest language model to obtain a predicted SQL statement for the negative sample. The electronic device then calculates the loss between the predicted SQL statement and the rejected SQL statement to adjust a small number of model parameters in the second largest language model until the second largest language model converges.

[0159] Exemplarily, the description texts corresponding to the SQL statements that were not adopted in the training sample set can be obtained in the following manner: the electronic device can cluster the description texts corresponding to the SQL statements that were not adopted according to the error type to obtain a description text set for each error type; then determine the target description text set with the most description texts in the description text set, and oversample the description texts in the target description text set (for example, ten times oversampling). Specifically, copy each description text in the target description text set 10 times and add it to the training sample set.

[0160] The electronic device can store the SQL statements written by the programmer and run correctly as statement templates in the case database, store the comments of the SQL statements as historical description texts, and use the data in the current case database to perform initial training on the second largest language model. After the SQL statement generation method provided in the embodiment of the present invention is run, the electronic device can use the latest generated SQL statements that conform to the current description text to update the case database. After the case database is updated, the electronic device can use the data in the updated case database to further train the second largest language model at the preset update time. The preset update time can be a manually set periodic time, such as 12 o'clock on the evening of the first day of each month.

[0161] In one implementation, the electronic device may use a dual-copy mechanism to train the second language model. Specifically, the electronic device can copy the initial second-largest language model to obtain two identical second-largest language models A and B. The second-largest language model A can be used as the large language model for the current online service and applied to the SQL statement generation method of the embodiment of the present invention. When the preset time point is reached, the electronic device can use the data in the updated case database to train the second-largest language model B to obtain the trained second-largest language model B; then, the description text entered by the user is input into the second-largest language model B in a gradual traffic volume slicing manner. Specifically, 1% of the current description texts among the multiple current description texts can be input into the second-largest language model B. If there is no abnormality after running for a period of time, the amount of the current description text input is gradually increased (for example, from 1% to 5% to 50% to 100%). If an abnormality occurs, such as the utilization rate of the graphics processing unit (GPU) increases by 20% within a short specified time period (for example, 1ms), the current description text is stopped from being input into the second-largest language model B, and the current description text is continued to be input into the second-largest language model A to ensure that there is a backup large language model for emergency response when an abnormality occurs, thereby avoiding the inability to generate SQL statements.

[0162] In one implementation, the second prompt may also include a target statement template that matches the first SQL statement, the table name and field names of the data table indicated by the first SQL statement, and preset statement rules. The second language model can generate more accurate SQL statements based on the richer semantics of the text.

[0163] Figure 5 A schematic diagram of the principle of a method for generating SQL statements provided by an embodiment of the present invention is shown as follows: Figure 5 As shown, the electronic device can obtain the description text input by the user for describing the SQL statement to be generated as the current description text; then input the first prompt word containing the current description text into the first large language model to obtain the first SQL statement; use the SQL syntax parser to parse the first SQL statement (i.e., SQL engine parsing) to determine the specified parameters in the first SQL statement; then use the specified parameters of the first SQL statement to calculate the complexity of the first SQL statement (i.e., complexity analysis).

[0164] If the complexity is not greater than the preset threshold, a target statement template matching the first SQL statement is retrieved from the pre-stored statement templates. The table name and field names of the data table indicated by the first SQL statement are added to the target statement template to generate an SQL statement that matches the current description text. This is done by executing a simple SQL generation method to fill in the template.

[0165] If the complexity is greater than a preset threshold, the second prompt word containing the current description text and the second largest language model are used to generate an SQL statement that matches the current description text. In other words, a complex SQL generation method is used to generate a large language model.

[0166] After generating an SQL statement that conforms to the current description text, the electronic device uses the standard verification layer to check for syntax errors and determine whether it complies with preset statement rules. It then corrects the syntax errors in the SQL statement and corrects it according to the preset statement rules. In other words, it performs compliance correction.

[0167] Specifically, identify whether there are grammatical errors in the latest generated SQL statement that conforms to the current description text; if there are grammatical errors, modify the latest generated SQL statement that conforms to the current description text to obtain a new SQL statement that conforms to the current description text; return to the step of identifying whether there are grammatical errors in the latest generated SQL statement that conforms to the current description text, until there are no grammatical errors in the latest generated SQL statement that conforms to the current description text. If there are no grammatical errors, determine whether the latest generated SQL statement that conforms to the current description text meets the preset statement rules; wherein the preset statement rules include desensitization sub-rules and efficiency-enhancing sub-rules; the efficiency-enhancing sub-rules are used to improve the execution efficiency of SQL statements; if the preset statement rules are not met, modify the latest generated SQL statement that conforms to the current description text according to the preset statement rules to obtain a new SQL statement that conforms to the current description text; return to the step of identifying whether there are grammatical errors in the latest generated SQL statement that conforms to the current description text, until the preset statement rules are met and the target SQL statement is obtained.

[0168] If the user receives an instruction to adopt a newly generated SQL statement that matches the current description, the electronic device can store the SQL statement that matches the current description as a new statement template, along with the current description. This creates a closed feedback loop. The second largest language model is trained using each statement template and the historical description text corresponding to it. The electronic device can also update preset statement rules based on business needs, essentially updating the knowledge base and iterating the model.

[0169] Figure 6 Schematic diagram of the principle of computing complexity in the SQL statement generation method provided by the embodiment of the present invention. Figure 6 As shown, the electronic device can obtain the description text input by the user for describing the SQL statement to be generated as the current description text; input the first prompt word containing the current description text into the first language model to obtain the first SQL statement; parse the first SQL statement through the SQL syntax parser to obtain the first syntax tree corresponding to the first SQL statement, that is, the abstract syntax tree.

[0170] Then, through the feature extraction module, the maximum path depth from the root node to the leaf node in the first syntax tree is reduced by one to obtain the number of nested layers of the first SQL statement; the number of nodes with a connection type in the first syntax tree is increased by one to obtain the number of data tables associated with the first SQL statement; the number of nodes with a window function type in the first syntax tree is counted to obtain the number of window functions contained in the first SQL statement; the number of sensitive fields in the statement content of nodes with a field type in the first syntax tree is counted. Then, based on the complexity evaluation model, the complexity of the first SQL statement is calculated using the specified parameters of the first SQL statement. Among them, the complexity evaluation model is the complexity formula mentioned in the aforementioned embodiment.

[0171] Figure 7 Schematic diagram of the principle of checking and correcting in the SQL statement generation method provided by the embodiment of the present invention. Figure 7 As shown, the electronic device can use the most recently generated SQL statement that conforms to the current description text as a candidate SQL. The candidate SQL is then input into the syntax verification layer to identify whether there are syntax errors in the most recently generated SQL statement that conforms to the current description text. If there are no syntax errors, the candidate SQL is input into the specification verification layer to determine whether the candidate SQL meets the preset statement rules. In the performance optimization layer, if the preset statement rules are not met, the most recently generated SQL statement that conforms to the current description text is corrected according to the preset statement rules to obtain a new SQL statement that conforms to the current description text; and the process returns to the step of identifying whether there are syntax errors in the most recently generated SQL statement that conforms to the current description text, until the preset statement rules are met and the target SQL statement is obtained.

[0172] The embodiment of the present invention also provides a device for generating SQL statements, such as Figure 8 As shown, the device includes:

[0173] An acquisition module 810 is used to acquire a description text input by a user for describing a desired SQL statement to be generated as the current description text; an input module 820 is used to input a first prompt word containing the current description text into a first large language model to obtain a first SQL statement; wherein the first prompt word is used to instruct the first large language model to generate an SQL statement that conforms to the input text; a first utilization module 830 is used to calculate the complexity of the first SQL statement using specified parameters of the first SQL statement; wherein the specified parameters include at least one of the following: the number of nesting levels of the first SQL statement, the number of associated data tables, the number of window functions included, and the number of sensitive fields included; the complexity is positively correlated with each of the specified parameters ; A first generation module 840 is used to generate an SQL statement that conforms to the current description text using a second prompt word and a second large language model containing the current description text if the complexity is greater than a preset threshold; wherein the second prompt word is used to instruct the second large language model to generate an SQL statement that conforms to the input text, and the number of model parameters of the second large language model is greater than the number of model parameters of the first large language model; a second generation module 850 is used to obtain a target statement template that matches the first SQL statement from each pre-stored statement template if the complexity is not greater than the preset threshold; and fill the table name of the data table indicated by the first SQL statement and the field name in the data table into the target statement template to generate an SQL statement that conforms to the current description text.

[0174] Optionally, the first utilization module 830 is specifically configured to calculate a weighted sum of parameters included in the designated parameters to obtain the complexity of the first SQL statement.

[0175] Optionally, the number of nesting levels, the number of associated data tables, the number of included window functions, and the number of included sensitive fields of the first SQL statement are determined by:

[0176] Parse the syntax units in the first SQL statement to obtain a first syntax tree corresponding to the first SQL statement; wherein, the nodes in the first syntax tree correspond one-to-one to the syntax units in the first SQL statement; subtract one from the maximum path depth from the root node to the leaf node in the first syntax tree to obtain the number of nesting levels of the first SQL statement; add one to the number of nodes with a connection type in the first syntax tree to obtain the number of data tables associated with the first SQL statement; count the number of nodes with a window function type in the first syntax tree to obtain the number of window functions included in the first SQL statement; count the number of sensitive fields in the statement content of nodes with a field type in the first syntax tree.

[0177] Optionally, the device also includes: an identification module, used to identify whether there is a syntax error in the latest generated SQL statement that conforms to the current description text; a modification module, used to modify the latest generated SQL statement that conforms to the current description text if a syntax error exists, to obtain a new SQL statement that conforms to the current description text; and return to execute the step of identifying whether there is a syntax error in the latest generated SQL statement that conforms to the current description text until there is no syntax error in the latest generated SQL statement that conforms to the current description text.

[0178] Optionally, the device also includes: a judgment module, which is used to judge whether the latest generated SQL statement that conforms to the current description text satisfies the preset statement rules if there are no syntax errors; wherein the preset statement rules include desensitization sub-rules and efficiency improvement sub-rules; the efficiency improvement sub-rules are used to improve the execution efficiency of SQL statements; a correction module, which is used to correct the latest generated SQL statement that conforms to the current description text according to the preset statement rules if the preset statement rules are not satisfied, to obtain a new SQL statement that conforms to the current description text; and return to execute the step of identifying whether there are syntax errors in the latest generated SQL statement that conforms to the current description text until the preset statement rules are satisfied and the target SQL statement is obtained.

[0179] Optionally, a preset sentence template is generated based on the historical description text input by the user;

[0180] The second generation module 850 is specifically used to generate a first semantic vector representing the first SQL statement and the current description text; determine the semantic vector with the highest similarity to the first semantic vector from the pre-stored second semantic vectors; wherein, one second semantic vector corresponds to one statement template, and one second semantic vector represents the corresponding statement template and the historical description text corresponding to the statement template; and use the statement template corresponding to the determined semantic vector as the target statement template.

[0181] Optionally, the device also includes: a storage module, which is used to store the SQL statement that conforms to the current description text as a new statement template if it receives an adoption instruction from the user for the latest generated SQL statement that conforms to the current description text, and store the current description text accordingly; the second largest language model is: obtained by training using each statement template and the historical description text corresponding to the statement template.

[0182] In the embodiment of the present invention, the machine can use a lightweight large language model to recognize the current description text and obtain preliminary SQL statements. According to the complexity of the calculated preliminary SQL statements, different methods are used to generate SQL statements, which can avoid the user manually judging the complexity of the SQL statements to be generated and selecting the method of generating SQL statements. This can also avoid the problem of generating SQL statements with low accuracy or resulting in low efficiency in generating SQL statements, making it convenient for users to manage data.

[0183] The embodiment of the present invention further provides an electronic device, such as Figure 9 As shown, it includes a processor 901 , a communication interface 902 , a memory 903 and a communication bus 904 , wherein the processor 901 , the communication interface 902 and the memory 903 communicate with each other via the communication bus 904 .

[0184] The memory 903 is used to store computer programs; the processor 901 is used to implement the SQL statement generation method provided in the above embodiment when executing the program stored in the memory 903.

[0185] The communication bus mentioned in the electronic devices mentioned above can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. This communication bus can be divided into address buses, data buses, control buses, etc. For ease of illustration, only a single thick line is used in the figure, but this does not mean that there is only one bus or only one type of bus.

[0186] The communication interface is used for communication between the above electronic device and other devices.

[0187] The memory may include random access memory (RAM) or non-volatile memory (NVM), such as at least one disk storage. Alternatively, the memory may be at least one storage device located away from the processor.

[0188] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, and discrete hardware components.

[0189] In another embodiment of the present invention, a computer-readable storage medium is provided, which stores a computer program. When the computer program is executed by a processor, the computer program implements the steps of any of the above-mentioned SQL statement generation methods.

[0190] In another embodiment of the present invention, a computer program product including instructions is provided. When the computer program product is run on a computer, the computer executes any one of the SQL statement generation methods in the above embodiments.

[0191] In the above embodiments, all or part of the embodiments can be implemented using software, hardware, firmware, or any combination thereof. When implemented using software, all or part of the embodiments can be implemented in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the processes or functions described in accordance with the embodiments of the present invention are generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that can be accessed by a computer, or a data storage device such as a server or data center that integrates one or more available media. The available medium can be magnetic media (e.g., floppy disk, hard disk, tape), optical media (e.g., DVD), or semiconductor media (e.g., solid-state disk (SSD)).

[0192] It should be noted that, in this document, relational terms such as first and second, etc., are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply the existence of any such actual relationship or order between these entities or operations. Moreover, the terms "comprises," "comprising," or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article, or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or device. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not exclude the presence of other identical elements in the process, method, article, or device comprising the element.

[0193] Each embodiment in this specification is described in a related manner. Similar portions between the embodiments can be referred to in conjunction with each other. Each embodiment focuses on the differences from other embodiments. In particular, the device embodiments are generally similar to the method embodiments, so their description is relatively simple. For related portions, refer to the description of the method embodiments.

[0194] The above description is only a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present invention are included in the scope of protection of the present invention.

Claims

1. A method for generating an SQL statement, characterized in that: The method comprises: Get the description text entered by the user, which is used to describe the SQL statement to be generated, as the current description text; Inputting a first prompt word containing the current description text into a first large language model to obtain a first SQL statement; wherein the first prompt word is used to instruct the first large language model to generate an SQL statement that conforms to the input text; Calculating the complexity of the first SQL statement using specified parameters of the first SQL statement; wherein the specified parameters include at least one of the following: the number of nesting levels of the first SQL statement, the number of associated data tables, the number of included window functions, and the number of included sensitive fields; and the complexity is positively correlated with each of the specified parameters; If the complexity is greater than a preset threshold, generating an SQL statement that conforms to the current description text using a second prompt word and a second language model containing the current description text; wherein the second prompt word is used to instruct the second language model to generate an SQL statement that conforms to the input text, and the number of model parameters of the second language model is greater than the number of model parameters of the first language model; If the complexity is not greater than the preset threshold, a target statement template that matches the first SQL statement is obtained from the pre-stored statement templates; the table name of the data table indicated by the first SQL statement and the field name in the data table are filled into the target statement template to generate an SQL statement that conforms to the current description text.

2. The method according to claim 1, characterized in that The calculating the complexity of the first SQL statement by using the specified parameters of the first SQL statement includes: A weighted sum of the parameters included in the specified parameters is calculated to obtain the complexity of the first SQL statement.

3. The method according to claim 1, characterized in that The number of nesting levels, the number of associated data tables, the number of included window functions, and the number of included sensitive fields of the first SQL statement are determined in the following manner: Parsing the syntax units in the first SQL statement to obtain a first syntax tree corresponding to the first SQL statement; wherein the nodes in the first syntax tree correspond one-to-one to the syntax units in the first SQL statement; Subtract one from the maximum path depth from the root node to the leaf node in the first syntax tree to obtain the number of nesting levels of the first SQL statement; Add one to the number of nodes of the connection type in the first syntax tree to obtain the number of data tables associated with the first SQL statement; Counting the number of nodes whose node type is a window function type in the first syntax tree to obtain the number of window functions included in the first SQL statement; Count the number of sensitive fields in the sentence content of the nodes whose node type is the field type in the first syntax tree.

4. The method according to claim 1, wherein The method further comprises: Identify whether there are syntax errors in the latest generated SQL statement that conforms to the current description text; If there is a syntax error, the newly generated SQL statement that conforms to the current description text is modified to obtain a new SQL statement that conforms to the current description text; and the step of identifying whether there is a syntax error in the newly generated SQL statement that conforms to the current description text is returned to execute until there is no syntax error in the newly generated SQL statement that conforms to the current description text.

5. The method according to claim 4, characterized in that The method further comprises: If there are no syntax errors, determine whether the newly generated SQL statement that conforms to the current description text meets the preset statement rules; wherein the preset statement rules include desensitization sub-rules and efficiency improvement sub-rules; the efficiency improvement sub-rules are used to improve the execution efficiency of the SQL statement; If the preset statement rules are not met, the newly generated SQL statement that conforms to the current description text is corrected according to the preset statement rules to obtain a new SQL statement that conforms to the current description text; and the process returns to the step of identifying whether there are syntax errors in the newly generated SQL statement that conforms to the current description text until the preset statement rules are met and the target SQL statement is obtained.

6. The method according to any one of claims 1 to 5, characterized in that The preset sentence template is generated based on the historical description text input by the user; The acquiring a target statement template matching the first SQL statement from each pre-stored statement template includes: Generate a first semantic vector representing the first SQL statement and the current description text; Determine, from the pre-stored second semantic vectors, a semantic vector having the highest similarity to the first semantic vector; wherein one second semantic vector corresponds to one sentence template, and one second semantic vector represents the corresponding sentence template and the historical description text corresponding to the sentence template; The sentence template corresponding to the determined semantic vector is used as the target sentence template.

7. The method according to claim 6, characterized in that The method further comprises: If an adoption instruction for the newly generated SQL statement that conforms to the current description text is received from the user, the SQL statement that conforms to the current description text is stored as a new statement template and the current description text is stored accordingly; The second largest language model is obtained by training using each sentence template and the historical description text corresponding to the sentence template.

8. A device for generating SQL statements, characterized in that: The device comprises: The acquisition module is used to acquire the description text input by the user and used to describe the SQL statement to be generated as the current description text; An input module, configured to input a first prompt word containing the current description text into a first large language model to obtain a first SQL statement; wherein the first prompt word is used to instruct the first large language model to generate an SQL statement that conforms to the input text; a first utilizing module, configured to calculate the complexity of the first SQL statement using specified parameters of the first SQL statement; wherein the specified parameters include at least one of the following: the number of nesting levels of the first SQL statement, the number of associated data tables, the number of included window functions, and the number of included sensitive fields; and the complexity is positively correlated with each of the specified parameters; a first generation module configured to generate an SQL statement that conforms to the current description text using a second prompt word and a second language model containing the current description text if the complexity is greater than a preset threshold; wherein the second prompt word is used to instruct the second language model to generate an SQL statement that conforms to the input text, and the number of model parameters of the second language model is greater than the number of model parameters of the first language model; The second generation module is used to obtain a target statement template that matches the first SQL statement from the pre-stored statement templates if the complexity is not greater than the preset threshold; fill the table name of the data table indicated by the first SQL statement and the field name in the data table into the target statement template to generate an SQL statement that conforms to the current description text.

9. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus; Memory for storing computer programs; A processor, configured to implement the method according to any one of claims 1 to 7 when executing a program stored in a memory.

10. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.

Citation Information

Patent Citations

  • Structured query language statement generation method and device, equipment and storage medium

    CN118363977A

  • SQL (Structured Query Language) statement generation method and device based on large language model

    CN119127922A