SQL Statement Generation Method, Apparatus, Electronic Device, and Storage Medium
By constructing an operation intention graph and calculating the similarity to determine the mapping nodes of the object node, the problem of poor flexibility in converting natural language into SQL statements in the prior art is solved, and SQL statement generation is realized to meet the needs of complex operations.
Patent Information
- Application Number
- CN202510451474.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-11
- Publication Date
- 2025-06-24
- Estimated Expiration
- 2045-04-11
AI Technical Summary
In the prior art, the method of using rules to convert natural language into SQL statements is poor in flexibility and difficult to cope with complex query needs.
By obtaining the graph structure representation of text data and database, an operation intention diagram of text data is constructed, and the similarity between the object node and the node in the graph structure representation is calculated to determine the mapping node of the object node, and finally generate SQL statements.
Break through the limitations of fixed conversion rules, has stronger flexibility, can adapt to complex operational needs, and ensure the accuracy of generated SQL statements.
Smart Images

Figure CN119961363B_ABST
Abstract
Description
Background Art
[0002] With the development of database technology, people's demand for data management has been increasing day by day. Especially in the fields of finance, healthcare, e-commerce, etc., users often need to operate on the data in the database through natural language (for example, querying, deleting, etc.).
[0003] In the related art, by using rules, different words in natural language are fixedly converted into corresponding SQL (Structured Query Language) words, so as to convert natural language into SQL statements.
[0004] However, the method of converting natural language into SQL statements by using rules has poor flexibility and is difficult to cope with complex query requirements. Summary of the Invention
[0005] The present application provides a method for generating SQL statements, which at least overcomes the problem of poor flexibility in the related art to a certain extent.
[0006] Other features and advantages of the present application will become apparent through the following detailed description, or be learned in part through the practice of the present application.
[0007] According to one aspect of the present application, there is provided a method for generating SQL statements, including: obtaining text data for operating on a database; obtaining a graph structure representation of the database, where the nodes in the graph structure representation include tables in the database and fields in the tables; constructing an operation intention graph of the text data, where the types of nodes in the operation intention graph include object nodes, each object node corresponds to an operation object or a condition object in the text data, and the type of each object node is an operation object node or a condition object node; calculating the similarity between each object node and each node in the graph structure representation; determining the node in the graph structure representation corresponding to each object node according to the similarity; when any object node corresponds to at least two nodes in the graph structure representation, determining the connection node closest to the any object node in the operation intention graph, where the type of the any object node is different from that of the connection node; when the connection node corresponds to one node in the graph structure representation, determining a relationship node having a connection relationship with the node corresponding to the connection node in the graph structure representation among the at least two nodes; when the number of the relationship nodes is one, using the relationship node as the mapping node of the any object node; when any object node corresponds to one node in the graph structure representation, using the node corresponding to the any object node in the graph structure representation as the mapping node; generating an SQL statement corresponding to the text data according to the mapping node of each object node.
[0008] The method generates an operation intention graph using text data, and then uses the corresponding graph structure representation in the database to match each object node in the operation intention graph, so as to determine the corresponding table or field for each object node, and further generate an SQL statement. This way breaks through the limitations of fixed conversion rules, has stronger flexibility, and can adapt to complex operation requirements.
[0009] Furthermore, when there are multiple nodes with relatively high similarity for any object node in the graph structure representation, the method determines whether there is a connection relationship between the nodes with relatively high similarity to the connection node in the graph structure representation and these multiple nodes, so as to determine the node (mapping node) that these multiple nodes truly correspond to for any object node. This comprehensively considers the structure of the operation intention and the structure of the database, so as to ensure the accuracy of the relationship between any object node and the mapping node, and thus make the generated SQL statement more accurate and can achieve the operation requirements.
[0010] In some embodiments, the nodes in the operation intention graph include object formats, and the object formats represent the data types corresponding to the object nodes; the nodes in the graph structure representation also include the data types of fields; the method further includes: when the number of relationship nodes is multiple, determining the similarities and differences between the data types of each relationship node and the object format of any object node to obtain multiple similarities and differences; when there is one same in the multiple similarities and differences, taking the relationship node corresponding to the same similarity and difference as the mapping node of any object node.
[0011] Using the form of whether the object format of the object node matches the data type of the field can further help determine the mapping node of any object node. For example, if the text data needs to be sorted according to "amount", and the "amount" is an object of a condition, which is a condition object node in the operation intention graph, it is obvious that the object format of the "amount" should be a numerical format. If the data type of one of the two relationship nodes is a text format (for example, the recorded content is the product name), and the data type of the other relationship node is a numerical format, it can be directly determined that the content in the text format cannot meet the requirement of sorting using "amount" in the operation intention, while the content in the numerical format can meet the requirement of sorting using "amount" in the operation intention.
[0012] In some embodiments, it also includes: when there are at least two identical identical similarity and difference relationships among the multiple similarity and difference relationships, determining a connection path between each relationship node corresponding to the identical similarity and difference relationship and a node corresponding to the connection node in the graph structure representation, to obtain at least two connection paths; obtaining the type of each node in each connection path, wherein the type of each node in each connection path is a table node or a field node; inputting the at least two connection paths and the type of each node in each connection path into a large language model, and generating a text description of each connection path by the large language model; calculating the matching degree of each text description with the text data, and using the relationship node corresponding to the text description with the highest matching degree as a mapping node for any of the object nodes.
[0013] By using the types of each node in the connection path, the subordinate relationship between the nodes in the connection path can be determined, so as to facilitate the large language model to describe the connection path. The similarity between the text data and the text description can reflect the accuracy of the mapping node between the operation intention and whether the connection node in the connection path is an object node. Therefore, by further using this method, the corresponding table or field of the object node in the database can be more accurately determined, so that the generated SQL statement is more accurate and in line with the operation intention in the text data.
[0014] In some embodiments, it also includes: when the connecting node corresponds to at least two nodes in the graph structure representation, respectively determining the connection status between each node corresponding to any object node in the graph structure representation and at least two nodes corresponding to the connecting node in the graph structure representation; when the connection status has a connection path, the node corresponding to any object node in the graph structure representation and on the connection path is used as the mapping node of the any object node.
[0015] In some embodiments, the nodes in the operation intention graph include an object format, and the object format represents the data type corresponding to the object node; the nodes in the graph structure representation also include the data type of the field; the method also includes: when the connection situation includes multiple connection paths, obtaining the type of each node in each connection path, and the type of each node in each connection path is a table node or a field node; inputting the multiple connection paths and the type of each node in each connection path into a large language model, and generating a text description of each connection path by the large language model; calculating the matching degree of each text description with the text data, and using the relationship node corresponding to the text description with the highest matching degree as the mapping node of any object node.
[0016] In some embodiments, the nodes in the operation intention graph include operation manners and conditions; generating the SQL statement corresponding to the text data according to the mapping nodes of each object node includes: verifying the legality of the operation manners and conditions in the database; and generating the SQL statement corresponding to the text data according to the mapping nodes of each object node when the legality verification is passed.
[0017] Performing legality verification on the operation manners and conditions in the operation intention graph in advance can avoid the generated SQL statement being inapplicable to the database. At the same time, after the legality verification fails, it can be reflected that the intention in the text data cannot be realized in the database.
[0018] According to another aspect of the present application, there is also provided an SQL statement generation device, including: a first acquisition module, configured to acquire text data for operating a database; a second acquisition module, configured to acquire a graph structure representation of the database, where the nodes in the graph structure representation include tables in the database and fields in the tables; a construction module, configured to construct an operation intention graph of the text data, where the types of nodes in the operation intention graph include object nodes, each object node corresponds to an operation object or a condition object in the text data, and the type of each object node is an operation object node or a condition object node; a calculation module, configured to calculate the similarity between each object node and each node in the graph structure representation; a first determination module, configured to determine the node corresponding to each object node in the graph structure representation according to the similarity; a second determination module, configured to, when any object node corresponds to at least two nodes in the graph structure representation, determine the connection node closest to the any object node in the operation intention graph, where the type of the any object node is different from that of the connection node; when the connection node corresponds to one node in the graph structure representation, determining a relationship node having a connection relationship with the node corresponding to the connection node in the graph structure representation among the at least two nodes; when the number of the relationship nodes is one, using the relationship node as the mapping node of the any object node; when any object node corresponds to one node in the graph structure representation, using the node corresponding to the any object node in the graph structure representation as the mapping node; and a generation module, configured to generate the SQL statement corresponding to the text data according to the mapping nodes of each object node.
[0019] In one embodiment, the nodes in the operation intention graph include an object format, and the object format represents the data type corresponding to the object node; the nodes in the graph structure representation further include the data types of fields; the second determination module is further configured to, when the number of relationship nodes is multiple, determine the similarities and differences between the data type of each relationship node and the object format of any one of the object nodes, and obtain a plurality of similarities and differences; when there is one same similarity and difference among the plurality of similarities and differences, use the relationship node corresponding to the same similarity and difference as the mapping node of any one of the object nodes; and, when there are at least two same similarities and differences among the plurality of similarities and differences, determine the connection paths between each relationship node corresponding to the same similarity and difference and the nodes corresponding to the connection node in the graph structure representation, and obtain at least two connection paths; obtain the types of the nodes in each connection path, and the types of the nodes in each connection path are table nodes or field nodes; input the at least two connection paths and the types of the nodes in each connection path into a large language model, and generate a text description of each connection path by the large language model; calculate the matching degree between each text description and the text data, and use the relationship node corresponding to the text description with the highest matching degree as the mapping node of any one of the object nodes.
[0020] In one embodiment, the second determination module is further configured to, when the connection node corresponds to at least two nodes in the graph structure representation, respectively determine the connection situations between each node corresponding to any one of the object nodes in the graph structure representation and the at least two nodes corresponding to the connection node in the graph structure representation; when there is one connection path in the connection situation, use the node corresponding to any one of the object nodes in the graph structure representation and on the connection path as the mapping node of any one of the object nodes; and, when the connection situation includes multiple connection paths, obtain the types of the nodes in each connection path, and the types of the nodes in each connection path are table nodes or field nodes; input the multiple connection paths and the types of the nodes in each connection path into a large language model, and generate a text description of each connection path by the large language model; calculate the matching degree between each text description and the text data, and use the relationship node corresponding to the text description with the highest matching degree as the mapping node of any one of the object nodes.
[0021] In one embodiment, the nodes in the operation intention graph include an operation mode and conditions; the generation module is configured to verify the legality of the operation mode and conditions in the database; and, when the legality verification is passed, generate an SQL statement corresponding to the text data according to the mapping node of each object node.
[0022] According to another aspect of the present application, there is also provided an electronic device, which includes: a processor; and a memory for storing executable instructions of the processor; wherein, the processor is configured to execute the SQL statement generation method described in any one of the above via executing the executable instructions.
[0023] According to yet another aspect of the present application, there is also provided a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the SQL statement generation method described in any one of the above is implemented.
[0024] According to yet another aspect of the present application, there is also provided a computer program product, including a computer program, and when the computer program is executed by a processor, the SQL statement generation method described in any one of the above is implemented. Description of the Drawings
[0025] Figure 1 Showing the flowchart of the SQL statement generation method in an embodiment of the present application;
[0026] Figure 2 Showing the schematic diagram of the graph structure representation in an embodiment of the present application;
[0027] Figure 3 Showing the schematic diagram of the operation intention graph in an embodiment of the present application;
[0028] Figure 4 Showing the schematic diagram of the operation intention graph in another embodiment of the present application;
[0029] Figure 5 Showing the schematic diagram of the graph structure representation in another embodiment of the present application;
[0030] Figure 6 Showing the schematic diagram of the graph structure representation in yet another embodiment of the present application;
[0031] Figure 7 Showing the flowchart of the SQL statement generation method in another embodiment of the present application;
[0032] Figure 8 Showing the flowchart of the SQL statement generation method in yet another embodiment of the present application;
[0033] Figure 9 Showing the flowchart of the SQL statement generation method in yet another embodiment of the present application;
[0034] Figure 10 Showing the flowchart of the SQL statement generation method in yet another embodiment of the present application;
[0035] Figure 11 Showing the schematic diagram of the SQL statement generation device in an embodiment of the present application;
[0036] Figure 12 Shows a structural block diagram of an electronic device in an embodiment of the present application. Detailed implementation manners
[0037] Example embodiments will now be described more fully with reference to the accompanying drawings. However, the example embodiments can be implemented in various forms and should not be construed as limited to the examples set forth herein; rather, these embodiments are provided so that this application will be more complete and comprehensive, and will fully convey the concept of the example embodiments to those skilled in the art. The features, structures, or characteristics described may be combined in any suitable manner in one or more embodiments.
[0038] In addition, the accompanying drawings are only schematic illustrations of the present application and are not necessarily drawn to scale. The same reference numerals in the drawings denote the same or similar parts, and thus repeated descriptions thereof will be omitted. Some of the block diagrams shown in the drawings are functional entities and do not necessarily correspond to physically or logically independent entities. These functional entities may be implemented in software form, or in one or more hardware modules or integrated circuits, or in different networks and / or processor devices and / or microcontroller devices.
[0039] For ease of understanding, before introducing the embodiments of the present application, several terms involved in the embodiments of the present application are explained as follows:
[0040] Text data: Natural language in text form used to describe the operation intention of a database;
[0041] Operation object: The object on which the operation method in the text data acts. For example, when selecting stores with sales greater than 10,000, "stores" is the operation object of the operation method of "selecting".
[0042] Condition object: The object used to restrict the operation object in the text data (for example, restricting the range, sorting method, etc. of the operation object). For example, when selecting stores with sales greater than 10,000, "sales" is the condition object of the operation object of "stores".
[0043] Object format: Represents the data type corresponding to the object node. For example, when selecting stores with sales greater than 10,000, the data type corresponding to "stores" is the names of each store, the object format is text format, and the data type corresponding to "sales" is the specific amount, and the object format is digital format.
[0044] The following will describe in detail the specific implementation manners of the embodiments of the present application with reference to the accompanying drawings.
[0045] In an embodiment of the present application, a method for generating an SQL statement is provided, and this method can be executed by any electronic device with computing and processing capabilities.
[0046] Figure 1 The flowchart of a method for generating an SQL statement in an embodiment of the present application is shown. As Figure 1 shown, the method for generating an SQL statement provided in the embodiment of the present application includes the following S101 to S110.
[0047] S101, obtain text data for operating on a database.
[0048] Among them, the embodiment of the present application does not limit which operation the text data is specifically used for operating on the database. For example, the text data is: delete the backup data before 2020. The embodiment of the present application does not limit the language type of the text data, and the language type of the text data can be Chinese, English, French, Russian, and so on.
[0049] The user can directly input the text data into the electronic device executing this solution through an input device (such as a keyboard, etc.), or the electronic device can arbitrarily obtain the text data from other devices.
[0050] S102, obtain the graph structure representation of the database, where the nodes in the graph structure representation include the tables in the database and the fields in the tables.
[0051] In one embodiment, obtaining the graph structure representation of the database may include: obtaining the tables included in the database; determining the fields included in each table; determining the foreign keys of the connections between different tables; and constructing the graph structure representation of the database according to the tables in the database, the fields in the tables, and the foreign keys of the connections between different tables.
[0052] For example, Figure 2 shows a part of the graph structure representation of the database. Among them, field 1 is the foreign key connecting table 1 and table 2, fields 1, 2, and 3 are all fields in table 1, and fields 1, 4, 5, 6, and 7 are all fields in table 2.
[0053] In another embodiment, if the graph structure representation of the database is pre-stored in the electronic device executing this solution, the electronic device can directly obtain the graph structure representation from the memory.
[0054] In one embodiment, the electronic device can also update the graph structure representation according to the database at a preset period.
[0055] S103. Construct an operation intention graph for the text data. The types of nodes in the operation intention graph include object nodes. Each object node corresponds to an operation object or a conditional object in the text data, and the type of each object node is an operation object node or a conditional object node.
[0056] In one embodiment, constructing the operation intention graph for the text data may include: performing intention recognition on the text data to obtain the operation intention of the text data; converting the operation intention into a graph structure to obtain the operation intention graph.
[0057] For example, if the text data is: Select stores in North China with sales greater than 10,000, then the operation intention of the text data is:
[0058] Operation method: Select;
[0059] Operation object: Store;
[0060] Conditional objects: Sales amount, region;
[0061] Conditions: Sales amount > 10,000, store is located in North China.
[0062] The operation intention graph constructed according to this operation intention may be as Figure 3 shown, Figure 3 only the object nodes in the operation intention are shown.
[0063] Figure 4 The operation intention graph in also shows the operation method, conditions, and object format.
[0064] Regarding how to identify the object format of the object nodes, the embodiments of the present application do not make limitations. The large language model can be directly used to analyze the most likely format of the specific content corresponding to the object nodes, so as to obtain the object format of each object node. For example, input "What is the most likely format to record the content under the'store' field in the table" into the large language model, and use the data format output by the large language model as the object format of the object node'store'.
[0065] S104. Calculate the similarity between each object node and each node in the graph structure representation.
[0066] Regarding what method to use to calculate the similarity between the object nodes and each node in the graph structure representation, the embodiments of the present application do not make limitations.
[0067] For example, the cosine similarity can be used to calculate the similarity between each object node and each node in the graph structure representation.
[0068] For example, if the text data is: Select stores in North China with sales greater than 10,000, then the object nodes in the operation intention graph include sales amount, such asFigure 5 As shown, the graph structure representation of the database includes nodes: store location, store name, store sales, store manager, fruit sales information in the first quarter, fruit sales quantity, and fruit sales amount.
[0069] Then calculate the cosine similarity between the text "sales amount" and "store operation information", "store location", "store name", "store sales", "store manager", "fruit sales information in the first quarter", "fruit sales quantity", and "fruit sales amount" respectively.
[0070] When calculating the similarity, it is also possible to calculate the L1 distance or other distances between the object nodes in the operation intention graph and the nodes in the graph structure representation to calculate the similarity between each object node and each node in the graph structure representation.
[0071] S105. Determine the corresponding node of each object node in the graph structure representation according to the similarity.
[0072] In one embodiment, determining the corresponding node of each object node in the graph structure representation according to the similarity may include: using the node with a similarity greater than the similarity threshold between the graph structure representation and the object node as the corresponding node of the object node in the graph structure representation.
[0073] For example, the similarities between the text "sales amount" and "store operation information", "store location", "store name", "store sales", "store manager", "fruit sales information in the first quarter", "fruit sales quantity", and "fruit sales amount" are 0.02, 0.01, 0.03, 0.95, 0.10, 0.02, 0.62, and 0.86 respectively. If the similarity threshold is set to 0.85, then both "store sales" and "fruit sales amount" are the corresponding nodes of the object node "sales amount" in the graph structure representation. If the similarity threshold is set to 0.9, then "store sales" is the corresponding node of the object node "sales amount" in the graph structure representation.
[0074] S106. When any object node corresponds to at least two nodes in the graph structure representation, determine the connection node closest to the any object node in the operation intention graph, and the types of the any object node and the connection node are different.
[0075] Still taking Figure 2For example, for the two object nodes of "sales amount" and "region", "store" is the connection node. For the object node of "store", both "sales amount" and "region" can be used as connection nodes. When there are multiple nodes among the connection nodes closest to any object node in this operation intention graph, any one of these multiple nodes is used as the final connection node. That is to say, either "sales amount" or "region" can be arbitrarily selected as the final connection node of the object node of "store".
[0076] In one embodiment, if there are multiple connection nodes closest to any object node in this operation intention graph, the number of nodes corresponding to each connection node in the graph structure representation can be determined first, and the one with fewer corresponding nodes in the graph structure representation is selected as the final connection node. If the number of corresponding nodes in the graph structure representation is the same, any one is selected as the final connection node.
[0077] S107. When the connection node corresponds to one node in the graph structure representation, determine the relationship node that has a connection relationship between the at least two nodes and the node corresponding to the connection node in the graph structure representation.
[0078] For example Figure 3 and Figure 5 As shown, still taking any object node as "sales amount" as an example, and the nodes corresponding to this "sales amount" in the graph structure representation are "fruit sales amount" and "store sales amount". The connection node corresponding to this "sales amount" is "store", and the node corresponding to this "store" in the graph structure representation shown in Figure 5 is "store name".
[0079] Then the connection situations between "store sales amount", "fruit sales amount" and "store name" are that "store name" is connected to "store sales amount" through "store operation information", while there is no connection relationship between "fruit sales amount" and "store name".
[0080] Then it can be determined that the relationship node that has a connection relationship between "store sales amount" and the "store name" corresponding to "store" in the graph structure representation, that is to say, among "store sales amount" and "fruit sales amount", "store sales amount" is the relationship node.
[0081] S108. When the number of these relationship nodes is one, use this relationship node as the mapping node of any object node.
[0082] S109. When any object node corresponds to one node in the graph structure representation, use the node corresponding to this object node in the graph structure representation as the mapping node.
[0083] For example, if the operation object "store" in the text data corresponds to a node "store name" in the graph structure representation, then the "store name" is the mapping node of the "store".
[0084] S110. Generate an SQL statement corresponding to the text data according to the mapping node of each object node.
[0085] After determining the mapping node of each object node, the mapping node of each object node can be used to accurately generate an SQL statement executable by the database.
[0086] In one embodiment, as Figure 4 shown, the nodes in the operation intention graph include the operation method and conditions. Generating an SQL statement corresponding to the text data according to the mapping node of each object node may include: verifying the legality of the operation method and conditions in the database; and generating an SQL statement corresponding to the text data according to the mapping node of each object node when the legality verification passes.
[0087] Performing a legality check on the operation method and conditions in the operation intention graph in advance can avoid the generated SQL statement being inapplicable in the database. At the same time, after the legality check fails, it can reflect that the intention in the text data cannot be realized in the database.
[0088] The method of generating an operation intention graph using text data and then matching each object node in the operation intention graph with the corresponding graph structure representation of the database to determine the corresponding table or field of each object node and further generate an SQL statement breaks through the limitation of fixed conversion rules, has stronger flexibility, and can adapt to complex operation requirements.
[0089] Furthermore, when there are multiple nodes with relatively high similarity for any object node in the graph structure representation, the method of determining the node (mapping node) that the multiple nodes truly correspond to the any object node by using whether there is a connection relationship between the nodes with relatively high similarity in the graph structure representation and the multiple nodes comprehensively considers the structure of the operation intention and the structure of the database, so as to ensure the accuracy of the relationship between any object node and the mapping node, and thus make the generated SQL statement more accurate and can realize the operation requirements.
[0090] In one embodiment, as Figure 4 and Figure 6 shown, the nodes in the operation intention graph include the object format, and the object format represents the data type corresponding to the object node. The nodes in the graph structure representation also include the data type of the field. Then, as Figure 7 shown, the SQL statement generation method may further include S71 - S72.
[0091] S71. When the number of such relationship nodes is multiple, determine the similarities and differences between the data type of each relationship node and the object format of any one of the object nodes, obtaining multiple similarities and differences relationships.
[0092] Taking Figure 4 and Figure 6 the operation intention diagram and the graph structure representation of the database shown as an example, the text data is: Select stores in North China with sales greater than 10,000.
[0093] Taking any one of the object nodes as "store" as an example, this "store" corresponds to two nodes in the graph structure representation, namely "store name" and "store location", the connection node of this "store" is "sales amount", and the node corresponding to this "sales amount" in the graph structure representation is "store sales amount".
[0094] Since there are connection relationships between both "store name" and "store location" and "store sales amount", so both "store location" and "store name" are relationship nodes.
[0095] In determining the similarities and differences between the data type of each relationship node and the object format of any one of the object nodes, it can be determined that the data type of "store location" is in digital format, the data type of "store name" is in text format, while the object format of "store" in the operation intention diagram is in text format.
[0096] Therefore, it can be determined that the similarity and difference relationship between the data type of "store name" and the object format of "store" is the same, and the similarity and difference relationship between the data type of "store location" and the object format of "store" is different.
[0097] S72. When there is one same among the multiple similarities and differences relationships, use the relationship node corresponding to the similarity and difference relationship that is the same as the mapping node of any one of the object nodes.
[0098] Still taking the example described in S71 as an example, since among the similarities and differences relationships between the data types of the two relationship nodes "store name" and "store location" and the object format of "store", only the data type of "store name" is the same as the object format of "store", therefore, "store name" can be directly used as the mapping node of "store".
[0099] Using the form of whether the object format of the object node matches the data type of the field can further help determine the mapping node of any one of the object nodes.
[0100] For example, if text data needs to be sorted according to "amount", the "amount" is a conditional object and a conditional object node in the operation intention diagram. It is obvious that the object format of the "amount" should be in digital format. If the data type of one of the two relationship nodes is in text format (for example, the recorded content is the product name), and the data type of the other relationship node is in digital format, it can be directly determined that the content in text format cannot achieve the requirement of sorting by "amount" in the operation intention, while the content in digital format can achieve the requirement of sorting by "amount" in the operation intention.
[0101] In one embodiment, Figure 7 Based on the SQL statement generation method shown in Figure 8 As shown, including: S81-S84.
[0102] S81, when there are at least two identical identical different relationships among the multiple identical different relationships, determine a connection path between each relationship node corresponding to the identical different relationship and a node corresponding to the connection node in the graph structure representation, and obtain at least two connection paths.
[0103] Still Figure 4 The operation intention diagram shown and Figure 6 Taking the graph structure representation of the database shown as an example, the text data is: select stores in North China with sales greater than 10,000.
[0104] Taking any object node as "store" as an example, the "store" corresponds to two nodes in the graph structure representation, namely "store name" and "store manager", and the connection node of the "store" is "sales", and the node corresponding to the "sales" in the graph structure representation is "store sales".
[0105] Since both "store name" and "store manager" are connected to "store sales", "store manager" and "store name" are relationship nodes, and the data types of "store manager" and "store name" are both in text format and have the same object format as "store". In this case, there are at least two identical similarity and difference relationships among the multiple similarity and difference relationships determined in S71.
[0106] The connection path between "store manager" and "store sales" is: store manager-store operation information-store sales. The connection path between "store name" and "store sales" is: store name-store operation information-store sales.
[0107] S82, obtaining the type of each node in each connection path, where the type of each node in each connection path is a table node or a field node.
[0108] Among them, the type of each node in the graph structure representation is clear when constructing the graph structure representation, and the types of each node can be directly recorded in the graph structure representation or stored separately in the electronic device executing this solution. The embodiments of this application do not limit this.
[0109] Among them, the types of "store name", "store manager", and "store sales" are all field nodes, and the type of "store operation information" is a table node.
[0110] S83. Input the at least two connection paths and the types of each node in each connection path into a large language model, and let the large language model generate a text description of each connection path.
[0111] The embodiments of this application do not limit which specific large language model it is. For example, the large language model can be DeepSeek, chatGPT, etc.
[0112] For example, taking the connection path: store name - store operation information - store sales as an example, then input "store name - store operation information - store sales" and the types of "store name" and "store sales" are both field nodes, and the type of "store operation information" is a table node into the large language model, and combine it with the prompt content "Please generate a continuous text description of this connection path according to the path and the types of each node in the path".
[0113] It should be noted that the above given prompt content is only exemplary.
[0114] The large language model can output a continuous text description to represent this connection path.
[0115] S84. Calculate the matching degree between each text description and the text data, and use the relationship node corresponding to the text description with the highest matching degree as the mapping node of any object node.
[0116] The embodiments of this application do not limit the method for calculating the matching degree between each text description and the text data. Any method for calculating the similarity between texts can be used to represent the matching degree. For example, cosine distance, Euclidean distance, etc. can be used.
[0117] Using the types of each node in the connection path, the subordinate relationship between each node in the connection path can be determined, which is convenient for the large language model to describe the connection path. The similarity between the text data and the text description can reflect the accuracy of whether the operation intention and the connection node in the connection path are the mapping nodes of the object node. Therefore, by further using this method, the table or field corresponding to the object node in the database can be determined more accurately, so that the generated SQL statement is more accurate and conforms to the operation intention in the text data.
[0118] In some embodiments, as Figure 9 shown, on the basis of the SQL statement generation method shown in Figure 1 , S91 - S92 may further be included.
[0119] S91. When the connection node corresponds to at least two nodes in the graph structure representation, respectively determine the connection situation between each node corresponding to the any object node in the graph structure representation and the at least two nodes corresponding to the connection node in the graph structure representation.
[0120] Still taking the operation intention graph shown in Figure 4 and the graph structure representation of the database shown in Figure 6 as an example, the text data is: Select stores in North China with sales greater than 10,000.
[0121] Taking any object node as "store" as an example, the "store" corresponds to two nodes in the graph structure representation, namely "store name" and "store manager", the connection node of the "store" is "sales", and the nodes corresponding to the connection node "sales" in the graph structure representation are two nodes, "store sales" and "fruit sales".
[0122] Then, respectively determine the connection situation between each node corresponding to the any object node in the graph structure representation and the at least two nodes corresponding to the connection node in the graph structure representation, and the following can be obtained:
[0123] Store manager - Store operation information - Store sales;
[0124] Store name - Store operation information - Store sales.
[0125] There are two connection paths in total.
[0126] S92. When the connection situation has one connection path, use the node corresponding to the any object node in the graph structure representation and on the connection path as the mapping node of the any object node.
[0127] In one embodiment, the nodes in the operation intention graph include an object format, and the object format represents the data type corresponding to the object node. The nodes in the graph structure representation also include the data type of the field. As Figure 10 shown, on the basis of the SQL statement generation method shown in Figure 9 , S1001 - S1003 may further be included. Among them, for the specific implementation of S1001 - S1003, reference may be made to the corresponding embodiments above Figure 8 , and details are not described herein again.
[0128] S1001. When the connection situation includes multiple connection paths, obtain the types of each node in each connection path, and the types of each node in each connection path are table nodes or field nodes.
[0129] S1002. Input the multiple connection paths and the types of each node in each connection path into a large language model, and let the large language model generate a text description for each connection path.
[0130] S1003. Calculate the matching degree between each text description and the text data, and use the relationship node corresponding to the text description with the highest matching degree as the mapping node of the any object node.
[0131] Based on the same inventive concept, an SQL statement generation device is also provided in an embodiment of the present application, as described in the following embodiments. Since the principle of solving problems in the device embodiment is similar to that of the above method embodiment, the implementation of the device embodiment can refer to the implementation of the above method embodiment, and the repeated parts will not be described again.
[0132] Figure 11 Show a schematic diagram of an SQL statement generation device in an embodiment of the present application, as Figure 11 shown, the device includes: a first acquisition module 111, configured to acquire text data for operating on a database; a second acquisition module 112, configured to acquire a graph structure representation of the database, and the nodes in the graph structure representation include tables in the database and fields in the tables; a construction module 113, configured to construct an operation intention graph of the text data, and the types of nodes in the operation intention graph include object nodes, and each object node corresponds to an operation object or a condition object in the text data, and the type of each object node is an operation object node or a condition object node; a calculation module 114, configured to calculate the similarity between each object node and each node in the graph structure representation; a first determination module 115, configured to determine the node corresponding to each object node in the graph structure representation according to the similarity; a second determination module 116, configured to, when any object node corresponds to at least two nodes in the graph structure representation, determine the connection node closest to the any object node in the operation intention graph, and the type of the any object node is different from that of the connection node; when the connection node corresponds to one node in the graph structure representation, determine a relationship node having a connection relationship with the node corresponding to the connection node in the graph structure representation among the at least two nodes; when the number of the relationship nodes is one, use the relationship node as the mapping node of the any object node; when any object node corresponds to one node in the graph structure representation, use the node corresponding to the any object node in the graph structure representation as the mapping node; a generation module 117, configured to generate an SQL statement corresponding to the text data according to the mapping node of each object node.
[0133] In one embodiment, the nodes in the operation intention graph include an object format, which represents the data type corresponding to the object node; the nodes in the graph structure representation further include the data types of the fields; the second determination module 116 is further configured to, when the number of relationship nodes is multiple, determine the similarities and differences between the data type of each relationship node and the object format of any one of the object nodes, and obtain multiple similarities and differences relationships; when there is one same relationship among the multiple similarities and differences relationships, use the relationship node corresponding to the same similarities and differences relationship as the mapping node of any one of the object nodes; and, when there are at least two same relationships among the multiple similarities and differences relationships, determine the connection paths between each relationship node corresponding to the same similarities and differences relationship and the nodes corresponding to the connection node in the graph structure representation, and obtain at least two connection paths; obtain the types of the nodes in each connection path, and the types of the nodes in each connection path are table nodes or field nodes; input the at least two connection paths and the types of the nodes in each connection path into a large language model, and generate a text description of each connection path by the large language model; calculate the matching degree between each text description and the text data, and use the relationship node corresponding to the text description with the highest matching degree as the mapping node of any one of the object nodes.
[0134] In one embodiment, the second determination module 116 is further configured to, when the connection node corresponds to at least two nodes in the graph structure representation, respectively determine the connection situations between each node corresponding to any one of the object nodes in the graph structure representation and the at least two nodes corresponding to the connection node in the graph structure representation; when there is one connection path in the connection situation, use the node corresponding to any one of the object nodes in the graph structure representation and on the connection path as the mapping node of any one of the object nodes; and, when the connection situation includes multiple connection paths, obtain the types of the nodes in each connection path, and the types of the nodes in each connection path are table nodes or field nodes; input the multiple connection paths and the types of the nodes in each connection path into a large language model, and generate a text description of each connection path by the large language model; calculate the matching degree between each text description and the text data, and use the relationship node corresponding to the text description with the highest matching degree as the mapping node of any one of the object nodes.
[0135] In one embodiment, the nodes in the operation intention graph include an operation mode and conditions; the generation module 117 is configured to verify the legality of the operation mode and conditions in the database; and, when the legality verification is passed, generate an SQL statement corresponding to the text data according to the mapping node of each object node.
[0136] By generating an operation intention diagram using text data, and then using the corresponding graph structure representation in the database to match each object node in the operation intention diagram, so as to determine the corresponding table or field for each object node, and further generate an SQL statement, it breaks through the limitations of fixed conversion rules, has stronger flexibility, and can adapt to complex operation requirements.
[0137] Furthermore, when there are multiple nodes with relatively high similarity for any object node in the graph structure representation, by using whether there is a connection relationship between the nodes with relatively high similarity to this connection node in the graph structure representation and these multiple nodes, to determine the node (mapping node) that these multiple nodes truly correspond to for any object node, this method comprehensively considers the structure of the operation intention and the structure of the database, so as to ensure the accuracy of the relationship between any object node and the mapping node, and thus make the generated SQL statement more accurate and can realize the operation requirements.
[0138] Those skilled in the art of the present technology can understand that various aspects of the present application can be implemented as a system, a method, or a program product. Therefore, various aspects of the present application can be specifically implemented in the following forms, namely: a complete hardware implementation, a complete software implementation (including firmware, microcode, etc.), or an implementation combining hardware and software aspects, which can be collectively referred to as "circuits", "modules", or "systems" here.
[0139] The following refers to Figure 12 to describe the electronic device 1200 according to this embodiment of the present application. Figure 12 The electronic device 1200 shown is merely an example and should not impose any limitations on the functions and usage scope of the embodiments of the present application.
[0140] As Figure 12 shown, the electronic device 1200 is presented in the form of a general computing device. The components of the electronic device 1200 may include but are not limited to: the above-mentioned at least one processing unit 1210, the above-mentioned at least one storage unit 1220, and a bus 1230 connecting different system components (including the storage unit 1220 and the processing unit 1210).
[0141] Among them, the storage unit stores program code, and the program code can be executed by the processing unit 1210, so that the processing unit 1210 executes the steps according to various exemplary embodiments of the present application described in the above "Exemplary Method" section of this specification. For example, the processing unit 1210 can execute the following steps of the above method embodiments: S101 - S110, S71 - S72, S81 - S84, S91 - S92, S1001 - S1003.
[0142] The storage unit 1220 may include a readable medium in the form of a volatile storage unit, such as a random access storage unit (RAM) 1221 and / or a cache storage unit 1222, and may further include a read-only storage unit (ROM) 1223.
[0143] The storage unit 1220 may also include a program / utilities 1224 having a set (at least one) of program modules 1225. Such program modules 1225 include, but are not limited to: an operating system, one or more application programs, other program modules, and program data. Each or some combination of these examples may include an implementation of a network environment.
[0144] The bus 1230 may represent one or more of several types of bus structures, including a memory bus or memory controller, a peripheral bus, an accelerated graphics port, a processing unit, or a local bus using any of a variety of bus structures.
[0145] The electronic device 1200 may also communicate with one or more external devices 1240 (such as a keyboard, a pointing device, a Bluetooth device, etc.), may also communicate with one or more devices that enable a user to interact with the electronic device 1200, and / or may communicate with any device that enables the electronic device 1200 to communicate with one or more other computing devices (such as a router, a modem, etc.). Such communication may be carried out through an input / output (I / O) interface 1250. Moreover, the electronic device 1200 may also communicate with one or more networks (such as a local area network (LAN), a wide area network (WAN), and / or a public network, such as the Internet) through a network adapter 1260. As shown in the figure, the network adapter 1260 communicates with other modules of the electronic device 1200 through the bus 1230. It should be understood that although not shown in the figure, other hardware and / or software modules may be used in conjunction with the electronic device 1200, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems, etc.
[0146] Through the description of the above embodiments, those skilled in the art can easily understand that the example embodiments described herein can be implemented by software, or can be implemented by a combination of software and necessary hardware. Therefore, the technical solutions according to the embodiments of the present application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, a USB flash drive, a mobile hard disk, etc.) or on a network, including several instructions to enable a computing device (which can be a personal computer, a server, a terminal device, or a network device, etc.) to execute the method according to the embodiments of the present application.
[0147] In particular, according to an embodiment of the present application, the process described above with reference to the flowchart can be implemented as a computer program product, which includes: a computer program that, when executed by a processor, implements the above SQL statement generation method.
[0148] In an exemplary embodiment of the present application, there is also provided a computer-readable storage medium, which can be a readable signal medium or a readable storage medium. A program product capable of implementing the above method of the present application is stored on the computer-readable storage medium.
[0149] In some possible implementation manners, various aspects of the present application can also be implemented in the form of a program product, which includes program code. When the program product runs on a terminal device, the program code is used to cause the terminal device to execute the steps according to various exemplary embodiments of the present application described in the "Exemplary Method" section of this specification.
[0150] More specific examples of the computer-readable storage medium in the present application may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above.
[0151] In the present application, the computer-readable storage medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries the readable program code. Such a propagated data signal may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. The readable signal medium may also be any readable medium other than the readable storage medium, and the readable medium can send, propagate, or transmit a program used by or in combination with an instruction execution system, apparatus, or device.
[0152] Optionally, the program code included on the computer-readable storage medium can be transmitted by any suitable medium, including but not limited to wireless, wired, optical cable, RF, etc., or any suitable combination of the above.
[0153] In specific implementation, program code for performing the operations of the present application can be written in any combination of one or more programming languages. The programming languages include object-oriented programming languages such as Java, C++, etc., and also include conventional procedural programming languages such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computing device, partially on the user's device, executed as an independent software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server. In the case of a remote computing device, the remote computing device can be connected to the user's computing device through any type of network, including a local area network (LAN) or a wide area network (WAN), or can be connected to an external computing device (for example, by using an Internet service provider to connect through the Internet).
[0154] It should be noted that although several modules or units of the device for action execution are mentioned in the above detailed description, this division is not mandatory. In fact, according to the embodiments of the present application, the features and functions of the two or more modules or units described above can be embodied in one module or unit. Conversely, the features and functions of one module or unit described above can be further divided and embodied by multiple modules or units.
[0155] In addition, although the steps of the method in the present application are described in a specific order in the drawings, this does not require or imply that the steps must be executed in that specific order, or that all the steps shown must be executed to achieve the desired result. Additionally or alternatively, some steps can be omitted, multiple steps can be combined into one step for execution, and / or one step can be decomposed into multiple steps for execution, etc.
[0156] Through the description of the above embodiments, those skilled in the art can easily understand that the exemplary embodiments described herein can be implemented by software or by a combination of software and necessary hardware. Therefore, the technical solutions according to the embodiments of the present application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, a USB flash drive, a mobile hard disk, etc.) or on a network, including several instructions to enable a computing device (which can be a personal computer, a server, a mobile terminal, or a network device, etc.) to execute the method according to the embodiments of the present application.
[0157] Other embodiments of the present application will be readily contemplated by those skilled in the art after considering the specification and practicing the invention disclosed herein. The present application is intended to cover any variations, uses, or adaptations of the present application, which follow the general principles of the present application and include known common general knowledge or conventional technical means in the technical field not disclosed in the present application. The specification and examples are only regarded as exemplary, and the true scope and spirit of the present application are pointed out by the appended claims.
Claims
1. A method for generating a SQL statement, characterized in that: include: Get text data for operating the database; Obtaining a graph structure representation of the database, wherein nodes in the graph structure representation include tables in the database and fields in the tables; Constructing an operation intention graph of the text data, wherein the types of nodes in the operation intention graph include object nodes, each object node corresponds to an operation object or a condition object in the text data, and the type of each object node is an operation object node or a condition object node; Calculating the similarity between each object node and each node in the graph structure representation; Determine, according to the similarity, a node corresponding to each object node in the graph structure representation; When any object node corresponds to at least two nodes in the graph structure representation, determining a connection node closest to the any object node in the operation intention graph, wherein the type of the any object node is different from that of the connection node; When the connection node corresponds to a node in the graph structure representation, determining a relationship node among the at least two nodes that has a connection relationship with the node corresponding to the connection node in the graph structure representation; When the number of the relationship nodes is one, use the relationship node as a mapping node of any object node; When any of the object nodes corresponds to a node in the graph structure representation, taking the node corresponding to any of the object nodes in the graph structure representation as a mapping node; According to the mapping node of each object node, an SQL statement corresponding to the text data is generated.
2. The SQL statement generation method according to claim 1, characterized in that: The nodes in the operation intention graph include an object format, and the object format represents the data type corresponding to the object node; The nodes in the graph structure representation also include data types of the fields; the method further includes: When the number of the relationship nodes is multiple, determining the similarity and difference relationship between the data type of each relationship node and the object format of any object node to obtain multiple similarity and difference relationships; When there is one identical relationship among the plurality of identical and different relationships, the relationship node corresponding to the identical identical and different relationship is used as a mapping node of any object node.
3. The SQL statement generation method according to claim 2, characterized in that: Also includes: When there are at least two identical identical / unidentified relationships among the plurality of identical / unidentified relationships, determining a connection path between each relationship node corresponding to the identical identical / unidentified relationship and a node corresponding to the connection node in the graph structure representation, to obtain at least two connection paths; Get the type of each node in each connection path, where the type of each node in each connection path is a table node or a field node; Inputting the at least two connection paths and the types of each node in each connection path into a large language model, and generating a text description of each connection path by the large language model; The matching degree between each text description and the text data is calculated, and the relationship node corresponding to the text description with the highest matching degree is used as a mapping node of any object node.
4. The SQL statement generation method according to claim 1, characterized in that: Also includes: When the connection node corresponds to at least two nodes in the graph structure representation, respectively determining the connection status between each node corresponding to any one of the object nodes in the graph structure representation and the at least two nodes corresponding to the connection node in the graph structure representation; When the connection situation has a connection path, a node corresponding to any object node in the graph structure representation and on the connection path is used as a mapping node of the any object node.
5. The SQL statement generation method according to claim 4, characterized in that: The nodes in the operation intention graph include an object format, and the object format indicates the data type corresponding to the object node; the nodes in the graph structure representation also include the data type of the field; and the method further includes: When the connection situation includes multiple connection paths, the type of each node in each connection path is obtained, and the type of each node in each connection path is a table node or a field node; Inputting the plurality of connection paths and the type of each node in each connection path into a large language model, and generating a text description of each connection path by the large language model; The matching degree between each text description and the text data is calculated, and the relationship node corresponding to the text description with the highest matching degree is used as a mapping node of any object node.
6. The SQL statement generation method according to claim 1, characterized in that: The nodes in the operation intention graph include operation modes and conditions; The step of generating a SQL statement corresponding to the text data according to the mapping node of each object node includes: Verify the legality of the operation methods and conditions described in the database; When the legality verification is passed, an SQL statement corresponding to the text data is generated according to the mapping node of each object node.
7. A SQL statement generating device, characterized in that: include: A first acquisition module, used to acquire text data used to operate the database; A second acquisition module is used to acquire a graph structure representation of the database, where the nodes in the graph structure representation include tables in the database and fields in the tables; A construction module, for constructing an operation intention graph of the text data, wherein the types of nodes in the operation intention graph include object nodes, each object node corresponds to an operation object or a condition object in the text data, and the type of each object node is an operation object node or a condition object node; A calculation module, used for calculating the similarity between each object node and each node in the graph structure representation; A first determination module, configured to determine a node corresponding to each object node in the graph structure representation according to the similarity; A second determination module is used to determine, when any object node corresponds to at least two nodes in the graph structure representation, a connection node closest to the any object node in the operation intention graph, and the type of the any object node is different from that of the connection node; When the connection node corresponds to a node in the graph structure representation, determining a relationship node among the at least two nodes that has a connection relationship with the node corresponding to the connection node in the graph structure representation; When the number of the relationship nodes is one, use the relationship node as a mapping node of any object node; When any of the object nodes corresponds to a node in the graph structure representation, taking the node corresponding to any of the object nodes in the graph structure representation as a mapping node; The generating module is used to generate the SQL statement corresponding to the text data according to the mapping node of each object node.
8. An electronic device, characterized in that: include: processor; as well as A memory, configured to store executable instructions of the processor; The processor is configured to execute the SQL statement generation method according to any one of claims 1 to 6 by executing the executable instructions.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the SQL statement generating method according to any one of claims 1 to 6 is implemented.
10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the SQL statement generating method according to any one of claims 1 to 6 is implemented.
Citation Information
Patent Citations
Replay generation method, electronic equipment and computer storage medium
CN114996294A
Data query method and device, electronic equipment and storage medium
CN116795860A