Query method of database query statement, storage medium and device
By parsing the syntax and semantics of SQL statements, constructing a query tree and separating it into parent and child query trees, the problem of complex SQL statements with secondary aggregation functions in existing technologies is solved, thus improving writing efficiency.
Patent Information
- Application Number
- CN202310640009.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-05-31
- Publication Date
- 2025-11-28
- Estimated Expiration
- 2043-05-31
AI Technical Summary
In existing technologies, SQL statements that perform secondary aggregation on the return results of aggregate functions are complex, which greatly limits users' SQL statement writing.
By parsing the query statement syntax and semantics, an initial query tree and a target column list are constructed. It is checked for nested calls to aggregate functions and the query tree is separated into a parent query tree and a sub-query tree. The query is then performed using the parent query tree and the sub-query tree.
It simplifies the process of writing query statements with nested aggregate function calls, improving writing efficiency.
Smart Images

Figure CN116610697B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of database technology, and in particular, to a query method of a database query statement, a storage medium and an apparatus. BACKGROUND
[0002] Structured Query Language (SQL) is a standard language for databases and is a very versatile database language. For SQL statements, there is a special function-aggregate function. The special feature of the aggregate function is that its input is a set of rows, rather than a single row. For example, the MAX function finds the maximum value from a set of row data; the AVG function calculates the average of a set of row data.
[0003] In the use of databases, sometimes the returned result of the aggregate function needs to be aggregated again, for example, first use the MAX function to find the highest salary in each department, and then use the AVG function to calculate the average of the maximum salary in all departments. However, due to the current database processing method of SQL statements, the SQL statement for aggregating the returned result of the aggregate function again is relatively complex, which greatly limits the user's SQL statement writing work. SUMMARY
[0004] An object of the present application is to provide a query method of a database query statement, a storage medium and an apparatus that can solve any of the above problems.
[0005] In particular, the present application provides a query method of a database query statement, comprising:
[0006] obtaining a query statement;
[0007] performing syntax analysis on the query statement to obtain a syntax tree;
[0008] performing semantic analysis on the syntax tree and constructing an initial query tree and a target column list;
[0009] checking whether the query statement has a nested call of an aggregate function;
[0010] if so, separating the initial query tree into a parent query tree and a child query tree according to the target column list;
[0011] performing a query using the parent query tree and the child query tree.
[0012] Optionally, the step of performing semantic analysis on the syntax tree comprises:
[0013] checking whether the target expression of the query statement has a nested call of an aggregate function, and recording mark information if so;
[0014] The step of checking whether the query statement has a nested call of the aggregation function comprises:
[0015] The step of judging whether the query statement has a nested call of the aggregation function according to the mark information.
[0016] Optionally, the step of performing semantic analysis on the syntax tree further comprises:
[0017] adding a table referenced by an expression of a FROM clause in the query statement to the initial query tree;
[0018] adding the target expression to the target column list and marking as an output target column.
[0019] Optionally, the step of performing semantic analysis on the syntax tree further comprises:
[0020] adding an expression of a first preset type of clause in the query statement to the target column list and marking as a non-output target column.
[0021] Optionally, the first preset type of clause comprises an ORDER BY clause, a GROUP BY clause and an OVER clause.
[0022] Optionally, the step of separating the initial query tree into a parent query tree and a child query tree according to the target column list comprises:
[0023] constructing a new query tree as the parent query tree and adding the initial query tree as the child query tree to the parent query tree;
[0024] traversing an expression in the target column list corresponding to the child query tree, judging whether the expression is an output target column, and if so, adding the expression to a target column list of the parent query tree;
[0025] if not, judging whether the expression belongs to a GROUP BY clause, and if so, keeping the expression in the target column list of the child query tree, and if not, deleting the expression;
[0026] after the traversal of the expression in the target column list corresponding to the child query tree is completed, traversing an expression in the target column list of the parent query tree, checking whether the expression in the target column list of the parent query tree contains an aggregation function as a parameter, and if so, back-pushing the parameter to the target column list of the child query tree.
[0027] Optionally, the step of performing semantic analysis on the syntax tree further comprises:
[0028] checking whether an expression of a second preset type of clause meets a preset requirement, and if there is a clause that does not meet the preset requirement, outputting error information.
[0029] Optionally, the second preset type of clause includes a WHERE clause, a HAVING clause, a LIMIT clause and a DISTINCT clause, and the step of checking whether the expression of the second preset type of clause meets the preset requirement comprises:
[0030] checking whether the expression type of the WHERE clause in the query statement is a Boolean type, and if yes, then it meets the requirement;
[0031] checking whether the expression type of the HAVING clause in the query statement is a Boolean type, and if yes, then it meets the requirement;
[0032] checking whether the expression type of the LIMIT clause in the query statement is a long integer type, and if yes, then it meets the requirement;
[0033] checking whether the expression of the DISTINCT clause in the query statement exists in the ORDER BY clause, and if yes, then it meets the requirement.
[0034] According to another aspect of the present application, there is also provided a machine readable storage medium having stored thereon a machine executable program which, when executed by a processor, implements the query method of the database query statement according to any one of the above.
[0035] According to still another aspect of the present application, there is also provided a computer device comprising a memory, a processor and a machine executable program stored on the memory and running on the processor, and the processor implements the query method of the database query statement according to any one of the above when executing the machine executable program.
[0036] The query method of the database query statement of the present application separates the query tree into a parent query tree and a child query tree according to the target column list in the case that the query statement has a nested call of an aggregate function by constructing the query tree and the target column list in the semantic analysis stage. That is, the query of the query statement in the preset form is realized by optimizing the semantic analysis stage of the query statement and using the hierarchical query tree, so that the server can process the simplified query statement with the nested call of the aggregate function, thereby simplifying the writing process of the query statement with the nested call of the aggregate function and helping to improve the writing efficiency.
[0037] The above and other objects, advantages and features of the present application will become more apparent from the following detailed description of specific embodiments thereof, when taken in conjunction with the accompanying drawings. BRIEF DESCRIPTION OF DRAWINGS
[0038] Some specific embodiments of the present application will be described in detail with reference to the attached drawings. In the drawings, like reference numerals refer to like parts, and in which:
[0039] Figure 1 is a schematic flow chart of a query method of a database query statement according to an embodiment of the present application;
[0040] Figure 2 is a schematic flow chart of a step of semantic parsing of a syntax tree in the query method of the database query statement according to an embodiment of the present application;
[0041] Figure 3 is a schematic flow chart of a step of separating a query tree into a parent query tree and a child query tree according to a target column list in the query method of the database query statement according to an embodiment of the present application;
[0042] Figure 4 is a schematic flow chart of a query method of a database query statement according to another embodiment of the present application;
[0043] Figure 5 is a schematic flow chart of a step of semantic parsing of a syntax tree in the query method of the database query statement according to another embodiment of the present application;
[0044] Figure 6 is a schematic flow chart of a step of rewriting a query tree in the query method of the database query statement according to an embodiment of the present application;
[0045] Figure 7 is a schematic diagram of a machine readable storage medium according to an embodiment of the present application;
[0046] Figure 8 is a schematic diagram of a computer device according to an embodiment of the present application. DETAILED DESCRIPTION
[0047] Those skilled in the art should understand that the embodiments described below are only a part of the embodiments of the present application, and are not intended to limit the protection scope of the present application. Based on the embodiments provided by the present application, all other embodiments obtained by those of ordinary skill in the art without creative labor should fall within the protection scope of the present application.
[0048] It is to be appreciated that the logic and / or steps represented in the flow diagrams and / or described herein, for example, can be considered as a sequence of executable instructions, and can be embodied in any computer-readable medium for use by an instruction execution system, apparatus, or device, such as a computer-based system, processor-containing system, or other system that can fetch the instructions from the instruction execution system, apparatus, or device and execute the instructions, or a combination thereof.
[0049] Currently, the SQL (Structured Query Language) statement for twice aggregation of the returned result of the aggregation function is complex. For example, for the single aggregation of the highest salary of the employee in each department in the employee table, the SQL statement is: select max(salary) from employees group by dep_id. If the average value of the highest salary is calculated, that is, twice aggregation, the SQL statement is: select avg(max_salary) from (select max(salary) as max_salary from employees group by dep_id). It can be seen that the SQL statement for twice aggregation is very complex to write.
[0050] However, by using the improved query method of the present application, the SQL statement can be simplified. For the above-mentioned twice query, it can be simplified to: select avg(max(salary)) from employees group by dep_id, thereby improving the writing efficiency.
[0051] In the following, the query method of the present application is described in combination with embodiments.
[0052] As shown in FIG. 1, in one embodiment, the query method of the database query statement generally comprises: Figure 1
[0053] Step S101, obtaining the query statement. Specifically, that is, receiving the SQL statement to be executed.
[0054] Step S102, performing syntax analysis on the query statement to obtain a syntax tree. Specifically, that is, checking whether the query statement conforms to the syntax rules, such as whether the various symbols are correct, whether the spelling of the keywords is correct, and the like. Finally, an internal representation of the SQL statement in the database is obtained, that is, a syntax tree. The syntax tree is a tree composed of various levels of structure bodies directly describing the query syntax.
[0055] Step S103, semantic parsing is performed on the syntax tree, and an initial query tree and a target column list are constructed. The semantic parsing on the syntax tree is also the semantic parsing on the query statement, for example, checking whether the relation name and the attribute name in the query statement exist and are valid. In the case where the query statement does not have errors, the initial query tree and the target column list are constructed. The query tree is the internal representation of the relational algebra expression of the SQL statement in the database.
[0056] Referring to Figure 2 As shown in the figure, specifically, step S103 includes:
[0057] Step S201, checking whether the target list expression of the query statement has a nested call of the aggregation function, and recording the mark information if the nested call exists. Specifically, the target list expression is the expression after select. Taking the query statement select avg(max(salary)) from employees group by dep_id as an example, the target list expression is avg(max(salary)). By checking the target list expression, it can be known whether the nested call of the aggregation function exists, that is, the secondary aggregation. For avg(max(salary)), the keywords of the aggregation functions avg and max are checked, and therefore it is determined that the nested call of the aggregation function exists.
[0058] Step S202, adding the table referenced by the expression of the FROM clause in the query statement to the initial query tree. Specifically, the FROM clause identifies the data source of the query statement, that is, identifies which table is queried. By processing the expression of the FROM clause, the table name therein is added to the initial query tree, that is, as the query table of the initial query tree.
[0059] Step S203, adding the target list expression to the target column list and marking it as an output target column. Specifically, taking the query statement select avg(max(salary)) from employees group by dep_id as an example, that is, adding the target list expression avg(max(salary)) to the target column list and marking it as an output target column.
[0060] Step S204, adding the expression of the first preset type of clause in the query statement to the target column list and marking it as a non-output target column. The first preset type of clause includes the ORDER BY clause, the GROUP BY clause and the OVER clause. Specifically, that is, adding the expression following the keyword of the first preset type of clause to the target column list and marking it as a non-output target column.
[0061] Step S205: Check whether the expressions of the clauses in the second preset category meet the preset requirements. If there are clauses that do not meet the preset requirements, output an error message. The clauses in the second preset category include WHERE clause, HAVING clause, LIMIT clause, and DISTINCT clause. The WHERE clause is used for conditional filtering, and the HAVING clause is also used for conditional filtering, but the HAVING clause needs to be used after the GROUP BY clause. The LIMIT clause is used to limit the number of results returned. The DISTINCT clause is used to limit the value of the variable in the current range to eliminate duplicate values in subsequent queries. Specifically, this step includes:
[0062] The query process checks whether the expression type of the WHERE clause is Boolean; if so, it meets the requirements. It also checks whether the expression type of the HAVING clause is Boolean; if so, it meets the requirements. Furthermore, it checks whether the expression type of the LIMIT clause is long integer; if so, it meets the requirements. Finally, it checks whether the expression in the DISTINCT clause exists in the ORDER BY clause; if so, it meets the requirements. If any clause fails to meet the requirements, an error message is output, and the query process is stopped.
[0063] Step S104: Check if the query statement contains nested calls to aggregate functions. Specifically, determine if the query statement contains nested calls to aggregate functions based on the marking information. During semantic parsing, the existence of nested calls to aggregate functions has already been determined based on the target column expression, and the marking information has been recorded. By checking the marking information, it is possible to check if the query statement contains nested calls to aggregate functions.
[0064] Step S105: If it exists, separate the initial query tree into a parent query tree and a subquery tree according to the target column list.
[0065] Reference Figure 3 As shown, specifically, this step includes:
[0066] Step S301: Construct a new query tree as the parent query tree, and add the initial query tree as a sub-query tree to the parent query tree. Specifically, this means constructing a new query tree as the parent query tree, and adding the initial query tree as a table (i.e., the data source) to the parent query tree.
[0067] Step S302: Traverse the expressions in the target column list corresponding to the subquery tree. Specifically, process each expression in the target column list corresponding to the initial query tree one by one.
[0068] Step S303, it is judged whether the expression is the output target column, if yes, step S304 is executed, if no, step S308 is executed. Specifically, the expressions added to the target column list in the semantic analysis stage are all marked, according to the mark, it can be checked whether the expression is the output target column.
[0069] Step S304, the expression is added to the target column list of the parent query tree. If the expression is the output target column, the expression is pulled up to the parent query tree. Taking the query statement select avg(max(salary)) from employees group by dep_id as an example, in the semantic analysis stage, the expression "avg(max(salary))" is added to the target column list of the query tree and marked as the output target column. Therefore, in the stage of building the new query tree, the expression "avg(max(salary))" is added to the target column list of the parent query tree.
[0070] Step S305, the expressions in the target column list of the parent query tree are traversed. Specifically, after the expressions in the target column list corresponding to the subquery tree are traversed, the expressions in the target column list of the parent query tree are traversed.
[0071] Step S306, it is checked whether the expression in the target column list of the parent query tree contains an aggregation function as a parameter, if yes, step S307 is executed. That is, it is checked whether the expression is an aggregation function containing a nested aggregation function, the inner aggregation function in the nested aggregation function is the parameter of the outer aggregation function, that is, the aggregation function as a parameter.
[0072] Step S307, the parameter is pushed back to the target column list of the subquery tree. That is, the inner aggregation is pushed down to the subquery tree. For example, the expression "avg(max(salary))", "max(salary)" is the inner aggregation, as the parameter of the outer aggregation avg, so "max(salary)" is pushed back to the target column list of the subquery tree. While the outer aggregation remains in the parent query tree.
[0073] Step S308, it is judged whether the expression belongs to the GROUP BY clause, if yes, step S309 is executed, if no, step S310 is executed. Specifically, in the semantic analysis stage, the expressions of the ORDER BY clause, the GROUP BY clause and the OVER clause are added to the target column list and marked as non-output target columns. That is, the non-output target column may belong to the ORDER BY clause, the GROUP BY clause or the OVER clause, so it is judged whether the expression belongs to the GROUP BY clause.
[0074] Step S309, keep the expression in the target column list of the subquery tree. If the expression belongs to the GROUP BY clause, keep the expression in the target column list of the subquery tree.
[0075] Step S310, delete the expression. If the expression does not belong to the GROUP BY clause, delete the expression, that is, the expression is not processed in the execution process.
[0076] Step S106, query using the parent query tree and the subquery tree. After the parent query tree and the subquery tree are constructed, the query statement is executed using the parent query tree and the subquery tree.
[0077] It should be noted that if the query statement does not exist nested call of the aggregation function, the query statement is executed using the initial query tree.
[0078] In the scheme of the embodiment, by constructing the query tree and the target column list in the semantic analysis stage, the query tree is separated into the parent query tree and the subquery tree according to the target column list in the case that the query statement exists nested call of the aggregation function. That is, by optimizing the semantic analysis stage of the query statement, the query of the query statement in the preset form is implemented by using the hierarchical query tree, so that the server can process the query statement with the nested call of the aggregation function after simplification, thereby simplifying the writing process of the query statement with the nested call of the aggregation function and helping to improve the writing efficiency.
[0079] Referring to Figure 4 As shown in the figure, in one embodiment, the query method of the database query statement generally comprises:
[0080] Step S401, receiving the query statement sent by the client. That is, receiving the SQL statement to be executed.
[0081] Step S402, performing syntax analysis on the query statement to obtain a syntax tree. Specifically, that is, checking whether the query statement conforms to the syntax rule, and finally obtaining the representation form of the SQL statement in the database, that is, the syntax tree.
[0082] Step S403, performing semantic analysis on the syntax tree to construct an initial query tree. The query statement is semantically analyzed to construct the query tree and the target column list.
[0083] Referring to Figure 5 As shown in the figure, this step comprises:
[0084] Step S501, the target expression in the query statement is processed. Specifically, the target expression after select is processed. It includes checking whether the target expression in the query statement exists nested call of the aggregation function, if exists, recording the mark information. And adding the target expression to the target column list, marking as the output target column.
[0085] Step S502, the FROM clause in the query statement is processed. Specifically, the expression referenced table in the FROM clause in the query statement is added to the query tree.
[0086] Step S503, the WHERE clause in the query statement is processed. Specifically, the expression type of the WHERE clause is checked whether it is a boolean type.
[0087] Step S504, the HAVING clause in the query statement is processed. Specifically, the expression type of the HAVING clause is checked whether it is a boolean type.
[0088] Step S505, the LIMIT clause in the query statement is processed. Specifically, the expression type of the LIMIT clause is checked whether it is a long integer type.
[0089] Step S506, the DISTINCT clause in the query statement is processed. Specifically, it is checked whether the expression of the DISTINCT clause exists in the ORDER BY clause.
[0090] Step S507, the ORDER BY clause in the query statement is processed. Specifically, the expression of the ORDER BY clause is added to the target column list, and marked as a non-output target column.
[0091] Step S508, the GROUP BY clause in the query statement is processed. Specifically, the expression of the GROUP BY clause is added to the target column list, and marked as a non-output target column.
[0092] Step S509, the OVER clause in the query statement is processed. Specifically, the expression of the ORDER BY and PARTITION BY clauses inside the OVER clause is added to the target column list, and marked as a non-output target column.
[0093] Step S404, in the case that the query statement exists nested call of the aggregation function, the initial query tree is rewritten. Specifically, in the case that the query statement exists nested call of the aggregation function, the initial query tree is separated into a parent query tree and a child query tree according to the target column list.
[0094] Referring to Figure 6 This step includes:
[0095] Step S601, a new query tree is constructed as a parent query. Specifically, the new query tree is constructed as a parent query tree, and the initial query tree is added to the parent query tree as a data source.
[0096] Step S602, the target column list corresponding to the child query tree is traversed. Specifically, the expressions in the target column list corresponding to the query tree formed in the semantic analysis stage are processed one by one.
[0097] Step S603, it is detected whether the expression is an output target column. If yes, step S604 is executed, and if no, step S607 is executed.
[0098] Step S604, the expression is pulled up to the parent query tree. That is, the expression is added to the target column list of the parent query tree.
[0099] Step S605, the target column list of the parent query is traversed. Specifically, after the expressions in the target column list corresponding to the child query tree are traversed, the expressions in the target column list of the parent query tree are processed.
[0100] Step S606, the nested aggregation function parameter expression is pushed back to the child query tree. Specifically, it is checked whether the expression marked as an output target column contains a nested aggregation function, and in the case of containing a nested aggregation function, the inner aggregation expression of the nested aggregation function is pushed back to the child query tree.
[0101] Step S607, it is detected whether the expression belongs to a GROUP BY clause. If yes, step S608 is executed, and if no, step S609 is executed.
[0102] Step S608, the expression is retained in the child query tree. The expression is retained in the target column list of the child query tree.
[0103] Step S609, the expression is deleted from the child query tree. That is, the expression is no longer processed in the execution process.
[0104] Step S405, the query tree is rewritten. Specifically, in the case of the query statement containing a nested called aggregation function, the query tree to which this step is directed is the hierarchical query tree, that is, the query tree including the child query tree and the parent query tree. In the case of the query statement not containing a nested called aggregation function, the query tree to which this step is directed is the initial query tree. This step is to perform algebraic optimization and physical optimization on the query tree, wherein the algebraic optimization and the physical optimization are prior art and will not be described here.
[0105] Step S406, generating a query path and a query plan. Specifically, the optimizer of the server generates all possible query paths according to the rules configured by itself, and selects the path with the minimum cost as the query plan according to the cost size.
[0106] Step S407, executing the query plan and returning the result. Specifically, the query statement is executed according to the query plan and the result is returned.
[0107] That is, steps S405-S407 are the process of executing the query statement according to the query tree.
[0108] In the scheme of the embodiment, by constructing the query tree and the target column list in the semantic analysis stage, in the case that the query statement has a nested call of the aggregation function, the query tree is separated into the parent query tree and the child query tree according to the target column list. That is, by optimizing the semantic analysis stage of the query statement, the query of the query statement in the preset form is realized by using the hierarchical query tree, so that the server can process the query statement with the simplified nested call of the aggregation function, thereby simplifying the writing process of the query statement with the nested call of the aggregation function, and helping to improve the writing efficiency.
[0109] The embodiment further provides a machine readable storage medium and a computer device. Figure 7 FIG. 1 is a schematic diagram of a machine readable storage medium 10 according to an embodiment of the present application. Figure 8 FIG. 2 is a schematic diagram of a computer device 20 according to an embodiment of the present application.
[0110] The machine readable storage medium 10 has a machine executable program 11 stored thereon, and the machine executable program 11 realizes the query method of the database query statement of any of the above embodiments when executed by a processor.
[0111] The computer device 20 can include a memory 210, a processor 220, and a machine executable program 11 stored on the memory 210 and running on the processor 220, and the processor 220 realizes the query method of the database query statement of any of the above embodiments when executing the machine executable program 11.
[0112] It should be noted that the logic and / or steps represented in the flowchart or otherwise described herein, for example, can be considered as a list of executable instructions for implementing logical functions, which can be specifically implemented in any machine readable storage medium for use by an instruction execution system, device or apparatus, such as a computer-based system, a system including a processor, or other system that can fetch and execute instructions from the instruction execution system, device or apparatus, or in conjunction with these instruction execution systems, devices or apparatus.
[0113] For the description of this embodiment, the machine-readable storage medium 10 can be any means for storing, communicating, propagating or transporting a program for use by or in connection with an instruction execution system, apparatus or device. More specific examples (a non-exhaustive list) of the computer-readable medium include the following: an electrical connection having one or more wires (electrical apparatus), a portable computer diskette (magnetic apparatus), a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or Flash memory), an optical fiber, and a portable compact disc read-only memory (CDROM). In addition, the machine-readable storage medium 10 can even be paper or another suitable medium on which the program is printed, since the program can be electronically obtained, for example by optical scanning of the paper or other medium, followed by electronic conversion into a useable form, and then stored in a computer memory.
[0114] It should be understood that parts of the application can be implemented in hardware, software, firmware or a combination thereof. In the above embodiments, a number of steps or methods can be implemented in software or firmware stored in a memory and executed by a suitable instruction execution system.
[0115] The computer device 20 can be, for example, a server, a desktop computer, a notebook computer, a tablet computer or a smartphone. In some examples, the computer device 20 can be a cloud computing node. The computer device 20 can be described in the general context of computer system executable instructions, such as program modules, being executed by a computer system. Generally, program modules can include routines, programs, objects, components, logic, data structures, etc. that perform particular tasks or implement particular abstract data types. The computer device 20 can be implemented in a distributed cloud computing environment in which remote processing devices are linked through a communication network. In a distributed cloud computing environment, program modules can be located on local or remote computer system storage media including storage devices.
[0116] The computer device 20 can include a processor 220 suitable for executing stored instructions, a memory 210 providing temporary storage space for the operation of the instructions during operation. The processor 220 can be a single-core processor, a multi-core processor, a computing cluster or any number of other configurations. The memory 210 can include random access memory (RAM), read-only memory, flash memory or any other suitable storage system.
[0117] The processor 220 can be connected through the system interconnect (e.g., PCI, PCI-Express, etc.) to an I / O interface (input / output interface) adapted to connect the computer device 20 to one or more I / O devices (input / output devices). The I / O devices can include, for example, a keyboard and a pointing device, where the pointing device can include a touchpad or a touchscreen, etc. The I / O devices can be built-in components of the computer device 20, or can be devices externally connected to the computer device.
[0118] The processor 220 can also be linked through the system interconnect to a display interface adapted to connect the computer device 20 to a display device. The display device can include a display screen that is a built-in component of the computer device 20. The display device can also include a computer monitor, a television, or a projector, etc. that is externally connected to the computer device 20. In addition, a network interface controller (NIC) can be adapted to connect the computer device 20 to a network through the system interconnect. In some embodiments, the NIC can use any suitable interface or protocol (such as Internet Small Computer System Interface, etc.) to transfer data. The network can be a cellular network, a radio network, a wide area network (WAN), a local area network (LAN), or the Internet, etc. Remote devices can be connected to the computer device through the network.
[0119] The flowcharts provided in the embodiments are not intended to indicate that the operations of the methods will be executed in any particular order, or that all of the operations included in the methods will be included in every implementation of the methods. Additionally, the methods can include additional operations. Additional changes can be made to the above-described methods within the scope of the technical ideas provided by the embodiments.
[0120] Up to now, those skilled in the art should recognize that, although the multiple exemplary embodiments of the present application have been shown and described in detail herein, many other variations and modifications that do not depart from the spirit and scope of the present application can be directly determined or deduced from the disclosure of the present application according to the technical ideas provided by the embodiments. Therefore, the scope of the present application should be understood and recognized as covering all such other variations or modifications.
Claims
1. A query method for a database query statement, comprising: Retrieve the query statement; The query statement is parsed to obtain a syntax tree; Semantic parsing is performed on the syntax tree, and an initial query tree and a target column list are constructed. Check if the query statement contains nested calls to aggregate functions; If it exists, the initial query tree is separated into a parent query tree and a sub-query tree according to the target column list; Perform a query using the parent query tree and the subquery tree; The steps for semantic parsing the syntax tree include: Check if there are nested calls to aggregate functions in the target column expression of the query statement; if so, record the marking information. The step of checking whether the query statement contains nested calls to aggregate functions includes: Based on the marking information, determine whether the query statement contains nested calls to aggregate functions; The step of semantic parsing the syntax tree further includes: Add the table referenced by the expression in the FROM clause of the query statement to the initial query tree; Add the target column expression to the target column list and mark it as the output target column; If the query statement contains a clause of a first preset type, the step of semantically parsing the syntax tree further includes: Add the expression of the first preset type clause in the query statement to the target column list and mark it as a non-output target column; The step of separating the initial query tree into a parent query tree and a subquery tree based on the target column list includes: Construct a new query tree as the parent query tree, and add the initial query tree as the child query tree to the parent query tree; Traverse the expressions in the target column list corresponding to the subquery tree, determine whether the expression is an output target column, and if so, add the expression to the target column list of the parent query tree; If not, determine whether the expression belongs to the GROUP BY clause. If yes, keep the expression in the target column list of the subquery tree. If no, delete the expression. After traversing the expressions in the target column list corresponding to the subquery tree, traverse the expressions in the target column list of the parent query tree, and check whether the expressions in the target column list of the parent query tree contain aggregate functions as parameters. If so, push the parameters back to the target column list of the subquery tree.
2. The query method for the database query statement according to claim 1, wherein, The first preset type of clauses includes the ORDER BY clause, the GROUP BY clause, and the OVER clause.
3. The query method for the database query statement according to claim 1, wherein, If the query statement contains a clause of a second preset type, the step of semantically parsing the syntax tree further includes: Check whether the expression of the clause of the second preset type meets the preset requirements. If there is a clause that does not meet the preset requirements, output an error message.
4. The query method for the database query statement according to claim 3, wherein, The second preset type of clauses includes WHERE clauses, HAVING clauses, LIMIT clauses, and DISTINCT clauses. The step of checking whether the expression of the clause of the second preset type meets the preset requirements includes: Check if the expression type of the WHERE clause in the query statement is Boolean; if it is, then it meets the requirements. Check if the expression type of the HAVING clause in the query statement is Boolean; if so, it meets the requirements. Check if the expression type of the LIMIT clause in the query statement is a long integer. If it is, then it meets the requirements. Check if the expression in the DISTINCT clause of the query statement exists in the ORDER BY clause. If it does, then the requirement is met.
5. A machine-readable storage medium having a machine-executable program stored thereon, wherein the machine-executable program, when executed by a processor, implements a query method for a database query statement according to any one of claims 1 to 4.
6. A computer device comprising a memory, a processor, and a machine-executable program stored in the memory and running on the processor, wherein the processor, when executing the machine-executable program, implements a query method for a database query statement according to any one of claims 1 to 4.
Citation Information
Patent Citations
Query sentence optimization device and query sentence optimization method
CN102902778A
System and method of query transformation
US20050010570A1
Database query mechanism using links as an aggregate base
US20120215810A1