A configuration-driven SQL semantic relationship construction device and method
Through the configuration-driven SQL semantic relationship construction device and method, the challenge of semantic parsing in database fuzz testing is solved, the semantic pass rate and testing efficiency are improved, the workload is reduced, and more efficient database operations are achieved.
Patent Information
- Application Number
- CN202410671660.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-28
- Publication Date
- 2024-10-18
- Estimated Expiration
- 2044-05-28
AI Technical Summary
Existing database fuzz testing technology has challenges in semantic parsing, resulting in test cases that cannot accurately simulate real database operation scenarios, and adapting to new database semantics requires a lot of labor costs.
Provides a configuration-driven SQL semantic relationship construction device and method. Through the semantic configuration parser, syntax parser and syntax tree builder, the SQL statement is converted into an abstract syntax tree, the semantic relationship graph is sorted out, and semantic information collection, processing and Combining modules to implement configurable SQL semantic relationships.
It improves the semantic pass rate during database fuzz testing, reduces the workload of writing semantic parsing modules, and avoids the overhead of repeated analysis and construction of semantic relationship graphs by predicting the semantic relationship of the next SQL sentence.
Smart Images

Figure CN118484197B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to the field of fuzzy testing technology, and in particular to a configuration-driven SQL semantic relationship construction device and method. Background Art
[0002] With the continuous development of fuzz testing technology, more and more databases are beginning to use fuzz testing technology to improve test efficiency and coverage. For database fuzz testing, it is crucial to ensure the correctness of SQL (Structured Query Language) syntax and semantics. Only when the SQL syntax and semantic specifications of the data are correctly adapted can the coverage of the test code be better improved, and the problem that the test cases generated by the test tool are not sufficient to cover various complex and diverse situations can be improved.
[0003] In terms of syntax, current database fuzz testing technology has made significant progress and can effectively handle the SQL syntax specifications of various databases. However, current database fuzz testing technology still faces challenges in semantic parsing. On the one hand, some database fuzz testing tools do not support semantic parsing well, resulting in the inability of the generated test cases to accurately simulate real database operation scenarios. On the other hand, although some database fuzz testing tools have developed unique semantic parsing modules for certain databases, this method has a large workload and requires a lot of manpower costs when adapting to new database semantics.
[0004] Improving the semantic parsing capability of database fuzz testing technology for different databases is one of the issues that need to be focused on at present. Only by overcoming the challenges in database semantic parsing can the efficiency and coverage of fuzz testing be further improved, thereby better ensuring the stability and security of the database system. Therefore, it is necessary to provide a configuration-driven SQL semantic relationship construction device and method to solve the above problems. Summary of the invention
[0005] The present invention provides a configuration-driven SQL semantic relationship construction device and method, which improves the semantic pass rate in the database fuzzy testing process and reduces the workload of writing a semantic parsing module of a supporting database by making the SQL semantic relationship configurable.
[0006] An embodiment of the present invention provides a configuration-driven SQL semantic relationship construction device, comprising:
[0007] A semantic configuration parser, which is used to parse the pre-configured semantic relationship configuration file into a data structure and a grammar file, and generate a semantic information collection module, a semantic information processing module and a semantic information combing module according to the data structure and the grammar file;
[0008] A syntax parser, which is used to parse SQL statements into NODE nodes, wherein the NODE nodes are user-defined data structures;
[0009] A syntax tree builder, which is used to convert the NODE node into an abstract syntax tree to convert the SQL statement into a binary tree data structure;
[0010] The semantic information collection module is used to traverse the abstract syntax tree, sort out the relationships between different leaf nodes into a semantic relationship graph, and put terminal nodes representing different semantics into different arrays;
[0011] The semantic information processing module is used to complete the semantic information in the SQL statement according to the semantic relationship graph, the semantic relationship graph generates a first name or a second name for the leaf node, the first name is a new name, and the second name is an existing name; when the first name is used, new semantic information is generated for the leaf node; when the second name is used, the semantic information is divided into first semantic information, second semantic information and third semantic information according to the semantic relationship graph, the first semantic information is semantic information that has no dependence on other semantic information, the second semantic information is semantic information that has dependence on other semantic information, and the third semantic information is semantic information of a special sentence pattern;
[0012] The semantic information combing module is used to comb the semantic relationship diagram between the clause and itself, the clause and the main clause, and different SQL statements, and split the SQL statement into smaller elements. If the smaller elements have a semantic impact on the clause itself, the smaller elements are stored in a temporary shared memory, waiting to be used by the semantic information processing module.
[0013] Preferably, in the same SQL statement, the SQL statement includes a main clause and a sub-clause, the main clause is used to describe the attributes, constraints and filtering conditions of the data, and the sub-clause is used to filter the main clause; in different SQL statements, the semantic relationship is used to describe the logical or operational relationship between the different SQL statements.
[0014] Preferably, the semantic relationship configuration file is pre-configured according to a grammar file, and the semantic information includes table names and column names in the SQL statement.
[0015] Preferably, the semantic information combing module includes:
[0016] A subquery information transmission unit, which is used to sort out the types of query results obtained in the subquery and the names of available data;
[0017] A joint query information transmission unit, which is used to sort out the relationship between the table name, column name, type, function calculation result and result alias in the joint query;
[0018] A group query information transmission unit, which is used to transmit the result information of the aggregate function to the main clause;
[0019] The window function clause information transmission unit is used to transmit the result information of the window function and the columns used for grouping in the clause to the main clause.
[0020] Preferably, the semantic information combing module is used to pass the semantic information in different SQL statements downward in the execution order of the SQL statements, and save the results of the changes to the database after the SQL statements are executed, and the results of the changes include deleting the table name, adding columns, and modifying the foreign keys of columns.
[0021] Preferably, the semantic information combing module is also used to analyze the semantic relationship graph of each SQL statement when processing SQL statements that will change the database, predict and filter out the semantic relationship required for the next SQL statement based on the type and context of the current SQL statement, and store it in a temporary variable.
[0022] Preferably, determining whether the current rule is in the beginning stage, the middle stage or the ending stage;
[0023] When the current rule is in the start phase, a new array is created to store all semantic relationship type structures of the current clause;
[0024] When the current rule is in the middle stage, the semantic relationship type structure of the upper layer is used;
[0025] When the current rule is at the end stage, a corresponding terminal symbol semantic collection code is generated.
[0026] Preferably, the semantic relationship configuration file includes a first configuration item, a second configuration item, a third configuration item, a fourth configuration item, and a fifth configuration item. The name of the non-terminal symbol in the grammar file is read, and the relevant code of the position of the IR node of the non-terminal symbol in the abstract syntax tree is generated according to the first configuration item to obtain the second configuration item. The third configuration item indicates the specific meaning or type of the non-terminal symbol. The fourth configuration item indicates that the non-terminal symbol needs to generate the first name or the second name. The fifth configuration item indicates which type of SQL statement the non-terminal symbol belongs to.
[0027] Preferably, when the semantic information is the first semantic information, the existing list and index information in the database are selected; when the semantic information is the second semantic information, the corresponding dependent processed list and index information in the database are selected; when the semantic information is the third semantic information, it is processed according to the general SQL sentence rules.
[0028] The embodiment of the present invention also provides a configuration-driven SQL semantic relationship construction method, including:
[0029] Parse the pre-configured semantic relationship configuration files into data structure and grammar files;
[0030] Parse the SQL statement into a NODE node, where the NODE node is a custom data structure;
[0031] Convert the NODE node into an abstract syntax tree to convert the SQL statement into a binary tree data structure;
[0032] Traversing the abstract syntax tree, sorting the relationships between different leaf nodes into a semantic relationship graph, and placing terminal nodes representing different semantics into different arrays;
[0033] The semantic information in the SQL statement is filled in completely according to the semantic relationship graph, and the semantic relationship graph generates a first name or a second name for the leaf node, wherein the first name is a new name and the second name is an existing name; when the first name is used, new semantic information is generated for the leaf node; when the second name is used, the semantic information is divided into first semantic information, second semantic information and third semantic information according to the semantic relationship graph, wherein the first semantic information is semantic information that has no dependency on other semantic information, the second semantic information is semantic information that has dependency on other semantic information, and the third semantic information is semantic information of a special sentence pattern;
[0034] The semantic relationship graph between the clause and itself, the clause and the main clause, and different SQL statements is sorted out, and the SQL statement is split into smaller elements. If the smaller elements have a semantic impact on the clause itself, the smaller elements are stored in a temporary shared memory.
[0035] Compared with the prior art, the technical solution of the embodiment of the present invention has the following beneficial effects:
[0036] The configuration-driven semantic relationship construction device and method of the embodiment of the present invention include: a semantic configuration parser, which is used to parse a pre-configured semantic relationship configuration file into a data structure and a syntax file, and generate a semantic information collection module, a semantic information processing module and a semantic information combing module according to the data structure and the syntax file; a syntax parser, which is used to parse an SQL statement into a NODE node, and the NODE node is a custom data structure; a syntax tree builder, which is used to convert the NODE node into an abstract syntax tree to convert the SQL statement into a binary tree data structure; the semantic information collection module is used to traverse the abstract syntax tree, comb the relationship between different leaf nodes into a semantic relationship graph, and put the terminal symbol nodes representing different semantics into different arrays; the semantic information processing module is used to complete the semantic information in the SQL statement according to the semantic relationship graph, and the semantic relationship graph generates a first name or a second name for the leaf node, and the The first name is a new name, and the second name is an existing name; when the first name is used, new semantic information is generated for the leaf node; when the second name is used, the semantic information is divided into first semantic information, second semantic information and third semantic information according to the semantic relationship graph, the first semantic information is semantic information that has no dependence on other semantic information, the second semantic information is semantic information that has dependence on other semantic information, and the third semantic information is semantic information of a special sentence pattern; the semantic information combing module is used to comb the semantic relationship graph between the clause and itself, the clause and the main clause, and different SQL statements, and split the SQL statement into smaller elements. If the smaller element has a semantic impact on the clause itself, the smaller element is stored in a temporary shared memory, waiting for the semantic information processing module to use it. By making the SQL semantic relationship configurable, the semantic pass rate in the database fuzzy test process is improved and the workload of the semantic parsing module for writing the supporting database is reduced;
[0037] Furthermore, the semantic information combing module is also used to analyze the semantic relationship graph of each SQL statement when processing SQL statements that will change the database, predict and screen out the semantic relationship required for the next SQL statement based on the type and context of the current SQL statement, and store it in a temporary variable. When the next SQL statement arrives, the required semantic relationship can be quickly extracted from the temporary variable, avoiding the overhead of repeated analysis and construction of the semantic relationship graph. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following is a brief introduction to the drawings required for use in the embodiments or the prior art descriptions. Obviously, the drawings described below are some embodiments of the present invention, not all embodiments. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative labor.
[0039] Figure 1 A schematic diagram of the structure of a configuration-driven SQL semantic relationship building device provided by an embodiment of the present invention;
[0040] Figure 2 A schematic diagram of the structure of a configuration-driven SQL semantic relationship building device provided by another embodiment of the present invention;
[0041] Figure 3 A schematic diagram of the structure of a semantic information combing module in a configuration-driven SQL semantic relationship building device provided by an embodiment of the present invention;
[0042] Figure 4 A schematic diagram of a flow chart of a configuration-driven SQL semantic relationship construction method provided by an embodiment of the present invention;
[0043] Figure 5 A schematic diagram of processing grammatical information in a configuration-driven SQL semantic relationship building device provided by an embodiment of the present invention;
[0044] Figure 6 A configuration example of a semantic relationship configuration file in a configuration-driven SQL semantic relationship construction device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0045] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0046] The technical solution of the present invention is described in detail with specific embodiments below. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described in detail in some embodiments.
[0047] Based on the problems existing in the prior art, an embodiment of the present invention provides a configuration-driven SQL semantic relationship construction device and method, which improves the semantic pass rate in the database fuzzy testing process and reduces the workload of writing a semantic parsing module for the supporting database by making the SQL semantic relationship configurable.
[0048] Figure 1 A schematic diagram of the structure of a configuration-driven SQL semantic relationship building device provided by an embodiment of the present invention, Figure 2 A schematic diagram of the structure of a configuration-driven SQL semantic relationship building device provided by another embodiment of the present invention. Figure 1 and Figure 2 The embodiment of the present invention provides a configuration-driven SQL semantic relationship construction device, including:
[0049] The semantic configuration parser 11 is used to parse the pre-configured semantic relationship configuration file into a data structure and a grammar file, and generate a semantic information collection module 111, a semantic information processing module 112 and a semantic information combing module 113 according to the data structure and the grammar file.
[0050] Specifically, a semantic relationship configuration file is pre-configured, semantic names such as table names and column names in SQL statements such as create, drop, update, select, etc. in the grammar file are marked in the grammar file, and various semantic rules, such as type checking, expression calculation, and data verification, are configured after the corresponding grammar rules.
[0051] The syntax parser 12 is used to parse the SQL statement into a NODE node, where the NODE node is a user-defined data structure.
[0052] Specifically, a NODE node is a self-defined data structure, and different NODE nodes have different node types, but the parent class is all of NODE type. The syntax parser 12 can decompose a complex SQL statement into a group of NODE nodes, and provides a basis for subsequent semantic analysis and test case generation.
[0053] The syntax tree builder 13 is used to convert the NODE node into an Abstract Syntax Tree (AST) so as to convert the SQL statement into a binary tree data structure.
[0054] The semantic information collection module 111 is used to traverse the abstract syntax tree, sort out the relationships between different leaf nodes into a semantic relationship graph, and put terminal nodes representing different semantics into different arrays.
[0055] Specifically, the semantic information collection module 111 is used to collect leaf nodes such as tables and columns in the abstract syntax tree AST that represent SQL semantics, and mainly includes the following steps:
[0056] Traverse the entire abstract syntax tree AST and collect the corresponding leaf nodes representing semantics when traversing to them.
[0057] The terminal nodes representing different semantics are placed into different arrays to sort out the relationship between the terminal nodes.
[0058] The relationships between different leaf nodes are organized into a semantic relationship graph, which represents the relationships between these leaf nodes. For example, if a represents a table name and b represents a column name under a, the semantic information collection module 111 will construct a table-column relationship between a and b, and put the table-column relationship into the semantic relationship graph.
[0059] The semantic information processing module 112 is used to complete the semantic information in the SQL statement according to the semantic relationship graph, the semantic relationship graph generates a first name or a second name for the leaf node, the first name is a new name, and the second name is an existing name; when the first name is used, new semantic information is generated for the leaf node; when the second name is used, the semantic information is divided into first semantic information, second semantic information and third semantic information according to the semantic relationship graph, the first semantic information is semantic information that has no dependence on other semantic information, the second semantic information is semantic information that has dependence on other semantic information, and the third semantic information is semantic information of a special sentence.
[0060] Specifically, the steps include:
[0061] According to the semantic relationship graph, the collected nodes are divided into those that need to generate a new name and those that use existing names.
[0062] Generate new table names, column names or other database information based on the nodes in the semantic relationship graph that need to generate new names. The new names are generated using a custom naming algorithm.
[0063] When processing using an existing name, according to the semantic relationship diagram of the semantic information collection module 111, the semantic information is divided into semantic information that has no dependency on other semantic information, semantic information that has dependency on other semantic information and semantic information belonging to a special sentence pattern.
[0064] The semantic information combing module 113 is used to comb the semantic relationship diagram between the clause and itself, the clause and the main clause, and different SQL statements, and split the SQL statement into smaller elements. If the smaller elements have a semantic impact on the clause itself, the smaller elements are stored in a temporary shared memory, waiting to be used by the semantic information processing module 112.
[0065] Specifically, the semantic relationship between a clause and itself, between a clause and the main clause, and between different SQL statements is as follows:
[0066] The semantic relationship between the clause and itself:
[0067] In the same SQL statement, there are specific semantic relationships between different clauses (such as SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, etc.), which are used to describe different operations and constraints.
[0068] The semantic relationship between the clause and the main clause:
[0069] In an SQL query statement, the semantic relationship between the clause and the main clause describes the attributes, constraints, and filtering conditions of the data set that the main clause (such as SELECT) is going to operate on. The clause provides the function of more precise operation and filtering of the main clause.
[0070] Semantic relationship between different SQL statements:
[0071] The semantic relationship between different SQL statements describes the logical or operational relationship between them. For example, a CREATE TABLE statement will create a table in the database after it is successfully executed, which affects the table that can be queried in the SELECT statement. Then the SELECT statement will have a table dependency on the CREATE TABLE statement, that is, the CREATE TABLE statement must be successfully executed first to create a table before the SELECT statement can start looking for data in the table.
[0072] In a specific implementation, in the same SQL statement, the SQL statement includes a main clause and a sub-clause, the main clause is used to describe the attributes, constraints and filtering conditions of the data, and the sub-clause is used to filter the main clause; in different SQL statements, the semantic relationship is used to describe the logical or operational relationship between the different SQL statements.
[0073] In a specific implementation, the semantic relationship configuration file is pre-configured according to a grammar file, and the semantic information includes table names and column names in the SQL statement.
[0074] Figure 3A schematic diagram of the structure of a semantic information combing module in a configuration-driven SQL semantic relationship building device provided by an embodiment of the present invention. Figure 3 The embodiment of the present invention provides a configuration-driven SQL semantic relationship construction device, including:
[0075] A semantic configuration parser 11, which is used to parse the pre-configured semantic relationship configuration file into a data structure and a grammar file, and generate a semantic information collection module 111, a semantic information processing module 112 and a semantic information combing module 113 according to the data structure and the grammar file;
[0076] A syntax parser 12, which is used to parse SQL statements into NODE nodes, wherein the NODE nodes are user-defined data structures;
[0077] A syntax tree builder 13, which is used to convert the NODE node into an abstract syntax tree to convert the SQL statement into a binary tree data structure;
[0078] The semantic information combing module 113 includes:
[0079] A subquery information transmission unit 1131, which is used to sort out the types of query results obtained in the subquery and the names of available data;
[0080] A joint query information transmission unit 1132, which is used to sort out the relationship between the table name, column name, type, function calculation result and result alias in the joint query;
[0081] A group query information transmission unit 1133, which is used to transmit the result information of the aggregation function to the main clause;
[0082] The window function clause information transmission unit 1134 is used to transmit the result information of the window function and the columns used for grouping in the clause to the main clause.
[0083] In a specific implementation, the semantic information combing module 113 is used to pass the semantic information in different SQL statements downward in the execution order of the SQL statements, and save the results of the changes to the database after the SQL statements are executed. The results of the changes include deleting the table name, adding columns, and modifying the foreign keys of the columns.
[0084] In a specific implementation, the semantic information combing module 113 is also used to analyze the semantic relationship graph of each SQL statement when processing SQL statements that will change the database, predict and filter out the semantic relationship required for the next SQL statement based on the type and context of the current SQL statement, and store it in a temporary variable.
[0085] The semantic information combing module 113 specifically processes the SQL statement in the following manner:
[0086] Split complex SQL statements into smaller, simpler parts. First, parse out the SELECT clause, FROM clause and other parts in a most basic SQL statement, and analyze the semantics of each part to specifically represent tables, columns, indexes or other elements in the SQL statement. Store the parts of these elements that will have a semantic impact on the clause itself in a temporary shared memory, waiting for the semantic information processing module 112 to use them.
[0087] After processing the simplest clause, the semantic information combing module 113 will continue to process more complex SQL statement hierarchical structures, which may contain nested subqueries, joint queries, logical connections between clauses, and other complex semantic relationships. For these complex sentences, the semantic information combing module 113 further includes a subquery information transmission unit 1131, a joint query information transmission unit 1132, a group query information transmission unit 1133, and a window function clause information transmission unit 1134. Each unit is as follows:
[0088] Subquery information transmission unit 1131: mainly sorts out semantic information such as the type of query result obtained in the subquery, the name of the available data, etc.
[0089] The joint query information transmission unit 1132 is mainly used to sort out the relationship between the table in the joint query, the name and type of the column, the calculation result of the function and the alias of the result.
[0090] Group query information transmission unit 1133: mainly transmits the result information of the aggregation function to the main sentence.
[0091] Window function clause information transmission unit 1134: mainly transmits the result information of the window function and the columns used for grouping in the PARTITIONBY and ORDER BY clauses to the main clause, so as to help the main query better understand and process the results of the subquery.
[0092] In this way, a complete SQL statement is processed. Different SQL statements will also have an impact on each other. For example, a create table statement will have a semantic impact on select, update, alter, drop and other SQL statements. Update can only modify the table created in create table, while the columns modified in the alter statement will have a semantic impact on the columns used in select, update and other statements. At this time, the semantic information combing module 113 needs to pass the semantic information in different SQL statements downward in the SQL execution order. The results of the changes to the database after the SQL statement is executed are saved, such as deleting the table name, adding columns, modifying the foreign keys of columns, etc. When processing DDL and DML statements such as CREATE, ALETE, DELETE, UPDATE, etc. that will change the database, analyze the semantic relationship graph of each SQL statement, and according to the type of the next SQL statement, filter out the semantic relationship required for the next SQL statement and store it in a temporary variable. According to the type and context of the current SQL statement, predict and filter out the semantic relationship that may be required for the next SQL statement. For example, if the CREATE TABLE statement is currently being executed, then read the following SQL in advance and analyze the type of the following SQL, which may include INSERT, UPDATE, or DELETE, and save the semantic relationships such as fields and constraints required by these SQL in advance.
[0093] These filtered semantic relationships are stored in temporary variables for subsequent operations. When the next SQL statement arrives, the required semantic relationships can be quickly extracted from the temporary variables, avoiding the overhead of repeated analysis and construction of semantic relationship graphs.
[0094] This method of predicting and filtering semantic relationships in advance not only improves the efficiency of SQL processing, but also makes the entire database operation smoother and more accurate. Through the intervention of the SQL semantic relationship filtering module, the database management system can better understand and execute complex SQL statements, providing users with more efficient and reliable services.
[0095] In the specific implementation, it also includes determining whether the current rule is in the beginning stage, the middle stage or the ending stage;
[0096] When the current rule is in the start phase, a new array is created to store all semantic relationship type structures of the current clause;
[0097] When the current rule is in the middle stage, the semantic relationship type structure of the upper layer is used;
[0098] When the current rule is at the end stage, a corresponding terminal symbol semantic collection code is generated.
[0099] In a specific implementation, the semantic relationship configuration file includes a first configuration item, a second configuration item, a third configuration item, a fourth configuration item, and a fifth configuration item. The name of the non-terminal symbol in the grammar file is read, and the relevant code of the position of the IR (Intermediate Representation) node of the non-terminal symbol in the abstract syntax tree is generated according to the first configuration item to obtain the second configuration item. The third configuration item indicates the specific meaning or type of the non-terminal symbol. The fourth configuration item indicates that the non-terminal symbol needs to generate the first name or the second name. The fifth configuration item indicates which type of SQL statement the non-terminal symbol belongs to.
[0100] In a specific implementation, when the semantic information is the first semantic information, the existing list and index information in the database is selected; when the semantic information is the second semantic information, the corresponding dependent processed list and index information in the database is selected; when the semantic information is the third semantic information, it is processed according to the general SQL sentence rules.
[0101] Figure 4 A flowchart of a configuration-driven SQL semantic relationship construction method provided by an embodiment of the present invention. Figure 4 The embodiment of the present invention also provides a configuration-driven SQL semantic relationship construction method, including:
[0102] Step S401: parsing the pre-configured semantic relationship configuration file into a data structure and a grammar file;
[0103] Step S402: Parse the SQL statement into a NODE node, where the NODE node is a custom data structure;
[0104] Step S403: converting the NODE node into an abstract syntax tree to convert the SQL statement into a binary tree data structure;
[0105] Step S404: traverse the abstract syntax tree, sort out the relationships between different leaf nodes into a semantic relationship graph, and put terminal symbol nodes representing different semantics into different arrays;
[0106] Step S405: Fill in the semantic information in the SQL statement according to the semantic relationship graph, the semantic relationship graph generates a first name or a second name for the leaf node, the first name is a new name, and the second name is an existing name; when the first name is used, new semantic information is generated for the leaf node; when the second name is used, the semantic information is divided into first semantic information, second semantic information and third semantic information according to the semantic relationship graph, the first semantic information is semantic information that has no dependence on other semantic information, the second semantic information is semantic information that has dependence on other semantic information, and the third semantic information is semantic information of a special sentence pattern;
[0107] Step S406: sort out the semantic relationship diagrams between the clause and itself, the clause and the main clause, and different SQL statements, split the SQL statement into smaller elements, and if the smaller elements have a semantic impact on the clause itself, store the smaller elements in a temporary shared memory.
[0108] The configuration-driven SQL semantic relationship construction device and method provided in the embodiments of the present invention are further described below.
[0109] First, the semantic relationship configuration file configured by the user is read, and a semantic information collection module 111, a semantic information processing module 112 and a semantic information combing module 113 are generated according to the semantic relationship configuration file.
[0110] Specifically, the SQL statement to be processed is input into the syntax parser 12 for parsing. If the SQL statement does not pass the syntax parser 12, it means that the SQL statement does not conform to the syntax specified by the database, and semantic analysis cannot be performed, and the parsing failure is returned. If the SQL statement passes the syntax parser 12, a NODE node is generated.
[0111] Then, the syntax tree builder 13 generates a corresponding binary tree data structure from these NODE nodes according to the syntax structure specified by the syntax file.
[0112] The semantic information collection module 111 traverses the abstract syntax tree generated by the syntax tree builder 13 to determine whether the root node contains semantic information. If the root node contains semantic information, a corresponding semantic information structure is created based on the semantic information, and the current semantic information is placed into the semantic information structure. If the root node does not contain semantic information, a determination is made as to whether the child node contains a node with semantic information. If it does not contain semantic information, the current node is skipped. If the child node contains semantic information, a recursive loop is performed until the entire abstract syntax tree is traversed.
[0113] The processing of the semantic information processing module 112 is interspersed in the traversal of the abstract syntax tree. The semantic information processing module 112 processes the semantic information that is not dependent on other clauses and the dependent semantic information in the dependency relationship based on the semantic information collected by the semantic information collection module 111. Based on these dependent semantic information, the dependent semantic information is processed and finally the semantic information of the special sentence pattern is processed.
[0114] After the semantic information processing module 112 processes the semantic information, the semantic information combing module 113 stores the semantic information in different structure arrays according to different SQL statements, and combs the semantic relationship diagrams between clauses and themselves, clauses and main clauses, and different SQL statements.
[0115] Figure 5 A schematic diagram of processing grammatical information in a configuration-driven SQL semantic relationship construction device provided by an embodiment of the present invention. Figure 5 Syntax information refers to the information obtained after the SQL syntax is converted and encoded. This information can be directly used to guide the construction of semantic information relationships. Figure 5 In the example, create_table_stmt is used to illustrate how the syntax information processing works. First, the syntax information is input into the processing program, and the processing program converts the syntax information into a corresponding data structure type; then the data structure is converted into a corresponding syntax tree for use by the semantic information processing module 112.
[0116] Figure 6 This is a configuration example of a semantic relationship configuration file in a configuration-driven SQL semantic relationship construction device provided by an embodiment of the present invention. Figure 6 , taking create_index_stmt as an example, this article explains in detail how to configure the semantic relationship configuration file, which uses the YAML file format that is easy to read and write.
[0117] The following are Figure 6 Configuration rules for configuration items:
[0118] int_token: You need to fill in the terminator representing the integer type in the grammar file.
[0119] int_rule_type: You need to fill in the non-terminal symbol representing the integer type in the grammar file.
[0120] For example, if int64_literal:NUM represents integer data in the grammar, then int_token should be filled with NUM, and int_rule_type should be filled with int64_literal. If int64_literal contains other terminals representing integer types, you need to create a new non-terminal, put NUM under the new non-terminal, and change int_rule_type to the newly created non-terminal accordingly.
[0121] float_token: You need to fill in the terminator representing the floating point type in the grammar file.
[0122] float_rule_type: You need to fill in the non-terminal symbol representing the floating point type in the grammar file.
[0123] The configuration rules are the same as those for integer types.
[0124] string_token: You need to fill in the terminator representing the string type in the grammar file.
[0125] string_rule_type: You need to fill in the non-terminal symbol representing the string type in the grammar file.
[0126] identifier_token: You need to fill in the terminal symbol representing the identifier type in the grammar file.
[0127] identifier_rule_types: You need to fill in the non-terminal symbol representing the identifier type in the grammar file.
[0128] split_stmt_types: A node used to split two different SQL statements. For example, in select 1; and select 2;, the node used to split these two independent SQL statements is fixed as the root node.
[0129] split_substmt_types: Nodes used to further split clauses. For example, in select * from (select * from a);, nodes used to split inner clauses.
[0130] validate_first_token: The node where semantic parsing starts, fixed as the root node.
[0131] validate_name_types: A list of semantic relationship types used to identify the meaning of the node. For example, table indicates that this node is a table name, which is fixed to the following types:
[0132] ["index","table","column","database","normal","user","logfile","table_space","datafile","server","server_wrapper","resource_group"," role","srs","view","trigger","sp","sf","udf","event","alias_table","order_column","partition","instance","function","procedure","sche ma","select_target_column","from_table","where_column","group_column","having_column","limit_column","with_table","constraint_column ","column_alias","inner_jion_table","outer_jion_table","natural_jion_table","aggregation_functions_column","on_column","jion_table"].
[0133] validate_select_graph_types: The type of semantic relationship of the query statement is fixed to the following types: ["with_table","select_target_column","from_table","where_column","group_column","having_column","order_column","limit_column","on_column","inner_jion_table","outer_jion_table","natural_jion_table","aggregation_functions_column"].
[0134] validate_sql_flags: Several basic SQL statements, fixed to the following types:
[0135] ["create","drop","alter","update","select","delete","insert"].
[0136] vaildate_map: a key semantic configuration part, used to configure the semantic relationship in the grammatical text, used to inform the semantic information processing module 112 which grammatical non-terminal symbols represent what meanings, the format is as follows:
[0137] rules name (such as create_index_stmt):
[0138] 0: Configuration item 0 mainly configures the information of rules name. Configuration item 0 has 3 contents: 1. Whether it is a new semantic relationship can be selected in [new_relation, old_relation] (new_relation indicates that this node is a new semantic relationship type, and old_relation indicates that this node is an old relationship type; after the new semantic relationship type is set, a new relationship storage variable will be generated to store the relationship of the clause, which is equivalent to the separator between clauses and clauses, clauses and main clauses, and different SQLs), 2. Whether it is the starting rule of a relationship, you can choose between [start, middle, end], start represents the starting node of the clause, middle represents the middle node of the clause, and end represents the node of semantic information.
[0139] There are five items in the relationship configuration item: 1. Non-terminal symbol name, such as ident; 2. The number of non-terminal symbols (in reverse order); 3. This indicates the semantic relationship type of the non-terminal symbol, and the items to be filled in are consistent with the validate_name_types configuration item; 4. The relationship type here indicates whether a new name is required or the old name is used. You can choose new or old. New means creating a new name, and old means using the old name. For example, create table a, a is a new type; 5. This indicates which type of SQL the current SQL statement belongs to, such as a create type SQL statement. The items to be filled in are consistent with the content of validate_sql_flags.
[0140] If there is one rule in rules, one rule must be configured; if there are several rules, several rules must be configured.
[0141] The following is a process of parsing the semantic relationship configuration file, generating a semantic information collection module 111, inserting the code of the semantic information processing module 112 into the process of traversing the abstract syntax tree, and finally generating a semantic information combing module 113 according to the semantic relationship graph. The specific examples are as follows:
[0142] Step 1: Parse the contents of the configuration file and grammar file to generate the corresponding configuration file and grammar file structure.
[0143] Step 2: Traverse the grammatical rules to determine whether the grammatical rules are in the semantic relationship configuration file; if the grammatical rules are not in the semantic relationship configuration file, the grammatical rules are generated into the semantic collection code according to the general semantic information collection module 111. If the grammatical rules are configured in the semantic relationship configuration file; it is necessary to determine whether the current rule configuration is new_relation or old_relation. If it is new_relation, create a new semantic relationship graph structure to collect the semantic information in the next abstract syntax tree, and insert the semantic information processing module 112 into the abstract syntax tree at the current node; if it is old_relation, use the original semantic relationship graph structure to collect the semantic information in the next abstract syntax tree; determine whether the current rule is start (starting stage), middle (middle stage) or end (ending stage). If it is start, create a new array to save all the semantic relationship type structures of the current clause. If it is middle, use the upper semantic relationship type structure array. If it is end, it represents a terminal symbol rule, and generates the corresponding terminal symbol semantic collection code.
[0144] Step 3: Configuration of grammar rule content. There are 5 configuration items in total. First, read the non-terminal symbol name in the grammar rule content; then generate the relevant code of the position of the IR node of this non-terminal symbol in the abstract syntax tree according to the configured position information. For details, please refer to Figure 5 , in order to obtain the corresponding configured IR node; the semantic information type represents the specific meaning or type of this non-terminal in the rule, and pushes the semantic information into the semantic relationship type structure array. The New and Old configurations represent whether this non-terminal needs to generate a new name or select an existing semantic name. The last configuration item represents what type of SQL statement this non-terminal belongs to.
[0145] Step 4: Generate the corresponding configuration item information structure based on the configuration information. The terminator semantic collection logic uses the configuration item information to collect semantic relationships into a specific array and generates the corresponding semantic relationship graph.
[0146] Step 5: The semantic information processing module 112 is inserted into the process of traversing the abstract syntax tree according to whether the configuration contains the start configuration.
[0147] Step 6: Generate a semantic information combing module 113 according to the semantic relationship graph information of the clauses and the semantic relationship graph information of the entire SQL statement. At this time, the semantic configuration parser 11 has completed the corresponding work.
[0148] In the specific implementation, semantic information collection mainly involves traversing the SQL syntax and parsing to generate an abstract syntax tree, parsing the syntax type represented by the IR node and determining whether the current IR node is a terminal node. If not, continue traversing. If so, collect the information into the corresponding semantic information array. After the collection is completed, a SQL semantic relationship diagram of the clause is generated.
[0149] In the specific implementation, for semantic information such as tables, columns, indexes, etc. without dependencies, the semantic name information of tables, columns, indexes, etc. existing in the database is selected; for dependent tables, columns, indexes information but without special sentences, the corresponding dependent processed table, column, index name information is selected; for semantic information such as tables, columns, indexes, etc. containing special sentences, they are processed according to general SQL sentence rules, and the processing rules refer to SQL-92, SQL-99, SQL-2016, and SQL-2023 standards.
[0150] In summary, the configuration-driven semantic relationship construction device and method of the embodiment of the present invention include: a semantic configuration parser, which is used to parse a pre-configured semantic relationship configuration file into a data structure and a syntax file, and generate a semantic information collection module, a semantic information processing module and a semantic information combing module according to the data structure and the syntax file; a syntax parser, which is used to parse an SQL statement into a NODE node, and the NODE node is a custom data structure; a syntax tree builder, which is used to convert the NODE node into an abstract syntax tree to convert the SQL statement into a binary tree data structure; the semantic information collection module is used to traverse the abstract syntax tree, comb the relationship between different leaf nodes into a semantic relationship graph, and put the terminal symbol nodes representing different semantics into different arrays; the semantic information processing module is used to complete the semantic information in the SQL statement according to the semantic relationship graph, and the semantic relationship graph generates a first name or a second name for the leaf node, The first name is a new name, and the second name is an existing name; when the first name is used, new semantic information is generated for the leaf node; when the second name is used, the semantic information is divided into first semantic information, second semantic information and third semantic information according to the semantic relationship graph, the first semantic information is semantic information that has no dependence on other semantic information, the second semantic information is semantic information that has dependence on other semantic information, and the third semantic information is semantic information of a special sentence pattern; the semantic information combing module is used to comb the semantic relationship graph between the clause and itself, the clause and the main clause, and different SQL statements, and split the SQL statement into smaller elements. If the smaller element has a semantic impact on the clause itself, the smaller element is stored in a temporary shared memory, waiting for the semantic information processing module to use it. By making the SQL semantic relationship configurable, the semantic pass rate in the database fuzzy test process is improved and the workload of the semantic parsing module for writing the supporting database is reduced;
[0151] Furthermore, the semantic information combing module is also used to analyze the semantic relationship graph of each SQL statement when processing SQL statements that will change the database, predict and screen out the semantic relationship required for the next SQL statement based on the type and context of the current SQL statement, and store it in a temporary variable. When the next SQL statement arrives, the required semantic relationship can be quickly extracted from the temporary variable, avoiding the overhead of repeated analysis and construction of the semantic relationship graph.
[0152] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A configuration-driven SQL semantic relationship construction device, characterized in that: include: A semantic configuration parser, which is used to parse the pre-configured semantic relationship configuration file into a data structure and a grammar file, and generate a semantic information collection module, a semantic information processing module and a semantic information combing module according to the data structure and the grammar file; A syntax parser, which is used to parse SQL statements into NODE nodes, wherein the NODE nodes are user-defined data structures; A syntax tree builder, which is used to convert the NODE node into an abstract syntax tree to convert the SQL statement into a binary tree data structure; The semantic information collection module is used to traverse the abstract syntax tree, sort out the relationships between different leaf nodes into a semantic relationship graph, and put terminal nodes representing different semantics into different arrays; The semantic information processing module is used to complete the semantic information in the SQL statement according to the semantic relationship graph, the semantic relationship graph generates a first name or a second name for the leaf node, the first name is a new name, and the second name is an existing name; when the first name is used, new semantic information is generated for the leaf node; when the second name is used, the semantic information is divided into first semantic information, second semantic information and third semantic information according to the semantic relationship graph, the first semantic information is semantic information that has no dependence on other semantic information, the second semantic information is semantic information that has dependence on other semantic information, and the third semantic information is semantic information of a special sentence pattern; The semantic information combing module is used to comb the semantic relationship diagram between the clause and itself, the clause and the main clause, and different SQL statements, and split the SQL statement into smaller elements. If the smaller elements have a semantic impact on the clause itself, the smaller elements are stored in a temporary shared memory, waiting to be used by the semantic information processing module.
2. The configuration-driven SQL semantic relationship building device according to claim 1, characterized in that: In the same SQL statement, the SQL statement includes a main clause and a sub-clause, the main clause is used to describe the attributes, constraints and filtering conditions of the data, and the sub-clause is used to filter the main clause; in different SQL statements, the semantic relationship is used to describe the logical or operational relationship between the different SQL statements.
3. The configuration-driven SQL semantic relationship building device according to claim 1, characterized in that: The semantic relationship configuration file is pre-configured according to the grammar file, and the semantic information includes the table name and column name in the SQL statement.
4. The configuration-driven SQL semantic relationship building device according to claim 1, characterized in that: The semantic information combing module includes: A subquery information transmission unit, which is used to sort out the types of query results obtained in the subquery and the names of available data; A joint query information transmission unit, which is used to sort out the relationship between the table name, column name, type, function calculation result and result alias in the joint query; A group query information transmission unit, which is used to transmit the result information of the aggregate function to the main clause; The window function clause information transmission unit is used to transmit the result information of the window function and the columns used for grouping in the clause to the main clause.
5. The configuration-driven SQL semantic relationship building device according to claim 1, characterized in that: The semantic information combing module is used to pass down the semantic information in different SQL statements in the execution order of the SQL statements, and save the results of the changes to the database after the SQL statements are executed. The results of the changes include deleting the table name, adding columns, and modifying the foreign keys of columns.
6. The configuration-driven SQL semantic relationship building device according to claim 5, characterized in that: The semantic information combing module is also used to analyze the semantic relationship graph of each SQL statement when processing SQL statements that will change the database, predict and filter out the semantic relationship required for the next SQL statement based on the type and context of the current SQL statement, and store it in a temporary variable.
7. The configuration-driven SQL semantic relationship building device according to claim 1, characterized in that: Determine whether the current rule is in the beginning, middle or end stage; When the current rule is in the start phase, a new array is created to store all semantic relationship type structures of the current clause; When the current rule is in the middle stage, the semantic relationship type structure of the upper layer is used; When the current rule is at the end stage, a corresponding terminal symbol semantic collection code is generated.
8. The configuration-driven SQL semantic relationship building device according to claim 1, characterized in that: The semantic relationship configuration file includes a first configuration item, a second configuration item, a third configuration item, a fourth configuration item, and a fifth configuration item. The non-terminal symbol name in the grammar file is read, and the relevant code of the position of the IR node of the non-terminal symbol in the abstract syntax tree is generated according to the first configuration item to obtain the second configuration item. The third configuration item indicates the specific meaning or type of the non-terminal symbol, the fourth configuration item indicates that the non-terminal symbol needs to generate the first name or the second name, and the fifth configuration item indicates which type of SQL statement the non-terminal symbol belongs to.
9. The configuration-driven SQL semantic relationship building device according to claim 1, characterized in that: When the semantic information is the first semantic information, the existing list and index information in the database are selected; when the semantic information is the second semantic information, the corresponding dependent processed list and index information in the database are selected; when the semantic information is the third semantic information, it is processed according to the general SQL sentence rules.
10. A configuration-driven SQL semantic relationship construction method, characterized in that: include: Parse the pre-configured semantic relationship configuration files into data structure and grammar files; Parse the SQL statement into a NODE node, where the NODE node is a custom data structure; Convert the NODE node into an abstract syntax tree to convert the SQL statement into a binary tree data structure; Traversing the abstract syntax tree, sorting the relationships between different leaf nodes into a semantic relationship graph, and placing terminal nodes representing different semantics into different arrays; The semantic information in the SQL statement is filled in completely according to the semantic relationship graph, and the semantic relationship graph generates a first name or a second name for the leaf node, wherein the first name is a new name and the second name is an existing name; when the first name is used, new semantic information is generated for the leaf node; when the second name is used, the semantic information is divided into first semantic information, second semantic information and third semantic information according to the semantic relationship graph, wherein the first semantic information is semantic information that has no dependency on other semantic information, the second semantic information is semantic information that has dependency on other semantic information, and the third semantic information is semantic information of a special sentence pattern; The semantic relationship graph between the clause and itself, the clause and the main clause, and different SQL statements is sorted out, and the SQL statement is split into smaller elements. If the smaller elements have a semantic impact on the clause itself, the smaller elements are stored in a temporary shared memory.
Citation Information
Patent Citations
SQL interpreter of HBase and optimization method
CN111309757A
Semantic analysis method supporting multi-dialect SQL blood relationship analysis
CN113326286A
