Sql statement processing method and device, and electronic equipment
By splitting the SQL statement binding process into multiple loosely coupled nodes and using node binding sequence diagrams and virtual tables for binding, the problem of code expansion and maintenance difficulties in existing technologies is solved, and more efficient SQL statement processing is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- ROCK JIAHUA (CHONGQING) TECH CO LTD
- Filing Date
- 2023-11-24
- Publication Date
- 2026-08-04
AI Technical Summary
In existing technologies, the binding of SQL statements, the generation of logical plans, and the physical plans are logically integrated, making the code difficult to extend and maintain.
The process of binding SQL statements is broken down into bindings between multiple loosely coupled nodes. By obtaining the node binding sequence diagram and node reference relationship diagram of the untyped syntax tree, the virtual table of the previous node is used to bind the target node until the untyped syntax tree is converted into a typed syntax tree.
It improves the extensibility and maintainability of the code, ensures the validity of SQL statements and the accuracy of the binding process, and enhances the testability and maintainability of the module.
Smart Images

Figure CN117370376B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of database technology, and more specifically, to an SQL statement processing method, apparatus, and electronic device. Background Technology
[0002] A database, as a software system used to store and manage data, helps users effectively organize, store, access, and manage data. Databases are typically accessed through SQL statements. When accessing a database using SQL statements, the server needs to compile the SQL statements. This compilation process includes parsing, binding, generating a logical plan, generating a physical plan, and execution by the execution engine.
[0003] In existing technologies, the binding of SQL statements, the generation of logical plans, and the physical plans are usually integrated together, making the code difficult to extend and maintain. Summary of the Invention
[0004] The purpose of this application is to provide an SQL statement processing method, apparatus, and electronic device to achieve the binding of SQL statements and improve the extensibility and maintainability of the code.
[0005] In a first aspect, embodiments of this application provide an SQL statement processing method, the method comprising: obtaining an untyped syntax tree of an SQL statement; the untyped syntax tree comprising multiple nodes; determining a target node to be bound among the multiple nodes according to a pre-obtained node binding sequence diagram; determining the previous node of the target node according to a node reference relationship diagram; the node reference relationship diagram being pre-generated according to SQL semantics; obtaining a virtual table of the previous node; the virtual table being used to represent the structural information of the previous node; binding the target node based on the virtual table of the previous node; and determining the next target node to be bound, until all nodes are bound, so as to convert the untyped syntax tree into a typed syntax tree.
[0006] In this embodiment, when binding SQL statements, the target node to be bound in the untyped syntax tree is determined according to the node binding sequence diagram, the previous node of the target node is determined according to the node reference relationship diagram, and then the target node is bound based on the virtual table of the previous node. This process is repeated until all nodes are bound, thus converting the untyped syntax tree into a typed syntax tree. In this process, the binding of SQL statements is broken down into bindings between multiple loosely coupled nodes. Furthermore, the binding logic for each node is consistent and unified, and the binding process is independent of other processes in the compilation of the SQL statement, improving the extensibility and maintainability of the code.
[0007] In some embodiments, binding a target node based on the virtual table of the previous node includes: matching and assigning values to column reference expressions in the target node based on the virtual table of the previous node to bind the target node.
[0008] This application embodiment transforms column reference expressions without data types into those with data types by matching and assigning values to the column reference expressions of the target node. This process is a necessary condition for deriving the data type of the entire expression and for generating the node virtual table. This ensures the smooth execution of the binding process.
[0009] In some embodiments, matching and assigning values to column reference expressions in the target node based on the virtual table of the previous node to bind the target node includes: matching the column names of the virtual table of the previous node with the names of column reference expressions in the target node; if the match is successful, assigning the data type corresponding to the column name to the column reference expression in the target node to bind the target node; if the match is unsuccessful, outputting a first verification failure message.
[0010] In this embodiment, the binding process utilizes the virtual table of the previous node to verify the semantic correctness of the target node and assign data types, ensuring the legality of the SQL statement. Furthermore, the consistent and universal verification method ensures verification reliability while improving the decoupling between nodes, thus enhancing the module's testability and maintainability.
[0011] In some embodiments, after binding the target node, the method further includes: generating a virtual table of the target node based on the name and data type of the column reference expression in the target node; and storing the virtual table of the target node.
[0012] This application embodiment generates and stores a virtual table of target nodes. This ensures the smooth execution of the verification process when binding the next target node, thereby guaranteeing the successful execution of the node binding process and improving the accuracy of the entire binding process.
[0013] In some embodiments, after assigning the data type corresponding to the column name to the column reference expression in the target node, the method further includes: deriving the data type of the compound expression to which the column reference expression in the target node belongs based on the data type of the column reference expression in the target node, to obtain the data type of the compound expression in the target node.
[0014] This application embodiment takes into account that the expressions in the nodes may also contain other types of expressions besides column reference expressions. Therefore, the data type of the compound expression is obtained through deduction, so that all kinds of expressions in the nodes have data types, laying the foundation for the smooth execution of the binding process of the next node, thereby enabling the conversion of a syntax tree without data types into a syntax tree with data types.
[0015] In some embodiments, obtaining the virtual table of the previous node includes: if the previous node is the root node, obtaining the metadata of the SQL statement; the metadata is used to characterize the structural information of the data table corresponding to the SQL statement; and using the metadata as the virtual table of the previous node.
[0016] This application embodiment obtains the virtual table of the corresponding node by distinguishing the type of the previous node, thereby providing the necessary virtual table for binding the next node. Furthermore, if the previous node is the root node, the metadata of the SQL statement is directly obtained as the virtual table of the previous node. Since the metadata represents the structural information of the data table corresponding to the SQL statement, the virtual table obtained from the metadata includes all column reference expressions and their corresponding data types, providing a foundation for starting the binding process.
[0017] In some embodiments, the target node includes multiple column reference expressions; determining the previous node of the target node according to the node reference relationship graph includes: determining the previous node of each column reference expression according to the node reference relationship graph; correspondingly, obtaining the virtual table of the previous node; binding the target node based on the virtual table of the previous node includes: obtaining the virtual table of the previous node of each column reference expression; binding the target node based on the order of each column reference expression and the virtual table of the previous node of each column reference expression.
[0018] This application embodiment takes into account that a target node may include multiple column reference expressions. Therefore, it is necessary to determine the previous node of each column reference expression according to the node reference relationship graph to ensure that the obtained virtual table is correct and to guarantee the smooth execution of the binding process. In addition, the node reference relationship graph is a prerequisite for realizing an independent and consistent node binding process, ensuring that the binding process is standardized and orderly.
[0019] In some embodiments, before determining the previous node of the target node based on the node reference relationship graph, the method further includes: determining whether the target node has a previous node based on the node reference relationship graph; if the target node has a previous node, then performing the step of determining the previous node of the target node based on the node reference relationship graph; if the target node does not have a previous node, then outputting a second verification failure message.
[0020] In addition to validating the expression of the target node based on the virtual table of the previous node, this embodiment of the application also includes validating the relationships between nodes, ensuring the completeness of the validation.
[0021] Secondly, embodiments of this application provide an SQL statement processing apparatus, comprising: a first acquisition module for acquiring an untyped syntax tree of an SQL statement; the untyped syntax tree includes multiple nodes; a first determination module for determining a target node to be bound among the multiple nodes according to a pre-generated node binding sequence diagram; a second determination module for determining the previous node of the target node according to a node reference relationship diagram; the node reference relationship diagram is pre-generated according to SQL semantics; a second acquisition module for acquiring a virtual table of the previous node; the virtual table is used to represent the structural information of the previous node; and a binding module for binding the target node based on the virtual table of the previous node, and determining the next target node to be bound, until all nodes are bound, so as to convert the untyped syntax tree into a typed syntax tree.
[0022] Thirdly, embodiments of this application provide an electronic device, including: a processor, a memory, a storage medium, and a bus, wherein the processor and the memory communicate with each other through the bus; the memory stores program instructions that can be executed by the processor, and the processor can execute the method steps of the first aspect by calling the program instructions.
[0023] Fourthly, embodiments of this application provide a non-transitory computer-readable storage medium, comprising: the computer-readable storage medium storing computer instructions, the computer instructions causing the computer to perform the method steps of the first aspect.
[0024] Other features and advantages of this application will be set forth in the following description and will be apparent in part from the description or may be learned by practicing embodiments of this application. Attached Figure Description
[0025] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments of this application will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0026] Figure 1 A flowchart for compiling an SQL statement is provided as an embodiment of this application;
[0027] Figure 2 A flowchart illustrating an SQL statement processing method provided in this application embodiment;
[0028] Figure 3 A node binding sequence diagram provided in an embodiment of this application;
[0029] Figure 4 A flowchart of a node reference relationship graph provided in an embodiment of this application;
[0030] Figure 5 This is a schematic diagram of the structure of an SQL statement processing device provided in an embodiment of this application;
[0031] Figure 6 This is a schematic diagram of the electronic device structure provided in an embodiment of this application. Detailed Implementation
[0032] The embodiments of the technical solution of this application will now be described in detail with reference to the accompanying drawings. These embodiments are only used to more clearly illustrate the technical solution of this application and are therefore merely examples, and should not be used to limit the scope of protection of this application.
[0033] It should be noted that all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application pertains; the terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit this application; the terms “comprising” and “having”, and any variations thereof, in the specification, claims, and foregoing description of the drawings are intended to cover non-exclusive inclusion.
[0034] In the description of the embodiments of this application, technical terms such as "first" and "second" are used only to distinguish different objects and should not be construed as indicating or implying relative importance or implicitly specifying the number, specific order, or primary and secondary relationship of the indicated technical features. In the description of the embodiments of this application, "multiple" means two or more, unless otherwise explicitly defined.
[0035] In the description of the embodiments in this application, the term "and / or" is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone. Additionally, the character " / " in this document generally indicates that the preceding and following related objects have an "or" relationship.
[0036] To facilitate understanding of the technical solutions provided in the embodiments of this application, the technical content involved in the embodiments of this application will be described before the technical solutions provided in the embodiments of this application are described. Figure 1 A flowchart for compiling an SQL statement is provided for an embodiment of this application, such as... Figure 1As shown, in SQL statement compilation, the input SQL statement is parsed by the server, generating an Abstract Syntax Tree (AST) without data types. This AST is then bound to generate an AST with data types, followed by a logical plan. Finally, the logical plan is combined with storage engine characteristics to transform the SQL statement into a physical plan, thus completing the compilation. The main task of SQL statement binding is to transform the AST without data types into an expression tree with data types, based on the metadata of the data table. Specifically, SQL statement binding includes: assigning data types to column reference expressions in the SQL statement, semantic detection and type validation of expressions, and data type deduction of expressions within the SQL statement. Therefore, the bound SQL statement will not contain semantic errors.
[0037] In existing technologies, SQL statement binding is not treated as an independent module. During SQL statement compilation, the abstract syntax tree corresponding to the SQL statement (without data types) is directly converted into a logical plan. The processes involved, such as type assignment, semantic detection, type validation, logical plan generation, and logic optimization, are integrated together, without a universally applicable method. This makes code maintenance and extensibility difficult. To address these issues, this application provides an SQL statement processing method that extracts the binding process, implementing binding during SQL statement compilation, thereby reducing code complexity and improving maintainability and extensibility.
[0038] It is understood that the SQL statement processing method provided in this application embodiment can be applied to terminal devices (also known as electronic devices) and servers; wherein the terminal device can specifically be a smartphone, tablet computer, computer, personal digital assistant (PDA), etc.; the server can specifically be an application server or a web server.
[0039] To facilitate the explanation of the technical solutions provided in the embodiments of this application, the application scenarios of the SQL statement processing method provided in the embodiments of this application will be introduced, taking the server as the execution subject as an example.
[0040] Figure 2 This is a flowchart illustrating an SQL statement processing method provided in an embodiment of this application, as shown below. Figure 2 As shown, the method includes:
[0041] Step 201: Obtain the untyped syntax tree of the SQL statement.
[0042] In practice, after receiving the current SQL statement, the server parses it to obtain the untyped syntax tree (LSB). The LSB consists of multiple nodes. Each node is composed of expressions; an SQL statement must contain at least one node, and each node contains at least one expression. Expressions include column reference expressions, compound expressions, aggregate function expressions, etc.
[0043] It should be noted that the SQL statements involved in the embodiments of this application are all SQL query statements.
[0044] Example 1, the SQL statement is as follows:
[0045] Select count(speed)From car Group By color,brand Having color="red"
[0046] In this SQL statement, the nodes include "Select count(speed)", "From car", "Group By color,brand", and "Having color="red"". Here, "Select", "From", "Group By", and "Having" represent the node names; "count(speed)", "car", "color", "brand", and "color="red"" represent the expressions for the corresponding nodes. "car", "color", and "brand" represent column reference expressions, and "count(speed)" represents an aggregate function expression. Additionally, expressions like "speed+1" represent compound expressions.
[0047] Step 202: Determine the target node to be bound among multiple nodes based on the pre-obtained node binding sequence diagram.
[0048] In practice, since the node binding sequence diagram is fixed and does not change with specific SQL statements, the server determines the target node to be bound from among multiple nodes based on the pre-obtained node binding sequence diagram.
[0049] Figure 3 A node binding sequence diagram provided in this application embodiment, such as Figure 3 As shown, the root node of the node binding sequence diagram is the FromTable node, followed by WhereCase, GroupBy, Having, SelectList, Orderby, and Limit / Offset.
[0050] according to Figure 3As can be seen, the node binding order is linear and unidirectional; once a node is bound, it will not be bound again. The root node, FromTable, is the first node in the binding order.
[0051] It should be noted that if a SQL statement contains only a portion of the nodes in the node binding sequence diagram, the binding order still follows the order of the node binding sequence diagram. For example, the SQL statement in Example 1 does not contain the WhereCase node. Therefore, after binding the root node FromTable, the next target node to be bound, as determined by the node binding sequence diagram, is the GroupBy node.
[0052] Step 203: Determine the previous node of the target node based on the node reference relationship graph.
[0053] Step 204: Obtain the virtual table of the previous node.
[0054] In the specific implementation process, based on Figure 3 Once the target node to be bound is determined, the binding process can begin. Binding requires finding the target node's predecessor node to obtain its virtual table, which is then used to bind the target node.
[0055] The node reference graph is generated in advance based on SQL semantics. That is, for a specific set of SQL statements, the reference relationships are fixed and do not change with the specific SQL statement.
[0056] Figure 4 A flowchart of a node reference relationship graph provided in an embodiment of this application, such as Figure 4 As shown, after the binding begins,
[0057] Determine if the target node type (NodeType) is any one of FromTable / WhereCase / GroupBy nodes;
[0058] If so, then determine that the previous node of the target node is the FromTable node;
[0059] If not, execute: Determine if an aggregate function exists in the SQL statement; if an aggregate function exists, determine if the column reference expression in the target node is an expression in the aggregate function; if no aggregate function exists, determine if the SQL statement contains grouping.
[0060] When determining whether a column reference expression in the target node is an expression in an aggregate function, if it is, then the previous node of the target node is determined to be a FromTable node; if it is not an expression in an aggregate function, then it is determined whether the SQL statement contains grouping.
[0061] When determining whether a SQL statement has grouping in the absence of aggregate functions, if the SQL statement has grouping, the previous node of the target node is determined to be the Group By node; if the SQL statement does not have grouping, the previous node of the target node is determined to be the FromTable node.
[0062] If the column reference expression in the target node is not an expression in an aggregate function, when determining whether the SQL statement contains groupings, if the SQL statement contains groupings, then the previous node of the target node is determined to be the Group By node; if the SQL statement does not contain groupings, an invalid value is returned. An invalid value indicates that the previous node of the target node cannot be found according to the node reference relationship graph, and an error message will be given in this case.
[0063] It's important to note that aggregate functions in SQL statements are used to perform calculations on a set of values and return a single value as the result. Common aggregate functions include COUNT, SUM, Avg, MAX, and MIN. Grouping refers to using GROUP BY in an SQL statement to group data according to a specified column.
[0064] according to Figure 4 As can be seen, the reference relationships between nodes are not linear; multiple nodes can share the same parent node. When a node has multiple column reference expressions, the parent node corresponding to each column reference expression may be a different node. Every node has at least one parent node; the parent node of the root node FromTable is itself. Therefore, when determining the parent node of a target node based on the node reference relationship graph, we determine the parent node of the column reference expressions within the target node based on the node reference relationship graph.
[0065] Therefore, when a target node includes multiple column reference expressions, it is necessary to determine the previous node of each column reference expression based on the node reference relationship graph and obtain the virtual table of the previous node for each column reference expression. The target node is then bound based on the order of the column reference expressions and the virtual table of the previous node. This ensures the correctness of binding the target node using the virtual table of the previous node, allowing the binding process to execute smoothly.
[0066] Virtual tables are used to represent the structural information of the previous node. To facilitate understanding of virtual tables, we will use the Group By node in the SQL statement of Example 1 as an example:
[0067] The `Group By` node includes the column reference expressions "color" and "brand", therefore, the virtual table of the `Group By` node includes the column reference expressions "color" and "brand" and their corresponding data types. The data structure of the virtual table can be defined as follows:
[0068]
[0069]
[0070] The virtual table for Group By is shown in Table 1:
[0071] Table 1
[0072]
[0073] Based on the data structure and table content above, it can be seen that the data type of the column reference expressions "color" and "brand" is "string".
[0074] For example, taking the SQL statement in Example 1 as an example, according to Figure 3 The currently identified target node to be bound is the Having node, according to... Figure 4 The flowchart illustrating the node reference relationships shows that the parent node of the Having node is the Group By node. After obtaining the virtual table of the Group By node, the Having node can be bound based on the virtual table of the Group By node.
[0075] Step 205: Bind the target node based on the virtual table of the previous node, and determine the next target node to be bound, until all nodes are bound, so as to convert the untyped syntax tree into a typed syntax tree.
[0076] In the specific implementation process, after binding the target node based on the virtual table of the previous node, the next target node to be bound is determined according to the node binding sequence diagram. Then, the previous node of the next target node to be bound is determined according to the node reference relationship diagram, and its corresponding virtual table is obtained. Binding is then performed based on the virtual table. The above process is repeated until all nodes are bound. Finally, the untyped syntax tree is converted into a typed syntax tree.
[0077] This application embodiment breaks down the SQL statement binding process into bindings between multiple loosely coupled nodes. Furthermore, the binding logic for each node is consistent and unified, and the binding process is independent of other processes during SQL statement compilation, thus improving code extensibility and maintainability.
[0078] In some embodiments, binding a target node based on the virtual table of the previous node includes: matching and assigning values to column reference expressions in the target node based on the virtual table of the previous node to bind the target node.
[0079] In practice, the binding process is the process of matching and assigning values to the column reference expressions in the target node based on the virtual table of the previous node.
[0080] Matching refers to matching the column names of the virtual table in the previous node with the names of the column reference expressions in the target node.
[0081] Matching is equivalent to validation. If the validation is successful, the data type corresponding to the column name is assigned to the column reference expression in the target node, so that the column reference expression in the target node has a data type, thereby completing the binding to the target node.
[0082] If the verification fails, it indicates that there is a semantic error in the current SQL statement. The binding process will be immediately terminated with an error message to indicate that the verification failed and the assignment operation will not be performed.
[0083] For example, taking the SQL statement in Example 1 as an example, the target node to be bound is the Having node, which includes the column reference expression color. According to... Figure 4 The flowchart illustrating the node reference relationships shows that the node preceding the Having node is the Group By node. According to Table 1, the virtual table of the Group By node contains columns named "color" and "brand". Therefore, the column reference expression "color" in the Having node of the SQL statement can be matched in the virtual table of the preceding Group By node. Thus, the match is successful, and the data type "string" for "color" is assigned to the column reference expression "color" in the Having node.
[0084] Since the Having node only includes a column reference expression "color", its data type is known through the above process. After binding is completed, a virtual table of the Having node can be constructed based on the name and data type of the column reference expression of the Having node.
[0085] However, it's important to understand that if a target node contains multiple column reference expressions, each expression needs to be matched, validated, and its type assigned. After binding the target node, a virtual table for the target node is generated based on the order of the column reference expressions, the name of each expression, and its data type, and this table is stored. This allows for direct access to the virtual table when binding the next target node, ensuring the smooth execution of the binding process.
[0086] Example 2: The SQL statement is as follows:
[0087] Select speed,count(speed)From car Group By color Having color="red"
[0088] Specifically, for the node `select`, there are two components: a separate column reference expression `speed` and an aggregate function expression `count(speed)`. The aggregate function expression also includes the column reference expression `speed`. According to... Figure 4 As shown in the node reference diagram, the node preceding the independent column reference expression `speed` is the `Group By` node, and the node preceding the column reference expression `speed` in the aggregate function expression `count(speed)` is the `FromTable` node. Since the preceding node for the independent column reference expression `speed` is the `Group By` node, and `speed` does not exist in the virtual table of the `Group By` node, when binding to the independent column reference expression `speed`, the column name `speed` cannot be matched in the column names of the virtual table of the `Group By` node. Therefore, the validation fails, and an error message is displayed.
[0089] In the above process, the semantic correctness of the target node can be verified based on the virtual table of the previous node, ensuring the legality of the SQL statement. Furthermore, data types can be assigned to successfully matched column reference expressions, laying the foundation for type inference of subsequent expressions containing column reference expressions and the generation of corresponding virtual tables. In addition, the consistent and universal verification method ensures verification reliability while improving the decoupling between nodes, thus enhancing the module's testability and maintainability.
[0090] In some embodiments, after assigning the data type corresponding to the column name to the column reference expression in the target node, the method further includes: deriving the data type of the compound expression to which the column reference expression in the target node belongs based on the data type of the column reference expression in the target node, to obtain the data type of the compound expression in the target node.
[0091] In practice, since the expressions of the target node are not necessarily column reference expressions, but may be compound expressions, after the column reference expression of the target node is matched in the virtual table of the previous node and a data type is assigned to the column reference expression of the target node, it is also necessary to deduce the data type of the compound expression containing the column reference expression based on the data type of the column reference expression.
[0092] Example 3, the SQL statement is as follows:
[0093] Select speed+1From car Group By speed Having color="red"
[0094] Specifically, for the node `select`, this includes the compound expression `speed+1`. According to... Figure 4 As shown in the node reference diagram, the previous node associated with the compound expression `speed+1` is the `Group By` node. The virtual table of the `Group By` node contains the value `speed`. Therefore, when binding to the `select` node, the `speed` value in the compound expression `speed+1` can only be matched within the virtual table of the `Group By` node to obtain the data type of `speed`. At this point, it is necessary to deduce the data type of the compound expression `speed+1` based on the data type of `speed`. For example, if the data type of `speed` is integer, then the data type of `speed+1` is also integer.
[0095] Therefore, after all column reference expressions in a compound expression have completed their data type assignments, the data type of the compound expression itself can be deduced. The type deduction logic is related to the logic of the compound expression itself. If there are no column reference expressions in the compound expression, the data type of the compound expression can be obtained directly.
[0096] It should be noted that if the target node contains a compound expression, a corresponding virtual table is generated based on the name and data type of the compound expression.
[0097] This application embodiment takes into account that the expressions in the nodes may also contain other types of expressions besides column reference expressions. Therefore, the data type of the compound expression is obtained through deduction, so that all kinds of expressions in the nodes have data types, laying the foundation for the smooth execution of the binding process of the next node, thereby enabling the conversion of a syntax tree without data types into a syntax tree with data types.
[0098] In some embodiments, obtaining the virtual table of the previous node includes: if the previous node is the root node, obtaining the metadata of the SQL statement; and using the metadata as the virtual table of the previous node.
[0099] In practical implementation, metadata is used to characterize the structural information of the data table corresponding to the SQL statement. Structural information refers to all the fields included in the data table and the data types of the fields. For example, a student table may contain fields such as "student ID", "name", and "gender". The data type of "student ID" is integer, the data type of "name" is character, and the data type of "gender" is character, etc.
[0100] When the node preceding the target node is the root node, the metadata is used as a virtual table for that node. When the node preceding the target node is the root node, a semantic check to determine if the database table exists is required.
[0101] It's important to note that SQL queries can involve subqueries, which are nested queries. For example, if query A depends on the result of query B, then query B is the subquery, and query A is the main query.
[0102] In the case of SQL subqueries, the virtual table of the root node of the subquery statement is the metadata of the database table, and the virtual table of the root node of the main query statement is the virtual table of the last node of the subquery statement.
[0103] This application embodiment obtains the virtual table of the corresponding node by distinguishing the type of the previous node, thereby providing the necessary virtual table for binding the next node. Furthermore, if the previous node is the root node, the metadata of the SQL statement is directly obtained as the virtual table of the previous node. Since the metadata represents the structural information of the data table corresponding to the SQL statement, the virtual table obtained from the metadata includes all column reference expressions and their corresponding data types, providing a foundation for starting the binding process.
[0104] In some embodiments, since invalid values may be returned when determining the previous node of a target node based on the node reference relationship graph, it is necessary to determine whether the target node has a previous node based on the node reference relationship graph before determining the previous node of the target node. If the target node has a previous node, the step of determining the previous node of the target node based on the node reference relationship graph is executed. If the target node does not have a previous node, it indicates that the SQL statement is invalid, the subsequent binding process is not executed, the verification fails, and an error message is output.
[0105] It should also be noted that for certain nodes, personalized validation is required based on the node's characteristics. For example, the WhereCase node needs to validate whether the data type of the expression in that node is Boolean (BOOL).
[0106] Figure 5 This is a schematic diagram of the structure of an SQL statement processing device provided in an embodiment of this application, as shown below. Figure 5 As shown, the device includes a first acquisition module 501, a first determination module 502, a second determination module 503, a second acquisition module 504, and a binding module 505, wherein,
[0107] The first acquisition module 501 is used to acquire the typeless syntax tree of the SQL statement; the typeless syntax tree includes multiple nodes; the first determination module 502 is used to determine the target node to be bound among the multiple nodes according to the pre-generated node binding sequence diagram; the second determination module 503 is used to determine the previous node of the target node according to the node reference relationship diagram; the node reference relationship diagram is pre-generated according to the SQL semantics; the second acquisition module 504 is used to acquire the virtual table of the previous node; the virtual table is used to represent the structural information of the previous node; the binding module 505 is used to bind the target node based on the virtual table of the previous node, and to determine the next target node to be bound, until all nodes are bound, so as to convert the typeless syntax tree into a typed syntax tree.
[0108] Based on the above embodiments, the binding module 505 is specifically used to: match and assign values to the column reference expressions in the target node based on the virtual table of the previous node, so as to bind the target node.
[0109] Based on the above embodiments, the binding module 505 is specifically used to: match the column name of the virtual table of the previous node with the name of the column reference expression in the target node; if the match is successful, assign the data type corresponding to the column name to the column reference expression in the target node to bind the target node; if the match is unsuccessful, output the first verification failure information.
[0110] Based on the above embodiments, the binding module 505 is specifically used to: generate a virtual table for the target node according to the name and data type of the column reference expression in the target node; and store the virtual table of the target node.
[0111] Based on the above embodiments, the binding module 505 is specifically used to: deduce the data type of the compound expression to which the column reference expression in the target node belongs based on the data type of the column reference expression in the target node, and obtain the data type of the compound expression in the target node.
[0112] Based on the above embodiments, the second acquisition module 504 is specifically used for: if the previous node is the root node, acquiring the metadata of the SQL statement; the metadata is used to characterize the structural information of the data table corresponding to the SQL statement; and using the metadata as a virtual table of the previous node.
[0113] Based on the above embodiments, the target node includes multiple column reference expressions; the second determining module 503 is specifically used to: determine the previous node of each column reference expression according to the node reference relationship diagram; correspondingly, obtain the virtual table of the previous node; bind the target node based on the virtual table of the previous node, including: obtaining the virtual table of the previous node of each column reference expression; binding the target node based on the order of each column reference expression and the virtual table of the previous node of each column reference expression.
[0114] Based on the above embodiments, the device further includes a judgment module, used to: determine whether the target node has a previous node according to the node reference relationship graph; if the target node has a previous node, then perform the step of determining the previous node of the target node according to the node reference relationship graph; if the target node does not have a previous node, then output a second verification failure message.
[0115] Figure 6 This is a schematic diagram of the electronic device structure provided in the embodiments of this application, such as... Figure 6 As shown, the electronic device includes a processor 601, a memory 602, and a bus 603; wherein the processor 601 and the memory 602 communicate with each other via the bus 603. The processor 601 is used to call program instructions in the memory 602 to execute the methods provided in the above-described method embodiments.
[0116] Processor 601 can be an integrated circuit chip with signal processing capabilities. The processor 601 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), an off-the-shelf programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It can implement or execute the various methods, steps, and logic block diagrams disclosed in the embodiments of this application. The general-purpose processor can be a microprocessor or any conventional processor.
[0117] The memory 602 may include, but is not limited to, random access memory (RAM), read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), etc.
[0118] This embodiment discloses a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium. The computer program includes program instructions, and when the program instructions are executed by a computer, the computer can perform the methods provided in the above-described method embodiments.
[0119] This embodiment provides a non-transitory computer-readable storage medium that stores computer instructions that cause the computer to execute the methods provided in the above-described method embodiments.
[0120] In the embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. Furthermore, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Additionally, the displayed or discussed mutual couplings, direct couplings, or communication connections may be through some communication interfaces; indirect couplings or communication connections between devices or units may be electrical, mechanical, or other forms.
[0121] Furthermore, the units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0122] Furthermore, the functional modules in the various embodiments of this application can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.
[0123] The above description is merely an embodiment of this application and is not intended to limit the scope of protection of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of protection of this application.
Claims
1. A method of processing SQL statements, characterized by, The method includes: Obtain the untyped syntax tree of the SQL statement; the untyped syntax tree includes multiple nodes; The target node to be bound among the plurality of nodes is determined based on the pre-obtained node binding sequence diagram; The previous node of the target node is determined based on the node reference relationship graph; the node reference relationship graph is generated in advance based on SQL semantics. Obtain the virtual table of the previous node; the virtual table is used to represent the structural information of the previous node; Bind the target node based on the virtual table of the previous node, and determine the next target node to be bound, until all nodes are bound, so as to convert the untyped syntax tree into a typed syntax tree. The binding of the target node based on the virtual table of the previous node includes: Match the column names of the virtual table in the previous node with the names of the column reference expressions in the target node; If a match is successful, the data type corresponding to the column name is assigned to the column reference expression in the target node to bind the target node; If the match fails, output the first verification failure message.
2. The method of claim 1, wherein, After binding the target node, the method further includes: A virtual table for the target node is generated based on the name and data type of the column reference expression in the target node; The virtual table of the target node is stored.
3. The method of claim 1, wherein, After assigning the data type corresponding to the column name to the column reference expression in the target node, the method further includes: Based on the data type of the column reference expression in the target node, the data type of the compound expression to which the column reference expression in the target node belongs is deduced, thereby obtaining the data type of the compound expression in the target node.
4. The method according to any of claims 1 to 3, characterized in that The step of obtaining the virtual table of the previous node includes: If the previous node is the root node, obtain the metadata of the SQL statement; the metadata is used to characterize the structure information of the data table corresponding to the SQL statement; The metadata is used as a virtual table of the previous node.
5. The method according to any one of claims 1-3, characterized in that, The target node includes multiple column reference expressions; determining the previous node of the target node based on the node reference relationship graph includes: The previous node of each column reference expression is determined based on the node reference relationship graph; Accordingly, the virtual table of the previous node is obtained; the target node is bound based on the virtual table of the previous node, including: Retrieve the virtual table of the previous node for each of the column reference expressions; The target node is bound based on the order of each column reference expression and the virtual table of the previous node of each column reference expression.
6. The method according to any one of claims 1-3, characterized in that, Before determining the previous node of the target node based on the node reference graph, the method further includes: Determine whether the target node has a predecessor node based on the node reference relationship graph. If the target node has a predecessor node, then the step of determining the predecessor node of the target node based on the node reference relationship graph is executed; If the target node does not have a previous node, then output the second verification failure message.
7. An SQL statement processing device, characterized in that, The device includes: The first acquisition module is used to acquire the untyped syntax tree of the SQL statement; the untyped syntax tree includes multiple nodes; The first determining module is used to determine the target node to be bound among the plurality of nodes according to a pre-generated node binding sequence diagram; The second determining module is used to determine the previous node of the target node based on the node reference relationship graph; the node reference relationship graph is generated in advance based on SQL semantics. The second acquisition module is used to acquire the virtual table of the previous node; the virtual table is used to represent the structural information of the previous node; The binding module is used to bind the target node based on the virtual table of the previous node, and to determine the next target node to be bound, until all nodes are bound, so as to convert the untyped syntax tree into a typed syntax tree. Specifically, the binding module is used for: Match the column names of the virtual table in the previous node with the names of the column reference expressions in the target node; If a match is successful, the data type corresponding to the column name is assigned to the column reference expression in the target node to bind the target node; If the match fails, output the first verification failure message.
8. An electronic device, characterized in that, include: A processor and a memory, the memory storing machine-readable instructions executable by the processor, which, when executed by the processor, perform the SQL statement processing method as described in any one of claims 1 to 6.