Database defect detection method and device based on function semantic equivalent replacement
By using the method of semantic equivalent substitution of functions, SQL query statements are obtained and reconstructed to identify logical defects in database management systems. This solves the problem of semantic inconsistencies in function implementation and query optimization that are difficult to detect in existing technologies, and improves the accuracy of detection.
Patent Information
- Application Number
- CN202611295669.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-25
- Publication Date
- 2026-11-27
AI Technical Summary
Existing technologies struggle to effectively identify logical flaws in database management systems, particularly semantic inconsistencies arising from function implementation and query optimization.
By using the function semantic equivalence replacement method, the original SQL query statement is obtained, the function call expression is located, semantic equivalence reconstruction is performed based on a pre-defined set of function semantic equivalence reconstruction rules, variant SQL query statements are generated, and the execution results are compared to generate a defect report.
It improves the accuracy and location of logical defects in database management systems, and can identify semantic inconsistencies in function implementation and query optimization processes while maintaining the semantic consistency of SQL queries.
Smart Images

Figure CN123178259A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of database management system testing, and in particular to a database defect detection method and apparatus based on function semantic equivalent substitution. Background Technology
[0002] Database management systems (DBMS) are one of the core infrastructures of modern software systems. They provide functions and expressions through Structured Query Language (SQL) to upper-layer applications, enabling data storage, retrieval, and complex query operations. However, due to the complexity of their internal implementation, DBMSs may still suffer from crashes and logical flaws during actual operation. Logical flaws typically do not cause abnormal system termination but instead return results inconsistent with expectations in specific query scenarios, thus affecting the correctness of upper-layer applications.
[0003] Therefore, it is necessary to check for logical defects in the database management system. Summary of the Invention
[0004] This application provides a database defect detection method and apparatus based on function semantic equivalent substitution, which can effectively identify logical defects in database management systems.
[0005] In a first aspect, embodiments of this application provide a database defect detection method based on function semantic equivalence substitution, including: Obtain the original SQL query statement, which contains at least one function call expression; Locate the function call expression in the original SQL query statement; Based on a pre-defined set of function semantic equivalence reconstruction rules, the function call expression is semantically reconstructed to generate variant SQL query statements; Execute the original SQL query statement and the variant SQL query statement respectively, and obtain their respective execution results; If the execution results are inconsistent, a defect report is generated.
[0006] In conjunction with the first aspect, in one implementation, the set of function semantic equivalence reconstruction rules is generated based on a function semantic constraint library, which stores function names, parameter constraints, and semantic information.
[0007] In conjunction with the first aspect, in one implementation, the function semantic constraint library is constructed through the following steps: extracting function names, parameter constraints, and function description information from official function documentation in a database; parsing semantic information based on the function description information; and storing the function names, parameter constraints, and semantic information in a structured manner to obtain the function semantic constraint library. And / or, the set of function semantic equivalence reconstruction rules is generated through the following steps: extracting the input-output relationship and behavioral features of functions from the semantic information in the function semantic constraint library; identifying candidate equivalent expressions based on the semantic information, and verifying whether the candidate expressions are semantically equivalent under the premise of satisfying parameter constraints; classifying the verified candidate expressions to generate a set of function semantic equivalence reconstruction rules; And / or, locating the function call expression in the original SQL query statement includes: scanning the original SQL query statement based on the function names in the function semantic constraint library to identify the function call location; determining the opening and closing parentheses of the function call location to obtain the function call interval; and determining the content within the function call interval as the function parameter interval.
[0008] In conjunction with the first aspect, in one implementation, the semantic equivalence reconstruction of the function call expression includes: The original function parameter expression in the function call expression is collapsed into placeholders; Based on the set of function semantic equivalence reconstruction rules, the structure of the folded function call expression is reconstructed to generate an intermediate SQL expression; The placeholders are replaced back with the original function parameter expressions to obtain the variant SQL query statement.
[0009] In conjunction with the first aspect, in one implementation, comparing the execution results includes: Compare the original SQL query statement with the variant SQL query statement in terms of exception information, number of rows in the query result, content of the result set, numerical content, and numerical calculation error; If any comparison item is inconsistent, the execution result is determined to be inconsistent.
[0010] In conjunction with the first aspect, in one implementation, generating defect report information includes: The original SQL query statement and the variant SQL query statement with inconsistent execution results are verified by repeated execution. Remove non-critical structures from the original SQL query statement to generate a minimal defect-reproducible SQL query statement that can reproduce inconsistent execution results. The output includes the original SQL query statement, the variant SQL query statement, the SQL query statement for minimizing defect reproduction, inconsistent execution results, and corresponding difference description information in the defect report information.
[0011] In conjunction with the first aspect, in one implementation, obtaining the original SQL query statement includes: Collect SQL query statements from database historical query logs, open-source test case libraries, or public benchmark sets, or generate SQL query statements based on SQL query statement generation methods; Select SQL query statements that contain at least one function call expression as the original SQL query statement.
[0012] Secondly, embodiments of this application provide a database defect detection device based on function semantic equivalence substitution, comprising: An acquisition module is used to acquire a raw SQL query statement, wherein the raw SQL query statement contains at least one function call expression; A positioning module, used to locate function call expressions in the original SQL query statement; The reconstruction module is used to perform semantic equivalence reconstruction on the function call expression based on a preset set of function semantic equivalence reconstruction rules, and generate variant SQL query statements; An execution module is used to execute the original SQL query statement and the variant SQL query statement respectively, and obtain the corresponding execution results; The generation module is used to compare the execution results, and if they are inconsistent, it generates defect report information.
[0013] Thirdly, embodiments of this application provide a database defect detection device based on function semantic equivalence substitution, including a processor, a memory, and a database defect detection program based on function semantic equivalence substitution stored in the memory and executable by the processor, wherein when the database defect detection program based on function semantic equivalence substitution is executed by the processor, it implements the steps of the database defect detection method based on function semantic equivalence substitution as described in any of the preceding claims.
[0014] Fourthly, embodiments of this application provide a computer-readable storage medium storing a database defect detection program based on function semantic equivalence substitution, wherein when the database defect detection program based on function semantic equivalence substitution is executed by a processor, it implements the steps of the database defect detection method based on function semantic equivalence substitution as described in any of the preceding claims.
[0015] The beneficial effects of the technical solution provided in this application include: This application provides a database defect detection method and apparatus based on function semantic equivalence substitution. By using a set of function semantic equivalence reconstruction rules, the method performs semantic equivalence reconstruction on function call expressions in the original SQL query statement to generate variant SQL query statements. The execution results of the original SQL query statement and the variant SQL query statement are obtained and compared. This application can construct multiple function equivalent expressions while maintaining the semantic consistency of SQL queries, thereby effectively identifying semantic inconsistencies in the function implementation and query optimization process of the database management system and improving the accuracy of logical defect detection in the database management system. Attached Figure Description
[0016] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0017] Figure 1 This is a flowchart illustrating an embodiment of the database defect detection method based on function semantic equivalence substitution in this application; Figure 2 This is a schematic diagram of the functional modules of an embodiment of the database defect detection device based on function semantic equivalent substitution of this application; Figure 3 This is a schematic diagram of the hardware structure of a database defect detection device based on function semantic equivalent substitution involved in the embodiments of this application. Detailed Implementation
[0018] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0019] The applicant generates variant SQL queries by semantically rewriting the original SQL query, and then determines whether defects exist by comparing the consistency of the execution results. However, this method still has problems because it mainly targets query-level semantic consistency detection. When defects exist at the function implementation or function semantic level, both the original SQL query and its variant SQL query may be affected simultaneously, resulting in semantically consistent but incorrect results, making it difficult to effectively identify related function semantic defects.
[0020] Therefore, in order to solve this problem and improve the accuracy of logical defect detection in database management systems, firstly, embodiments of this application provide a database defect detection method based on semantic equivalence substitution of functions, which can effectively identify semantic inconsistencies in database function implementations and improve the accuracy and location capability of database defect detection.
[0021] In one embodiment, reference is made to Figure 1 , Figure 1 This is a flowchart illustrating an embodiment of the database defect detection method based on function semantic equivalence substitution according to this application. Figure 1 As shown, database defect detection methods based on functional semantic equivalent substitution include: S101: Obtain the original SQL query statement, which contains at least one function call expression.
[0022] The original SQL query statement was not processed by function semantic equivalence reconstruction when it was retrieved.
[0023] The process of obtaining the original SQL query statement includes: collecting SQL query statements containing function call expressions from the database historical query logs of the database management system, open-source test case libraries, or public benchmark test sets; or generating SQL query statements containing function call expressions based on the SQL query statement generation method in the database management system test; and selecting SQL query statements containing at least one function call expression as the original SQL query statement, thereby improving the function coverage of subsequent function semantic equivalence reconstruction.
[0024] The SQL query statement generation methods include, but are not limited to, syntax rule-based SQL query statement generation methods (such as the context-free grammar (CFG) generation method used by the open-source tool SQLsmith), template-based SQL query statement construction methods (such as the standard query template construction method in the TPC-H or TPC-DS benchmark suites), and query mutation-based SQL query statement generation methods (such as the query mutation strategy or Equivalence ModuloInputs (EMI) mutation method in the SQLancer tool).
[0025] S102: Locate the function call expression in the original SQL query statement.
[0026] S103: Based on a pre-defined set of function semantic equivalence reconstruction rules, perform semantic equivalence reconstruction on the function call expression to generate variant SQL query statements.
[0027] S104: Execute the original SQL query statement and the variant SQL query statement respectively, and obtain their respective execution results.
[0028] In step S104, the original SQL query statement and the variant SQL query statement are executed in a uniformly configured runtime environment.
[0029] The unified configuration of the runtime environment includes: selecting the same database instance, loading the same database state, and setting consistent system parameters and optimizer strategies. This unified configuration of the runtime environment upon which semantic consistency verification depends ensures the consistency and comparability of the test environments.
[0030] For example, use the same MySQL 8.0 database instance as the runtime environment. Before executing the original SQL query and the variant SQL query, reset the database to the same state to ensure that the table data volume and index statistics are consistent. At the same time, set system parameters such as memory limits and optimizer policy switches to the same configuration. This aims to eliminate deviations in execution results caused by environmental differences, ensuring that the comparison results only reflect the consistency of the function implementation logic and avoiding false alarms.
[0031] S105: Compare the execution results. If they are inconsistent, generate a defect report.
[0032] In this embodiment, by using a set of function semantic equivalence reconstruction rules, the function call expression in the original SQL query statement is semantically reconstructed to generate a variant SQL query statement. The execution results of the original SQL query statement and the variant SQL query statement are obtained and compared. This application can construct multiple function equivalent expressions while maintaining the semantic consistency of SQL queries, thereby effectively identifying semantic inconsistencies in the function implementation and query optimization process of the database management system and improving the accuracy of logical defect detection in the database management system.
[0033] Furthermore, in one embodiment, the set of function semantic equivalence reconstruction rules is generated based on a function semantic constraint library.
[0034] The function semantic constraint library is used to describe the basic attributes and semantic features of official functions in the database. The function semantic constraint library stores function names, parameter constraints, function description information, and semantic information obtained by parsing the function description information.
[0035] The function semantic constraint library is constructed through the following steps: S201: Extract function names, parameter constraints, and function descriptions from the official database function documentation.
[0036] For example, in the MySQL official documentation, we can analyze the COALESCE function section. We can extract the function name "COALESCE", the parameter constraint "input and output types must match", and the function description "returns the first non-NULL value".
[0037] S202: Obtain semantic information by parsing the function description information.
[0038] The semantic information is used to characterize the function's function, input-output relationship, and behavioral characteristics under different input conditions.
[0039] By performing semantic parsing on the function description information, the corresponding semantic information can be obtained.
[0040] The semantic parsing can be achieved through a semantic parsing model, a rule-based parsing method, or a combination of both, to convert function description information into a structured semantic representation.
[0041] For example, based on the function description "returns the first non-NULL value", semantic information is obtained through semantic parsing. The description is then transformed into the logical expression "IF Value1 IS NOT NULL THEN Value1 ELSE Value2".
[0042] S203: The function names, parameter constraints, and semantic information are stored in a structured manner to obtain a function semantic constraint library.
[0043] For example, the function name "COALESCE", parameter constraints "input and output types are consistent", and semantic information "logical expression" are stored in a relational database table. Each record corresponds to a function, forming a key-value pair structure.
[0044] Furthermore, in one embodiment, the set of function semantic equivalence reconstruction rules is used to describe the semantic equivalence relationships between different function expressions, so as to support subsequent semantic equivalence reconstruction of function call expressions.
[0045] In some implementations, the semantic equivalence relations between function expressions can be formally represented as:
[0046] in, and This represents two function call expressions. Indicates semantic equivalence relation. It represents the set of valid inputs that satisfy the function parameter constraints, type constraints, and domain restrictions.
[0047] For numerical results, the semantic equivalence relation also includes numerical equivalence within a preset error range.
[0048] The semantically equivalent reconstruction rules for functions include at least the following types: 1) Alias equivalence reconstruction rules based on function alias relations; For example, in MySQL databases, the LOWER() and LCASE() functions are completely identical, both used to convert strings to lowercase, differing only in their names. The original SQL query uses LOWER(name), while the variant SQL query uses LCASE(name). They are equivalent aliases, used to check the consistency of database processing when different function names point to the same internal implementation.
[0049] 2) Function combination equivalence reconstruction rules based on the formation of equivalent expressions from multiple function combinations; For example, the aggregate function AVG(col) is semantically equivalent to a combination of SUM(col) / COUNT(col). The original SQL query uses AVG(price), while the variant SQL query uses SUM(price) / COUNT(price). This rule replaces a single complex function with a combination of multiple basic functions to check the consistency of the database's results between a single function implementation and a multi-function combination implementation.
[0050] 3) Mathematical equivalence reconstruction rules based on mathematical equivalence relations; For example, the power function POWER(price,2) is mathematically equivalent to the multiplication expression of price*price. The original SQL query uses POWER(price,2), while the variant SQL query uses price*price. This rule, based on mathematical identities, is used to check the consistency of the database's handling of the function's built-in algorithms and underlying operator implementations.
[0051] 4) Conditional semantic equivalence reconstruction rules based on function semantic constraints and input conditions.
[0052] For example, the null value handling function COALESCE(price,0) is semantically equivalent to the conditional expression CASE WHEN price IS NOT NULL THEN price ELSE 0 END. The original SQL query uses function calls, while the variant SQL query uses conditional logic. This rule, based on function semantic constraints and input conditions, is used to detect the consistency of the database's handling of specialized functions and general conditional logic.
[0053] Furthermore, in one embodiment, the set of function semantic equivalence reconstruction rules is generated through the following steps: S301: Extract the input-output relationship and behavioral characteristics of the function from the semantic information in the function semantic constraint library.
[0054] For example, the semantic information of COALESCE is read from the function semantic constraint library, and the input-output relationship is extracted as "multiple inputs and single outputs with consistent types", and the behavioral feature is "non-empty selection". This step transforms the semantic information into structured features, clarifies the data flow logic of the function, and provides a quantitative basis for subsequent matching of candidate expressions with the same features.
[0055] S302: Identify candidate equivalent expressions based on the semantic information, and verify whether the candidate expressions are semantically equivalent under the premise of satisfying parameter constraints.
[0056] For example, CASE WHEN is identified as a candidate expression based on behavioral features. Under the premise of satisfying the parameter constraint "input and output types are consistent," the logical truth tables of the two expressions are verified to be identical. This step, through constraint validation and logical comparison, excludes expressions that are similar in form but not substantially equivalent, ensuring that the generated variant SQL queries are strictly equivalent in semantics.
[0057] S303: Categorize the validated candidate expressions and generate a set of rules for semantically equivalent reconstruction of functions.
[0058] For example, the verified correspondences are categorized as conditional semantic equivalence reconstruction rules, and multiple such function semantic equivalence reconstruction rules are aggregated to construct a set of function semantic equivalence reconstruction rules.
[0059] Further, in one embodiment, locating the function call expression in the original SQL query statement includes: S401: Based on the function names in the function semantic constraint library, scan the original SQL query statement to identify the function call location.
[0060] S402: Determine the starting and ending parentheses of the function call location to obtain the function call interval.
[0061] S403: The content within the function call interval is determined as the function parameter interval.
[0062] The function call expression refers to the function call structure in the original SQL query statement that satisfies the function semantic equivalence reconstruction condition.
[0063] To facilitate subsequent refactoring, the complete text range corresponding to each function call expression is defined as the function call range.
[0064] The function call interval consists of the function name and all content from its starting parenthesis to its matching closing parenthesis, and the parenthesis boundary is determined by parenthesis matching.
[0065] It's important to note that a function call expression refers to the code text within an SQL statement that contains complete function logic, specifically including the function name, opening brackets, closing brackets, and the content within the brackets (i.e., parameters); while a function call interval refers to the range of character positions of that code text within the SQL statement string. Essentially, both refer to the same object; the function call interval defines the physical boundaries of the function call expression, facilitating location and truncation by the computer program.
[0066] Since function call expressions may contain nested sub-function call expressions, the function parameter range within the function parameter range is further scanned to identify any nested function calls. The nested functions within the function parameter range are then recursively located until all levels of function call expressions are located, thereby obtaining all function call expressions and their corresponding function parameter ranges in the multi-level structure.
[0067] For example, let's take the original SQL query statement generated by SQLite3 as an example: SELECT COALESCE(t0.c0+3,LOWER(t0.c1)),ROUND(t0.c2 / 2,1) FROM t0 First, the original SQL query statement is scanned and matched based on the function names in the function semantic constraint library.
[0068] When the function COALESCE is identified, its function call range is determined to be COALESCE(t0.c0+3,LOWER(t0.c1)), and its function parameter range is determined to be (t0.c0+3,LOWER(t0.c1)) by matching brackets; Continue scanning the function within the function parameter range. When the nested function LOWER is identified, determine its function call range as LOWER(t0.c1) and its function parameter range as (t0.c1).
[0069] Then, the original SQL query statement was scanned. When the function ROUND was identified, its function call range was determined to be ROUND(t0.c2 / 2,1) and its function parameter range was (t0.c2 / 2,1).
[0070] After completing the syntax parsing, we obtain all the reconfigurable function call expressions and their corresponding function parameter ranges in the original SQL query statement.
[0071] Furthermore, in one embodiment, function semantic equivalence reconstruction refers to performing semantically preserved equivalence reconstruction on the function call expression in the original SQL query statement based on a set of function semantic equivalence reconstruction rules, so as to generate variant SQL query statements with the same semantics but different expression forms.
[0072] It's important to note that the function parameter expression refers to the specific code text within the parentheses of the function call expression; while the function parameter range refers to the range of character positions of that code text within the SQL statement string. Essentially, they refer to the same object. The function parameter range is used to define the physical boundaries of the parameters to facilitate recursive scanning of nested functions; the function parameter expression is used for subsequent placeholder folding and reconstruction operations.
[0073] The semantic equivalence reconstruction of the function call expression includes: S501: Fold the original function parameter expression in the function call expression into placeholders.
[0074] In step S501, the function parameter expression undergoes structure-preserving placeholder replacement processing, folding the function parameter expression into a placeholder representation.<arg_i> Where i is the parameter index, i∈{1,2,…,n}, used to distinguish different parameter expressions in the same function call; each placeholder<arg_i> Establish a one-to-one mapping relationship between the original function parameter expression and the original function parameter expression.
[0075] The placeholder is used to isolate the function parameter expression from the function structure during the semantic equivalence reconstruction process, so that the semantic reconstruction mainly acts on the function structure itself, thereby avoiding semantic interference caused by the complexity of the function parameter expression or the nested structure.
[0076] S502: Based on the set of function semantic equivalence reconstruction rules, the structure of the folded function call expression is reconstructed to generate an intermediate SQL expression.
[0077] In step S502, based on the set of function semantic equivalence reconstruction rules, the function call expression is matched, and under the premise of satisfying parameter constraints and domain conditions, the corresponding function semantic equivalence reconstruction rule is selected to perform semantic equivalence reconstruction on the function call structure, generating an intermediate SQL expression containing placeholders.
[0078] When multiple candidate function semantically equivalent reconstruction rules exist, rule selection can be performed based on a preset rule selection strategy. The preset rule selection strategy includes a random selection strategy, a coverage priority strategy, or a complexity control strategy to generate diverse variant SQL query statements.
[0079] For a random selection strategy: For example, when the COALESCE function matches three candidate rules, one of them is randomly selected using a random number generator. For instance, the alias replacement rule might be selected in this test, and the conditional semantic replacement rule in the next test. This helps increase the diversity of variant SQL queries, avoids a monotonous test path, and prevents specific defects from being missed due to fixed rules.
[0080] For a coverage-first strategy: for example, analyze the historical usage frequency of each rule and prioritize rules with fewer usages. If rule A has been used 100 times, while rule B has only been used 10 times, then rule B should be selected. This helps cover more internal code paths within the database, improves the comprehensiveness of defect detection, and reduces testing blind spots caused by uneven rule usage.
[0081] For complexity control strategies: For example, when evaluating the length or nesting level of the refactored SQL, prioritize rules that generate shorter intermediate SQL expressions. If rule A generates 10 lines of code and rule B generates 100 lines, choose rule A. This helps prevent overly complex SQL from causing execution timeouts or parsing errors, ensuring the stability and efficiency of the testing process. S503: Replace the placeholder back with the original function parameter expression to obtain the variant SQL query statement.
[0082] In step S503, the placeholders in the intermediate SQL expression are replaced back with the corresponding original function parameter expressions to restore the complete SQL query statement structure and obtain the variant SQL query statement.
[0083] Optionally, for function parameter expressions containing nested function calls, function semantic equivalence refactoring can be performed recursively in either an inside-out or outside-in order to generate multi-level semantic equivalence variant expressions.
[0084] Through the above processing, the generated variant SQL query statement satisfies the following semantic equivalence relation:
[0085] in, This represents a function call expression. This represents the function call expression generated based on the set of function semantic equivalence reconstruction rules, and E represents the function parameter expression.
[0086] Taking the original SQL query statement as an example: SELECT COALESCE(t0.c0+3,LOWER(t0.c1)),ROUND(t0.c2 / 2,1) FROM t0 First, placeholder replacement is performed on the function parameter expression to obtain the intermediate expression: SELECT COALESCE(<arg_1> ,<arg_2> ), ROUND(<arg_3> ,1) FROM t0; in,<arg_1> =t0.c0+3,<arg_2> =LOWER(t0.c1),<arg_3> =t0.c2 / 2.
[0087] Based on this, and using the aforementioned set of function semantic equivalence reconstruction rules, the function call expression is semantically reconstructed, transforming the COALESCE function into a conditional expression form that is semantically equivalent to it, resulting in an intermediate SQL expression containing placeholders: SELECT CASE WHEN<arg_1> IS NOT NULL THEN<arg_1> ELSE<arg_2> END, ROUND<arg_3> ,1) FROM t0; in: COALESCE(x,y) CASE WHEN x IS NOT NULL THEN x ELSE y END.
[0088] In this embodiment, only the COALESCE function is reconstructed semantically, while the LOWER and ROUND functions are not reconstructed. This is because no corresponding rule is matched in the current set of semantically equivalent reconstruction rules, or the preset rule selection strategy does not select them for reconstruction.
[0089] Then, the placeholders are replaced back with the corresponding original parameter expressions to restore the complete SQL structure, resulting in the variant SQL query statement: SELECT CASE WHEN t0.c0+3 IS NOT NULL THEN t0.c0+3 ELSE LOWER(t0.c1)END, ROUND(t0.c2 / 2,1) FROM t0; After the reconstruction is completed, a variant SQL query statement that is semantically equivalent to the original SQL query statement is obtained.
[0090] Then, semantic consistency verification is performed. Semantic consistency verification is a verification process used to determine whether the original SQL query and the variant SQL query maintain semantic equivalence after execution, thereby identifying potential logical defects in the database management system.
[0091] The execution results are collected and organized, including the result set data returned by the query, the exception information generated during the execution process, and the execution status information.
[0092] The execution results are compared and analyzed based on semantically aware consistency detection rules to determine whether the two satisfy semantic equivalence.
[0093] The semantically aware consistency detection rules include, but are not limited to: Anomaly consistency detection rules based on anomaly information are used to determine whether anomalies occur during two execution processes and whether the anomaly types are consistent. The result size consistency check rule based on the number of rows in the query result is used to determine whether the number of records in the two execution results is consistent. Order-independent equivalence rules based on result set order-independent semantics are used to compare the contents of result sets without ignoring the result order. Value normalization rules based on data representation normalization are used to normalize the result data before comparing the numerical content. Numerical semantic equivalence rules based on numerical computation error tolerance are used to determine the equivalence of numerical computation errors within a preset error range.
[0094] Further, in one embodiment, comparing the execution results includes: S601: Compare the original SQL query statement with the variant SQL query statement in terms of exception information, number of rows in the query result, content of the result set, numerical content, and numerical calculation error.
[0095] S602: If any comparison item is inconsistent, the execution result is determined to be inconsistent; otherwise, the execution result is determined to be consistent.
[0096] For exception information: For example, the original SQL query throws a "divide by zero" exception, while the variant SQL query executes successfully and returns a result. Or both throw errors, but the exception types are "syntax error" and "type mismatch" respectively. By comparing the existence and type consistency of the exceptions, inconsistencies in execution results can be determined, thereby identifying defects in the database's logic for handling exception scenarios.
[0097] Regarding the number of rows in the query results: For example, the original SQL query returns 100 rows, while the variant SQL query returns 99 rows. Even if the content of a single row is the same, the difference in the number of rows indicates a logical inequivalence. By counting the number of records in the result set, we can quickly filter out data omissions or redundancies caused by errors in filtering conditions or join logic, and determine inconsistencies in the execution results.
[0098] Regarding result set content: For example, the original SQL query returns a result set in the order (A, B), while the variant SQL query returns (B, A). Based on the order-independent equivalence rule, the sorting difference is ignored, and the content hash values are compared. If the content is the same, it is considered the same, avoiding misjudgment of defects due to sorting differences caused by different execution plans chosen by the optimizer, thus improving detection accuracy.
[0099] For numerical content: For example, a raw SQL query might return the boolean value "true", while a variant SQL query might return the integer "1". Based on value normalization rules, both are converted to a standard format before comparison. This eliminates false positives caused by differences in data type representation, ensuring that the comparison is based on semantic values rather than storage formats, thus improving the semantic accuracy of the comparison.
[0100] To address numerical calculation errors: For example, the original SQL query might result in 0.30000001, while the variant SQL query might result in 0.3. Based on numerical semantic equivalence rules, an error tolerance range of 1e-6 is set. If the difference is within this range, the query is considered equivalent. This resolves misjudgments caused by differences in floating-point arithmetic precision, adapts to the underlying computational characteristics of the database, and ensures the robustness of logical defect detection.
[0101] Furthermore, in one embodiment, when an inconsistency in execution results is identified, defect report information is output. The defect report information refers to the defect-related description information identified during the execution of semantic consistency verification when the execution results of the original SQL query statement and its variant SQL query statement do not satisfy the semantic equivalence relationship.
[0102] Specifically, the information for generating the defect report includes: S701: Perform repeated execution verification on the original SQL query statement and the variant SQL query statement whose execution results are inconsistent.
[0103] The original SQL query and its variant that showed inconsistent execution results were executed repeatedly to verify the reproducibility of the inconsistent results, thereby eliminating occasional differences caused by fluctuations in the execution environment or uncertain factors.
[0104] S702: Delete the non-critical structure of the original SQL query statement and generate a minimal defect-reproducing SQL query statement that can reproduce inconsistent execution results.
[0105] Provided that the inconsistent results are confirmed to be stable and reproducible, the original SQL query statement is simplified.
[0106] The reduction process is achieved by progressively deleting, replacing, or simplifying non-critical structures in the original SQL query statement, including but not limited to nested function structures, compound expressions, and redundant clauses, in order to reduce the complexity of the SQL query statement and narrow the scope of defect triggering, thereby generating a minimal SQL query statement for defect reproduction.
[0107] For example, starting with the original SQL query that triggered inconsistent execution results, redundant clauses, table joins, or nested structures unrelated to the defect are progressively removed. After each removal step, a corresponding variant SQL query is regenerated based on the simplified original SQL query, or the variant SQL query undergoes the same simplification process simultaneously, and a comparison verification is performed again. If the execution results of the two simplified queries are still inconsistent, the simplified structure is retained; if the results tend to be consistent, the removal operation is rolled back. Ultimately, a minimal defect-reproducing SQL query is generated that can stably reproduce the defect while maintaining the simplest structure, facilitating developers to quickly locate errors in the function implementation logic.
[0108] During the reduction process, key function calls and their parameter expressions that affect defect triggering are preserved, and controlled reconstruction is performed when necessary in conjunction with the function semantic equivalence reconstruction rules to ensure that the reduced SQL query statement can still trigger the inconsistent execution result behavior.
[0109] After completing the reproduction verification and reduction process, the corresponding defect report information is output.
[0110] S703: Output defect report information including the original SQL query statement, variant SQL query statement, minimized defect reproduction SQL query statement, inconsistent execution results, and corresponding difference description information.
[0111] Secondly, embodiments of this application also provide a database defect detection device based on function semantic equivalent substitution.
[0112] In one embodiment, reference is made to Figure 2 , Figure 2 This is a functional module diagram of an embodiment of the database defect detection device based on function semantic equivalence substitution according to this application. Figure 2 As shown, the database defect detection device based on function semantic equivalent substitution includes: An acquisition module is used to acquire a raw SQL query statement, wherein the raw SQL query statement contains at least one function call expression; A positioning module, used to locate function call expressions in the original SQL query statement; The reconstruction module is used to perform semantic equivalence reconstruction on the function call expression based on a preset set of function semantic equivalence reconstruction rules, and generate variant SQL query statements; An execution module is used to execute the original SQL query statement and the variant SQL query statement respectively, and obtain the corresponding execution results; The generation module is used to compare the execution results, and if they are inconsistent, it generates defect report information.
[0113] Furthermore, in one embodiment, the database defect detection device based on function semantic equivalence substitution further includes a construction module, used for: extracting function names, parameter constraints, and function description information from the official function documentation of the database; parsing semantic information based on the function description information; and storing the function names, parameter constraints, and semantic information in a structured manner to obtain a function semantic constraint library.
[0114] Furthermore, in one embodiment, the construction module is also used to extract the input-output relationship and behavioral features of the function from the semantic information in the function semantic constraint library; identify candidate equivalent expressions based on the semantic information, and verify whether the candidate expressions are semantically equivalent under the premise of satisfying parameter constraints; classify the verified candidate expressions to generate a set of function semantic equivalence reconstruction rules.
[0115] Furthermore, in one embodiment, the positioning module locates the function call expression in the original SQL query statement, including: scanning the original SQL query statement based on the function names in the function semantic constraint library to identify the function call position; determining the starting and ending parentheses of the function call position to obtain the function call interval; and determining the content within the function call interval as the function parameter interval.
[0116] Furthermore, in one embodiment, the reconstruction module performs semantic equivalence reconstruction on the function call expression, including: folding the original function parameter expression in the function call expression into placeholders; performing structural reconstruction on the folded function call expression based on the set of semantic equivalence reconstruction rules to generate an intermediate SQL expression; and replacing the placeholders back with the original function parameter expression to obtain the variant SQL query statement.
[0117] Furthermore, in one embodiment, the generation module compares the execution results, including: comparing the exception information, number of rows in the query results, result set content, numerical content, and numerical calculation error between the original SQL query statement and the variant SQL query statement; if any comparison item is inconsistent, it is determined that the execution results are inconsistent.
[0118] Furthermore, in one embodiment, the generation module generates defect report information, including: repeatedly executing and verifying the original SQL query statement and the variant SQL query statement with inconsistent execution results; deleting non-critical structures of the original SQL query statement to generate a minimal defect reproduction SQL query statement that can reproduce the inconsistent execution results; and outputting defect report information containing the original SQL query statement, the variant SQL query statement, the minimal defect reproduction SQL query statement, the inconsistent execution results, and the corresponding difference description information.
[0119] Further, in one embodiment, the acquisition module acquires the original SQL query statement by: collecting SQL query statements from database historical query logs, open-source test case libraries, or public benchmark test sets, or generating SQL query statements based on SQL query statement generation methods; and filtering out SQL query statements containing at least one function call expression as the original SQL query statement. The functions of each module in the database defect detection device based on function semantic equivalent substitution correspond to the steps in the database defect detection method embodiment based on function semantic equivalent substitution. Their functions and implementation processes will not be described in detail here.
[0120] Thirdly, embodiments of this application provide a database defect detection device based on function semantic equivalent substitution. The database defect detection device based on function semantic equivalent substitution can be a personal computer (PC), laptop computer, server, or other device with data processing capabilities.
[0121] Reference Figure 3 , Figure 3 This is a schematic diagram of the hardware structure of a database defect detection device based on function semantic equivalence substitution, as described in an embodiment of this application. In this embodiment, the database defect detection device based on function semantic equivalence substitution may include a processor, a memory, a communication interface, and a communication bus.
[0122] The communication bus can be of any type and is used to interconnect the processor, memory, and communication interface.
[0123] The communication interface includes input / output (I / O) interfaces, physical interfaces, and logical interfaces used for interconnecting internal components of the database defect detection device based on function semantic equivalence substitution, as well as interfaces used for interconnecting the database defect detection device with other devices (such as other computing devices or user equipment). Physical interfaces can be Ethernet interfaces, fiber optic interfaces, ATM interfaces, etc.; user equipment can be displays, keyboards, etc.
[0124] Memory can be various types of storage media, such as random access memory (RAM), read-only memory (ROM), non-volatile RAM (NVRAM), flash memory, optical storage, hard disk, programmable ROM (PROM), erasable PROM (EPROM), electrically erasable PROM (EEPROM), etc.
[0125] The processor can be a general-purpose processor, which can call a database defect detection program based on function semantic equivalence substitution stored in memory and execute the database defect detection method based on function semantic equivalence substitution provided in the embodiments of this application. For example, the general-purpose processor can be a central processing unit (CPU). The method executed when the database defect detection program based on function semantic equivalence substitution is called can be referred to in the various embodiments of the database defect detection method based on function semantic equivalence substitution in this application, and will not be repeated here.
[0126] Those skilled in the art will understand that Figure 3 The hardware structure shown does not constitute a limitation of this application and may include more or fewer components than shown, or combine certain components, or have different component arrangements.
[0127] Fourthly, embodiments of this application also provide a computer-readable storage medium.
[0128] This application provides a computer-readable storage medium storing a database defect detection program based on function semantic equivalence substitution, wherein when the database defect detection program based on function semantic equivalence substitution is executed by a processor, it implements the steps of the database defect detection method based on function semantic equivalence substitution as described above.
[0129] The method implemented when the database defect detection program based on function semantic equivalent substitution is executed can be referred to in the various embodiments of the database defect detection method based on function semantic equivalent substitution in this application, and will not be repeated here.
[0130] It should be noted that the sequence numbers of the embodiments in this application are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.
[0131] The terms "comprising" and "having," and any variations thereof, in the specification, claims, and accompanying drawings of this application are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or apparatus that includes a series of steps or units is not limited to the listed steps or units, but may optionally include steps or units not listed, or may optionally include other steps or units inherent to such process, method, product, or apparatus. The terms "first," "second," and "third," etc., are used to distinguish different objects, etc., and do not indicate a sequence, nor do they limit "first," "second," and "third" to different types.
[0132] In the description of the embodiments of this application, terms such as "exemplary," "for example," or "for instance" are used to indicate examples, illustrations, or explanations. Any embodiment or design described as "exemplary," "for example," or "for instance" in the embodiments of this application should not be construed as being more preferred or advantageous than other embodiments or designs. Specifically, the use of terms such as "exemplary," "for example," or "for instance" is intended to present the relevant concepts in a concrete manner.
[0133] In the description of the embodiments of this application, unless otherwise stated, " / " means "or". For example, A / B can mean A or B. The "and / or" in the text is merely a description of the relationship between related objects, indicating that there can be three relationships. For example, A and / or B can mean: A exists alone, A and B exist simultaneously, and B exists alone. In addition, in the description of the embodiments of this application, "multiple" means two or more.
[0134] In some processes described in the embodiments of this application, multiple operations or steps are included in a specific order. However, it should be understood that these operations or steps may not be executed in the order they appear in the embodiments of this application, or they may be executed in parallel. The sequence number of the operation is only used to distinguish different operations, and the sequence number itself does not represent any execution order. In addition, these processes may include more or fewer operations, and these operations or steps may be executed sequentially or in parallel, and these operations or steps may be combined.
[0135] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods of the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) as described above, and includes several instructions to cause a terminal device to execute the methods described in the various embodiments of this application.
[0136] The above are merely preferred embodiments of this application and do not limit the patent scope of this application. Any equivalent structural or procedural transformations made using the content of this application's specification and drawings, or direct or indirect applications in other related technical fields, are similarly included within the patent protection scope of this application.
Claims
1. A database defect detection method based on functional semantic equivalent substitution, characterized in that, include: Obtain the original SQL query statement, which contains at least one function call expression; Locate the function call expression in the original SQL query statement; Based on a pre-defined set of function semantic equivalence reconstruction rules, the function call expression is semantically reconstructed to generate variant SQL query statements; Execute the original SQL query statement and the variant SQL query statement respectively, and obtain their respective execution results; If the execution results are inconsistent, a defect report is generated.
2. The database defect detection method according to claim 1, characterized in that, The set of function semantic equivalence reconstruction rules is generated based on the function semantic constraint library, which stores function names, parameter constraints, and semantic information.
3. The database defect detection method according to claim 2, characterized in that: The function semantic constraint library is constructed through the following steps: extracting function names, parameter constraints, and function descriptions from official function documentation in the database; parsing the function descriptions to obtain semantic information; and storing the function names, parameter constraints, and semantic information in a structured manner to obtain the function semantic constraint library. And / or, the set of function semantic equivalence reconstruction rules is generated through the following steps: extracting the input-output relationship and behavioral features of the function from the semantic information in the function semantic constraint library; Based on the semantic information, candidate equivalent expressions are identified, and the semantic equivalence between candidate expressions is verified under the premise of satisfying parameter constraints. The verified candidate expressions are classified to generate a set of function semantic equivalence reconstruction rules. And / or, locating the function call expression in the original SQL query statement includes: scanning the original SQL query statement based on the function names in the function semantic constraint library to identify the function call location; determining the opening and closing parentheses of the function call location to obtain the function call interval; and determining the content within the function call interval as the function parameter interval.
4. The database defect detection method according to claim 1, characterized in that, The semantic equivalence reconstruction of the function call expression includes: The original function parameter expression in the function call expression is collapsed into placeholders; Based on the set of function semantic equivalence reconstruction rules, the structure of the folded function call expression is reconstructed to generate an intermediate SQL expression; The placeholders are replaced back with the original function parameter expressions to obtain the variant SQL query statement.
5. The database defect detection method according to claim 1, characterized in that, The comparison of the execution results includes: Compare the original SQL query statement with the variant SQL query statement in terms of exception information, number of rows in the query result, content of the result set, numerical content, and numerical calculation error; If any comparison item is inconsistent, the execution result is determined to be inconsistent.
6. The database defect detection method according to claim 1, characterized in that, The information for generating the defect report includes: The original SQL query statement and the variant SQL query statement with inconsistent execution results are verified by repeated execution. Remove non-critical structures from the original SQL query statement to generate a minimal defect-reproducible SQL query statement that can reproduce inconsistent execution results. The output includes the original SQL query statement, the variant SQL query statement, the SQL query statement for minimizing defect reproduction, inconsistent execution results, and corresponding difference description information in the defect report information.
7. The database defect detection method according to claim 1, characterized in that, The process of obtaining the original SQL query statement includes: Collect SQL query statements from database historical query logs, open-source test case libraries, or public benchmark sets, or generate SQL query statements based on SQL query statement generation methods; Select SQL query statements that contain at least one function call expression as the original SQL query statement.
8. A database defect detection device based on functional semantic equivalent substitution, characterized in that, include: An acquisition module is used to acquire a raw SQL query statement, wherein the raw SQL query statement contains at least one function call expression; A positioning module, used to locate function call expressions in the original SQL query statement; The reconstruction module is used to perform semantic equivalence reconstruction on the function call expression based on a preset set of function semantic equivalence reconstruction rules, and generate variant SQL query statements; An execution module is used to execute the original SQL query statement and the variant SQL query statement respectively, and obtain the corresponding execution results; The generation module is used to compare the execution results, and if they are inconsistent, it generates defect report information.
9. A database defect detection device based on functional semantic equivalent substitution, characterized in that, The system includes a processor, a memory, and a database defect detection program based on functional semantic equivalence substitution stored in the memory and executable by the processor, wherein when the database defect detection program based on functional semantic equivalence substitution is executed by the processor, it implements the steps of the database defect detection method based on functional semantic equivalence substitution as described in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, It stores a database defect detection program based on function semantic equivalent substitution, wherein when the database defect detection program based on function semantic equivalent substitution is executed by a processor, it implements the steps of the database defect detection method based on function semantic equivalent substitution as described in any one of claims 1 to 7.
