Equivalence verification method and system for templated database query plans

By converting SQL query plans into unbounded semi-rings and first-order logical expressions, combined with SMT solvers, the shortcomings of SQL statement equivalence verification in the existing technology are solved, especially the support for NULL values ​​and outer join operators. Implemented more comprehensive equivalence verification and automated verification capabilities.

CN114756581BActive Publication Date: 2024-10-25SHANGHAI JIAOTONG UNIV
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202210427799.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-04-22
Publication Date
2024-10-25
Estimated Expiration
2042-04-22

AI Technical Summary

Technical Problem

The existing technology cannot support complex predicates, some SQL features and integrity constraints when verifying the equivalence of SQL statements, especially the modeling of NULL values ​​and outer join operators, resulting in incomplete equivalence verification.

Method used

By converting SQL query plans into unbounded semicyclic expressions and first-order logical expressions, combined with SMT solvers, the equivalence of query plans is verified, modeling of NULL values ​​and outer join operators is supported, and integrity constraints are extended verification capabilities.

Benefits of technology

A more comprehensive verification of SQL query equivalence is achieved, equivalence in more practical applications is derived, and the ability to automate verification is improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114756581B_ABST
    Figure CN114756581B_ABST
Patent Text Reader

Abstract

The present invention provides a method and system for verifying the equivalence of a templated database query plan, comprising: step S1: converting two preset symbolic query plan tree structures into two unbounded semiring expressions respectively; step S2: converting an equation composed of the two unbounded semiring expressions into a first-order logic expression; step S3: converting a constraint set into a first-order logic expression; step S4: forming a first-order logic expression to be solved representing the equivalence of two query plans based on the first-order logic expression composed of the two unbounded semiring expressions and the first-order logic expression converted from the constraint set; solving the first-order logic expression to be solved by using an SMT solver to obtain a result of whether the two query plans are equivalent; the constraint set is a set of symbolic constraints within the symbolic query plan tree structure or between a pair of symbolic query plan tree structures.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of relational database SQL statement equivalence verification, and in particular, to a method and system for verifying the equivalence of templated database query plans, and more particularly, to a method and system for verifying the equivalence of templated database query plans based on a semiring algebraic structure and an SMT solver. Background Art

[0002] Structured Query Language (SQL) is a database query language used to access data and query, update, and manage relational database systems. Verifying the equivalence of SQL statements is an important issue in the database field, which helps optimize queries and reduce redundant query calculations.

[0003] In recent years, a series of works have emerged to formally verify the equivalence of SQL statements. Existing works based on ProofAssistant use algebraic theory methods to formally model SQL statements as semi-ring algebraic expressions, extend the semi-ring semantics of SQL, and prove the equivalence of SQL statements under set and bag (sets that allow repeated elements) semantics with the help of a series of algebraic theories and auxiliary axioms.

[0004] However, the existing SQL statement equivalence proof based on Proof Assistant and algebraic theory still has many shortcomings, specifically in the following aspects:

[0005] 1. Does not support equivalent conversion of complex predicates: When there are structural differences between predicates, equivalence cannot be proved.

[0006] 2. Some SQL features are not supported: "three-valued logic" is not supported, that is, NULL is not modeled, and all column attributes are non-NULL by default. The lack of support for NULL also leads to the lack of support for all OUTER JOIN type relational operators.

[0007] 3. The verification process relies on predefined expression normalization rewriting rules, and the verification equivalence relies on the syntactic isomorphism between the rewritten expressions, which limits the completeness of equivalence verification of such methods.

[0008] Other SQL statement equivalence verification work verifies equivalence based on Satisfiabil ity ModuloTheories (SMT) solvers. Satisfiabil ity ModuloTheories generalizes the Boolean satisfiability problem to equational reasoning, arithmetic, arrays, quantifiers, and other first-order theories. SMT solvers are used to solve the satisfiability problem of first-order logic formulas involving the above theories. This type of SMT-based SQL statement equivalence verification work verifies the equivalence of SQL statements by modeling SQL statements as symbolic representations and converting the definition of query equivalence problems into first-order logic expressions, and using SMT solvers to verify the equivalence of SQL statements.

[0009] However, the existing work on verifying the equivalence of SQL statements based on SMT solvers still has many shortcomings, specifically in the following aspects:

[0010] 1. It does not support the modeling of integrity constraints of relational objects (such as foreign keys, unique constraints, etc.), and cannot verify the equivalence of SQL statements involving relational objects with integrity constraints.

[0011] 2. Also relying on predefined normalization rewriting rules, the symbolic structure representation of the query is rewritten into a normalized form. It is difficult to verify the equivalence of SQL statements with large structural differences, which limits the completeness of equivalence verification of this method.

[0012] At present, the work of SQL statement equivalence verification and solution still has gaps in supporting some SQL features and integrity constraints. There is no SQL statement equivalence verification work that can combine algebraic methods and SMT solvers to improve the support for SQL features such as "three-valued logic" and integrity constraints. Therefore, previous work on verifying SQL statement equivalence is not comprehensive and there are still some gaps.

[0013] Patent document CN112231205A (application number: 202011055948.9) discloses a first-order logic formula program verification method and system based on an SMT solver, including receiving a program formal verification data interaction request sent by any of at least one client; extracting the first-order logic formula in the data verification request based on the acquired data verification request content; and performing satisfiability solving on the first-order logic formula in the above data verification request based on the SMT solver. The present invention realizes the automatic proof of the first-order logic formula based on the SMT solver, so that the formal verification system can automatically verify the program and improve the credibility of the software. Summary of the invention

[0014] In view of the defects in the prior art, the object of the present invention is to provide a method and system for verifying the equivalence of templated database query plans.

[0015] A method for verifying the equivalence of a templated database query plan provided by the present invention comprises:

[0016] Step S1: converting two preset symbolic query plan tree structures into two unbounded semiring expressions respectively;

[0017] Step S2: converting the equation consisting of two unbounded semiring expressions into a first-order logic expression;

[0018] Step S3: convert the constraint set into a first-order logic expression;

[0019] Step S4: A first-order logic expression to be solved representing the equivalence of the two query plans is formed based on the first-order logic expression formed by the two unbounded semiring expressions and the first-order logic expression converted by the constraint set; the first-order logic expression to be solved is solved by using an SMT solver to obtain a result of whether the two query plans are equivalent;

[0020] The constraint set is a set of symbolic constraints within a symbolic query plan tree structure or between a pair of symbolic query plan tree structures.

[0021] Preferably, the step S1 adopts:

[0022] Step S1.1: Replace the parameters of the preset specific relational objects in the SQL with symbols to generate templated SQL;

[0023] Step S1.2: Each templated SQL statement is converted into a corresponding symbolic query plan tree structure through the query optimizer of the database, and an expression based on an unbounded semiring algebraic structure is generated based on the symbolic query plan tree structure.

[0024] Preferably, the step S1.1: replace the parameters of the preset specific relational objects in the SQL, including: table, column attributes and judgment predicates, with preset symbols to generate modular SQL.

[0025] Preferably, the step S1.2: the expression based on the unbounded semiring algebraic structure adopts: the terms defined in the UDP algorithm, and the SQL query plan is expressed as an operation on a natural number semiring under packet semantics;

[0026] A SQL statement is modeled as a function [[q]]: tuple->natural number N, which means inputting a tuple and outputting the number of times the tuple appears in the result set of the SQL query.

[0027] Preferably, the processing of NULL value tuples is added, so as to expand the semantic scope of verifying the equivalence of query plans;

[0028] A new predicate is introduced to convert unknown in three-valued logic into false in two-valued logic, thereby realizing the conversion of NULL from three-valued logic to two-valued logic, while ensuring that the unbounded semiring expression can correctly represent the semantics of the query plan;

[0029] Based on the processing of NULL, new modeling of the semantics of the outer join operator has been added, supporting the conversion of the outer join operator into an unbounded semiring expression representing its semantics.

[0030] Preferably, the step S3 adopts: for a given constraint set C, each constraint c∈C therein is converted into a first-order logic expression, and then the constraint set C is converted into a conjunction of the expressions converted from each constraint therein;

[0031]

[0032] Among them, FOL stands for first-order logic.

[0033] Preferably, the step S4 adopts:

[0034]

[0035] Among them, C I represents the truth value of the first-order logic expression of the constraint set C under instantiation I; It indicates universal quantifier; It represents the actual query plan obtained after all symbols in the src query plan are assigned by the instantiation operation I; It represents the actual query plan obtained after all symbols in the dest query plan are allocated by the instantiation operation I; t represents any tuple.

[0036] Preferably, the step S4 adopts:

[0037]

[0038] According to the present invention, a templated database query plan equivalence verification system is provided, comprising:

[0039] Module M1: Convert two preset symbolic query plan tree structures into two unbounded semiring expressions respectively;

[0040] Module M2: Convert the equation consisting of two unbounded semiring expressions into a first-order logic expression;

[0041] Module M3: Convert constraint sets into first-order logic expressions;

[0042] Module M4: A first-order logic expression to be solved representing the equivalence of two query plans is formed based on the first-order logic expression formed by the two unbounded semiring expressions and the first-order logic expression transformed by the constraint set; the first-order logic expression to be solved is solved by using the SMT solver to obtain the result of whether the two query plans are equivalent;

[0043] The constraint set is a set of symbolic constraints within a symbolic query plan tree structure or between a pair of symbolic query plan tree structures.

[0044] Preferably, the module M1 adopts:

[0045] Module M1.1: Replace the parameters of the preset specific relational objects in SQL with symbols to generate templated SQL;

[0046] Module M1.2: Convert each templated SQL statement into a corresponding symbolic query plan tree structure through the database query optimizer, and generate an expression based on the unbounded semiring algebraic structure based on the symbolic query plan tree structure;

[0047] The module M1.1: replaces the parameters of the preset specific relational objects in the SQL, including: table, column attributes and judgment predicates, with preset symbols to generate modular SQL;

[0048] The module M1.2: Expressions based on unbounded semiring algebraic structures adopt: terms defined in the UDP algorithm, and represent SQL query plans as operations on natural number semirings under package semantics;

[0049] A SQL statement is modeled as a function [[q]]: tuple->natural number N, which means inputting a tuple and outputting the number of times the tuple appears in the result set of the SQL query;

[0050] The module M3 adopts: for a given constraint set C, each constraint c∈C therein is converted into a first-order logic expression, and then the constraint set C is converted into a conjunction of the expressions converted from each constraint therein;

[0051]

[0052] Among them, FOL stands for first-order logic;

[0053] The module M4 adopts:

[0054]

[0055] Among them, C I represents the truth value of the first-order logic expression of the constraint set C under instantiation I; It indicates universal quantifier; It represents the actual query plan obtained after all symbols in the src query plan are assigned by the instantiation operation I; represents the actual query plan obtained after all symbols in the dest query plan are assigned by the instantiation operation I; t represents any tuple;

[0056] The module M4 adopts:

[0057]

[0058] Compared with the prior art, the present invention has the following beneficial effects:

[0059] 1. Symbolize and template SQL statements to model and verify that the equivalence of symbolized SQL queries can derive the equivalence between more SQL queries in practical applications;

[0060] 2. Combining unbounded semiring expressions with the satisfiability modulo theory solver improves the modeling of NULL values ​​and the support for "three-valued logic", supports the modeling of multiple integrity constraints, and expands the ability to automatically verify SQL equivalence under package semantics. BRIEF DESCRIPTION OF THE DRAWINGS

[0061] Other features, objects and advantages of the present invention will become more apparent from the detailed description of non-limiting embodiments made with reference to the following drawings:

[0062] Figure 1 Flowchart of the equivalence verification method for templated database query plans. DETAILED DESCRIPTION

[0063] The present invention is described in detail below in conjunction with specific embodiments. The following embodiments will help those skilled in the art to further understand the present invention, but are not intended to limit the present invention in any form. It should be noted that, for those of ordinary skill in the art, several changes and improvements can also be made without departing from the concept of the present invention. These all belong to the protection scope of the present invention.

[0064] Example 1

[0065] A method for verifying the equivalence of a templated database query plan provided by the present invention comprises:

[0066] Step S1: converting two preset symbolic query plan tree structures into two unbounded semiring expressions respectively;

[0067] Step S2: converting the equation consisting of two unbounded semiring expressions into a first-order logic expression;

[0068] Step S3: convert the constraint set into a first-order logic expression;

[0069] Step S4: A first-order logic expression to be solved representing the equivalence of the two query plans is formed based on the first-order logic expression formed by the two unbounded semiring expressions and the first-order logic expression converted by the constraint set; the first-order logic expression to be solved is solved by using an SMT solver to obtain a result of whether the two query plans are equivalent;

[0070] The constraint set is a set of symbolic constraints within a symbolic query plan tree structure or between a pair of symbolic query plan tree structures.

[0071] Specifically, the step S1 adopts:

[0072] Step S1.1: Replace the parameters of the preset specific relational objects in the SQL with symbols to generate templated SQL;

[0073] Step S1.2: Each templated SQL statement is converted into a corresponding symbolic query plan tree structure through the query optimizer of the database, and an expression based on an unbounded semiring algebraic structure is generated based on the symbolic query plan tree structure.

[0074] Specifically, the step S1.1: replace the parameters of the preset specific relational objects in the SQL, including: table, column attributes and judgment predicates, with preset symbols to generate modular SQL.

[0075] Specifically, the step S1.2: based on the expression of the unbounded semiring algebraic structure, adopts: the terms defined in the UDP algorithm, and represents the SQL query plan as an operation on the natural number semiring under the package semantics;

[0076] A SQL statement is modeled as a function [[q]]: tuple->natural number N, which means inputting a tuple and outputting the number of times the tuple appears in the result set of the SQL query.

[0077] Specifically, new processing for NULL-valued tuples is added, which can expand the semantic scope of verifying query plan equivalence;

[0078] A new predicate is introduced to convert unknown in three-valued logic into false in two-valued logic, thereby realizing the conversion of NULL from three-valued logic to two-valued logic, while ensuring that the unbounded semiring expression can correctly represent the semantics of the query plan;

[0079] Based on the processing of NULL, new modeling of the semantics of the outer join operator has been added, supporting the conversion of the outer join operator into an unbounded semiring expression representing its semantics.

[0080] Specifically, the step S3 adopts: for a given constraint set C, each constraint c∈C therein is converted into a first-order logic expression, and then the constraint set C is converted into a conjunction of the expressions converted from each constraint therein;

[0081]

[0082] Among them, FOL stands for first-order logic.

[0083] Specifically, the step S4 adopts:

[0084]

[0085] Among them, C I represents the truth value of the first-order logic expression of the constraint set C under instantiation I; It indicates universal quantifier; It represents the actual query plan obtained after all symbols in the src query plan are assigned by the instantiation operation I; It represents the actual query plan obtained after all symbols in the dest query plan are allocated by the instantiation operation I; t represents any tuple.

[0086] Specifically, the step S4 adopts:

[0087]

[0088] According to the present invention, a templated database query plan equivalence verification system is provided, comprising:

[0089] Module M1: Convert two preset symbolic query plan tree structures into two unbounded semiring expressions respectively;

[0090] Module M2: Convert the equation consisting of two unbounded semiring expressions into a first-order logic expression;

[0091] Module M3: Convert constraint sets into first-order logic expressions;

[0092] Module M4: A first-order logic expression to be solved representing the equivalence of two query plans is formed based on the first-order logic expression formed by the two unbounded semiring expressions and the first-order logic expression transformed by the constraint set; the first-order logic expression to be solved is solved by using the SMT solver to obtain the result of whether the two query plans are equivalent;

[0093] The constraint set is a set of symbolic constraints within a symbolic query plan tree structure or between a pair of symbolic query plan tree structures.

[0094] Specifically, the module M1 adopts:

[0095] Module M1.1: Replace the parameters of the preset specific relational objects in SQL with symbols to generate templated SQL;

[0096] Module M1.2: Convert each templated SQL statement into a corresponding symbolic query plan tree structure through the database query optimizer, and generate an expression based on the unbounded semiring algebraic structure based on the symbolic query plan tree structure;

[0097] The module M1.1: replaces the parameters of the preset specific relational objects in the SQL, including: table, column attributes and judgment predicates, with preset symbols to generate modular SQL;

[0098] The module M1.2: Expressions based on unbounded semiring algebraic structures adopt: terms defined in the UDP algorithm, and represent SQL query plans as operations on natural number semirings under package semantics;

[0099] A SQL statement is modeled as a function [[q]]: tuple->natural number N, which means inputting a tuple and outputting the number of times the tuple appears in the result set of the SQL query;

[0100] The module M3 adopts: for a given constraint set C, each constraint c∈C therein is converted into a first-order logic expression, and then the constraint set C is converted into a conjunction of the expressions converted from each constraint therein;

[0101]

[0102] Among them, FOL stands for first-order logic;

[0103] The module M4 adopts:

[0104]

[0105] Among them, C I represents the truth value of the first-order logic expression of the constraint set C under instantiation I; It indicates universal quantifier; It represents the actual query plan obtained after all symbols in the src query plan are assigned by the instantiation operation I; represents the actual query plan obtained after all symbols in the dest query plan are assigned by the instantiation operation I; t represents any tuple;

[0106] The module M4 adopts:

[0107]

[0108] Example 2

[0109] Embodiment 2 is a preferred embodiment of Embodiment 1

[0110] In view of the deficiencies in the prior art, the present invention provides a symbolic SQL statement equivalence solver based on a semiring algebraic structure and satisfiability modular theory.

[0111] According to the present invention, a symbolic SQL statement equivalence solver based on semiring algebraic structure and satisfiability module theory is provided. The symbolic SQL statement, that is, the parameters of the specific relational objects in SQL: table, column attributes and judgment predicates, are all replaced by symbols to become templated SQL. The symbolic SQL equivalence problem is defined as given two symbolic SQLs and a set of constraints between the symbols on the two SQLs, verifying whether the two SQLs are equivalent under all the constraints in the constraint set. Each symbolic SQL statement has a corresponding SQL query plan tree structure. The query plan tree structure is used to generate an expression based on an unbounded semiring algebraic structure, and the SMT solver is used to verify the SQL equivalence. Specifically, the following steps are included:

[0112] Step 1: Modeling the SQL query plan: Each SQL will be converted into a query plan tree by the query optimizer of the database, and each tree node contains an operator and one or more parameters. When executing SQL, each node takes the output table of its child nodes as input and executes the operators on each tree node from bottom to top. The present invention symbolizes the parameters of the specific relational objects on the operators in the SQL query plan to form a symbolic SQL query.

[0113] Step 2: Modeling the SQL query plan as an unbounded semi-ring algebraic expression: The present invention uses an unbounded semi-ring-based expression to represent the query plan of an SQL statement. The expression uses the terms defined in the UDP algorithm, and represents the SQL query plan as an operation on a natural number semi-ring under packet semantics, that is, an SQL statement is modeled as a function [[q]]: tuple->natural number N, which means inputting a tuple and outputting the number of times the tuple appears in the result set of the SQL query (the [[]] symbol is omitted in the subsequent representation).

[0114] Step 3: Convert the constraint set into a first-order logic expression (First Order Logic, FOL): For a given constraint set C, convert each constraint c∈C into a first-order logic expression. Then convert the constraint set C into the conjunction of the expressions converted from its elements, that is:

[0115]

[0116] Step 4: Formalize the SQL equivalence problem:

[0117] "Instantiation" definition: Given a SQL query q, which is represented as an unbounded semiring expression, an instantiation operation I is to assign a specific relation object to all symbols on q, making it an instantiated query q I Similarly, under instantiation I, the truth value of constraint set C is denoted by C I .

[0118] A SQL equivalence verification problem consisting of two SQL queries and a constraint set src ,q dest ,C>, define q src ,q dest Equivalence is true if and only if the following formula holds. This formula is consistent with the definition of package equivalence: for any instantiation I, if the constraints in the constraint set hold, then the two SQL queries are equivalent, that is, the number of times any tuple t appears in the result sets of the two instantiation SQL queries is always equal.

[0119]

[0120] Step 5: Logic-based equivalence verification procedure: Convert the above SQL equivalence definition into a first-order logic expression and use the SMT solver to perform equivalence verification.

[0121] To eliminate the outer universal quantifier in the equivalence definition formula Each symbol in SQL needs to be declared as an uninterpreted function.

[0122] Since the constraint set C has been converted into a first-order logic expression, we only need to convert q src (t) = q dest (t) is converted into a first-order logic expression, and then the SMT solver is used to solve the unsatisfiability of the following formula. If the formula is unsatisfiable, the two SQL statements are equivalent.

[0123]

[0124] Specifically, in step 1, define the symbol type as follows:

[0125] table: A table symbol tbl, symbolizing an input table.

[0126] Column attribute list: A column attribute list symbol a represents a list of column attributes. In particular, a table symbol tbl also corresponds to a column attribute list symbol a tbl , representing all column attributes in this table.

[0127] Predicate: A predicate symbol p symbolizes a predicate expression in the WHERE clause of an SQL statement.​

[0128] Specifically, in step 1, the equivalence verification is limited to the SQL query plans containing the following operators, where the relational objects on the operators have been symbolized as symbolic representations:

[0129]

[0130]

[0131] Specifically, in step 2, for each symbol in the query plan, it is converted into an uninterpreted function:

[0132] Each table symbol t corresponds to a function t:Tuple->N, whose output is the number of times the input tuple appears in the table.

[0133] Each column attribute list a corresponds to a function a:Tuple->Tuple, whose output is the tuple obtained by projecting the input tuple onto the column attribute list a.

[0134] Each judgment predicate p corresponds to a function p:Tuple->Boolean, whose output is a Boolean value, indicating whether the value of the input tuple satisfies the predicate condition.

[0135] Specifically, in step 2, the query plan tree structure is summarized from bottom to top, that is, the child node expression is parsed first, and then the parent node expression is constructed using the child node expression. It is characterized in that each operator node constructs an expression through the following conversion table, and the expression corresponding to the root node is the conversion result of the SQL. The subscript of the operator node in the table is the symbol on the operator; f l ,f r An expression representing the transformation of left and right child nodes, t l ,t r Represents the input variable in the left and right child node expressions (if there is only one child node, f is used by default l and t l indicates); t l ·t r Indicates t l ,t r The tuples produced by the concatenation.

[0136]

[0137] Specifically, in step 2, the processing of NULL values ​​is supported as follows:

[0138] Define a "NULL" tuple as a tuple whose column attributes are all NULL values, then all "NULL" tuples are the same tuple, such as in Table tblIn the node, the expression tbl(NULL) is the number of times the "NULL" tuple appears in table tbl.

[0139] For a predicate p, in three-valued logic, the NULL value will cause the predicate to return an unknown result. p,a The node only retains tuples whose predicate p evaluates to true, so unknown in three-valued logic can be treated as false in two-valued logic, with the values ​​of [unknown] and [false] both being 0.

[0140] In the definition of unbounded semiring expression, a new predicate IsNull:Tuple->Boolean is introduced. If the input tuple is a "NULL" tuple, the return value is true. This predicate can be used to filter out "NULL" tuples in the expressions corresponding to In-Sub Filter and the three Join operators.

[0141] Specifically, in step 2, based on the above modeling of NULL, the present invention can support the modeling of the semantics of the outer join operator. Taking LeftJoin as an example, it returns all tuples of the left table, regardless of whether there are matching tuples in the right table. For tuples that do not have matching values ​​in the right table, NULL values ​​will be used instead. Therefore, the expression of LeftJoin is the sum of two terms: one term is the same as the Inner Join expression, representing that there are matching tuples in the right table; one term represents that there are no matching tuples in the right table, which is the product of three terms, f l (t l ) means to keep all tuples in the left table, [IsNull(t r )] indicates that the column attribute value originally belonging to the right table is a tuple of NULL values. Indicates the judgment situation that the Join condition is not met.

[0142] Specifically, in step 3, define the following constraint types:

[0143] TableEq(tbl1,tbl2): The two input tables tbl1 and tbl2 are identical.

[0144] AttrEq(a1,a2): The two column attribute lists are the same, that is, they have the same column attributes.

[0145] PredEq(p1,p2): two predicates are equivalent, that is,

[0146] SubAttr(a1,a2): means that the column attributes in a1 also exist in a2, that is, the column attributes contained in a1 are a subset of a2.

[0147] Reference(tbl1,a1,tbl2,a2): means that the column attribute a1 in table tbl1 is a reference to the column attribute a2 in table tbl2, that is, any tuple value projected on a1 in table tbl1 belongs to the set of tuples projected on a2 in table tbl2.

[0148] Unique(tbl,a): means that the tuple value projected on a in table tbl is subject to a uniqueness constraint.

[0149] NotNull(tbl,a): means that the tuple value projected on a in table tbl has a non-NULL constraint.

[0150] Specifically, in step 3, a constraint is converted into a first-order logic expression according to the following table:

[0151]

[0152]

[0153] Specifically, in step 5, in the logic-based equivalence verification procedure, an SMT solver, Z3, is used to perform equivalence verification. In Z3, each symbol in SQL is declared as an uninterpreted function using the "declare-fun" statement:

[0154] Table symbol t: declare-fun t(Tuple)int

[0155] Column attribute list notation a: declare-fun a(Tuple)Tuple

[0156] Predicate symbol p: declare-fun p(Tuple)Bool

[0157] where the type "Tuple" is declared in the Z3 solver as an uninterpreted sort type in SMTLIB.

[0158] Specifically, in step 5, in the logic-based equivalence verification procedure, the following conversion table is constructed to define the corresponding conversion relationship between unbounded semiring expressions and first-order logic expressions. Among them, Tr means recursively converting sub-expressions according to this table; ite(p, v0, v1) means that if the predicate p is true, v0 is returned, otherwise v1 is returned.

[0159]

[0160]

[0161] Specifically, in step 5, in the logic-based equivalence verification procedure, the timeout of the Z3 solver is set to 20 seconds. The possible return results of the Z3 solver are divided into: SAT-satisfiable, UNSAT-unsatisfiable, and UNKNOWN. When the Z3 solver cannot return a result within 20 seconds, UNKNOWN is returned, and the equivalence verification of the SQL query is considered to have failed.

[0162] Example 3

[0163] Embodiment 3 is a preferred embodiment of Embodiment 1

[0164] According to a symbolic SQL statement equivalence solver based on semiring algebraic structure and satisfiability module theory provided by the present invention, taking the following two SQL statements q0 and q1 and constraint set C as examples, the method will perform the following steps in sequence:

[0165] q0:SELECT t0.a2 FROM t0 INNER JOIN t1 ON t0.a0=t1.a1

[0166] q1:SELECT t2.a3 FROM t2

[0167] Constraint set C={TabelEq(t0,t2),AttrEq(a2,a3)}∪{SubAttr(a0,t0),SubAttrs(a1,t1),SubAttr(a2,t0)}∪{Unique(t1,a1),NotNull(t0,a0),Reference(t0,a0,t1,a1)}

[0168] 1. Construct the query plan tree structure corresponding to the two SQL statements, in which the constraints involving symbolic equivalence, namely TabelEq(t0,t2) and AttrEq(a2,a3), are applied to the construction of the query plan tree.

[0169] 2. Model the SQL query plan as an unbounded semi-ring algebraic expression: perform expression induction on the tree structure from bottom to top, that is, first parse the expression of the child node, and then use the child node expression to build the parent node expression. The expression generated by the transformation process of the two SQL query plans is:

[0170]

[0171] q1(t)=Σ x [t=a2(x)]×t0(x) (#1)

[0172] Convert the constraint set into a first-order logic expression: Convert the constraint set C into the conjunction of the expressions converted from its elements:

[0173]

[0174] 4.Formally define the SQL equivalence problem: q0 is equivalent to q1 if and only if the following formula holds:

[0175]

[0176] 5. Logic-based equivalence verification procedure: Convert q0(t)=q1(t) into a first-order logic expression:

[0177]

[0178] in:

[0179] f(x) is the term [t=a2(x)] in expression #1,

[0180] g(x) is the not([IsNull(a0(x))]) term in expression #0.

[0181] h(x,y) is the three terms [t=a2(x·y)]×t1(y)×[a0(x)=a1(y)] in expression #0.

[0182] Tr(f(x))=Tr([t=a2(x)])=ite(t=a2(x),1,0),

[0183] Tr(g(x))=Tr(not([IsNull(a0(x))]))=ite(ite(IsNull(a0(x)),1,0),1,0),

[0184]

[0185]

[0186] Verify the expression using the Z3 solver If the final result is unsatisfiable, the two SQL statements are equivalent.

[0187] The present invention provides a symbolic SQL statement equivalence solver based on semiring algebraic structure and satisfiability modulo theory. The present invention models symbolic SQL statements as expressions based on unbounded semiring algebraic structure under package semantics, defines conversion rules between unbounded semiring expressions and first-order logic expressions, converts two SQL statement equivalence conditions into first-order logic expressions, and verifies SQL equivalence by using a satisfiability modulo theory solver. Compared with the prior art, the present invention improves the modeling of NULL values ​​and the support of "three-valued logic" in SQL, supports the modeling of multiple integrity constraints, and expands the ability of automatic verification of SQL equivalence.

[0188] Those skilled in the art know that, in addition to implementing the system, device and its various modules provided by the present invention in a purely computer-readable program code, it is entirely possible to implement the same program in the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers and embedded microcontrollers by logically programming the method steps. Therefore, the system, device and its various modules provided by the present invention can be considered as a hardware component, and the modules included therein for implementing various programs can also be considered as structures within the hardware component; the modules for implementing various functions can also be considered as both software programs for implementing the method and structures within the hardware component.

[0189] The above describes the specific embodiments of the present invention. It should be understood that the present invention is not limited to the above specific embodiments, and those skilled in the art can make various changes or modifications within the scope of the claims, which does not affect the essence of the present invention. In the absence of conflict, the embodiments of the present application and the features in the embodiments can be combined with each other arbitrarily.

Claims

1. A method for verifying the equivalence of a templated database query plan, characterized in that: include: Step S1: converting two preset symbolic query plan tree structures into two unbounded semiring expressions respectively; Step S2: converting the equation consisting of two unbounded semiring expressions into a first-order logic expression; Step S3: convert the constraint set into a first-order logic expression; Step S4: A first-order logic expression to be solved representing the equivalence of the two query plans is formed based on the first-order logic expression formed by the two unbounded semiring expressions and the first-order logic expression converted by the constraint set; the first-order logic expression to be solved is solved by using an SMT solver to obtain a result of whether the two query plans are equivalent; The constraint set is a set of symbolic constraints within a symbolic query plan tree structure or between a pair of symbolic query plan tree structures; New handling of NULL value tuples, which expands the semantic scope of verifying query plan equivalence; Introduce a new predicate and convert the three-valued logic Transformed into binary logic , thus realizing the transformation of NULL's three-valued logic into two-valued logic, while ensuring that the unbounded semiring expression can correctly represent the semantics of the query plan; Based on NULL processing, new modeling of the semantics of the outer join operator has been added, supporting the conversion of the outer join operator into an unbounded semi-ring expression representing its semantics; The step S3 adopts: for a given constraint set C, for each constraint Transform it into a first-order logic expression, and then transform the constraint set C into the conjunction of the expressions transformed from each constraint; in, represents first-order logic; The step S4 adopts: for a SQL equivalence verification problem consisting of two SQL queries and a constraint set ,definition Equivalence holds if and only if the following formula holds; this formula is consistent with the definition of package equivalence: for any instantiation If all constraints in the constraint set are met, then the two SQL queries are equivalent, and any tuple The number of occurrences in the result sets of two instantiated SQL queries is always equal; in, Indicates that in instantiation Next, the constraint set The truth value of a first-order logic expression of ; where the instantiation is defined as given a SQL query , which is represented as an unbounded semiring expression, an instantiation operation Yes All symbols above are assigned a specific relation object, making it an instantiated query ; In instantiation Next, the constraint set The truth value of ; It indicates universal quantifier; Indicates that all symbols in the src query plan are instantiated. The actual query plan obtained after allocation; Indicates that all symbols in the dest query plan are instantiated. The actual query plan obtained after allocation; represents any ancestor; The step S4 adopts: a logic-based equivalence verification procedure: converting the above SQL equivalence definition into a first-order logic expression, and using an SMT solver to perform equivalence verification; Since the constraint set has been converted into a first-order logic expression, then we only need to Convert it into a first-order logic expression, and then use the SMT solver to solve the unsatisfiability of the following formula. If the formula is unsatisfiable, the two SQL statements are equivalent; 。 2. The equivalence verification method of templated database query plan according to claim 1, characterized in that: The step S1 adopts: Step S1.1: Replace the parameters of the preset specific relational objects in the SQL with symbols to generate templated SQL; Step S1.2: Each templated SQL statement is converted into a corresponding symbolic query plan tree structure through the query optimizer of the database, and an expression based on an unbounded semiring algebraic structure is generated based on the symbolic query plan tree structure.

3. The equivalence verification method of templated database query plan according to claim 2, characterized in that: The step S1.1: replacing the parameters of the preset specific relational objects in the SQL, including: table, column attributes and judgment predicates, with preset symbols to generate modular SQL.

4. The equivalence verification method of templated database query plan according to claim 2, characterized in that: The step S1.2: based on the expression of the unbounded semiring algebraic structure, the terms defined in the UDP algorithm are used to express the SQL query plan as an operation on the natural number semiring under the packet semantics; A SQL statement is modeled as a function :tuple ->Natural number , which means inputting a tuple and outputting the number of times the tuple appears in the result set of the SQL query.

5. A templated database query plan equivalence verification system, characterized in that: include: Module M1: Convert two preset symbolic query plan tree structures into two unbounded semiring expressions respectively; Module M2: Convert the equation consisting of two unbounded semiring expressions into a first-order logic expression; Module M3: Convert constraint sets into first-order logic expressions; Module M4: A first-order logic expression to be solved representing the equivalence of two query plans is formed based on the first-order logic expression formed by the two unbounded semiring expressions and the first-order logic expression transformed by the constraint set; the first-order logic expression to be solved is solved by using the SMT solver to obtain the result of whether the two query plans are equivalent; The constraint set is a set of symbolic constraints within a symbolic query plan tree structure or between a pair of symbolic query plan tree structures; New handling of NULL value tuples, which expands the semantic scope of verifying query plan equivalence; Introduce a new predicate and convert the three-valued logic Transformed into binary logic , thus realizing the transformation of NULL's three-valued logic into two-valued logic, while ensuring that the unbounded semiring expression can correctly represent the semantics of the query plan; Based on NULL processing, new modeling of the semantics of the outer join operator has been added, supporting the conversion of the outer join operator into an unbounded semi-ring expression representing its semantics; The module M3 adopts: for a given set of constraints C, for each constraint Transform it into a first-order logic expression, and then transform the constraint set C into the conjunction of the expressions transformed from each constraint; in, represents first-order logic; The module M4 adopts: for a SQL equivalence verification problem consisting of two SQL queries and a constraint set ,definition Equivalence holds if and only if the following formula holds; this formula is consistent with the definition of package equivalence: for any instantiation If all constraints in the constraint set are met, then the two SQL queries are equivalent, and any tuple The number of occurrences in the result sets of two instantiated SQL queries is always equal; in, Indicates that in instantiation Next, the constraint set The truth value of a first-order logic expression of ; where the instantiation is defined as given a SQL query , which is represented as an unbounded semiring expression, an instantiation operation Yes All symbols above are assigned a specific relation object, making it an instantiated query ; In instantiation Next, the constraint set The truth value of ; It indicates universal quantifier; Indicates that all symbols in the src query plan are instantiated. The actual query plan obtained after allocation; Indicates that all symbols in the dest query plan are instantiated. The actual query plan obtained after allocation; represents any ancestor; The module M4 adopts: a logic-based equivalence verification procedure: converting the above SQL equivalence definition into a first-order logic expression, and using an SMT solver to perform equivalence verification; Since the constraint set has been converted into a first-order logic expression, then we only need to Convert it into a first-order logic expression, and then use the SMT solver to solve the unsatisfiability of the following formula. If the formula is unsatisfiable, the two SQL statements are equivalent; 。 6. The equivalence verification system for templated database query plans according to claim 5, characterized in that: The module M1 adopts: Module M1.1: Replace the parameters of the preset specific relational objects in SQL with symbols to generate templated SQL; Module M1.2: Convert each templated SQL statement into a corresponding symbolic query plan tree structure through the database query optimizer, and generate an expression based on the unbounded semiring algebraic structure based on the symbolic query plan tree structure; The module M1.1: replaces the parameters of the preset specific relational objects in the SQL, including: table, column attributes and judgment predicates, with preset symbols to generate modular SQL; The module M1.2: Expressions based on unbounded semiring algebraic structures adopt: terms defined in the UDP algorithm, and represent SQL query plans as operations on natural number semirings under package semantics; A SQL statement is modeled as a function :tuple ->Natural number , which means inputting a tuple and outputting the number of times the tuple appears in the result set of the SQL query.

Citation Information

Patent Citations

  • First-order logic formula program verification method and system based on SMT solver

    CN112231205A

  • SQL injection defect detection method, system and device and storage medium

    CN109426722A

  • Symbolic finite automata

    US20120130932A1

  • Sophisticated run-time system for graph processing

    US20160188391A1