A data query statement generation method, system, device and storage medium
By using a preset statement generation model and syntax correction algorithm, SQL query statements are automatically constructed, solving the problem of low efficiency in manually constructing SQL statements and achieving efficient and accurate data querying.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEIJING SHANGYIN MICRO CORE TECH CO LTD
- Filing Date
- 2022-11-10
- Publication Date
- 2026-05-01
AI Technical Summary
In existing technologies, users need to manually construct SQL statements to query data, resulting in low query efficiency and accuracy. This is especially difficult and yields poor query results for those unfamiliar with SQL syntax.
The data query corpus is processed by three sub-models (first sub-model, second sub-model and third sub-model) of the preset statement generation model to determine the target data table, target fields and logical relationships respectively, and a preset syntax correction algorithm is used to generate query statements that conform to the SQL syntax structure.
It enables the automatic generation of data query statements that conform to SQL syntax, improving the accuracy and efficiency of data queries and avoiding the reduction in query efficiency and accuracy caused by redundant data.
Smart Images

Figure CN115617841B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of data processing technology, and in particular to a method, system, device, and storage medium for generating data query statements. Background Technology
[0002] A database is a repository for organizing, storing, and managing data based on data structures. With the development of internet technology, the structure of data tables stored in databases has become increasingly complex. Currently, the way to query data from a database requires users to outline their data query requirements based on the data to be queried, and then construct a Structured Query Language (SQL) based on those requirements to complete the data query.
[0003] However, constructing SQL statements based on data query requirements requires manual work by operators familiar with SQL syntax, which reduces the efficiency of data querying. Furthermore, for operators unfamiliar with SQL syntax, constructing SQL statements is too difficult and can lead to reduced accuracy and efficiency in data queries. Therefore, improving the accuracy and efficiency of data queries has become an urgent problem to be solved. Summary of the Invention
[0004] The purpose of this invention is to provide a method, system, device, and storage medium for generating data query statements, so as to automatically generate data query statements that meet the SQL syntax structure, thereby improving the accuracy and efficiency of data querying. The specific technical solution is as follows:
[0005] A method for generating a data query statement, the method comprising:
[0006] An initial query corpus is obtained, and the initial query corpus is preprocessed to obtain a data query corpus, wherein the initial query corpus is natural language;
[0007] The data query corpus is input into the first sub-model so that the first sub-model can determine the target data table corresponding to the data query corpus from multiple data tables in the database. The first sub-model is a sub-model in a preset sentence generation model.
[0008] The data query corpus and the target data table are input into the second sub-model so that the second sub-model can determine, based on the data query corpus, multiple target fields in the target data table that satisfy a preset matching relationship with the data query corpus. The second sub-model is a sub-model in the preset sentence generation model.
[0009] The data query corpus and each of the target fields are input into the third sub-model so that the third sub-model can determine multiple logical relationships between the target fields based on the data query corpus. The third sub-model is a sub-model in the preset sentence generation model.
[0010] The target data table number, each target field, and each logical relationship are added to their respective positions in the preset statement template to obtain the initial query statement, wherein the initial query statement is a statement edited based on a programming language;
[0011] The initial query statement is grammatically corrected using a preset syntax correction algorithm to generate a data query statement.
[0012] Optionally, the training process of the preset statement generation model includes:
[0013] Multiple initial training sample data are obtained, wherein the initial training sample data consists of sample corpora and their corresponding sample data tables;
[0014] For each of the initial training sample data: perform word segmentation on the sample corpus, and determine the correspondence between each word segmentation result and each field in the sample data table; based on the sample corpus, the number of the sample data table, and each of the correspondences, generate training sample data corresponding to the initial training sample data;
[0015] Using the training sample data, the initial first sub-model, the initial second sub-model, and the initial third sub-model are trained respectively to obtain the preset statement generation model composed of the first sub-model, the second sub-model, and the third sub-model. The input of the preset statement generation model is the data query corpus, and the output is the target data table, each of the target fields, and each of the logical relationships.
[0016] Optionally, the step of inputting the data query corpus into the first sub-model, so that the first sub-model determines the target data table corresponding to the data query corpus from multiple data tables in the database, includes:
[0017] The data query corpus is concatenated with the names of each data table in the database to obtain multiple first input data that satisfy the input format of the first sub-model.
[0018] Each of the first input data is input into the first sub-model, so that the first sub-model calculates the matching degree between the data query corpus and the data table in each of the first input data, and determines the data table corresponding to the matching degree with the largest value as the target data table.
[0019] Optionally, the step of inputting the data query corpus and the target data table into the second sub-model, so that the second sub-model determines, based on the data query corpus, multiple target fields in the target data table that satisfy a preset matching relationship with the data query corpus, includes:
[0020] Extract the field names from the target data table;
[0021] The data query corpus is concatenated with each of the field names to obtain second input data that satisfies the input format of the second sub-model;
[0022] The second input data is input into the second sub-model, so that the second sub-model determines whether each field name in the second input data and the data query corpus satisfy at least one matching relationship in the preset matching relationship group. If so, the field corresponding to the field name that satisfies at least one matching relationship is determined as the target field, and the matching relationship is added as a label to the target field.
[0023] Optionally, the step of inputting the data query corpus and each of the target fields into the third sub-model, so that the third sub-model determines multiple logical relationships between the target fields based on the data query corpus, includes:
[0024] Extract at least one keyword character of data type "keyword" from the data query corpus;
[0025] For each target field: determine whether the target field satisfies at least one of the matching relationships, and whether the matching relationship in which the data type of the target field is a condition field is included. If so, then the target field is determined as a condition field.
[0026] Obtain multiple logical symbol identifiers corresponding to the field types of each of the aforementioned condition fields;
[0027] Based on the content of each field, determine the correspondence between each field name and each preset field type, and construct multiple three-element data groups based on the correspondence.
[0028] Each of the three data sets is input into the third sub-model so that the third sub-model performs binary classification on each of the three data sets and, based on the classification results, determines at least one of the three data sets with a matching degree greater than a preset threshold as the target three data set. The three data set includes a keyword character, a condition field and its corresponding logical symbol identifier.
[0029] The logical symbol representation in the target three-dimensional data set is determined as the logical relation.
[0030] Optionally, the step of adding the target data table number, each target field, and each logical relationship to their respective positions in the preset statement template to obtain the initial query statement includes:
[0031] Obtain the mapping identifier of each target field and the number of the target data table;
[0032] Add the target data table number to the data table lookup position in the preset statement template;
[0033] Each of the aforementioned field names is added to its corresponding position in the preset statement template, wherein the position corresponds to the mapping identifier;
[0034] Add the logical symbol corresponding to the logical relationship to the logical symbol position in the preset statement template;
[0035] Obtain the initial query statement.
[0036] Optionally, the preprocessing operation on the initial query corpus to obtain the data query corpus includes:
[0037] Using a preset regular expression matching algorithm, text characters representing dates and numbers in the initial query corpus are found, and the text characters are converted into numeric characters.
[0038] A data query statement generation system, the system comprising:
[0039] The corpus processing module is used to obtain an initial query corpus and perform preprocessing operations on the initial query corpus to obtain a data query corpus, wherein the initial query corpus is natural language;
[0040] The first data determination module is used to input the data query corpus into the first sub-model so that the first sub-model can determine the target data table corresponding to the data query corpus from multiple data tables in the database. The first sub-model is a sub-model in a preset sentence generation model.
[0041] The second data determination module is used to input the data query corpus and the target data table into the second sub-model, so that the second sub-model determines multiple target fields in the target data table that satisfy a preset matching relationship with the data query corpus based on the data query corpus. The second sub-model is a sub-model in the preset sentence generation model.
[0042] The third data determination module is used to input the data query corpus and each of the target fields into the third sub-model, so that the third sub-model can determine multiple logical relationships between the target fields based on the data query corpus. The third sub-model is a sub-model in the preset sentence generation model.
[0043] The data filling module is used to add the target data table number, each target field and each logical relationship to their respective positions in the preset statement template to obtain an initial query statement, wherein the initial query statement is a statement edited based on a programming language;
[0044] The statement generation module is used to perform syntax correction on the initial query statement using a preset syntax correction algorithm to generate a data query statement.
[0045] Optionally, the system further includes a model training module, which is configured to: When training the preset statement generation model, the model training module is set to:
[0046] Multiple initial training sample data are obtained, wherein the initial training sample data consists of sample corpora and their corresponding sample data tables;
[0047] For each of the initial training sample data: perform word segmentation on the sample corpus, and determine the correspondence between each word segmentation result and each field in the sample data table; based on the sample corpus, the number of the sample data table, and each of the correspondences, generate training sample data corresponding to the initial training sample data;
[0048] Using the training sample data, the initial first sub-model, the initial second sub-model, and the initial third sub-model are trained respectively to obtain the preset statement generation model composed of the first sub-model, the second sub-model, and the third sub-model. The input of the preset statement generation model is the data query corpus, and the output is the target data table, each of the target fields, and each of the logical relationships.
[0049] Optionally, the first data determining module is configured as follows:
[0050] The data query corpus is concatenated with the names of each data table in the database to obtain multiple first input data that satisfy the input format of the first sub-model.
[0051] Each of the first input data is input into the first sub-model, so that the first sub-model calculates the matching degree between the data query corpus and the data table in each of the first input data, and determines the data table corresponding to the matching degree with the largest value as the target data table.
[0052] Optionally, the second data determining module is configured as follows:
[0053] Extract the field names from the target data table;
[0054] The data query corpus is concatenated with each of the field names to obtain second input data that satisfies the input format of the second sub-model;
[0055] The second input data is input into the second sub-model, so that the second sub-model determines whether each field name in the second input data and the data query corpus satisfy at least one matching relationship in the preset matching relationship group. If so, the field corresponding to the field name that satisfies at least one matching relationship is determined as the target field, and the matching relationship is added as a label to the target field.
[0056] Optionally, the third data determination module is configured as follows:
[0057] Extract at least one keyword character of data type "keyword" from the data query corpus;
[0058] For each target field: determine whether the target field satisfies at least one of the matching relationships, and whether the matching relationship in which the data type of the target field is a condition field is included. If so, then the target field is determined as a condition field.
[0059] Obtain multiple logical symbol identifiers corresponding to the field types of each of the aforementioned condition fields;
[0060] Based on the content of each field, determine the correspondence between each field name and each preset field type, and construct multiple three-element data groups based on the correspondence.
[0061] Each of the three data sets is input into the third sub-model so that the third sub-model performs binary classification on each of the three data sets and, based on the classification results, determines at least one of the three data sets with a matching degree greater than a preset threshold as the target three data set. The three data set includes a keyword character, a condition field and its corresponding logical symbol identifier.
[0062] The logical symbol representation in the target three-dimensional data set is determined as the logical relation.
[0063] Optionally, the data filling module is configured as follows:
[0064] Obtain the mapping identifier of each target field and the number of the target data table;
[0065] Add the target data table number to the data table lookup position in the preset statement template;
[0066] Each of the aforementioned field names is added to its corresponding position in the preset statement template, wherein the position corresponds to the mapping identifier;
[0067] Add the logical symbol corresponding to the logical relationship to the logical symbol position in the preset statement template;
[0068] Obtain the initial query statement.
[0069] Optionally, the corpus processing module is configured to perform preprocessing operations on the initial query corpus to obtain the data query corpus:
[0070] Using a preset regular expression matching algorithm, text characters representing dates and numbers in the initial query corpus are found, and the text characters are converted into numeric characters.
[0071] A data query statement generation device, the generation device comprising:
[0072] processor;
[0073] Memory used to store the processor's executable instructions;
[0074] The processor is configured to execute the instructions to implement the data query statement generation method as described above.
[0075] A computer-readable storage medium, when the instructions in the computer-readable storage medium are executed by a processor of a data query statement generating device, enables the generating device to perform a data query statement generating method as described above.
[0076] This invention provides a method, system, device, and storage medium for generating data query statements. Preprocessing of the initial query statement avoids the risk of inaccurate recognition due to colloquial strings. By setting a first sub-model in a preset statement generation model, redundant data and query content in the data query corpus are distinguished, and the target data table containing the query content is selected. Simultaneously, by setting a second and third sub-model, the data and logical relationships representing the query content in the data table are accurately extracted, avoiding the risk of reduced efficiency and accuracy in data query statement generation due to the presence of other redundant data. Finally, by adding to the constructed preset statement template and using a preset syntax correction algorithm to perform syntax correction on the obtained initial query statement, the accuracy and efficiency of the final data query statement are improved.
[0077] Of course, any product or method implementing the present invention does not necessarily need to achieve all of the advantages described above at the same time. Attached Figure Description
[0078] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0079] Figure 1 A flowchart illustrating a method for generating a data query statement according to an embodiment of the present invention;
[0080] Figure 2 A block diagram of a data query statement generation system provided as an optional embodiment of the present invention;
[0081] Figure 3 A block diagram of a data query statement generation device provided as another optional embodiment of the present invention. Detailed Implementation
[0082] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0083] This invention provides a method for generating data query statements, such as... Figure 1 As shown, the generation method includes:
[0084] S101. Obtain the initial query corpus and perform preprocessing operations on the initial query corpus to obtain the data query corpus, wherein the initial query corpus is natural language.
[0085] Optionally, in an optional embodiment of the present invention, the aforementioned initial query corpus may be a string of natural language representing the query content input by the user through a human-computer interaction interface, or it may be a string of natural language converted by a speech recognition algorithm through a sound acquisition device.
[0086] Optionally, in another alternative embodiment of the present invention, the preprocessing operation described above may be a type conversion operation on the characters in the initial query corpus. Since the character content of the initial query corpus tends to be colloquial in practical applications, for example, using "first half of the year" to represent January to June, this is not conducive to subsequent model processing. Therefore, the present invention processes the initial query corpus through the above preprocessing operation, avoiding the risk that colloquial strings cannot be accurately identified, improving the processing accuracy of subsequent models, and thus improving the generation accuracy of data query statements.
[0087] S102. Input the data query corpus into the first sub-model so that the first sub-model can determine the target data table corresponding to the data query corpus from multiple data tables in the database. The first sub-model is a sub-model in the preset statement generation model.
[0088] Optionally, in one alternative embodiment of the present invention, the aforementioned preset statement generation model can be constructed based on a deep bidirectional representation from transformers (BERT) model. Because existing unidirectional language models can only recognize unidirectional contextual feature information, they limit the ability to represent strings, resulting in low accuracy in generating existing data query statements. The present invention, by constructing the aforementioned statement generation model, can improve the utilization rate of contextual feature information in the data query corpus, thereby improving the accuracy of the final generated data query statement.
[0089] Optionally, in another optional embodiment of the present invention, the target data table may be a data table storing the query content in the aforementioned data query corpus. Since the aforementioned data query corpus is user-edited, its structure does not meet the syntax structure of Structured Query Language (SQL), and includes not only the query content but also some redundant data. Therefore, to improve the accuracy of the final generated data query statement, it is necessary to clarify the correspondence between the query content in the aforementioned data query corpus and the data tables stored in the database. Therefore, the present invention, by setting the first sub-model in the aforementioned preset statement generation model, distinguishes between redundant data and query content in the aforementioned data query corpus and filters out the target data table storing the aforementioned query content, thereby improving the accuracy and efficiency of the final generated data query statement.
[0090] S103. Input the data query corpus and the target data table into the second sub-model so that the second sub-model can determine multiple target fields in the target data table that satisfy the preset matching relationship with the data query corpus based on the data query corpus. The second sub-model is a sub-model in the preset sentence generation model.
[0091] It should be noted that in practical applications, the data table includes not only the query content data in the aforementioned data query corpus but also other data. Therefore, this invention, by setting up the aforementioned second sub-model, achieves the extraction of data representing the query content from the data table, thereby avoiding the risk of reduced efficiency and accuracy in generating data query statements due to the presence of other redundant data.
[0092] Optionally, in one alternative embodiment of the present invention, the aforementioned preset matching relationship can be at least one filtering condition used to determine the correspondence between different types of query content and different fields in the target data table. For example, if the query content of the aforementioned data query corpus is "the total box office of movie A", then there can be two preset matching relationships, namely a first preset matching relationship and a second preset matching relationship. The first preset matching relationship is: whether the current field is a movie name field, and whether the content of that field is A. The second preset matching relationship is: whether the content of the current field is daily box office.
[0093] S104. Input the data query corpus and each target field into the third sub-model so that the third sub-model can determine multiple logical relationships between each target field based on the data query corpus. The third sub-model is a sub-model in the preset statement generation model.
[0094] It should be noted that, in practical applications, the query content of the aforementioned data query corpus may be the result of operations or comparisons performed on multiple fields in the target data table. Therefore, by determining the logical relationships through the third sub-model, this invention can clearly identify the operators or comparison symbols contained in the data query corpus, thereby improving the accuracy of the final generated data query statement.
[0095] S105. Add the target data table number, each target field, and each logical relationship to their respective positions in the preset statement template to obtain the initial query statement. The initial query statement is a statement edited based on a programming language.
[0096] It should be noted that in practical applications, SQL-based data query statements have a fixed structure, requiring the inclusion of search fields (including aggregate functions), target data table search fields, search condition fields, and grouping fields. Therefore, this invention constructs a preset statement template including these four fields and automatically adds the target data table number, target field content, and logical relationships based on a preset mapping relationship. This improves the accuracy and efficiency of the generated data query statements. The preset mapping relationship refers to the mapping relationship between the aforementioned fields and the field filling positions in the preset statement template.
[0097] S106. Using a preset syntax correction algorithm, perform syntax correction on the initial query statement to generate a data query statement.
[0098] It should be noted that in practical use cases, the initial query statement above is arbitrarily readable. However, because the fields filled in the initial query statement are extracted from the data table, the initial query statement does not meet the structure of an SQL execution statement. Therefore, to improve the executability of the final generated data query statement, it is necessary to perform syntax correction using the aforementioned preset syntax correction algorithm so that users can directly query the database based on the data query statement. The aforementioned preset syntax correction algorithm can be an algorithm built based on the syntax structure of the SQL language. Its specific functions include, but are not limited to: adding connectors such as AND and OR between multiple query conditions; adding time conversion functions such as the TO-DATE function; performing boundary correction on time range type query conditions; and adding the LIKE function to fuzzy matching strings.
[0099] This invention preprocesses the initial query query through a preprocessing operation, avoiding the risk of inaccurate recognition due to colloquial strings. Furthermore, by setting a first sub-model in the preset statement generation model, it distinguishes redundant data from the query content in the data query corpus, filtering out the target data table containing the query content. Simultaneously, by setting a second and third sub-model, it accurately extracts the data and logical relationships representing the query content from the data table, avoiding the risk of reduced efficiency and accuracy in data query statement generation due to the presence of other redundant data. Finally, by adding to the constructed preset statement template and using a preset syntax correction algorithm to correct the syntax of the obtained initial query statement, the accuracy and efficiency of the final data query statement are improved.
[0100] Optionally, the training process of the preset sentence generation model includes:
[0101] Multiple initial training sample data are obtained, which consist of sample corpora and their corresponding sample data tables;
[0102] For each initial training sample data: perform word segmentation on the sample corpus and determine the correspondence between each segmentation result and each field in the sample data table; based on the sample corpus, the sample data table number, and the correspondence, generate training sample data corresponding to the initial training sample data.
[0103] Using the training sample data, the initial first sub-model, the initial second sub-model, and the initial third sub-model are trained respectively to obtain a preset sentence generation model composed of the first sub-model, the second sub-model, and the third sub-model. The input of the preset sentence generation model is the data query corpus, and the output is the target data table, each target field, and each logical relationship.
[0104] It should be noted that in actual application scenarios, the above sample data table can be obtained after structural adjustment and type classification adjustment. Since the table structure in the data table is usually edited in English. If the sample corpus is a natural language of Chinese or other types, it will cause the initial sentence generation model to be unable to recognize. And due to the繁多 of field types in the data table, it is not conducive to improving the model efficiency. Therefore, the above sample data table is a data table obtained after the table structure is manually marked and the field types are reclassified.
[0105] Among them, the implementation methods for the above reclassification of field types include:
[0106] Unify the field types for storing time or date in the data table into the date (Date) type; unify the field types for storing data types of integer and floating-point into the number (Number) type; unify the other field types in the data table except the above date (Date) type and number (Number) type into the string (Text) type.
[0107] It should be noted that in actual application scenarios, there are multiple implementation methods for generating training sample data corresponding to the initial training sample data based on the above sample corpus, sample data table numbers and their corresponding relationships. Here, an example is provided:
[0108] Set the sample corpus in the current application scenario to "What is the total box office of Movie A". The above sample data table includes five fields: movie name, daily box office, screening date, screening theater, and movie duration. Among them, the number of the movie name field is 1, the number of the daily box office field is 2, the number of the screening date field is 3, the number of the screening theater field is 4, and the number of the movie duration field is 5.
[0109] Obtain a training sample template. The training sample template consists of multiple fields, including: corpus (Question) field, table number (table_id) field, lookup (select) field, aggregation function (agg) field, lookup condition (conds) field, and grouping (group) field.
[0110] Then, after obtaining the above sample corpus, sample data table numbers and their corresponding relationships, add the sample corpus to the above corpus field and add the sample data table number to the above table number field.
[0111] According to the above corresponding relationships, "A" in the sample corpus corresponds to the movie name field, and "box office" corresponds to the daily box office field. Then add the number 1 of the movie name field and the number 2 of the daily box office field to the lookup field. Add the number 1 of the movie name field to the grouping field. It should be noted that the word "繁多" in the original text seems to be incorrect. I translated it as "繁多" as it is in the original, but it might be a misspelling. It could perhaps be "繁多" which means "numerous" or "many". Also, some of the content might need further refinement for better English expression, but this translation adheres to the given rules.
[0112] Based on the above correspondence, it can be seen that "total" in the sample corpus corresponds to the summation function, and the summation object is the daily box office field. For the movie name field, an OR operation is performed. Then, the no-operation identifier and the summation function identifier are added to the function field.
[0113] Based on the above sample corpus, the search condition is determined to be "movie name = A". Therefore, the programming language would assign "A" to "movie name". Thus, the movie name field's number 1, the assignment function identifier, and the character "A" are added sequentially to the search condition field.
[0114] It should be noted that in practical applications, if there are multiple search conditions, they can be concatenated using logical connectors. For example, search condition 1 AND search condition 2.
[0115] Based on the sample corpus above, it can be determined that the grouping content is set according to the movie title. Therefore, add the number 1 of the movie title field to the group field.
[0116] The training sample template, after the above addition steps, is compiled and determined as the training sample data.
[0117] It should be noted that in real-world applications, different companies have different business focuses, resulting in relatively fixed target business scenarios. Furthermore, the sample size varies due to company size limitations. Therefore, to improve training quality and universality, the aforementioned models can be trained using pre-training and fine-tuning methods. Specifically, the initial BERT model is trained using the aforementioned sample data. After pre-training, the initial first model, the initial second sub-model, and the initial third sub-model are trained using the same sample data through fine-tuning.
[0118] Those skilled in the art will understand that in practical application scenarios, the corpus will contain a large number of technical terms. Therefore, in order to improve the model's recognition accuracy of the corpus, a business dictionary can be constructed to organize technical terms in specific business scenarios, such as product types, corresponding codes, and business flow status flag codes, into the business dictionary for the model to call. This invention does not impose too many limitations or elaborate on the specific construction process of the above-mentioned business dictionary and the process of the model calling the business dictionary.
[0119] Optionally, the data query corpus is input into the first sub-model, so that the first sub-model determines the target data table corresponding to the data query corpus from multiple data tables in the database, including:
[0120] The data query corpus is concatenated with the names of each data table in the database to obtain multiple first input data that meet the input format of the first sub-model.
[0121] Each first input data is input into the first sub-model so that the first sub-model can calculate the matching degree between the data query corpus and the data table in each first input data, and determine the data table corresponding to the matching degree with the largest value as the target data table.
[0122] Optionally, in one alternative embodiment of the present invention, when concatenating the data query corpus with the names of each data table in the database, each data table can be any single data table in the database, or it can be multiple data tables of a certain type after filtering. For example, when Department A generates a data query statement, it only filters the data table in the database with the department identifier A. Alternatively, based on the business identifier of the business scenario corresponding to the data query statement, it searches for and filters the data table in the database that contains that business identifier.
[0123] Optionally, in another optional embodiment of the present invention, the above-mentioned calculation of the matching degree between the data query corpus and the data table can be achieved by calculating the matching degree between a string consisting of several consecutive characters in the data query corpus and a string consisting of several consecutive characters in the data table name. Therefore, before calculating the matching degree, the first sub-model needs to perform word segmentation on the data query corpus and the data table name.
[0124] Those skilled in the art will understand that, in practical applications, the above-mentioned word segmentation can be achieved using the BasicTokenizer built into the BERT model. This invention does not elaborate further or limit the specific implementation methods for word segmentation using the aforementioned tokenizer.
[0125] Optionally, the data query corpus and the target data table are input into the second sub-model, so that the second sub-model determines multiple target fields in the target data table that satisfy a preset matching relationship with the data query corpus, including:
[0126] Extract the field names from the target data table;
[0127] The data query corpus is concatenated with the names of each field to obtain the second input data that meets the input format of the second sub-model;
[0128] The second input data is input into the second sub-model, so that the second sub-model determines whether each field name in the second input data and the data query corpus satisfy at least one matching relationship in the preset matching relationship group. If so, the field corresponding to the field name that satisfies at least one matching relationship is determined as the target field, and the matching relationship is added as a label to the target field.
[0129] Optionally, in an optional embodiment of the present invention, the specific implementation method described above for enabling the second sub-model to determine whether each field name in each second input data satisfies at least one matching relationship in a preset matching relationship group can be:
[0130] The second sub-model is configured to use sequence labeling to compare and predict field names against the data query corpus, and determine whether the field name satisfies at least one matching relationship in a preset matching relationship group. This preset matching relationship group can include multiple matching relationships with different content. For example: based on the data query corpus, it determines whether the field name is selected; if so, the data type corresponding to that field is determined as the search field; it determines whether the field name is the content of a grouping field; if so, the data type corresponding to that field name is determined as the grouping field; it determines whether the field name is a field in the search conditions; if so, the data type corresponding to that field name is determined as the condition field.
[0131] Optionally, the data query corpus and each target field are input into the third sub-model, so that the third sub-model can determine multiple logical relationships between the target fields based on the data query corpus, including:
[0132] Extract at least one keyword character of type keyword from the data query corpus;
[0133] For each target field: Determine whether at least one matching relationship satisfied by the target field includes a matching relationship in which the data type of the target field is a condition field. If so, then the target field is determined as a condition field.
[0134] Obtain multiple logical symbol identifiers corresponding to the field types of each condition field;
[0135] Based on the content of each field, determine the correspondence between each field name and each preset field type, and construct multiple three-data groups based on the correspondence;
[0136] Each three-element data set is input into the third sub-model so that the third sub-model performs binary classification on each three-element data set. Based on the classification results, at least one three-element data set with a matching degree greater than a preset threshold is identified as the target three-element data set. The three-element data set includes a keyword character, a condition field and its corresponding logical symbol identifier.
[0137] The logical symbols in the target three-dimensional data set are defined as logical relations.
[0138] Optionally, in one alternative embodiment of the present invention, the aforementioned keyword characters can be strings used to construct search condition fields in the data query corpus. For example, the data query corpus could be: "All financing and loan details of Company A this year". Here, "Company A" is the keyword character whose data type is a keyword.
[0139] It should be noted that in practical applications, the content in the search criteria usually appears in the form of proper nouns such as company names, product types, and morphological codes. To improve the accuracy of determining the criteria fields, this invention can use Name Entity Recognition (NER) technology, combined with the aforementioned business dictionary and regular expression matching algorithm, to achieve this determination.
[0140] Optionally, in another optional embodiment of the present invention, the aforementioned preset field type can be constructed to avoid incorrect selection of logical symbols in the three-element data set. For example, suppose the current two field names are date and box office revenue, and the data types of the fields corresponding to these two field names are both integers. Obviously, there is no logical relationship between date and box office revenue. However, since the data types of the two field contents are both integers, there is a risk that the model may establish an incorrect logical relationship due to the fact that they have the same data type. Therefore, the present invention avoids the construction of incorrect logical relationships by constructing a preset field type and determining the correspondence between the field name and the preset field type based on the field content.
[0141] The aforementioned preset field types are the same as the field type reclassification result provided in an optional embodiment of the present invention. That is, the aforementioned preset field types are Date type, Number type, and Text type, respectively.
[0142] Optionally, the target data table number, each target field, and each logical relationship are added to their respective positions in the preset statement template to obtain the initial query statement, including:
[0143] Obtain the mapping identifier of each target field and the number of the target data table;
[0144] Add the target data table number to the data table lookup position in the preset statement template;
[0145] Each field name is added to its corresponding position in the preset statement template, where the position corresponds to the mapping identifier;
[0146] Add the logical symbols corresponding to the logical relationships to the logical symbol positions in the preset statement template;
[0147] Obtain the initial query statement.
[0148] Optionally, preprocessing operations are performed on the initial query corpus to obtain the data query corpus, including:
[0149] Using a pre-defined regular expression matching algorithm, we find text characters representing dates and numbers in the initial query corpus and convert them into numeric characters.
[0150] Corresponding to the above method embodiments, the present invention also provides a data query statement generation system, such as... Figure 2 As shown, the generation system includes:
[0151] The corpus processing module 201 is used to obtain the initial query corpus and perform preprocessing operations on the initial query corpus to obtain the data query corpus, wherein the initial query corpus is natural language;
[0152] The first data determination module 202 is used to input the data query corpus into the first sub-model so that the first sub-model can determine the target data table corresponding to the data query corpus from multiple data tables in the database. The first sub-model is a sub-model in the preset sentence generation model.
[0153] The second data determination module 203 is used to input the data query corpus and the target data table into the second sub-model, so that the second sub-model can determine multiple target fields in the target data table that satisfy the preset matching relationship with the data query corpus based on the data query corpus. The second sub-model is a sub-model in the preset sentence generation model.
[0154] The third data determination module 204 is used to input the data query corpus and each target field into the third sub-model so that the third sub-model can determine multiple logical relationships between each target field based on the data query corpus. The third sub-model is a sub-model in the preset sentence generation model.
[0155] The data filling module 205 is used to add the target data table number, each target field and each logical relationship to their respective positions in the preset statement template to obtain the initial query statement, wherein the initial query statement is a statement edited based on a programming language;
[0156] The statement generation module 206 is used to perform syntax correction on the initial query statement using a preset syntax correction algorithm to generate a data query statement.
[0157] Optionally, the above are as follows: Figure 2 The generation system shown also includes a model training module, which is configured to: When training a preset statement generation model, the model training module is set to:
[0158] Multiple initial training sample data are obtained, which consist of sample corpora and their corresponding sample data tables;
[0159] For each initial training sample data: perform word segmentation on the sample corpus and determine the correspondence between each segmentation result and each field in the sample data table; based on the sample corpus, the sample data table number, and the correspondence, generate training sample data corresponding to the initial training sample data.
[0160] Using the training sample data, the initial first sub-model, the initial second sub-model, and the initial third sub-model are trained respectively to obtain a preset sentence generation model composed of the first sub-model, the second sub-model, and the third sub-model. The input of the preset sentence generation model is the data query corpus, and the output is the target data table, each target field, and each logical relationship.
[0161] Optionally, the aforementioned first data determination module 202 is configured as follows:
[0162] The data query corpus is concatenated with the names of each data table in the database to obtain multiple first input data that meet the input format of the first sub-model.
[0163] Each first input data is input into the first sub-model so that the first sub-model can calculate the matching degree between the data query corpus and the data table in each first input data, and determine the data table corresponding to the matching degree with the largest value as the target data table.
[0164] Optionally, the second data determination module 203 described above is configured as follows:
[0165] Extract the field names from the target data table;
[0166] The data query corpus is concatenated with the names of each field to obtain the second input data that meets the input format of the second sub-model;
[0167] The second input data is input into the second sub-model, so that the second sub-model determines whether each field name in the second input data and the data query corpus satisfy at least one matching relationship in the preset matching relationship group. If so, the field corresponding to the field name that satisfies at least one matching relationship is determined as the target field, and the matching relationship is added as a label to the target field.
[0168] Optionally, the aforementioned third data determination module 204 is configured as follows:
[0169] Extract at least one keyword character of type keyword from the data query corpus;
[0170] For each target field: Determine whether at least one matching relationship satisfied by the target field includes a matching relationship in which the data type of the target field is a condition field. If so, then the target field is determined as a condition field.
[0171] Obtain multiple logical symbol identifiers corresponding to the field types of each condition field;
[0172] Based on the content of each field, determine the correspondence between each field name and each preset field type, and construct multiple three-data groups based on the correspondence;
[0173] Each three-element data set is input into the third sub-model so that the third sub-model performs binary classification on each three-element data set. Based on the classification results, at least one three-element data set with a matching degree greater than a preset threshold is identified as the target three-element data set. The three-element data set includes a keyword character, a condition field and its corresponding logical symbol identifier.
[0174] The logical symbols in the target three-dimensional data set are defined as logical relations.
[0175] Optionally, the data filling module 205 described above is configured as follows:
[0176] Obtain the mapping identifier of each target field and the number of the target data table;
[0177] Add the target data table number to the data table lookup position in the preset statement template;
[0178] Each field name is added to its corresponding position in the preset statement template, where the position corresponds to the mapping identifier;
[0179] Add the logical symbols corresponding to the logical relationships to the logical symbol positions in the preset statement template;
[0180] Obtain the initial query statement.
[0181] Optionally, the corpus processing module 201 described above is configured to perform preprocessing operations on the initial query corpus to obtain the data query corpus as follows:
[0182] Using a pre-defined regular expression matching algorithm, we find text characters representing dates and numbers in the initial query corpus and convert them into numeric characters.
[0183] This invention also provides a device for generating data query statements, such as... Figure 3 As shown, the generating device includes:
[0184] Processor 301;
[0185] Memory 302 is used to store executable instructions of processor 301;
[0186] The processor 302 is configured to execute instructions to implement any of the above-described methods for generating data query statements.
[0187] This invention also provides a computer-readable storage medium that, when the instructions in the computer-readable storage medium are executed by the processor of a data query statement generating device, enables the generating device to perform any of the data query statement generation methods described above.
[0188] In a typical configuration, the device includes one or more processors (CPUs), memory, and a bus. The device may also include input / output interfaces, network interfaces, etc.
[0189] Memory may include non-persistent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, like read-only memory (ROM) or flash RAM, and memory includes at least one memory chip. Memory is an example of computer-readable media.
[0190] Computer-readable media includes both permanent and non-permanent, removable and non-removable media that can store information using any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic magnetic disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.
[0191] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0192] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes the element.
[0193] The various embodiments in this specification are described in a related manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the system embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0194] The above are merely embodiments of this application and are not intended to limit the scope of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of the claims of this application.
Claims
1. A method for generating data query statements, characterized in that, The method includes: An initial query corpus is obtained, and the initial query corpus is preprocessed to obtain a data query corpus, wherein the initial query corpus is natural language; The data query corpus is concatenated with the names of each data table in the database to obtain multiple first input data that satisfy the input format of the first sub-model; Each of the first input data is input into the first sub-model so that the first sub-model calculates the matching degree between the data query corpus and the data table in each of the first input data, and determines the data table corresponding to the matching degree with the largest value as the target data table. The first sub-model is a sub-model in the preset sentence generation model. Extract the field names from the target data table; The data query corpus is concatenated with each of the field names to obtain second input data that meets the input format of the second sub-model. The second sub-model is a sub-model in the preset statement generation model. The second input data is input into the second sub-model so that the second sub-model determines whether each field name in the second input data and the data query corpus satisfy at least one matching relationship in the preset matching relationship group. If so, the field corresponding to the field name that satisfies at least one matching relationship is determined as the target field, and the matching relationship is added as a label to the target field. Extract at least one keyword character of type keyword from the data query corpus; For each target field: determine whether the target field satisfies at least one of the matching relationships, and whether the matching relationship in which the data type of the target field is a condition field is included. If so, then the target field is determined as a condition field. Obtain multiple logical symbol identifiers corresponding to the field types of each of the aforementioned condition fields; Based on the content of each field, determine the correspondence between each field name and each preset field type, and construct multiple three-element data groups based on the correspondence. Each of the three data sets is input into the third sub-model so that the third sub-model performs binary classification on each of the three data sets and, based on the classification results, determines at least one of the three data sets with a matching degree greater than a preset threshold as the target three data set. The three data set includes a keyword character, a condition field and its corresponding logical symbol identifier. The third sub-model is a sub-model in the preset statement generation model. The logical symbols in the target three-dimensional data set are determined as logical relations; The target data table number, each target field, and each logical relationship are added to their respective positions in the preset statement template to obtain the initial query statement, wherein the initial query statement is a statement edited based on a programming language; The initial query statement is grammatically corrected using a preset syntax correction algorithm to generate a data query statement.
2. The method according to claim 1, characterized in that, The training process of the preset statement generation model includes: Multiple initial training sample data are obtained, wherein the initial training sample data consists of sample corpora and their corresponding sample data tables; For each of the initial training sample data: perform word segmentation on the sample corpus, and determine the correspondence between each word segmentation result and each field in the sample data table; based on the sample corpus, the number of the sample data table, and each of the correspondences, generate training sample data corresponding to the initial training sample data; Using the training sample data, the initial first sub-model, the initial second sub-model, and the initial third sub-model are trained respectively to obtain the preset statement generation model composed of the first sub-model, the second sub-model, and the third sub-model. The input of the preset statement generation model is the data query corpus, and the output is the target data table, each of the target fields, and each of the logical relationships.
3. The method according to claim 1, characterized in that, The step of adding the target data table number, each target field, and each logical relationship to their respective positions in the preset statement template to obtain the initial query statement includes: Obtain the mapping identifier of each target field and the number of the target data table; Add the target data table number to the data table lookup position in the preset statement template; Each of the aforementioned field names is added to its corresponding position in the preset statement template, wherein the position corresponds to the mapping identifier; Add the logical symbol corresponding to the logical relationship to the logical symbol position in the preset statement template; Obtain the initial query statement.
4. The method according to claim 1, characterized in that, The preprocessing operation on the initial query corpus to obtain the data query corpus includes: Using a preset regular expression matching algorithm, text characters representing dates and numbers in the initial query corpus are found, and the text characters are converted into numeric characters.
5. A data query statement generation system, characterized in that, The system includes: The corpus processing module is used to obtain an initial query corpus and perform preprocessing operations on the initial query corpus to obtain a data query corpus, wherein the initial query corpus is natural language; The first data determination module is used to input the data query corpus into the first sub-model so that the first sub-model can determine the target data table corresponding to the data query corpus from multiple data tables in the database. The first sub-model is a sub-model in a preset sentence generation model. The second data determination module is used to input the data query corpus and the target data table into the second sub-model, so that the second sub-model determines multiple target fields in the target data table that satisfy a preset matching relationship with the data query corpus based on the data query corpus. The second sub-model is a sub-model in the preset sentence generation model. The third data determination module is used to input the data query corpus and each of the target fields into the third sub-model, so that the third sub-model can determine multiple logical relationships between the target fields based on the data query corpus. The third sub-model is a sub-model in the preset sentence generation model. The data filling module is used to add the target data table number, each target field and each logical relationship to their respective positions in the preset statement template to obtain an initial query statement, wherein the initial query statement is a statement edited based on a programming language; The statement generation module is used to perform syntax correction on the initial query statement using a preset syntax correction algorithm to generate a data query statement; The first data determination module is configured to: concatenate the data query corpus with the names of each data table in the database to obtain multiple first input data that meet the input format of the first sub-model; input each first input data into the first sub-model so that the first sub-model calculates the matching degree between the data query corpus and the data table in each first input data, and determines the data table with the largest matching degree as the target data table; The second data determination module is configured to: extract the field names of each field in the target data table; concatenate the data query corpus with each field name to obtain second input data that meets the input format of the second sub-model; input the second input data into the second sub-model so that the second sub-model determines whether each field name in the second input data and the data query corpus meet at least one matching relationship in the preset matching relationship group. If so, the field corresponding to the field name that meets at least one matching relationship is determined as the target field, and the matching relationship is added as a label to the target field. The third data determination module is configured to: extract at least one keyword character whose data type is a keyword from the data query corpus; for each target field: determine whether the target field satisfies at least one matching relationship, including a matching relationship where the data type of the target field is a condition field; if so, determine the target field as a condition field; obtain multiple logical symbol identifiers corresponding to the field types of each condition field; determine the correspondence between each field name and each preset field type according to the field content of each field, and construct multiple three-element data groups based on the correspondence; input each three-element data group into the third sub-model so that the third sub-model performs binary classification on each three-element data group, and based on the classification result, determine at least one three-element data group with a matching degree greater than a preset threshold as the target three-element data group, wherein the three-element data group includes a keyword character, a condition field and its corresponding logical symbol identifier; and determine the logical symbol representation in the target three-element data group as a logical relationship.
6. A device for generating data query statements, characterized in that, The generating device includes: processor; Memory used to store the processor's executable instructions; The processor is configured to execute the instructions to implement the method for generating a data query statement as described in any one of claims 1 to 4.
7. A computer-readable storage medium, characterized in that, When the instructions in the computer-readable storage medium are executed by the processor of the data query statement generating device, the generating device is enabled to perform the data query statement generating method as described in any one of claims 1 to 4.
Citation Information
Patent Citations
Method, system and device for converting natural language query into SQL and storage medium
CN114547072A
SQL (Structured Query Language) query statement generation method and device, electronic equipment and readable storage medium
CN114625748A