Method, device, equipment and storage medium for automatic conversion of cross-dialect queries based on large language model

The automatic cross-dialect query conversion method, which combines a large language model with syntax parsing and feature embedding, solves the problem of incompatibility of query statements across database systems, achieves high-precision and reliable query conversion, and adapts to database version updates.

CN120045584BActive Publication Date: 2025-09-16TSINGHUA UNIVERSITY
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510125596.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-01-26
Publication Date
2025-09-16
Estimated Expiration
2045-01-26

AI Technical Summary

Technical Problem

Existing cross-dialect query conversion tools have problems such as inaccurate syntax parsing, insufficient representation of the functional characteristics of syntax elements, and a lack of effective verification and optimization mechanisms, which lead to incompatible query statements when migrating or integrating across database systems.

Method used

An automatic cross-dialect query conversion method based on a large language model is adopted. Through grammatical parsing, feature embedding and semantic matching, combined with syntax tree matching and a large language model, fragments in query statements that are incompatible with the target dialect are identified and converted. A cross-dialect grammatical embedding model is used for vector space matching, and a double verification mechanism is introduced to ensure the correctness of the conversion.

Benefits of technology

It achieves high-precision cross-dialect query conversion, improves matching accuracy and conversion reliability, reduces maintenance costs, can adapt to database version updates, and supports the conversion of complex grammatical structures.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120045584B_ABST
    Figure CN120045584B_ABST
Patent Text Reader

Abstract

Disclosed is a method, apparatus, device and storage medium for automatic conversion of cross-dialect queries based on a large language model. The method includes: generating a syntax tree and functional specifications of grammatical elements based on the grammatical files and database documents of the source dialect and the target dialect; dividing the original query into multiple functional segments based on the syntax tree; identifying the segments to be converted based on target dialect analysis; inputting the segments to be converted and the functional specifications into an embedding model to obtain an embedding vector, and matching it with the vector of the grammatical element of the target dialect; inputting the matched target dialect grammatical elements and their functional specifications into the large language model to generate a converted segment; replacing the segments to be converted in the original query to obtain a converted query; performing syntax and semantic verification on the converted query, repeating the conversion if the verification fails, and expanding the conversion range to regenerate the embedding vector if the maximum number of retries is reached. The present disclosure achieves high-quality cross-dialect query conversion and has good maintainability and scalability.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of information retrieval technology, and in particular to a method, apparatus, device and storage medium for automatic conversion of cross-dialect queries based on a large language model. Background Art

[0002] With the development of database technology, different database management systems have developed their own unique SQL dialects. These dialects differ in syntax and functionality. For example, the AGE function in PostgreSQL may not exist in other databases, leading to query incompatibility issues when migrating or integrating between different database systems. The demand for cross-dialect query conversion is growing, especially in distributed database systems and hybrid cloud environments.

[0003] Existing dialect conversion tools have serious flaws. For example, open-source tools like SQLGlot suffer from missing rules (unable to convert the AGE function in PostgreSQL), conversion errors (incorrectly converting the JSON_EXTRACT_PATH function in PostgreSQL to the non-existent JSON_EXTRACT function in Oracle), and lack of support for specific database versions (converting "LIMIT 1" to "FETCH FIRST 1 ROW ONLY," which is supported only in Oracle 19c but not Oracle 11g).

[0004] Traditional methods primarily include rule-based, template-based, and machine learning-based approaches. Rule-based approaches require the manual coding of numerous conversion rules, resulting in high maintenance costs and difficulty coping with complex conversion scenarios arising from dialect differences. Template-based approaches use predefined templates for matching and replacement, but template coverage is limited and they struggle to handle conversion tasks requiring contextual understanding. While machine learning-based approaches offer some degree of automatic learning capabilities, they lack a deep understanding of SQL syntax and semantics, making the accuracy and reliability of conversion results difficult to guarantee. These approaches are particularly error-prone when dealing with complex syntax structures such as nested queries and window functions.

[0005] Furthermore, existing methods commonly suffer from the following problems: First, the grammatical parsing of query statements is not precise enough, making it difficult to accurately identify the segments that need to be converted; second, the functional characteristics of grammatical elements are not fully characterized, which affects the accuracy of grammatical element matching during conversion; third, there is a lack of effective verification and optimization mechanisms, making it difficult to ensure the correctness of the conversion results; and fourth, there is insufficient utilization of unstructured information such as the functional specifications and usage documentation of the database dialect, making it impossible to fully utilize domain knowledge to guide the conversion process. Addressing these issues traditionally requires engineers to write a large amount of code, which is both expensive and inflexible, especially when updating database versions.

[0006] Therefore, a technical solution is needed that can accurately understand the grammatical structure and semantic features of query statements and effectively perform cross-dialect conversion. Summary of the Invention

[0007] The present disclosure provides an automatic cross-dialect query conversion solution based on a large language model. This solution combines grammatical parsing, feature embedding, semantic matching and a large language model to accurately identify and convert fragments in query statements that are incompatible with the target dialect, thereby achieving high-quality cross-dialect query conversion.

[0008] According to one embodiment of the present disclosure, a method for automatic conversion of cross-dialect queries based on a large language model is proposed, comprising:

[0009] Extract grammatical elements from the grammar files and database documents of the source and target dialects offline, generate syntax trees for the grammatical elements, and annotate the corresponding functional specifications;

[0010] Parsing the original query based on the source dialect to obtain the source dialect grammar tree of the original query;

[0011] matching a subtree of the source dialect syntax tree of the original query with a syntax tree of the source dialect syntax element to divide the original query into a plurality of source dialect feature fragments;

[0012] Parsing the original query based on the target dialect, identifying segments that are incompatible with the target dialect, and correspondingly determining to which source dialect functional segment the incompatible segment belongs, and using the source dialect functional segment as the segment to be converted;

[0013] Converting the to-be-converted segment and the corresponding functional specification into a first embedding vector using a cross-dialect grammar embedding model, wherein the cross-dialect grammar embedding model is used to convert the input into an embedding vector in a vector space measured by functional equivalence;

[0014] Performing similarity matching between the first embedding vector and a pre-stored embedding vector of a grammatical element of the target dialect to obtain a matched grammatical element of the target dialect;

[0015] Inputting the segment to be converted and the corresponding functional specification, the matched target dialect grammatical elements and the corresponding functional specification into the large language model to generate a converted segment in the target dialect form;

[0016] The to-be-converted segment in the original query is replaced with the converted segment to obtain a converted query.

[0017] In some implementations, grammatical elements are extracted offline from grammar files and database documents of the source and target dialects, syntax trees of the grammatical elements are generated, and corresponding functional specifications are annotated, including:

[0018] Extract grammatical elements from the grammar files of the source and target dialects offline, and use BNF definitions to generate corresponding syntax trees for each grammatical element, where leaf nodes represent terminal symbols and internal nodes represent non-terminal symbols;

[0019] The syntax tree of each syntax element is traversed depth-first, each node in the syntax tree is matched with the syntax element node in the database document, and the functional specification obtained from the database document through matching is annotated on the syntax tree.

[0020] In some embodiments, before inputting the segment to be converted into the cross-dialect grammar embedding model, the method further includes performing the following simplification processing on the segment to be converted:

[0021] Replace the database system-defined functions in the fragment to be converted with equivalent standard SQL functions;

[0022] Replace the components of the fragment to be converted that are not related to the conversion with non-terminal symbols;

[0023] Replace query clauses in the fragment to be converted that are not related to the conversion with non-terminal symbols.

[0024] In some embodiments, converting the to-be-converted segment and the corresponding functional specification into a first embedding vector using a cross-dialect grammar embedding model includes:

[0025] Generate a structural embedding vector of the segment to be converted through the grammatical structure encoder in the cross-dialect grammar embedding model;

[0026] Generate a multi-dimensional feature embedding vector of functional norms through the grammatical norm encoder in the cross-dialect grammar embedding model;

[0027] The structure embedding vector and the multi-dimensional feature embedding vector are integrated through a multi-head cross attention mechanism to generate the first embedding vector.

[0028] In some embodiments, generating a multi-dimensional feature embedding vector of a functional specification by a grammatical specification encoder in a cross-dialect grammar embedding model includes:

[0029] Use multiple encoders to capture features in functional specifications from different dimensions and mix expert knowledge to generate multiple feature embedding vectors of different dimensions;

[0030] Dynamically calculate the feature weights of each dimension based on the input functional specifications through a gating mechanism;

[0031] The feature embedding vectors are weightedly aggregated based on the feature weights to obtain the multidimensional feature embedding vector.

[0032] In some embodiments, the method further comprises training the cross-dialect grammar embedding model using contrastive learning, comprising:

[0033] The cross-dialect grammatical embedding model is trained using the following contrastive loss function, so that the scaled cosine similarity between the output embedding vector and the embedding vector of the positive sample increases, and the scaled cosine similarity between the output embedding vector and the embedding vector of the hard negative sample decreases:

[0034]

[0035] in, is the i-th sample s i The loss value, Represents the sample s i The set of paired positive samples, Represents the positive sample set The number of positive samples, Represents the sample s i A collection of paired hard negative samples, zip represents sample s i The embedding vector of the matched positive sample s p The scaled cosine similarity between the embedding vectors of in Represents sample s i The embedding vector and the matching hard negative sample s n The scaled cosine similarity between the embedding vectors of .

[0036] In some embodiments, the method further comprises:

[0037] Construct the sample s by i Positive sample set:

[0038] For sample s i The corresponding functional specifications are replaced with synonyms or word order is adjusted to obtain the standard variants as positive samples;

[0039] The other database systems have i Syntactic elements with the same keywords and functions are used as positive samples;

[0040] Use the database dialect conversion tool to convert the sample s i Equivalent grammatical elements converted to other database systems are used as positive samples;

[0041] Construct the sample s by i The hard negative sample set:

[0042] Cluster the grammatical elements that are positive samples of each other and select i Belong to different clusters and have the same i The similarity between the embedding vectors of is higher than that of sample si The syntactic element with the similarity to the embedding vectors of all positive samples is regarded as a hard negative sample.

[0043] In some embodiments, the method further comprises:

[0044] Verify the converted query;

[0045] If the verification fails, repeat the steps of generating the transformed segment using the large language model, updating the transformed query, and verifying it.

[0046] If the verification still fails after the number of repeated executions reaches the preset maximum number of retries, the range of the segment to be converted is expanded to obtain an extended segment to be converted, and the syntax tree and corresponding functional specifications of the extended segment to be converted are input into the cross-dialect grammar embedding model to obtain a new first embedding vector, and similarity matching and subsequent steps are performed again based on the new first embedding vector.

[0047] In some implementations, validating the converted query includes:

[0048] Use a parser that supports the target dialect to validate the syntax of the converted query;

[0049] Use a large language model to semantically verify the converted query.

[0050] In some embodiments, expanding the range of the segments to be converted to obtain the expanded segments to be converted includes:

[0051] Select a new segment from the adjacent source dialect functional segments of the source dialect functional segment to which the original segment to be converted belongs;

[0052] The original segment to be converted is merged with the selected newly added segment to obtain an extended segment to be converted.

[0053] According to one embodiment of the present disclosure, a device for automatic cross-dialect query conversion based on a large language model is proposed, comprising:

[0054] An offline information extraction unit, used to extract grammatical elements from the grammar files and database documents of the source and target dialects offline, generate a syntax tree of the grammatical elements, and annotate the corresponding functional specifications;

[0055] A first parsing unit is configured to parse an original query based on a source dialect to obtain a source dialect grammar tree of the original query;

[0056] a function segmentation unit, configured to match a subtree of a source dialect grammar tree of an original query with a grammar tree of a source dialect grammar element, so as to divide the original query into a plurality of source dialect function segments;

[0057] an incompatible identification unit, configured to parse the original query based on the target dialect, identify segments incompatible with the target dialect, and correspondingly determine to which source dialect functional segment the incompatible segment belongs, and use the source dialect functional segment as a segment to be converted;

[0058] a cross-dialect grammatical embedding model, configured to convert the to-be-converted segment and the corresponding functional specification into a first embedding vector, wherein the cross-dialect grammatical embedding model is configured to convert the input into an embedding vector in a vector space measured by functional equivalence;

[0059] a target dialect matching unit, configured to perform similarity matching between the first embedding vector and a pre-stored embedding vector of a target dialect grammatical element to obtain a matched target dialect grammatical element;

[0060] A large language model conversion unit, configured to input the segment to be converted and the corresponding functional specification, the matched target dialect grammatical elements and the corresponding functional specification into the large language model, and generate a converted segment in the target dialect;

[0061] The query replacement unit is configured to replace the to-be-converted segment in the original query with the converted segment to obtain a converted query.

[0062] In some embodiments, the device further comprises:

[0063] a verification unit, configured to verify the converted query, and if the verification fails, repeatedly executing the large language model conversion unit, the query replacement unit, and the verification unit;

[0064] A fragment expansion unit, if the verification unit still indicates verification failure after the number of repeated executions reaches a preset maximum number of retries, the fragment expansion unit is used to expand the range of the fragment to be converted to obtain an expanded fragment to be converted, the expanded fragment to be converted is input into the cross-dialect grammar embedding model, and the cross-dialect grammar embedding model, the target dialect matching unit, the large language model conversion unit and the query replacement unit are executed again.

[0065] According to one embodiment of the present disclosure, an electronic device is provided, comprising a memory and a processor, wherein the memory is used to store computer instructions executable on the processor, and the processor is used to implement any of the above methods when executing the computer instructions.

[0066] According to one embodiment of the present disclosure, a computer-readable storage medium is provided, on which a computer program is stored. When the program is executed by a processor, the method described in any one of the above items is implemented.

[0067] The technical solution proposed in this disclosure has at least the following beneficial effects:

[0068] (1) High parsing and recognition accuracy

[0069] The present disclosure divides the query into functional segments through syntax tree matching, identifies incompatible parts based on target dialect parsing, and accurately determines the scope to be converted;

[0070] (2) Strong matching accuracy

[0071] The present disclosure utilizes functional specifications to enhance grammatical element representation, performs matching in functional similarity vector space, and combines structural and semantic features to improve matching accuracy;

[0072] (3) High conversion reliability

[0073] Some embodiments of the present disclosure introduce dual verification of syntax and semantics, support multiple retries and range expansion optimization, and use a large language model to ensure conversion rationality;

[0074] (4) Strong adaptability

[0075] This paper makes full use of the domain knowledge in database documents and improves the generalization ability of the model through comparative learning, which can handle the differences between different database versions;

[0076] (5) Low maintenance cost

[0077] By applying the cross-dialect query automatic conversion solution proposed in this disclosure, there is no need to manually write and maintain conversion rules. New grammatical features can be automatically learned and it can easily adapt to database version updates.

[0078] Other features and advantages of the technical solution proposed in this disclosure are described in detail below. BRIEF DESCRIPTION OF THE DRAWINGS

[0079] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the specification and, together with the description, serve to explain the principles of the specification.

[0080] Figure 1 A flowchart of a method for automatic conversion of cross-dialect queries based on a large language model according to an embodiment of the present disclosure is shown.

[0081] Figure 2 A schematic diagram of the architecture of a cross-dialect grammar embedding model according to an exemplary embodiment of the present disclosure is shown.

[0082] Figure 3 The figure shows an overall architecture diagram of a database dialect conversion process based on a large language model according to an exemplary embodiment of the present disclosure.

[0083] Figure 4A workflow diagram of database dialect conversion based on a large language model according to an exemplary embodiment of the present disclosure is shown.

[0084] Figure 5 It is a schematic structural diagram of an electronic device according to at least one embodiment of the present disclosure. DETAILED DESCRIPTION

[0085] Exemplary embodiments will be described in detail herein, with examples illustrated in the accompanying drawings. In the following description, when referring to the drawings, identical numerals in different figures represent identical or similar elements, unless otherwise indicated. The embodiments described in the following exemplary embodiments are not intended to represent all possible embodiments consistent with the present disclosure. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the present disclosure, as detailed in the appended claims.

[0086] The disclosed embodiments may be applied to a computer system / server that is operable with numerous other general-purpose or special-purpose computing system environments or configurations. Examples of well-known computing systems, environments, and / or configurations suitable for use with the computer system / server include, but are not limited to, personal computer systems, server computer systems, thin clients, thick clients, handheld or laptop devices, microprocessor-based systems, set-top boxes, programmable consumer electronics, network personal computers, minicomputer systems, mainframe computer systems, and distributed cloud computing technology environments including any of the above, among others.

[0087] Computer systems / servers may be described in the general context of computer system-executable instructions, such as program modules, executed by a computer system. Generally, program modules may include routines, programs, objects, components, logic, data structures, and the like, that perform specific tasks or implement specific abstract data types. Computer systems / servers may be implemented in a distributed cloud computing environment where tasks are performed by remote processing devices linked through a communications network. In a distributed cloud computing environment, program modules may be located on local or remote computer system storage media, including storage devices.

[0088] This paper proposes a cross-dialect query automatic conversion solution based on a large language model. It innovatively combines syntax parsing, embedding matching, and a large language model to construct a unified cross-dialect conversion framework. It segmentes queries through syntax tree matching, maps syntax elements to a functional similarity vector space for matching using a cross-dialect syntax embedding model, and then uses a large language model to accurately convert based on functional specifications. Verification and optimization mechanisms are also introduced to ensure the correctness of the conversion results. This effectively addresses the problems of existing methods in terms of parsing precision, matching accuracy, and conversion reliability.

[0089] Figure 1A flowchart of a method for automatic conversion of cross-dialect queries based on a large language model according to an embodiment of the present disclosure is shown. As shown in the figure, the method includes steps 1 to 8.

[0090] Step 1: Extract grammatical elements from the grammar files and database documents of the source and target dialects offline, generate a syntax tree of the grammatical elements, and annotate the corresponding functional specifications.

[0091] A grammar file is a file that describes the grammatical rules of a database query language. Syntax elements generally refer to the basic components of a query statement, including terminal symbols (such as keywords and operators) and non-terminal symbols (such as expressions and clauses). A functional specification describes the functionality and specifications of a grammar element, including functional descriptions, usage restrictions, parameters and default values, and usage examples.

[0092] In some embodiments, grammatical elements can first be extracted offline from the grammar files of the source dialect and the target dialect, and a corresponding grammar tree can be generated for each grammatical element using the BNF (Backus-Naur Form) definition, where leaf nodes represent terminal symbols and internal nodes represent non-terminal symbols; then, the grammar tree of each grammatical element is traversed depth-first, and each node in the grammar tree is matched with the grammar element nodes in the database document, and the functional specifications obtained from the database document through matching are annotated onto the grammar tree.

[0093] According to this embodiment, the syntax tree structure is accurately constructed using BNF definitions, and the functional specification information matching each node is ensured to be obtained through depth-first traversal, thereby effectively associating the domain knowledge in the database document with the syntax structure.

[0094] This step provides basic data for subsequent query parsing and conversion, including complete syntax tree structure information and rich functional specification information.

[0095] Step 2: Parse the original query based on the source dialect to obtain the source dialect grammar tree of the original query.

[0096] Existing SQL grammar parsing technology can be used to generate the source dialect grammar tree of the original query.

[0097] For example, if the source dialect is PostgreSQL, you can use the open-source PostgreSQL parser pg_query to generate the corresponding syntax tree. Based on the native PostgreSQL parser, pg_query accurately recognizes PostgreSQL-specific syntax structures and functions (such as DATE_PART). The generated syntax tree includes a root node and its subtrees, fully preserving the query's grammatical structure.

[0098] For example, if the source dialect is MySQL, you can use the syntax parser provided by MySQL Workbench. For example, if the source dialect is Oracle, you can use a general SQL parser such as JSqlParser and configure the corresponding dialect mode.

[0099] These parsers can convert query statements into a standard syntax tree form, providing a basis for subsequent functional analysis and conversion.

[0100] Step 3: Match the subtree of the source dialect grammar tree of the original query with the grammar tree of the source dialect grammar element to divide the original query into multiple source dialect function fragments.

[0101] The syntax tree of the original query can be traversed from bottom to top. Each time a subtree is visited, the subtree can be matched with the syntax tree of the syntax element of the source dialect. The matching process for each subtree can include: obtaining the nodes of the subtree and their hierarchical relationship information, comparing this information with the nodes and their hierarchical relationships in the syntax tree of each syntax element, and if there is a complete match, marking the subtree as a functional fragment. For the successfully matched functional fragment, its position information in the original query syntax tree can be recorded, and the functional specification of the corresponding syntax element can be annotated on the functional fragment.

[0102] During the traversal process, if a subtree does not match any grammatical element, the subtree corresponding to its parent node can be traversed upwards. In this way, each functional fragment in the query statement can be fully identified and its corresponding functional specification can be annotated.

[0103] Conventional string matching methods (such as regular expressions) are difficult to accurately match grammatical elements in complex queries, especially when matching is required across multiple clauses containing the same keywords. This embodiment proposes a syntax tree-based matching. Through syntax tree-based matching, the boundaries of each functional fragment in the query can be identified and its corresponding functional specification can be obtained. This decomposition method based on grammatical structure makes it possible to subsequently process the functional fragments that need to be converted in a targeted manner, thereby improving the accuracy of the conversion. At the same time, the functional specification information marked on the functional fragment also provides a basis for subsequent processing.

[0104] Step 4: Parse the original query based on the target dialect, identify segments that are incompatible with the target dialect, and correspondingly determine to which source dialect functional segment the incompatible segment belongs, and use the source dialect functional segment as the segment to be converted.

[0105] A parser that supports the target dialect can be used to attempt to parse the original query. When the parser fails to recognize certain grammatical constructs or reports a syntax error, these parts are considered incompatible with the target dialect. For example, submitting a query containing the PostgreSQL AGE function to an Oracle parser will result in an error indicating that the AGE function is unrecognized. Syntax elements that trigger incompatibility warnings during syntax tree generation are considered incompatible.

[0106] For identified incompatible segments, the corresponding source dialect functional segment can be determined. Based on the location of the incompatible segment in the original query, the source dialect functional segment containing that location can be found and used as the segment to be converted. This ensures that the complete grammatical elements including the incompatible segment are obtained, facilitating subsequent high-quality conversion.

[0107] Step 5: Use a cross-dialect grammar embedding model to convert the to-be-converted segment and the corresponding functional specification into a first embedding vector, wherein the cross-dialect grammar embedding model is used to convert the input into an embedding vector in a vector space measured by functional equivalence.

[0108] In some implementations, before using the cross-dialect grammar embedding model to process the segment to be converted, various simplification processes may be performed on the segment to be converted.

[0109] Replace database-defined functions in the fragment to be converted with equivalent standard SQL functions. Database systems often implement a variety of custom functions to enhance usability, but these functions are often not supported by other database systems. Predefined mappings can be used to map custom functions to equivalent expressions using generic functions. For example, the predefined mapping "str ILIKE pattern::=LOWER(str)LIKE LOWER(pattern)" can normalize PostgreSQL's case-insensitive ILIKE function into a standard SQL expression that applies LIKE and lowercases the result.

[0110] Replace unrelated components of the fragment to be converted with non-terminal symbols. Functions or expressions nested within a fragment are typically not used during the conversion and can be abstracted as non-terminal symbols. For example, in the fragment "CAST(CONCAT('DR.′',”,name)AS TEXT)", the CONCAT function within it does not affect the CAST operation and can be abstracted as CAST(column_expr AS TEXT), where column_expr represents a non-terminal symbol.

[0111] Replace query clauses in the fragment to be converted that are not relevant to the conversion with non-terminal symbols. Some query clauses may be irrelevant to the primary functionality or behavior of a particular grammar and can be abstracted into non-terminal symbols. For example, in the MySQL query "SELECT * FROM child WHERE age>=10 LIMIT 2 OFFSET 10", the SELECT clause and WHERE condition are irrelevant to the conversion of the LIMIT clause. Therefore, these parts can be abstracted, retaining only the LIMIT clause, i.e., select_stmt LIMIT2OFFSET 10.

[0112] The above simplification process can highlight the grammatical features that require special attention, improving the efficiency and accuracy of subsequent matching. At the same time, the basic structure of the fragment to be converted is retained without affecting the overall functionality.

[0113] The simplified syntax tree and corresponding functional specifications of the to-be-converted segment can be input into a cross-dialect syntax embedding model, which converts them into an embedding vector in a space measured by functional equivalence. In some embodiments, this conversion may include:

[0114] Generate a structural embedding vector of the segment to be converted through the grammatical structure encoder in the cross-dialect grammar embedding model;

[0115] Generate a multi-dimensional feature embedding vector of functional norms through the grammatical norm encoder in the cross-dialect grammar embedding model;

[0116] The structure embedding vector and the multi-dimensional feature embedding vector are integrated through a multi-head cross attention mechanism to generate the first embedding vector.

[0117] Specifically, the grammatical structure encoder module is used to capture the structural features of the segment to be converted. It can use the code encoder model (such as StarEncoder) whose training data contains SQL statements to extract the structural representation of the query. The input segment to be converted can be first tokenized into a token sequence t = [t1, t2, ..., t n ], then input the encoder to get the structure embedding vector

[0118] The grammatical specification encoder can use multiple natural language based encoders to capture the multi-dimensional features of the specification, and then calculate the embedding vector of the specification in a weighted manner by mixing expert knowledge. For example, let d represent the functional specification associated with the segment to be converted S, and M encoders that capture different aspects of the specification can be used. Calculate and generate embedding vectors These embedding vectors can then be aggregated using a gating mechanism to obtain the final canonical embedding vector The gating weight is determined by the gate function Determine. Here g m (d) is the output of the gating network of the mth encoder, which allows the model to dynamically assign weights based on the input functional specifications. For example, when encoding the detailed specifications of the CONCAT function in MySQL, a higher weight can be given to the encoder that specifically understands and encodes the MySQL specification.

[0119] The structure-norm aggregator can use the multi-head cross attention mechanism to integrate the grammatical structure embedding vector With the normalized embedding vector Ensure that the cross-dialect grammar embedding model focuses on the most relevant normative part of the grammatical structure and generates the final first embedding vector E(q):

[0120]

[0121] The cross-dialect grammar embedding model according to this embodiment can fully consider information at both the grammatical structure and functional specification semantic levels, effectively improving the expressive power of the embedding vector for functional equivalence.

[0122] The training of the cross-dialect grammar embedding model will be introduced later.

[0123] Step 6: Perform similarity matching between the first embedding vector and the pre-stored embedding vector of the target dialect grammatical element to obtain a matched target dialect grammatical element.

[0124] Pre-stored embedding vectors for the target dialect's grammatical elements can be obtained using a cross-dialect grammatical embedding model. The syntax tree of the target dialect's grammatical elements and their corresponding functional specifications can be pre-entered into the cross-dialect grammatical embedding model, which converts them into embedding vectors and stores them for subsequent matching. These embedding vectors are in the same vector space as the first embedding vector generated for the segment to be converted, allowing for direct similarity calculations.

[0125] In the target dialect, a similarity calculation method can be used to retrieve syntactic elements that are functionally equivalent to the segment to be converted. For example, cosine similarity, vector dot product, or Euclidean distance can be used to measure the similarity between embedding vectors.

[0126] The target dialect grammatical element with the highest similarity can be selected as the matching result. If there are multiple candidates with close similarity, they can be screened based on grammatical rules.

[0127] Vector similarity matching effectively leverages the functional equivalence features learned by the cross-dialect grammatical embedding model, eliminating the need to manually write matching rules. Furthermore, because the model considers both grammatical structure and functional specifications when generating embedding vectors, it significantly improves matching accuracy.

[0128] Step 7: Input the segment to be converted and the corresponding functional specification, the matched target dialect grammatical elements and the corresponding functional specification into the large language model to generate a converted segment in the target dialect.

[0129] Using a prompt template, you can input the segment to be converted, its corresponding functional specifications, the matching target dialect grammatical elements and their corresponding functional specifications, the source dialect name, the target dialect name, and prompts into the large language model. The large language model then integrates this information to generate the converted segment. This approach fully leverages the language understanding and generation capabilities of the large language model, allowing for flexible handling of complex conversion scenarios based on context.

[0130] The large language model performs the conversion based on the information in the prompt template. The names of the source and target dialects provide the large language model with an understanding of the context of the conversion. The fragment to be converted and its functional specifications provide the model with an understanding of the content and functional requirements of the conversion. The grammatical elements of the target dialect and their functional specifications provide the model with the target form and constraints for the conversion. Prompts explicitly require the model to maintain functional equivalence and adhere to the grammatical specifications of the target dialect.

[0131] This embodiment proposes conversion based on a large language model, which offers significant advantages. The model, through pre-training, acquires rich SQL knowledge and fully understands the grammatical characteristics of different dialects. It can perform conversion based on functional specifications to ensure functional equivalence. It can generate results based on target grammatical elements to ensure grammatical correctness. It can also handle complex conversion scenarios, such as those requiring grammatical structure adjustments.

[0132] In particular, when there's no fully corresponding grammatical element in the target dialect, the model can understand the requirements based on functional specifications and achieve equivalent functionality by combining multiple grammatical elements. For situations requiring contextual adaptation, the model can make appropriate adjustments based on semantic understanding. When data type conversion is involved, the model can select the appropriate type based on its understanding of the characteristics of different dialects. These are all advantages of using a large language model for cross-dialect conversion.

[0133] Step 8: Replace the to-be-converted segment in the original query with the converted segment to obtain a converted query.

[0134] Based on the position of the segment to be converted in the original query, the scope that needs to be replaced can be located. The converted segment generated by the large language model is then replaced at that location, while keeping the rest of the query unchanged, to obtain a complete converted query.

[0135] In some embodiments, the method further comprises:

[0136] Query after conversion for verification;

[0137] If the verification fails, repeat the steps of generating the transformed segment using the large language model, updating the transformed query, and verifying it.

[0138] If the verification still fails after the number of repeated executions reaches the preset maximum number of retries, the range of the segment to be converted is expanded to obtain an extended segment to be converted, and the syntax tree and corresponding functional specifications of the extended segment to be converted are input into the cross-dialect grammar embedding model to obtain a new first embedding vector, and the similarity matching and subsequent steps are repeated based on the new first embedding vector.

[0139] In some implementations, validating the converted query includes: performing syntax validation on the converted query using a syntax parser that supports the target dialect, and performing semantic validation on the converted query using a large language model.

[0140] In some embodiments, expanding the range of the segments to be converted to obtain the expanded segments to be converted includes:

[0141] Select a new segment from the adjacent source dialect functional segments of the source dialect functional segment to which the original segment to be converted belongs;

[0142] The original segment to be converted is merged with the selected newly added segment to obtain an extended segment to be converted.

[0143] An exemplary description is given below.

[0144] Verification of the converted query can include both syntax and semantic validation. Syntax validation can be performed using a syntax parser that supports the target dialect to check whether the query is successfully generated and conforms to the target dialect's syntax specifications. For example, a syntax tree can be generated using the target dialect's BNF definition and checked for syntax warnings. If warnings are generated, the syntax validation is considered to have failed. If no warnings are generated and the syntax tree is correctly generated, the syntax validation is considered to have passed. You can choose a suitable existing SQL parser for syntax validation, such as ANTL, JSqlParser, or a native parser for a specific database (such as PostgreSQL's pg_query).

[0145] Semantic verification can use a large language model to analyze the functional equivalence of queries before and after conversion. Based on the two fragments before and after conversion and their corresponding functional specifications, the large language model can infer and verify the functional equivalence of the two queries. If the large language model infers functional equivalence, semantic verification passes; otherwise, semantic verification fails. Syntax verification can be performed first, and semantic verification can be performed on the converted queries that pass syntax verification. If both semantic and syntax verification pass, the conversion is considered successful. Semantic verification can be performed using a large language model that supports SQL understanding, such as the GPT series of models.

[0146] When verification fails, an iterative optimization strategy can be adopted. Return to step 7 above, use the large language model again to generate a new converted segment in the target dialect, and replace the segment to be converted in the original query with the new converted segment to obtain a new converted query, and then perform semantic verification and grammatical verification on the new converted query. If a converted query that can pass the verification is still not obtained after the preset maximum number of retries, consider determining a new segment to be converted through segment expansion, also called an extended segment to be converted, and return to step 5 to obtain a new first embedding vector based on the extended segment to be converted, and repeat the similarity matching, large language model conversion, grammatical verification and semantic verification processes.

[0147] The segment expansion proposed in this embodiment is a gradual strategy from local to global. Starting from the source dialect functional segment to which the original segment to be converted belongs, adjacent source dialect functional segments are selected and merged to expand the conversion scope. The selection of adjacent segments is based on the structural relationship of the syntax tree, with priority given to functional segments that have direct grammatical dependencies with the current segment.

[0148] For example, for the SQL query:

[0149]

[0150] If the AGE function fails to convert multiple times, you can expand the scope to the entire SELECT clause and process the AGE function and the adjacent EXTRACT function together to expand the conversion range.

[0151] The expanded segment serves as the new unit to be converted. As described above, return to step 5 to obtain a new first embedding vector based on the expanded segment to be converted. The similarity matching, large language model conversion, and grammatical and semantic verification processes are repeated. This process can be iterated multiple times until a suitable conversion result is found or the maximum expansion range is reached.

[0152] The verification and optimization mechanism proposed in the above implementation ensures the correctness of the conversion result through double verification, improves the conversion success rate through the retry mechanism, and adopts a progressive expansion strategy to handle complex dependencies while avoiding unnecessary large-scale conversions.

[0153] Figure 2 FIG. 1 shows a schematic diagram of a cross-dialect grammar embedding model according to an exemplary embodiment of the present disclosure. Figure 2 As shown in the figure, the cross-dialect grammar embedding model adopts a dual encoder plus aggregator design architecture. The grammatical structure encoder on the lower left processes the input grammatical tree, including grammatical tree tokens such as "CONCAT," through a code encoder and a feedforward neural network. The grammatical specification encoder on the lower right consists of M parallel natural language encoders, which process grammatical specifications such as "Concatenates," "Text," and "Arguments." The encoded results are processed by N feedforward neural networks and optimized through residual connections and layer normalization.

[0154] At the top level, the structure-norm aggregator receives the outputs of the two encoders, calculates attention scores through dot products, and uses the softmax function to obtain attention weights, ultimately generating a SQL embedding vector. The contrastive learning module to the right of the aggregator optimizes the representational power of the vector space by strengthening connections with positive samples (green) and increasing distance from negative samples (red).

[0155] The hierarchical architecture design proposed in this embodiment enables the embedding model to simultaneously capture grammatical structure features and functional semantic information, providing a good vector representation for subsequent similarity matching.

[0156] Contrastive learning can be used to train the cross-dialect grammar embedding model.

[0157] For the syntax element samples collected from the official documents (referred to as database documents) and syntax files of various database systems in Keywords or constructs that represent syntactic elements, Represents a feature specification, and its positive and hard negative samples can be generated for subsequent training.

[0158] In some embodiments, the sample s can be constructed by i Positive sample set:

[0159] For sample s i The corresponding functional specifications are replaced with synonyms or word order is adjusted to obtain the standard variant as the positive sample, which is the same as the sample s i The expressions are different but the semantics are the same;

[0160] The other database systems have i Syntactic elements with the same keywords and functions are used as positive samples. For example, the same built-in functions in MySQL and Oracle may have different specification styles to help the model identify equivalent functions in different database dialects.

[0161] Use the database dialect conversion tool to convert the sample s i The equivalent grammatical elements obtained by converting them into other database systems are used as positive samples. For example, tools such as SQLGlot are used to convert s i Query Convert to other database dialects and generate new queries Each transformed query is combined with its corresponding specification to form a new grammatical element tuple s k For s i Positive samples.

[0162] In some embodiments, the sample s can be constructed by i The hard negative sample set: cluster the grammatical elements that are positive samples of each other, and select the hard negative sample set that is the same as the sample s i Belong to different clusters and have the same i The similarity between the embedding vectors of is higher than that of sample s i The syntactic element with the similarity to the embedding vectors of all positive samples is regarded as a hard negative sample.

[0163] Specifically, after generating positive samples, the grammatical elements can be clustered according to their functional equivalence, and the grammatical elements that are positive samples of each other can be grouped into the same cluster, and the grammatical elements that are positive samples of each other can be grouped into the same cluster. i The grammatical elements belonging to different clusters are used as candidate negative samples; the grammatical elements s are calculated separately. i The similarity between the embedding vector of the candidate negative sample and the embedding vector of the syntax element, and the syntax element s i The maximum similarity between the embedding vector of the grammatical element s and the embedding vector of the positive sample is used as a metric. i If the similarity between the candidate negative sample and the candidate negative sample is greater than the metric, the candidate negative sample is used as the grammatical element s i Hard negative samples.

[0164] If negative samples are selected based only on belonging to different clusters, it may introduce the syntax element s i Syntax elements with low similarity, irrelevant or large differences in their embedding vectors are used as negative samples, causing significant noise. According to this embodiment, negative samples with high similarity in vector space but not equivalent in actual function can be selected, which helps to improve the model's discriminative ability.

[0165] In some embodiments, the cross-dialect grammatical embedding model is trained using the following contrastive loss function to increase the scaled cosine similarity with the embedding vector of the positive sample and decrease the scaled cosine similarity with the embedding vector of the hard negative sample:

[0166]

[0167] in, is the i-th sample s i The loss value, Represents the sample s i The set of paired positive samples, Represents the positive sample set The number of positive samples, Represents the sample s i A collection of paired hard negative samples, zip represents sample s i The embedding vector of the matched positive sample s p The scaled cosine similarity between the embedding vectors of in Represents sample s i The embedding vector and the matching hard negative sample s n The scaled cosine similarity between the embedding vectors of .

[0168] By minimizing the above loss function, the cross-dialect grammatical embedding model can be trained to increase the similarity between the sample and its positive sample, while reducing the similarity with the hard negative sample, thereby learning better functional equivalence representation.

[0169] Figure 3 This diagram illustrates the overall architecture of a database dialect conversion process based on a large language model, according to an exemplary embodiment of the present disclosure. The upper portion illustrates function-based query preprocessing, while the lower portion illustrates local-to-global conversion. This diagram illustrates the overall architecture of the database dialect conversion method based on a large language model.

[0170] During the query preprocessing phase, the system first parses the input SQL query (Q) to generate a syntax tree containing node structures such as select_smt, func_expr, and where_clause. The query then enters the query partitioning phase, where the query is functionally segmented based on the syntax tree annotated with functional specifications, resulting in multiple functional fragments based on the source dialect. During the query simplification phase, two types of rules are employed: Rule 1, "Custom Function Standardization," such as mapping the ILIKE operation (q3) to its standard form; and Rule 2, "Abstract Transformation of Irrelevant Components," such as converting the CONCAT structure to the non-terminal symbol name_expr. Finally, the query syntax tree is obtained, segmented and simplified according to its functionality.

[0171] In the local to global conversion phase, the fragments to be converted are first identified through the verification-driven selection process of the operations to be converted, such as the AGE function of q1 in the figure. When the local conversion fails, the system will expand the scope to the associated fragments, such as from q1 to q2. The currently determined fragment to be converted q i , enter the grammar enhancement conversion stage, perform grammar matching based on the cross-dialect grammar embedding model, and convert the conversion fragment q i The target grammar (including functional specifications) and the matched target grammar (including functional specifications) are converted using a large language model. The conversion results are verified with mixed queries to ensure correctness. The far right shows the architecture of the cross-dialect embedding model, which includes a grammatical structure encoder, a grammatical specification encoder, and a structure-specification aggregator. Comparative learning is used to optimize model performance. The entire process demonstrates a gradual conversion strategy from local to global, ensuring the accuracy and reliability of the conversion.

[0172] Figure 4 A workflow diagram for database dialect conversion based on a large language model, according to an exemplary embodiment of the present disclosure, is shown. As shown, the input SQL query statement is first parsed to generate a syntax tree. The query preprocessing phase then begins, encompassing query segmentation and query simplification. The query is then divided into multiple functional segments and standardized.

[0173] During the conversion process, a hybrid query is first used to verify the existence of segments requiring conversion. If verification fails, the segments to be converted are located. If verification fails and the number of attempts has been exceeded, an expansion operation is triggered, expanding the conversion scope and reprocessing.

[0174] For each segment to be converted, the system generates prompts based on its syntax tree and functional specifications, which are then fed into the large language model. The large language model then generates the converted result, which is then substituted into the original query to produce the complete converted query. Finally, a hybrid query is run to verify the correctness of the converted result. If the result passes verification, the final SQL query is output; otherwise, the conversion process repeats.

[0175] According to one embodiment of the present disclosure, there is also provided an automatic cross-dialect query conversion device based on a large language model, comprising:

[0176] An offline information extraction unit, used to extract grammatical elements from the grammar files and database documents of the source and target dialects offline, generate a syntax tree of the grammatical elements, and annotate the corresponding functional specifications;

[0177] A first parsing unit is configured to parse an original query based on a source dialect to obtain a source dialect grammar tree of the original query;

[0178] a function segmentation unit, configured to match a subtree of a source dialect grammar tree of an original query with a grammar tree of a source dialect grammar element, so as to divide the original query into a plurality of source dialect function segments;

[0179] an incompatible identification unit, configured to parse the original query based on the target dialect, identify segments incompatible with the target dialect, and correspondingly determine to which source dialect functional segment the incompatible segment belongs, and use the source dialect functional segment as a segment to be converted;

[0180] a cross-dialect grammatical embedding model, configured to convert the to-be-converted segment and the corresponding functional specification into a first embedding vector, wherein the cross-dialect grammatical embedding model is configured to convert the input into an embedding vector in a vector space measured by functional equivalence;

[0181] a target dialect matching unit, configured to perform similarity matching between the first embedding vector and a pre-stored embedding vector of a target dialect grammatical element to obtain a matched target dialect grammatical element;

[0182] A large language model conversion unit, configured to input the segment to be converted and the corresponding functional specification, the matched target dialect grammatical elements and the corresponding functional specification into the large language model, and generate a converted segment in the target dialect;

[0183] The query replacement unit is configured to replace the to-be-converted segment in the original query with the converted segment to obtain a converted query.

[0184] In some embodiments, the device further comprises:

[0185] A verification unit, used to verify the converted query;

[0186] If the verification fails, the large language model conversion unit, the query replacement unit, and the verification unit are repeatedly executed;

[0187] If the verification still fails after the number of repeated executions reaches the preset maximum number of retries, the range of the segment to be converted is expanded by the segment expansion unit to obtain an expanded segment to be converted, and the expanded segment to be converted is input into the cross-dialect grammar embedding model, and the cross-dialect grammar embedding model, the target dialect matching unit, the large language model conversion unit and the query replacement unit are executed again.

[0188] For other details and features of this embodiment, please refer to the relevant description above.

[0189] Figure 5An electronic device provided for at least one embodiment of the present disclosure includes a memory and a processor, wherein the memory is used to store computer instructions that can be executed on the processor, and the processor is used to implement the method for automatic conversion of cross-dialect queries based on a large language model as described in any embodiment or implementation of the present disclosure when executing the computer instructions.

[0190] At least one embodiment of the present disclosure further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method for automatic conversion of cross-dialect queries based on a large language model as described in any embodiment or implementation of the present disclosure.

[0191] Those skilled in the art will appreciate that one or more embodiments of this specification may be provided as a method, system, or computer program product. Thus, one or more embodiments of this specification may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware. Furthermore, one or more embodiments of this specification may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0192] The various embodiments in this specification are described in a progressive manner. Similar portions between the various embodiments can be referenced to each other. Each embodiment focuses on the differences between the other embodiments. In particular, the data processing device embodiment is generally similar to the method embodiment, so its description is relatively simple. For relevant portions, refer to the description of the method embodiment.

[0193] The foregoing description of this specification describes specific embodiments. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in an order different from that described in the embodiments and still achieve the desired results. Furthermore, the processes depicted in the accompanying drawings do not necessarily require the specific order shown or sequential order to achieve the desired results. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.

[0194] Embodiments of the subject matter and functional operations described in this specification may be implemented in the following: digital electronic circuits, tangibly embodied computer software or firmware, computer hardware including the structures disclosed in this specification and their structural equivalents, or a combination of one or more of them. Embodiments of the subject matter described in this specification may be implemented as one or more computer programs, i.e., one or more modules of computer program instructions encoded on a tangible, non-transitory program carrier to be executed by a data processing device or to control the operation of the data processing device. Alternatively or additionally, the program instructions may be encoded on an artificially generated propagation signal, such as a machine-generated electrical, optical, or electromagnetic signal, which is generated to encode information and transmit it to a suitable receiver device for execution by the data processing device. The computer storage medium may be a machine-readable storage device, a machine-readable storage substrate, a random or serial access memory device, or a combination of one or more of them.

[0195] The processes and logic flows described in this specification can be performed by one or more programmable computers executing one or more computer programs to perform the corresponding functions by operating on input data and generating output. The processes and logic flows can also be performed by, and apparatus can be implemented as, special purpose logic circuitry, such as an FPGA (field programmable gate array) or an ASIC (application-specific integrated circuit).

[0196] Computers suitable for executing computer programs include, for example, general-purpose and / or special-purpose microprocessors, or any other type of central processing unit. Typically, the central processing unit will receive instructions and data from a read-only memory and / or random access memory. The basic components of a computer include a central processing unit for implementing or executing instructions and one or more memory devices for storing instructions and data. Typically, a computer will also include one or more mass storage devices for storing data, such as magnetic disks, magneto-optical disks, or optical disks, or the computer will be operably coupled to such mass storage devices to receive data from them or to transmit data to them, or both. However, a computer does not necessarily have such devices. In addition, a computer can be embedded in another device, such as a mobile phone, a personal digital assistant (PDA), a mobile audio or video player, a game console, a global positioning system (GPS) receiver, or a portable storage device such as a universal serial bus (USB) flash drive, to name a few.

[0197] Computer-readable media suitable for storing computer program instructions and data include all forms of non-volatile memory, media, and storage devices, including, for example, semiconductor memory devices (e.g., EPROM, EEPROM, and flash memory devices), magnetic disks (e.g., internal hard disks or removable disks), magneto-optical disks, and CD ROM and DVD-ROM disks. The processor and memory can be supplemented by, or incorporated in, special purpose logic circuitry.

[0198] Although this specification includes many specific implementation details, these should not be interpreted as limiting the scope of any invention or the scope of protection claimed, but are mainly used to describe the features of specific embodiments of specific inventions. Certain features described in multiple embodiments within this specification may also be implemented in combination in a single embodiment. On the other hand, the various features described in a single embodiment may also be implemented separately in multiple embodiments or in any suitable sub-combination. In addition, although features may work in certain combinations as described above and even initially claimed as such, one or more features from the claimed combination may be removed from the combination in some cases, and the claimed combination may point to a sub-combination or a variation of the sub-combination.

[0199] Similarly, although operations are depicted in a particular order in the accompanying drawings, this should not be understood as requiring that these operations be performed in the particular order shown or performed sequentially, or that all illustrated operations be performed to achieve the desired results. In some cases, multitasking and parallel processing may be advantageous. In addition, the separation of various system modules and components in the above-described embodiments should not be understood as requiring such separation in all embodiments, and it should be understood that the described program components and systems can generally be integrated together in a single software product, or packaged into multiple software products.

[0200] Thus, specific embodiments of the subject matter have been described. Other embodiments are within the scope of the following claims. In some cases, the actions recited in the claims can be performed in a different order and still achieve the desired results. Furthermore, the processes depicted in the accompanying drawings do not necessarily require the particular order shown or sequential sequence to achieve the desired results. In some implementations, multitasking and parallel processing may be advantageous.

[0201] The above description is merely a preferred embodiment of one or more embodiments of this specification and is not intended to limit one or more embodiments of this specification. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of one or more embodiments of this specification shall be included in the scope of protection of one or more embodiments of this specification.

Claims

1. A method for automatic conversion of cross-dialect queries based on a large language model, characterized in that: include: Extract grammatical elements from the grammar files and database documents of the source and target dialects offline, generate syntax trees for the grammatical elements, and annotate the corresponding functional specifications; Parsing the original query based on the source dialect to obtain the source dialect grammar tree of the original query; matching a subtree of the source dialect syntax tree of the original query with a syntax tree of the source dialect syntax element to divide the original query into a plurality of source dialect feature fragments; Parsing the original query based on the target dialect, identifying segments that are incompatible with the target dialect, and correspondingly determining to which source dialect functional segment the incompatible segment belongs, and using the source dialect functional segment as the segment to be converted; Converting the to-be-converted segment and the corresponding functional specification into a first embedding vector using a cross-dialect grammar embedding model, wherein the cross-dialect grammar embedding model is used to convert the input into an embedding vector in a vector space measured by functional equivalence; Performing similarity matching between the first embedding vector and a pre-stored embedding vector of a grammatical element of the target dialect to obtain a matched grammatical element of the target dialect; Inputting the segment to be converted and the corresponding functional specification, the matched target dialect grammatical elements and the corresponding functional specification into the large language model to generate a converted segment in the target dialect form; The to-be-converted segment in the original query is replaced with the converted segment to obtain a converted query.

2. The method according to claim 1, characterized in that Extract grammatical elements from the grammar files and database documents of the source and target dialects offline, generate syntax trees for the grammatical elements, and annotate the corresponding functional specifications, including: Extract grammatical elements from the grammar files of the source and target dialects offline, and use BNF definitions to generate corresponding syntax trees for each grammatical element, where leaf nodes represent terminal symbols and internal nodes represent non-terminal symbols; The syntax tree of each syntax element is traversed depth-first, each node in the syntax tree is matched with the syntax element node in the database document, and the functional specification obtained from the database document through matching is annotated on the syntax tree.

3. The method according to claim 1, characterized in that Before inputting the segment to be converted into the cross-dialect grammar embedding model, the method further includes performing the following simplification processing on the segment to be converted: Replace the database system-defined functions in the fragment to be converted with equivalent standard SQL functions; Replace the components of the fragment to be converted that are not related to the conversion with non-terminal symbols; Replace query clauses in the fragment to be converted that are not related to the conversion with non-terminal symbols.

4. The method according to claim 1, wherein Converting the to-be-converted segment and the corresponding functional specification into a first embedding vector using a cross-dialect grammar embedding model includes: Generate a structural embedding vector of the segment to be converted through the grammatical structure encoder in the cross-dialect grammar embedding model; Generate a multi-dimensional feature embedding vector of functional norms through the grammatical norm encoder in the cross-dialect grammar embedding model; The structure embedding vector and the multi-dimensional feature embedding vector are integrated through a multi-head cross attention mechanism to generate the first embedding vector.

5. The method according to claim 4, characterized in that The grammatical specification encoder in the cross-dialect grammar embedding model generates a multi-dimensional feature embedding vector of functional specifications, including: Use multiple encoders to capture features in functional specifications from different dimensions and mix expert knowledge to generate multiple feature embedding vectors of different dimensions; Dynamically calculate the feature weights of each dimension based on the input functional specifications through a gating mechanism; The feature embedding vectors are weightedly aggregated based on the feature weights to obtain the multidimensional feature embedding vector.

6. The method according to claim 1, characterized in that The method further includes training the cross-dialect grammar embedding model using contrastive learning, including: The cross-dialect grammatical embedding model is trained using the following contrastive loss function, so that the scaled cosine similarity between the output embedding vector and the embedding vector of the positive sample increases, and the scaled cosine similarity between the output embedding vector and the embedding vector of the hard negative sample decreases: in, is the i-th sample s i The loss value, Represents the sample s i The set of paired positive samples, Represents the positive sample set The number of positive samples, Represents the sample s i The set of paired hard negative samples, z ip Represents sample s i The embedding vector of the matched positive sample s p The scaled cosine similarity between the embedding vectors of in Represents sample s i The embedding vector and the matching hard negative sample s n The scaled cosine similarity between the embedding vectors of .

7. The method according to claim 6, characterized in that The method further comprises: Construct the sample s by i Positive sample set: For sample s i The corresponding functional specifications are replaced with synonyms or word order is adjusted to obtain the standard variants as positive samples; The other database systems have i Syntactic elements with the same keywords and functions are used as positive samples; Use the database dialect conversion tool to convert the sample s i Equivalent grammatical elements converted to other database systems are used as positive samples; Construct the sample s by i The hard negative sample set: Cluster the grammatical elements that are positive samples of each other and select i Belong to different clusters and have the same i The similarity between the embedding vectors of is higher than that of sample s i The syntactic element with the similarity to the embedding vectors of all positive samples is regarded as a hard negative sample.

8. The method according to claim 1, characterized in that The method further comprises: Verify the converted query; If the verification fails, repeat the steps of generating the transformed segment using the large language model, updating the transformed query, and verifying it. If the verification still fails after the number of repeated executions reaches the preset maximum number of retries, the range of the segment to be converted is expanded to obtain an extended segment to be converted, and the syntax tree and corresponding functional specifications of the extended segment to be converted are input into the cross-dialect grammar embedding model to obtain a new first embedding vector, and similarity matching and subsequent steps are performed again based on the new first embedding vector.

9. The method according to claim 8, characterized in that Verify the converted query, including: Use a parser that supports the target dialect to validate the syntax of the converted query; Use a large language model to semantically verify the converted query.

10. The method according to claim 8, characterized in that The scope of the segments to be converted is expanded to include: Select a new segment from the adjacent source dialect functional segments of the source dialect functional segment to which the original segment to be converted belongs; The original segment to be converted is merged with the selected newly added segment to obtain an extended segment to be converted.

11. A cross-dialect query automatic conversion device based on a large language model, characterized in that: include: An offline information extraction unit, used to extract grammatical elements from the grammar files and database documents of the source and target dialects offline, generate a syntax tree of the grammatical elements, and annotate the corresponding functional specifications; A first parsing unit is configured to parse an original query based on a source dialect to obtain a source dialect grammar tree of the original query; a function segmentation unit, configured to match a subtree of a source dialect grammar tree of an original query with a grammar tree of a source dialect grammar element, so as to divide the original query into a plurality of source dialect function segments; an incompatible identification unit, configured to parse the original query based on the target dialect, identify segments incompatible with the target dialect, and correspondingly determine to which source dialect functional segment the incompatible segment belongs, and use the source dialect functional segment as a segment to be converted; a cross-dialect grammatical embedding model, configured to convert the to-be-converted segment and the corresponding functional specification into a first embedding vector, wherein the cross-dialect grammatical embedding model is configured to convert the input into an embedding vector in a vector space measured by functional equivalence; a target dialect matching unit, configured to perform similarity matching between the first embedding vector and a pre-stored embedding vector of a target dialect grammatical element to obtain a matched target dialect grammatical element; A large language model conversion unit, configured to input the segment to be converted and the corresponding functional specification, the matched target dialect grammatical elements and the corresponding functional specification into the large language model, and generate a converted segment in the target dialect; The query replacement unit is configured to replace the to-be-converted segment in the original query with the converted segment to obtain a converted query.

12. The device according to claim 11, characterized in that The device further comprises: a verification unit, configured to verify the converted query, and if the verification fails, repeatedly executing the large language model conversion unit, the query replacement unit, and the verification unit; A fragment expansion unit, if the verification unit still indicates verification failure after the number of repeated executions reaches a preset maximum number of retries, the fragment expansion unit is used to expand the range of the fragment to be converted to obtain an expanded fragment to be converted, the expanded fragment to be converted is input into the cross-dialect grammar embedding model, and the cross-dialect grammar embedding model, the target dialect matching unit, the large language model conversion unit and the query replacement unit are executed again.

13. An electronic device, characterized in that: The device includes a memory and a processor, wherein the memory is used to store computer instructions that can be executed on the processor, and the processor is used to implement the method according to any one of claims 1 to 10 when executing the computer instructions.

14. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 10 is implemented.

Citation Information

Patent Citations

  • Video generation method and device based on virtual object and electronic equipment

    CN117292022A

  • Method for converting natural language to SQL (Structured Query Language) based on large language model

    CN118643050A