Method and device for converting natural language into structured query language
By filtering and cropping historical SQL statements and generating new SQL statements, the problem of inaccurate SQL statements in NL to SQL technology is solved, and higher accuracy and completeness are achieved.
Patent Information
- Application Number
- CN202311785217.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Priority Date
- 2023-11-27
- Filing Date
- 2023-12-22
- Publication Date
- 2025-05-27
AI Technical Summary
When generating SQL statements, it is difficult to accurately express the user's intentions, and the user's failure to fully describe the problem, resulting in the generated SQL statements being inaccurate.
By obtaining the natural language NL statements entered by the user, filter out the matching collection of candidate SQL statements from multiple historical SQL statements, and crop and splice out new SQL statements to improve the accuracy of generating SQL statements.
Drawing on the accuracy and completeness of historical SQL statements, the generated SQL statements are more accurate and complete, solving the problem of unclear expression and incomplete description of the user's intention to input NL statements.
Smart Images

Figure CN120045576A_ABST
Abstract
Description
[0001] This application claims priority to the Chinese patent application filed with the State Intellectual Property Office of China on November 27, 2023, with application number 202311607944.0, and priority to the Chinese patent application entitled “NLtoSQL method and computing device based on historical SQL”, all contents of which are incorporated by reference in this application. Technical Field
[0002] The present application relates to the field of cloud computing, and more specifically, to a method, apparatus and computing device for converting natural language into structured query language. Background Art
[0003] Structured query language (SQL) is a database language with multiple functions such as data manipulation and data definition. This language is interactive and can provide great convenience for users. Natural language to structured query language (NL to SQL) is a natural language processing technology that can convert natural language instructions into SQL statements. It provides a more efficient interactive method, making it easier for SQL developers to develop SQL, and even allows ordinary users to use natural language instructions to operate on data in the database, making it easier for users to understand and use.
[0004] Although NL to SQL technology has achieved certain success, it is still far from being used in real business scenarios. For example, when users enter natural language NL, they cannot accurately express what they want, which will result in inaccurate SQL generated based on NL to SQL technology. For another example, when users enter natural language NL, it is difficult to fully describe the problem because they do not know the table content, which will result in inaccurate SQL generated based on NL to SQL technology.
[0005] Therefore, how to generate more accurate SQL statements in the NL to SQL process has become a technical problem that urgently needs to be solved. Summary of the invention
[0006] The present application provides a method for converting a natural language to a structured query language NL to SQL, which can improve the accuracy of a generated SQL statement during the NL to SQL process.
[0007] In a first aspect, a method for converting natural language to structured query language NL to SQL is provided, the method comprising: obtaining a first natural language NL statement input by a user; filtering a set of candidate SQL statements from multiple historical structured query language SQL statements based on the first NL statement, the candidate SQL statement set including at least one candidate historical SQL statement matching the first NL statement, the candidate historical SQL statement including some or all keywords in the first NL statement; generating a first SQL statement for expressing the first NL statement based on at least one candidate historical SQL statement included in the candidate SQL statement set; and displaying the first SQL statement to the user.
[0008] In the above technical solution, in the process of NL to SQL, firstly, historical SQL statements matching the NL statements input by the user are searched from multiple historical SQL statements, then SQL fragments related to the NL statements are cut out from these matching historical SQL statements, and finally a new SQL statement is spliced. In this way, the generated SQL statements draw on the historical SQL statements, and since the historical SQL statements are all generated SQL statements, the meanings they express are relatively accurate and complete, so the accuracy of the SQL statements generated in this way is greatly improved compared to the SQL statements directly converted from the NL statements input by the user in the prior art.
[0009] In combination with the first aspect, in certain implementations of the first aspect, based on the metadata of each historical SQL statement in the multiple historical SQL statements, at least one candidate historical SQL statement that matches the first NL statement is screened out from the multiple historical SQL statements, and the metadata of the candidate historical SQL statement includes some or all of the keywords in the first NL statement.
[0010] In combination with the first aspect, in some implementations of the first aspect, the metadata of each historical SQL statement includes metadata of each table involved in the historical SQL statement.
[0011] In combination with the first aspect, in certain implementations of the first aspect, the method also includes: obtaining the multiple historical SQL statements; obtaining metadata of a table involved in each of the historical SQL statements, the metadata of the table including a table name and a column name; and generating metadata for each of the historical SQL statements based on the metadata of the table involved in each of the historical SQL statements.
[0012] In the above technical solution, the metadata information of historical SQL statements is extracted and stored in the database to achieve a high degree of summary of historical SQL statements, that is, an abstract representation of historical SQL statements at the semantic level. In this way, in the process of NL to SQL based on historical SQL statements, since the metadata information after the summary or representation of historical SQL statements is stored in the database, the user does not need to enter a long question when entering the NL statement, but only needs to enter the keyword of the question.
[0013] In combination with the first aspect, in certain implementations of the first aspect, the candidate SQL statement set includes a first candidate historical SQL statement, the keywords in the first NL statement are all included in the first candidate historical SQL statement, and a SQL fragment for expressing the keywords in the first NL statement is cut out from the first candidate historical SQL statement; the first SQL statement is obtained based on the SQL fragment.
[0014] In combination with the first aspect, in certain implementations of the first aspect, the first SQL statement is obtained according to the conditional items in the first NL statement and the SQL fragments clipped from the first candidate historical SQL statement.
[0015] In combination with the first aspect, in certain implementations of the first aspect, the keywords in the first NL statement are included in at least two candidate historical SQL statements in the candidate SQL statement set, and SQL fragments for expressing the keywords in the first NL statement are respectively cut out from the at least two candidate historical SQL statements; the SQL fragments respectively cut out from the at least two candidate historical SQL statements are spliced to obtain spliced SQL fragments; and the first SQL statement is obtained based on the spliced SQL fragments.
[0016] In combination with the first aspect, in certain implementations of the first aspect, a keyword portion in the first NL statement is included in at least one candidate historical SQL statement in the candidate SQL statement set, and a first SQL fragment for expressing the first keyword in the first NL statement is obtained, and the first keyword in the first NL statement is not included in at least one candidate historical SQL statement in the candidate SQL statement set; SQL fragments for expressing the first NL statement are respectively cut out from the at least one candidate historical SQL statement; the first SQL fragment and the SQL fragment cut out from the at least one candidate historical SQL statement are spliced to obtain a spliced SQL fragment; and the first SQL statement is obtained based on the spliced SQL fragment.
[0017] In combination with the first aspect, in certain implementations of the first aspect, the first SQL fragment is generated according to a first model, input information of the first model includes the first keyword in the first NL statement, and output information of the first model includes the first SQL fragment.
[0018] In combination with the first aspect, in some implementations of the first aspect, the method further includes: displaying the candidate SQL statement set to the user.
[0019] In combination with the first aspect, in certain implementations of the first aspect, the method also includes: obtaining a second NL statement input by the user, the second NL statement being an NL statement obtained by the user after modifying the first NL statement according to at least one candidate historical SQL statement included in the candidate SQL statement set; generating a second SQL statement for expressing the second NL statement based on the second NL statement and at least one candidate historical SQL statement included in the candidate SQL statement set; and displaying the second SQL statement to the user.
[0020] In combination with the first aspect, in certain implementations of the first aspect, the method is applied to a cloud management platform, which is used to manage an infrastructure for providing cloud services, wherein the infrastructure includes at least one cloud data center, and each of the cloud data centers is provided with at least one server.
[0021] In a second aspect, a device for converting natural language into structured query language is provided, comprising an acquisition module, a screening module, a generation module, and a display module, wherein the acquisition module is used to acquire a first natural language NL statement input by a user; the screening module is used to filter out a set of candidate SQL statements from multiple historical structured query language SQL statements based on the first NL statement, the candidate SQL statement set including at least one candidate historical SQL statement matching the first NL statement, the candidate historical SQL statement containing some or all keywords in the first NL statement; the generation module is used to generate a first SQL statement for expressing the first NL statement based on at least one candidate historical SQL statement included in the candidate SQL statement set; and the display module is used to display the first SQL statement to the user.
[0022] In combination with the second aspect, in certain implementations of the second aspect, the screening module is specifically used to: based on the metadata of each historical SQL statement in the multiple historical SQL statements, screen out at least one candidate historical SQL statement that matches the first NL statement from the multiple historical SQL statements, and the metadata of the candidate historical SQL statement includes some or all of the keywords in the first NL statement.
[0023] In combination with the second aspect, in some implementations of the second aspect, the metadata of each historical SQL statement includes metadata of each table involved in the historical SQL statement.
[0024] In combination with the second aspect, in certain implementations of the second aspect, the acquisition module is also used to acquire the multiple historical SQL statements; the acquisition module is also used to acquire the metadata of the table involved in each of the historical SQL statements, and the metadata of the table includes the table name and the column name; the generation module is also used to generate the metadata of each of the historical SQL statements based on the metadata of the table involved in each of the historical SQL statements.
[0025] In combination with the second aspect, in certain implementations of the second aspect, the candidate SQL statement set includes a first candidate historical SQL statement, and the keywords in the first NL statement are all included in the first candidate historical SQL statement. The generation module is specifically used to: cut out an SQL fragment used to express the keywords in the first NL statement from the first candidate historical SQL statement; and obtain the first SQL statement based on the SQL fragment.
[0026] In combination with the second aspect, in some implementations of the second aspect, the generation module is specifically used to: obtain the first SQL statement according to the conditional items in the first NL statement and the SQL fragments cut out from the first candidate historical SQL statement.
[0027] In combination with the second aspect, in certain implementations of the second aspect, the keywords in the first NL statement are included in at least two candidate historical SQL statements in the candidate SQL statement set, and the generation module is specifically used to: respectively cut out SQL fragments used to express the keywords in the first NL statement from the at least two candidate historical SQL statements; splice the SQL fragments respectively cut out from the at least two candidate historical SQL statements to obtain spliced SQL fragments; and obtain the first SQL statement based on the spliced SQL fragments.
[0028] In combination with the second aspect, in certain implementations of the second aspect, the keyword portion in the first NL statement is included in at least one candidate historical SQL statement in the candidate SQL statement set, and the generation module is specifically used to: obtain a first SQL fragment for expressing a first keyword in the first NL statement, the first keyword in the first NL statement not included in at least one candidate historical SQL statement in the candidate SQL statement set; respectively cut out SQL fragments for expressing the first NL statement from the at least one candidate historical SQL statement; splice the first SQL fragment and the SQL fragment cut out from the at least one candidate historical SQL statement to obtain a spliced SQL fragment; and obtain the first SQL statement based on the spliced SQL fragment.
[0029] In combination with the second aspect, in certain implementations of the second aspect, the generation module is specifically used to: generate the first SQL fragment according to the first model, the input information of the first model includes the first keyword in the first NL statement, and the output information of the first model includes the first SQL fragment.
[0030] In combination with the second aspect, in some implementations of the second aspect, the display module is further used to display the candidate SQL statement set to the user.
[0031] In combination with the second aspect, in certain implementations of the second aspect, the acquisition module is further used to acquire a second NL statement input by the user, where the second NL statement is an NL statement obtained by the user after modifying the first NL statement according to at least one candidate historical SQL statement included in the candidate SQL statement set; the generation module is further used to generate a second SQL statement for expressing the second NL statement based on the second NL statement and at least one candidate historical SQL statement included in the candidate SQL statement set; and the display module is further used to display the second SQL statement to the user.
[0032] In combination with the second aspect, in certain implementations of the second aspect, the device is applied to a cloud management platform, which is used to manage an infrastructure for providing cloud services, wherein the infrastructure includes at least one cloud data center, and each of the cloud data centers is provided with at least one server.
[0033] In a third aspect, a computing device is provided, comprising a processor and a memory, and optionally, an input / output interface, wherein the processor is used to control the input / output interface to send and receive information, the memory is used to store a computer program, and the processor is used to call and run the computer program from the memory, so that the method in the first aspect or any possible implementation of the first aspect is executed.
[0034] Optionally, the processor may be a general-purpose processor, which may be implemented by hardware or software. When implemented by hardware, the processor may be a logic circuit, an integrated circuit, etc.; when implemented by software, the processor may be a general-purpose processor implemented by reading software code stored in a memory, which may be integrated in the processor or located outside the processor and exist independently.
[0035] In a fourth aspect, a computing device cluster is provided, comprising at least one computing device, each computing device comprising a processor and a memory; the processor of the at least one computing device is used to execute instructions stored in the memory of the at least one computing device, so that the computing device cluster executes the method in the first aspect or any possible implementation of the first aspect.
[0036] In a fifth aspect, a chip is provided, which obtains instructions and executes the instructions to implement the method in the above-mentioned first aspect and any implementation manner of the first aspect.
[0037] Optionally, as an implementation manner, the chip includes a processor and a data interface, and the processor reads instructions stored in the memory through the data interface to execute the method in the above-mentioned first aspect and any implementation manner of the first aspect.
[0038] Optionally, as an implementation method, the chip may also include a memory, in which instructions are stored, and the processor is used to execute the instructions stored in the memory. When the instructions are executed, the processor is used to execute the method in the first aspect and any one of the implementation methods of the first aspect.
[0039] In a sixth aspect, a computer program product comprising instructions is provided. When the instructions are executed by a computing device, the computing device executes the method in the first aspect and any one of the implementations of the first aspect.
[0040] In a seventh aspect, a computer program product comprising instructions is provided. When the instructions are executed by a computing device cluster, the computing device cluster executes the method in the first aspect and any one of the implementations of the first aspect.
[0041] In an eighth aspect, a computer-readable storage medium is provided, comprising computer program instructions. When the computer program instructions are executed by a computing device, the computing device executes the method in the above-mentioned first aspect and any one of the implementations of the first aspect.
[0042] By way of example, these computer-readable storages include, but are not limited to, one or more of the following: read-only memory (ROM), programmable ROM (PROM), erasable PROM (EPROM), Flash memory, electrically EPROM (EEPROM), and hard drive.
[0043] Optionally, as an implementation manner, the above-mentioned storage medium may specifically be a non-volatile storage medium.
[0044] In a ninth aspect, a computer-readable storage medium is provided, comprising computer program instructions. When the computer program instructions are executed by a computing device cluster, the computing device cluster executes the method in the first aspect and any one of the implementations of the first aspect.
[0045] By way of example, these computer-readable storages include, but are not limited to, one or more of the following: read-only memory (ROM), programmable ROM (PROM), erasable PROM (EPROM), Flash memory, electrically EPROM (EEPROM), and hard drive.
[0046] Optionally, as an implementation manner, the above-mentioned storage medium may specifically be a non-volatile storage medium. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] Figure 1 It is a schematic block diagram of a cloud scenario applicable to an embodiment of the present application.
[0048] Figure 2 It is a schematic flow chart of a method for converting natural language into structured language provided in an embodiment of the present application.
[0049] Figure 3 It is a schematic flowchart of a method for obtaining the schema of historical SQL statements provided in an embodiment of the present application.
[0050] Figure 4 It is a schematic flowchart of another method for converting natural language into structured query language provided in an embodiment of the present application.
[0051] Figure 5 It is a schematic block diagram of a device 500 for converting natural language into structured language provided in an embodiment of the present application.
[0052] Figure 6 It is a schematic diagram of the architecture of a computing device 1500 provided in an embodiment of the present application.
[0053] Figure 7 It is a schematic diagram of the architecture of a computing device cluster provided in an embodiment of the present application.
[0054] Figure 8 It is a schematic diagram of the connection between computing devices 1500A and 1500B via a network provided in an embodiment of the present application. DETAILED DESCRIPTION
[0055] The technical solution in this application will be described below in conjunction with the accompanying drawings.
[0056] The present application will present various aspects, embodiments or features around a system including multiple devices, components, modules, etc. It should be understood and appreciated that each system may include additional devices, components, modules, etc., and / or may not include all devices, components, modules, etc. discussed in conjunction with the figures. In addition, combinations of these schemes may also be used.
[0057] In addition, in the embodiments of the present application, words such as "exemplary" and "for example" are used to indicate examples, illustrations or explanations. Any embodiment or design described as "exemplary" in the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of the word "exemplary" is intended to present concepts in a concrete way.
[0058] In the embodiments of the present application, "corresponding (corresponding, relevant)" and "corresponding (corresponding)" can sometimes be used interchangeably. It should be pointed out that when the distinction between them is not emphasized, the meanings they intend to express are consistent.
[0059] The business scenarios described in the embodiments of the present application are intended to more clearly illustrate the technical solutions of the embodiments of the present application, and do not constitute a limitation on the technical solutions provided in the embodiments of the present application. A person of ordinary skill in the art can appreciate that, with the evolution of network architecture and the emergence of new business scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.
[0060] References to "one embodiment" or "some embodiments" etc. described in this specification mean that a particular feature, structure or characteristic described in conjunction with the embodiment is included in one or more embodiments of the present application. Thus, the phrases "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. that appear at different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized in other ways.
[0061] In the present application, "at least one" means one or more, and "plurality" means two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can mean: including the existence of A alone, the existence of A and B at the same time, and the existence of B alone, where A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following items" or similar expressions refers to any combination of these items, including any combination of single items or plural items. For example, at least one of a, b, or c can mean: a, b, c, ab, ac, bc, or abc, where a, b, c can be single or multiple.
[0062] For ease of description, the terms involved in the embodiments of the present application are explained below.
[0063] 1. Structured query language (SQL)
[0064] SQL is a database language with multiple functions such as data manipulation and data definition. This language is interactive and can provide great convenience for users.
[0065] As an example, the type of SQL may include, but is not limited to: ordinary SQL, virtual SQL, quasi-SQL, etc.
[0066] Ordinary SQL is a standard database query language used to retrieve, manipulate, and modify data from a database. It supports a variety of data operations, including inserting, updating, deleting, and querying data, as well as creating, modifying, and deleting table structures. Ordinary SQL is a procedural language that requires specific operation steps to be specified, and is suitable for small to medium-sized database systems.
[0067] Virtual SQL cannot be executed directly in the strict sense, and must be interactively configured with the input of the front-end business system before it can be executed. For example, when the user's SQL code is associated with the upper-level business system, a virtual SQL code may be formed.
[0068] SQL-like is a query language similar to SQL, but it does not fully comply with the SQL standard. SQL-like is usually used in some specific applications or frameworks. It has similar syntax and query semantics to SQL, but may add some customized syntax and functions. For example, scenarios where SQL-like is used include but are not limited to: graph databases, time series databases, spatiotemporal databases, etc.
[0069] 2. Natural language to structured query language (NL to SQL)
[0070] With the development of IT and digital economy, the amount and types of data have increased dramatically, and querying data has become one of the daily tasks of many people. However, for non-professionals, querying data is often a tedious task. The traditional SQL query language requires learning professional SQL language and knowledge, which is difficult for ordinary users to use.
[0071] In order to solve the above problems, NL to SQL technology came into being. NL to SQL is a natural language processing technology that can convert natural language instructions into SQL statements. It provides a more efficient interactive method, making it easier for SQL developers to develop SQL, and even allows ordinary users to use natural language instructions to operate data in the database, making it easier for users to understand and use. In addition, NL to SQL technology can also improve the efficiency of data query. Traditional SQL query language requires users to enter accurate syntax and structure, while NL to SQL technology can not only recognize the user's intention, but also automatically generate SQL statements that meet the requirements. Therefore, NL to SQL technology can also improve the efficiency of data query and has great application prospects and promotion value.
[0072] NL to SQL technology can be applied in a variety of fields. Here are a few examples.
[0073] For example, NL to SQL technology can be applied in the field of business intelligence. Enterprises can use NL to SQL technology to query data. This technology can help enterprises quickly obtain the required data, so as to better understand customer needs and make appropriate decisions.
[0074] Another example is that NL to SQL technology can also be applied in the field of data development and analysis. Enterprises can use NL to SQL technology to carry out various data development and analysis tasks. This technology can help enterprises to mine valuable data information from massive data, so that data can generate greater value for enterprises.
[0075] Another example is that NL to SQL technology can also be used in the field of human-computer interaction. Users can apply NL to SQL technology to home smart systems, allowing users to interact with smart devices using natural language and control home devices more conveniently.
[0076] Another example is that NL to SQL technology can also be applied in the field of database management. NL to SQL technology can be used for database management. Database administrators can use NL to SQL technology to better manage databases, thereby performing query and update operations more quickly.
[0077] The research on NL to SQL technology began in the 1990s, and many researchers have been committed to improving the accuracy and reliability of natural language queries. In recent years, with the advancement of deep learning and natural language processing technology, researchers have achieved remarkable results in this field. Large language model (LLM) is a generative neural network technology that can learn and generate natural language text from a large corpus. It uses a model with tens of billions or even hundreds of billions of parameters. Through learning from a large corpus, it has produced results that are significantly better than traditional methods in various NLP task fields, which is called "emergence". The essence of LLM is to train a model with a large parameter scale through the learning of massive data, thereby obtaining the ability of general artificial intelligence. LLM has been widely used in language generation, machine translation, question-answering systems, text classification and other fields, and has also become a hot spot in the field of NL to SQL. Due to LLM's excellent ability in understanding user intent, logical reasoning, and common sense, researchers frequently use LLM to refresh the NL to SQL competition list.
[0078] The large language model LLM may include but is not limited to: Seq to Seq (also known as Seq2Seq) model, SQLNet model, and Spider model.
[0079] The Seq to Seq (also known as Seq2Seq) model is one of the earliest widely used NL to SQL models and is also the mainstream model of NL to SQL. Seq to Seq uses an encoder to map natural language queries and table information into a common high-dimensional space, matches and fuses the information of the two, and encodes them into a vector. Then, a decoder is used to convert the vector representation into the final SQL statement. Due to the powerful representation ability of the encoder-decoder structure, the Seq to Seq model occupies a very prominent position in NL to SQL technology.
[0080] The SQLNet model is an improved algorithm based on the Seq2Seq model. It enhances the performance of the Seq2Seq model by introducing an SQL intermediate representation. The SQLNet model maps natural language queries and SQL queries to SQL intermediate representations, and then converts them into SQL queries. The SQL intermediate representation includes query columns, query tables, query conditions, query connections, and other parts. It enables the model to learn the mapping relationship between natural language queries and SQL queries more accurately. Since some operations in the SQL intermediate, such as join, generally do not have corresponding field descriptions in natural language descriptions, the addition of the intermediate layer can temporarily avoid some special operations in the SQL grammar, give priority to mapping the natural language to the SQL intermediate layer, and then rely on the SQL grammar structure to generate the final executable SQL statement from the SQL intermediate layer. Through the introduction of the intermediate layer, the intermediate representation of the SQLNet model has a higher degree of abstraction, so it can capture more complex query semantics, thereby improving the overall NL to SQL accuracy.
[0081] The Spider model is a more advanced Seq2Seq model that is optimized for complex SQL queries. Unlike other text-to-SQL algorithms, the Spider model can convert not only simple SQL queries, but also complex query types including joins, aggregations, nested subqueries, etc. The key to the Spider model is the introduction of database schema information, that is, the tables in the database and the relationships between them. The model converts natural language queries and database schema information by mapping them into a joint space, which enables the model to understand and obtain better SQL generation.
[0082] Although the NL to SQL technology based on LLM has achieved certain success, it is still far from being used in real business scenarios. The following are some real business scenarios that may cause problems when using the NL to SQL technology.
[0083] 1. When users input natural language NL, they cannot accurately express what they want, which results in inaccurate SQL generated based on NL to SQL technology.
[0084] For example, in a specific industry, it is very difficult to distinguish semantically between words, and professional domain knowledge is often required to distinguish them. For example, the types of life insurance in the insurance industry include ordinary life insurance, annuity insurance, simple life insurance, group life insurance, dividend insurance, investment-linked insurance, universal insurance, and so on. When a user enters "basic life insurance data" for query, it is difficult for the model to distinguish whether it is "ordinary life insurance" or "simple life insurance", resulting in the SQL generated based on NL to SQL technology not being very accurate. Therefore, it is not easy for users to accurately express what they think in their hearts. Sometimes, the problems described by users through natural language NL are ambiguous or even wrong.
[0085] 2. When users input natural language NL, it is difficult for them to fully describe the problem because they do not know the table content, which will cause the SQL generated based on NL to SQL technology to be inaccurate.
[0086] For example, the question a user enters through the natural language NL is: Count the number of people whose salary is greater than 5000. After looking at the table in the database, it is found that salary includes basic salary, performance salary, and labor allowance, so the question needs to be changed to: Count the number of people whose sum of basic salary, performance salary, and labor allowance is greater than 5000.
[0087] In summary, if you want to write an accurate and complete SQL, you need to understand the relevant table information. However, in business scenarios, users may not know which table they need or where to get it, which leads to inaccurate and incomplete SQL generated by LLM-based NL to SQL technology.
[0088] In view of this, an embodiment of the present application provides a method for converting natural language to structured language NL to SQL, which can generate new SQL based on historical SQL, so that the generated SQL is more accurate and more complete.
[0089] It should be understood that the method provided in the embodiments of the present application can be applied to scenarios such as business intelligence and data development & analysis, human-computer interaction, home intelligent systems, database management, etc., or can also be applied to cloud service scenarios, and the embodiments of the present application do not make specific limitations on this.
[0090] In one example, the method provided in the embodiment of the present application is applied to a cloud service scenario, and the method is executed by a cloud management platform in the cloud service scenario. Figure 1 , describe the cloud service scenarios in detail.
[0091] Figure 1is a schematic block diagram of a cloud scenario applicable to an embodiment of the present application. Figure 1 As shown, the cloud scenario may include: a cloud management platform 110 , the Internet 120 , and a client 130 .
[0092] like Figure 1 As shown, the cloud management platform 110 is used to manage the infrastructure that provides multiple cloud services. The infrastructure includes multiple cloud data centers, each of which includes multiple servers, each of which includes cloud service resources to provide corresponding cloud services for tenants.
[0093] The cloud management platform 110 may be located in a cloud data center, which may provide an access interface (such as an interface or an application program interface (API)). The tenant may operate the client 130 to remotely access the access interface to register a cloud account and password on the cloud management platform 110, and log in to the cloud management platform 110. After the cloud management platform 110 successfully authenticates the cloud account and password, the tenant may further pay to select and purchase a virtual machine of specific specifications (processor, memory, disk) on the cloud management platform 110. After the paid purchase is successful, the cloud management platform 110 provides the remote login account and password of the purchased virtual machine, and the client 130 may remotely log in to the virtual machine, install and run the tenant's application in the virtual machine. Therefore, the tenant may create, manage, log in and operate a virtual machine in the cloud data center through the cloud management platform 110. Among them, the virtual machine may also be called a cloud server (elastic compute service, ECS) or an elastic instance (different cloud service providers have different names).
[0094] It should be understood that tenants of cloud services can be individuals, enterprises, schools, hospitals, administrative agencies, etc.
[0095] The functions of the cloud management platform 110 include, but are not limited to, user console, computing management service, network management service, storage management service, authentication service, and image management service. The user console provides an interface or API to interact with tenants, the computing management service is used to manage servers running virtual machines and containers and bare metal servers, the network management service is used to manage network services (such as gateways, firewalls, etc.), the storage management service is used to manage storage services (such as data bucket services), the authentication service is used to manage tenant accounts and passwords, and the image management service is used to manage virtual machine images. Tenants can use the client 130 to log in to the cloud management platform 110 through the Internet 120 to manage the rented cloud services.
[0096] Combine the following Figure 2 , a method for converting natural language into structured language provided in an embodiment of the present application is described in detail. It should be understood that Figure 2The examples are only intended to help those skilled in the art understand the embodiments of the present application, and are not intended to limit the embodiments of the present application to Figure 2 The specific numerical values or specific scenarios shown in the examples. Figure 2 The examples given below are obviously susceptible to various equivalent modifications or changes, and such modifications and changes also fall within the scope of the embodiments of the present application.
[0097] Figure 2 is a schematic flow chart of a method for converting natural language into structured language provided in an embodiment of the present application. Figure 2 As shown, the method may include steps 210-240, and steps 210-240 are described in detail below.
[0098] Step 210: Obtain a first natural language NL sentence input by a user.
[0099] In an embodiment of the present application, a user may upload a first natural language NL sentence through an application programming interface (API), and the first NL sentence may also be referred to as a first NL question. In one example, a user may input the first natural language NL sentence through an API of a cloud management platform. In another example, a user may also input the first natural language NL sentence through an API of an NL to SQL device or an NL to SQL system, which is not specifically limited in the embodiment of the present application.
[0100] It should be understood that NL to SQL devices may include but are not limited to: data development & analysis devices, human-computer interaction devices, etc. NL to SQL systems may include but are not limited to: SQL query systems such as databases / data warehouses.
[0101] Step 220: Filter out a set of candidate SQL statements from a plurality of historical structured query language SQL statements according to the first NL statement.
[0102] In an embodiment of the present application, after obtaining a first natural language NL statement input by a user, at least one candidate historical SQL statement that matches the first NL statement can be screened out from multiple historical structured query language SQL statements, and these at least one candidate historical SQL statement that matches the first NL statement constitute a candidate SQL statement set.
[0103] It should be understood that at least one candidate historical SQL statement that matches the first NL statement refers to a candidate historical SQL statement that contains some or all of the keywords in the first NL statement. That is, the candidate historical SQL statement may contain all or some of the keywords in the first NL statement, and the embodiment of the present application does not specifically limit this.
[0104] In a possible implementation, at least one candidate historical SQL statement matching the first NL statement may be screened out from the multiple historical SQL statements based on metadata of each historical SQL statement in the multiple historical SQL statements.
[0105] The metadata of the historical SQL statements can also be called the schema of the historical SQL statements (or simply SQL schema). The schema of the historical SQL statements can include the metadata of one or more tables involved in the historical SQL statements (also called table schema, or table schema). Figure 3 A specific implementation example in the text describes in detail how to obtain the schema of historical SQL statements, which will not be described in detail here.
[0106] Step 230: Generate a first SQL statement for expressing a first NL statement according to at least one candidate historical SQL statement included in the candidate SQL statement set.
[0107] In the embodiment of the present application, after obtaining a candidate SQL statement set corresponding to the first NL statement, a first SQL statement for expressing the first NL statement may be generated according to at least one historical candidate SQL statement included in the candidate SQL statement set. Several possible implementations are described below.
[0108] In one implementation, the key fields in the first NL statement are included in a candidate historical SQL statement in the candidate SQL statement set. That is, a candidate historical SQL statement in the candidate SQL statement set can completely include all the key fields parsed from the first NL statement. In this implementation, SQL fragments used to express the key fields in the first NL statement can be cut out from the candidate historical SQL statement, and these cut SQL fragments constitute the first SQL statement used to express the first NL statement.
[0109] Optionally, in some embodiments, if the filtering condition of where in the candidate historical SQL statement containing all key fields in the first NL statement is inconsistent with the filtering condition (also referred to as condition item) in the first NL statement, the filtering condition in the first NL statement can also be automatically configured to the SQL fragment cut out from this candidate historical SQL statement to obtain the first SQL statement used to express the first NL statement.
[0110] In another implementation, the key field in the first NL statement is included in at least two candidate historical SQL statements in the candidate SQL statement set. That is, the key field parsed in the first NL statement is included in at least two candidate historical SQL statements in the candidate SQL statement set. In this implementation, SQL fragments for expressing the key field in the first NL statement can be cut out from at least two candidate historical SQL statements, respectively, and the cut SQL fragments are spliced to obtain spliced SQL fragments, and these spliced SQL fragments constitute the first SQL statement for expressing the first NL statement.
[0111] Optionally, in some embodiments, if the filtering conditions of where in at least two candidate historical SQL statements included in the first NL statement are inconsistent with the filtering conditions (also referred to as conditional items) in the first NL statement, the filtering conditions in the first NL statement can also be automatically configured to the SQL fragments trimmed and spliced from the at least two candidate historical SQL statements to obtain the first SQL statement used to express the first NL statement.
[0112] In another implementation, the key field part in the first NL statement is included in at least one candidate historical SQL statement in the candidate SQL statement set, and the rest does not exist in any candidate historical SQL statement. In this implementation, it is necessary to generate SQL fragments (new SQL fragments) for expressing the fields in the first NL statement that are not included in the candidate historical SQL statements based on the model technology, and cut out SQL fragments for expressing the fields in the first NL statement from at least one candidate historical SQL statement, splice the above new SQL fragments and the SQL fragments cut out from the at least one candidate historical SQL statement to obtain spliced SQL fragments, and these spliced SQL fragments constitute the first SQL statement for expressing the first NL statement.
[0113] For example, the new SQL fragment can be generated according to the first model, the input information of the first model includes the fields in the first NL statement that are not included in the historical SQL statement, and the output information of the first model includes the new SQL fragment. Specifically, in a possible implementation, the first NL to SQL model at a remote end (for example, the cloud) can be called, and the fields in the uploaded first NL statement that are not included in the historical SQL statement are used as the input of the first NL to SQL model to obtain the new SQL fragment output by the first NL to SQL model.
[0114] As an example, the first NL to SQL model may be a large language model LLM provided by a third-party service provider. For specific instructions on the large language model LLM, please refer to the above description, which will not be repeated here.
[0115] Step 240: Display the generated first SQL statement to the user.
[0116] It should be noted that the SQL statements mentioned in the embodiments of the present application (for example, the first SQL statement, the historical SQL statement, etc.) can be ordinary SQL, or can also be virtual SQL, or quasi-SQL, and the present application does not make specific limitations on this.
[0117] In an embodiment of the present application, after obtaining the first SQL statement for expressing the first NL statement, the first SQL statement can be displayed to the user. Specifically, the user can obtain the first SQL statement through an API. In one example, the user can obtain the first SQL statement through an API of a cloud management platform. In another example, the user can also obtain the first SQL statement through an API of an NL to SQL device or an NL to SQL system, which is not specifically limited in the embodiment of the present application.
[0118] In the above technical solution, the matching historical SQL statements are determined according to the NL statements input by the user, and the SQL statements corresponding to the NL statements input by the user are generated according to the historical SQL statements. Since the historical SQL statements are all SQL statements that have been generated, the meanings they express are relatively accurate and complete. Therefore, the SQL statements corresponding to the NL statements input by the user generated based on the historical SQL statements will also be relatively complete and accurate, thus avoiding the problem that the generated SQL is not very accurate due to the inaccurate and incomplete meanings of the NL statements input by the user during the NL to SQL process, and improving the accuracy and completeness of the SQL statements generated during the NL to SQL process.
[0119] Combine the following Figure 3 , describes in detail an implementation method of how to obtain the schema of historical SQL statements. It should be understood that Figure 3 The examples are only intended to help those skilled in the art understand the embodiments of the present application, and are not intended to limit the embodiments of the present application to Figure 3 The specific numerical values or specific scenarios shown in the examples. Figure 3 The examples given below are obviously susceptible to various equivalent modifications or changes, and such modifications and changes also fall within the scope of the embodiments of the present application.
[0120] It should be understood that Figure 3 The schema of historical SQL statements obtained can also be understood as a representation of historical SQL statements at an abstract semantic level by highly summarizing the historical SQL statements. In this way, the problem of the NL statement that the user needs to input being too long due to the SQL statement being too long can be avoided.
[0121] Figure 3 FIG. 1 is a schematic flow chart of a method for obtaining the schema of historical SQL statements provided in an embodiment of the present application. Figure 3 As shown, the method may include steps 310-340, and steps 310-340 are described in detail below.
[0122] Step 310: parse the historical SQL statements to obtain semantic information and table information contained in the historical SQL statements.
[0123] In an embodiment of the present application, historical SQL statements can be obtained and parsed to obtain semantic information and table information contained in the historical SQL statements. The table information may include, for example, the table name of the original table involved in the historical SQL statements.
[0124] Step 320: Generate the schema (SQL schema) of the historical SQL statements according to the semantic information and table information contained in the historical SQL statements.
[0125] In an embodiment of the present application, after parsing the semantic information and table information contained in the historical SQL statements, the schema (table schema) of the corresponding table can be found according to the table name by looking up the table, for example, by means of an inverted index, and these schemas (table schema) of the corresponding tables are combined into a SQL schema.
[0126] It should be understood that the inverted index originates from the need to find records based on attribute values in practical applications. Each item in this index table includes an attribute value and the address of each record with the attribute value. Since the attribute value is not determined by the record, but the location of the record is determined by the attribute value, it is called an inverted index. We call the file with an inverted index an inverted index file, or inverted file for short.
[0127] It should also be understood that table schema can also be called metadata of a table, which can include but is not limited to: table name, column name, build time, data type, view, primary key, foreign key, etc. SQL schema can also be called metadata of a SQL statement, which includes the table schema of the table involved in the SQL statement.
[0128] Step 330: vectorize the SQL schema and store it in a vector database.
[0129] In the embodiment of the present application, the SQL schema can be vectorized, and a vectorized dense index can be established and stored in a vector database. In one example, the SQL schema can be vectorized by vector embedding technology and stored in a vector database.
[0130] It should be understood that vector embedding is a technology that converts data such as text, audio, and video into vectors. These vectors can capture the semantic and contextual information in the data and can be used to compare the similarity between different entities. Vector embedding is a commonly used technology in fields such as natural language processing and recommendation systems.
[0131] Step 340: Perform inverted indexing on the SQL schema and store it in an inverted index database.
[0132] In the embodiment of the present application, the SQL schema may be inverted indexed and stored in an inverted index database.
[0133] It should be understood that the inverted index database may also be referred to as the index database. The index database and the vector database may be the same database or different databases, which is not specifically limited in this application.
[0134] It should be noted that Figure 3In the embodiment of the present invention, step 330 may be executed without executing step 340, or step 340 may be executed without executing step 330, or step 330 and step 340 may be executed. If step 330 and step 340 are executed, the embodiment of the present application does not specifically limit the execution order, and step 330 may be executed first, and then step 340, or step 340 may be executed first, and then step 330, or step 330 and step 340 may be executed simultaneously.
[0135] In the above technical solution, in order to avoid the problem that the NL statement that the user needs to input is too long due to the SQL statement being too long, the metadata information (SQL schema information) of the historical SQL statements is extracted and stored in the database, so as to achieve a high degree of summary of the historical SQL statements, that is, an abstract representation of the historical SQL statements at the semantic level. In this way, in the process of NL to SQL based on the historical SQL statements, the user does not need to input a long question when inputting the NL statement, but only needs to input the keywords of the question, because the metadata information after the historical SQL statements are summarized or represented is stored in the database.
[0136] It should be noted that Figure 3 The process of obtaining the schema of historical SQL statements shown is an offline process. Generally, the semantic representation of historical SQL statements is performed once every certain period of time.
[0137] Combine the following Figure 4 , a specific implementation process of the method for converting natural language to structured query language provided in the embodiment of the present application is described in detail. It should be understood that Figure 4 The examples are only intended to help those skilled in the art understand the embodiments of the present application, and are not intended to limit the embodiments of the present application to Figure 4 The specific numerical values or specific scenarios shown in the examples. Figure 4 The examples given below are obviously susceptible to various equivalent modifications or changes, and such modifications and changes also fall within the scope of the embodiments of the present application.
[0138] Figure 4 FIG. 1 is a schematic flow chart of another method for converting natural language into structured query language provided in an embodiment of the present application. Figure 4 As shown, the method may include steps 410-470, and steps 410-470 are described in detail below.
[0139] Step 410: Obtain the NL question input by the user.
[0140] It should be understood that NL questions can also be referred to as NL sentences.
[0141] Step 420: Vectorize the NL problem.
[0142] In an embodiment of the present application, the NL problem may be vectorized. For example, the NL problem may be vectorized by using vector embedding technology.
[0143] Step 430: Search the candidate historical SQL set matching the NL question by querying the index database and / or the vector database.
[0144] In the embodiment of the present application, the NL problem can be vectorized and queried. Figure 3 The vector database in the NL question is used to filter out candidate historical SQL sets that are similar or matching to the NL question. Alternatively, you can also query based on the NL question. Figure 3 The index database in is used to filter out candidate historical SQL sets that are similar to or match the NL problem from multiple historical SQLs.
[0145] Step 440: Provide the final ranking result of candidate historical SQLs through a sophisticated comparison technique.
[0146] In the embodiment of the present application, the candidate SQLs included in the candidate historical SQL set can be sorted and recommended to the user through fine comparison technology such as large models.
[0147] In the above steps 410-440, the search of historical SQL is used to solve the problem that the user cannot correctly express what he / she wants. By searching and clarifying the historical SQL, the user can accurately describe the problem in his / her mind. For example, when the user searches for life insurance, the front end can recommend multiple candidate historical SQLs including universal life insurance, annuity insurance, simple life insurance, group life insurance, etc. The user can find that "universal life insurance" is what he / she really wants to query from multiple candidate historical SQLs, so the user can use "universal life insurance" to describe his / her real needs.
[0148] Step 450: Find the field in the NL question that corresponds to the SQL Schema of the candidate historical SQL.
[0149] In the embodiment of the present application, the NL question input by the user is parsed at a fine-grained level to find out the fields in the NL question that correspond to the SQL Schema of the candidate historical SQL. These fields become the keywords in the SQL corresponding to the output NL question.
[0150] Step 460: Automatically match the conditional items in the NL question to the corresponding candidate historical SQL, thereby generating the SQL corresponding to the NL question.
[0151] In an embodiment of the present application, the NL question input by the user will be parsed to obtain the condition item (also called filter word) in the NL question, and the condition item will be automatically matched to the corresponding candidate historical SQL. For example, the condition item will become the filter condition of Where in the corresponding candidate historical SQL, thereby generating the SQL corresponding to the NL question.
[0152] It should be understood that generating SQL corresponding to the NL question refers to generating SQL for expressing the NL question.
[0153] In the embodiment of the present application, there are multiple ways to generate the SQL corresponding to the NL problem based on the corresponding candidate historical SQL. Several possible situations are introduced below.
[0154] For example, the key field parsed from the NL question (the field corresponding to the SQL Schema of the candidate historical SQL) can be completely included in the SQL Schema field of a certain historical SQL. In this case, you only need to trim the historical SQL and automatically add the conditional items in the NL question to get the SQL corresponding to the NL question.
[0155] Another example, the key field parsed from the NL question (the field corresponding to the SQL Schema of the candidate historical SQL) is included in the SQL Schema fields of more than two historical SQLs. In this case, you need to trim the two or more historical SQLs first, then splice the trimmed SQL fragments together, and automatically match the condition items in the NL question to get the SQL corresponding to the NL question.
[0156] Another example, the key fields parsed from the NL question (the fields corresponding to the SQL Schema of the candidate historical SQL) partially exist in the SQL Schema fields of the historical SQL, and partially do not exist in the SQL Schema fields of any historical SQL. In this case, it is necessary to use model technology to generate new SQL fragments for these fields, and then splice them with the SQL fragments that have been trimmed from the historical SQL containing the key fields in the NL question. After automatically matching the conditional items in the NL question, you can get the SQL corresponding to the NL question.
[0157] Step 470: Optimize the SQL corresponding to the NL problem.
[0158] In an embodiment of the present application, after obtaining the SQL corresponding to the NL problem based on the historical SQL, the SQL corresponding to the NL problem can also be optimized. For example, redundant codes in the SQL corresponding to the NL problem can be removed, which can improve the execution efficiency of the SQL corresponding to the NL problem.
[0159] The above steps 450 to 470 are used to describe the SQL generation process, and new SQL is generated by cutting and splicing historical SQL data. In this way, the user may no longer need to understand the relevant tables, and can directly reuse the SQL operations of the previous users on the same table. This avoids the problem of inaccurate and incomplete description of the problem due to the user not knowing the table content.
[0160] It should be noted that Figure 4 The natural language to structured query language conversion method shown is an online process and a running state. Users will frequently interact with the cloud management platform, NL to SQL device, or NL to SQL system through the API interface.
[0161] Combination of the above Figures 1 to 4 , describes in detail the method provided by the embodiment of the present application, and will be combined with Figure 5-Figure 8 , describes in detail the embodiment of the device of the present application. It should be understood that the description of the method embodiment corresponds to the description of the device embodiment, so the parts not described in detail can refer to the previous method embodiment.
[0162] Figure 5 1 is a schematic block diagram of a device 500 for converting natural language into structured query language provided in an embodiment of the present application. The device 500 can be implemented by software, hardware, or a combination of both. The device 500 provided in an embodiment of the present application can implement the embodiment of the present application. Figure 2-Figure 4 According to the method flow shown, the device 500 includes: an acquisition module 510, a screening module 520, a generation module 530, and a display module 540, wherein the acquisition module 510 is used to acquire a first natural language NL statement input by a user; the screening module 520 is used to screen out a candidate SQL statement set from multiple historical structured query language SQL statements according to the first NL statement, and the candidate SQL statement set includes at least one candidate historical SQL statement matching the first NL statement, and the candidate historical SQL statement includes some or all keywords in the first NL statement; the generation module 530 is used to generate a first SQL statement for expressing the first NL statement according to at least one candidate historical SQL statement included in the candidate SQL statement set; and the display module 540 is used to display the first SQL statement to the user.
[0163] Optionally, the filtering module 520 is specifically used to: filter out at least one candidate historical SQL statement that matches the first NL statement from the multiple historical SQL statements based on the metadata of each historical SQL statement in the multiple historical SQL statements, and the metadata of the candidate historical SQL statement includes some or all of the keywords in the first NL statement.
[0164] Optionally, the metadata of each historical SQL statement includes metadata of each table involved in the historical SQL statement.
[0165] Optionally, the acquisition module 510 is also used to acquire the multiple historical SQL statements; the acquisition module 510 is also used to acquire the metadata of the table involved in each of the historical SQL statements, and the metadata of the table includes the table name and the column name; the generation module 530 is also used to generate the metadata of each of the historical SQL statements based on the metadata of the table involved in each of the historical SQL statements.
[0166] Optionally, the candidate SQL statement set includes a first candidate historical SQL statement, and the keywords in the first NL statement are all included in the first candidate historical SQL statement. The generation module 530 is specifically used to: cut out an SQL fragment used to express the keywords in the first NL statement from the first candidate historical SQL statement; and obtain the first SQL statement based on the SQL fragment.
[0167] Optionally, the generating module 530 is specifically configured to obtain the first SQL statement according to the conditional items in the first NL statement and the SQL fragments cut out from the first candidate historical SQL statement.
[0168] Optionally, the keywords in the first NL statement are included in at least two candidate historical SQL statements in the candidate SQL statement set, and the generation module 530 is specifically used to: respectively cut out SQL fragments used to express the keywords in the first NL statement from the at least two candidate historical SQL statements; splice the SQL fragments respectively cut out from the at least two candidate historical SQL statements to obtain spliced SQL fragments; and obtain the first SQL statement based on the spliced SQL fragments.
[0169] Optionally, the keyword part in the first NL statement is included in at least one candidate historical SQL statement in the candidate SQL statement set, and the generation module 530 is specifically used to: obtain a first SQL fragment for expressing a first keyword in the first NL statement, the first keyword in the first NL statement is not included in at least one candidate historical SQL statement in the candidate SQL statement set; respectively cut out SQL fragments for expressing the first NL statement from the at least one candidate historical SQL statement; splice the first SQL fragment and the SQL fragment cut out from the at least one candidate historical SQL statement to obtain a spliced SQL fragment; and obtain the first SQL statement based on the spliced SQL fragment.
[0170] Optionally, the generation module 530 is specifically used to: generate the first SQL fragment according to the first model, the input information of the first model includes the first keyword in the first NL statement, and the output information of the first model includes the first SQL fragment.
[0171] Optionally, the display module 540 is also used to display the candidate SQL statement set to the user.
[0172] Optionally, the acquisition module 510 is also used to acquire a second NL statement input by the user, where the second NL statement is an NL statement obtained by the user after modifying the first NL statement according to at least one candidate historical SQL statement included in the candidate SQL statement set; the generation module 530 is also used to generate a second SQL statement for expressing the second NL statement based on the second NL statement and at least one candidate historical SQL statement included in the candidate SQL statement set; the display module 540 is also used to display the second SQL statement to the user.
[0173] Optionally, the device 500 is applied to a cloud management platform, which is used to manage an infrastructure for providing cloud services. The infrastructure includes at least one cloud data center, and each cloud data center is provided with at least one server.
[0174] The device 500 here can be embodied in the form of a functional module. The term "module" here can be implemented in the form of software and / or hardware, and is not specifically limited to this.
[0175] For example, a "module" may be a software program, a hardware circuit, or a combination of the two that implements the above functions. Exemplarily, the following takes the acquisition module 510 as an example to introduce the implementation of the acquisition module 510. Similarly, the implementation of other modules, such as the screening module 520, the generation module 530, and the display module 540, can refer to the implementation of the acquisition module 510.
[0176] The acquisition module 510 is taken as an example of a software functional unit, and the acquisition module 510 may include code running on a computing instance. Among them, the computing instance may include at least one of a physical host (computing device), a virtual machine, and a container. Further, the above-mentioned computing instance may be one or more. For example, the acquisition module 510 may include code running on multiple hosts / virtual machines / containers. It should be noted that the multiple hosts / virtual machines / containers used to run the code may be distributed in the same region (region) or in different regions. Furthermore, the multiple hosts / virtual machines / containers used to run the code may be distributed in the same availability zone (AZ) or in different AZs, each AZ including a data center or multiple data centers with similar geographical locations. Among them, usually a region may include multiple AZs.
[0177] Similarly, multiple hosts / virtual machines / containers used to run the code can be distributed in the same virtual private cloud (VPC) or in multiple VPCs. Usually, a VPC is set up in a region. For cross-region communication between two VPCs in the same region and between VPCs in different regions, a communication gateway needs to be set up in each VPC to achieve interconnection between VPCs through the communication gateway.
[0178] The acquisition module 510 is taken as an example of a hardware functional unit, and the acquisition module 510 may include at least one computing device, such as a server, etc. Alternatively, the acquisition module 510 may also be a device implemented by an application-specific integrated circuit (ASIC) or a programmable logic device (PLD), etc. The PLD may be a complex programmable logical device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL), or any combination thereof.
[0179] The multiple computing devices included in the acquisition module 510 can be distributed in the same region or in different regions. The multiple computing devices included in the acquisition module 510 can be distributed in the same AZ or in different AZs. Similarly, the multiple computing devices included in the acquisition module 510 can be distributed in the same VPC or in multiple VPCs. The multiple computing devices can be any combination of computing devices such as servers, ASICs, PLDs, CPLDs, FPGAs, and GALs.
[0180] Therefore, the modules of each example described in the embodiments of the present application can be implemented with electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present application.
[0181] It should be noted that: when the device provided in the above embodiment executes the above method, only the division of the above functional modules is used as an example. In actual application, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. For example, the acquisition module 510 can be used to execute any step in the above method, the screening module 520 can be used to execute any step in the above method, the generation module 530 can be used to execute any step in the above method, and the display module 540 can be used to execute any step in the above method. The steps that the acquisition module 510, the screening module 520, the generation module 530, and the display module 540 are responsible for implementing can be specified as needed, and the acquisition module 510, the screening module 520, the generation module 530, and the display module 540 respectively implement different steps in the above method to realize all the functions of the above device.
[0182] In addition, the device and method embodiments provided in the above embodiments belong to the same concept, and their specific implementation processes are detailed in the method embodiments above, which will not be repeated here.
[0183] The method provided in the embodiment of the present application can be performed by a computing device, which can also be referred to as a computer system. It includes a hardware layer, an operating system layer running on the hardware layer, and an application layer running on the operating system layer. The hardware layer includes hardware such as a processing unit, a memory and a memory control unit, and then the function and structure of the hardware are described in detail. The operating system is any one or more computer operating systems that implement business processing through a process, for example, a Linux operating system, a Unix operating system, an Android operating system, an iOS operating system, or a windows operating system. The application layer includes applications such as a browser, an address book, a word processing software, and an instant messaging software. In addition, optionally, the computer system is a handheld device such as a smart phone, or a terminal device such as a personal computer, and the present application is not particularly limited, as long as the method provided by the embodiment of the present application can be used. The execution subject of the method provided in the embodiment of the present application can be a computing device, or a functional module in a computing device that can call a program and execute a program.
[0184] Combine the following Figure 6 , a computing device provided in an embodiment of the present application is described in detail.
[0185] Figure 6 1 is a schematic diagram of the architecture of a computing device 1500 provided in an embodiment of the present application. The computing device 1500 may be a server or a computer or other device with computing capabilities. Figure 6 The computing device 1500 shown includes at least one processor 1510 and a memory 1520 .
[0186] It should be understood that the present application does not limit the number of processors and memories in the computing device 1500 .
[0187] The processor 1510 executes the instructions in the memory 1520, so that the computing device 1500 implements the method provided by the present application. Alternatively, the processor 1510 executes the instructions in the memory 1520, so that the computing device 1500 implements the functional modules provided by the present application, thereby implementing the method provided by the present application.
[0188] Optionally, the computing device 1500 further includes a communication interface 1530. The communication interface 1530 uses a transceiver module such as, but not limited to, a network interface card or a transceiver to implement communication between the computing device 1500 and other devices or a communication network.
[0189] Optionally, the computing device 1500 further includes a system bus 1540, wherein the processor 1510, the memory 1520, and the communication interface 1530 are respectively connected to the system bus 1540. The processor 1510 can access the memory 1520 through the system bus 1540. For example, the processor 1510 can read and write data or execute code in the memory 1520 through the system bus 1540. The system bus 1540 is a peripheral component interconnect express (PCI) bus or an extended industry standard architecture (EISA) bus, etc. The system bus 1540 is divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 6 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0190] In a possible implementation, the function of the processor 1510 is mainly to interpret the instructions (or codes) of the computer program and process the data in the computer software. The instructions of the computer program and the data in the computer software can be stored in the memory 1520 or the cache 1516.
[0191] Optionally, the processor 1510 may be an integrated circuit chip with signal processing capabilities. As an example and not a limitation, the processor 1510 is a general-purpose processor, a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic device, a discrete gate or transistor logic device, a discrete hardware component. Among them, the general-purpose processor is a microprocessor, etc. For example, the processor 1510 is a central processing unit (CPU).
[0192] Optionally, each processor 1510 includes at least one processing unit 1512 and a memory control unit 1514 .
[0193] Optionally, the processing unit 1512 is also called a core or kernel, which is the most important component of the processor. The processing unit 1512 is manufactured from single crystal silicon using a certain production process, and all calculations, command reception, command storage, and data processing of the processor are performed by the core. The processing units run program instructions independently and use the ability of parallel computing to speed up the running speed of the program. Various processing units have a fixed logical structure. For example, the processing unit includes logical units such as a first-level cache, a second-level cache, an execution unit, an instruction-level unit, and a bus interface.
[0194] In one implementation example, the memory control unit 1514 is used to control data interaction between the memory 1520 and the processing unit 1512. Specifically, the memory control unit 1514 receives a memory access request from the processing unit 1512, and controls access to the memory based on the memory access request. As an example and not a limitation, the memory control unit is a device such as a memory management unit (MMU).
[0195] In one implementation example, each memory control unit 1514 addresses the memory 1520 through the system bus. And an arbiter ( Figure 6 ), which is responsible for handling and coordinating competing accesses of multiple processing units 1512.
[0196] In an implementation example, the processing unit 1512 and the memory control unit 1514 are connected to each other through connection lines inside the chip, such as address lines, so as to achieve communication between the processing unit 1512 and the memory control unit 1514.
[0197] Optionally, each processor 1510 also includes a cache 1516, wherein the cache is a buffer for data exchange (called cache). When the processing unit 1512 wants to read data, it will first search for the required data from the cache. If it is found, it will be executed directly. If it is not found, it will be searched from the memory. Since the running speed of the cache is much faster than the memory, the role of the cache is to help the processing unit 1512 run faster.
[0198] The memory 1520 can provide a running space for the processes in the computing device 1500. For example, the computer program (specifically, the program code) used to generate the process is stored in the memory 1520. After the computer program is executed by the processor to generate the process, the processor allocates a corresponding storage space for the process in the memory 1520. Furthermore, the above storage space further includes a text segment, an initialized data segment, a bit initialized data segment, a stack segment, a heap segment, etc. The memory 1520 stores the data generated during the running of the process in the storage space corresponding to the above process, such as intermediate data, process data, etc.
[0199] Optionally, the storage is also called memory, and its function is to temporarily store the operation data in the processor 1510 and the data exchanged with the external storage such as the hard disk. As long as the computer is running, the processor 1510 will transfer the data to be calculated to the memory for calculation, and when the calculation is completed, the processing unit 1512 will transmit the result.
[0200] As an example and not limitation, memory 1520 is a volatile memory or a non-volatile memory, or may include both volatile and non-volatile memories. Among them, the non-volatile memory is a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), or a flash memory. The volatile memory is a random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDR SDRAM), enhanced SDRAM (ESDRAM), synchlink DRAM (SLDRAM), and direct rambus RAM (DRRAM). It should be noted that the memory 1520 of the systems and methods described herein is intended to include, but is not limited to, these and any other suitable types of memory.
[0201] The structure of the computing device 1500 listed above is only an example, and the present application is not limited thereto. The computing device 1500 of the embodiment of the present application includes various hardware in the computer system in the prior art. For example, the computing device 1500 also includes other memories besides the memory 1520, such as disk storage, etc. Those skilled in the art should understand that the computing device 1500 may also include other devices necessary for normal operation. At the same time, according to specific needs, those skilled in the art should understand that the above-mentioned computing device 1500 may also include hardware devices for implementing other additional functions. In addition, those skilled in the art should understand that the above-mentioned computing device 1500 may also include only the devices necessary to implement the embodiment of the present application, and does not necessarily include Figure 6 All devices shown in .
[0202] The embodiment of the present application also provides a computing device cluster. The computing device cluster includes at least one computing device. The computing device may be a server. In some embodiments, the computing device may also be a terminal device such as a desktop computer, a laptop computer, or a smart phone.
[0203] like Figure 7 As shown, the computing device cluster includes at least one computing device 1500. The memory 1520 in one or more computing devices 1500 in the computing device cluster may store the same instructions for executing the above method.
[0204] In some possible implementations, the memory 1520 in one or more computing devices 1500 in the computing device cluster may also store some instructions for executing the above method. In other words, the combination of one or more computing devices 1500 may jointly execute the instructions of the above method.
[0205] It should be noted that the memory 1520 in different computing devices 1500 in the computing device cluster may store different instructions, which are respectively used to execute part of the functions of the above-mentioned apparatus. That is, the instructions stored in the memory 1520 in different computing devices 1500 may implement the functions of one or more modules in the above-mentioned apparatus.
[0206] In some possible implementations, one or more computing devices in the computing device cluster may be connected via a network, which may be a wide area network or a local area network. Figure 8 A possible implementation is shown. Figure 8 As shown, two computing devices 1500A and 1500B are connected via a network. Specifically, they are connected to the network via a communication interface in each computing device.
[0207] It should be understood that Figure 8The functionality of the computing device 1500A shown in FIG. 1 may also be implemented by multiple computing devices 1500. Similarly, the functionality of the computing device 1500B may also be implemented by multiple computing devices 1500.
[0208] In this embodiment, a computer program product including instructions is also provided, and the computer program product may be software or a program product including instructions that can be run on a computing device or stored in any available medium. When the computer program product is run on a computing device, the computing device is caused to execute the method provided above, or the computing device is caused to implement the function of the apparatus provided above.
[0209] In this embodiment, a computer-readable storage medium is also provided. The computer-readable storage medium may be any available medium that can be stored by a computing device or a data storage device such as a data center that includes one or more available media. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk). The computer-readable storage medium includes instructions. When the instructions in the computer-readable storage medium are executed on a computing device, the computing device executes the method provided above.
[0210] It should be understood that in the various embodiments of the present application, the size of the serial numbers of the above-mentioned processes does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0211] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0212] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0213] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0214] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0215] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0216] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in the various embodiments of the present application. The aforementioned storage media include: various media that can store program codes, such as USB flash drives, mobile hard disks, read-only memories (ROM), random access memories (RAM), magnetic disks or optical disks.
[0217] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A method for converting natural language to structured query language, characterized in that, the method includes: obtaining a first natural language NL statement input by a user; screening out a set of candidate SQL statements from multiple historical structured query language SQL statements according to the first NL statement, where the set of candidate SQL statements includes at least one candidate historical SQL statement that matches the first NL statement, and the candidate historical SQL statement contains some or all of the keywords in the first NL statement; generating a first SQL statement for expressing the first NL statement according to at least one candidate historical SQL statement included in the set of candidate SQL statements; displaying the first SQL statement to the user.
2. The method according to claim 1, characterized in that, the screening out of the set of candidate SQL statements from multiple historical structured query language SQL statements according to the first NL statement includes: screening out at least one candidate historical SQL statement that matches the first NL statement from the multiple historical SQL statements according to the metadata of each historical SQL statement in the multiple historical SQL statements, and the metadata of the candidate historical SQL statement contains some or all of the keywords in the first NL statement.
3. The method according to claim 2, characterized in that, the metadata of each historical SQL statement includes the metadata of the tables involved in each historical SQL statement.
4. The method according to claim 2 or 3, characterized in that, the method further includes: obtaining the multiple historical SQL statements; obtaining the metadata of the tables involved in each historical SQL statement, where the metadata of the tables includes table names and column names; generating the metadata of each historical SQL statement according to the metadata of the tables involved in each historical SQL statement.
5. The method according to any one of claims 1 to 4, characterized in that, the set of candidate SQL statements includes a first candidate historical SQL statement, and all the keywords in the first NL statement are included in the first candidate historical SQL statement, the generating of a first SQL statement for expressing the first NL statement according to the first NL statement and at least one candidate historical SQL statement included in the set of candidate SQL statements includes: cropping out an SQL fragment for expressing the keywords in the first NL statement from the first candidate historical SQL statement; obtaining the first SQL statement according to the SQL fragment.
6. The method according to claim 5, characterized in that, the obtaining of the first SQL statement according to the SQL fragment includes: obtaining the first SQL statement according to the conditional items in the first NL statement and the SQL fragment cropped out from the first candidate historical SQL statement.
7. The method according to any one of claims 1 to 4, characterized in that, the keywords in the first NL statement are included in at least two candidate historical SQL statements in the set of candidate SQL statements, Generating a first SQL statement for expressing the first NL statement according to the first NL statement and at least one candidate historical SQL statement included in the set of candidate SQL statements includes: Respectively cropping SQL fragments for expressing keywords in the first NL statement from the at least two candidate historical SQL statements; Splicing the SQL fragments respectively cropped from the at least two candidate historical SQL statements to obtain a spliced SQL fragment; Obtaining the first SQL statement according to the spliced SQL fragment.
8. The method according to any one of claims 1 to 4, wherein, The keyword part in the first NL statement is included in at least one candidate historical SQL statement in the set of candidate SQL statements, Generating a first SQL statement for expressing the first NL statement according to the first NL statement and at least one candidate historical SQL statement included in the set of candidate SQL statements includes: Obtaining a first SQL fragment for expressing a first keyword in the first NL statement, where the first keyword in the first NL statement is not included in at least one candidate historical SQL statement in the set of candidate SQL statements; Respectively cropping SQL fragments for expressing the first NL statement from the at least one candidate historical SQL statement; Splicing the first SQL fragment and the SQL fragments cropped from the at least one candidate historical SQL statement to obtain a spliced SQL fragment; Obtaining the first SQL statement according to the spliced SQL fragment.
9. The method according to claim 8, wherein, The obtaining a first SQL fragment for expressing a first keyword in the first NL statement includes: Generating the first SQL fragment according to a first model, where the input information of the first model includes the first keyword in the first NL statement, and the output information of the first model includes the first SQL fragment.
10. The method according to any one of claims 1 to 9, wherein, The method further includes: Displaying the set of candidate SQL statements to the user.
11. The method according to claim 10, wherein, The method further includes: Obtaining a second NL statement input by the user, where the second NL statement is an NL statement obtained by the user modifying the first NL statement according to at least one candidate historical SQL statement included in the set of candidate SQL statements; Generating a second SQL statement for expressing the second NL statement according to the second NL statement and at least one candidate historical SQL statement included in the set of candidate SQL statements; Displaying the second SQL statement to the user.
12. The method according to any one of claims 1 to 11, wherein, The method is applied to a cloud management platform which is used to manage the infrastructure that provides cloud services. The infrastructure includes at least one cloud data center, and each cloud data center is provided with at least one server.
13. An apparatus for converting natural language to structured query language, characterized in that, the apparatus includes: an acquisition module, configured to acquire a first natural language NL statement input by a user; a screening module, configured to screen out a set of candidate SQL statements from multiple historical structured query language SQL statements according to the first NL statement. The set of candidate SQL statements includes at least one candidate historical SQL statement that matches the first NL statement, and the candidate historical SQL statement contains some or all of the keywords in the first NL statement; a generation module, configured to generate a first SQL statement for expressing the first NL statement according to at least one candidate historical SQL statement included in the set of candidate SQL statements; a display module, configured to display the first SQL statement to the user.
14. The apparatus according to claim 13, characterized in that, the screening module is specifically configured to: screen out at least one candidate historical SQL statement that matches the first NL statement from the multiple historical SQL statements according to the metadata of each historical SQL statement in the multiple historical SQL statements. The metadata of the candidate historical SQL statement contains some or all of the keywords in the first NL statement.
15. The apparatus according to claim 14, characterized in that, the metadata of each historical SQL statement includes the metadata of the table involved in each historical SQL statement.
16. The apparatus according to claim 14 or 15, characterized in that, the acquisition module is further configured to acquire the multiple historical SQL statements; the acquisition module is further configured to acquire the metadata of the table involved in each historical SQL statement, and the metadata of the table includes the table name and column name; the generation module is further configured to generate the metadata of each historical SQL statement according to the metadata of the table involved in each historical SQL statement.
17. The apparatus according to any one of claims 13 to 16, characterized in that, the set of candidate SQL statements includes a first candidate historical SQL statement, and all the keywords in the first NL statement are included in the first candidate historical SQL statement, the generation module is specifically configured to: crop out an SQL fragment for expressing the keywords in the first NL statement from the first candidate historical SQL statement; obtain the first SQL statement according to the SQL fragment.
18. The apparatus according to claim 17, characterized in that, the generation module is specifically configured to: obtain the first SQL statement according to the conditional items in the first NL statement and the SQL fragment cropped out from the first candidate historical SQL statement.
19. The apparatus according to any one of claims 13 to 16, characterized in that, The keywords in the first NL statement are included in at least two candidate historical SQL statements in the set of candidate SQL statements. Specifically, the generating module is configured to: respectively cut out SQL fragments for expressing the keywords in the first NL statement from the at least two candidate historical SQL statements; splice the SQL fragments respectively cut out from the at least two candidate historical SQL statements to obtain a spliced SQL fragment; obtain the first SQL statement according to the spliced SQL fragment.
20. The apparatus according to any one of claims 13 to 16, characterized in that a keyword part of the first NL statement is included in at least one candidate historical SQL statement in the set of candidate SQL statements, Specifically, the generating module is configured to: obtain a first SQL fragment for expressing a first keyword in the first NL statement, where the first keyword in the first NL statement is not included in at least one candidate historical SQL statement in the set of candidate SQL statements; respectively cut out SQL fragments for expressing the first NL statement from the at least one candidate historical SQL statement; splice the first SQL fragment and the SQL fragments cut out from the at least one candidate historical SQL statement to obtain a spliced SQL fragment; obtain the first SQL statement according to the spliced SQL fragment.
21. The apparatus according to claim 20, characterized in that Specifically, the generating module is configured to: generate the first SQL fragment according to a first model, where input information of the first model includes the first keyword in the first NL statement, and output information of the first model includes the first SQL fragment.
22. The apparatus according to any one of claims 13 to 21, characterized in that the display module is further configured to display the set of candidate SQL statements to the user.
23. The apparatus according to claim 22, characterized in that the obtaining module is further configured to obtain a second NL statement input by the user, where the second NL statement is an NL statement obtained by the user modifying the first NL statement according to at least one candidate historical SQL statement included in the set of candidate SQL statements; the generating module is further configured to generate a second SQL statement for expressing the second NL statement according to the second NL statement and at least one candidate historical SQL statement included in the set of candidate SQL statements; the display module is further configured to display the second SQL statement to the user.
24. The apparatus according to any one of claims 13 to 23, characterized in that the apparatus is applied to a cloud management platform, the cloud management platform is used to manage the infrastructure providing cloud services, the infrastructure includes at least one cloud data center, and each cloud data center is provided with at least one server.
25. A cluster of computing devices, characterized in that includes at least one computing device, and each computing device includes a processor and a memory; The processor of the at least one computing device is configured to execute instructions stored in the memory of the at least one computing device, such that the computing device cluster executes the method according to any one of claims 1 to 12.
26. A computer program product comprising instructions, wherein, when the instructions are run by a computing device cluster, the computing device cluster is caused to execute the method according to any one of claims 1 to 12.
27. A computer-readable storage medium, wherein, it includes computer program instructions, and when the computer program instructions are executed by a computing device cluster, the computing device cluster executes the method according to any one of claims 1 to 12.