SQL statement acquisition method, medium and device
Through large language models and vector conversion technology, the problem of non-professional users writing SQL statements when the database changes is solved, the accuracy and adaptability of SQL statements are achieved, and the statements can still be executed correctly when the fields change.
Patent Information
- Application Number
- CN202411969381.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-30
- Publication Date
- 2025-09-23
- Estimated Expiration
- 2044-12-30
AI Technical Summary
In the prior art, it is difficult for non-professional users to write SQL statements that meet their needs, especially when the database structure changes, it is difficult to generate accurate SQL statements.
By obtaining the initial statement and scenario input by the user, using the preset large language model and vector conversion model, we find similar field names and calculate the degree of similarity, replace the keywords in the initial SQL statement, and generate the target SQL statement.
Ensure that SQL statements can be adjusted in time when database fields change, avoid logical errors, and improve the accuracy and adaptability of generated SQL statements.
Smart Images

Figure CN119862199B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing, and in particular to a method, medium and device for obtaining SQL statements. Background Art
[0002] In the field of database applications, SQL (Structured Query Language) statements are the key language used to interact with databases and manipulate data. However, for many users, especially non-professional database operators, writing accurate and compliant SQL statements is somewhat difficult. On the one hand, SQL syntax rules are relatively complex, requiring familiarity with the specific syntax structures corresponding to various operations such as query, insert, and update, as well as detailed information such as the table structure and field names in the database. On the other hand, with the continuous changes in business needs and the continuous expansion of database content, fields in the table may be adjusted, added, or renamed, requiring existing SQL statements to be modified accordingly to adapt to the new database structure.
[0003] At the same time, a large number of initial SQL statements accumulated in actual applications are written based on previous business needs and database status. When the database changes or you want to query or operate data in a different way, it is difficult to accurately obtain new SQL statements that meet current needs.
[0004] Therefore, how to improve the accuracy of SQL statement acquisition becomes an urgent problem to be solved. Summary of the Invention
[0005] In order to solve the above technical problems, the present invention adopts a method for obtaining SQL statements, which includes the following steps:
[0006] S100 , obtaining an initial statement input by a target user, a target scenario corresponding to the initial statement, a target table and an initial SQL statement, several initial field names corresponding to the target table, and a target vector conversion model corresponding to the target scenario.
[0007] S200: Input each initial field name into a preset large language model, and obtain a number of similar field names and the number of similar field names corresponding to each initial field name.
[0008] S300: determining initial field names whose corresponding similar field names are within a preset number threshold as reference field names, and determining each similar field name corresponding to each reference field name as an intermediate field name.
[0009] S400: extract keywords from the initial SQL statement to obtain several reference keywords corresponding to the initial SQL statement.
[0010] S500 , converting each reference keyword and each intermediate field name according to a target vector conversion model to obtain a reference vector corresponding to each reference keyword and an intermediate vector corresponding to each intermediate field name.
[0011] S600, for any reference keyword, obtain the similarity between the current reference keyword and each intermediate field name based on the current reference keyword, the reference vector corresponding to the current reference keyword, each intermediate field name and the intermediate vector corresponding to each intermediate field name.
[0012] S700: Acquire the target field name corresponding to the current reference keyword based on the similarity between the current reference keyword and each intermediate field name.
[0013] S800: Use the target field name corresponding to each reference keyword to replace the corresponding reference keyword in the initial SQL statement, and obtain the target SQL statement corresponding to the initial statement.
[0014] The present invention also provides a non-transitory computer-readable storage medium, which stores at least one instruction or at least one program. The at least one instruction or at least one program is loaded and executed by a processor to implement the above-mentioned SQL statement acquisition method.
[0015] The present invention also provides an electronic device comprising a processor and the above-mentioned non-transitory computer-readable storage medium.
[0016] The present invention has at least the following beneficial effects: in response to field name changes, similar field names are found with the help of a large language model, and target field names are screened by converting reference keywords and intermediate field names into vectors and calculating the degree of similarity, so that SQL statements can be adjusted in a timely manner as database fields change, and ensure that the target field names replaced in the initial SQL statements conform to the original semantic logic, thereby avoiding logical errors in SQL statements or unexpected query results caused by arbitrary field name replacement, ensuring that corresponding data operations can still be accurately executed in the new field environment, and enhancing the adaptability and accuracy of the generated SQL statements to the actual structure of the database. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0018] Figure 1A flowchart of a method for obtaining SQL statements provided in Example 1 of the present invention;
[0019] Figure 2 This is a structural diagram of an SQL statement acquisition device provided in Example 2 of the present invention. DETAILED DESCRIPTION
[0020] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without making any creative efforts shall fall within the scope of protection of the present invention.
[0021] It should be noted that the terms "first", "second", etc. in the description and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It is understood that, where appropriate, the above-mentioned terms used to distinguish similar objects can be interchanged so that the present invention can also implement other embodiments other than the above-mentioned illustrated embodiments or described embodiments. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or server that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0022] Example 1
[0023] This embodiment provides a method for obtaining SQL statements, which includes the following steps: Figure 1 As shown:
[0024] S100 , obtaining an initial statement input by a target user, a target scenario corresponding to the initial statement, a target table and an initial SQL statement, several initial field names corresponding to the target table, and a target vector conversion model corresponding to the target scenario.
[0025] The initial statement entered by the user is a data operation requirement expressed in natural language. The target scenario categorizes the business or operation type within the initial statement. The target table is the specific data table in the database related to the requirement expressed by the user in the initial statement.
[0026] For example, target scenarios can be categorized by business area into sales, finance, and human resources scenarios. They can also be categorized by operation type into query, insert, and update scenarios. In an enterprise management database, target tables might include employee information tables, sales data tables, and department information tables. The description for an employee information table might state, "This table contains fields such as employee ID, name, department, and start date. The employee ID is the primary key, uniquely identifying each employee."
[0027] The initial SQL statement is an existing SQL statement that was previously obtained based on the business requirements corresponding to the initial statement. It is the basis for subsequent adjustments and optimizations to obtain more accurate SQL statements.
[0028] Each target table contains multiple fields. The initial field names are the specific names of the fields in the target table. They serve as specific identifiers for data stored in the database and as objects that need to be explicitly referenced when performing queries and operations in SQL statements. They reflect the data structure details of the target table and play a key role in accurately generating target SQL statements. For example, in an "Employee Information Table," initial field names might include "Employee Number," "Name," "Department," and "Job Entry Date." In a "Sales Data Table," field names might include "Sales Amount," "Sales Date," and "Salesperson."
[0029] The vector conversion model converts natural language text into a vector representation. Because different scenarios vary in language characteristics, data relationships, and other factors, a suitable vector conversion model is required to more accurately measure the relationship between natural language text and vector representations, providing a suitable vector data foundation for subsequent SQL statement generation.
[0030] In a specific embodiment, S100 includes the following steps:
[0031] S101, obtaining an initial vector conversion model, description text samples corresponding to a number of preset tables, preset scenes and description vector samples corresponding to each description text sample, and a preset sampling ratio corresponding to each preset scene.
[0032] S102 : Determine the preset sampling ratio as the target sampling ratio, determine each preset scene as the initial scene, and initialize the number of repetitions N=1.
[0033] S103, sampling from a plurality of description text samples according to a target sampling ratio corresponding to each initial scene, obtaining an Nth description text training sample set, an Nth description text verification sample set, and an Nth number of description text samples corresponding to each initial scene in the Nth description text training sample set.
[0034] S104, training the initial vector conversion model based on the Nth description text training sample set, the Nth description text verification sample set and the description vector sample corresponding to each description text sample, and obtaining the Nth vector conversion model and the Nth accuracy of each initial scene in the Nth vector conversion model.
[0035] S105 , obtaining an Nth degree of adaptation between each initial scene and the Nth vector conversion model according to the Nth number and the Nth accuracy rate corresponding to each initial scene.
[0036] S106: For any initial scene, determine the adaptation result corresponding to the current initial scene according to the comparison result of the Nth adaptation degree corresponding to the current initial scene and the preset degree threshold, wherein the adaptation result includes adaptation, non-matching and pending.
[0037] S107: If the adaptation result corresponding to the current initial scene is adaptation, the current initial scene is determined as the first scene, and the Nth vector conversion model is determined as the target vector conversion model corresponding to the current first scene.
[0038] S108: If the adaptation result corresponding to the current initial scene is not compatible, the current initial scene is determined as the second scene.
[0039] S109: If the adaptation result corresponding to the current initial scene is to be determined, the current initial scene is determined as the third scene.
[0040] S110, traverse all initial scenes, obtain all first scenes and the target vector conversion model corresponding to each first scene, all second scenes, all third scenes and the number of scenes corresponding to the third scenes.
[0041] S111 , acquiring a target description text sample set corresponding to each second scene according to the preset scene and description vector sample corresponding to each description text sample.
[0042] S113 , training an initial vector conversion model according to the target description text sample set corresponding to each second scene and the description vector sample corresponding to each description text sample, to obtain a target vector conversion model corresponding to each second scene.
[0043] S114: If the number of scenes is greater than 0, update the preset sampling ratio corresponding to each third scene and determine the updated preset sampling ratio as the target sampling ratio, determine each third scene as the initial scene, update N=N+1, and repeat step S103 until the updated number of scenes is equal to 0, and obtain the target vector conversion model corresponding to each preset scene.
[0044] S115 , acquiring a target vector conversion model corresponding to the target scene according to the target vector conversion model corresponding to each preset scene.
[0045] The description text sample is used to describe in detail the structure, field meaning, data range, and other information of each preset table, which helps to understand the specific composition of the table so as to accurately build the association between the SQL statement and the table.
[0046] The initial vector conversion model is a general vector conversion model that is not optimized for specific scenarios. Subsequent training and adjustments will be conducted based on the initial vector conversion model and combined with specific data to enable it to adapt to the text-to-vector conversion requirements in different preset scenarios.
[0047] The description vector sample is the correct vector representation of the corresponding description text sample and is used to guide the model in learning how to accurately convert the description text into a vector. The description vector sample can be obtained by converting the description text sample based on known vector conversion techniques. Those skilled in the art will recognize that any vector conversion technique in the prior art falls within the scope of protection of the present invention and will not be described in detail here. For example, the vector conversion technique can be Word2Vec technology.
[0048] During the training process of the initial vector conversion model, the description text samples from the Nth description text training sample set are input into the initial vector conversion model to assist the initial vector conversion model in learning how to convert the description text samples into vectors. The description text samples from the Nth description text verification sample set are then used for verification, and the differences between the model output vectors and the corresponding description vector samples are compared. Evaluation metrics such as cosine similarity are used to measure the accuracy of the model's conversion in each initial scenario. Ultimately, the Nth vector conversion model and the Nth accuracy rate of each initial scenario in the Nth vector conversion model are obtained, which reflects the accuracy of the current model in converting text into vectors in the corresponding initial scenario.
[0049] For any initial scene, the corresponding Nth degree of adaptation is compared with a pre-set threshold. If the Nth degree of adaptation exceeds the threshold, it indicates that the Nth vector conversion model performs well in this initial scene and is ideally adapted. If it is below the threshold, the Nth vector conversion model is considered to be poorly adapted to this initial scene. If the degree of adaptation is near the threshold, or in an uncertain situation, such as when the degree of adaptation is near the threshold, it is considered to be in a pending state, requiring further updating and matching of the vector conversion model adapted to the initial scene.
[0050] For the case determined to be the first scene, the target vector conversion model corresponding to each first scene is determined to complete the work of determining the appropriate model for some scenes.
[0051] For the cases determined to be the second scene, a target description text sample set suitable for retraining is collected and sorted, and the initial vector conversion model corresponding to each second scene is trained separately to obtain the target vector conversion model corresponding to each second scene.
[0052] For situations judged to be the third scenario, the vector conversion model is continuously optimized by updating the sampling ratio, repeating training, and other operations until all preset scenarios have clear model adaptation judgment results, and finally the target vector conversion model corresponding to each preset scenario is obtained.
[0053] The above method of finding suitable vector conversion models for different preset scenarios through multiple iterative training and evaluation aims to improve the accuracy and effectiveness of subsequent SQL statement conversion, so that it can better adapt to diverse business scenarios and natural language input situations.
[0054] By obtaining basic data including the initial vector conversion model, descriptive text samples and their related preset scenarios and description vector samples, preset sampling ratios, etc., and through multiple rounds of iterative operations, each preset scenario and related sample data and vector conversion model are sampled, trained, evaluated and classified, and each initial scenario is divided into an adapted first scenario and its target vector conversion model is determined, an unfit second scenario is to be retrained, and a third scenario to be judged is to be further analyzed and optimized, until all preset scenarios obtain an adapted target vector conversion model. This lays a precise and adaptable vector conversion foundation for the subsequent natural language and SQL statement conversion based on the target vector conversion model, improves the depth and accuracy of the understanding of the initial statements corresponding to each scenario during the vector conversion process, thereby improving the accuracy of SQL statement generation and reducing the risk of data errors or business process interference caused by the execution of erroneous SQL statements.
[0055] In a specific embodiment, the preset sampling ratio corresponding to each preset scene includes a training sample sampling ratio and a verification sample sampling ratio. S103 includes the following steps:
[0056] S1031, for any initial scene, according to the training sample sampling ratio corresponding to the current initial scene, sample a number of description text samples from all description text samples corresponding to the current initial scene as training samples, and obtain the Nth number of description text samples corresponding to the current initial scene as training samples.
[0057] S1032 , according to the sampling ratio of verification samples corresponding to the current initial scene, sample a plurality of description text samples from all description text samples corresponding to the current initial scene as verification samples.
[0058] S1033: traverse all initial scenes to obtain a number of description text samples corresponding to each initial scene as training samples and a number of description text samples as verification samples.
[0059] S1034: Determine a set of several description text samples corresponding to all initial scenes as training samples as the Nth description text training sample set.
[0060] S1035 : Determine a set of several description text samples corresponding to all initial scenes as verification samples as the Nth description text verification sample set.
[0061] As mentioned above, from the descriptive text samples in each initial scenario, training samples and verification samples are reasonably divided according to the corresponding sampling ratios, and finally the overall training sample set and verification sample set are constructed. This provides the key data preparation and organizational foundation for the subsequent training, verification, and gradual optimization of the initial vector conversion model based on the training sample set and verification sample set, thereby creating suitable vector conversion models for different preset scenarios, and ensuring the scientificity and rationality of the entire model training process.
[0062] In one specific embodiment, S105 includes the following steps:
[0063] S1051 , normalize the Nth number corresponding to each initial scene to obtain the normalized Nth number corresponding to each initial scene.
[0064] S1052: Input the normalized Nth number and Nth accuracy corresponding to each initial scene into a preset adaptation degree calculation model to obtain the Nth adaptation degree of each initial scene and the Nth vector conversion model.
[0065] Among them, the number of descriptive text samples extracted as training samples from different initial scenes may vary greatly. This embodiment uses a normalization operation to uniformly convert the quantitative values of different magnitudes into a standard, comparable range, creating conditions for subsequent accurate measurement of the degree of adaptation. The value range of the Nth number after normalization is [0, 1]. Those skilled in the art know that any normalization method with a value range of [0, 1] in the prior art falls within the protection scope of the present invention and will not be repeated here. For example, the normalization method in this embodiment can be a Min-Max normalization method.
[0066] When the normalized Nth number corresponding to the initial scene is large, it means that there is relatively abundant training data for the vector conversion model to learn the text conversion pattern related to the initial scene. Correspondingly, the abundant data volume should help the vector conversion model better capture the rules and improve the accuracy. However, when the Nth accuracy corresponding to the initial scene is low, it means that the vector conversion model cannot effectively extract features and understand semantics for the specific data features of the initial scene, resulting in a large deviation between the generated vector and the actual expected vector. Therefore, when the normalized Nth number corresponding to the initial scene is large and the Nth accuracy is low, it means that the structural design of the vector conversion model cannot fully adapt to the complexity and diversity of the initial scene data, and cannot effectively utilize the descriptive text samples and corresponding description vector samples under the initial scene. Therefore, the Nth degree of adaptation is inversely proportional to the corresponding normalized Nth number, and the Nth degree of adaptation is directly proportional to the corresponding Nth accuracy.
[0067] Among them, the preset adaptation degree calculation model is a weighted summation model. The implementer can set the first weight corresponding to the normalized Nth number and the second weight corresponding to the Nth accuracy rate according to actual conditions. By performing weighted summation on the normalized Nth number and the Nth accuracy rate, the Nth adaptation degree of each initial scene and the Nth vector conversion model can be obtained.
[0068] As described above, normalization ensures that the number of training samples participating in the evaluation is fair and comparable. Based on the preset relationship between the degree of adaptation and the normalized Nth number and Nth accuracy, the degree of adaptation is calculated using the adaptation calculation model. This provides a scientific and quantitative basis for accurately determining the adaptation status of each initial scenario and the vector conversion model throughout the entire process. This helps improve the accuracy and reliability of building vector conversion models that adapt to different preset scenarios, thereby improving the accuracy of subsequent SQL statement conversion.
[0069] In a specific embodiment, the preset degree threshold includes a first degree threshold and a second degree threshold, and S106 includes the following steps:
[0070] S1061: If the Nth adaptation degree corresponding to the current initial scene is less than or equal to the first degree threshold, determine that the adaptation result corresponding to the current initial scene is unmatched.
[0071] S1062: If the Nth adaptation degree corresponding to the current initial scene is greater than or equal to the second degree threshold, it is determined that the adaptation result corresponding to the current initial scene is not adapted.
[0072] S1063: If the Nth adaptation degree corresponding to the current initial scene is less than the second degree threshold and greater than the first degree threshold, determine that the adaptation result corresponding to the current initial scene is to be determined.
[0073] Among them, if the Nth adaptation degree corresponding to the current initial scene is less than or equal to the pre-set first degree threshold, it means that the Nth vector conversion model in the initial scene has very poor sample utilization effect, scene semantic understanding accuracy, and ability to generate expected vectors. In this case, it can be clearly determined that the adaptation result corresponding to the initial scene is unsuitable, that is, the vector conversion model trained together under multiple initial scenes is not suitable for the current initial scene. Then, a target description text sample set suitable for retraining can be collected and sorted, and the initial vector conversion model corresponding to each second scene can be trained separately to obtain an adapted target vector conversion model.
[0074] If the Nth adaptation degree corresponding to the current initial scene is greater than or equal to the set second degree threshold, it means that the Nth vector conversion model has reached an ideal adaptation state in the current initial scene, can effectively convert the initial sentence in the current initial scene into a suitable vector, and performs well in terms of accuracy, etc., then the current Nth vector conversion model can be used as the target vector conversion model for the current initial scene.
[0075] If the Nth degree of adaptation corresponding to the current initial scene is less than the second degree threshold and greater than the first degree threshold, the adaptation result corresponding to the current initial scene is determined to be pending, and more rounds of analysis and processing are required to determine the adaptability of the current initial scene and the current vector conversion model.
[0076] The specific values of the first degree threshold and the second degree threshold can be set by the implementer according to actual conditions.
[0077] By setting the first and second degree thresholds, the three adaptation results are clearly and reasonably divided into unmatched, matched, and pending. This provides clear criteria and classification basis for evaluating and screening vector conversion models in different initial scenarios. This allows for the systematic classification of numerous initial scenarios and the adoption of corresponding optimization strategies for different adaptation results. This helps to more accurately identify the appropriate target vector conversion model for each preset scenario, thereby ensuring the accuracy and effectiveness of subsequent SQL statement conversion.
[0078] In a specific embodiment, S100 further includes the following steps:
[0079] S1, obtaining several preset tables, preset description texts and preset scenarios corresponding to each preset table.
[0080] S2, converting a target vector corresponding to a preset scene and a preset description text corresponding to each preset table into a model, to obtain a description vector corresponding to each preset table.
[0081] S3, converting the model according to the target vector corresponding to the target scene corresponding to the initial sentence, and obtaining the sentence vector corresponding to the initial sentence.
[0082] S4, obtaining a target matching degree between the initial sentence and each preset table according to the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence.
[0083] S5, based on the target matching degree between the initial statement and each preset table, a target table corresponding to the initial statement is screened out from all preset tables.
[0084] According to the target vector conversion model corresponding to the preset scene corresponding to each preset table, the preset description text corresponding to each preset table is vector-converted to obtain the description vector corresponding to each preset table.
[0085] According to the target vector conversion model corresponding to the preset scenario corresponding to the initial sentence, the initial sentence is vector converted to obtain the sentence vector corresponding to the initial sentence.
[0086] In a specific embodiment, S4 includes the following steps:
[0087] S41, calculating the similarity between the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence.
[0088] S42: Determine the similarity between the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence as the target matching degree between the initial sentence and each preset table.
[0089] Among them, the similarity between the description vector corresponding to each preset table and the statement vector corresponding to the initial statement can be used to measure the closeness of the association between the initial statement and each preset table, thereby obtaining the target matching degree between the initial statement and each preset table, which serves as the basis for accurately screening out the target table that best matches the initial statement from a large number of preset tables, thereby improving the accuracy and rationality of the generated SQL statement.
[0090] In a specific embodiment, S5 includes the following steps:
[0091] S51 : Compare the target matching degree between the initial statement and each preset table to obtain the maximum target matching degree.
[0092] S52: Determine the preset table corresponding to the maximum target matching degree as the target table corresponding to the initial sentence.
[0093] As mentioned above, by building corresponding target vector conversion models for different preset scenarios, whether converting the description text of the preset table into a description vector or converting the initial sentence entered by the user into a sentence vector, accurate mapping can be performed based on the semantic characteristics and data association logic in the specific scenario, so that the vector can more accurately carry the key information in the text, providing data support for table screening and SQL statement generation, reducing erroneous conversions caused by problems such as semantic understanding deviation, and improving the accuracy of SQL statement conversion results in each preset scenario.
[0094] In a specific embodiment, S100 further includes the following steps:
[0095] S10, obtain the historical input database and the preset keyword configuration table corresponding to the target scenario, wherein the historical input database includes several historical statements, the historical statement vector corresponding to each historical statement, and the historical SQL statement, and the preset keyword configuration table includes several custom keywords and the regular expression corresponding to each custom keyword.
[0096] S20: extract keywords from the initial sentence to obtain several target keywords corresponding to the initial sentence.
[0097] S30 , matching each target keyword with the preset keyword configuration table to obtain a matching result corresponding to each target keyword, wherein the matching result includes a set consisting of a corresponding custom keyword and a corresponding regular expression.
[0098] S40: According to the matching results of all target keywords, several target regular expressions corresponding to the initial sentence are obtained.
[0099] S50, converting the model according to the target vector corresponding to the target scene corresponding to the initial sentence to obtain the sentence vector corresponding to the initial sentence.
[0100] S60 , obtaining the similarity between the initial sentence and each historical sentence based on the sentence vector and the historical sentence vector corresponding to each historical sentence.
[0101] S70 , based on a comparison between the similarity between the initial sentence and each historical sentence and a preset similarity threshold, a plurality of reference sentences corresponding to the initial sentence are screened out from all historical sentences.
[0102] S80: Input the sentence vector, target table, several target regular expressions corresponding to the initial sentence, several reference sentences corresponding to the initial sentence, and historical SQL statements corresponding to each reference sentence into the preset large language model to obtain the initial SQL statement corresponding to the initial sentence.
[0103] The historical input database stores a large amount of past database interaction data, including several historical statements. Each historical statement corresponds to a historical statement vector and a corresponding historical SQL statement. A historical statement vector is a digital, computer-friendly representation of a historical statement after vector conversion. It contains information such as the semantics of the historical statement. A historical SQL statement is the SQL code that was successfully generated and executed based on the corresponding historical statement and conforms to database syntax specifications.
[0104] The preset keyword configuration table is a pre-set table containing several custom keywords and the regular expression corresponding to each custom keyword. Custom keywords are important words with specific meanings and database operation directions extracted from a business perspective, such as "joining time" and "leaving time" in human resources management scenarios. Regular expression is a tool for matching and standardizing text formats. The regular expression corresponding to each custom keyword specifies how to accurately identify the keyword and its related expression form in natural language. For example, the regular expression corresponding to "joining time" may be "joining\s+[\s+] time", which is used to match statement expressions such as "joining on January 1, 2024". In this way, key information can be accurately extracted and standardized from texts such as the initial statement, assisting in the determination of the generation logic of subsequent SQL statements.
[0105] Among them, those skilled in the art know that any keyword extraction method in the prior art falls within the protection scope of the present invention, and will not be described in detail here.
[0106] As mentioned above, by extracting keywords from the initial sentence and matching it with the preset keyword configuration table to obtain the target regular expression, it is possible to accurately capture the key semantic information in natural language, standardize the fuzzy and diverse natural language expressions according to established business rules, and avoid the large language model's incorrect interpretation of user intentions. At the same time, by screening out reference sentences with high similarity to the initial sentence from many historical sentences and with reference significance for the current SQL statement generation, it helps the preset large language model to generate more accurate initial SQL statements that are in line with user intentions based on previous similar situations, thereby improving the accuracy of SQL statement generation.
[0107] In a specific embodiment, S30 further includes the following steps:
[0108] S301, for any target keyword, perform word consistency matching on the current target keyword and each custom keyword in the preset keyword configuration table, and obtain word consistency matching results between the current target keyword and each custom keyword, wherein the word consistency matching results include word consistency and word inconsistency.
[0109] S302: For any custom keyword, if the word consistency matching result between the current target keyword and the current custom keyword is word consistency, a set consisting of the current custom keyword and the regular expression corresponding to the current custom keyword is determined as the matching result corresponding to the current target keyword.
[0110] S303: traverse all custom keywords. If the word consistency matching results between the current target keyword and all custom keywords are all word inconsistencies, determine an empty set as the matching result corresponding to the current target keyword.
[0111] In the above, by comparing the target keywords with the custom keywords in the preset keyword configuration table one by one, the matching between the target keywords and the custom keywords at the word level is determined, and on this basis, the matching results corresponding to each target keyword are obtained, which lays a solid foundation for the subsequent integration of the matching results of all target keywords, further guiding the SQL statement generation logic based on these results, and improving the accuracy and standardization of SQL statement generation.
[0112] S200: Input each initial field name into a preset large language model, and obtain a number of similar field names and the number of similar field names corresponding to each initial field name.
[0113] Among them, the preset large language model is also used to find other field names that are similar in semantics, functions, or usage scenarios for each initial field name based on its powerful language understanding and semantic association capabilities, and count the number of similar field names.
[0114] The above lays the foundation for subsequently screening out suitable options that can be used to replace relevant field names in the initial SQL statement, and further ensuring that the SQL statement can still accurately execute corresponding operations when adapting to changes in the database structure, etc., which helps to improve the rationality and accuracy of SQL statement updates and optimizations.
[0115] S300: determining initial field names whose corresponding similar field names are within a preset number threshold as reference field names, and determining each similar field name corresponding to each reference field name as an intermediate field name.
[0116] In a specific embodiment, the preset quantity threshold range includes a first quantity threshold and a second quantity threshold, and S300 further includes the following steps:
[0117] S310 , obtaining the number distribution of similar field names corresponding to all initial field names based on the number of similar field names corresponding to each initial field name.
[0118] S320: Determine the first quartile corresponding to the quantity distribution of similar field names as a first quantity threshold.
[0119] S330: Determine the third quartile corresponding to the quantity distribution of similar field names as a second quantity threshold.
[0120] S340 , for any initial field name, if the number of similar field names corresponding to the current initial field name is greater than or equal to a first quantity threshold and less than or equal to a second quantity threshold, the current initial field name is determined as a reference field name.
[0121] The quartiles are the values used to divide similar field names into four equal parts after sorting them in ascending order. The first quartile is the value in the top 25% of the sorted data, and the third quartile is the value in the top 75% of the sorted data. By leveraging the statistical characteristics of data distribution, the first and second thresholds can reasonably reflect the boundary conditions in the dataset.
[0122] If the number of similar field names is too small, such as less than the preset first quantity threshold, it means that the initial field name and other field names may only be accidentally associated with the initial field name in some very special situations, that is, the semantic correlation between the initial field name and other field names is not strong, and there is a lack of initial field names with sufficient alternative options. If the number of similar field names is too large, such as exceeding the preset second quantity threshold, it means that the association range of the initial field name may be too broad, which will make the subsequent determination of suitable replacement field names complicated and difficult, and increase the risk of selecting inappropriate replacement items. Therefore, this embodiment determines the initial field name whose number of similar field names is greater than or equal to the first quantity threshold and less than or equal to the second quantity threshold as the reference field name.
[0123] As mentioned above, by understanding the distribution of the number of similar field names, determining the upper and lower thresholds based on the quartiles, and filtering out reference field names according to the threshold range, the drawbacks of traditional manual subjective threshold setting or arbitrary field name screening are avoided, making the screening process more objective and scientific. It can accurately select a subset of field names with relatively appropriate relevance and applicability from a large number of initial field names and their similar field names, laying a solid foundation for subsequent accurate field name replacement in SQL statements and generating target SQL statements that meet the requirements, thereby improving the accuracy and reliability of the entire SQL statement acquisition method.
[0124] S400: extract keywords from the initial SQL statement to obtain several reference keywords corresponding to the initial SQL statement.
[0125] Among them, those skilled in the art know that any keyword extraction method in the prior art falls within the protection scope of the present invention, and will not be described in detail here.
[0126] In the above, by extracting reference keywords from the initial SQL statement, we can grasp the core semantics and key operation information of the initial SQL statement, which serves as an important basis for subsequent operations such as semantic matching and field name replacement. This helps to accurately adjust and optimize the initial SQL statement according to database changes and current needs, and convert it into a target SQL statement that meets the requirements.
[0127] S500 , converting each reference keyword and each intermediate field name according to a target vector conversion model to obtain a reference vector corresponding to each reference keyword and an intermediate vector corresponding to each intermediate field name.
[0128] S600, for any reference keyword, obtain the similarity between the current reference keyword and each intermediate field name based on the current reference keyword, the reference vector corresponding to the current reference keyword, each intermediate field name and the intermediate vector corresponding to each intermediate field name.
[0129] In a specific implementation, S600 includes the following steps:
[0130] S610: Calculate the character overlap between the current reference keyword and each intermediate field name.
[0131] S620: Calculate the vector similarity between the reference vector corresponding to the current reference keyword and the intermediate vector corresponding to each intermediate field name.
[0132] S630, based on the character overlap between the current reference keyword and each intermediate field name, the first weight corresponding to the character overlap, the vector similarity between the reference vector corresponding to the current reference keyword and the intermediate vector corresponding to each intermediate field name, and the second weight corresponding to the vector similarity, obtain the similarity between the current reference keyword and each intermediate field name.
[0133] Among them, the character overlap degree can reflect the similarity between two words in text form. By calculating the character overlap degree between the current reference keyword and each intermediate field name, the degree of association can be intuitively measured at the character level, providing a dimension based on literal information for comprehensive evaluation of the overall similarity degree, assisting in subsequent more comprehensive and accurate judgment of the matching relationship between the reference keyword and the intermediate field name.
[0134] Calculating the vector similarity between the reference vector and the intermediate vector, and measuring the degree of association between the reference keyword and the intermediate field name from the perspective of digital semantic representation, can more accurately determine whether they are consistent in terms of semantic connotation and usage scenarios, and provide a key dimension data based on semantic understanding for the final determination of the similarity. This data complements the character overlap and jointly builds a more comprehensive similarity assessment system.
[0135] A common way to calculate character overlap is to calculate the ratio of the number of identical characters in two words to the total number of characters.
[0136] The specific values of the first weight and the second weight can be set by the implementer according to actual conditions.
[0137] By obtaining the degree of overlap from the character level, deriving the similarity from the perspective of semantic vectors, and finally performing weighted synthesis, this method fully utilizes the literal information and deep semantic information of the words, avoids the limitations of a single measurement method, and makes the calculated similarity more accurately reflect the matching relationship between the two. This lays a solid and scientific foundation for subsequently selecting the most appropriate target field name based on the similarity, accurately replacing the reference keywords in the initial SQL statement, and generating a target SQL statement that meets the requirements, thereby improving the accuracy and rationality of the entire SQL statement acquisition method.
[0138] S700: Acquire the target field name corresponding to the current reference keyword based on the similarity between the current reference keyword and each intermediate field name.
[0139] Among them, the middle field name corresponding to the maximum similarity is determined as the target field name corresponding to the current reference keyword.
[0140] S800: Use the target field name corresponding to each reference keyword to replace the corresponding reference keyword in the initial SQL statement, and obtain the target SQL statement corresponding to the initial statement.
[0141] Among them, the corresponding reference keywords in the initial SQL statement are replaced by the target field name corresponding to each reference keyword to complete the adjustment and optimization of the initial SQL statement, and finally obtain the target SQL statement that meets the current needs, so that the target SQL statement can adapt to the changes in the database structure, and at the same time still accurately reflect the database operations that the user wants to perform in terms of semantics and logic, and achieve the goal of quickly generating new and valid SQL statements using existing SQL statement resources.
[0142] In the above, each initial field name is input into the preset large language model, and several similar field names and the number of similar field names corresponding to each initial field name are obtained. The initial field name whose corresponding number of similar field names is within the preset number threshold is determined as a reference field name, and each similar field name corresponding to each reference field name is determined as an intermediate field name. Keywords are extracted from the initial SQL statement to obtain several reference keywords corresponding to the initial SQL statement. Each reference keyword and each intermediate field name is converted according to the target vector conversion model to obtain a reference vector corresponding to each reference keyword and an intermediate vector corresponding to each intermediate field name. For any reference keyword, the similarity between the current reference keyword and each intermediate field name is obtained according to the current reference keyword, the reference vector corresponding to the current reference keyword, each intermediate field name and the intermediate vector corresponding to each intermediate field name. The similarity between the previous reference keyword and each intermediate field name is used to obtain the target field name corresponding to the current reference keyword. The target field name corresponding to each reference keyword is used to replace the corresponding reference keyword in the initial SQL statement, and the target SQL statement corresponding to the initial statement is obtained. In response to the field name change, similar field names are found with the help of a large language model, and the target field names are screened by converting the reference keyword and the intermediate field name into vectors and calculating the similarity. This allows the SQL statement to be adjusted in time with the changes in the database fields, and ensures that the target field name replaced in the initial SQL statement conforms to the original semantic logic, avoiding logical errors in the SQL statement or unexpected query results due to arbitrary replacement of field names, ensuring that the corresponding data operations can still be accurately executed in the new field environment, and enhancing the adaptability and accuracy of the generated SQL statement to the actual database structure.
[0143] Example 2
[0144] This embodiment 2 provides a device for obtaining SQL statements, the device comprising: Figure 2 As shown:
[0145] The data acquisition module 21 is used to acquire the initial statement input by the target user, the target scenario corresponding to the initial statement, the target table and the initial SQL statement, several initial field names corresponding to the target table and the target vector conversion model corresponding to the target scenario.
[0146] The similar field name acquisition module 22 is used to input each initial field name into a preset large language model, and obtain a number of similar field names and the number of similar field names corresponding to each initial field name.
[0147] The field name screening module 23 is configured to determine initial field names whose corresponding similar field names are within a preset number threshold as reference field names, and determine each similar field name corresponding to each reference field name as an intermediate field name.
[0148] The keyword extraction module 24 is used to extract keywords from the initial SQL statement and obtain a number of reference keywords corresponding to the initial SQL statement.
[0149] The vector conversion module 25 is used to convert each reference keyword and each intermediate field name according to the target vector conversion model to obtain a reference vector corresponding to each reference keyword and an intermediate vector corresponding to each intermediate field name.
[0150] The similarity acquisition module 26 is used to obtain the similarity between the current reference keyword and each intermediate field name based on the current reference keyword, the reference vector corresponding to the current reference keyword, each intermediate field name and the intermediate vector corresponding to each intermediate field name for any reference keyword.
[0151] The target field name acquisition module 27 is used to acquire the target field name corresponding to the current reference keyword according to the similarity between the current reference keyword and each intermediate field name.
[0152] The SQL statement optimization module 28 is configured to replace the corresponding reference keyword in the initial SQL statement with the target field name corresponding to each reference keyword, and obtain a target SQL statement corresponding to the initial statement.
[0153] In a specific embodiment, the data acquisition module 21 includes:
[0154] The first data acquisition submodule is used to obtain an initial vector conversion model, description text samples corresponding to a number of preset tables, preset scenes and description vector samples corresponding to each description text sample, and a preset sampling ratio corresponding to each preset scene.
[0155] The data initialization submodule is used to determine the preset sampling ratio as the target sampling ratio, determine each preset scene as the initial scene, and the initialization repetition number N=1.
[0156] The first training data acquisition submodule is used to sample from a number of description text samples according to the target sampling ratio corresponding to each initial scene, to obtain the Nth description text training sample set, the Nth description text verification sample set, and the Nth number of description text samples corresponding to each initial scene in the Nth description text training sample set.
[0157] The first model training submodule is used to train the initial vector conversion model based on the Nth description text training sample set, the Nth description text verification sample set and the description vector sample corresponding to each description text sample, and obtain the Nth vector conversion model and the Nth accuracy of each initial scene in the Nth vector conversion model.
[0158] The adaptation degree acquisition submodule is used to obtain the Nth adaptation degree of each initial scene and the Nth vector conversion model according to the Nth quantity and Nth accuracy corresponding to each initial scene.
[0159] The adaptation result acquisition submodule is used to determine the adaptation result corresponding to the current initial scene for any initial scene based on the comparison result of the Nth adaptation degree corresponding to the current initial scene and the preset degree threshold, wherein the adaptation result includes adaptation, non-matching and pending judgment.
[0160] The first scene determination submodule is used to determine the current initial scene as the first scene if the adaptation result corresponding to the current initial scene is adaptation, and determine the Nth vector conversion model as the target vector conversion model corresponding to the current first scene.
[0161] The second scene determination submodule is used to determine the current initial scene as the second scene if the adaptation result corresponding to the current initial scene is not compatible.
[0162] The third scene determination submodule is configured to determine the current initial scene as the third scene if the adaptation result corresponding to the current initial scene is to be determined.
[0163] The initial scene traversal submodule is used to traverse all initial scenes, obtain all first scenes and the target vector conversion model corresponding to each first scene, all second scenes, all third scenes and the number of scenes corresponding to the third scene.
[0164] The second training data acquisition submodule is used to acquire a target description text sample set corresponding to each second scene according to the preset scene and description vector sample corresponding to each description text sample.
[0165] The second model training submodule is used to train the initial vector conversion model according to the target description text sample set corresponding to each second scene and the description vector sample corresponding to each description text sample, and obtain the target vector conversion model corresponding to each second scene.
[0166] The data update submodule is used to update the preset sampling ratio corresponding to each third scene if the number of scenes is greater than 0 and determine the updated preset sampling ratio as the target sampling ratio, determine each third scene as the initial scene, update N=N+1, and repeatedly execute the first training data acquisition submodule until the updated number of scenes is equal to 0, and obtain the target vector conversion model corresponding to each preset scene.
[0167] In a specific embodiment, the preset sampling ratio corresponding to each preset scene includes a training sample sampling ratio and a verification sample sampling ratio, and the first training data acquisition submodule includes:
[0168] The training sample acquisition unit is used to sample a number of description text samples as training samples from all description text samples corresponding to the current initial scene according to the training sample sampling ratio corresponding to the current initial scene for any initial scene, and obtain the Nth number of description text samples corresponding to the current initial scene as training samples.
[0169] The verification sample acquisition unit is used to sample a number of description text samples as verification samples from all description text samples corresponding to the current initial scene according to the verification sample sampling ratio corresponding to the current initial scene.
[0170] The scene traversal unit is used to traverse all initial scenes and obtain a number of description text samples corresponding to each initial scene as training samples and a number of description text samples as verification samples.
[0171] The training sample set determining unit is configured to determine a set of several description text samples corresponding to all initial scenes as training samples as an Nth description text training sample set.
[0172] The verification sample set determining unit is configured to determine a set of several description text samples corresponding to all initial scenes as verification samples as the Nth description text verification sample set.
[0173] In a specific embodiment, the adaptation degree acquisition submodule includes:
[0174] The data normalization unit is used to normalize the Nth quantity corresponding to each initial scene, and obtain the normalized Nth quantity corresponding to each initial scene.
[0175] The adaptation degree acquisition unit is used to input the normalized Nth number and Nth accuracy corresponding to each initial scene into a preset adaptation degree calculation model to obtain the Nth adaptation degree of each initial scene and the Nth vector conversion model, wherein the Nth adaptation degree is inversely proportional to the corresponding normalized Nth number, and the Nth adaptation degree is directly proportional to the corresponding Nth accuracy.
[0176] In a specific embodiment, the preset degree threshold includes a first degree threshold and a second degree threshold, and the adaptation result acquisition submodule includes:
[0177] The first adaptation result obtaining unit is configured to determine that the adaptation result corresponding to the current initial scene is unmatched if the Nth adaptation degree corresponding to the current initial scene is less than or equal to a first degree threshold.
[0178] The second adaptation result obtaining unit is configured to determine that the adaptation result corresponding to the current initial scene is not adapted if the Nth adaptation degree corresponding to the current initial scene is greater than or equal to a second degree threshold.
[0179] The third adaptation result obtaining unit is configured to determine that the adaptation result corresponding to the current initial scene is to be determined if the Nth adaptation degree corresponding to the current initial scene is less than the second degree threshold and greater than the first degree threshold.
[0180] In a specific embodiment, the data acquisition module 21 further includes:
[0181] The first data acquisition submodule is used to acquire a plurality of preset tables, a preset description text corresponding to each preset table, and a preset scene.
[0182] The description vector acquisition submodule is used to convert the target vector corresponding to the preset scene corresponding to the preset description text corresponding to each preset table into a model, so as to obtain the description vector corresponding to each preset table.
[0183] The sentence vector acquisition submodule is used to convert the model according to the target vector corresponding to the target scene corresponding to the initial sentence, and obtain the sentence vector corresponding to the initial sentence.
[0184] The target matching degree acquisition submodule is used to acquire the target matching degree between the initial sentence and each preset table according to the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence.
[0185] The target table acquisition submodule is used to filter out the target table corresponding to the initial statement from all preset tables based on the target matching degree between the initial statement and each preset table.
[0186] In a specific embodiment, the data acquisition module 21 further includes:
[0187] The second data acquisition submodule is used to obtain the historical input database and the preset keyword configuration table corresponding to the target scenario, wherein the historical input database includes several historical statements, the historical statement vectors corresponding to each historical statement, and the historical SQL statements, and the preset keyword configuration table includes several custom keywords and the regular expression corresponding to each custom keyword.
[0188] The keyword extraction submodule is used to extract keywords from the initial sentence and obtain several target keywords corresponding to the initial sentence.
[0189] The matching result acquisition submodule is used to match each target keyword with the preset keyword configuration table to obtain the matching result corresponding to each target keyword, wherein the matching result includes a set consisting of the corresponding custom keyword and the corresponding regular expression.
[0190] The target regular expression acquisition submodule is used to obtain several target regular expressions corresponding to the initial sentence based on the matching results of all target keywords.
[0191] The sentence vector acquisition submodule is used to convert the model according to the target vector corresponding to the target scene corresponding to the initial sentence, and obtain the sentence vector corresponding to the initial sentence.
[0192] The similarity acquisition submodule is used to obtain the similarity between the initial sentence and each historical sentence based on the sentence vector and the historical sentence vector corresponding to each historical sentence.
[0193] The sentence screening submodule is used to screen out several reference sentences corresponding to the initial sentence from all historical sentences based on the comparison between the similarity between the initial sentence and each historical sentence and a preset similarity threshold.
[0194] The initial SQL statement acquisition submodule is used to input the statement vector, the target table, several target regular expressions corresponding to the initial statement, several reference statements corresponding to the initial statement, and the historical SQL statements corresponding to each reference statement into the preset large language model to obtain the initial SQL statement corresponding to the initial statement.
[0195] In a specific embodiment, the preset quantity threshold range includes a first quantity threshold and a second quantity threshold, and the field name screening module 23 further includes:
[0196] The quantity distribution acquisition submodule is used to obtain the quantity distribution of similar field names corresponding to all initial field names based on the quantity of similar field names corresponding to each initial field name.
[0197] The first quantity threshold acquisition submodule is used to determine the first quartile corresponding to the quantity distribution of similar field names as the first quantity threshold.
[0198] The second quantity threshold acquisition submodule is used to determine the third quartile corresponding to the quantity distribution of similar field names as the second quantity threshold.
[0199] The reference field name acquisition submodule is used to determine, for any initial field name, the current initial field name as the reference field name if the number of similar field names corresponding to the current initial field name is greater than or equal to the first quantity threshold and less than or equal to the second quantity threshold.
[0200] It should be noted that the information interaction, execution process and other contents between the above modules are based on the same concept as the embodiment of the method of the present invention. Their specific functions and technical effects can be found in the method embodiment part and will not be repeated here.
[0201] Example 3
[0202] A third embodiment of the present invention provides a non-transitory computer-readable storage medium, wherein the non-transitory computer-readable storage medium stores at least one instruction or at least one program segment, and the at least one instruction or at least one program segment is loaded and executed by a processor to implement the following steps:
[0203] S100 , obtaining an initial statement input by a target user, a target scenario corresponding to the initial statement, a target table and an initial SQL statement, several initial field names corresponding to the target table, and a target vector conversion model corresponding to the target scenario.
[0204] S200: Input each initial field name into a preset large language model, and obtain a number of similar field names and the number of similar field names corresponding to each initial field name.
[0205] S300: determining initial field names whose corresponding similar field names are within a preset number threshold as reference field names, and determining each similar field name corresponding to each reference field name as an intermediate field name.
[0206] S400: extract keywords from the initial SQL statement to obtain several reference keywords corresponding to the initial SQL statement.
[0207] S500 , converting each reference keyword and each intermediate field name according to a target vector conversion model to obtain a reference vector corresponding to each reference keyword and an intermediate vector corresponding to each intermediate field name.
[0208] S600, for any reference keyword, obtain the similarity between the current reference keyword and each intermediate field name based on the current reference keyword, the reference vector corresponding to the current reference keyword, each intermediate field name and the intermediate vector corresponding to each intermediate field name.
[0209] S700: Acquire the target field name corresponding to the current reference keyword based on the similarity between the current reference keyword and each intermediate field name.
[0210] S800: Use the target field name corresponding to each reference keyword to replace the corresponding reference keyword in the initial SQL statement, and obtain the target SQL statement corresponding to the initial statement.
[0211] Those skilled in the art will understand that all or part of the processes in the above-mentioned embodiment methods can be implemented by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application may include non-volatile and volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronization link DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0212] Those skilled in the art will clearly understand that for the sake of convenience and brevity in description, only the division of the above-mentioned functional units and modules is used as an example. In actual applications, the above-mentioned functions can be distributed and completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above.
[0213] Example 4
[0214] A fourth embodiment of the present invention provides an electronic device, which includes a processor and the non-transitory computer-readable storage medium according to the third embodiment of the present invention.
[0215] The above are merely preferred embodiments of the present invention and are not intended to limit the present invention in any form. Although the present invention has been disclosed as above in terms of preferred embodiments, they are not intended to limit the present invention. Any technician familiar with this profession can make some changes or modifications to equivalent embodiments using the technical contents disclosed above without departing from the scope of the technical solution of the present invention. However, any simple modifications, equivalent changes and modifications made to the above embodiments based on the technical essence of the present invention without departing from the content of the technical solution of the present invention are still within the scope of the technical solution of the present invention.
Claims
1. A method for obtaining SQL statements, characterized in that: The SQL statement acquisition method comprises the following steps: S100: Acquire an initial statement input by a target user, a target scenario corresponding to the initial statement, a target table and an initial SQL statement, several initial field names corresponding to the target table, and a target vector conversion model corresponding to the target scenario, wherein the initial SQL statement is an existing SQL statement acquired based on the business requirement corresponding to the initial statement; S200: Input each initial field name into a preset large language model to obtain a number of similar field names and the number of similar field names corresponding to each initial field name; S300, determining initial field names whose number of corresponding similar field names is within a preset number threshold as reference field names, and determining each similar field name corresponding to each reference field name as an intermediate field name; S400, performing keyword extraction on the initial SQL statement to obtain a plurality of reference keywords corresponding to the initial SQL statement; S500, converting each reference keyword and each intermediate field name according to the target vector conversion model to obtain a reference vector corresponding to each reference keyword and an intermediate vector corresponding to each intermediate field name; S600: For any reference keyword, obtain the similarity between the current reference keyword and each intermediate field name based on the current reference keyword, the reference vector corresponding to the current reference keyword, each intermediate field name, and the intermediate vector corresponding to each intermediate field name; S700, obtaining the target field name corresponding to the current reference keyword based on the similarity between the current reference keyword and each intermediate field name; S800: Use the target field name corresponding to each reference keyword to replace the corresponding reference keyword in the initial SQL statement, and obtain the target SQL statement corresponding to the initial statement.
2. The SQL statement acquisition method according to claim 1, characterized in that: S100 includes the following steps: S101, obtaining an initial vector conversion model, description text samples corresponding to a plurality of preset tables, preset scenes and description vector samples corresponding to each description text sample, and a preset sampling ratio corresponding to each preset scene; S102, determining a preset sampling ratio as a target sampling ratio, determining each preset scene as an initial scene, and initializing the number of repetitions N=1; S103, sampling from the plurality of description text samples according to a target sampling ratio corresponding to each initial scene, to obtain an Nth description text training sample set, an Nth description text verification sample set, and an Nth number of description text samples corresponding to each initial scene in the Nth description text training sample set; S104: training the initial vector conversion model based on the Nth description text training sample set, the Nth description text verification sample set, and the description vector sample corresponding to each description text sample, to obtain the Nth vector conversion model and the Nth accuracy of each initial scene in the Nth vector conversion model; S105, obtaining an Nth degree of adaptation between each initial scene and the Nth vector conversion model according to the Nth number and the Nth accuracy rate corresponding to each initial scene; S106, for any initial scene, determining an adaptation result corresponding to the current initial scene based on a comparison result of the Nth adaptation degree corresponding to the current initial scene with a preset degree threshold, wherein the adaptation result includes adapted, unfit, and pending; S107, if the adaptation result corresponding to the current initial scene is adaptation, determining the current initial scene as the first scene, and determining the Nth vector conversion model as the target vector conversion model corresponding to the current first scene; S108, if the adaptation result corresponding to the current initial scene is not compatible, determining the current initial scene as the second scene; S109, if the adaptation result corresponding to the current initial scene is to be determined, the current initial scene is determined as the third scene; S110, traversing all initial scenes, obtaining all first scenes and the target vector conversion model corresponding to each first scene, all second scenes, all third scenes, and the number of scenes corresponding to the third scenes; S111, acquiring a target description text sample set corresponding to each second scene according to the preset scene and description vector sample corresponding to each description text sample; S113, training the initial vector conversion model based on the target description text sample set corresponding to each second scene and the description vector sample corresponding to each description text sample, to obtain a target vector conversion model corresponding to each second scene; S114: If the number of scenes is greater than 0, then updating the preset sampling ratio corresponding to each third scene and determining the updated preset sampling ratio as the target sampling ratio, determining each third scene as the initial scene, updating N=N+1, and repeating step S103 until the updated number of scenes is equal to 0, thereby obtaining the target vector conversion model corresponding to each preset scene; S115 , acquiring a target vector conversion model corresponding to the target scene according to the target vector conversion model corresponding to each preset scene.
3. The SQL statement acquisition method according to claim 2, characterized in that: The preset sampling ratio corresponding to each preset scene includes a training sample sampling ratio and a verification sample sampling ratio. S103 includes the following steps: S1031: For any initial scene, according to the training sample sampling ratio corresponding to the current initial scene, a number of description text samples are sampled from all description text samples corresponding to the current initial scene to obtain as training samples, and the Nth number of description text samples corresponding to the current initial scene as training samples is obtained; S1032, according to the sampling ratio of verification samples corresponding to the current initial scene, sampling from all description text samples corresponding to the current initial scene to obtain a plurality of description text samples as verification samples; S1033, traversing all initial scenes, obtaining a number of description text samples corresponding to each initial scene as training samples and a number of description text samples as verification samples; S1034, determining a set of several description text samples corresponding to all initial scenes as training samples as the Nth description text training sample set; S1035 : Determine a set of several description text samples corresponding to all initial scenes as verification samples as the Nth description text verification sample set.
4. The SQL statement acquisition method according to claim 2, characterized in that: S105 includes the following steps: S1051, normalizing the Nth number corresponding to each initial scene to obtain the normalized Nth number corresponding to each initial scene; S1052: Input the normalized Nth number and Nth accuracy corresponding to each initial scene into a preset adaptation degree calculation model to obtain the Nth adaptation degree of each initial scene and the Nth vector conversion model.
5. The SQL statement acquisition method according to claim 2, characterized in that: The preset degree threshold includes a first degree threshold and a second degree threshold, and S106 includes the following steps: S1061: If the Nth adaptation degree corresponding to the current initial scene is less than or equal to the first degree threshold, determining that the adaptation result corresponding to the current initial scene is unsuitable; S1062: If the Nth adaptation degree corresponding to the current initial scene is greater than or equal to the second degree threshold, determining that the adaptation result corresponding to the current initial scene is not adapted; S1063: If the Nth adaptation degree corresponding to the current initial scene is less than the second degree threshold and greater than the first degree threshold, determine that the adaptation result corresponding to the current initial scene is to be determined.
6. The SQL statement acquisition method according to claim 2, characterized in that: S100 also includes the following steps: S1, obtaining several preset tables, the preset description text and the preset scene corresponding to each preset table; S2, converting the target vector corresponding to the preset scene corresponding to the preset description text corresponding to each preset table into a model, and obtaining the description vector corresponding to each preset table; S3, acquiring a sentence vector corresponding to the initial sentence according to a target vector conversion model corresponding to the target scene corresponding to the initial sentence; S4, obtaining a target matching degree between the initial sentence and each preset table based on the description vector corresponding to each preset table and the sentence vector corresponding to the initial sentence; S5 , based on the target matching degree between the initial statement and each preset table, a target table corresponding to the initial statement is screened out from all preset tables.
7. The SQL statement acquisition method according to claim 3, characterized in that: S100 also includes the following steps: S10, obtaining a historical input database and a preset keyword configuration table corresponding to the target scenario, wherein the historical input database includes a plurality of historical statements, a historical statement vector corresponding to each historical statement, and a historical SQL statement, and the preset keyword configuration table includes a plurality of custom keywords and a regular expression corresponding to each custom keyword; S20, performing keyword extraction on the initial sentence to obtain a plurality of target keywords corresponding to the initial sentence; S30, matching each target keyword with the preset keyword configuration table to obtain a matching result corresponding to each target keyword, wherein the matching result includes a set consisting of a corresponding custom keyword and a corresponding regular expression; S40, obtaining several target regular expressions corresponding to the initial sentence based on the matching results of all target keywords; S50, acquiring a sentence vector corresponding to the initial sentence according to a target vector conversion model corresponding to a target scene corresponding to the initial sentence; S60, obtaining a similarity between the initial sentence and each historical sentence based on the sentence vector and the historical sentence vector corresponding to each historical sentence; S70, based on the comparison between the similarity between the initial sentence and each historical sentence and a preset similarity threshold, screening out a plurality of reference sentences corresponding to the initial sentence from all historical sentences; S80, input the sentence vector, the target table, several target regular expressions corresponding to the initial sentence, several reference sentences corresponding to the initial sentence, and historical SQL statements corresponding to each reference sentence into the preset large language model to obtain the initial SQL statement corresponding to the initial sentence.
8. The SQL statement acquisition method according to claim 1, characterized in that: The preset quantity threshold range includes a first quantity threshold and a second quantity threshold, and S300 further includes the following steps: S310, obtaining the number distribution of similar field names corresponding to all initial field names based on the number of similar field names corresponding to each initial field name; S320, determining the first quartile corresponding to the quantity distribution of similar field names as a first quantity threshold; S330, determining the third quartile corresponding to the quantity distribution of similar field names as a second quantity threshold; S340: For any initial field name, if the number of similar field names corresponding to the current initial field name is greater than or equal to the first number threshold and less than or equal to the second number threshold, the current initial field name is determined as the reference field name.
9. A non-transitory computer-readable storage medium, wherein at least one instruction or at least one program is stored in the non-transitory computer-readable storage medium, characterized in that: The at least one instruction or the at least one program segment is loaded and executed by a processor to implement the SQL statement acquisition method according to any one of claims 1 to 8.
10. An electronic device, characterized in that: The device comprises a processor and the non-transitory computer-readable storage medium as claimed in claim 9.
Citation Information
Patent Citations
SQL (Structured Query Language) statement generation method and device based on large language model
CN119127922A
Method and system for building a report for execution against a data store
US20050039033A1