SQL statement generation method and device based on multiple libraries
By compressing table creation information in multi-database scenarios and specifying model recognition, the problem of SQL generation accuracy in multi-database scenarios is solved, achieving higher SQL statement accuracy.
Patent Information
- Application Number
- CN202510826559.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-19
- Publication Date
- 2025-10-17
- Estimated Expiration
- 2045-06-19
AI Technical Summary
In a multi-database scenario, existing technologies have difficulty accurately generating SQL statements, resulting in decreased accuracy.
By compressing the table creation information of multiple databases, the specified model is used to identify database objects and object relationships and generate SQL statements.
Improves the accuracy of SQL generation in multi-database scenarios, reduces information loss, and improves the accuracy of generated SQL statements.
Smart Images

Figure CN120804132A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of artificial intelligence, in particular to a SQL statement method and device based on multiple databases. BACKGROUND
[0002] Text2sql is a technology for converting natural language into SQL statements. Currently, it is mainly based on large language models combined with schema connection technology, and the complete table structure in a single database is directly embedded into the prompt word to guide the large language model to identify the mapping relationship between the user's problem and the table field, so as to generate SQL statements.
[0003] However, in the actual application of text2sql, it is not a single database with a small number of tables, but contains a large number of databases and table information. In actual application, it is often necessary to query multiple databases and multiple tables. The database information far exceeds the input space of the model, and cutting it may lead to the loss of key information, thereby reducing the accuracy of SQL. Therefore, there is an urgent need to improve the accuracy of SQL generation in a multi-database scenario. SUMMARY
[0004] In view of the above problems, the purpose of the present application is to provide a SQL statement generation method and device based on multiple databases, which can improve the accuracy of SQL generation in a multi-database scenario.
[0005] To solve the above technical problems, the present application provides the following technical solutions: On the one hand, the present application provides a SQL statement generation method based on multiple databases, comprising: obtaining first table creation information and second table creation information of multiple databases in a target scenario, the first table creation information being original table creation information, and the second table creation information being information obtained by compressing the length of the first table creation information according to a preset compression rule; determining first reference table creation information corresponding to a to-be-processed query from the second table creation information based on the input length limit of a specified model, the to-be-processed query being a query in the target scenario; controlling the specified model to identify database objects and object relationships related to the to-be-processed query according to the first reference table creation information and the preset compression rule, and obtaining skeleton information; retrieving second reference table creation information from the second table creation information using the skeleton information; controlling the specified model to perform extension processing on the skeleton information according to the second reference table creation information and the to-be-processed query, and obtaining a target SQL statement corresponding to the to-be-processed query.
[0006] On the other hand, the present application also provides a SQL statement generation device based on multiple databases, comprising: The table building acquisition module is configured to acquire first table building information and second table building information of a plurality of databases in a target scene, the first table building information being original table building information, and the second table building information being information obtained by compressing the length of the first table building information according to a preset compression rule; The first determination module is configured to determine, based on an input length limit of a specified model, first reference table building information corresponding to a to-be-processed query from the second table building information, the to-be-processed query being a query in the target scene; The skeleton recognition module is configured to control the specified model to recognize, according to the first reference table building information and a preset compression rule, a database object and an object relationship related to the to-be-processed query, and obtain skeleton information. The second determination module is configured to search for second reference table building information from the second table building information by using the skeleton information. The target generation module is configured to control the specified model to perform expansion processing on the skeleton information according to the second reference table building information and the to-be-processed query, and obtain a target SQL statement corresponding to the to-be-processed query.
[0007] In another aspect, the present application further provides an electronic device comprising a processor and a memory, wherein the memory stores a plurality of instructions; the processor loads the instructions from the memory to execute the steps of any of the SQL statement generation methods based on multiple databases provided by the present application.
[0008] In another aspect, the present application further provides a computer readable storage medium, wherein the computer readable storage medium stores a plurality of instructions, and the instructions are suitable for being loaded by a processor to execute the steps of any of the SQL statement generation methods based on multiple databases provided by the present application.
[0009] In another aspect, the present application further provides a computer program product comprising computer programs / instructions, wherein the computer programs / instructions are executed by a processor to implement the steps of any of the SQL statement generation methods based on multiple databases provided by the present application.
[0010] The technical solutions provided by the present application have at least the following beneficial effects: In the embodiment of the present application, the lengths of the table building information of multiple databases are compressed to shorten the lengths of the table building information, the first reference table building information and the second reference table building information are determined from the compressed second table building information in subsequent processing, the skeleton information is recognized according to the first reference table building information and the preset compression rule by using the specified model, and the skeleton information is expanded by using the second reference table building information by the specified model to obtain the target SQL statement. The length of the table building information is greatly shortened in the specified model by compressing the length of the table building information, information loss is maximally reduced, and the accuracy of the generated SQL in the multi-database scenario is improved. BRIEF DESCRIPTION OF DRAWINGS
[0011] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed in the embodiment description will be briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative labor.
[0012] Figure 1 is a scene schematic diagram of the multi-database-based SQL statement generation method provided by the embodiment of the present application; Figure 2 is a flow schematic diagram of the multi-database-based SQL statement generation method provided by the embodiment of the present application; Figure 3 is a schematic diagram for determining the first reference table building information provided by the embodiment of the present application; Figure 4 is a schematic diagram for obtaining multiple training samples provided by the embodiment of the present application; Figure 5 is a schematic diagram for calculating sample rewards provided by the embodiment of the present application; Figure 6 is a structural schematic diagram of the multi-database-based SQL statement generation device provided by the embodiment of the present application; Figure 7 is a structural schematic diagram of the electronic device provided by the embodiment of the present application. DETAILED DESCRIPTION
[0013] The technical solutions in the embodiments of the present application will be described clearly and completely in combination with the drawings in the embodiments of the present application. Obviously, the described embodiments are only some embodiments of the present application, not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative labor are within the scope of protection of the present application.
[0014] It can be understood that in the specific embodiments of the present application, the data related to user information and the like need to obtain user permission or consent, and the collection, use and processing of the related data need to comply with the relevant laws, regulations and standards of the country and region.
[0015] Referring to Figure 1 , a schematic diagram of an application scenario of the multi-database-based SQL statement generation method is shown. The application scenario can include a terminal 101 and a server 102, and the terminal 101 and the server 102 can exchange data through a network. The terminal 101 can be installed with an application program related to question and answer. The terminal 101 can be a mobile phone, a tablet computer, a smart Bluetooth device, a computer, a large screen, a robot, or the like. The server 102 can be a single server or a server cluster composed of multiple servers.
[0016] For SQL statement generation in a target scenario, a user can send a to-be-processed query to the server 102 through the terminal 101. The server 102 can receive the to-be-processed query, obtain first table creation information and second table creation information of multiple databases in the target scenario, the first table creation information being original table creation information, and the second table creation information being information obtained by compressing the length of the first table creation information according to a preset compression rule. Based on an input length limit of a specified model, the server 102 determines first reference table creation information corresponding to the to-be-processed query from the second table creation information, the to-be-processed query being a query in the target scenario. The server 102 controls the specified model to identify database objects and object relationships related to the to-be-processed query according to the first reference table creation information and the preset compression rule, and obtains skeleton information. The server 102 controls the specified model to perform extension processing on the skeleton information according to the second reference table creation information and the to-be-processed query, and obtains a target SQL statement corresponding to the to-be-processed query. Then the server 102 can send the target SQL statement to the terminal 101, so as to display the target SQL statement to the user.
[0017] In this embodiment, a multi-database-based SQL statement generation method is provided, as shown in Figure 2 , the specific process of the multi-database-based SQL statement generation method can be as follows: S110, obtaining first table creation information and second table creation information of multiple databases in a target scenario.
[0018] The target scene refers to a business scene in which a SQL statement needs to be generated. The business scene can be set according to actual needs, and is not specifically limited herein. Generally, in an actual business scene, data in a business scene can be stored in multiple data tables of multiple databases, and when a SQL statement is generated, multiple databases need to be jointly queried. Therefore, for a target scene, information related to multiple databases corresponding to the target scene can be obtained for processing.
[0019] The first table building information refers to original table building information of each database. The table building information can include related structure information of the database, for example, information such as tables, columns, primary keys, foreign keys, and the like in the database. The second table building information is information obtained after the length of the second table building information is compressed according to a preset compression rule. That is, the length of the second table building information is much smaller than that of the first table building information, but the amount of information contained is unchanged, and only the length changes.
[0020] The preset compression rule can be set according to actual needs. In an embodiment of the present application, the preset compression rule can be a series of plain symbolization escape rules, which have the characteristics of reversibility, plainness, and generalization. Alternatively, the preset compression rule can include a mapping relationship between a preset type and a sub-compression rule. When the first table building information and the second table building information are obtained, the first table building information of multiple databases in a target scene can be obtained; the content in the first table building information is divided and processed according to the preset type, to obtain multiple sub-contents; the length of each sub-content is compressed according to the preset compression rule corresponding to the preset type, to obtain compressed content; and all compressed content is organized as the second table building information.
[0021] For a certain target scene, the first table building information of multiple databases in the target scene can be obtained. The preset compression rule can include a mapping relationship between a preset type and a sub-compression rule. In an embodiment of the present application, the mapping relationship can be seen from Table 1.
[0022] Table 1
[0023] It should be noted that only part of the examples shown in Table 1 are shown. According to the preset type, the content can be divided according to the library name / table name, data type, constraint, annotation / default value in the first table building information, to obtain corresponding sub-contents; and the divided sub-contents are compressed according to the sub-compression rule corresponding to the preset type, to obtain corresponding compressed content.
[0024] For example, in order to facilitate the display of the compression effect of each sub-content, Table 2 shows the content before and after the compression of the sub-content.
[0025] Table 2
[0026] Optionally, when converting the first table-building information into the second table-building information, a preset compression rule can be incorporated into the prompt word template, and then the large language model can be used to automatically convert the first table-building information into the second table-building information. After compressing the first table-building information in this manner, the resulting second table-building information is significantly shorter than the first table-building information and contains the same amount of information.
[0027] For example, the first table creation information is: --MySQL DB1 CREATE TABLE U ( User ID INT PRIMARY KEY AUTO_INCREMENT, Name VARCHAR(50) NOT NULL COMMENT 'User's real name', Registration time DATETIME DEFAULT NOW() ); -- PostgreSQL DB2 CREATE TABLE O ( Order ID SERIAL PRIMARY KEY, User ID INT REFERENCES DB1.U (User ID), Amount NUMERIC(10,2), Notes TEXT ) The second table creation information obtained after compression is: DB1@U(User ID , first name <s:50>{ name}, registration time {NOW}) DB2@O(Order ID , user ID -> DB1@U.user ID, amount <d2>, remarks <t>) After lossless compression of the first table creation information of each database in the above manner, a plurality of corresponding second table creation information can be obtained. The first table creation information and the second table creation information can be converted to each other according to a preset compression rule.
[0028] In S120, based on the input length limit of the specified model, the first reference table creation information corresponding to the to-be-processed query is determined from the second table creation information.
[0029] The specified model can be an existing large language model, or a model trained by a large amount of data in a target scenario. In the embodiment of the present application, in order to ensure that the SQL statement can be accurately generated in the multi-database scenario, the specified model is a model trained by training data. The specified model can extract skeleton information from natural language, and then generate the SQL statement corresponding to the natural language based on the skeleton information.
[0030] The to-be-processed query is a natural language statement provided by a user. The to-be-processed query statement is a query in a target scenario, that is, the scenario of the to-be-processed query and the scenario of the table creation information need to be consistent. The first reference table creation information refers to the second table creation information related to the to-be-processed query, which can provide a basis for accurately generating the SQL statement subsequently.
[0031] It can be understood that any model has its corresponding input length limit, and cannot input data unlimitedly. The second table creation information needs to be input into the specified model subsequently, and thus the length of the second table creation information, that is, the first reference table creation information, is also limited. According to the input length limit of the specified model, the first reference table creation information can be determined from the second table creation information. In some embodiments, based on the input length limit of the specified model, when the first reference table creation information corresponding to the to-be-processed query is determined from the second table creation information, if the length of the second table creation information is not greater than the input length limit of the specified model, the second table creation information is taken as the first reference table creation information; if the length of the second table creation information is greater than the input length limit of the specified model, the first candidate table creation information is determined from the second table creation information according to the similarity between the to-be-processed query and the first table creation information; the second candidate table creation information is determined from the second table creation information according to the similarity between the to-be-processed query and the second table creation information; and the first candidate table creation information and the second candidate table creation information are merged as the first reference table creation information.
[0032] Also see Figure 3 , shows a schematic diagram of determining the first reference table building information. Among them, the input length limit of the specified model is the maximum length of the first reference table building information set in advance, which can be set according to actual needs, but the length limit should not exceed the total input length limit of the specified model. Compare the second table building information with the input length limit of the specified model, and the length of the second table building information is not greater than the input length limit of the specified model, and the second table building information can be directly used as the first reference table building information.
[0033] If the length of the second table building information is greater than the input length limit of the specified model, the first reference table building information related to the to-be-processed query can be selected from the second table building information based on the similarity between the second table building information and the to-be-processed query.
[0034] As an implementation manner, each second table building information can be converted into a table building vector, and the to-be-processed query can be converted into a query vector, and the cosine similarity between the table building vector and the query vector can be calculated. The second table building information with a cosine similarity greater than a certain threshold or a preset number of second table building information ranked in the front can be used as the first reference table building information. Among them, the threshold and the preset number can be set according to actual needs, which is not limited here. Of course, when the preset number of second table building information is selected as the first reference table building information, the input length limit can be determined, for example, the length of the existing first reference table building information can be added until the input length limit of the specified model is exceeded when a new first reference table building information is added.
[0035] As another implementation manner, in order to improve the accuracy of the first reference table building information, the first candidate table building information can be determined from the second table building information according to the similarity between the to-be-processed query and the first table building information; the second candidate table building information can be determined from the second table building information according to the similarity between the to-be-processed query and the second table building information; and the first candidate table building information and the second candidate table building information are merged as the first reference table building information.
[0036] Similarly, the first table building information and the to-be-processed query can be converted into corresponding vectors, and the cosine similarity between the vectors can be calculated as the similarity between the to-be-processed query and the first table building information. The first table building information is sorted in descending order of similarity, and the specified number of first table building information ranked in the front is used as the first candidate table building information. The second candidate table building information can be determined in a similar manner. It can be understood that the first candidate table building information is essentially the first table building information, and the first table building information and the second table building information can be converted into each other. When the first table building information is compressed into the second table building information, a corresponding relationship between the first table building information and the second table building information can be established in advance.
[0037] By querying the correspondence, the second table building information corresponding to the first candidate table building information is obtained, denoted as third candidate table building information. The second candidate table building information and the third candidate table building information are essentially second table building information, and the second candidate table building information and the third candidate table building information can be merged and de-duplicated to obtain the final first reference table building information, which is all compressed table building information.
[0038] When the total length of the second table building information does not exceed the input length limit, all the second table building information can be directly used as the first reference table building information; when the total length of the second table building information exceeds the input length limit, the relevant second table building information is screened out by similarity to be used as the first reference table building information, which can ensure comprehensive coverage of the table building information related to the to-be-processed query and improve the accuracy of subsequent generation of SQL statements.
[0039] S130, controlling the specified model to identify the database object and the object relationship related to the to-be-processed query according to the first reference table building information and the preset compression rule to obtain skeleton information.
[0040] The specified model has the ability to extract skeleton information after being trained by a large amount of data. The skeleton information refers to the skeleton of the SQL statement and can include database objects and the relationship between objects. The database objects can include database names and table names, and the object relationship can include the logical relationship between the databases and the table names, but does not include column names and sample values.
[0041] The preset compression rule refers to the rule used when converting the first table building information into the second table building information. Providing the first reference table building information and the preset compression rule to the specified model can control the specified model to identify the data object and the object relationship related to the to-be-processed query to obtain the skeleton information.
[0042] As an implementation manner, in order to improve the accuracy of extracting the skeleton information, the reference business data can be determined according to the similarity between the business data in the target scene and the to-be-processed query; the first prompt word is constructed by using the reference business data, the first reference table building information, and the preset compression rule; and the first prompt word is used to guide the specified model to identify the database object and the object relationship involved in the to-be-processed query to obtain the skeleton information.
[0043] Providing the reference business data can enhance the accuracy of the specified model in understanding the data in the target scene. The business data in the target scene can be collected in advance and stored in the corresponding database after being sliced. In order to avoid data redundancy caused by introducing too much business data, the similarity between the to-be-processed query and each piece of business data can be calculated, and a certain number of business data with high similarity are used as reference business data.
[0044] The first prompt word is constructed by reference service data, first reference table building information and a preset compression rule. Specifically, a first template can be preset, the first template is a prompt word template for extracting skeleton information, the prompt word template can include required reference information, extraction requirements and reply requirements. The first template can be specifically set according to actual needs. In the embodiment of the application, the first template is as follows: "You are an expert in multi-database SQL parsing. Please extract the SQL skeleton information according to the following structure information: # Table building information symbolization compression representation {Sample_schema_zip} # Current problem to be parsed Problem description: {Problem description text} # Reference knowledge base {Sample_doc} # Metadata symbolization compression algorithm rule [Metadata symbolization compression algorithm rule] Extraction requirements 1. Identify all database objects (database name, table name) involved in the problem description.
[0045] 2. Construct the SQL skeleton structure in the form of SELECT, FROM, JOIN, ON, UNION, etc. (including but not limited to) to describe the relationship between the database objects involved in the problem.
[0046] 3. The skeleton structure is represented using the metadata symbolization compression algorithm rule.
[0047] 4. The skeleton structure only includes database names, table names (reference reply examples), and the logical relationship of each database and table name, and does not include column names, sample values, etc.
[0048] Reference reply example: FROM DB1@U INNER JOIN DB2@O ON "" In the first template provided by the embodiment of the application, the specific role, the task to be performed and the specific requirements for performing the task are included. In the above first template, {Sample_schema_zip} can be filled with the first reference table building information; {Problem description text} can be filled with the query to be processed; {Sample_doc} can be filled with reference service data; and [Metadata symbolization compression algorithm rule] can be filled with a preset compression rule, so as to generate the first prompt word.
[0049] The first prompt word is input into the specified model, which can guide the specified model to identify and extract all database names and table names related to the to-be-processed query according to the extraction requirements in the first prompt word, and identify the logical relationship between the databases and tables involved, and output the identified information in the format of the reference reply example, that is, obtain the skeleton information.
[0050] S140, retrieve second reference table building information from the second table building information by using the skeleton information.
[0051] The extracted skeleton information includes database names and table names related to the to-be-processed query and the logical relationship between the databases and tables. Based on the skeleton information, the second reference table building information can be further retrieved for subsequent generation of SQL statements.
[0052] As an implementation manner, when determining the second reference table building information, the skeleton information can be used as a search term to perform semantic search in the second table building information to obtain the second reference table building information. For example, the similarity between the skeleton information and the second table building information can be calculated, and after sorting in descending order of the similarity, a certain number of second table building information with high ranking can be used as the second reference table building information.
[0053] As another implementation manner, since the skeleton information includes database names and table names, the second table building information associated with the database names and table names can be directly used as the second reference table building information.
[0054] S150, control the specified model to expand the skeleton information according to the second reference table building information and the to-be-processed query to obtain a target SQL statement corresponding to the to-be-processed query.
[0055] The second reference table building information is table building information related to the skeleton information, and the specified model can expand the skeleton information by using the second reference table building information and the to-be-processed query to generate a target SQL statement corresponding to the to-be-processed query.
[0056] Specifically, the specified model has the ability to generate SQL statements according to the skeleton information. The second reference table building information and the to-be-processed query can be used to construct a corresponding prompt word to guide the specified model to generate an SQL statement.
[0057] As an implementation manner, when generating the target SQL statement, reference business data can be determined according to the similarity between the business data in the target scene and the to-be-processed query, a second prompt word can be constructed by using the second reference table building information, the skeleton information, the to-be-processed query, and the reference business data, and the specified model can be guided to expand the skeleton information by using the second prompt word to obtain a target SQL statement corresponding to the to-be-processed query.
[0058] Similar to the generation of the skeleton information, the similarity between the to-be-processed query and the business data in the target scene can be calculated to determine the reference business data. The specific implementation manner can refer to the corresponding part described above, and details are not described herein again to avoid repetition. The second prompt word is constructed by using the second reference table building information, the skeleton information, the to-be-processed query, and the reference business data. Specifically, a second template can be set in advance, the second template being a prompt word template used for generating an SQL statement based on the skeleton information. The prompt word template can include a specific task, a context required for executing the task, and a requirement for executing the task. The second template can be specifically set according to actual needs. In the embodiment of the present application, the second template is as follows: "# Role and task You are an expert in multi-database SQL generation and optimization, and need to expand the SQL skeleton into a complete and executable SQL statement based on the context information.
[0059] ## Context input
[0060] - Original question : {question description text}
[0061] - SQL skeleton : {SQL_bone}
[0062] - Symbolically compressed table building information : {Sample_schema_zip}
[0063] - Business knowledge base fragment : {Sample_doc}
[0064] ## Generation requirement
[0065] - The SQL statement obtained after the expansion of the SQL skeleton is complete and executable.
[0066] - The generated SQL is constrained in the ```` format. {your reply}``` format.
[0067] Wherein, the role and the task contain the description of the specific task to be performed; the context input can contain the information required to perform the task; the generation requirement contains the specific requirements to be followed when performing the task. Among them, in the context input, {question description text} can fill in the query to be processed; {SQL_bone} can fill in the extracted skeleton information; {Sample_schema_zip} can fill in the second reference table creation information; {Sample_doc} can fill in the reference business data.
[0068] After filling the above data into the corresponding position, the second prompt word can be generated. The second prompt word is input into the specified model to guide the specified model to generate the SQL statement according to the corresponding requirements and output. The SQL statement output by the specified model is the target SQL statement.
[0069] The above generation of the target SQL statement cannot be separated from the specified model, and the specified model can be obtained by pre-training. The training process of the specified model will be described in detail below.
[0070] Wherein, the specified model can be obtained by training as follows: obtaining a plurality of training samples, the training samples including sample questions, sample SQL statements, sample skeleton information and sample improvement information; for each training sample, a first sample prompt word is constructed based on reference information and the sample question, and a predicted skeleton information is inferred by a basic model based on the guidance of the first sample prompt word, the reference information including second table creation information, sample business data and pre-set compression rules; a second sample prompt word is constructed based on the predicted skeleton information, the sample improvement information and the reference information, and a predicted SQL statement is inferred by the basic model based on the guidance of the second sample prompt word; the consistency of the sample skeleton information and the predicted skeleton information and the consistency of the predicted SQL statement and the standard SQL statement are used to calculate the sample reward of the training sample; the model parameters of the basic model are adjusted based on the sample rewards of all training samples to obtain the specified model.
[0071] The training sample is data used to train the basic model to obtain the specified model, wherein a training sample contains a sample question, a sample SQL statement, a sample skeleton information and a sample improvement information. The sample SQL statement is a SQL statement corresponding to the sample question, the sample skeleton information is the skeleton information extracted from the sample question, and the sample improvement information is an improvement suggestion for guiding the generation of the SQL statement in the training.
[0072] Wherein, the training sample can be generated by collecting data in the target scene in advance and stored in a specified database, so as to directly obtain the training sample when training the specified model.
[0073] As an implementation manner, when the plurality of training samples are acquired, a plurality of SQL question and answer pairs in a target scene and business data can be acquired, the SQL question and answer pairs comprising a sample question and a sample SQL statement; a sample database object is extracted from each sample SQL statement, and second table building information corresponding to the sample database object is acquired to obtain sample table building information; sample business data is determined according to a similarity between the business data and the sample question; sample skeleton information is extracted from the sample question according to the sample table building information, the sample business data and the preset compression rule; a sample predicted SQL statement corresponding to the sample question is generated by using the sample skeleton information, the sample question, the sample table building information, the sample business data and the preset compression rule; and sample improvement information is generated based on consistency between the sample predicted SQL statement and the sample SQL statement.
[0074] In which, refer to Figure 4 , a schematic diagram for acquiring a plurality of training samples is shown. Data in a target scene is collected, including business data and SQL question and answer pairs, wherein the SQL question and answer pairs can include a specific question in the target scene, i.e., a sample question, and an SQL statement used to solve the question, i.e., a sample SQL statement. As for the business data, it can be stored after being sliced. For each sample SQL statement, the structure of the sample SQL statement can be parsed to extract a sample database object, which can include database name, table name and other information, and then the second table building information corresponding to the extracted data object is acquired to obtain the sample table building information.
[0075] The similarity between the sample question and each business data is calculated, and some business data with high similarity can be selected as sample business data. The specific number of sample business data can be set according to actual needs.
[0076] The sample skeleton information can be extracted from the sample question by using the sample table building information, the sample business data and the preset compression rule. Specifically, the first template used before can be referred to, the sample table building information is filled into {Sample_schema_zip}, the sample question is filled into {Question description text}, the sample business data is filled into {Sample_doc}, and the preset compression rule is filled into [metadata symbolization compression algorithm rule], so as to obtain the corresponding prompt word, and then the prompt word is input into the large language model to guide the large language model to extract the sample skeleton information.
[0077] In some embodiments, the content output by the large language model can be evaluated to ensure the accuracy of the sample skeleton information. For example, a first specified prompt word can be constructed based on sample table building information, sample business data, and a preset compression rule; the first specified prompt word is used to guide the large language model to extract the specified skeleton information corresponding to the sample problem; the specified skeleton information is evaluated in the first specified dimension; if the specified skeleton information passes the skeleton evaluation processing, the specified skeleton information is determined as the sample skeleton information; if the specified skeleton information does not pass the skeleton evaluation processing, the specified skeleton information is optimized according to the skeleton evaluation processing result to obtain the sample skeleton information.
[0078] The first specified prompt word is obtained by filling the sample table building information, the sample problem, the sample business data, and the preset compression rule into the corresponding positions in the first template. The content output by the large language model can be referred to as specified skeleton information. The specified skeleton information can be automatically evaluated by the large language model to determine whether the specified skeleton information is accurate.
[0079] Optionally, the first specified dimension can be preset, which is a dimension for evaluating the specified skeleton information. The first specified dimension can include object recognition accuracy, structure compliance, symbolization rule compliance, and logical integrity, which can be set according to actual needs. When evaluating the specified skeleton information, the large language model can be provided with necessary context information, which can include the sample problem, the sample SQL statement, the specified skeleton information, the first table building information, the second table building information, the sample business data, and the preset compression rule.
[0080] The skeleton evaluation template can be preset, which is a prompt word template for evaluating skeleton information. The prompt word template can include evaluation rules corresponding to the first specified dimension and output requirements of the skeleton evaluation result. Specifically, the skeleton evaluation template provided by the present embodiment can be: "# Task You are a senior SQL expert and LLM output quality evaluator, required to comprehensively evaluate the generated SQL skeleton based on the given rules and context.
[0081] ## Context input
[0082] - Original question : {question description text}
[0083] - Standard answer SQL : {SQL}
[0084] - SQL skeleton to be evaluated : {SQL_bone}
[0085] - Original table creation information : {Sample_schema}
[0086] - Compressed table creation information : {Sample_schema_zip}
[0087] - Related business documents : {Sample_doc}
[0088] - Symbolization compression rules : {S1-1 Metadata Symbolization Rules}
[0089] ## Evaluation dimensions (sorted by priority)
[0090] 1. Object identification accuracy
[0091] - Whether the database name and table name involved in the problem are identified completely
[0092] - Whether there are redundant or incorrect object references
[0093] 2. Structural compliance
[0094] - Whether the selection of primary tables conforms to business logic (refer to Sample_doc)
[0095] - In the form of SELECT, FROM, JOIN, ON, UNION, etc. (including but not limited to), describe the relationship between database objects involved in the problem
[0096] - Whether the table association level conforms to SQL best practices
[0097] 3. Symbolization rule compliance
[0098] - Whether the compression rules are accurately applied (such as DB1@U symbolization)
[0099] - Whether there are uncompressed cases
[0100] 4. Logical integrity
[0101] - Is the necessary association table missing?
[0102] - Is there a risk of circular references?
[0103] - Whether it conforms to the original SQL association logic
[0104] ## Evaluation Rules
[0105] - Use binary judgment: pass / fail
[0106] - If any core dimension does not meet the standard, it will be judged as failed
[0107] - Specific non-compliance items and corresponding rules and regulations must be clearly stated
[0108] ## Response format requirements
[0109] ```json
[0110] {
[0111] "is_passed": true / false, "strengths": ["Accurate selection of main table",...], "defects": [ { "dimension": "Object recognition accuracy", "description": "Missing order details table DB3@T" } ], "improvement": "Suggest adding associated DB3@T tables and verifying the rationality of LEFT JOIN." }```" By filling the context information into the corresponding position in the skeleton evaluation template, you can get the skeleton evaluation prompt word, and then input the skeleton evaluation prompt word into the large language model so that the large language model can evaluate the specified skeleton information according to the requirements in the skeleton evaluation prompt word and output the skeleton evaluation result according to the corresponding output requirements.
[0112] The skeleton evaluation result can be pass or fail, and if it fails, it also includes corresponding skeleton suggestion information, that is, improvement. If the specified skeleton information passes the skeleton evaluation process, it indicates that the specified skeleton information is accurate, and the skeleton information can be directly determined as the sample skeleton information. If the specified skeleton information fails the skeleton evaluation process, it indicates that the specified skeleton information does not meet the requirements in the first specified dimension, and the specified skeleton information is not accurate enough. Therefore, the corresponding skeleton evaluation result can be used to correct it to obtain the sample skeleton information.
[0113] Optionally, the skeleton suggestion information in the skeleton evaluation result can be obtained, and automatic correction and optimization are performed based on the skeleton suggestion information. For example, the historical dialogue data generated by the large language model when evaluating the specified skeleton information, the skeleton suggestion information and the modification instruction can be fused, so that the large language model automatically corrects and optimizes the specified skeleton information based on the fused information to obtain the sample skeleton information.
[0114] The specific fused information is as follows: [{"role": "user", "content": [skeleton evaluation prompt words]}, {"role": "assistant", "content": [skeleton evaluation result]}, {"role": "user", "content": "Please optimize the SQL skeleton according to the improvement content, so that the score is as high as possible. 5 points, please give me the result directly, you don't need to analyze the process."}] The skeleton evaluation prompt words and the skeleton evaluation result are historical dialogue data, and the rest are modification instructions. Input them into the large language model, and the model can output the sample skeleton information after analysis and reasoning.
[0115] After extracting the sample skeleton information, a sample prediction SQL statement corresponding to the sample problem can be generated based on the sample skeleton information, the sample problem, the sample table building information, the sample business data and the preset compression rule. Specifically, a third template can be pre-set, which is a prompt word template for predicting SQL statements. In the embodiment of the present application, the third template is as follows: # Role and task You are an expert in multi-database SQL generation and optimization, and you need to expand the SQL skeleton into a complete and executable SQL statement based on the context information.
[0116] ## Context input
[0117] - Original question : {question description text}
[0118] - Optimized SQL skeleton : {SQL_bone}
[0119] - Symbolically compressed table creation information : {Sample_schema_zip}
[0120] - Business knowledge base fragment : {Sample_doc}
[0121] - Symbolically compressed rules : {metadata symbolization rules}
[0122] ## Generation requirements
[0123] - The generated SQL needs to be strictly reversed according to the symbolically compressed rules to make the SQL skeleton extended after the SQL complete and executable.
[0124] - The generated SQL is constrained by the `` format. {your response}``` format.
[0125] Among them, fill in the sample question to {question description text}, fill in the sample skeleton information to {SQL_bone}, fill in the sample table creation information to {Sample_schema_zip}, fill in the sample business data to {Sample_doc}, and fill in the preset compression rules to {metadata symbolization rules} to obtain the predicted prompt word. Input the predicted prompt word into the large language model to guide the large language model to output the sample predicted SQL statement corresponding to the sample question.
[0126] Use the consistency between the sample predicted SQL statement and the sample SQL statement to generate sample improvement information, which refers to the part that needs to be improved in the sample predicted SQL statement compared with the sample SQL statement.
[0127] As an implementation, it can be directly compared between the sample predicted SQL statement and the sample SQL statement; if consistent, the improvement information is set to empty; if not consistent, the difference information between the sample predicted SQL statement and the sample SQL statement is taken as the improvement information.
[0128] As another implementation, it can be compared whether the execution results of the sample predicted SQL statement and the sample SQL statement are consistent; if not, the sample problem, the sample SQL statement, the sample predicted SQL statement, and the reference information are used to perform a statement evaluation process on the sample predicted SQL statement in a second specified dimension; if the sample predicted SQL statement passes the statement evaluation process, an artificial summary is obtained as the sample improvement information; if the sample predicted SQL statement fails the statement evaluation process, the sample improvement information is generated according to the statement evaluation process result.
[0129] Since SQL statements can have different expressions, but do not affect the final execution result, in order to accurately evaluate the sample predicted SQL statement, the sample predicted SQL statement can be input into a database executable terminal to obtain the execution result of the sample predicted SQL statement, denoted as a predicted result; the sample SQL statement is input into the database executable terminal to obtain the execution result of the sample SQL statement, denoted as a sample result. If the sample result and the predicted result are consistent, no other processing can be performed.
[0130] If the sample result and the predicted result are inconsistent, it indicates that the sample predicted SQL statement is not accurate enough, and it can be considered that there is a large probability of deviation when generating the SQL statement. Therefore, in order to accurately obtain the deviation when generating the SQL statement, as a prompt for subsequent generation of the SQL statement, a large language model can be used to perform a statement evaluation process on the sample predicted SQL statement.
[0131] Among them, a statement evaluation template can be pre-set, and the statement evaluation template is a prompt word template used for statement evaluation processing. The second specified dimension is an evaluation dimension for statement evaluation, which can be set according to actual needs. In the embodiment of the present application, it can include syntax correctness, semantic accuracy, symbol restoration correctness, execution feasibility, and business logic compliance. Evaluating the sample predicted SQL statement in the second specified dimension is more comprehensive and reliable.
[0132] Optionally, the statement evaluation template includes a plurality of to-be-filled slots for filling the sample problem, the sample SQL statement, the sample predicted SQL statement, and the reference information, wherein the reference information includes second table creation information, sample business data, and a preset compression rule.
[0133] In the embodiment of the present application, the statement evaluation template is specifically as follows: "# Task You are a senior SQL expert and LLM output quality evaluator, and need to comprehensively evaluate the generated complete SQL based on the given rules and context.
[0134] ## Context input
[0135] - Original question : {question description text}
[0136] - Standard answer SQL : {SQL}
[0137] - SQL prediction result to be evaluated : {SQL_pred}
[0138] - SQL skeleton information : {SQL_bone}
[0139] - Compressed table creation information : {Sample_schema_zip}
[0140] - Related business documents : {Sample_doc}
[0141] - Symbolic compression rules : {S1-1 metadata symbolic compression rules}
[0142] ## Evaluation dimensions
[0143] 1. Syntax correctness
[0144] - Whether it meets the SQL syntax specifications of the target database
[0145] - Whether there are syntax errors (such as missing keywords, symbol misplacement)
[0146] 2. Semantic accuracy
[0147] - Whether all data elements required by the question requirements are fully implemented
[0148] - Whether the WHERE condition, JOIN logic, etc. are equivalent to the standard answer
[0149] - Whether the calculation result field matches the requirements
[0150] 3. Symbol restoration correctness
[0151] - Whether the inverse conversion of symbolic rules is accurately applied
[0152] - Whether the database name, table name, and column name are completely restored to their original names
[0153] - Whether there is an uncompressed symbolic representation
[0154] 4. Implementation feasibility
[0155] - Is there a risk of runtime errors (such as field non-existence, type mismatch)?
[0156] - Whether to include virtual tables / databases that do not actually exist
[0157] - Whether the database constraints (such as primary and foreign key relationships) are met
[0158] 5. Business logic compliance
[0159] - Whether the field calculation logic complies with the business document definition
[0160] - Whether the table join order follows the best practice
[0161] - Whether business terms are used appropriately
[0162] ## Evaluation Rules
[0163] - Use binary judgment: pass / fail
[0164] - If any of the core dimensions (the first four) do not meet the standards, the candidate will be deemed to have failed.
[0165] - Specific non-compliance items and corresponding rules and regulations must be clearly stated
[0166] ## Response format requirements
[0167] ```json
[0168] {
[0169] "is_passed": true / false, "strengths": ["Complete grammatical structure", "Accurate conditional logic",...], "defects": [ { "dimension": "Symbolic restoration correctness", "description": "DB1@U was not restored to user_db" } ], "improvement": "Suggest to check the symbol mapping table and make sure that all compressed symbols are reversed" }```” Wherein, {question description text} can be filled in sample questions; {SQL} can be filled in sample SQL statements; {SQL_pred} can be filled in sample predicted SQL statements; {SQL_bone} can be filled in sample skeleton information; {Sample_schema_zip} can be filled in sample table creation information determined from the second table creation information. The specific determination method of the sample table creation information can refer to the description of the corresponding part described above, and will not be repeated here. {Sample_doc} can be filled in sample business data, and {metadata symbolization rule} can be filled in the preset compression rule. After filling, the sentence evaluation prompt word can be obtained. Input the sentence evaluation prompt word into the large language model, and the sentence evaluation result can be obtained.
[0170] It should be noted that only when the execution results of the sample predicted SQL statement and the sample SQL statement are inconsistent, the sentence evaluation processing will be performed, and the sentence evaluation result should be failed. If the sentence evaluation result is passed, the artificial summary can be obtained by professional personnel analysis and summary, which can be directly used as sample improvement information.
[0171] If the sentence evaluation result is failed, the suggestion information carried in the corresponding sentence evaluation result can be obtained, for example, the content corresponding to the improvement field in the foregoing sentence evaluation template, which can be directly used as sample improvement suggestion.
[0172] Thus, each training sample can include a sample question, a sample SQL statement, a sample skeleton information, and a sample improvement information. Among them, for the training sample with the same sample predicted SQL statement and sample SQL statement, the sample improvement information is empty; for the training sample with the sample predicted SQL statement passing the sentence evaluation processing, the sample improvement information is the summary information obtained by artificial analysis; for the training sample with the sample predicted SQL statement passing the sentence evaluation processing, the sample improvement information is the content corresponding to the improvement in the sentence evaluation processing result.
[0173] According to the above manner, a plurality of training samples can be constructed, and these training samples can be divided into a training set and a validation set according to a certain proportion. For example, the training set and the validation set are divided according to the proportion of 8:2.
[0174] For each training sample in the training set, a first sample prompt can be constructed according to the reference information and the sample question, wherein the first sample prompt is a prompt from natural language to schema information during model training. Similarly, the first sample prompt can be obtained by filling the corresponding information into the first template. The difference is that in the {Sample_schema_zip} of the first template, in addition to filling in the corresponding sample table building information, part of the noise can be randomly added. These noises can be randomly extracted from the remaining second table building information. The first sample prompt is input into the base model to obtain the corresponding predicted schema information.
[0175] Based on the predicted schema information, the sample improvement information and the reference information, a second sample prompt is constructed, wherein the second sample prompt is a prompt from schema information to SQL statement during model training. Similarly, the second sample prompt can be obtained by filling the corresponding information into the third template. The difference is that in the context information of the third template, "- Reference : {evidence} ” is added for filling in the sample improvement information, and the remaining content can refer to the corresponding description in the foregoing embodiments.
[0176] At this time, for a training sample, the predicted schema information and the predicted SQL statement corresponding to the sample question in the training sample can be obtained by using the base model. The sample reward of the training sample can be calculated by the consistency of the predicted schema information and the sample schema information, and the consistency of the predicted SQL statement and the sample SQL statement.
[0177] Wherein, the sample reward can include a format reward and an execution reward, the format reward can include a schema format reward and a statement format reward, the execution reward can include a schema execution reward and a statement execution reward, when calculating the sample reward, the schema format reward can be determined according to the inclusion relationship between the predicted schema information and the schema label; the statement format reward can be determined according to the inclusion relationship between the predicted SQL statement and the statement label; the schema execution reward can be determined according to the execution result of the predicted schema information and the sample schema information; the statement execution reward can be determined according to the execution result of the predicted SQL statement and the sample SQL statement; and the sample reward can be obtained by accumulating the schema format reward, the statement format reward, the schema execution reward and the statement execution reward.
[0178] For example, refer to Figure 5 , which shows a schematic diagram for calculating the sample reward. Wherein, the schema label is a label carried by the base model when normally inferring the predicted schema information, and the statement label is a label carried by the base model when normally inferring the predicted SQL statement. The schema label and the statement label can be set according to actual needs. In the embodiments of the present application, the schema label can be set as <reasoning>< / reasoning> <bone>< / bone> For the skeleton label, if the skeleton label is carried in the model output generating the predicted skeleton information, the skeleton format reward is set to a first value, if only a first number of the four skeleton labels is missing, the skeleton format reward can be set to a second value, otherwise, it is set to a third value. The first value can be 0.5, the first number can be 1, the second value can be 0.01, and the third value can be 0.
[0179] The statement label can be set to <reasoning>< / reasoning> <sql>< / sql> For the statement label, if the statement label is carried in the model output generating the predicted SQL information, the statement format reward is set to a fourth value, if only a second number of the four statement labels is missing, the statement format reward can be set to a fifth value, otherwise, it is set to a sixth value. The fourth value can be 0.5, the second number can be 1, the fifth value can be 0.01, and the sixth value can be 0.
[0180] For the execution reward, there can be a skeleton execution reward and a statement execution reward. For the skeleton execution reward, the predicted skeleton information and the sample skeleton information can be cleaned, for example, the carriage return line feed is cleaned, the space symbol is used as a separator, the continuous space is treated as a space, and then the list obtained by the two skeleton information is converted into a set, and whether the two sets are consistent is compared; if consistent, the skeleton execution reward is set to a seventh value; otherwise, the skeleton execution reward is set to an eighth value. The seventh value can be 0.5, and the eighth value can be 0.
[0181] For the statement execution reward, the sample SQL statement and the predicted SQL statement can be input into the SQL terminal for execution, and the execution results of the two SQL statements are compared; if the execution results are consistent, the statement execution reward is set to a ninth value; if the execution results are inconsistent, the statement execution reward is set to a tenth value. The ninth value can be 0.5, and the tenth value can be 0.
[0182] In this way, the skeleton format reward, the statement format reward, the skeleton execution reward, and the statement execution reward can be superimposed to obtain a sample reward. Based on the sample reward, the base model can be learned in the direction of being able to obtain a higher cumulative reward, and the base model can adjust its model parameters according to the reward signal to maximize the long-term reward. In the training process of the base model, a validation set can be used, and the model parameters with the highest average reward score are reserved as the final specified model. The training method is various, and can be selected according to actual needs. In the embodiment of the present application, GRPO training can be used.
[0183] The multi-database SQL statement generation solution provided by the embodiments of the present invention can be applied in various scenarios involving complex SQL statement generation, such as recommendation scenarios, data analysis scenarios, and industrial production scenarios. The solution provided by the embodiments of the present invention can maximize the compression of table creation information corresponding to multiple databases, minimize the loss of key information, and improve the accuracy of SQL statement generation.
[0184] During the training of a specified model, skeleton and statement evaluation mechanisms are introduced when collecting multiple training samples to ensure accurate sample skeleton information and sample improvement information. In subsequent actual training, the predicted skeleton information is first generated, and then the predicted SQL statements generated by the sample improvement information are introduced. By combining the formats and execution rewards of the two parts and adjusting the model parameters, it can be ensured that the specified model can accurately generate SQL statements based on the provided data.
[0185] To better implement the above method, an embodiment of the present invention further provides a multi-repository-based SQL statement generation device. The multi-repository-based SQL statement generation device can be integrated into an electronic device, such as a terminal or a server. The terminal can be a mobile phone, tablet computer, smart Bluetooth device, laptop computer, personal computer, or other device; the server can be a single server or a server cluster consisting of multiple servers.
[0186] For example, in this embodiment, the method of the embodiment of the present invention will be described in detail by taking the specific integration of the SQL statement generation device based on multiple libraries in the server as an example.
[0187] For example, Figure 6 As shown, the multi-library-based SQL statement generation device 200 may include a table building acquisition module 210 , a first determination module 220 , a skeleton recognition module 230 , a second determination module 240 and a target generation module 250 .
[0188] A table creation acquisition module 210 is configured to acquire first table creation information and second table creation information for multiple databases in a target scenario, wherein the first table creation information is original table creation information, and the second table creation information is information obtained by compressing the length of the first table creation information according to a preset compression rule; A first determining module 220 is configured to determine, based on an input length limit of a specified model, first reference table building information corresponding to a query to be processed from the second table building information, where the query to be processed is a query in the target scenario; A skeleton identification module 230 is configured to control the designated model to identify database objects and object relationships related to the query to be processed based on the first reference table building information and preset compression rules, and obtain skeleton information; The second determining module 240 is configured to search second reference table information from the second table information according to the skeleton information. The target generating module 250 is configured to control the specified model to expand the skeleton information according to the second reference table information and the to-be-processed query, so as to obtain a target SQL statement corresponding to the to-be-processed query.
[0189] In some embodiments, the preset compression rule includes a mapping relationship between a preset type and a sub-compression rule, and the table obtaining module 210 is specifically configured to: obtain first table information of a plurality of databases in a target scenario; divide content in the first table information according to the preset type, so as to obtain a plurality of sub-contents; compress a length of each sub-content according to a preset compression rule corresponding to the preset type, so as to obtain compressed content; organize all the compressed content as second table information.
[0190] In some embodiments, the first determining module 220 is specifically configured to: if the length of the second table information is not greater than an input length limit of the specified model, the second table information is taken as the first reference table information; if the length of the second table information is greater than the input length limit of the specified model, a first candidate table information is determined from the second table information according to a similarity between the to-be-processed query and the first table information; a second candidate table information is determined from the second table information according to a similarity between the to-be-processed query and the second table information; the first candidate table information and the second candidate table information are merged as the first reference table information.
[0191] In some embodiments, the skeleton recognizing module 230 is specifically configured to: determine reference business data according to a similarity between business data in a target scenario and the to-be-processed query; construct a first prompt word based on the reference business data, the first reference table information, and the preset compression rule; use the first prompt word to guide the specified model to recognize a database object and an object relationship involved in the to-be-processed query, so as to obtain skeleton information.
[0192] In some embodiments, the target generating module 250 is specifically configured to: determine reference business data according to a similarity between business data in a target scenario and the to-be-processed query; construct a second prompt word based on the second reference table building information, the skeleton information, the to-be-processed query, and the reference business data; guide the specified model to perform expansion processing on the skeleton information by using the second prompt word, to obtain a target SQL statement corresponding to the to-be-processed query.
[0193] In some embodiments, the multi-database-based SQL statement generation apparatus further includes a training module, which is specifically configured to: obtain a plurality of training samples, the training samples including sample questions, sample SQL statements, sample skeleton information, and sample improvement information; for each training sample, construct a first sample prompt word based on reference information and the sample question, and guide a basic model to infer predicted skeleton information based on the first sample prompt word, the reference information including second table building information, sample business data, and a preset compression rule; construct a second sample prompt word based on the predicted skeleton information, the sample improvement information, and the reference information, and guide the basic model to infer a predicted SQL statement based on the second sample prompt word; calculate a sample reward of the training sample based on consistency of the sample skeleton information and the predicted skeleton information and consistency of the predicted SQL statement and the sample SQL statement; adjust model parameters of the basic model based on sample rewards of all training samples, to obtain a specified model.
[0194] In some embodiments, the training module is specifically configured to: obtain a plurality of SQL question and answer pairs and business data in a target scenario, the SQL question and answer pairs including sample questions and sample SQL statements; extract sample database objects from each sample SQL statement, and obtain second table building information corresponding to the sample database objects, to obtain sample table building information; determine sample business data according to similarity between the business data and the sample questions; extract sample skeleton information from the sample questions based on the sample table building information, the sample business data, and the preset compression rule; generate a sample predicted SQL statement corresponding to the sample questions based on the sample skeleton information, the sample questions, the sample table building information, the sample business data, and the preset compression rule; generate sample improvement information based on consistency between the sample predicted SQL statement and the sample SQL statement.
[0195] In some embodiments, the training module is specifically configured to: construct a first specified prompt word based on the table building information of the sample, the sample business data, and a preset compression rule; extract the specified skeleton information corresponding to the sample question by using the first specified prompt word to guide the large language model; perform skeleton evaluation processing on the specified skeleton information in a first specified dimension; If the specified skeleton information passes the skeleton evaluation processing, the specified skeleton information is determined as sample skeleton information; If the specified skeleton information does not pass the skeleton evaluation processing, the specified skeleton information is optimized according to the skeleton evaluation processing result to obtain sample skeleton information.
[0196] In some embodiments, the training module is specifically configured to: compare whether the execution results of the sample predicted SQL statement and the sample SQL statement are consistent; If not, perform statement evaluation processing on the sample predicted SQL statement in a second specified dimension by using the sample question, the sample SQL statement, the sample predicted SQL statement, and the reference information; If the sample predicted SQL statement passes the statement evaluation processing, obtain summary information as the sample improvement information; If the sample predicted SQL statement does not pass the statement evaluation processing, generate the sample improvement information according to the statement evaluation processing result.
[0197] In specific implementation, each of the above modules can be implemented as an independent entity, or can be combined as the same or several entities. The specific implementation of each unit can be referred to the method embodiments above, and will not be described here.
[0198] As can be seen from the above, the SQL statement generation device based on multiple databases can compress the length of the table building information of multiple databases to shorten the length of the table building information. In subsequent processing, the first reference table building information and the second reference table building information are determined from the compressed second table building information. The skeleton information is identified according to the first reference table building information and the preset compression rule by using the specified model. The skeleton information is expanded by using the second reference table building information by the specified model to obtain the target SQL statement. By compressing the length of the table building information, the length of the table building information in the specified model can be greatly shortened, the information loss is minimized, and the accuracy of generating SQL in the multiple database scenario can be improved.
[0199] An embodiment of the present invention further provides an electronic device, which may be a terminal, a server, or the like. The terminal may be a mobile phone, a tablet computer, a smart Bluetooth device, a laptop computer, a personal computer, or the like; the server may be a single server or a server cluster consisting of multiple servers, or the like.
[0200] In some embodiments, the multi-library-based SQL statement generation device can also be integrated into multiple electronic devices. For example, the multi-library-based SQL statement generation device can be integrated into multiple servers, and the multi-library-based SQL statement generation method of the present invention can be implemented by multiple servers.
[0201] In this embodiment, the electronic device of this embodiment is a server as an example for detailed description, for example, Figure 7 , which shows a schematic structural diagram of an electronic device involved in an embodiment of the present invention, specifically: The electronic device may include one or more processing core processors 310, one or more computer-readable storage media memories 320, a power supply 330, an input module 340, and a communication module 350. Those skilled in the art will appreciate that Figure 7 The electronic device structure shown in the figure does not constitute a limitation of the electronic device, and may include more or fewer components than shown in the figure, or combine certain components, or arrange components differently. The processor 310 is the control center of the electronic device. It connects all parts of the electronic device using various interfaces and circuits. It executes software programs and / or modules stored in the memory 320 and accesses data stored in the memory 320 to perform various functions of the electronic device and process data. In some embodiments, the processor 310 may include one or more processing cores. In some embodiments, the processor 310 may integrate an application processor and a modem processor. The application processor primarily handles the operating system, user interface, and application programs, while the modem processor primarily handles wireless communications. It is understood that the modem processor may not be integrated into the processor 310.
[0202] The memory 320 can be used to store software programs and modules, and the processor 310 can execute various function applications and data processing by running the software programs and modules stored in the memory 320. The memory 320 can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, application programs required by at least one function (such as a sound playing function, an image playing function, etc.), and the like; and the data storage area can store data created according to the use of the electronic device, etc. In addition, the memory 320 can include a high-speed random access memory, and can also include a non-volatile memory, such as at least one magnetic disk storage device, a flash memory device, or other volatile solid-state memory device. Accordingly, the memory 320 can also include a memory controller to provide the processor 310 with access to the memory 320.
[0203] The electronic device also includes a power supply 330 for powering the various components, and in some embodiments, the power supply 330 can be logically connected to the processor 310 through a power management system, so that the power management system can be used to manage charging, discharging, and power consumption management, etc. The power supply 330 can also include one or more direct current or alternating current power supplies, recharging systems, power failure detection circuits, power converters or inverters, power status indicators, etc.
[0204] The electronic device can also include an input module 340, which can be used to receive input digital or character information, and to generate keyboard, mouse, joystick, optical or trackball signal inputs related to user settings and function controls.
[0205] The electronic device can also include a communication module 350, which in some embodiments can include a wireless module, and the electronic device can use the wireless module of the communication module 350 for short-range wireless transmission, thereby providing the user with wireless broadband Internet access. For example, the communication module 350 can be used to help the user send and receive emails, browse web pages, and access streaming media, etc.
[0206] Although not shown, the electronic device can also include a display unit, etc., which will not be described here. In particular, in the present embodiment, the processor 310 in the electronic device will load the executable file corresponding to the process of one or more application programs into the memory 320 according to the following instructions, and the processor 310 will run the application programs stored in the memory 320, thereby implementing the steps in the method of each embodiment of the present application.
[0207] The specific implementation of each of the above operations can refer to the previous embodiments, which will not be described here.
[0208] As can be seen, the electronic device provided in the embodiment of the present application can compress the length of the table building information of the plurality of databases, so as to shorten the length of the table building information, determine the first reference table building information and the second reference table building information from the compressed second table building information in subsequent processing, recognize the skeleton information according to the first reference table building information and the preset compression rule by using the specified model, and expand the skeleton information by using the second reference table building information by the specified model to obtain the target SQL statement. By compressing the length of the table building information, the length of the table building information in the specified model can be greatly shortened, information loss is minimized, and the accuracy of the generated SQL in the multi-database scenario can be improved.
[0209] Those skilled in the art can understand that all or part of the steps in the various methods of the above embodiments can be completed by instructions, or by controlling related hardware by the instructions, which can be stored in a computer readable storage medium and loaded and executed by a processor.
[0210] To this end, the embodiment of the present application provides a computer readable storage medium, which stores a plurality of instructions capable of being loaded by a processor to execute the steps in any of the SQL generation methods based on multiple databases provided by the embodiment of the present application.
[0211] The storage medium can include a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.
[0212] According to an aspect of the present application, a computer program product or computer program is provided, which includes computer programs / instructions stored in a computer readable storage medium. A processor of an electronic device reads the computer programs / instructions from the computer readable storage medium, and the processor executes the computer programs / instructions, so that the electronic device executes the methods provided in the various optional implementations of the SQL generation aspects or the specified model training aspects provided in the above embodiments.
[0213] The above describes in detail the method and device for generating SQL based on multiple databases provided by the embodiment of the present application. The principles and implementation manners of the present application are described by applying specific examples. The above embodiment is only used to help understand the method and core idea of the present application. Meanwhile, for those skilled in the art, according to the idea of the present application, the specific implementation manner and application range can be changed. In summary, the content of the present description should not be understood as limiting the present application.< / t>
Claims
1. A method for generating SQL statements based on multiple databases, characterized in that: The method comprises: Obtaining first table creation information and second table creation information of multiple databases in a target scenario, where the first table creation information is original table creation information, and the second table creation information is information obtained by compressing the length of the first table creation information according to a preset compression rule; Determining, based on an input length limit of a specified model, first reference table building information corresponding to a query to be processed from the second table building information, where the query to be processed is a query in the target scenario; Controlling the designated model to identify database objects and object relationships related to the query to be processed according to the first reference table building information and preset compression rules, and obtaining skeleton information; Retrieving second reference table building information from the second table building information using the skeleton information; The designated model is controlled to perform expansion processing on the skeleton information according to the second reference table building information and the query to be processed, so as to obtain a target SQL statement corresponding to the query to be processed.
2. The method according to claim 1, characterized in that The preset compression rule includes a mapping relationship between a preset type and a sub-compression rule. The obtaining of first table creation information and second table creation information of multiple databases in a target scenario includes: Obtain the first table creation information of multiple databases in the target scenario; Dividing the content in the first table creation information according to the preset type to obtain multiple sub-contents; Compress the length of each sub-content according to the preset compression rule corresponding to the preset type to obtain compressed content; All compressed contents are organized as the second table creation information.
3. The method according to claim 1, characterized in that The determining, based on the input length limit of the specified model, first reference table creation information corresponding to the query to be processed from the second table creation information includes: If the length of the second table creation information is not greater than the input length limit of the specified model, the second table creation information is used as the first reference table creation information; If the length of the second table building information is greater than the input length limit of the specified model, determining first candidate table building information from the second table building information according to the similarity between the query to be processed and the first table building information; determining second candidate table creation information from the second table creation information based on a similarity between the query to be processed and the second table creation information; The first candidate table creation information and the second candidate table creation information are combined as first reference table creation information.
4. The method according to claim 1, wherein Controlling the designated model to identify database objects and object relationships related to the query to be processed based on the first reference table building information and preset compression rules to obtain skeleton information includes: Determining reference business data based on the similarity between the business data in the target scenario and the query to be processed; Constructing a first prompt word using the reference business data, the first reference table building information, and the preset compression rule; The first prompt word is used to guide the designated model to identify database objects and object relationships involved in the query to be processed, and obtain skeleton information.
5. The method according to claim 1, wherein The controlling the designated model to perform expansion processing on the skeleton information according to the second reference table creation information and the query to be processed to obtain a target SQL statement corresponding to the query to be processed includes: Determining reference business data based on the similarity between the business data in the target scenario and the query to be processed; Constructing a second prompt word using the second reference table creation information, the skeleton information, the query to be processed, and the reference business data; The second prompt word is used to guide the designated model to perform expansion processing on the skeleton information to obtain a target SQL statement corresponding to the query to be processed.
6. The method according to any one of claims 1 to 5, characterized in that The specified model is trained in the following way: Acquire multiple training samples, wherein the training samples include sample questions, sample SQL statements, sample skeleton information, and sample improvement information; For each training sample, construct a first sample prompt word using reference information and the sample question, and infer predicted skeleton information based on a guided basic model of the first sample prompt word, wherein the reference information includes the second table construction information, sample business data, and preset compression rules; Constructing a second sample prompt word using the prediction skeleton information, the sample improvement information, and the reference information, and guiding the basic model to infer a predicted SQL statement based on the second sample prompt word; Calculating a sample reward for the training sample using the consistency between the sample skeleton information and the predicted skeleton information and the consistency between the predicted SQL statement and the sample SQL statement; The model parameters of the base model are adjusted using the sample rewards of all training samples to obtain a specified model.
7. The method according to claim 6, characterized in that The obtaining of multiple training samples includes: Acquire multiple SQL question-answer pairs and business data in a target scenario, wherein the SQL question-answer pairs include sample questions and sample SQL statements; Extracting a sample database object from each sample SQL statement, and obtaining second table creation information corresponding to the sample database object to obtain sample table creation information; Determining sample business data based on the similarity between the business data and the sample question; Extracting sample skeleton information from the sample question according to the sample table building information, sample business data, and the preset compression rule; Generate a sample prediction SQL statement corresponding to the sample question based on the sample skeleton information, sample question, sample table creation information, sample business data, and preset compression rules; Based on the consistency between the sample prediction SQL statement and the sample SQL statement, sample improvement information is generated.
8. The method according to claim 7, characterized in that The extracting sample skeleton information from the sample question according to the sample table building information, the sample business data and the preset compression rule includes: Constructing a first designated prompt word based on sample table creation information, sample business data, and preset compression rules; Using the first designated prompt word to guide the large language model to extract designated skeleton information corresponding to the sample question; performing skeleton evaluation processing on the specified skeleton information in a first specified dimension; If the designated skeleton information passes the skeleton evaluation process, determining the designated skeleton information as sample skeleton information; If the designated skeleton information fails the skeleton evaluation process, the designated skeleton information is optimized according to the skeleton evaluation process result to obtain sample skeleton information.
9. The method according to claim 7, characterized in that The generating sample improvement information based on the consistency between the sample prediction SQL statement and the sample SQL statement includes: Comparing the sample prediction SQL statement with the execution result of the sample SQL statement to see whether they are consistent; If they are inconsistent, performing statement evaluation processing on the sample predicted SQL statement in a second specified dimension using the sample question, the sample SQL statement, the sample predicted SQL statement, and the reference information; If the sample prediction SQL statement passes the statement evaluation process, obtaining summary information as the sample improvement information; If the sample prediction SQL statement fails the statement evaluation process, the sample improvement information is generated according to the statement evaluation process result.
10. A multi-library-based SQL statement generation device, the device being used to implement the method according to any one of claims 1 to 9, characterized in that: The device comprises: A table creation acquisition module is configured to acquire first table creation information and second table creation information of multiple databases in a target scenario, wherein the first table creation information is original table creation information, and the second table creation information is information obtained by compressing the length of the first table creation information according to a preset compression rule; a first determining module, configured to determine, from the second table building information, first reference table building information corresponding to a query to be processed based on an input length limit of a specified model, where the query to be processed is a query in the target scenario; a skeleton recognition module, configured to control the designated model to identify database objects and object relationships related to the query to be processed based on the first reference table building information and preset compression rules, and obtain skeleton information; A second determining module is configured to retrieve second reference table building information from the second table building information using the skeleton information; The target generation module is used to control the specified model to expand the skeleton information according to the second reference table building information and the query to be processed, and obtain the target SQL statement corresponding to the query to be processed.
Citation Information
Patent Citations
Database statement generation method and device, equipment and medium
CN117827885A
Business query language generation method, system, equipment and medium
CN118939679A
Optimization of database query
US20140095469A1