SQL automatic generation method based on learning

Through the learning-based SQL automatic generation method, using reinforcement learning and finite state automata technology, the problems of inaccurate and high cost of SQL statement generation in the existing technology are solved, and efficient and accurate SQL statement generation is achieved.

CN114896266BActive Publication Date: 2025-05-02TSINGHUA UNIVERSITY

Patent Information

Application Number
CN202210457835.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-04-27
Publication Date
2025-05-02
Estimated Expiration
2042-04-27

AI Technical Summary

Technical Problem

When generating SQL statements, the prior art cannot accurately and fully meet the actual needs of users, and the cost is high.

Method used

The learning-based SQL automatic generation method is adopted to regulate the generation problems of SQL statements into the selection problems of each word in the sequence, design the word selection model through reinforcement learning, and ensure the legality of generating SQL statements through finite state automata.

Benefits of technology

It realizes the rapid generation of SQL statements that meet different constraints that meet user requirements, which improves the accuracy and efficiency of generating SQL statements and reduces costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114896266B_ABST
    Figure CN114896266B_ABST
Patent Text Reader

Abstract

The present application proposes a learning-based SQL automatic generation method, which includes: obtaining a database to be queried and preset target constraints, and constructing a vocabulary required for generating SQL statements according to the database; generating a finite state automaton according to SQL grammar rules; inputting the encoding of the specified initial word into the constructed word selection probability model, obtaining the SQL statement output based on the encoding of the initial word, and calculating the actual benefit value of each word in the SQL statement; calculating the benefit estimation value of the next word through the constructed word selection benefit estimation model and constraint encoding network, and updating the parameters of each constructed network model according to the difference between the actual benefit value and the benefit estimation value; repeating the above steps until the network model converges, and generating the target SQL statement through the trained word selection probability model. The method can automatically generate SQL statements that meet the current constraints, improve the accuracy of generating SQL statements, and reduce costs.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of information retrieval technology, and in particular to a learning-based SQL automatic generation method. Background Art

[0002] At present, in the process of database application, many database optimization operations, such as slow SQL diagnosis, database testing and optimizer tuning, require a large number of queries based on Structured Query Language (SQL). However, in order to ensure the privacy of data, it is difficult to obtain the real SQL query statement, so the generation of SQL statement is a very important task in database optimization.

[0003] In the related art, when generating SQL statements, some tools that randomly generate SQL are usually used, such as SQLsmith and RAGs, to randomly obtain SQL statements. However, this method has great limitations. First, the SQL query statements generated by this method may be useless, such as SQL that returns empty results; second, it is impossible to generate SQL queries that meet user needs. For example, when users need to enhance the optimizer, they need to optimize SQL queries with a small cardinality, but the tools that randomly generate SQL cannot customize the generation of SQL queries with a small cardinality. In addition, some tools that randomly generate SQL use heuristic algorithms, such as hill climbing and branch and bound methods, to generate useful SQL that can meet user needs. However, this method requires database experts to manually create high-quality SQL query templates, and the creation cost of many constraint templates is high. In addition, it is also difficult to create templates when unfamiliar with new database instances. Secondly, in different application scenarios, using specially made templates may not be able to find query statements that meet the current constraints, and may miss important SQL statements.

[0004] In summary, the above method for generating SQL statements cannot accurately and fully meet the actual needs of users, and the cost is high. Therefore, there is an urgent need for a method for generating SQL statements that can be more accurate and convenient. Summary of the invention

[0005] The present application aims to solve one of the technical problems in the related art at least to some extent.

[0006] To this end, the first purpose of this application is to propose a learning-based SQL automatic generation method, which reduces the SQL statement generation problem to the selection problem of each word in the sequence, selects each word in the SQL statement in sequence based on the word selection model after reinforcement learning, and designs a reward function to accurately guide the generation direction of the statement. And by designing a finite state automaton to ensure the legitimacy of the generated SQL statement, and by generalizing the task scenarios applicable to this method through a meta-learning criticism network, SQL with different constraints that meet user requirements can be quickly generated.

[0007] The second objective of this application is to propose a learning-based SQL automatic generation system.

[0008] A third object of the present application is to provide a non-transitory computer-readable storage medium.

[0009] To achieve the above objectives, the first embodiment of the present application is to propose a learning-based SQL automatic generation method, the method comprising the following steps:

[0010] S101: Obtain a database to be queried and preset target constraints, and construct a vocabulary required to generate a structured query language SQL statement based on the information of the database, and encode each word in the vocabulary;

[0011] S102: Generate a finite state automaton according to SQL grammar rules, and construct a word selection probability model, a word selection benefit estimation model and a constraint coding network, and obtain the coding of the specified initial word in the vocabulary;

[0012] S103: inputting the code of the initial word into the word selection probability model, obtaining the SQL statement output by the word selection probability model based on the code of the initial word, and calculating the actual benefit value of each word in the SQL statement according to the performance of the SQL statement in the database and the target constraint condition;

[0013] S104: Calculate the estimated benefit value of the next word after the initial word in the SQL statement through the word selection benefit estimation model and the constraint coding network, and update the parameters of each constructed network model according to the difference between the actual benefit value and the estimated benefit value;

[0014] S105: Repeat steps S103 to S104 until each of the constructed network models converges, and generate a target SQL statement through the trained word selection probability model.

[0015] Optionally, in one embodiment of the present application, the word selection probability model includes a first embedding layer, a first recurrent neural network and a Softmax layer, and the obtaining of the SQL statement output by the word selection probability model based on the encoding of the initial word includes: S10: inputting the encoding of the initial word into the first embedding layer to generate an embedding vector corresponding to the encoding of the initial word; S20: inputting the embedding vector into the first recurrent neural network to generate a hidden state vector of the next word; S30: inputting the initial word into the finite state automaton to obtain a legal candidate word for the next word from all the words in the vocabulary; S40: inputting the hidden state vector into the Softmax layer to generate a selection probability distribution of the next word; S50: selecting the next word from the legal candidate words according to the selection probability distribution; S60: inputting the encoding of the next word into the first embedding layer to generate a corresponding embedding vector, and repeatedly executing steps S20 to S50 until the terminator word is selected to generate the complete SQL statement.

[0016] Optionally, in one embodiment of the present application, the word selection profit estimation model includes a second embedding layer and a second recurrent neural network, and the profit estimation value of the next word after the initial word in the SQL statement is calculated through the word selection profit estimation model and the constraint coding network, including: inputting the encoding of each word in the SQL statement into the second embedding layer in sequence, and obtaining the embedding vector corresponding to each encoding; generating a triple according to the hidden state vector, encoding and actual profit value of the next word, and reversing a preset number of time series with the order of the hidden state vector of the next word as the starting point, and inputting the triple sequence within the preset number of time series into the constraint coding network to obtain the constraint coding; after splicing the embedding vector corresponding to each coding with the constraint coding, input it into the second recurrent neural network to obtain the profit estimation value of the next word.

[0017] Optionally, in one embodiment of the present application, calculating the actual revenue value of each word in the SQL statement according to the performance of the SQL statement in the database and the target constraint condition includes:

[0018] When the target constraint is a point constraint, the actual benefit value of the SQL statement is calculated by the following formula:

[0019]

[0020] Among them, c′ is the performance, c is the point constraint, and R is the actual benefit value of the SQL statement;

[0021] When the target constraint is a range constraint, the actual benefit value of the SQL statement is calculated by the following formula:

[0022]

[0023] Among them, c′ is the performance, [c l , c r ] is the range constraint, and R is the actual return value of the SQL statement;

[0024] The actual benefit value of each word is set equal to the actual benefit value of the SQL statement.

[0025] Optionally, in one embodiment of the present application, updating the parameters of each constructed network model according to the difference between the actual revenue value and the estimated revenue value includes:

[0026] The difference error between the actual profit value and the profit estimate value is calculated by the following formula:

[0027] A t =|r t +v t+1 -v t |

[0028] Among them, r t is the actual revenue value of the current time series, v t is the estimated value of the current time series, v t+1 is the estimated value of the revenue in the next time series;

[0029] The parameters of each constructed network model are updated by controlling the gradient descent of the strategy so that the differential error reaches the minimum value.

[0030] Optionally, in one embodiment of the present application, a finite state automaton is generated according to SQL grammar rules, including: extracting the primary and foreign key relationships of the relational tables in the database, and collecting the data in the relational tables; constructing the nodes and edges of the finite state automaton according to the primary and foreign key relationships, the data in the relational tables and the SQL grammar rules, wherein the nodes correspond to the states of the SQL statements, and each of the nodes is connected to the next word that can be spliced ​​through a corresponding edge.

[0031] To achieve the above purpose, the second aspect of the present application also proposes a learning-based SQL automatic generation system, including the following modules:

[0032] The acquisition module is used to obtain the database to be queried and the preset target constraints, and to construct the vocabulary required to generate the structured query language SQL statement based on the information of the database, and to encode each word in the vocabulary;

[0033] A construction module is used to generate a finite state automaton according to SQL grammar rules, and to construct a word selection probability model, a word selection benefit estimation model and a constraint coding network, and to obtain the coding of a specified initial word in the vocabulary;

[0034] A first generating module is used to input the code of the initial word into the word selection probability model, obtain the SQL statement output by the word selection probability model based on the code of the initial word, and calculate the actual benefit value of each word in the SQL statement according to the performance of the SQL statement in the database and the target constraint condition;

[0035] An updating module, used to calculate the estimated benefit value of the next word after the initial word in the SQL statement through the word selection benefit estimation model and the constraint coding network, and update the parameters of each constructed network model according to the difference between the actual benefit value and the estimated benefit value;

[0036] The second generation module is used to control the first generation module and the update module to repeatedly execute their own functions until each of the constructed network models converges, and generate a target SQL statement through the trained word selection probability model.

[0037] Optionally, in one embodiment of the present application, the word selection probability model includes a first embedding layer, a first recurrent neural network and a Softmax layer, and the first generation module is specifically used to: input the encoding of the initial word into the first embedding layer to generate an embedding vector corresponding to the encoding of the initial word; input the embedding vector into the first recurrent neural network to generate a hidden state vector of the next word; input the initial word into the finite state automaton to obtain a legal candidate word for the next word from all the words in the vocabulary; input the hidden state vector into the Softmax layer to generate a selection probability distribution of the next word; select the next word from the legal candidate words according to the selection probability distribution; input the encoding of the next word into the first embedding layer to generate a corresponding embedding vector, and repeat the step of generating the next word until the terminator word is selected to generate the complete SQL statement.

[0038] Optionally, in one embodiment of the present application, the word selection profit estimation model includes a second embedding layer and a second recurrent neural network, and the update module is specifically used to: input the encoding of each word in the SQL statement into the second embedding layer in sequence to obtain the embedding vector corresponding to each encoding; generate a triplet according to the hidden state vector, encoding and actual profit value of the next word, and reverse the preset number of time series with the order of the hidden state vector of the next word as the starting point, input the triplet sequence within the preset number of time series into the constraint coding network to obtain the constraint coding; splice the embedding vector corresponding to each coding with the constraint coding, and input it into the second recurrent neural network to obtain the profit estimation value of the next word.

[0039] In order to implement the above embodiments, the third aspect of the present application further proposes a non-temporary computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the learning-based SQL automatic generation method in the above embodiments is implemented.

[0040] The technical solution provided by the embodiments of the present application brings at least the following beneficial effects: the present application adopts the strategy of exploration and utilization in reinforcement learning (RL), learns the SQL generation direction through the explored SQL statement execution results, and uses the learned results to generate SQL statements that meet the constraints. The present application designs the reward function in RL according to the actual application scenario to accurately guide the generation process, and ensures the legal query generation space through an integrated finite state machine. In addition, the meta-learning network is used to accelerate model training, so that the solution of the present application can be effectively extended to other SQL generation tasks with different constraints, thereby improving the efficiency and applicability of the SQL automatic generation method of the present application. Thus, the present application can automatically generate SQL statements that meet the current constraints, improve the accuracy and efficiency of generating SQL statements, and reduce costs.

[0041] Additional aspects and advantages of the present invention will be given in part in the following description and in part will be obvious from the following description, or will be learned through practice of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS

[0042] The above and / or additional aspects and advantages of the present application will become apparent and easily understood from the following description of the embodiments in conjunction with the accompanying drawings, in which:

[0043] Figure 1 A flowchart of a learning-based SQL automatic generation method proposed in an embodiment of the present application;

[0044] Figure 2A flowchart of a specific method for generating SQL statements based on initial words proposed in an embodiment of the present application;

[0045] Figure 3 A schematic diagram of the architecture of a specific learning-based automatic SQL statement generation system proposed in an embodiment of the present application;

[0046] Figure 4 A flowchart of a process for generating a SQL statement proposed in an embodiment of the present application;

[0047] Figure 5 A flowchart of a specific SQL statement generation process proposed in an embodiment of the present application;

[0048] Figure 6 A schematic diagram of the structure of a finite state automaton proposed in an embodiment of the present application;

[0049] Figure 7 A schematic diagram of generalization in a model training proposed in an embodiment of the present application;

[0050] Figure 8 A structural diagram of a learning-based SQL statement automatic generation system proposed in an embodiment of the present application. DETAILED DESCRIPTION

[0051] Embodiments of the present invention are described in detail below, examples of which are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are intended to be used to explain the present invention, and should not be construed as limiting the present invention.

[0052] A learning-based SQL automatic generation method and system proposed in an embodiment of the present invention will be described below with reference to the accompanying drawings.

[0053] Figure 1 A flowchart of a learning-based SQL automatic generation method proposed in an embodiment of the present application, such as Figure 1 As shown, the method comprises the following steps:

[0054] Step S101: Obtain the database to be queried and the preset target constraint conditions, and construct the vocabulary required to generate the structured query language SQL statement according to the information of the database, and encode each word in the vocabulary.

[0055] The database to be queried refers to a database that needs to be queried based on SQL to complete the optimization, and the database to be queried can be various types of databases. The preset target constraint condition can be a constraint condition defined by the user according to actual needs, for example, the target constraint condition is that the cardinality of the SQL query statement is within a smaller range, etc. The type of the target constraint condition can be a cost constraint or a cardinality constraint, and the constraint form can be a point constraint or a range constraint, etc., which are not limited here.

[0056] Specifically, the database to be queried and the preset target constraint conditions are first obtained. Then, various types of information such as relational tables and data included in the obtained database are extracted, and the vocabulary required to generate the SQL statement corresponding to the database is statistically calculated based on the extracted information.

[0057] In one embodiment of the present application, the word library constructed according to the information of the database is denoted as A, and the word library includes five types of data: database reserved fields, metadata, relational table data, operators and terminators. Each word in the word library is encoded, and after encoding, each word corresponds to a hot encoding. All words in the word library constitute an action space, in which each word corresponds to an action, and the size of the action space is denoted as |A|.

[0058] S102: Generate a finite state automaton according to the SQL grammar rules, and build a word selection probability model, a word selection benefit estimation model and a constraint coding network to obtain the coding of the specified initial word in the vocabulary.

[0059] The initial word is the first word specified when training the constructed word selection probability model, word selection benefit estimation model and constraint coding network, or generating SQL statements. The initial word can be any word in the vocabulary. Since each word in the vocabulary has been encoded in step S101, after determining the initial word, the encoding of the initial word can be directly found in the vocabulary. Specifically, the initial word can be specified in different ways. As a possible implementation method, the initial word can be specified by the user in the vocabulary.

[0060] Specifically, the finite state automaton FSM includes a finite number of states, each state can migrate to zero or more states, and inputting a string into the finite state automaton can determine which state to migrate to. The word selection benefit estimation model and the constraint encoding network can be constructed based on meta-learning, that is, the word selection benefit estimation model and the constraint encoding network can be used as a meta-learning criticism network to accelerate the training of the word selection probability model, so that the trained word selection probability model can quickly generate SQL statements with different constraints that meet user needs.

[0061] In one embodiment of the present application, when a finite state automaton is generated according to SQL grammar rules, the primary and foreign key relationships of the relational tables in the database are first extracted, and the data in the relational tables are collected. Then, based on the primary and foreign key relationships, the data in the relational tables, and the SQL grammar rules, the nodes and edges of the finite state automaton are constructed, wherein the nodes correspond to the state of the SQL statement, and each node is connected to the next word that can be spliced ​​through the corresponding edge, that is, each edge connecting the current state of the SQL statement corresponds to the next word that can be spliced ​​with the current SQL statement, and words without edge connection represent illegal words.

[0062] When constructing a word selection probability model specifically, as an example, the word selection probability model can be set to include a first embedding layer, a first recurrent neural network and a Softmax layer, wherein the first recurrent neural network is a long short-term memory neural network, and the long short-term memory neural network is composed of two long short-term memory neural network hidden layers, each hidden layer includes three neural network layers of a forget gate, an input gate and an output gate, and the long short-term memory neural network input layer is set to 256 dimensions, the input dimension of the first hidden layer is 256 dimensions, and the output dimension is 128 dimensions, and the input dimension of the second hidden layer is 128 dimensions, and the output dimension is 128 dimensions.

[0063] When constructing a constraint coding network, as an example, the constraint coding network can be set as a single-layer long short-term memory neural network. According to the data subsequently input to the constraint coding network, its input layer is set to 513 layers and its output layer size is set to 256 dimensions.

[0064] When constructing a specific word selection benefit estimation model, as an example, the word selection benefit estimation model can be set to include a second embedding layer and a second recurrent neural network, wherein the second recurrent neural network is a long short-term memory neural network, and the long short-term memory neural network is composed of two layers of long short-term memory neural network hidden layers, and each hidden layer contains three neural network layers of forget gate, input gate and output gate. According to the size of the constraint coding and state vector data subsequently input to the word selection benefit estimation model, the input layer size of the long short-term memory neural network is set to 512, the first hidden layer input dimension is 256 dimensions, the output dimension is 128 dimensions, the second hidden layer input dimension is 128 dimensions, and the output layer size is 1.

[0065] S103: Input the encoding of the initial word into the word selection probability model, obtain the SQL statement output by the word selection probability model based on the encoding of the initial word, and calculate the actual benefit value of each word in the SQL statement according to the performance of the SQL statement in the database and the target constraints.

[0066] Specifically, after the word selection probability model, the word selection benefit estimation model and the constraint coding network are constructed, the recurrent neural network and the constraint coding network in the model are first trained so that the sequence of SQL statements required by the user can be output through the trained word selection probability model. During the training process, the encoding of the initial word determined above is first input into the word selection probability model, a complete SQL statement output by the word selection probability model is obtained, and the SQL statement is put into the database to obtain the actual benefit value of the SQL statement. Then, the word selection benefit estimation model and the constraint coding network are used to assist in the training, specifically, the parameters of each model are adjusted by the difference between the actual value and the estimated value of the benefit, so that the recurrent neural network converges.

[0067] In order to more clearly illustrate the specific implementation process of the word selection probability model of the present application outputting SQL statements according to the specified initial words, the following is an exemplary description of a method for generating SQL statements based on initial words proposed in an embodiment of the present application. This method is applicable to the example of the network model constructed in step S102, such as Figure 2 As shown, the method comprises the following steps:

[0068] S10: Input the encoding of the initial word into the first embedding layer to generate an embedding vector corresponding to the encoding of the initial word.

[0069] Specifically, an embedding layer of the word selection probability model is constructed. The embedding layer is a single-layer neural network. The input layer size of the embedding layer is |A|, and the output layer size is 256. Then, the encoding of the initial word is input into the embedding layer of the word selection probability model to obtain the embedding vector corresponding to the word encoding, where the embedding vector refers to a relatively low-dimensional vector representing the hot encoding of a word. The shorter the distance between the embedding vectors of two words, the higher the similarity between the two words.

[0070] S20: Input the embedding vector into the first recurrent neural network to generate a hidden state vector of the next word.

[0071] S30: Input the initial word into the finite state automaton, and obtain the legal candidate words for the next word from all the words in the vocabulary.

[0072] Specifically, the word is input into the finite state automaton to obtain a valid candidate word. A valid candidate word refers to a word that can generate an executable SQL statement if each of the following steps is selected within it.

[0073] S40: Input the hidden state vector to the Softmax layer to generate the probability distribution of the next word selection.

[0074] Specifically, the input layer size of the softmax layer is the dimension of the state vector, and the output layer size is |A|. The number of each dimension of the output layer of the softmax layer is between 0 and 1, indicating the probability of selecting the word represented by this dimension in the next step. The state hidden vector is input to the softmax layer to obtain the selection probability distribution of the candidate word.

[0075] S50: Select the next word from the legal words to be selected according to the selection probability distribution.

[0076] Specifically, the next word to generate SQL is selected from the legal candidate words according to the obtained probability distribution and is recorded as a, and the probability of illegal words is masked, that is, the probability of illegal words is set to 0.

[0077] S60: Input the encoding of the next word into the first embedding layer to generate a corresponding embedding vector, and repeat steps S20 to S50 until the terminator word is selected to generate a complete SQL statement.

[0078] Specifically, the initial word in step S10 is replaced with the next word currently generated, the encoding of the next word is used as the input of the embedding layer, and the above steps are repeated until the word selected in the current round is a terminator word, thereby obtaining a complete SQL statement.

[0079] Thus, the constructed word selection probability model is used as the actor for SQL statement generation, and an SQL statement is output based on the specified initial words.

[0080] Further, the complete SQL statement generated by the word selection probability model is input into the database to be queried in step S101 to obtain the performance of the SQL statement in the database, wherein the performance is of the same type as the preset target constraint, which can be an estimated cost or an estimated cardinality. Then, the actual benefit of the SQL is calculated based on the obtained performance and the preset target constraint, and then the benefit of each word is calculated based on the actual benefit of the SQL.

[0081] In specific implementation, since the preset target constraint conditions may be in various forms in actual applications, in order to ensure the applicability of the generation method of the present application, in one embodiment of the present application, the actual benefit value of each word in the SQL statement is calculated according to the performance of the SQL statement in the database and the target constraint conditions, the following steps are performed:

[0082] When the target constraint is a point constraint, the actual benefit value of the SQL statement is calculated using the following formula:

[0083]

[0084] Among them, c′ is the performance, c is the point constraint, and R is the actual benefit value of the SQL statement.

[0085] When the target constraint is a range constraint, the actual benefit value of the SQL statement is calculated using the following formula:

[0086]

[0087] Among them, c′ is the performance, [c l , c r ] is the range constraint and R is the actual return value of the SQL statement.

[0088] Then, after calculating the actual benefit value of the entire SQL statement, the actual benefit value of each word in the statement is set equal to the actual benefit value of the SQL statement, that is, the benefit corresponding to each word in the SQL statement is recorded as r=R.

[0089] S104: Calculate the estimated benefit value of the next word after the initial word in the SQL statement through the word selection benefit estimation model and the constraint coding network, and update the parameters of each constructed network model according to the difference between the actual benefit value and the estimated benefit value.

[0090] Among them, each network model constructed is a word selection probability model, a word selection benefit estimation model and a constraint encoding network.

[0091] When specifically calculating the estimated benefit value of the next word, as a possible implementation method, first, the encoding of each word in the SQL statement is sequentially input into the second embedding layer to obtain the embedding vector corresponding to each encoding. The second embedding layer is a single-layer neural network, the input layer size of the embedding layer is |A|, and the output layer size is 256. The word encoding corresponding to the SQL sequence generated in step S103 is sequentially input into the embedding layer of the word selection benefit estimation model to obtain the embedding vector corresponding to the word encoding.

[0092] Then, a triple is generated according to the hidden state vector, code and actual benefit value of the next word, and a preset number of time sequences are reversed from the order of the hidden state vector of the next word, and the triple sequence within the preset number of time sequences is input into the constraint coding network to obtain the constraint code. In this embodiment, the state vector s generated in the embodiment of step S103, the code a of the next selected word and the benefit r of selecting word a are formed into a triple (s, r, k), and k time sequences are reversed from the order of s, that is, (s, r, k) t-k to (s, r, k) t , and then input the triple sequence in the time series into the constraint coding network to obtain the constraint coding denoted as z t .

[0093] Finally, the embedding vector corresponding to each code is concatenated with the constraint code and input into the second recurrent neural network to obtain the estimated benefit value of the next word. t They are concatenated and input into the recurrent neural network to obtain the estimated benefit of selecting word a under the obtained target constraints and state s.

[0094] Furthermore, the parameters of the word selection probability model, the word selection benefit estimation model and the constraint encoding network are updated according to the difference between the actual benefit value and the benefit estimation value.

[0095] As one possible implementation method, the difference error between the actual revenue value and the revenue estimate value can be calculated by the following formula:

[0096] A t =|r t +v t+1 -v t |

[0097] Among them, r t is the actual revenue value of the current time series, v t is the estimated value of the current time series, v t+1 is the estimated value of the profit of the next time series. Then, the parameters of each constructed network model are updated through the control strategy gradient descent to minimize the differential error.

[0098] S105: Repeat steps S103 to S104 until each constructed network model converges, and generate a target SQL statement through the trained word selection probability model.

[0099] Specifically, the word selection probability model, the word selection benefit estimation model and the constraint coding network are trained by repeatedly executing step S103 to step S104 until the recurrent networks in the word selection probability model and the word selection benefit estimation model, and the constraint coding network converge.

[0100] In order to more clearly illustrate the complete process of model training in the learning-based SQL automatic generation method of the present application, a specific embodiment is used for illustration below.

[0101] In this embodiment, the first word is specified, and the corresponding code is input into the embedding layer and finite state automaton of the word selection probability model to obtain the embedding vector corresponding to the word code and the set of legal candidate words. The embedding vector output by the embedding layer of the word selection probability model is input into the recurrent neural network of the word selection probability model to obtain the state hidden vector s, and then the state hidden vector s is input into the softmax layer of the word selection probability model to obtain the probability distribution of the candidate words. Then, according to the probability distribution output by the softmax layer of the word selection probability model, the next word a is selected from the legal candidate words, and the next word code is used as input to repeat the above steps until the word end is selected to obtain a complete SQL, recorded as Q. Then Q is input into the database to obtain the performance of Q, recorded as c′, and the actual benefit is calculated based on c′ and the user input constraint. If the constraint input by the user is a point constraint, recorded as c, then the benefit is recorded as If the constraint entered by the user is a range constraint, it is recorded as [c l , c r ], then the income is recorded as Then record the corresponding benefit of each word in Q as r=R.

[0102] Furthermore, the word encoding of the SQL query statement is used as the input of the embedding layer of the word selection benefit estimation model to obtain an embedding vector, and the state vector s, the next selected word encoding a, and the benefit r of selecting word a are formed into a triple, starting from the order of s and working backwards for k time series, and the triple sequence in the time series is input into the constraint coding network to obtain the constraint coding. Then, the constraint coding and the embedding vector are concatenated and input into the recurrent neural network of the word selection benefit estimation model to obtain the benefit estimation value of selecting word a under the constraint condition c and state s, which is recorded as v.

[0103] Furthermore, the differential error A is calculated t =|r t +v t+1 -v t |, where t is the current time sequence and t+1 is the next time sequence. Then, the query error is used to update the parameters of the word selection probability model and the word selection benefit estimation model. The word selection model is updated by policy gradient descent, and the differential error is minimized by updating the word selection benefit estimation model.

[0104] Finally, the entire process described above in this embodiment is repeated, and when the recurrent network converges, the parameters of the trained recurrent neural network can be obtained.

[0105] Therefore, in the learning-based SQL automatic generation method of the present application, during the training process, each time a SQL statement is generated by the word selection probability model, it is a forward propagation of the recurrent neural network, and then the model is updated by performing policy gradient descent. In the embodiment of the present application, the same word can be specified each time, that is, the initial word can be the same, and each time a complete SQL statement is generated, the statement is entered into the database to obtain a reward R, and then the word selection benefit estimation model and the constraint coding network are used to assist in training. The purpose of the training is to enable the word selection probability model to generate a SQL statement that obtains a reward R.

[0106] Furthermore, the trained word selection probability model is used to generate a target SQL statement according to the current needs of the user. For example, according to the target constraints and initial words set by the current user, a corresponding target SQL statement is generated and returned to the user end.

[0107] Among them, the way in which the trained word selection probability model generates the target SQL statement corresponds to the way in which the SQL statement is generated during the training process, and the corresponding number of SQL statements can be generated according to user needs. For example, obtain the specified first word, input the corresponding code into the embedding layer of the word selection probability model, obtain the embedding vector corresponding to the word code, and then input the embedding vector into the recurrent neural network of the word selection probability model to obtain the hidden vector of the next word to be selected, and input the hidden vector into the softmax layer of the word selection probability model to obtain the probability distribution of the word to be selected. Then input the word into the finite state automaton to obtain a set of legal words to be selected, select the next word from the legal words to be selected according to the probability distribution output by the softmax layer of the word selection probability model, and repeat the above steps with the next word code as input until the word terminator is selected to obtain a complete SQL. Further, by repeating the above steps, new SQL statements are continuously generated until the number of SQL statements that meet user needs is reached, and all generated SQL statements are returned to the user.

[0108] Therefore, this application reduces the SQL statement generation problem to the problem of selecting each word in the sequence, selects each word in the SQL statement in order based on the word selection model after reinforcement learning, and designs a reward function to accurately guide the generation direction of the statement. The legality of the generated SQL statement is guaranteed by designing a finite state automaton, and the task scenarios applicable to this method are generalized through a meta-learning criticism network, so that SQL statements with different constraints that meet user requirements can be quickly generated.

[0109] In summary, the learning-based SQL automatic generation method of the embodiment of the present application adopts the strategy of exploration and utilization in reinforcement learning RL, learns the SQL generation direction through the explored SQL statement execution results, and uses the learned results to generate SQL statements that meet the constraints. The method designs the reward function in RL according to the actual application scenario to accurately guide the generation process, and ensures the legal query generation space through an integrated finite state machine. In addition, the meta-learning network is used to accelerate model training, so that the method can be effectively extended to other SQL generation tasks with different constraints, improving the efficiency and applicability of the SQL statement automatic generation method. Thus, the method can automatically generate SQL statements that meet the current constraints, improve the accuracy and efficiency of generating SQL statements, and reduce costs.

[0110] In order to more clearly illustrate the learning-based automatic SQL statement generation method of the present application, the specific process of model design and statement generation in actual application is described below in combination with Figures 3 to 7 , described in detail with a specific embodiment.

[0111] In this embodiment, first build Figure 3 The overall framework of the system is shown in Figure 3 The interactive process of each component in the system shown in is trained, wherein the user input component is used to obtain the information input by the user, the intelligent agent component includes the word selection probability model, the word selection benefit estimation model and the constraint coding network in the above embodiment, and the finite state automaton is used as the environment for generating sentences. During the training process, the generated Figure 6 The finite state automaton shown in Figure 1 can be Figure 7 After the training of each model in the system is completed, you can Figure 4 and Figure 5 The process shown generates SQL statements, wherein the action performed is to select the next word from the legal candidate words, and the state is to generate a hidden state vector. The specific implementation process of each step can refer to the description in the above embodiment, which will not be repeated here. The process of automatically generating SQL statements based on learning in this embodiment is described in detail below.

[0112] It should be noted that the following method is applicable to the scenario where the word selection probability model (actor) and the word selection benefit estimation model (critic) have been built and trained in advance. In this method, generating an SQL statement includes the following steps:

[0113] Step S1: Get Figure 3The database shown includes relational tables: Score (T1) and Student (T2), where the data includes tuples (1, Math, 95.9) and (1, English, 100) of T1, and tuples (1, Jack) and (2, Mary) of T2. Figure 3 A user-defined goal constraint is shown with a cardinality range of [1K,2K].

[0114] Step S2: construct a vocabulary and a finite state automaton, wherein the finite state machine is a component of the environment.

[0115] Step S3: Specify the initial word "From", and represent the SQL statement to be synthesized by Q, where Q = From. Input "From" to the agent, and obtain the probability distribution of all words in the vocabulary. Figure 6 As shown in the figure, Q enters From from the start state in the finite state machine and reaches node n1, returning the words Score and Student on the edge connected to n1. The agent selects Score, which represents the table word, from the words Score and Student according to probability. Among them, under the parameters trained by the model, selecting Score in the current state is more likely to generate a SQL statement that meets the user constraints.

[0116] Step S4: Update Q in step S3 to Q = From Score, input Score into the agent to obtain the next word probability distribution, and input Score into the finite state automaton to reach the n2 node, such as Figure 6 As shown, the legal candidate words select and student are obtained, and the word select is selected from the legal candidate words according to probability. Among them, under the trained parameters, selecting select in the current state is more likely to generate a SQL statement that meets the user constraints.

[0117] Step S5: Update Q in step S4 to Q = From Score Select, input select into the agent to obtain the next word probability distribution, and input select into the finite state automaton to reach Figure 6 The n5 node shown obtains the legal candidate words T1.score, etc., and selects the word ID from the legal candidate words according to probability. Under the trained parameters, selecting the ID in the current state is more likely to finally generate an SQL statement that meets the user constraints.

[0118] Step S6: Update Q in step S5 to Q=From Score SelectID, input ID into the agent to obtain the next word probability distribution, and input ID into the finite state automaton to obtain the legal candidate words Where, etc., and select the word Where from the legal candidate words according to the probability.

[0119] Step S7: Update Q in step S6 to Q = From Score Select ID Where, input Where into the agent to obtain the next word probability distribution, and input Where into the finite state automaton to obtain the legal candidate words Score, Course, ID, etc., and select the word Score from the legal candidate words according to the probability.

[0120] Step S8: Update Q in step S7 to Q=From Score SelectIDWhereScore, input Score into the agent to obtain the next word probability distribution, and input Score into the finite state automaton to obtain legal candidate words "<", ">" and "=", etc., and select the word "<" from the legal candidate words according to probability.

[0121] Step S9: Update Q in step S8 to Q = From Score SelectIDWhereScore<, input "<" into the agent to obtain the next word probability distribution; input "<" into the finite state automaton to obtain legal candidate words 95, 100, etc., and select word 95 from the legal candidate words according to probability.

[0122] Step S10: Update Q in step S9 to Q=From Score SelectIDWhereScore<95, input 95 into the agent to obtain the next word probability distribution; input 95 into the finite state automaton to obtain legal candidate words and, or and EOF (end terminator), etc., and select the word EOF from the legal candidate words according to the probability.

[0123] Step S11: Generate a complete SQL statement, end the statement generation process, adjust the Q sequence, and return the generated SQL statement to the user.

[0124] In order to implement the above embodiment, the present application also proposes a learning-based SQL automatic generation system. Figure 8 A structural diagram of a learning-based SQL automatic generation system proposed in an embodiment of the present application is shown in FIG. Figure 8 As shown, the system includes an acquisition module 100 , a construction module 200 , a first generation module 300 , an update module 400 and a second generation module 500 .

[0125] The acquisition module 100 is used to acquire the database to be queried and the preset target constraint conditions, and to construct a vocabulary required to generate a structured query language SQL statement according to the information of the database, and to encode each word in the vocabulary.

[0126] The construction module 200 is used to generate a finite state automaton according to the SQL grammar rules, and to construct a word selection probability model, a word selection benefit estimation model and a constraint coding network, and to obtain the coding of the specified initial word in the vocabulary.

[0127] The first generation module 300 is used to input the encoding of the initial word into the word selection probability model, obtain the SQL statement output by the word selection probability model based on the encoding of the initial word, and calculate the actual benefit value of each word in the SQL statement according to the performance of the SQL statement in the database and the target constraint conditions.

[0128] The updating module 400 is used to calculate the estimated benefit value of the next word after the initial word in the SQL statement through the word selection benefit estimation model and the constraint coding network, and update the parameters of each constructed network model according to the difference between the actual benefit value and the estimated benefit value.

[0129] The second generation module 500 is used to control the first generation module and the update module to repeatedly execute their own functions until each constructed network model converges, and generates a target SQL statement through the trained word selection probability model.

[0130] Optionally, in one embodiment of the present application, the word selection probability model includes a first embedding layer, a first recurrent neural network and a Softmax layer, and the first generation module 300 is specifically used to: input the encoding of the initial word into the first embedding layer to generate an embedding vector corresponding to the encoding of the initial word; input the embedding vector into the first recurrent neural network to generate a hidden state vector of the next word; input the initial word into a finite state automaton to obtain legal candidate words for the next word from all words in the vocabulary; input the hidden state vector into the Softmax layer to generate a selection probability distribution for the next word; select the next word from the legal candidate words according to the selection probability distribution; input the encoding of the next word into the first embedding layer to generate a corresponding embedding vector, and repeat the step of generating the next word until the terminator word is selected to generate a complete SQL statement.

[0131] Optionally, in one embodiment of the present application, the word selection profit estimation model includes a second embedding layer and a second recurrent neural network, and the update module 400 is specifically used to: input the encoding of each word in the SQL statement into the second embedding layer in sequence, and obtain the embedding vector corresponding to each encoding; generate a triple according to the hidden state vector, encoding and actual profit value of the next word, and reverse the preset number of time series with the order of the hidden state vector of the next word as the starting point, and input the triple sequence within the preset number of time series into the constraint coding network to obtain the constraint coding; after splicing the embedding vector corresponding to each coding with the constraint coding, input it into the second recurrent neural network to obtain the profit estimation value of the next word.

[0132] Optionally, in one embodiment of the present application, the first generating module 300 is specifically used to: when the target constraint condition is a point constraint, calculate the actual benefit value of the SQL statement by the following formula:

[0133]

[0134] Among them, c′ is the performance, c is the point constraint, and R is the actual benefit value of the SQL statement;

[0135] When the target constraint is a range constraint, the actual benefit value of the SQL statement is calculated using the following formula:

[0136]

[0137] Among them, c′ is the performance, [c l , c r ] is the range constraint, and R is the actual return value of the SQL statement;

[0138] Set the actual return value of each word equal to the actual return value of the SQL statement.

[0139] Optionally, in one embodiment of the present application, the updating module 400 is further configured to: calculate the differential error between the actual revenue value and the estimated revenue value by using the following formula:

[0140] A t =|r t +v t+1 -v t |

[0141] Among them, r t is the actual revenue value of the current time series, v t is the estimated value of the current time series, v t+1 is the estimated value of the revenue in the next time series;

[0142] The parameters of each constructed network model are updated by controlling the policy gradient descent so that the differential error reaches the minimum value.

[0143] Optionally, in one embodiment of the present application, the construction module 200 is specifically used to: extract the primary and foreign key relationships of the relational tables in the database, and collect the data in the relational tables; construct the nodes and edges of the finite state automaton according to the primary and foreign key relationships, the data in the relational tables and the SQL grammar rules, wherein the nodes correspond to the states of the SQL statements, and each node is connected to the next word that can be spliced ​​through the corresponding edge.

[0144] In summary, the learning-based SQL automatic generation system of the embodiment of the present application adopts the strategy of exploration and utilization in reinforcement learning RL, learns the SQL generation direction through the explored SQL statement execution results, and uses the learned results to generate SQL statements that meet the constraints. The system designs the reward function in RL according to the actual application scenario to accurately guide the generation process, and ensures the legal query generation space through an integrated finite state machine. In addition, the meta-learning network is used to accelerate model training, so that the system can be effectively extended to other SQL generation tasks with different constraints, enhancing the statement generation efficiency and generality of the system. Thus, the system can automatically generate SQL statements that meet the current constraints, improve the accuracy and efficiency of generating SQL statements, and reduce costs.

[0145] In order to implement the above embodiments, the present application further proposes a non-transitory computer-readable storage medium storing computer instructions, wherein the computer instructions are used to enable the computer to execute any one of the learning-based SQL automatic generation methods described in the above embodiments.

[0146] In the description of this specification, the description with reference to the terms "one embodiment", "some embodiments", "example", "specific example", or "some examples" etc. means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present application. In this specification, the schematic representations of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner. In addition, those skilled in the art may combine and combine the different embodiments or examples described in this specification and the features of the different embodiments or examples, without contradiction.

[0147] In addition, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined as "first" and "second" may explicitly or implicitly include at least one of the features. In the description of this application, the meaning of "plurality" is at least two, such as two, three, etc., unless otherwise clearly and specifically defined.

[0148] Any process or method description in a flowchart or otherwise described herein may be understood to represent a module, fragment or portion of code comprising one or more executable instructions for implementing the steps of a custom logical function or process, and the scope of the preferred embodiments of the present application includes alternative implementations in which functions may not be performed in the order shown or discussed, including performing functions in a substantially simultaneous manner or in the reverse order depending on the functions involved, which should be understood by technicians in the technical field to which the embodiments of the present application belong.

[0149] The logic and / or steps represented in the flowchart or otherwise described herein, for example, can be considered as an ordered list of executable instructions for implementing logical functions, and can be embodied in any computer-readable medium for use by an instruction execution system, device or apparatus (such as a computer-based system, a system including a processor, or other system that can fetch instructions from an instruction execution system, device or apparatus and execute the instructions), or in combination with these instruction execution systems, devices or apparatuses. For the purpose of this specification, "computer-readable medium" can be any device that can contain, store, communicate, propagate or transmit a program for use by an instruction execution system, device or apparatus, or in combination with these instruction execution systems, devices or apparatuses. More specific examples of computer-readable media (a non-exhaustive list) include the following: an electrical connection with one or more wires (electronic device), a portable computer disk box (magnetic device), a random access memory (RAM), a read-only memory (ROM), an erasable and programmable read-only memory (EPROM or flash memory), a fiber optic device, and a portable compact disk read-only memory (CDROM). In addition, the computer-readable medium may even be paper or other suitable medium on which the program is printed, since the program may be obtained electronically, for example, by optically scanning the paper or other medium and then editing, interpreting or processing in other suitable ways if necessary, and then stored in a computer memory.

[0150] It should be understood that the various parts of the present application can be implemented by hardware, software, firmware or a combination thereof. In the above-mentioned embodiments, multiple steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented by hardware, as in another embodiment, it can be implemented by any one of the following technologies known in the art or their combination: a discrete logic circuit having a logic gate circuit for implementing a logic function for a data signal, a dedicated integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.

[0151] A person skilled in the art may understand that all or part of the steps in the method for implementing the above-mentioned embodiment may be completed by instructing related hardware through a program, and the program may be stored in a computer-readable storage medium, which, when executed, includes one or a combination of the steps of the method embodiment.

[0152] In addition, each functional unit in each embodiment of the present application may be integrated into a processing module, or each unit may exist physically separately, or two or more units may be integrated into one module. The above-mentioned integrated module may be implemented in the form of hardware or in the form of a software functional module. If the integrated module is implemented in the form of a software functional module and sold or used as an independent product, it may also be stored in a computer-readable storage medium.

[0153] The storage medium mentioned above may be a read-only memory, a magnetic disk or an optical disk, etc. Although the embodiments of the present application have been shown and described above, it can be understood that the above embodiments are exemplary and cannot be understood as limiting the present application. A person of ordinary skill in the art may change, modify, replace and modify the above embodiments within the scope of the present application.

Claims

1. A learning-based SQL automatic generation method, characterized in that: The following steps are involved: S101: Obtain a database to be queried and preset target constraints, and construct a vocabulary required to generate a structured query language SQL statement based on the information of the database, and encode each word in the vocabulary; S102: Generate a finite state automaton according to SQL grammar rules, and construct a word selection probability model, a word selection benefit estimation model and a constraint coding network, and obtain the coding of the specified initial word in the vocabulary; S103: inputting the code of the initial word into the word selection probability model, obtaining the SQL statement output by the word selection probability model based on the code of the initial word, and calculating the actual benefit value of each word in the SQL statement according to the performance of the SQL statement in the database and the target constraint condition; S104: Calculate the estimated benefit value of the next word after the initial word in the SQL statement through the word selection benefit estimation model and the constraint coding network, and update the parameters of each constructed network model according to the difference between the actual benefit value and the estimated benefit value; S105: Repeat steps S103 to S104 until each of the constructed network models converges, and generate a target SQL statement through the trained word selection probability model.

2. The method according to claim 1, characterized in that The word selection probability model includes a first embedding layer, a first recurrent neural network and a Softmax layer, and the step of obtaining an SQL statement outputted by the word selection probability model based on the encoding of the initial word includes: S10: Inputting the encoding of the initial word into the first embedding layer to generate an embedding vector corresponding to the encoding of the initial word; S20: Input the embedding vector into the first recurrent neural network to generate a hidden state vector of the next word; S30: inputting the initial word into the finite state automaton, and obtaining a legal candidate word for the next word from all the words in the vocabulary; S40: Input the hidden state vector to the Softmax layer to generate a selection probability distribution of the next word; S50: selecting the next word from the legal words to be selected according to the selection probability distribution; S60: Input the encoding of the next word into the first embedding layer to generate a corresponding embedding vector, and repeat steps S20 to S50 until the terminator word is selected to generate the complete SQL statement.

3. The method according to claim 2, characterized in that The word selection benefit estimation model includes a second embedding layer and a second recurrent neural network, and the benefit estimation value of the next word after the initial word in the SQL statement is calculated by the word selection benefit estimation model and the constraint coding network, including: Inputting the encoding of each word in the SQL statement into the second embedding layer in sequence, and obtaining an embedding vector corresponding to each encoding; Generate a triplet according to the hidden state vector, code and actual benefit value of the next word, and reverse a preset number of time sequences from the order of the hidden state vector of the next word as the starting point, input the triplet sequence within the preset number of time sequences into the constraint coding network, and obtain the constraint coding; After concatenating the embedding vector corresponding to each code with the constraint code, the concatenated vector is input into the second recurrent neural network to obtain the estimated benefit value of the next word.

4. The method according to claim 1, characterized in that The step of calculating the actual revenue value of each word in the SQL statement according to the performance of the SQL statement in the database and the target constraint condition comprises: When the target constraint is a point constraint, the actual benefit value of the SQL statement is calculated by the following formula: Among them, c′ is the performance, c is the point constraint, and R is the actual benefit value of the SQL statement; When the target constraint is a range constraint, the actual benefit value of the SQL statement is calculated by the following formula: Among them, c′ is the performance, [c l , c r ] is the range constraint, and R is the actual return value of the SQL statement; The actual benefit value of each word is set equal to the actual benefit value of the SQL statement.

5. The method according to claim 1, characterized in that The parameter of each network model constructed according to the difference between the actual benefit value and the estimated benefit value is updated, including: The difference error between the actual profit value and the profit estimate value is calculated by the following formula: A t =|r t +v t+1 -v t | Among them, r t is the actual revenue value of the current time series, v t is the estimated value of the current time series, v t+1 is the estimated value of the revenue in the next time series; The parameters of each constructed network model are updated by controlling the gradient descent of the strategy so that the differential error reaches the minimum value.

6. The method according to any one of claims 1 to 5, characterized in that: The generating of a finite state automaton according to SQL grammar rules comprises: Extracting the primary and foreign key relationships of the relational tables in the database, and collecting the data in the relational tables; According to the primary and foreign key relationships, the data in the relationship table and the SQL grammar rules, the nodes and edges of the finite state automaton are constructed, wherein the nodes correspond to the states of the SQL statements, and each of the nodes is connected to the next word that can be spliced ​​through a corresponding edge.

7. A learning-based SQL automatic generation system, characterized in that: include: The acquisition module is used to obtain the database to be queried and the preset target constraints, and to construct the vocabulary required to generate the structured query language SQL statement based on the information of the database, and to encode each word in the vocabulary; A construction module is used to generate a finite state automaton according to SQL grammar rules, and to construct a word selection probability model, a word selection benefit estimation model and a constraint coding network, and to obtain the coding of a specified initial word in the vocabulary; A first generating module is used to input the code of the initial word into the word selection probability model, obtain the SQL statement output by the word selection probability model based on the code of the initial word, and calculate the actual benefit value of each word in the SQL statement according to the performance of the SQL statement in the database and the target constraint condition; An updating module, used to calculate the estimated benefit value of the next word after the initial word in the SQL statement through the word selection benefit estimation model and the constraint coding network, and update the parameters of each constructed network model according to the difference between the actual benefit value and the estimated benefit value; The second generation module is used to control the first generation module and the update module to repeatedly execute their own functions until each of the constructed network models converges, and generate a target SQL statement through the trained word selection probability model.

8. The system according to claim 7, characterized in that The word selection probability model includes a first embedding layer, a first recurrent neural network and a Softmax layer, and the first generation module is specifically used for: Inputting the encoding of the initial word into the first embedding layer to generate an embedding vector corresponding to the encoding of the initial word; Inputting the embedding vector into the first recurrent neural network to generate a hidden state vector of the next word; Inputting the initial word into the finite state automaton, and obtaining legal words to be selected for the next word from all the words in the vocabulary; Input the hidden state vector into the Softmax layer to generate a probability distribution of the next word selection; Selecting the next word from the legal words to be selected according to the selection probability distribution; The encoding of the next word is input into the first embedding layer to generate a corresponding embedding vector, and the step of generating the next word is repeated until a terminator word is selected to generate the complete SQL statement.

9. The system according to claim 8, characterized in that The word selection benefit estimation model includes a second embedding layer and a second recurrent neural network, and the update module is specifically used to: Inputting the encoding of each word in the SQL statement into the second embedding layer in sequence, and obtaining an embedding vector corresponding to each encoding; Generate a triplet according to the hidden state vector, code and actual benefit value of the next word, and reverse a preset number of time sequences from the order of the hidden state vector of the next word as the starting point, input the triplet sequence within the preset number of time sequences into the constraint coding network, and obtain the constraint coding; After concatenating the embedding vector corresponding to each code with the constraint code, the concatenated vector is input into the second recurrent neural network to obtain the estimated benefit value of the next word.

10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the learning-based SQL automatic generation method as described in any one of claims 1 to 6 is implemented.

Citation Information

Patent Citations

  • Neural network based translation of natural language queries to database queries

    CN110945495A

  • Method for constructing SQL statement based on actor-critic network

    US20200301924A1

Cited By

  • Generating structured query language using machine learning

    US12511282B1