Conditional expression extraction method, system and device, storage medium and program product

By converting SQL statements into ASTs and constructing structured conditional expression trees using visitor pattern recognition and recursive parsing, the problems of missing conditional logic and coarse extraction granularity in existing technologies are solved, enabling support for advanced applications.

CN121833749APending Publication Date: 2026-04-10CHINA MERCHANTS BANK
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
CHINA MERCHANTS BANK
Filing Date
2026-03-09
Publication Date
2026-04-10

AI Technical Summary

Technical Problem

Existing SQL parsing technologies cannot parse the internal logic of conditional expressions, resulting in missing conditional logic, loss of business rules, coarse extraction granularity, lack of automation tool support, and difficulty in handling complex nested conditions.

Method used

The SQL statement is converted into an abstract syntax tree (AST) using an SQL parsing engine and syntax rule file. The visitor pattern is used to traverse the AST, identify and filter condition context nodes, and construct a structured condition expression tree through recursive parsing.

Benefits of technology

It enables the complete extraction of conditional logic from SQL statements without manual intervention, generating a structured conditional expression tree that preserves nested structure and semantics, and supports advanced applications such as data transmission row integrity and business rule mining.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121833749A_ABST
    Figure CN121833749A_ABST
Patent Text Reader

Abstract

The invention discloses a condition expression extraction method, system and device, a storage medium and a program product, and relates to the technical field of data analysis, and the method comprises the steps: converting an SQL statement into an AST based on an SQL analysis engine and a grammar rule file corresponding to an SQL dialect; traversing the AST by utilizing a preset visitor mode, and identifying and screening out at least one condition context node meeting a preset target condition type; and performing recursive analysis on each conditional context node, extracting a corresponding conditional expression, and constructing a corresponding structured conditional expression tree. According to the scheme, the AST is traversed in the visitor mode to recognize the multiple conditional context nodes, and conditional logic can be completely extracted from the SQL statement on the premise that manual intervention is not needed; and meanwhile, recursive analysis is performed on each conditional context node, and a structured conditional expression tree is constructed, so that an extraction result has a tree expression form which retains an original nested structure and semantics.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of data analysis technology, and in particular to methods, systems, devices, storage media, and program products for extracting conditional expressions. Background Technology

[0002] Current mainstream SQL parsing technologies (such as those based on ANTLR and JSqlParser) mainly focus on data lineage analysis. The typical process is as follows: First, the SQL statement is converted into an abstract syntax tree (AST); then, by traversing the AST, the nodes of clauses such as projection, filtering, and association are located to extract the dependencies between fields and tables; finally, a lineage graph of point-to-point (dependencies between fields, tables, and between fields and tables) is output.

[0003] However, existing technologies have the following drawbacks: ① Missing conditional logic: Existing solutions only extract table / field nodes from clauses such as filtering and association, or treat them as whole text, and cannot parse the internal logic of conditional expressions (such as nested relationships, value comparisons, value ranges, etc.).

[0004] ② Business rules are lost: Filtering rules in conditional expressions (such as salary>5000 AND department ='IT') cannot be extracted in a structured form, which makes it impossible to support advanced applications such as data transmission row integrity and business rule mining.

[0005] ③ Coarse extraction granularity: Traditional methods (such as regular expressions) have difficulty handling complex nested conditions (such as parentheses and subqueries) and are easily affected by string values ​​or comments, resulting in incomplete or incorrect extraction.

[0006] ④ Optimization relies on manual intervention: Developers need to manually analyze conditions and extract expression logic, lacking support from automated tools.

[0007] The above content is only used to help understand the technical solution of this application and does not represent an admission that the above content is prior art. Summary of the Invention

[0008] The main purpose of this application is to provide a method, system, device, storage medium and program product for extracting conditional expressions, which aims to solve the technical problem of how to extract complete and structured conditional expressions from SQL statements without human intervention.

[0009] To achieve the above objectives, this application proposes a conditional expression extraction method, which includes: Retrieve the SQL statement; Based on the SQL parsing engine and the corresponding SQL dialect syntax rule file, the SQL statement is converted into an abstract syntax tree (AST). The AST is traversed using a preset visitor pattern to identify and filter at least one conditional context node that meets the preset target condition type. Each condition context node is recursively parsed to extract the corresponding condition expression, and a corresponding structured condition expression tree is constructed.

[0010] In one embodiment, the step of traversing the AST using a preset visitor pattern to identify and filter at least one conditional context node that satisfies a preset target condition type includes: The AST nodes in the AST are traversed using a preset visitor pattern. During the traversal, determine whether the node type of the current AST node matches any of the preset target condition context types; If a match is found, the AST node is used as a condition context node, and the node information corresponding to the condition context node is recorded. Continue traversing the next AST node until a complete traversal of the abstract syntax tree is completed, and output the list of conditional context nodes.

[0011] In one embodiment, the preset target condition context type includes any one of the following: filter condition, grouped filter condition, connection condition, and conditional branch; The conditional context nodes include filter conditional context nodes and grouped filter context nodes. Connect either the conditional context node or the conditional branch context node.

[0012] In one embodiment, the step of recursively parsing each condition context node to extract the corresponding condition expression and constructing the corresponding structured condition expression tree includes: Starting from the abstract syntax subtree corresponding to the conditional context node, the abstract syntax subtree is recursively parsed using a preset visitor pattern; During the recursive parsing process, each node of the abstract syntax subtree is visited, and different operations are performed for different types of nodes to obtain multiple nodes with the type attribute; Based on the multiple nodes with the type attribute, construct a structured conditional expression tree.

[0013] In one embodiment, the different types of nodes include logical operators, comparison operators, and operands, wherein the operands include fields, constant values, and subqueries; The steps of performing different operations on different types of nodes to obtain multiple nodes with a type attribute include: When the logical operator is accessed, a corresponding logical node is created. When the comparison operator is invoked, a corresponding comparison node is created; When the field is accessed, an identifier node is created; When the constant value is accessed, a constant value node is created; When the subquery is accessed, a new condition expression extraction process is initiated to obtain an independent condition expression tree.

[0014] In one embodiment, the step of constructing a structured conditional expression tree based on the plurality of nodes having the type attribute includes: For each logical node, the conditional expressions corresponding to the left operand and the right operand of the logical node are processed recursively, and the processing results are used as the left child node and the right child node of the logical node, respectively. For each comparison node, process the left and right operands of the comparison node respectively, and use the processing results as the left and right child nodes of the comparison node; The identifier node and constant value node are incorporated as operand nodes into the structured conditional expression tree; The independent condition expression tree generated by the subquery is incorporated as a child node into the corresponding comparison node or logical node; The final structured conditional expression tree is assembled based on the logical nodes, comparison nodes, identifier nodes, constant value nodes, and independent conditional expression trees.

[0015] Furthermore, to achieve the above objectives, this application also proposes a conditional expression extraction system, which includes: The retrieval module is used to retrieve SQL statements; The conversion module is used to convert the SQL statement into an abstract syntax tree (AST) based on the SQL parsing engine and the corresponding SQL dialect syntax rule file. The traversal module is used to traverse the AST using a preset visitor pattern, identify and filter at least one conditional context node that satisfies a preset target condition type. The extraction module is used to recursively parse each of the condition context nodes, extract the corresponding condition expressions, and construct the corresponding structured condition expression tree.

[0016] In addition, to achieve the above objectives, this application also proposes a conditional expression extraction device, the device comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the conditional expression extraction method as described above.

[0017] In addition, to achieve the above objectives, this application also proposes a storage medium, which is a computer-readable storage medium, on which a computer program is stored, and which, when executed by a processor, implements the steps of the conditional expression extraction method described above.

[0018] In addition, to achieve the above objectives, this application also provides a computer program product, which includes a computer program that, when executed by a processor, implements the steps of the conditional expression extraction method described above.

[0019] This application proposes a method, system, device, storage medium, and program product for extracting conditional expressions. The method includes: acquiring an SQL statement; converting the SQL statement into an Abstract Syntax Tree (AST) based on an SQL parsing engine and a syntax rule file for the corresponding SQL dialect; traversing the AST using a preset visitor pattern to identify and filter at least one conditional context node that satisfies a preset target condition type; recursively parsing each conditional context node to extract the corresponding conditional expression and constructing a corresponding structured conditional expression tree. This solution, by using a visitor pattern to traverse the AST to identify multiple conditional context nodes, can completely extract conditional logic from SQL statements without manual intervention. Simultaneously, by recursively parsing each conditional context node and constructing a structured conditional expression tree, the extraction result has a tree-like representation that preserves the original nested structure and semantics. Attached Figure Description

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

[0021] 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, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0022] Figure 1 This is a flowchart illustrating an embodiment of the method for extracting conditional expressions in this application. Figure 2This is a flowchart illustrating the process of performing recursive AST traversal based on the visitor pattern, provided in Embodiment 1 of the condition expression extraction method of this application. Figure 3 A simplified flowchart illustrating the conditional expression extraction method provided in Embodiment 1 of this application; Figure 4 This is a schematic diagram of the module structure of the condition expression extraction system according to an embodiment of this application; Figure 5 This is a schematic diagram of the device structure of the hardware operating environment involved in the conditional expression extraction method in the embodiments of this application.

[0023] The purpose, features, and advantages of this application will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation

[0024] It should be understood that the specific embodiments described herein are merely illustrative of the technical solutions of this application and are not intended to limit this application.

[0025] To better understand the technical solution of this application, a detailed description will be provided below in conjunction with the accompanying drawings and specific implementation methods.

[0026] The main solution of this application embodiment is: to obtain the SQL statement; to convert the SQL statement into an abstract syntax tree (AST) based on the SQL parsing engine and the syntax rule file of the corresponding SQL dialect; to traverse the AST using a preset visitor pattern, identify and filter at least one condition context node that satisfies the preset target condition type; to recursively parse each condition context node, extract the corresponding condition expression, and construct the corresponding structured condition expression tree.

[0027] In this embodiment, for ease of description, the following description will focus on the conditional expression extraction system as the execution entity.

[0028] Because existing technologies only extract table and field information, local conditions, or simply concatenate multiple clause conditions from single clauses (such as filtering), the conditional logic is fragmented into isolated pieces. For example, association conditions and filtering conditions cannot be logically unified, and the semantic boundaries between grouped filtering and basic filtering (WHERE) are confused, thus failing to form a complete and executable global business rule view.

[0029] Furthermore, traditional methods (such as those based on regular expressions) struggle to handle nested logic (such as multi-level AND / OR combinations, subqueries, and function calls), resulting in the loss of operator levels or subexpression structures in the extracted results.

[0030] Meanwhile, the variable text output cannot be directly used for data quality verification (such as value range rule extraction), query optimization (such as index field identification), security auditing (such as sensitive field access analysis), and other scenarios.

[0031] This application provides a solution that can synchronously identify and merge condition context nodes scattered across multiple clauses such as WHERE, HAVING, JOIN…ON, CASE…WHEN from the entire SQL statement. By recursively parsing the logical operators, comparison operators, and operands (including fields, constants, and subqueries) within each context, it constructs one or more structured condition expression trees that retain complete nested structures and possess type identifiers (type attribute) and original semantic attributes (such as position, data type, and alias).

[0032] This structured output not only fully restores the conditional logic of the original SQL, but can also be directly used as programmatic input to serve advanced application scenarios such as rule engines, lineage analysis, and intelligent optimizers, significantly improving the completeness, accuracy, and usability of conditional logic extraction.

[0033] It should be noted that the executing entity in this embodiment can be a computing service device with data processing, network communication, and program execution functions, such as a tablet computer, personal computer, or mobile phone, or an electronic device capable of performing the above functions. The following description uses a personal computer as an example to illustrate this embodiment and the subsequent embodiments.

[0034] Technical terms used in the embodiments of this application: Abstract Syntax Tree (AST): A tree-like data structure obtained by a syntax parser from an SQL statement, used to represent the syntactic components of the statement and their hierarchical nesting relationships. AST ignores irrelevant details in the original text (such as parentheses and spaces), retaining only nodes with syntactic meaning (such as SELECT clauses, WHERE conditions, column references, operators, etc.), thus providing structured input for procedural analysis. For example, the SQL statement `SELECT name, age FROM users WHERE age>30;` can be parsed into a tree structure containing child nodes such as "query fields," "data source table," and "filter conditions."

[0035] Visitor Pattern: A behavioral design pattern used to define new operational logic for existing data (such as AST node classes) without modifying it. Its core mechanism is: Element: This refers to the various nodes in the AST, such as the WhereClauseContext node; Visitor: This refers to the conditional context locator and conditional expression extractor in this patent. They are operations defined to "access" AST nodes.

[0036] When a visitor traverses the AST, each node invokes the visitor's corresponding method, thereby triggering type-specific processing logic (such as condition extraction). This pattern allows this application to flexibly extend the condition extraction functionality while maintaining the stability of the AST structure.

[0037] Based on this, embodiments of this application provide a method for extracting conditional expressions, referring to... Figure 1 , Figure 1 This is a flowchart illustrating the first embodiment of the condition expression extraction method of this application.

[0038] In this embodiment, the conditional expression extraction method includes steps S10 to S40: Step S10: Obtain the SQL statement; Step S20: Based on the SQL parsing engine and the syntax rule file of the corresponding SQL dialect, the SQL statement is converted into an abstract syntax tree (AST). It should be noted that the SQL dialect syntax rule file refers to a formal grammar description file used to define the SQL syntax structure of a specific database system (such as MySQL, PostgreSQL, Oracle, etc.), such as the .g4 format file supported by ANTLR4. This file contains lexical rules and parser rules, which guide the parser to accurately identify keywords, identifiers, operators, and nested structures in SQL statements.

[0039] In this embodiment, a mature SQL parser (such as ANTLR4) is used to load a syntax rule file (such as MySQLParser.g4) that matches the target database, parsing the input SQL statement string into a structured abstract syntax tree (AST). This AST accurately reflects the syntactic composition of the original SQL in a tree structure: its root node represents the entire query statement, and its child nodes correspond to the projected field list in the SELECT clause, the data source table in the FROM clause, the filtering conditions in the WHERE clause, the grouped filtering conditions in the HAVING clause, the join conditions in the JOIN clause, and the conditional branches in the CASE expression, etc.

[0040] Specifically, elements such as table names, field names, comparison operators, logical combinations, and nested subqueries in the original SQL are all mapped to corresponding syntax node positions in the AST, forming a hierarchical structured representation. For example, for the SQL statement SELECT name, age FROM users WHERE age>30; its AST will have nodes containing the "query field" node (including name and age), the "source table" node (users), and the "filter condition" node (age>30) as direct or indirect child nodes of the root node.

[0041] Through the above steps, standardized and structured parsing of arbitrary SQL dialect statements is achieved, providing a reliable and complete syntactic foundation for subsequent accurate location of conditional context nodes and extraction of structured conditional expressions.

[0042] Step S30: Traverse the AST using a preset visitor pattern to identify and filter at least one conditional context node that satisfies a preset target condition type. It should be noted that the target condition types include filtering conditions, post-grouping filtering, connection conditions, and conditional branches; The conditional context nodes include filtering conditional context nodes, grouped filtering context nodes, connection conditional context nodes, and conditional branching context nodes. The filtering condition context node is the syntax node of the WHERE clause in the corresponding SQL, and its subtree contains the complete condition expression for filtering basic data (such as age>18). The grouped filtering context node is the syntax node corresponding to the HAVING clause. Its subtree usually contains conditional expressions involving aggregate functions (such as SUM(pricepaid)>10000), which are used to perform secondary filtering on the results after GROUP BY. The connection condition context node is the syntax node of the ON clause in the corresponding JOIN statement. Its subtree represents the association conditions between tables (such as sales.eventid = event.eventid), which is the key basis for constructing the relationship between tables. The conditional branch context is the syntax node of the WHEN clause in the corresponding CASE expression. Each WHEN clause contains an independent conditional expression. The locator needs to traverse all such nodes to extract all branch conditions.

[0043] It is worth noting that due to the extensions or differences in SQL standards among different database systems (for example, MySQL uses the LIMIT clause to truncate results, while Oracle relies on ROWNUM; PostgreSQL supports array and JSON operators, while SQL Server uses the TOP keyword), conditional expressions exhibit diversity in syntax structure. If hard-coded rules or regular expression matching methods are used, they are very easy to be missed or mis-parsed due to dialect differences.

[0044] Therefore, by executing step S30, the abstract syntax tree is traversed through the visitor pattern to identify the syntax nodes of clauses such as filtering, grouping and filtering, and association. This avoids the strong dependence on specific SQL dialects and the incompleteness and fragility caused by text pattern matching, thereby achieving stable and complete unified extraction of multi-clause conditional contexts across database dialects.

[0045] like Figure 2 As shown, Figure 2 This is a flowchart illustrating the recursive traversal of an AST based on the visitor pattern. In this embodiment, the visitor pattern enables a depth-first traversal of each node in the AST, and dynamically identifies and captures context nodes that meet preset target conditions without modifying the AST structure, thereby efficiently completing the task of locating the conditional context.

[0046] Furthermore, step S30 also includes steps S31 to S34: Step S31: Traverse each AST node in the AST using a preset visitor pattern; In an Abstract Syntax Tree (AST), conditional expressions are not directly presented as text, but are encapsulated within specific syntax nodes. The core task of the conditional context locator is to accurately locate these nodes that carry the conditional logic within the AST.

[0047] Specifically, in this embodiment, the visitor pattern uses a depth-first traversal strategy to visit each node in the AST sequentially and trigger the processing method corresponding to the node type, thereby achieving a systematic scan of the entire tree.

[0048] Step S32: During the traversal, determine whether the node type of the current AST node matches any type in the preset target condition context type; In this embodiment, the locator internally maintains a list of target node types for matching syntax nodes in the AST that contain conditional expressions. This list explicitly covers the aforementioned four types of context nodes: filtering conditions, post-grouping filtering, connection conditions, and conditional branches.

[0049] Step S33: If a match is found, the AST node is used as a conditional context node, and the node information corresponding to the conditional context node is recorded. In this embodiment, the recorded node information includes the node's reference pointer in the AST, its start and end positions in the original SQL statement, the corresponding original text fragment, and contextual attributes (such as whether it is in NOT negation logic), providing a complete input basis for subsequent recursive parsing.

[0050] Step S34: Continue traversing the next AST node until the complete traversal of the abstract syntax tree is completed, and output the list of conditional context nodes.

[0051] In this embodiment, the traversal process covers all levels of the AST, ensuring that the conditional contexts in nested structures (such as the WHERE clause within a subquery) can also be accurately identified. The final output list of conditional context nodes serves as a unified entry set for subsequent "recursive parsing and structured tree construction of conditional expressions".

[0052] Through the above steps, this application can automatically, completely, and across dialects extract all conditional context nodes scattered across multiple clauses from a single SQL statement, effectively solving the problem of fragmented conditional logic caused by focusing only on local clauses or relying on textual rules in the prior art, and laying a solid foundation for building a globally consistent and programmable structured business rule view.

[0053] Step S40: Recursively parse each of the condition context nodes to extract the corresponding condition expressions and construct the corresponding structured condition expression tree.

[0054] It should be noted that the conditional expression refers to the logical or comparison operation in an SQL statement used to determine whether data meets specific business rules.

[0055] The structured conditional expression tree refers to a conditional expression represented by a tree-like data structure, wherein: Internal nodes are logical nodes (such as AND, OR) or comparison nodes (such as =, >, IN); Leaf positions consist of identifier nodes (representing fields, such as salary) or constant value nodes (representing literals, such as 5000, 'Beijing'); The subquery is recursively parsed into independent subtrees, which are then embedded as child nodes of the parent node; Each node carries a type attribute to identify its type and records metadata such as the original text, its position in the SQL, data type, alias, and whether it is in negation logic.

[0056] Understandably, existing technologies typically treat conditional expressions as indivisible flat text fragments, failing to recognize their internal operator hierarchy, operand types, and nesting relationships. This makes them difficult to process programmatically in scenarios such as data governance, query optimization, or security auditing. Therefore, step S40, by deeply parsing the conditional expression and constructing a structured tree structure, no longer treats the subtrees of the conditional context nodes as black-box text blocks. Instead, it recursively parses their internal syntactic units and organizes them into a hierarchical tree structure based on their operations and nesting relationships. This fully preserves the semantics and structure of the original conditions, generating a standardized conditional representation that is computable, traversable, and reconstructable. This provides high-quality structured input for downstream tasks such as rule extraction, sensitive field identification, and index suggestion generation.

[0057] In one feasible implementation, step S40 may include steps S41 to S43: Step S41: Taking the abstract syntax subtree corresponding to the conditional context node as the parsing starting point, the abstract syntax subtree is recursively parsed using a preset visitor pattern; In this embodiment, the Visitor pattern provided by ANTLR4 is used to traverse the AST subtree corresponding to the conditional clause. This pattern achieves depth-first traversal of the tree structure by defining a corresponding visit conditional expression extraction() method for each type of syntax node. For example, when a logical operator node (such as AND, OR) or a comparison expression node (such as equal to, greater than, IN) is accessed, the system will automatically call the corresponding processing function, thereby supporting the layer-by-layer parsing of complex nested structures.

[0058] Step S42: During the recursive parsing process, each node of the abstract syntax subtree is accessed, and different operations are performed for different types of nodes to obtain multiple nodes with the type attribute, wherein the multiple nodes with the type attribute together represent the conditional expression; It should be noted that the different types of nodes include logical operators (such as AND, OR), comparison operators (such as =, >, <, LIKE, IN) and operands, wherein the operands include fields (such as salary), constant values ​​(such as 5000) and subqueries (such as id IN (SELECT…)).

[0059] Furthermore, step S42 may also include steps S421 to S425: Step S421: When the logical operator is accessed, create the corresponding logical node. Specifically, when a logical operator (such as AND or OR) is accessed through the visitor pattern, a new logical node is created in the conditional expression tree as the parent node.

[0060] In this embodiment, the type attribute of the logical node is set to "and" or "or", its value attribute records the original operator text (such as "AND"), and a children list is reserved for subsequent mounting of left and right operand subtrees.

[0061] Step S422: When the comparison operator is accessed, a corresponding comparison node is created; In this embodiment, when a visitor recognizes a comparison operator, a comparison node is created. Its type attribute is set to standard identifiers such as eq (=), neq (<>), gt (>), lt (<), gte (>=), lte (<=), like, in, etc., according to the operator type. The value attribute retains the original symbol (such as "="). At the same time, the children list is initialized to accommodate the left operand (usually a field) and the right operand (which may be a constant, function, or subquery).

[0062] Step S423: When the field is accessed, an identifier node is created; In this embodiment, fields (such as salary, user.name) are parsed as identifier nodes, with their type attribute set to "column" and value attribute recording the complete field name (including table aliases, such as "t1.salary"). Additionally, the data type of the field in the database (such as INT, VARCHAR(50)), whether an alias is used, and the start and end positions in the original SQL are recorded for subsequent type validation or sensitive field identification.

[0063] Step S424: When the constant value is accessed, a constant value node is created; In this embodiment, constant values ​​(such as the number 5000, the string 'Beijing', the boolean value TRUE, and the date DATE '2025-01-01') are converted into constant value nodes, with their type attribute set to "constant" and their value attribute storing their literal values. At the same time, their SQL data types (such as INTEGER, CHAR, BOOLEAN, DATE) are inferred and recorded to ensure type safety and provide a basis for extracting data quality rules.

[0064] Step S425: When the subquery is accessed, a new condition expression extraction process is started to obtain an independent condition expression tree.

[0065] In this embodiment, when a visitor encounters a subquery expression (such as WHERE id IN (SELECT id FROM...)), the system will recursively call the entire condition expression extraction process (i.e., repeatedly execute steps S10 to S40), independently parse the SQL statement inside the subquery, and generate a complete sub-condition expression tree; this subtree is then encapsulated into a node of type "subquery" and associated as a right operand with its parent comparison node (such as the IN node), thereby achieving complete preservation of nested logic.

[0066] In addition to type and value, each node also contains the following extended attributes to fully preserve the semantic information of the original SQL conditions and support subsequent automated processing: Original Text: Records the text fragment corresponding to this node in the original SQL statement, used for debugging, log tracing, and bidirectional mapping with the source code; Position information: including startOffset and endOffset, which accurately identifies the range of the node in the original SQL string in the form of a character index, facilitating syntax highlighting, error location, or incremental parsing; Data type (dataType): For identifier nodes (fields) and constant value nodes, record their SQL data type in the database, providing a basis for data quality rule verification (such as value range checking) and type compatibility analysis; Alias ​​information: When a field uses an alias in SQL (such as SELECT name AS user_name FROM ...), the alias is recorded in the corresponding identifier node to ensure that semantic parsing is not distorted by the alias; Negated flag: Used to indicate whether the node is in a logical negation context (e.g., NOT (age>18)). It is explicitly marked by a boolean value (true / false) to avoid introducing additional NOT nodes while accurately restoring the scope of negation.

[0067] Through the above steps, the system can accurately identify and type-model various syntactic elements in the AST subtree, laying the foundation for building a structurally complete and semantically lossless conditional expression tree.

[0068] Step S43: Construct a structured conditional expression tree based on the plurality of nodes with the type attribute.

[0069] Understandably, existing technologies typically treat conditional expressions as indivisible flat strings, making it impossible to distinguish the hierarchical relationship between operators and operands, and also making it difficult to handle nested logic or subqueries. This leads to reliance on fragile regular expression matching or manual intervention in scenarios such as rule extraction and sensitive field analysis. Therefore, by executing step S43, the parent-child relationship and left-right order between operators and operands are explicitly modeled, avoiding the loss of conditional semantics and structural distortion. This achieves lossless structural restoration of the original SQL conditional logic, generating a standardized tree representation that can be programmatically traversed, semantically reconstructed, and type-validated.

[0070] Furthermore, step S43 may also include steps S431 to S435: Step S431: For each logical node, recursively process the conditional expressions corresponding to the left operand and the right operand of the logical node, and use the processing results as the left child node and right child node of the logical node, respectively. In this embodiment, when a logical node generated by a logical operator (such as AND or OR) is encountered, the system recursively performs a complete conditional expression parsing process on its left and right operand subtrees, and the results are used as the left and right child nodes of the logical node, respectively. The order of the child nodes strictly follows the writing order in the original SQL to ensure the accuracy of the logical evaluation semantics.

[0071] Step S432: For each comparison node, process the left operand and right operand of the comparison node respectively, and use the processing result as the left child node and right child node of the comparison node; In this embodiment, the left operand of a comparison node (e.g., generated by operators such as =, >, IN) is typically a field (e.g., salary), while the right operand may be a constant value (e.g., 5000), a function call (e.g., UPPER(name)), or a subquery (e.g., (SELECT id FROM users)). The system performs type identification and structured parsing on both the left and right operands, and organizes the parsing results sequentially into the left and right child nodes of the comparison node, thus fully preserving the semantic structure of the comparison relationship.

[0072] Step S433: Incorporate the identifier node and the constant value node as operand nodes into the structured conditional expression tree; In this embodiment, identifier nodes (representing database fields) and constant value nodes (representing literals) are directly embedded in the tree as leaf-level operands, without recursive expansion. Each node carries attributes such as type, value, dataType, originalText, and position information, providing a structured data foundation for subsequent type validation and rule extraction.

[0073] Step S434: Incorporate the independent condition expression tree generated by the subquery as a child node into the corresponding comparison node or logical node; In this embodiment, when the operand is a subquery, the system will recursively start a complete condition expression extraction process (i.e., repeatedly execute steps S10 to S40), independently parse the SQL statement inside the subquery, and generate a complete sub-condition expression tree; then, the entire subtree is mounted as the right operand (or the operand at the corresponding position) to its parent comparison node or logical node, thereby realizing the structured preservation of conditions with arbitrary depth of nesting.

[0074] Step S435: Based on the logical nodes, comparison nodes, identifier nodes, constant value nodes, and independent conditional expression trees, assemble the final structured conditional expression tree.

[0075] In this embodiment, through a bottom-up recursive assembly mechanism, all local parsing results are combined into a complete tree structure rooted at the top-level logic or comparison node. This tree strictly reflects the operation priority, core nature, and nesting relationship of the original SQL conditions, and each node carries complete type and semantic attributes, forming a standardized condition representation that can be processed programmatically.

[0076] Furthermore, in a structured conditional expression tree, whether the child nodes of a basic attribute need to explicitly distinguish between left and right structures (e.g., by labeling "left expression" and "right expression" with fields), or implicitly represent the left and right relationships by array order by default, requires a comprehensive decision based on factors such as implementation simplicity, data redundancy, and downstream processing efficiency.

[0077] In this embodiment, an array-based implicit distinction between left and right operands is adopted: the default order implicitly distinguishes left and right operands (the current scheme), and the left and right operands are implicitly defined by the order of the arrays in the child nodes, with the first child node being the left operand and the second being the right operand. This method requires no additional fields, reduces data redundancy, and conforms to common abstract syntax tree design conventions (such as SQL parsers typically using array order to represent operands). This allows developers like Kezhijie to access left and right operands through array indices without additional logical judgments.

[0078] Of course, in other feasible implementations, an explicit marking strategy can also be adopted, such as adding a position attribute to each child node to enhance the readability of the structure. However, this approach increases storage overhead and serialization complexity, and its benefits are limited in binary operation scenarios. Therefore, this application preferably adopts an implicit left-right distinction mechanism based on array order, achieving the best balance between structural compactness and processing efficiency while ensuring semantic accuracy.

[0079] Through the above steps, this application achieves lossless structured modeling of complex SQL conditional expressions. It not only fully preserves the operator hierarchy, operand types, and subquery nesting relationships, but also ensures semantic traceability and type security through a unified node attribute system. This provides high-fidelity, computable structured input for downstream tasks such as data governance, security auditing, and intelligent optimization, effectively overcoming the vulnerability and incompleteness of traditional text matching methods when facing complex SQL.

[0080] The above-described method obtains an SQL statement; based on the SQL parsing engine and the corresponding SQL dialect's syntax rule file, the SQL statement is converted into an Abstract Syntax Tree (AST); the AST is traversed using a preset visitor pattern to identify and filter at least one conditional context node that satisfies a preset target condition type; each conditional context node is recursively parsed to extract the corresponding conditional expression, and a corresponding structured conditional expression tree is constructed. This solution, by using the visitor pattern to traverse the AST to identify multiple conditional context nodes, can completely extract conditional logic from the SQL statement; simultaneously, by recursively parsing each conditional context node and constructing a structured conditional expression tree, the extracted result has a tree-like representation that preserves the original nested structure and semantics.

[0081] For example, to help understand the implementation flow of the conditional expression extraction method obtained by combining this embodiment with the above embodiment one, please refer to... Figure 3 , Figure 3 A simplified flowchart of a conditional expression extraction method is provided, specifically: First, the system receives the raw SQL statement string to be processed. Then, the SQL parser, built on ANTLR4, loads the grammar rule file corresponding to the database dialect, performs lexical and syntactic analysis on the input SQL statement, and generates the raw abstract syntax tree.

[0082] Next, the SQL statement is structured into a hierarchical AST, which contains the various components of the query statement, such as the SELECT clause, FROM clause, WHERE clause, etc., and each node carries its type and text information.

[0083] Subsequently, the AST is traversed using the visitor pattern to identify and locate nodes that meet the preset target condition types, including filtering conditions (WHERE), grouping filtering (HAVING), connection conditions (ON), and conditional branching (CASE WHEN), and the corresponding conditional context node list is obtained.

[0084] For each condition context node's corresponding AST subtree, a recursive parsing method is used to extract the condition expressions layer by layer, and a structured condition expression tree is constructed. This process includes: Traverse the logical operators, comparison operators, fields, constant values, and subqueries in the subtree; then, create structured nodes with a type attribute for different types of nodes, and assemble them into a tree structure with operators as nodes and operands as children according to operator precedence and nesting relationship; in addition, for subqueries, recursively start a new extraction process, generate independent subtrees and attach them to the parent node.

[0085] Finally, the generated structured conditional representation tree is output in JSON or XML format for use by downstream systems.

[0086] Among them, Figure 3 The example of a conditional expression tree structure provided below shows the structured parsing result of the condition `salary>5000AND (dept = 'IT' OR bonus>1000)`: The root node is a logical operator, its left child is a comparison node (>), and its right child is an OR node; The left operand of the node is the field salary (type: column), and the right operand is the constant 5000 (type: integer). The left child of the OR node is the = node (left: dept, right: 'IT'), and the right child is the > node (left: bonus, right: 1000). All nodes carry attributes such as type, value, and children, fully preserving the original logic and semantic information.

[0087] Through the above process, this application realizes end-to-end automatic conversion from raw SQL statements to structured conditional expression trees. It not only solves the problems of "condition fragmentation" and "semantic loss" in traditional methods, but also supports the accurate restoration of complex nested logic, providing a high-quality structured input foundation for scenarios such as data governance, security auditing, and intelligent optimization.

[0088] It should be noted that the above examples are only for understanding this application and do not constitute a limitation on the method for extracting conditional expressions in this application. Any simple transformations based on this technical concept are within the protection scope of this application.

[0089] This application also provides a conditional expression extraction system; please refer to [reference needed]. Figure 4 The conditional expression extraction system includes: Module 10 is used to retrieve SQL statements; The conversion module 20 is used to convert the SQL statement into an abstract syntax tree (AST) based on the SQL parsing engine and the syntax rule file of the corresponding SQL dialect. Traversal module 30 is used to traverse the AST using a preset visitor pattern, and identify and filter at least one conditional context node that satisfies a preset target condition type. The extraction module 40 is used to recursively parse each of the condition context nodes, extract the corresponding condition expression, and construct the corresponding structured condition expression tree.

[0090] The conditional expression extraction system provided in this application, employing the conditional expression extraction method described in the above embodiments, can solve the technical problem of how to extract complete and structured conditional expressions from SQL statements. Compared with the prior art, the beneficial effects of the conditional expression extraction system provided in this application are the same as those of the conditional expression extraction method provided in the above embodiments, and other technical features of the conditional expression extraction system are the same as those disclosed in the methods of the above embodiments, and will not be repeated here.

[0091] This application provides a condition expression extraction device, which 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, which are executed by the at least one processor to enable the at least one processor to perform the condition expression extraction method in Embodiment 1 above.

[0092] The following is for reference. Figure 5 The diagram illustrates a structural schematic suitable for implementing the condition expression extraction device in the embodiments of this application. The condition expression extraction device in the embodiments of this application may include, but is not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Description), PMPs (Portable Media Players), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 5 The conditional expression extraction device shown is merely an example and should not impose any limitations on the functionality and scope of use of the embodiments of this application.

[0093] like Figure 5As shown, the condition expression extraction device may include a processing unit 1001 (e.g., a central processing unit, a graphics processing unit, etc.) that can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 1002 or a program loaded from a storage device 1003 into a random access memory (RAM) 1004. The RAM 1004 also stores various programs and data required for the operation of the condition expression extraction device. The processing unit 1001, ROM 1002, and RAM 1004 are interconnected via a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Typically, the following systems can be connected to the I / O interface 1006: input devices 1007 including, for example, a touchscreen, touchpad, keyboard, mouse, image sensor, microphone, accelerometer, gyroscope, etc.; output devices 1008 including, for example, a liquid crystal display (LCD), speaker, vibrator, etc.; storage devices 1003 including, for example, magnetic tape, hard disk, etc.; and communication devices 1009. The communication device 1009 allows the condition expression extraction device to communicate wirelessly or wiredly with other devices to exchange data. Although the figure shows condition expression extraction devices with various systems, it should be understood that it is not required to implement or possess all of the systems shown. More or fewer systems may be implemented alternatively.

[0094] Specifically, according to the embodiments disclosed in this application, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments disclosed in this application include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device, or installed from storage device 1003, or installed from ROM 1002. When the computer program is executed by processing device 1001, it performs the functions defined in the methods of the embodiments disclosed in this application.

[0095] The conditional expression extraction device provided in this application, employing the conditional expression extraction method in the above embodiments, can solve the technical problem of how to extract complete and structured conditional expressions from SQL statements. Compared with the prior art, the beneficial effects of the conditional expression extraction device provided in this application are the same as those of the conditional expression extraction method provided in the above embodiments, and other technical features of this conditional expression extraction device are the same as those disclosed in the previous embodiment method, and will not be repeated here.

[0096] It should be understood that the various parts disclosed in this application can be implemented using hardware, software, firmware, or a combination thereof. In the description of the above embodiments, specific features, structures, materials, or characteristics can be combined in a suitable manner in any at least one embodiment or example.

[0097] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

[0098] This application provides a computer-readable storage medium having computer-readable program instructions (i.e., a computer program) stored thereon, the computer-readable program instructions being used to execute the conditional expression extraction method in the above embodiments.

[0099] The computer-readable storage medium provided in this application may be, for example, a USB flash drive, but is not limited to, electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems or devices, or any combination thereof. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection having at least one wire, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this embodiment, the computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system or device. The program code contained on the computer-readable storage medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (Radio Frequency), etc., or any suitable combination thereof.

[0100] The aforementioned computer-readable storage medium may be included in the conditional expression extraction device; or it may exist independently and not be assembled into the conditional expression extraction device.

[0101] The aforementioned computer-readable storage medium carries one or more programs. When the aforementioned one or more programs are executed by the condition expression extraction device, the condition expression extraction device causes the following: to obtain an SQL statement; to convert the SQL statement into an abstract syntax tree (AST) based on an SQL parsing engine and a syntax rule file of the corresponding SQL dialect; to traverse the AST using a preset visitor pattern, identify and filter at least one condition context node that satisfies a preset target condition type; to recursively parse each condition context node, extract the corresponding condition expression, and construct the corresponding structured condition expression tree.

[0102] Computer program code for performing the operations of this application can be written in one or more programming languages ​​or a combination thereof, including object-oriented programming languages ​​such as Java, Smalltalk, and C++, and conventional procedural programming languages ​​such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a Local Area Network (LAN) or a Wide Area Network (WAN)—or can be connected to an external computer (e.g., via the Internet using an Internet service provider).

[0103] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing at least one executable instruction for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.

[0104] The modules described in the embodiments of this application can be implemented in software or hardware. The names of the modules do not necessarily limit the functionality of the unit itself.

[0105] The readable storage medium provided in this application is a computer-readable storage medium that stores computer-readable program instructions (i.e., a computer program) for executing the above-described conditional expression extraction method, thereby solving the technical problem of how to extract complete and structured conditional expressions from SQL statements. Compared with the prior art, the beneficial effects of the computer-readable storage medium provided in this application are the same as those of the conditional expression extraction method provided in the above embodiments, and will not be repeated here.

[0106] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the conditional expression extraction method described above.

[0107] The computer program product provided in this application solves the technical problem of how to extract complete and structured conditional expressions from SQL statements. Compared with the prior art, the beneficial effects of the computer program product provided in this application are the same as those of the conditional expression extraction method provided in the above embodiments, and will not be repeated here.

[0108] The above description is only a part of the embodiments of this application and does not limit the patent scope of this application. All equivalent structural transformations made under the technical concept of this application and using the contents of the specification and drawings of this application, or direct / indirect applications in other related technical fields, are included in the patent protection scope of this application.

Claims

1. A method for extracting conditional expressions, characterized in that, The conditional expression extraction method includes: Retrieve the SQL statement; Based on the SQL parsing engine and the corresponding SQL dialect syntax rule file, the SQL statement is converted into an abstract syntax tree (AST). The AST is traversed using a preset visitor pattern to identify and filter at least one conditional context node that meets the preset target condition type. Each condition context node is recursively parsed to extract the corresponding condition expression, and a corresponding structured condition expression tree is constructed.

2. The conditional expression extraction method as described in claim 1, characterized in that, The step of traversing the AST using a preset visitor pattern to identify and filter at least one conditional context node that satisfies a preset target condition type includes: The AST nodes in the AST are traversed using a preset visitor pattern. During the traversal, determine whether the node type of the current AST node matches any of the preset target condition context types; If a match is found, the AST node is used as a condition context node, and the node information corresponding to the condition context node is recorded. Continue traversing the next AST node until a complete traversal of the abstract syntax tree is completed, and output the list of conditional context nodes.

3. The conditional expression extraction method as described in claim 2, characterized in that, The preset target condition context type includes any one of the following: filter condition, grouped filter condition, connection condition, and conditional branch; The conditional context nodes include filter conditional context nodes and grouped filter context nodes. Connect either the conditional context node or the conditional branch context node.

4. The conditional expression extraction method as described in claim 1, characterized in that, The steps of recursively parsing each condition context node, extracting the corresponding condition expression, and constructing the corresponding structured condition expression tree include: Starting from the abstract syntax subtree corresponding to the conditional context node, the abstract syntax subtree is recursively parsed using a preset visitor pattern; During the recursive parsing process, each node of the abstract syntax subtree is visited, and different operations are performed on different types of nodes to obtain multiple nodes with the type attribute, wherein the multiple nodes with the type attribute together represent the conditional expression; Based on the multiple nodes with the type attribute, construct a structured conditional expression tree.

5. The conditional expression extraction method as described in claim 4, characterized in that, The different types of nodes include logical operators, comparison operators, and operands, where operands include fields, constant values, and subqueries. The steps of performing different operations on different types of nodes to obtain multiple nodes with a type attribute include: When the logical operator is accessed, the corresponding logical node is created; When the comparison operator is accessed, a corresponding comparison node is created; When the field is accessed, an identifier node is created; When the constant value is accessed, a constant value node is created; When the subquery is accessed, a new condition expression extraction process is initiated to obtain an independent condition expression tree.

6. The conditional expression extraction method as described in claim 5, characterized in that, The step of constructing a structured conditional expression tree based on the plurality of nodes with the type attribute includes: For each logical node, the conditional expressions corresponding to the left operand and the right operand of the logical node are processed recursively, and the processing results are used as the left child node and the right child node of the logical node, respectively. For each comparison node, process the left and right operands of the comparison node respectively, and use the processing results as the left and right child nodes of the comparison node; The identifier node and the constant value node are incorporated as operand nodes into the structured conditional expression tree; The independent condition expression tree generated by the subquery is incorporated as a child node into the corresponding comparison node or logical node; The final structured conditional expression tree is assembled based on the logical nodes, comparison nodes, identifier nodes, constant value nodes, and independent conditional expression trees.

7. A conditional expression extraction system, characterized in that, The conditional expression extraction system includes: The retrieval module is used to retrieve SQL statements; The conversion module is used to convert the SQL statement into an abstract syntax tree (AST) based on the SQL parsing engine and the corresponding SQL dialect syntax rule file. The traversal module is used to traverse the AST using a preset visitor pattern, identify and filter at least one conditional context node that satisfies a preset target condition type. The extraction module is used to recursively parse each of the condition context nodes, extract the corresponding condition expressions, and construct the corresponding structured condition expression tree.

8. A conditional expression extraction device, characterized in that, The device includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, the computer program being configured to implement the steps of the conditional expression extraction method as described in any one of claims 1 to 6.

9. A storage medium, characterized in that, The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, it implements the steps of the conditional expression extraction method as described in any one of claims 1 to 6.

10. A computer program product, characterized in that, The computer program product includes a computer program that, when executed by a processor, implements the steps of the conditional expression extraction method as described in any one of claims 1 to 6.