A multi-database syntax adaptive parsing method, device and medium

By using multi-layer feature analysis and abstract syntax tree optimization, the problems of syntax compatibility and code redundancy in multi-database syntax parsing are solved, enabling the reuse of cross-dialect execution plans and unified engine support, thereby improving execution efficiency and code reusability.

CN122903438BActive Publication Date: 2026-11-24HIGHGO SOFTWARE
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202611401813.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2026-09-10
Publication Date
2026-11-24
Estimated Expiration
2046-09-10

AI Technical Summary

Technical Problem

Existing database syntax parsing suffers from problems such as syntax compatibility difficulties, code redundancy, and difficulty in improving execution efficiency. Especially in multi-database environments, existing technologies cannot effectively support multiple database syntaxes on a single port, and optimization strategies cannot be shared.

Method used

By employing multi-layer feature extraction and analysis, and based on abstract syntax trees and dialect-aware optimization methods, candidate execution plans are generated by dynamically identifying the syntax type of SQL statements, and then optimized using a dialect-aware cost model, thereby achieving cross-dialect execution plan reuse and unified engine support.

Benefits of technology

It achieves more flexible and efficient multi-database syntax compatibility, reduces code redundancy, improves execution efficiency, and supports the mixing of three syntaxes under any port, resulting in a significant performance improvement.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122903438B_ABST
    Figure CN122903438B_ABST
Patent Text Reader

Abstract

This invention discloses an adaptive parsing method, device, and medium for multi-database syntax, belonging to the field of database technology. It addresses the technical problems of difficulty in achieving syntax compatibility, code redundancy, and reduced execution efficiency in existing multi-database syntax parsing. The method includes: extracting and analyzing multi-level features of SQL statements to determine dialect syntax type statements; performing database dialect optimization processing on dialect syntax type statements based on dialect optimization rules; estimating cost weights for candidate execution plans to obtain the final execution plan; if the semantic fingerprint of a newly input current dialect syntax type statement has the same semantic fingerprint in the query index, then directly reusing the cached final execution plan for the current dialect syntax type statement; executing both the final execution plan and the new final execution plan using the execution engine, and returning the execution results to the client.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of databases, and more particularly to an adaptive parsing method, device, and medium for multiple database syntaxes. Background Technology

[0002] Current database syntax parsing methods often deploy SQL transformation middleware at the application layer; however, this can easily increase network latency and prevent the utilization of the database's native optimizer. Alternatively, multiple complete parsers can be built into the database, with selection based on connection type; however, this can lead to high code duplication, maintenance difficulties, and the inability to share optimization strategies. Another approach is to bind PostgreSQL and Oracle parsers to separate ports, selecting the appropriate parser based on the client's connection port number; however, this statically binds port numbers to parsers, requiring new ports to be added for new syntax extensions, and only allowing independent optimization for the two parsers results in code redundancy.

[0003] Therefore, there is an urgent need for an adaptive parsing and unified optimization method for multiple database syntaxes, so as to enable a single port / any port to support multiple database syntaxes; to unify the parsing engine to support Oracle, MySQL and PostgreSQL syntaxes; and to enable cross-dialect execution plan cache reuse. Summary of the Invention

[0004] This application provides an adaptive parsing method, device, and medium for multi-database syntax to solve the following technical problem: In existing multi-database syntax parsing, syntax compatibility is difficult, which can easily lead to code redundancy and make it difficult to improve execution efficiency.

[0005] The embodiments of this application adopt the following technical solutions: On one hand, embodiments of this application provide an adaptive parsing method for multi-database syntax, including: extracting and analyzing multi-level features of an SQL statement received from any port, and determining a dialect grammar type statement based on the confidence score of each feature; performing database dialect optimization processing on the dialect grammar type statement based on dialect optimization rules through a preset abstract syntax tree, and generating a candidate execution plan; estimating the cost weight of the candidate execution plan through the dialect preference factor of each database dialect in a preset dialect-aware cost model, and obtaining a final execution plan; if the semantic fingerprint of a newly input current dialect grammar type statement has the same semantic fingerprint in the query index, then the cached final execution plan is directly reused for the current dialect grammar type statement; if the semantic fingerprint of a newly input current dialect grammar type statement does not have the same semantic fingerprint in the query index, then a new final execution plan is generated; both the final execution plan and the new final execution plan are executed by the execution engine, and the execution result is returned to the client.

[0006] This application's embodiments break the existing technical framework of static binding of port number + parser through dynamic recognition, unified engine, dialect awareness optimization, and semantic reuse. Furthermore, through a four-layer system architecture of dynamic syntax recognition, unified abstract syntax tree, dialect awareness optimization, and semantic fingerprint reuse, it achieves a more flexible, efficient, and scalable multi-database syntax compatibility solution. Moreover, any port can also support three syntaxes mixed, significantly improving performance and code reusability.

[0007] In one feasible implementation, based on the SQL statement received from any port, multi-level feature extraction and analysis are performed on the SQL statement, and the dialect grammar type statement is determined based on the confidence score of each feature. Specifically, this includes: performing lexical analysis on the SQL statement and extracting keyword features, function features, operator features, and word structure features to obtain lexical features; identifying the overall structural pattern of the SQL statement and matching it with a preset database dialect structure template to determine the overall structural features of the statement; wherein the overall structural pattern includes: query block nesting level, join type, and subquery position; performing feature analysis processing on the SQL statement related to dialect context semantics based on session history information, application type identifier, and user configuration preferences to obtain context features; configuring weight coefficients for the lexical features, the overall structural features of the statement, and the context features respectively, and calculating the comprehensive confidence score of the current SQL statement belonging to each database dialect (Oracle, MySQL, PostgreSQL); and determining the database dialect with the highest confidence score among the comprehensive confidence scores as the dialect grammar type statement of the previous SQL statement.

[0008] In one feasible implementation, before performing database dialect optimization processing on the dialect grammar type statements based on dialect optimization rules, the method further includes: constructing a dialect-independent node layer related to database dialects based on the basic node types of database operations; wherein the basic node types include at least: query statement nodes, insert statement nodes, update statement nodes, delete statement nodes, join expression nodes, and conditional expression nodes; defining dialect extension node interfaces based on the dialect-independent node layer, and constructing a dialect extension node layer; wherein the dialect extension node interfaces are used to encapsulate the grammatical structure of each dialect database; each The dialect extension nodes contain source dialect identification information and undergo normalization transformation to convert them into equivalent dialect-independent node combinations. The grammatical results of dialect grammatical type statements in each dialect extension node layer are then converted into standard relational algebra identifiers, resulting in a normalization transformation mechanism. Based on the dialect-independent node layer, the dialect extension node layer, the normalization transformation mechanism, multi-dialect bitmap identifiers, and semantic fingerprints, the abstract syntax tree is constructed. The multi-dialect bitmap identifiers are used to identify the dialect mask information of each tree node in the abstract syntax tree. The semantic fingerprint on each tree node is used for execution plan reuse processing across dialect databases.

[0009] In one feasible implementation, the dialect grammar type statements are subjected to database dialect optimization processing based on dialect optimization rules through a preset abstract syntax tree, and candidate execution plans are generated. Specifically, this includes: converting all dialect grammar type statements in the dialect extension nodes of the dialect extension node layer into standardized dialect grammar type statements in the dialect-independent node layer through the normalization transformation mechanism in the abstract syntax tree; performing dialect-independent optimization processing on the standardized dialect grammar type statements through optimization rules common to all databases to obtain initial optimized dialect grammar type statements; wherein the optimization rules include at least: predicate pushdown, join rearrangement, subquery expansion, and constant folding. The optimization rules include: overlapping and redundancy elimination; the optimization rules apply to all dialect SQL statements in the standardized dialect syntax type statement; based on the dialect identifier of each SQL statement, the initial optimized dialect syntax type statement is dynamically optimized under specific optimization rules to obtain the final optimized dialect syntax type statement; wherein, the specific optimization rules include: optimization rules for Oracle syntax, optimization rules for MySQL syntax, and optimization rules for PostgreSQL syntax; the final optimized dialect syntax type statement is parsed to generate the initial candidate execution plan that conforms to the database dialect characteristics; wherein, the candidate execution plan includes execution plans under multiple optimization rules.

[0010] In one feasible implementation, before estimating the cost weights of the candidate execution plans, the method further includes: calculating the basic execution cost of each operator based on statistical information in the execution plans; wherein the statistical information includes at least: the number of table rows, the number of column cardinality, and the index selectivity; the basic execution cost includes at least: I / O cost, CPU cost, memory cost, and network cost; defining and constructing a dialect preference factor based on each database dialect; wherein the dialect preference factor is used to represent the execution strategy tendency of the candidate execution plans; defining and constructing a version adaptation factor based on the capability differences of different dialect database versions; wherein the version adaptation factor is associated with the version differences of the dialect database; constructing a dialect-aware cost model based on the basic execution cost, the dialect preference factor, the version adaptation factor, and a hardware-aware mechanism; wherein the hardware-aware mechanism is used to dynamically adjust the parameters of the dialect-aware cost model to make the generated execution plan more suitable for the operating environment.

[0011] In one feasible implementation, the final execution plan is obtained by estimating the cost weight of the candidate execution plan using the dialect preference factor of each database dialect in a preset dialect-aware cost model. Specifically, this includes: calculating the current basic execution cost of each operator in the candidate execution plan using the dialect-aware cost model; determining the current dialect preference factor corresponding to the candidate execution plan based on the dialect preference factor in the dialect-aware cost model; estimating the cost weight of the candidate execution plan based on the current basic execution cost and the current dialect preference factor to obtain a comprehensive cost estimation result; and determining the candidate execution plan with the lowest total cost in the comprehensive cost estimation result as the final execution plan that best matches the database dialect characteristics.

[0012] In one feasible implementation, after determining the dialect grammar type statement, the method further includes: extracting core semantic features from the dialect-independent node layer in the abstract syntax tree that are not related to the database dialect; wherein, the core semantic features include: table set, predicate condition, projection column set, connection graph, and aggregation operation; combining the core semantic features in a preset fixed order and fixed format to generate the semantic fingerprint based on hash value; wherein, the semantic fingerprint is the fingerprint value of the equivalent SQL statement under the elimination of database dialect differences; and establishing a mapping index table between the semantic fingerprint and the execution plan obtained after corresponding database dialect optimization processing based on the dialect grammar type statement.

[0013] In one feasible implementation, if the semantic fingerprint of a newly input current dialect grammar type statement has the same semantic fingerprint in the query index, then the cached final execution plan is directly reused for the current dialect grammar type statement; if the semantic fingerprint of a newly input current dialect grammar type statement does not have the same semantic fingerprint in the query index, then a new final execution plan is generated, specifically including: receiving the newly input current dialect grammar type statement; generating a corresponding current semantic fingerprint based on the current dialect grammar type statement; and performing a query index judgment on the current semantic fingerprint through the mapping index table: if a semantic fingerprint exists that matches the current semantic fingerprint... If semantic fingerprints are identical, the execution plan corresponding to the mapping index table is determined as the current execution plan for the current semantic fingerprint; and the current execution plan is determined as the final execution plan for direct reuse caching of the current dialect grammar type statement; wherein, the direct reuse caching process is to directly execute the current execution plan; if no semantic fingerprint is identical to the current semantic fingerprint, database dialect optimization and cost weight estimation processing are performed on the current dialect grammar type statement to obtain the new final execution plan; and the new final execution plan and the corresponding new semantic fingerprint are cached in the mapping index table.

[0014] Secondly, embodiments of this application also provide an adaptive parsing device for multiple database syntaxes, the device comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor to enable the at least one processor to execute an adaptive parsing method for multiple database syntaxes as described in any of the above embodiments.

[0015] Thirdly, embodiments of this application also provide a non-volatile computer storage medium, which is a non-volatile computer-readable storage medium storing at least one program, each program including instructions, which, when executed by a terminal, cause the terminal to execute an adaptive parsing method for multi-database syntax as described in any of the above embodiments.

[0016] This application provides an adaptive parsing method, device, and medium for multiple database syntaxes. Compared with the prior art, the embodiments of this application have the following beneficial technical effects: 1. By employing techniques such as dynamic recognition, a unified engine, dialect awareness optimization, and semantic reuse, the existing technical framework of static binding of port number and parser is broken, achieving more flexible syntax compatibility.

[0017] 2. By using a unified abstract syntax tree and dialect-aware optimization method, the parsing engine can uniformly support multiple database syntaxes, avoiding code redundancy.

[0018] 3. By utilizing semantic fingerprint reuse, optimization strategies for different database dialects can be shared, thereby improving execution efficiency.

[0019] 4. It also enables the reuse of execution plans for SQL statements with the same semantics but different dialects. Attached Figure Description

[0020] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort. In the drawings: Figure 1 A flowchart of an adaptive parsing method for multi-database syntax provided in this application embodiment; Figure 2 An overall architecture diagram of an adaptive parsing system for multi-database syntax is provided for embodiments of this application; Figure 3 This is a schematic diagram of the structure of an adaptive parsing device for multiple database syntaxes provided in an embodiment of this application. Detailed Implementation

[0021] To enable those skilled in the art to better understand the technical solutions in this application, the technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this specification, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of this application.

[0022] This application provides an adaptive parsing method for multi-database syntax, such as... Figure 1 As shown, the adaptive parsing method for multi-database syntax specifically includes steps S101-S105: S101. Based on the SQL statement received by any port, extract and analyze the SQL statement with multiple layers of features, and determine the dialect grammar type statement based on the confidence score of each layer of features.

[0023] Specifically, lexical analysis is first performed on the SQL statements received by any port, and keyword features, function features, operator features, and word structure features are extracted to obtain lexical features.

[0024] Furthermore, the overall structural pattern of the SQL statement is identified and matched against a preset database dialect structure template to determine the overall structural characteristics of the statement. The overall structural pattern includes: query block nesting level, join type, and subquery position.

[0025] Furthermore, based on session history information, application type identifiers, and user configuration preferences, feature analysis processing related to dialect context semantics is performed on SQL statements to obtain context features.

[0026] In one embodiment, such as Figure 2 As shown, in the syntax feature recognition module, the first layer is lexical feature extraction: Lexical analysis is performed on the input SQL statement to extract keyword features (such as ROWNUM, LIMIT, RETURNING, etc.), function features (such as NVL, IFNULL, COALESCE, etc.), operator features (such as (+) outer join, <=> safe equality, :: type conversion, etc.), and structural features (such as INSERT ALL, CONNECT BY, ON CONFLICT, etc.). The second layer is structural feature analysis: Identifying the overall structural pattern of the SQL statement, including query block nesting levels, join types, and subquery positions, and matching it with preset database dialect structure templates. The third layer is contextual feature analysis: Combining session history information (such as the SQL dialect type previously executed for this connection), application type identifiers (such as JDBC driver type, client tool features), and user configuration preferences to assist in dialect determination.

[0027] As a feasible implementation method, the key feature library generated during multi-level feature extraction of SQL statements includes: In keyword features: Oracle's strong features are ROWNUM, CONNECT BY, START WITH; MySQL's strong features are LIMIT, AUTO_INCREMENT; PostgreSQL's strong features are RETURNING, OFFSET, WITH. In function features: Oracle's strong features are NVL(), DECODE(), SUBSTR(str, start, len); MySQL's strong features are IFNULL(), CONCAT(), SUBSTRING(); PostgreSQL's strong features are COALESCE(), STRING_AGG(), SUBSTRING(str FROM start FOR len). In structural features: Oracle's strong features are INSERT ALL / FIRST, CONNECTBY; MySQL's strong feature is INSERT ... ON DUPLICATE KEY; PostgreSQL's strong features are INSERT ... ONCONFLICT, RETURNING.

[0028] Furthermore, weight coefficients were configured for lexical features, overall sentence structure features, and contextual features, and the comprehensive confidence score of the current SQL statement belonging to each database dialect (Oracle, MySQL, PostgreSQL) was calculated. Finally, the database dialect with the highest comprehensive confidence score was determined as the dialect grammatical type of the previous SQL statement.

[0029] In one embodiment, the comprehensive confidence score calculation assigns weight coefficients (e.g., 0.4, 0.4, 0.2) to lexical features, structural features, and contextual features, respectively, and calculates the comprehensive confidence score for the SQL statement to belong to each of the Oracle, MySQL, and PostgreSQL dialects. Then, the dialect with the highest confidence score is selected as the dialect type of the statement. When the highest confidence score is lower than a preset threshold (e.g., 0.75), a mixed syntax mode is automatically enabled. In this mode, the system independently performs dialect recognition for each SQL statement and dynamically switches the parsing context based on the recognition results, achieving seamless switching between multiple dialects within the same connection.

[0030] S102. Using a pre-defined abstract syntax tree, perform database dialect optimization processing on dialect syntax type statements based on dialect optimization rules, and generate candidate execution plans.

[0031] Specifically, based on the basic node types of database operations, a dialect-independent node layer related to database dialects is first constructed. The basic node types include at least: query statement nodes, insert statement nodes, update statement nodes, delete statement nodes, join expression nodes, and conditional expression nodes.

[0032] Furthermore, based on the dialect-independent node layer, a dialect extension node interface is defined, and a dialect extension node layer is constructed. The dialect extension node interface encapsulates the syntactic structure of each dialect database. Each dialect extension node contains source dialect identification information and undergoes normalization transformation to convert it into an equivalent combination of dialect-independent nodes.

[0033] Furthermore, the grammatical results of dialect grammatical type statements in each dialect extension node layer are converted into standard relational algebra identifiers, resulting in a normalization conversion mechanism.

[0034] Furthermore, an abstract syntax tree (AST) is constructed based on a dialect-independent node layer, a dialect extension node layer, a normalization transformation mechanism, multi-dialect bitmap identifiers, and semantic fingerprints. The multi-dialect bitmap identifiers are used to identify the dialect mask information of each tree node in the AST. The semantic fingerprint on each tree node is used for execution plan reuse processing across dialect databases.

[0035] As a feasible implementation method, the unified AST (Abstract Syntax Tree) needs to be structurally configured, and the abstract syntax tree should include: 1) Dialect-Independent Node Layer: Defines the basic node types for database operations, including query statement nodes (SelectStmt), insert statement nodes (InsertStmt), update statement nodes (UpdateStmt), delete statement nodes (DeleteStmt), join expression nodes (JoinExpr), and conditional expression nodes (WhereClause). These node types are independent of specific database dialects and are a common abstraction for all SQL statements.

[0036] 2) Dialect Extension Node Layer: Building upon the dialect-independent nodes, a dialect extension node interface is defined to encapsulate the unique grammatical structures of each database. Each dialect extension node contains source dialect identification information and provides a normalization conversion method, enabling it to be converted into an equivalent combination of dialect-independent nodes.

[0037] 3) Normalization Conversion Mechanism: Each dialect extension node implements a normalization conversion method, converting the dialect-specific grammatical structure into a standard relational algebra representation. For example, Oracle's INSERT ALL statement is normalized into a combination of multiple condition judgment nodes and multiple standard insert statement nodes; MySQL's LIMIT clause is normalized into a standard restricted offset node; and Oracle's ROWNUM condition is normalized into a standard restricted node.

[0038] The Abstract Syntax Tree (AST) also features multi-dialect bitmap identification, meaning each AST node carries dialect mask information and uses a bitmap to identify the database dialect types it supports. This facilitates targeted optimization by the optimizer based on dialect characteristics. Furthermore, the AST also supports semantic fingerprint generation, where each AST node generates a semantic fingerprint for cross-dialect execution plan reuse. The fingerprint generation process is based on the normalized node structure, eliminating dialect differences and ensuring that semantically equivalent SQL statements generate the same fingerprint value.

[0039] Furthermore, through the normalization transformation mechanism in the abstract syntax tree, all dialect grammar type statements of dialect extension nodes in the dialect extension node layer are converted into standardized dialect grammar type statements of dialect-independent nodes in the dialect-independent node layer.

[0040] Furthermore, dialect-independent optimization is performed on standardized dialect grammar type statements using optimization rules common to all databases, resulting in initially optimized dialect grammar type statements. These optimization rules include at least: predicate pushdown, join rearrangement, subquery expansion, constant folding, and redundancy elimination. These optimization rules apply to all dialect SQL statements within the standardized dialect grammar type statements.

[0041] Furthermore, based on the dialect identifier of each SQL statement, dynamic optimization processing under specific optimization rules is required for the initial optimized dialect syntax type statements to obtain the final optimized dialect syntax type statements. These specific optimization rules include optimization rules for Oracle syntax, MySQL syntax, and PostgreSQL syntax. Finally, statement parsing of the final optimized dialect syntax type statements generates initial candidate execution plans that conform to the database dialect characteristics. These candidate execution plans include execution plans under various optimization rules.

[0042] In one embodiment, normalization is performed first: A unified AST is received, and the normalization methods for each dialect extension node are called to convert all dialect extension nodes into dialect-independent nodes, forming a fully standardized internal representation. Then, dialect-independent optimization is performed: optimization rules common to all databases are applied, including predicate pushdown, join rearrangement, subquery expansion, constant folding, and redundancy elimination. These optimization rules are applicable to SQL statements in all dialects, enabling the sharing and reuse of optimization strategies. Next, dialect-specific optimization is performed: based on the dialect identifier of the SQL statement, dialect-specific optimization rules are dynamically loaded. For example, optimization rules for Oracle syntax include ROWNUM short-circuit evaluation optimization and CONNECT BY recursive optimization; optimization rules for MySQL syntax include LIMIT delayed materialization optimization and index merging optimization; and optimization rules for PostgreSQL syntax include parallel scan optimization and window function optimization. Finally, the final optimized dialect syntax type statement is parsed to generate an initial candidate execution plan that conforms to the database dialect characteristics.

[0043] As one possible implementation, the dialect extension node may include: OracleInsertAllStmt, MySQLLimitClause, OracleConnectBy, MySQLOnDuplicateKey, and OracleRowNum; its corresponding normalization objective is: IfStmt + List <insertstmt>StandardLimitClause, RecursiveCTE, InsertStmt + UpdateStmt, and LimitClause; their corresponding optimization strategies are shared as follows: batch insertion optimization and parallel execution, delayed materialization and Top-N optimization, recursive optimization and loop detection, conflict detection optimization and batch merging, and counter optimization and short-circuit evaluation.

[0044] S103. By using the dialect preference factors of each database dialect in the preset dialect perception cost model, the cost weight of the candidate execution plan is estimated to obtain the final execution plan.

[0045] Specifically, based on the statistical information in the execution plan, the basic execution cost of each operator is calculated. This statistical information includes at least: the number of table rows, column cardinality, and index selectivity. The basic execution cost includes at least: I / O cost, CPU cost, memory cost, and network cost. For each database dialect, a dialect preference factor is defined and constructed. This factor represents the execution strategy preference of the candidate execution plan. Based on the capability differences between different dialect database versions, a version adaptation factor is defined and constructed. This factor is associated with the version differences of the dialect database.

[0046] Furthermore, a dialect-aware cost model is constructed based on the basic execution cost, dialect preference factor, version adaptation factor, and hardware awareness mechanism. The hardware awareness mechanism is used to dynamically adjust the parameters of the dialect-aware cost model to make the generated execution plan more suitable for the operating environment.

[0047] As a feasible implementation method, the dialect-aware cost model can achieve the following: 1) Basic cost calculation: Based on unified statistical information (number of table rows, column cardinality, index selectivity, etc.), calculate the basic execution cost of each operator, including I / O cost, CPU cost, memory cost, network cost, etc. 2) Dialect preference factor: Define a set of preference adjustment factors for each database dialect to reflect the tendency of that dialect in execution strategy. For example, the Oracle optimizer traditionally prefers nested loop joins, so the cost weight of nested loop joins is appropriately reduced in Oracle dialect mode; PostgreSQL's hash join implementation is more efficient, so the cost weight of hash joins is reduced in PostgreSQL dialect mode. 3) Version adaptation factor: Set version adaptation factors according to the capability differences of different database versions. For example, MySQL version 8.0 supports hash joins, while older versions do not, so when an older version of the MySQL dialect is detected, a higher penalty coefficient is set for hash joins. 4) Hardware-aware adjustment: Combine the underlying hardware characteristics (such as the number of CPU cores, memory size, disk type, network bandwidth, etc.) to dynamically adjust the parameters of the cost model so that the generated execution plan is more in line with the actual running environment.

[0048] Furthermore, a dialect-aware cost model is needed to calculate the current base execution cost of each operator in the candidate execution plan. Then, based on the dialect preference factor in the dialect-aware cost model, the current dialect preference factor corresponding to the candidate execution plan is determined. Next, based on the current base execution cost and the current dialect preference factor, cost weight estimation is performed on the selected execution plans to obtain a comprehensive cost estimation result. Finally, the candidate execution plan with the lowest total cost in the comprehensive cost estimation result is determined as the final execution plan that best matches the database dialect characteristics.

[0049] In one embodiment, after generating candidate execution plans, a dialect-aware cost model is used for cost estimation. The cost model includes preference adjustment factors for each database dialect; for example, Oracle prefers nested loop joins, older versions of MySQL have weaker support for hash joins, and PostgreSQL has higher efficiency for parallel sequential scans. Then, the cost weights of each operator are adjusted according to the dialect preference factors to select the execution plan that best suits the characteristics of that dialect. Finally, the execution plan with the lowest total cost from the comprehensive cost estimation results is selected as the final execution plan. The selection process must consider dialect-specific constraints, such as prioritizing Oracle's ROWNUM constraint and optimizing MySQL's LIMIT constraint in conjunction with sorting operations.

[0050] S104: If the semantic fingerprint of the newly input current dialect-specific syntax type statement has an identical semantic fingerprint in the query index, directly reuse the cached final execution plan for the current dialect-specific syntax type statement. If the semantic fingerprint of the newly input current dialect-specific syntax type statement does not have an identical semantic fingerprint in the query index, generate a new final execution plan.

[0051] Specifically, first extract the core semantic features of the dialect-specific syntax type statement that are unrelated to database dialects through the dialect-agnostic node layer in the abstract syntax tree. The core semantic features include: table set, predicate conditions, projection column set, join relationship graph, and aggregation operations.

[0052] In one embodiment, semantic normalization fingerprint technology is used to map semantically equivalent SQL statements of different dialects to the same fingerprint value, enabling cross-dialect reuse of execution plans. First, parse the input SQL statement into a unified AST (Abstract Syntax Tree), and perform complete normalization transformation to eliminate all dialect-specific syntax structure differences, that is, perform database dialect optimization processing on the dialect-specific syntax type statement based on dialect optimization rules to obtain a pure relational algebra representation. Then extract dialect-agnostic core semantic features from the final optimized dialect-specific syntax type statement after normalization, including the involved table set (sorted by name), predicate conditions (normalized representation), projection column set (sorted by name), join relationship graph (join type and join conditions), aggregation operations (aggregation function type and grouping columns), etc.

[0053] As a feasible implementation, the predicate conditions in the core semantic features also need to be normalized to eliminate non-equivalence caused by writing differences. Normalization processing includes constant folding (e.g., converting a>1+2 to a>3), comparison direction unification (e.g., converting 55), logical expression normalization (e.g., converting (a AND b) OR (a AND c) to a AND (b OR c)), etc.

[0054] Further, combine the core semantic features according to a preset fixed order and fixed format to generate a hash value-based semantic fingerprint. The semantic fingerprint is the fingerprint value of equivalent SQL statements after eliminating database dialect differences. Then, based on the dialect-specific syntax type statement, finally establish a mapping index table between the semantic fingerprint and the execution plan obtained after corresponding database dialect optimization processing.

[0055] In one embodiment, the extracted core semantic features need to be combined in a fixed order and format to generate deterministic hash values ​​as semantic fingerprints. Since the feature extraction process has eliminated dialect differences, semantically equivalent SQL statements (regardless of the dialect used) will generate exactly the same fingerprint values. Finally, a mapping index table is established between semantic fingerprints and the execution plans obtained after dialect optimization in the corresponding database, so that each semantic fingerprint has a corresponding execution plan.

[0056] Furthermore, the system first receives a new input statement of the current dialect grammar type. Based on this statement, a corresponding current semantic fingerprint is generated. Then, the current semantic fingerprint is queried and analyzed using a mapping index table. If a semantic fingerprint exists that is identical to the current semantic fingerprint, then the execution plan corresponding to the mapping index table is determined as the current execution plan for the current semantic fingerprint. This current execution plan is then used as the final execution plan for the direct reuse cache of statements in the current dialect syntax type. The process of directly reusing the cache involves directly executing the current execution plan.

[0057] If no semantic fingerprint exists that matches the current semantic fingerprint, then database dialect optimization and cost weight estimation are performed on the current dialect grammar type statement to obtain a new final execution plan. The new final execution plan and its corresponding new semantic fingerprint are then cached in the mapping index table.

[0058] In one embodiment, when a new SQL statement arrives, a semantic fingerprint is generated based on the current dialect grammar type statement after database dialect optimization. Then, the mapping index table is queried to see if a semantic fingerprint with the same recorded fingerprint value exists. If it exists, the cached execution plan is directly reused, i.e., the execution plan corresponding to the same semantic fingerprint in the mapping index table is used, skipping the aforementioned complete parsing and optimization process. If it does not exist, the complete parsing and optimization process is executed, including database dialect optimization and cost weight estimation, to obtain a new final execution plan. This newly generated final execution plan is then added to the mapping index table, thus achieving data caching.

[0059] As a feasible implementation method, semantic fingerprinting can be used to achieve cross-dialect reuse of different database dialects. Semantic equivalent SQL statements from different dialects are mapped to the same fingerprint value, enabling cross-dialect reuse of the same execution plan. This can include scenarios such as: Pagination query reuse: Oracle dialect's SELECT FROM t WHERE ROWNUM<= 10 and SELECT statements in MySQL dialects The query FROM t LIMIT 10, after normalization, represents a query on the first 10 rows of table t, generating the same semantic fingerprint, thus allowing the reuse of the same execution plan.

[0060] Null value handling reuse: The Oracle dialect's SELECT NVL(col, 0) FROM t and the MySQL dialect's SELECTIFNULL(col, 0) FROM t, after normalization, both represent null value replacement operations on the col column, have the same semantic fingerprint, and their execution plans can be reused.

[0061] Outer join reuse: Oracle dialect SELECT FROM t1, t2 WHERE t1.id = t2.id(+) and SELECT in PostgreSQL dialects The operations FROM t1 LEFT JOIN t2 ON t1.id = t2.id, after normalization, are all represented as left outer join operations, with the same semantic fingerprint, and the execution plan can be reused.

[0062] Conditional insert reuse: The Oracle dialect's INSERT ALL WHEN condition THEN INTO... and multiple INSERT statements split at the application layer can be identified as having the same batch insert semantics under specific conditions, thus achieving execution plan reuse.

[0063] S105. The execution engine processes both the final execution plan and the new final execution plan, and returns the execution results to the client.

[0064] As a feasible implementation method, in Oracle INSERT ALL compatibility: 1) Scenario: Migrating INSERT ALL statements in Oracle applications.

[0065] 2) Enter the SQL statement: INSERT ALL WHEN sal>10000 THEN INTO high_sal VALUES (emp_id, sal) WHEN sal>5000 THEN INTO mid_sal VALUES (emp_id, sal) ELSE INTO low_sal VALUES (emp_id, sal) SELECT emp_id, sal FROM employees; 3) Processing flow: Syntax recognition: The system performs lexical analysis on the input SQL, identifies Oracle-specific keywords such as INSERT ALL, WHEN, THEN, and ELSE, and combines structural features (multi-condition branch insertion) to calculate the Oracle dialect confidence score of 0.95, thus identifying it as an Oracle dialect.

[0066] AST Construction: Construct the OracleInsertAllStmt dialect extension node, which contains two WhenClause nodes (with conditions sal>10000 and sal>5000 respectively) and one ElseClause node.

[0067] Normalization transformation: Call the normalization method of OracleInsertAllStmt to convert it into a combination of IfStmt node (condition judgment) and three InsertStmt nodes (corresponding to the high_sal, mid_sal and low_sal tables respectively).

[0068] Unified optimization: Apply dialect-independent optimization rules (such as predicate pushdown and constant folding), then load Oracle-specific optimization rules (batch insert optimization) to generate a batch binding execution plan.

[0069] Planned execution: Three insert operations are executed in parallel within a single transaction. Batch binding reduces network round trips and ensures atomicity (if any insert fails, all operations are rolled back).

[0070] As a feasible implementation method, in MySQL LIMIT optimization, 1) Scenario: Handling MySQL pagination queries.

[0071] 2) Enter the SQL statement: SELECT FROM orders WHERE status = 'pending' LIMIT 100, 20; 3) Processing flow: Syntax recognition: Recognize the syntactic structure LIMIT 100, 20 (offset 100, return 20 rows), calculate the MySQL dialect confidence score as 0.92, and identify it as a MySQL dialect.

[0072] AST Construction: Construct the MySQLLimitClause dialect extension node, which contains two expressions: offset=100 and count=20.

[0073] Normalization transformation: Convert MySQLLimitClause to StandardLimitClause node (limit=20, offset=100).

[0074] Dialect-aware optimization: In MySQL dialect mode, enable the LIMIT delayed materialization optimization rule to avoid obtaining all results before truncation; at the same time, enable Top-N optimization to use index skip scan to directly locate the 101st row of data.

[0075] Execution plan: Skip the first 100 rows by using the index and return the next 20 rows directly to avoid a full table scan.

[0076] As a feasible implementation method, in cross-dialect project reuse: 1) Scenario: Queries in different dialects with the same semantic meaning.

[0077] SQL A (Oracle dialect): SELECT FROM t WHERE ROWNUM <= 10; SQL B (MySQL dialect): SELECT FROM t LIMIT 10; 2) Processing flow: SQL A processing: Parse into a unified AST, construct an OracleRowNum dialect extension node (condition ROWNUM<=10), normalize to StandardLimitClause (limit=10, offset=0), generate semantic fingerprint FP_001, generate execution plan PLAN_001 and cache it.

[0078] SQL B processing: Parse into a unified AST, construct a MySQLLimitClause dialect extension node (limit=10), normalize to StandardLimitClause (limit=10, offset=0), and generate a semantic fingerprint FP_001.

[0079] Fingerprint comparison: The semantic fingerprint FP_001 of SQL B is exactly the same as FP_001 in the cache, and the system determines that the two SQL statements are semantically equivalent.

[0080] Plan reuse: Directly reuse PLAN_001, skipping the complete parsing and optimization process, reducing the execution time from 15ms to 2ms.

[0081] In addition, embodiments of this application also provide an adaptive parsing device for multiple database syntaxes, such as... Figure 3 As shown, the adaptive parsing device 300 for multiple database syntaxes specifically includes: At least one processor 301; and a memory 302 communicatively connected to the at least one processor 301; wherein the memory 302 stores instructions executable by the at least one processor 301 to enable the at least one processor 301 to execute: Based on the SQL statements received by the port, multi-level features are extracted and analyzed from the SQL statements, and the dialect grammar type statement is determined based on the confidence score of each feature. Using a pre-defined abstract syntax tree, database dialect optimization is performed on dialect syntax type statements based on dialect optimization rules, and candidate execution plans are generated. By using the dialect preference factors of each database dialect in the preset dialect perception cost model, the cost weight of the candidate execution plan is estimated to obtain the final execution plan; If the semantic fingerprint of the newly input statement of the current dialect grammar type has the same semantic fingerprint in the query index, the cached final execution plan is directly reused for the statement of the current dialect grammar type; if the semantic fingerprint of the newly input statement of the current dialect grammar type does not have the same semantic fingerprint in the query index, a new final execution plan is generated. The execution engine processes both the final execution plan and the new final execution plan, and returns the execution results to the client.

[0082] This application's embodiments break the existing technical framework of static binding of port number + parser through dynamic recognition, unified engine, dialect awareness optimization, and semantic reuse. Furthermore, through a four-layer system architecture of dynamic syntax recognition, unified abstract syntax tree, dialect awareness optimization, and semantic fingerprint reuse, it achieves a more flexible, efficient, and scalable multi-database syntax compatibility solution. Moreover, any port can also support three syntaxes mixed, significantly improving performance and code reusability.

[0083] The various embodiments in this application are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the device and medium embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the description of the method embodiments.

[0084] The devices and media provided in this application are one-to-one with the methods. Therefore, the devices and media also have similar beneficial technical effects as their corresponding methods. Since the beneficial technical effects of the methods have been described in detail above, the beneficial technical effects of the devices and media will not be repeated here.

[0085] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0086] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0087] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.

[0088] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.

[0089] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0090] Memory may include non-persistent storage in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.

[0091] Computer-readable media includes both permanent and non-permanent, removable and non-removable media that can store information using any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.

[0092] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0093] The above description is merely an embodiment of this application and is not intended to limit the scope of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of this specification.< / insertstmt>

Claims

1. An adaptive parsing method for multi-database syntax, characterized in that, The method includes: Based on the SQL statement received from any port, multi-level features are extracted and analyzed from the SQL statement, and the dialect grammar type statement is determined based on the confidence score of each feature. Based on the basic node types of database operations, a dialect-independent node layer related to database dialects is constructed; wherein, the basic node types include at least: query statement nodes, insert statement nodes, update statement nodes, delete statement nodes, join expression nodes, and conditional expression nodes. Based on the dialect-independent node layer, a dialect extension node interface is defined, and a dialect extension node layer is constructed; wherein, the dialect extension node interface is used to encapsulate the grammatical structure of each dialect database; each dialect extension node contains source dialect identification information and is subjected to normalization transformation processing to be converted into an equivalent dialect-independent node combination. The grammatical results of the dialect grammatical type statements in each dialect extension node layer are converted into standard relational algebra identifiers to obtain a normalization conversion mechanism. An abstract syntax tree is constructed based on the dialect-independent node layer, the dialect extended node layer, the normalization transformation mechanism, the multi-dialect bitmap identifier, and the semantic fingerprint; wherein, the multi-dialect bitmap identifier is used to identify the dialect mask information of each tree node in the abstract syntax tree; the semantic fingerprint on each tree node is used for execution plan reuse processing across dialect databases; Through the dialect-independent node layer in the abstract syntax tree, the core semantic features of the dialect grammar type statement that are not related to the database dialect are extracted; wherein, the core semantic features include: table set, predicate condition, projection column set, connection graph and aggregation operation; The core semantic features are combined and processed according to a preset fixed order and fixed format to generate the semantic fingerprint based on hash value; wherein, the semantic fingerprint is the fingerprint value of the equivalent SQL statement under the elimination of database dialect differences; Based on the dialect grammar type statement, establish a mapping index table between the semantic fingerprint and the execution plan obtained after dialect optimization processing in the corresponding database; Using a pre-defined abstract syntax tree, the dialect syntax type statements are subjected to database dialect optimization processing based on dialect optimization rules, and candidate execution plans are generated. The cost weight of the candidate execution plan is estimated by using the dialect preference factor of each database dialect in the preset dialect perception cost model, and the final execution plan is obtained. If the semantic fingerprint of the newly input statement of the current dialect grammar type has the same semantic fingerprint in the query index, then the cached final execution plan is directly reused for the statement of the current dialect grammar type; if the semantic fingerprint of the newly input statement of the current dialect grammar type does not have the same semantic fingerprint in the query index, then a new final execution plan is generated, specifically including: Receive a new input statement of the current dialect grammar type; and generate a corresponding current semantic fingerprint based on the current dialect grammar type statement; The current semantic fingerprint is queried and determined using the mapping index table: If a semantic fingerprint exists that is identical to the current semantic fingerprint, then the execution plan corresponding to the mapping index table is determined as the current execution plan of the current semantic fingerprint; The current execution plan is then determined as the final execution plan for the direct reuse cache of the current dialect syntax type statement; wherein, the direct reuse cache processing procedure is to directly execute the current execution plan; If no semantic fingerprint exists that is identical to the current semantic fingerprint, then database dialect optimization and cost weight estimation are performed on the current dialect grammar type statement to obtain the new final execution plan; and the new final execution plan and the corresponding new semantic fingerprint are cached in the mapping index table. The execution engine processes both the final execution plan and the new final execution plan, and returns the execution results to the client.

2. The adaptive parsing method for multi-database syntax according to claim 1, characterized in that, Based on the SQL statements received from any port, multi-level feature extraction and analysis are performed on the SQL statements, and the dialect grammar type statement is determined based on the confidence score of each feature level, specifically including: Lexical analysis is performed on the SQL statement, and keyword features, function features, operator features, and word structure features are extracted to obtain lexical features; The overall structural pattern of the SQL statement is identified and matched with a preset database dialect structure template to determine the overall structural features of the statement; wherein, the overall structural pattern includes: query block nesting level, join type and subquery position; Based on session history information, application type identifiers, and user configuration preferences, the SQL statement is subjected to feature analysis processing related to dialect context semantics to obtain context features; The weight coefficients of the lexical features, the overall structure features of the statement, and the context features are configured respectively, and the comprehensive confidence score of the current SQL statement belonging to the dialects of Oracle, MySQL, and PostgreSQL is calculated. The database dialect with the highest confidence score among the comprehensive confidence scores is identified as the dialect grammatical type statement of the preceding SQL statement.

3. The adaptive parsing method for multi-database syntax according to claim 1, characterized in that, Using a pre-defined abstract syntax tree, the dialect syntax type statements are subjected to database dialect optimization processing based on dialect optimization rules, and candidate execution plans are generated, specifically including: Through the normalization transformation mechanism in the abstract syntax tree, the dialect grammar type statements of the dialect extension nodes in the dialect extension node layer are all converted into the standardized dialect grammar type statements of the dialect-independent nodes in the dialect-independent node layer. Using optimization rules common to all databases, dialect-independent optimization is performed on the standardized dialect grammar type statements to obtain the initial optimized dialect grammar type statements; wherein, the optimization rules include at least: predicate pushdown, join rearrangement, subquery expansion, constant folding, and redundancy elimination; the optimization rules are applicable to all dialect SQL statements in the standardized dialect grammar type statements; Based on the dialect identifier of each SQL statement, the initial optimized dialect syntax type statement is dynamically optimized under specific optimization rules to obtain the final optimized dialect syntax type statement; wherein, the specific optimization rules include: optimization rules for Oracle syntax, optimization rules for MySQL syntax, and optimization rules for PostgreSQL syntax; The final optimized dialect grammar type statement is parsed to generate an initial candidate execution plan that conforms to the dialect characteristics of the database; wherein, the candidate execution plan includes execution plans under multiple optimization rules.

4. The adaptive parsing method for multi-database syntax according to claim 1, characterized in that, Before performing cost weight estimation on the candidate execution plans, the method further includes: Based on the statistical information in the execution plan, the basic execution cost of each operator is calculated; wherein, the statistical information includes at least: the number of table rows, the number of column cardinality, and the index selectivity; the basic execution cost includes at least: I / O cost, CPU cost, memory cost, and network cost; Based on each database dialect, a dialect preference factor is defined and constructed; wherein, the dialect preference factor is used to represent the execution strategy preference of the candidate execution plan; Based on the capability differences of different dialect database versions, a version adaptation factor is defined and constructed; wherein, the version adaptation factor is associated with the version differences of the dialect database. Based on the aforementioned basic execution cost, dialect preference factor, version adaptation factor, and hardware awareness mechanism, a dialect-aware cost model is constructed; wherein, the hardware awareness mechanism is used to dynamically adjust the parameters of the dialect-aware cost model so that the generated execution plan is more in line with the operating environment.

5. The adaptive parsing method for multi-database syntax according to claim 4, characterized in that, By using the dialect preference factors of each database dialect in the preset dialect perception cost model, the cost weight of the candidate execution plan is estimated to obtain the final execution plan, which specifically includes: The dialect-aware cost model is used to calculate the current basic execution cost of each operator in the candidate execution plan; Based on the dialect preference factor in the dialect perception cost model, the current dialect preference factor corresponding to the candidate execution plan is determined; Based on the current basic execution cost and the current dialect preference factor, the cost weight of the selected execution plan is estimated to obtain a comprehensive cost estimation result; The candidate execution plan with the lowest total cost among the comprehensive cost estimation results is determined as the final execution plan that best matches the characteristics of the database dialect.

6. An adaptive parsing device for multiple database syntaxes, characterized in that, The device includes: At least one processor; and, A memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor to enable the at least one processor to perform an adaptive parsing method for a multi-database syntax according to any one of claims 1-5.

7. A non-volatile computer storage medium, characterized in that, The storage medium is a non-volatile computer-readable storage medium that stores at least one program, each program including instructions that, when executed by a terminal, cause the terminal to perform an adaptive parsing method for multi-database syntax according to any one of claims 1-5.

Citation Information

Patent Citations

  • Double-port analysis method and device based on PostgreSQL (Structured Query Language)

    CN116150250A

  • Multi-database compatible query processing method, equipment and medium

    CN120596511A