SQL statement generation method, device, equipment and storage medium
By inputting the natural language query problems, database schema and content input by users into the preset SLESQL model, SQL statements are generated, and the problem of poor generation of SQL statements in complex problem environments is solved, and efficient database content is achieved Utilization and cost reduction.
Patent Information
- Application Number
- CN202210344221.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-02
- Publication Date
- 2025-02-07
- Estimated Expiration
- 2042-04-02
AI Technical Summary
The prior art is difficult to accurately generate SQL statements in complex problem environments, resulting in increased cost of data analysis tools.
By obtaining the natural language query problems, database schema and database content input by the user, input them into the preset SLESQL model, and using the preset entity recognition model, encoding model and decoding model to generate SQL statements.
It improves the utilization effect of database content, can generate complex and nested SQL statements, and reduces the cost of using data analysis tools.
Smart Images

Figure CN114780577B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing technology, and in particular to a method, device, equipment and storage medium for generating SQL statements. Background Art
[0002] In recent years, with the rapid development of mobile Internet and artificial intelligence-related industries, data assets have become an indispensable part of corporate and personal life, and technologies related to data mining and analysis have also become the focus of attention in the era of big data. However, the use of mainstream data analysis software today still requires professional database operation commands (such as SQL statements), which undoubtedly increases the cost of using data analysis tools. Automatic Text2SQL technology has emerged. As a natural language processing technology, its goal is to parse user natural language input into corresponding SQL query statements, giving data analysis tools artificial intelligence technology and natural language understanding capabilities. However, due to the complexity of user intent and the diversity of databases in real scenarios, Text2SQL systems that can be provided in the current market are still scarce, and often do not have good results in cross-domain environments with complex problems. Therefore, how to accurately generate SQL statements has become an urgent problem to be solved.
[0003] The above contents are only used to assist in understanding the technical solution of the present invention and do not constitute an admission that the above contents are prior art. Summary of the invention
[0004] The main purpose of the present invention is to provide a method, device, equipment and storage medium for generating SQL statements, aiming to solve the technical problem of how to accurately generate SQL statements.
[0005] To achieve the above object, the present invention provides a method for generating a SQL statement, the method comprising the following steps:
[0006] Obtain natural language query questions, database schema, and database content input by users;
[0007] The natural language query question, the database schema, and the database content are input into a preset SLESQL model to generate an SQL statement.
[0008] Optionally, the preset SLESQL model includes: a preset entity recognition model, a preset encoding model and a preset decoding model;
[0009] The step of inputting the natural language query question, the database schema, and the database content into a preset SLESQL model to generate an SQL statement specifically includes:
[0010] Inputting the natural language query question, the database schema, and the database content into the preset entity recognition model to obtain a candidate value set;
[0011] Inputting the candidate value set into the preset coding model to obtain an encoded candidate value set;
[0012] The encoded candidate value set is input into the preset decoding model to generate an SQL statement.
[0013] Optionally, the step of inputting the natural language query question, the database schema, and the database content into the preset entity recognition model to obtain a candidate value set specifically includes:
[0014] Inputting the natural language query question into the preset entity recognition model to obtain a set of candidate values;
[0015] determining a candidate value based on the candidate value set, the database content, and the database schema;
[0016] A set of candidate values is determined based on the candidate values and the database schema.
[0017] Optionally, the step of determining the candidate value according to the candidate value set, the database content and the database schema specifically includes:
[0018] Determine a target candidate value set according to the candidate value set, the database content, and the database schema;
[0019] The target candidate value set is verified according to the number of candidate values in the target candidate value set, the number of numerical values in the database content, and a preset similarity threshold to obtain candidate values.
[0020] Optionally, the step of determining a target candidate value set according to the candidate value set, the database content and the database schema specifically includes:
[0021] Obtaining a word embedding text distance between each candidate value in the candidate value set and the database content;
[0022] When the word embedding text distance meets a preset text distance condition, obtaining an initial candidate value set;
[0023] Obtaining an edit distance between each initial candidate value in the initial candidate value set and the database pattern;
[0024] When the edit distance satisfies a preset edit distance condition, a target candidate value set is obtained.
[0025] Optionally, the step of inputting the candidate value set into the preset coding model to obtain the encoded candidate value set specifically includes:
[0026] Obtaining a matching type corresponding to the candidate value set;
[0027] Marking the candidate value set according to the matching type to obtain a marked candidate value set;
[0028] The marked candidate value set is input into the preset coding model to obtain a coded candidate value set.
[0029] Optionally, the preset decoding model includes: a skeleton decoder and a detail decoder;
[0030] The step of inputting the encoded candidate value set into the preset decoding model to generate an SQL statement specifically includes:
[0031] Inputting the encoded candidate value set into the skeleton decoder to obtain a sentence skeleton;
[0032] The statement skeleton and the encoded candidate value set are input into the detail decoder to obtain the SQL statement.
[0033] In addition, to achieve the above object, the present invention also provides a SQL statement generating device, the SQL statement generating device comprising:
[0034] An information acquisition module is used to obtain natural language query questions, database schemas, and database contents input by users;
[0035] The SQL statement generation module is used to input the natural language query question, the database model and the database content into a preset SLESQL model to generate an SQL statement.
[0036] In addition, to achieve the above-mentioned purpose, the present invention also proposes a SQL statement generation device, which includes: a memory, a processor, and a SQL statement generation program stored in the memory and executable on the processor, wherein the SQL statement generation program is configured to implement the steps of the SQL statement generation method described above.
[0037] In addition, to achieve the above object, the present invention further proposes a storage medium, on which a SQL statement generation program is stored, and when the SQL statement generation program is executed by a processor, the steps of the SQL statement generation method described above are implemented.
[0038] The present invention obtains natural language query questions, database schema and database content input by the user, and then inputs the natural language query questions, database schema and database content into a preset SLESQL model to generate SQL statements. The present invention generates SQL statements by inputting natural language query questions, database schema and database content into a preset SLESQL model. Compared with the existing method of converting the user's natural language input into executable SQL statements through Text2SQL, which has poor effect in complex problem environments, the above method of the present invention can improve the utilization effect of database content and generate complex and nested SQL statements through a preset SLESQL model. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Figure 1 It is a structural schematic diagram of a SQL statement generating device in a hardware operating environment involved in an embodiment of the present invention;
[0040] Figure 2 This is a flow chart of a first embodiment of a method for generating a SQL statement according to the present invention;
[0041] Figure 3 This is a flow chart of a second embodiment of a method for generating SQL statements according to the present invention;
[0042] Figure 4 This is a structural block diagram of the first embodiment of the SQL statement generating device of the present invention.
[0043] The realization of the purpose, functional features and advantages of the present invention will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0044] It should be understood that the specific embodiments described herein are only used to explain the present invention, and are not used to limit the present invention.
[0045] Reference Figure 1 , Figure 1 The present invention is a schematic diagram of a SQL statement generation device structure for a hardware operating environment involved in an embodiment of the present invention.
[0046] like Figure 1As shown, the SQL statement generation device may include: a processor 1001, such as a central processing unit (CPU), a communication bus 1002, a user interface 1003, a network interface 1004, and a memory 1005. Among them, the communication bus 1002 is used to realize the connection and communication between these components. The user interface 1003 may include a display screen (Display), an input unit such as a keyboard (Keyboard), and the optional user interface 1003 may also include a standard wired interface and a wireless interface. The network interface 1004 may optionally include a standard wired interface and a wireless interface (such as a wireless fidelity (Wireless-Fidelity, Wi-Fi) interface). The memory 1005 may be a high-speed random access memory (Random Access Memory, RAM), or a stable non-volatile memory (Non-Volatile Memory, NVM), such as a disk storage. The memory 1005 may also be a storage device independent of the aforementioned processor 1001.
[0047] Those skilled in the art will understand that Figure 1 The structure shown in the figure does not constitute a limitation on the SQL statement generating device, and may include more or less components than those shown in the figure, or combine certain components, or arrange the components differently.
[0048] like Figure 1 As shown, the memory 1005 as a storage medium may include an operating system, a network communication module, a user interface module, and an SQL statement generation program.
[0049] exist Figure 1 In the SQL statement generating device shown, the network interface 1004 is mainly used for data communication with the network server; the user interface 1003 is mainly used for data interaction with the user; the processor 1001 and the memory 1005 in the SQL statement generating device of the present invention can be set in the SQL statement generating device, and the SQL statement generating device calls the SQL statement generating program stored in the memory 1005 through the processor 1001, and executes the SQL statement generating method provided by the embodiment of the present invention.
[0050] Based on the above-mentioned SQL statement generation device, an embodiment of the present invention provides a SQL statement generation method, referring to Figure 2 , Figure 2 This is a flow chart of the first embodiment of the SQL statement generation method of the present invention.
[0051] In this embodiment, the SQL statement generation method includes the following steps:
[0052] Step S10: Obtaining the natural language query question, database model and database content input by the user;
[0053] It should be noted that the execution subject of this embodiment can be a computing service device with data processing, network communication and program running functions, such as a mobile phone, a tablet computer, a personal computer, etc., or an electronic device or SQL statement generation device that can realize the above functions. The following takes the SQL statement generation device as an example to illustrate this embodiment and the following embodiments.
[0054] It is understandable that a natural language query question refers to a query question input by a user, for example: "Show origin and destination for flights with price higher than 300." The natural language query question in this embodiment is composed of English.
[0055] It should be understood that a database schema may include rows and columns of a database, and database content refers to values in the database.
[0056] Step S20: input the natural language query question, the database schema and the database content into a preset SLESQL model to generate an SQL statement.
[0057] It should be noted that the preset SLESQL model refers to a pre-set SLESQL model, which can generate SQL statements.
[0058] This embodiment obtains the natural language query question, database schema and database content input by the user, and then inputs the natural language query question, database schema and database content into the preset SLESQL model to generate SQL statements. The present invention generates SQL statements by inputting the natural language query question, database schema and database content into the preset SLESQL model. Compared with the existing method of converting the user's natural language input into executable SQL statements through Text2SQL, which is not effective in complex problem environments, the above method of the present invention can improve the utilization effect of the database content and generate complex and nested SQL statements through the preset SLESQL model.
[0059] refer to Figure 3 , Figure 3 This is a flow chart of the second embodiment of the SQL statement generation method of the present invention.
[0060] Based on the above first embodiment, in this embodiment, step S20 includes:
[0061] Step S201: inputting the natural language query question, the database schema, and the database content into the preset entity recognition model to obtain a candidate value set;
[0062] It should be noted that the preset SLESQL model in this embodiment may include a preset entity recognition model, a preset encoding model, and a preset decoding model. The preset entity recognition model refers to a preset model for entity recognition, the preset encoding model refers to a preset model for encoding, and the preset decoding model refers to a preset model for decoding.
[0063] It can be understood that the candidate value set may include candidate values related to the natural language query question, and rows and columns corresponding to the candidate values.
[0064] Furthermore, in order to accurately determine the candidate value set, in this embodiment, the step S201 includes: inputting the natural language query question into the preset entity recognition model to obtain a candidate value set; determining the candidate value based on the candidate value set, the database content and the database model; determining the candidate value set based on the candidate value and the database model.
[0065] It is understandable that the preset entity recognition model in this embodiment can be a TENER model, which is an improvement on the original Transformer for the NER task, and uses an improved Transformer encoder to model character-level features and word-level features.
[0066] It should be understood that the present embodiment also uses some simple and effective heuristic methods to extract candidate values, 1) content in quotation marks: for example, whose first name has the substring 'Ha'?; 2) capitalized terms: for example, showing all flight numbers with AirbusA340-300 aircraft; 3) single letters: for example, when were the employees whose names do not contain the letter M hired?
[0067] In a specific implementation, a natural language query problem is input into a preset entity recognition model to obtain a candidate value set, which includes multiple candidate values. The candidate value refers to a value related to the entity in the natural language query problem. For example, the natural language query problem includes the entity "Xiao Ming", and the candidate value set may include candidate values such as "Ming Yu" and "Ming Feng".
[0068] Furthermore, in order to accurately determine the candidate values, in the present embodiment, the step of determining the candidate values based on the candidate value set, the database content and the database schema specifically includes: determining a target candidate value set based on the candidate value set, the database content and the database schema; verifying the target candidate value set based on the number of candidate values in the target candidate value set, the number of numerical values in the database content and a preset similarity threshold to obtain candidate values.
[0069] It should be noted that the target candidate value set refers to a candidate value set obtained after screening all candidate values in the candidate value set.
[0070] It is understandable that after determining the target candidate value set, the number of candidate values in the target candidate values and the number of numerical values in the database content can be obtained. For example: the number of candidate values in the target candidate values is 60, and the number of numerical values in the database content is 100. At this time, the ratio between the number of candidate values and the number of numerical values is too large, indicating that too many candidate values are selected, and the similarity threshold needs to be reset until the ratio between the number of selected candidate values and the number of numerical values is less than the preset ratio.
[0071] It should be understood that the similarity threshold may include the preset text distance condition and the preset edit distance condition mentioned below. By changing the above two preset distance conditions, the ratio between the number of selected values in the target candidate value and the number of values in the database content can be reduced. This process is the verification process.
[0072] In a specific implementation, this embodiment may also not follow up similarity to verify the target candidate set, but use exact matching. The following are two examples: 'List the top 3 albums of Elton John in the Billboard charts' and 'Find all albums of Elton John starting with "goodbye"'. In these cases, this embodiment will not find '3' or "goodbye" in the database content. In the first example, the value 3 is not part of the database, but is used in the SQL query to limit the results. In the second example, the tag "goodbye" requires a wildcard match. However, wildcard matching is not enough to verify candidate values, because this method is too flexible and will lead to more wrong candidates.
[0073] Furthermore, in order to accurately determine the target candidate value set, in the present embodiment, the step of determining the target candidate value set based on the candidate value set, the database content and the database model specifically includes: obtaining the word embedding text distance between each candidate value in the candidate value set and the database content; when the word embedding text distance satisfies a preset text distance condition, obtaining an initial candidate value set; obtaining the edit distance between each initial candidate value in the initial candidate value set and the database model; when the edit distance satisfies a preset edit distance condition, obtaining the target candidate value set.
[0074] It should be noted that word embedding is a form of word representation, which allows words with similar meanings to have similar representations. The word embedding text distance refers to the text distance between each candidate value in the candidate value set and each database value in the database content. The preset text distance condition refers to a pre-set text distance condition, which can be set according to actual conditions. This embodiment does not impose any specific restrictions on this.
[0075] It is understandable that in this embodiment, all candidate values that meet the preset text distance condition can be selected from the candidate value set to form an initial candidate value set for initial screening.
[0076] It should be understood that the edit distance refers to a quantitative measurement of the degree of difference between two character strings (such as English words), and the measurement method is to see how many times of processing is required to transform one character string into another character string. The edit distance in this embodiment refers to the edit distance between each initial candidate value in the initial candidate value set and the rows and columns in the database model. The preset edit distance condition refers to a pre-set edit distance condition, which can be set according to actual conditions. This embodiment does not impose specific restrictions on this.
[0077] In a specific implementation, this embodiment can select all candidate values that meet the preset edit distance condition from the initial candidate value set to form a target candidate value set for further screening.
[0078] In addition, this embodiment can also generate a target candidate value set in two other ways. The first way is through a hand-crafted heuristic method, because the database has some specific (recurring) methods to implement certain data types. This embodiment currently uses the following heuristic methods: (1) Classic gender values, for example, are usually implemented as VARCHAR type columns with contents of "F" or "M". (2) Boolean data types are usually implemented with numeric columns, with values of 0 and 1. (3) For example, ordinal numbers are usually implemented with numeric columns, usually represented by an integer column. (4) Months (such as August) are usually part of a complete date column, so they can be found by using wildcards (such as 8 / %). Although these simple hand-crafted heuristic methods cannot be extended to databases in every field, they can guide the generative model to learn this pattern in a more dynamic way. The second way is to use n-grams. This embodiment uses this technology on words that are marked multiple times. If multiple consecutive words are marked as candidate column names or candidate values, n-grams are used for these words.
[0079] Specifically, the above three methods can be used in combination, and one method, two methods, or three methods can be used. This embodiment does not impose specific restrictions on this.
[0080] Step S202: inputting the candidate value set into the preset coding model to obtain a coded candidate value set;
[0081] Furthermore, in order to accurately obtain the encoded candidate value set, in this embodiment, the step S202 includes: obtaining the matching type corresponding to the candidate value set; marking the candidate value set according to the matching type to obtain the marked candidate value set; inputting the marked candidate value set into the preset encoding model to obtain the encoded candidate value set.
[0082] It should be noted that the matching types in this embodiment may include a complete match class and a candidate value match class. If a table or a column completely matches a word in a natural language query question, this article classifies it as a complete match class. If it is only a partial match, it is classified as a candidate value match class.
[0083] It is understandable that this embodiment marks and classifies each word in the input natural language query question into tables, columns, values, aggregation operations and supervalues, etc., which will become a more important basis in synthetic queries. This embodiment performs character-based precise matching on all words in the database schema and database content, which will provide some prior knowledge for the neural network. Because if the neural network only inputs natural language query questions and database schemas, a lot of additional information will be lost. For example, when we want to know whether a table name or column name in the database schema is mentioned in the natural language query statement, we need to provide the neural network with suggestive information about the table name and column name. But those more complex relationships, for example, "higher" may refer to price or height, which will be established in the neural network encoder.
[0084] In a specific implementation, after marking the candidate value set, all candidate values in the candidate value set can be marked as a complete match class or a candidate value match class, and the marked candidate value set is obtained, and the marked candidate value set is input into the preset encoding model. The preset encoding model of this embodiment is based on a pre-trained Transformer, which is also used in the latest Text2SQL system RATSQL. The Transformer architecture has been used for different tasks, such as natural language translation, natural language generation, and recently in the field of entity recognition. This only-focused Transformer structure can better represent natural language sequences than the classic recursive neural network (RNN). Therefore, in many natural language processing tasks, the Transformer structure usually produces better results than traditional neural networks. The preset encoding model of this embodiment is an extension of the IRNet encoder. The main difference is that the encoder of SLESQL not only inputs information about the database schema, but also inputs candidate values extracted from the database content. Therefore, the preset encoding model of this embodiment can also understand the correlation between the label of the natural language query problem and the actual value in the database content.
[0085] Step S203: input the encoded candidate value set into the preset decoding model to generate an SQL statement.
[0086] Furthermore, in order to accurately generate SQL statements, in this embodiment, step S203 includes: inputting the encoded candidate value set into the skeleton decoder to obtain a statement skeleton; inputting the statement skeleton and the encoded candidate value set into the detail decoder to obtain a SQL statement.
[0087] It should be noted that the preset decoding model in this embodiment may include a skeleton decoder and a detail decoder.
[0088] In a specific implementation, the preset decoding model in this embodiment receives the encoding of the question / table / column / value from the preset encoding model as input, and the output is a synthesized SemQL query. The preset decoding model consists of an LSTM architecture and multiple pointer networks for selecting tables, columns, and values. This embodiment uses a grammar-based decoder (TRANX) and uses LSTM to simulate the generation process of SemQL queries. This embodiment adopts a coarse-to-fine framework to decompose the decoding process of SemQL queries into two stages. In the first stage, a skeleton decoder outputs the skeleton of the SemQL query. Then, the detail decoder fills in the missing details in the skeleton by selecting columns and tables, and the candidate value set and the skeleton are completed into a complete SQL statement in the detail decoder.
[0089] This embodiment obtains a candidate value set by inputting a natural language query, a database schema, and database content into a preset entity recognition model, then inputs the candidate value set into a preset encoding model to obtain an encoded candidate value set, and then inputs the encoded candidate value set into a preset decoding model to generate an SQL statement. This embodiment first extracts and generates a candidate value set through a preset entity recognition model, and then uses the candidate value set to synthesize a complex, nested SQL statement through a preset encoder and a preset decoder.
[0090] Reference Figure 4 , Figure 4 This is a structural block diagram of the first embodiment of the SQL statement generating device of the present invention.
[0091] like Figure 4 As shown, the SQL statement generating device proposed in the embodiment of the present invention includes:
[0092] The information acquisition module 10 is used to obtain the natural language query question, database mode and database content input by the user;
[0093] The SQL statement generation module 20 is used to input the natural language query question, the database schema and the database content into a preset SLESQL model to generate an SQL statement.
[0094] This embodiment obtains the natural language query question, database schema and database content input by the user, and then inputs the natural language query question, database schema and database content into the preset SLESQL model to generate SQL statements. The present invention generates SQL statements by inputting the natural language query question, database schema and database content into the preset SLESQL model. Compared with the existing method of converting the user's natural language input into executable SQL statements through Text2SQL, which is not effective in complex problem environments, the above method of the present invention can improve the utilization effect of the database content and generate complex and nested SQL statements through the preset SLESQL model.
[0095] It should be noted that the workflow described above is merely illustrative and does not limit the scope of protection of the present invention. In practical applications, technicians in this field can select part or all of them according to actual needs to achieve the purpose of the present embodiment, and no limitation is made here.
[0096] In addition, for technical details not fully described in this embodiment, reference can be made to the SQL statement generation method provided in any embodiment of the present invention, and will not be repeated here.
[0097] Based on the above-mentioned first embodiment of the SQL statement generating device of the present invention, a second embodiment of the SQL statement generating device of the present invention is proposed.
[0098] In this embodiment, the preset SLESQL model includes: a preset entity recognition model, a preset encoding model and a preset decoding model; the SQL statement generation module 20 is also used to input the natural language query question, the database model and the database content into the preset entity recognition model to obtain a candidate value set; input the candidate value set into the preset encoding model to obtain an encoded candidate value set; input the encoded candidate value set into the preset decoding model to generate an SQL statement.
[0099] Furthermore, the SQL statement generation module 20 is also used to input the natural language query question into the preset entity recognition model to obtain a candidate value set; determine the candidate value based on the candidate value set, the database content and the database model; determine the candidate value set based on the candidate value and the database model.
[0100] Furthermore, the SQL statement generation module 20 is also used to determine a target candidate value set based on the candidate value set, the database content and the database model; verify the target candidate value set based on the number of candidate values in the target candidate value set, the number of numerical values in the database content and a preset similarity threshold to obtain candidate values.
[0101] Furthermore, the SQL statement generation module 20 is also used to obtain the word embedding text distance between each candidate value in the candidate value set and the database content; when the word embedding text distance meets the preset text distance condition, obtain the initial candidate value set; obtain the edit distance between each initial candidate value in the initial candidate value set and the database model; when the edit distance meets the preset edit distance condition, obtain the target candidate value set.
[0102] Furthermore, the SQL statement generation module 20 is also used to obtain the matching type corresponding to the candidate value set; mark the candidate value set according to the matching type to obtain the marked candidate value set; input the marked candidate value set into the preset encoding model to obtain the encoded candidate value set.
[0103] Furthermore, the preset decoding model includes: a skeleton decoder and a detail decoder; the SQL statement generation module 20 is also used to input the encoded candidate value set into the skeleton decoder to obtain a statement skeleton; input the statement skeleton and the encoded candidate value set into the detail decoder to obtain a SQL statement.
[0104] Other embodiments or specific implementations of the SQL statement generation device of the present invention can refer to the above-mentioned method embodiments, which will not be described in detail here.
[0105] In addition, an embodiment of the present invention further provides a storage medium, on which a SQL statement generation program is stored. When the SQL statement generation program is executed by a processor, the steps of the SQL statement generation method described above are implemented.
[0106] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or system including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or system. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or system including the element.
[0107] The serial numbers of the above embodiments of the present invention are only for description and do not represent the advantages or disadvantages of the embodiments.
[0108] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course can also be implemented by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present invention, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as a read-only memory / random access memory, a magnetic disk, or an optical disk), and includes a number of instructions for a terminal device (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to execute the methods described in each embodiment of the present invention.
[0109] The above are only preferred embodiments of the present invention, and are not intended to limit the patent scope of the present invention. Any equivalent structure or equivalent process transformation made using the contents of the present invention specification and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present invention.
Claims
1. A method for generating a SQL statement, characterized in that: The SQL statement generation method comprises the following steps: Obtain natural language query questions, database schema, and database content input by users; Inputting the natural language query question, the database schema, and the database content into a preset SLESQL model to generate an SQL statement; The preset SLESQL model includes: a preset entity recognition model, a preset encoding model and a preset decoding model; The step of inputting the natural language query question, the database schema, and the database content into a preset SLESQL model to generate an SQL statement specifically includes: Inputting the natural language query question, the database schema, and the database content into the preset entity recognition model to obtain a candidate value set; Inputting the candidate value set into the preset coding model to obtain an encoded candidate value set; Inputting the encoded candidate value set into the preset decoding model to generate an SQL statement; The step of inputting the natural language query question, the database schema, and the database content into the preset entity recognition model to obtain a candidate value set specifically includes: Inputting the natural language query question into the preset entity recognition model to obtain a set of candidate values; determining a candidate value based on the candidate value set, the database content, and the database schema; determining a candidate value set based on the candidate values and the database schema; The step of determining the candidate value according to the candidate value set, the database content and the database schema specifically includes: Determine a target candidate value set according to the candidate value set, the database content, and the database schema; Verifying the target candidate value set according to the number of candidate values in the target candidate value set, the number of values in the database content, and a preset similarity threshold to obtain a candidate value; The step of determining a target candidate value set according to the candidate value set, the database content, and the database schema specifically includes: Obtaining a word embedding text distance between each candidate value in the candidate value set and the database content; When the word embedding text distance meets a preset text distance condition, obtaining an initial candidate value set; Obtaining an edit distance between each initial candidate value in the initial candidate value set and the database pattern; When the edit distance satisfies a preset edit distance condition, a target candidate value set is obtained.
2. The SQL statement generation method according to claim 1, characterized in that: The step of inputting the candidate value set into the preset coding model to obtain the encoded candidate value set specifically includes: Obtaining a matching type corresponding to the candidate value set; Marking the candidate value set according to the matching type to obtain a marked candidate value set; The marked candidate value set is input into the preset coding model to obtain a coded candidate value set.
3. The SQL statement generation method according to claim 1, characterized in that: The preset decoding model includes: a skeleton decoder and a detail decoder; The step of inputting the encoded candidate value set into the preset decoding model to generate an SQL statement specifically includes: Inputting the encoded candidate value set into the skeleton decoder to obtain a sentence skeleton; The statement skeleton and the encoded candidate value set are input into the detail decoder to obtain the SQL statement.
4. A SQL statement generating device, characterized in that: The SQL statement generating device comprises: An information acquisition module is used to obtain natural language query questions, database schemas, and database contents input by users; An SQL statement generation module, used for inputting the natural language query question, the database schema and the database content into a preset SLESQL model to generate an SQL statement; The preset SLESQL model includes: a preset entity recognition model, a preset encoding model and a preset decoding model; The SQL statement generation module is further used to input the natural language query question, the database model and the database content into the preset entity recognition model to obtain a candidate value set; input the candidate value set into the preset encoding model to obtain an encoded candidate value set; input the encoded candidate value set into the preset decoding model to generate an SQL statement; The SQL statement generation module is further used to input the natural language query question into the preset entity recognition model to obtain a candidate value set; determine the candidate value according to the candidate value set, the database content and the database mode; determine the candidate value set according to the candidate value and the database mode; The SQL statement generation module is further used to determine a target candidate value set according to the candidate value set, the database content and the database mode; verify the target candidate value set according to the number of candidate values in the target candidate value set, the number of values in the database content and a preset similarity threshold to obtain candidate values; The SQL statement generation module is also used to obtain the word embedding text distance between each candidate value in the candidate value set and the database content; when the word embedding text distance meets the preset text distance condition, obtain the initial candidate value set; obtain the edit distance between each initial candidate value in the initial candidate value set and the database model; when the edit distance meets the preset edit distance condition, obtain the target candidate value set.
5. A device for generating SQL statements, characterized in that: The device comprises: a memory, a processor, and a SQL statement generation program stored in the memory and executable on the processor, wherein the SQL statement generation program is configured to implement the steps of the SQL statement generation method according to any one of claims 1 to 3.
6. A storage medium, characterized in that: The storage medium stores a SQL statement generation program, and when the SQL statement generation program is executed by the processor, the steps of the SQL statement generation method according to any one of claims 1 to 3 are implemented.
Citation Information
Patent Citations
Method for generating structured query statement based on natural language
CN111813802A
Tree model-based complex natural language query-to-SQL method
CN113032418A