A predicate expression tree parsing method, device and medium
By judging and optimizing the predicate expression tree subtree value, the problem that the existing ORM framework cannot be applied to multiple database development is solved, dynamic SQL parsing and cross-database operation is realized, and development costs are reduced.
Patent Information
- Application Number
- CN202011026967.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-09-25
- Publication Date
- 2025-02-25
- Estimated Expiration
- 2040-09-25
AI Technical Summary
The existing ORM framework cannot be fully applicable to the development needs of multiple sets of database products, resulting in increased development costs.
By determining whether each subtree in the predicate expression tree can further obtain values and optimize the expression tree based on the subtree that can further obtain values, the optimized expression tree is finally parsed into the target code.
It implements dynamic parsing of the predicate expression tree into SQL statements, reducing development costs and supporting cross-database operation.
Smart Images

Figure CN112230928B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing technology, and in particular to a predicate expression tree parsing method, device and medium. Background Art
[0002] EntityFramework is an ORM (Object Relational Mapping) framework built into the .NET Framework. It has an unparalleled natural advantage over LINQ (Language Integrated Query) syntax and expression tree parsing algorithms. Developers do not need to write entity classes themselves, and even provide a visual data relationship diagram in VisualStudio. It has extremely strong code readability, but its overly heavy architecture is not conducive to promotion within multiple small projects of many enterprise application cloud platforms.
[0003] Dapper (Distributed and Parallel Program Execution Runtime) is an open source lightweight ORM framework that uses Emit to reflect the sequence queue of IDataReader to quickly obtain and generate objects. Its operating performance is almost equivalent to handwritten SQL (Structured Query Language), but its shortcomings are also particularly obvious. Its native state does not support LINQ syntax or expression tree parsing.
[0004] At present, in the process of developing multiple projects of multi-enterprise application cloud platforms, enterprises mostly use handwritten SQL statements for database development, and the existing ORM framework is not fully suitable for the development of these projects. This requires manual coding for multiple databases every time a platform and product is released, which greatly increases development costs. Summary of the invention
[0005] The present invention aims to solve the following technical problems at least to a certain extent.
[0006] The existing ORM framework cannot fully meet the development requirements of multiple database products.
[0007] A first aspect of an embodiment of the present invention provides a method for parsing a predicate expression tree, comprising:
[0008] Determine whether each subtree in the predicate expression tree can be further evaluated;
[0009] Optimizing the predicate expression tree based on a subtree that can further take values;
[0010] The optimized predicate expression tree is parsed into target code.
[0011] In some examples, determining whether each subtree in the predicate expression tree can be further evaluated includes:
[0012] Traversing each subtree of the predicate expression tree;
[0013] Based on the constants in each subtree, each subtree is judged respectively to determine the subtree that can be further valued.
[0014] In some examples, the step of judging each subtree in the predicate expression tree to determine the subtree that can be further evaluated includes:
[0015] Judge each subtree in order from bottom to top;
[0016] If it is determined that a subtree cannot be further valued, then the parent tree corresponding to the subtree cannot be further valued;
[0017] According to the result of the judgment, the subtree that can be further valued is determined.
[0018] In some examples, the optimizing the predicate expression tree based on the subtree that can further take values includes:
[0019] Incorporating the subtree that can be further valued into a set;
[0020] Traversing the subtrees of the predicate expression tree and determining whether each subtree exists in the set;
[0021] If so, the predicate expression tree is optimized according to the evaluation results of the subtree that can further evaluate.
[0022] In some examples, the optimizing the predicate expression tree according to the evaluation result of the subtree capable of further evaluating the value includes:
[0023] Convert the subtree that can further take values into a parameterless method expression tree;
[0024] Compile the method expression tree and perform dynamic call to obtain a value, and determine the result of obtaining a subtree that can be further evaluated;
[0025] After all subtrees capable of further obtaining values have been obtained, the predicate expression tree is optimized according to the obtained results.
[0026] In some examples, the optimizing the predicate expression tree based on the subtree that can further take values includes:
[0027] Cache the subtree that can be further valued;
[0028] Determine the value results of the subtrees that can be further valued from the back to the front in the order of the subtree cache;
[0029] The predicate expression tree is optimized in real time based on the evaluation result until all subtrees in the set are evaluated.
[0030] In some examples, parsing the optimized predicate expression tree into target code includes:
[0031] According to the type of the subtree in the optimized predicate expression tree, the subtree of the corresponding type is parsed into a corresponding database statement and variable parameter set.
[0032] In some examples, the step of parsing the subtree into a corresponding database statement and a set of binding variables according to the type of the subtree in the optimized predicate expression tree includes:
[0033] If the type of the subtree is a constant expression tree, parsing it into a database-bound variable placeholder and a variable value, wherein the database-bound variable placeholder belongs to the database statement and the variable value belongs to the variable parameter set; or
[0034] If the type of the subtree is a member expression tree, parsing it into a data column name, wherein the data column name belongs to the database statement; or
[0035] If the type of the subtree is a conditional expression tree, parsing it into a conditional judgment result expression, wherein the conditional judgment result expression belongs to the database statement; or
[0036] If the type of the subtree is a unary expression tree, parsing it into a yes / no conditional statement, wherein the yes / no conditional statement belongs to the database statement; or
[0037] If the type of the subtree is a binary expression tree, it is parsed into a comparison statement / conditional connection statement / arithmetic operation statement, wherein the comparison statement / conditional connection statement / arithmetic operation statement belongs to the database statement; or
[0038] If the type of the subtree is a method expression tree, it is parsed as a condition / value / date conversion statement, wherein the condition / value / date conversion statement belongs to the database statement.
[0039] A second aspect of an embodiment of the present invention provides a predicate expression tree parsing device, comprising:
[0040] processor, and
[0041] a memory communicatively connected to the processor, wherein:
[0042] The memory stores computer instructions that can be executed by the processor, and the instructions are executed by the processor to perform the following steps:
[0043] Determine whether each subtree in the predicate expression tree can be further evaluated;
[0044] Optimizing the predicate expression tree based on a subtree that can further take values;
[0045] The optimized predicate expression tree is parsed into target code.
[0046] A third aspect of an embodiment of the present invention provides a non-volatile computer storage medium for parsing a predicate expression tree, storing computer instructions, wherein the instructions are configured to:
[0047] Determine whether each subtree in the predicate expression tree can be further evaluated;
[0048] Optimizing the predicate expression tree based on a subtree that can further take values;
[0049] The optimized predicate expression tree is parsed into target code. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0051] Figure 1 A schematic diagram of the structure of a predicate expression tree provided for an exemplary embodiment of the present invention;
[0052] Figure 2 A schematic diagram of the overall logical framework of a predicate expression tree parsing method provided for an exemplary embodiment of the present invention;
[0053] Figure 3 A flowchart of a predicate expression tree parsing method provided for an exemplary embodiment of the present invention;
[0054] Figure 4 A logic diagram of a constant set construction algorithm in a predicate expression tree parsing method provided for an exemplary embodiment of the present invention;
[0055] Figure 5 A logic diagram of a constant set value algorithm in a predicate expression tree parsing method provided for an exemplary embodiment of the present invention;
[0056] Figure 6 A logic diagram of a conditional statement translation algorithm in a predicate expression tree parsing method provided for an exemplary embodiment of the present invention;
[0057] Figure 7 A schematic diagram of the framework structure of a device corresponding to a predicate expression tree parsing method provided for an exemplary embodiment of the present invention. DETAILED DESCRIPTION
[0058] In order to more clearly illustrate the overall concept of the present application, a detailed description is given below in an illustrative manner in conjunction with the accompanying drawings.
[0059] Before introducing the specific embodiments of the present invention, the terms appearing in the embodiments of the present invention are first introduced.
[0060] An expression tree is a data structure that is used to store functions that need to be calculated and operated, but does not perform actual calculations. A predicate expression tree is a data structure that stores functions that return true values.
[0061] From bottom to top means according to Figure 1 The arrangement of expression trees shown is from the bottom to the top of the expression tree.
[0062] In an object-oriented ORM framework, the predicate expression tree is the best object-oriented representation of conditional statements. A good predicate expression tree parsing algorithm is the basis of an excellent object-oriented ORM framework.
[0063] In the actual coding process, in order to simplify the code and improve readability, developers often put some member variables and constants that may have further operations in the predicate expression tree. In the predicate expression tree parsing algorithm, in order to reduce the amount of data exchange between the application server and the database server during the execution of the statement, this part of the data should be optimized and simplified.
[0064] In view of this demand scenario, the embodiment of the present invention provides a method for parsing a predicate expression tree and its corresponding solution. The method of the embodiment of the present invention innovates the traditional handwritten SQL statement string concatenation method in database development work, realizes the dynamic parsing of the predicate expression tree into an SQL statement string, and lays the underlying technical foundation for a localized lightweight object-oriented ORM framework. At the same time, the embodiment of the present invention constructs the predicate expression tree in a completely object-oriented manner and parses it into the corresponding database conditional statement, which can standardize and encapsulate the database grammar rules, and can realize cross-database operation after development is completed.
[0065] Figure 1A schematic diagram of the structure of a predicate expression tree provided for an exemplary embodiment of the present invention, in Figure 1 In the example, "value" indicates a member variable, "1" and "2" indicate constants. Constants can be further assigned values, but member variables cannot be further assigned values because they need to be queried from the database.
[0066] Figure 1 Several types of expression trees are given as examples. Generally speaking, the types of expression trees include but are not limited to:
[0067] Constant expression tree (English: ConstantExpression);
[0068] Member expression tree (English: MemberExpression);
[0069] Conditional Expression Tree (English: ConditionalExpression);
[0070] Unary expression tree (English: UnaryExpression);
[0071] Binary expression tree (English: BinaryExpression);
[0072] Method expression tree (English: MethodCallExpression).
[0073] Figure 2 A schematic diagram of the overall logical framework of a predicate expression tree parsing method provided for an exemplary embodiment of the present invention is shown in FIG. Figure 2 As shown, the embodiment of the present invention is implemented by three algorithms, namely, a constant set construction algorithm, a constant set value algorithm, and a conditional statement translation algorithm; after the predicate expression tree is optimized by these three algorithms, a target code is obtained. In some embodiments of the present invention, the target code is an executable conditional statement.
[0074] Figure 3 A flowchart of a predicate expression tree parsing method provided for an exemplary embodiment of the present invention is provided. Figure 2 The three algorithms shown in the figure process the predicate expression tree in turn. The specific processing process is as follows: Figure 3 As shown, the method includes:
[0075] Step 301: Determine whether each subtree in the predicate expression tree can be further evaluated;
[0076] This step is mainly implemented through the constant set construction algorithm.
[0077] Step 302: Optimize the predicate expression tree based on the subtree that can further take values;
[0078] This step is mainly implemented through the constant set value algorithm.
[0079] Step 303: Parse the optimized predicate expression tree into target code.
[0080] This step is mainly achieved through the conditional statement translation algorithm.
[0081] The embodiment of the present invention constructs a predicate expression tree in a completely object-oriented manner, and parses the predicate expression tree into corresponding database executable statements, standardizes and encapsulates database grammar rules, and realizes one-time development and cross-database execution.
[0082] Figure 4 A logical diagram of a constant set construction algorithm in a predicate expression tree parsing method provided for an exemplary embodiment of the present invention, such as Figure 4 As shown, the main implementation process of step 301 includes:
[0083] Traverse all subtrees (sub-expression trees) of the predicate expression tree, and based on the composition of each subtree (mainly constants and member variables), evaluate whether each subtree has the possibility of further value taking.
[0084] In one embodiment of the present invention, the evaluation index refers to whether the subtree is composed entirely of constants.
[0085] If the subtree has the possibility of further value taking, the subtree is saved in the constant expression tree set;
[0086] If the subtree has no possibility of further evaluation, the subtree is marked as unvaluable;
[0087] After a subtree is judged, the judgment method is called again, recursively, and all subtrees in the predicate expression tree are judged, and finally a set of constant expression trees with possible values is obtained.
[0088] In some embodiments of the present invention, in the process of traversing the predicate expression tree, Figure 1 The traversal starts from the root node and goes down to the bottom, and finally reaches the bottom leaf node. During the traversal, each subtree traversed is cached in a local variable.
[0089] In some examples, the traversal functionality is implemented through the ExpressionVisitor provided in the .NET Framework.
[0090] In the process of judging whether each subtree can be further valued, the judgment is made starting from the leaf node and proceeding to the next level.
[0091] Therefore, during the judgment process, if it is found that a subtree cannot be further valued, then all the upper parent trees corresponding to the subtree cannot be valued. At this time, all the upper parent trees corresponding to the subtree are marked by marking, and there is no need to call the corresponding judgment method for judgment by recursion.
[0092] Figure 4 A logical diagram of a constant set construction algorithm in a predicate expression tree parsing method provided for an exemplary embodiment of the present invention, such as Figure 4 As shown, the implementation process of step 302 may be:
[0093] If the subtree that can be further evaluated is not included in the set in the previous algorithm, the subtree that can be further evaluated is first included in the set to obtain a set of constant expression trees that can be evaluated.
[0094] Then re-traverse all the sub-expression trees of the predicate expression tree and determine whether each expression tree exists in the constant expression tree set. If so, convert it into a parameterless method expression tree, then compile the method expression tree and dynamically call the value, and encapsulate the value result into a constant expression tree and return it, finally obtaining a simplified predicate expression tree.
[0095] In some examples, the parameterless method expression tree is a lambda expression tree.
[0096] In some examples, the value result refers to the operation of two constants "1" and "2" through the operator "+", and finally obtaining the value result "3".
[0097] In this implementation, only one operation is required, but a class file must be added during the execution.
[0098] In some other embodiments of the present invention, step 302 may be implemented as follows:
[0099] Cache the subtree that can be further valued;
[0100] Determine the value results of the subtrees that can be further valued from the back to the front in the order of the subtree cache;
[0101] Based on the evaluation result, the predicate expression tree is optimized in real time until all subtrees in the set are evaluated.
[0102] As mentioned earlier, the traversal process is from top to bottom, so the order of subtree caching is also from top to bottom. Therefore, when optimizing the value, the subtrees are optimized from bottom to top. It should be noted that after each subtree is evaluated, the structure of the predicate expression tree must be updated.
[0103] In this implementation, multiple operations may be required, but no new class files need to be added during the execution.
[0104] It is understandable that in the actual operation process, an appropriate method should be selected for implementation according to the structure and requirements of the predicate expression tree, such as selecting one or a combination of the two.
[0105] It should be noted that different traversal methods will affect the specific calculation method when optimizing the predicate expression tree. Therefore, when optimizing the predicate expression tree, it is necessary to use the corresponding traversal method.
[0106] Figure 6 A logical diagram of a conditional statement translation algorithm in a predicate expression tree parsing method provided for an exemplary embodiment of the present invention, such as Figure 6 As shown, the implementation process of step 303 is:
[0107] According to the type of the subtree in the optimized predicate expression tree, the subtree of the corresponding type is parsed into a corresponding database statement and variable parameter set.
[0108] Specifically, through the previous algorithm, an optimized and streamlined predicate expression tree is obtained, and the corresponding type of subtree is parsed into database statements and variable parameter sets. The logic is as follows:
[0109] If the subtree type is a constant expression tree, it is parsed into a database-bound variable placeholder and a variable value, wherein the database-bound variable placeholder belongs to the database statement and the variable value belongs to the variable parameter set; or
[0110] If the subtree type is a member expression tree, it is parsed as a data column name, where the data column name belongs to the database statement; or
[0111] If the subtree type is a conditional expression tree, it is parsed into a conditional judgment result expression, where the conditional judgment result expression belongs to a database statement; or
[0112] If the subtree type is a unary expression tree, it is parsed as a yes / no conditional statement, wherein the yes / no conditional statement belongs to a database statement; or
[0113] If the subtree type is a binary expression tree, it is parsed as a comparison statement / conditional connection statement / arithmetic statement, where the comparison statement / conditional connection statement / arithmetic statement belongs to a database statement; or
[0114] If the subtree type is a method expression tree, it is parsed as a conditional / value / date conversion statement, where the conditional / value / date conversion statement belongs to a database statement.
[0115] Based on the same idea, the embodiment of the present invention also provides a medium and a non-volatile computer storage medium corresponding to the above method.
[0116] Figure 7 A schematic diagram of the framework structure of a device corresponding to the predicate expression tree parsing method provided by an exemplary embodiment of the present invention is shown in FIG. Figure 7 As shown, the second aspect of the embodiment of the present invention provides a parsing device for a predicate expression tree, comprising:
[0117] processor, and
[0118] a memory communicatively connected to the processor, wherein:
[0119] The memory stores computer instructions that can be executed by the processor, and the instructions are executed by the processor to perform the following steps:
[0120] Determine whether each subtree in the predicate expression tree can be further evaluated;
[0121] Optimizing the predicate expression tree based on a subtree that can further take values;
[0122] The optimized predicate expression tree is parsed into target code.
[0123] A third aspect of an embodiment of the present invention provides a non-volatile computer storage medium for parsing a predicate expression tree, storing computer instructions, wherein the instructions are configured to:
[0124] Determine whether each subtree in the predicate expression tree can be further evaluated;
[0125] Optimizing the predicate expression tree based on a subtree that can further take values;
[0126] The optimized predicate expression tree is parsed into target code.
[0127] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0128] The above are only embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the application.
Claims
1. A method for parsing a predicate expression tree, characterized in that: include: Determining whether each subtree in the predicate expression tree can further take a value includes: Traversing each subtree of the predicate expression tree; Based on the constants in each subtree, each subtree is judged respectively to determine the subtree that can be further valued; Judge each subtree in order from bottom to top; If it is determined that a subtree cannot be further valued, then the parent tree corresponding to the subtree cannot be further valued; According to the judgment result, determining the subtree that can be further valued; Optimizing the predicate expression tree based on a subtree that can further take values; Parsing the optimized predicate expression tree into target code includes: according to the type of subtree in the optimized predicate expression tree, parsing the subtree of the corresponding type into corresponding database statements and variable parameter sets.
2. The method according to claim 1, characterized in that The optimizing process of the predicate expression tree based on the subtree capable of further taking values comprises: Incorporating the subtree that can be further valued into a set; Traversing the subtrees of the predicate expression tree and determining whether each subtree exists in the set; If so, the predicate expression tree is optimized according to the evaluation results of the subtree that can further evaluate.
3. The method according to claim 2, characterized in that The optimizing process of the predicate expression tree according to the evaluation result of the subtree capable of further evaluating the value comprises: Convert the subtree that can further take values into a parameterless method expression tree; Compile the method expression tree and perform dynamic call to obtain a value, and determine the result of obtaining a subtree that can be further evaluated; After all subtrees capable of further obtaining values have been obtained, the predicate expression tree is optimized according to the obtained results.
4. The method according to claim 1, characterized in that: The optimizing process of the predicate expression tree based on the subtree capable of further taking values comprises: Cache the subtree that can be further valued; Determine the value results of the subtrees that can be further valued from the back to the front in the order of the subtree cache; The predicate expression tree is optimized in real time based on the evaluation result until all subtrees in the set are evaluated.
5. The method according to claim 1, characterized in that: The method of parsing the subtree into a corresponding database statement and a binding variable set according to the type of the subtree in the optimized predicate expression tree includes: If the type of the subtree is a constant expression tree, parsing it into a database-bound variable placeholder and a variable value, wherein the database-bound variable placeholder belongs to the database statement and the variable value belongs to the variable parameter set; or If the type of the subtree is a member expression tree, parsing it into a data column name, wherein the data column name belongs to the database statement; or If the type of the subtree is a conditional expression tree, parsing it into a conditional judgment result expression, wherein the conditional judgment result expression belongs to the database statement; or If the type of the subtree is a unary expression tree, parsing it into a yes / no conditional statement, wherein the yes / no conditional statement belongs to the database statement; or If the type of the subtree is a binary expression tree, it is parsed into a comparison statement / conditional connection statement / arithmetic operation statement, wherein the comparison statement / conditional connection statement / arithmetic operation statement belongs to the database statement; or If the type of the subtree is a method expression tree, it is parsed as a condition / value / date conversion statement, wherein the condition / value / date conversion statement belongs to the database statement.
6. A predicate expression tree parsing device, characterized in that ,include: processor, and a memory communicatively connected to the processor, wherein: The memory stores computer instructions that can be executed by the processor, and the instructions are executed by the processor to perform the following steps: Determining whether each subtree in the predicate expression tree can further take a value includes: Traversing each subtree of the predicate expression tree; Based on the constants in each subtree, each subtree is judged respectively to determine the subtree that can be further valued; Judge each subtree in order from bottom to top; If it is determined that a subtree cannot be further valued, then the parent tree corresponding to the subtree cannot be further valued; According to the judgment result, determining the subtree that can be further valued; Optimizing the predicate expression tree based on a subtree that can further take values; Parsing the optimized predicate expression tree into target code includes: according to the type of subtree in the optimized predicate expression tree, parsing the subtree of the corresponding type into corresponding database statements and variable parameter sets.
7. A non-volatile computer storage medium for parsing a predicate expression tree, storing computer instructions, characterized in that: The instructions are set to: Determining whether each subtree in the predicate expression tree can further take a value includes: Traversing each subtree of the predicate expression tree; Based on the constants in each subtree, each subtree is judged respectively to determine the subtree that can be further valued; Judge each subtree in order from bottom to top; If it is determined that a subtree cannot be further valued, then the parent tree corresponding to the subtree cannot be further valued; According to the judgment result, determining the subtree that can be further valued; Optimizing the predicate expression tree based on a subtree that can further take values; Parsing the optimized predicate expression tree into target code includes: according to the type of subtree in the optimized predicate expression tree, parsing the subtree of the corresponding type into corresponding database statements and variable parameter sets.
Citation Information
Patent Citations
Tax intermediate index extraction method based on subtree pattern mining
CN104111987A
Tree structure mathematics application question solving method and system based on semantic alignment
CN111259632A