Complex query rewriting

A method, a system, and a computer program product for rewriting queries. A received query is parsed into a plurality of subqueries, where each subquery has one or more query elements. One or more identical subqueries are identified and grouped into one or more groups. Based on the groups of subqueries, an alias parameter is assigned to each identical subquery. The identical subqueries in the received query are replaced with corresponding aliases. An expression language statement is generated based on the received query, where each identical subquery is replaced with the corresponding assigned alias parameter in the expression language. The generated expression language statement is executed.

TECHNICAL FIELD

This disclosure relates generally to data processing and, in particular, to rewriting of complex query statements, such as, for example, rewriting of complex SQL statements using common table expressions syntax.

BACKGROUND

Database management systems have become an integral part of many computer systems. For example, some systems handle hundreds if not thousands of transactions per second. On the other hand, some systems perform very complex multidimensional analysis on data. In both cases, the underlying database may need to handle responses to queries very quickly in order to satisfy systems requirements with respect to transaction time. Execution of a query typically requires generation of a query plan or query execution plan, which is an ordered set of operations that is used to access stored data (e.g., access data in a SQL relational database management system). Upon submission of a query to the database system, requested data is retrieved based on parameters of the query. Given complexities of modern-day queries and/or their volumes, the underlying database systems face challenges, such as, significant compute resource and memory consumption, total cost of ownership, extended times for execution of complex queries, etc.

SUMMARY

In some implementations, the current subject matter relates to a computer implemented method for rewriting queries. The method may include parsing a received query into a plurality of subqueries, each subquery in the plurality of subqueries having one or more query elements, identifying one or more identical subqueries in the plurality of subqueries and grouping the identified one or more subqueries into one or more groups, assigning, based on the one or more groups of subqueries, an alias parameter to each identical subquery in the plurality of subqueries, replacing one or more identical subqueries in the received query with corresponding aliases, generating an expression language statement based on the received query, wherein each identical subquery is replaced with the corresponding assigned alias parameter in the expression language, and executing the generated expression language statement.

In some implementations, the current subject matter may include one or more of the following optional features. The parsing of the query (e.g., may be performed by a parser) may include generation of a syntactical tree having a plurality nodes. Each node in the plurality of nodes may correspond to a subquery in the plurality of subqueries. Further, the generated expression language statement may be generated using the generated syntactical tree. The syntactical tree may be configured to define a structure of the received query. Moreover, the identified identical subqueries may be grouped into one or more groups using one or more parent nodes in the syntactical tree of nodes including the identical queries. Further, the alias parameters may be configured to be assigned using parent nodes in the syntactical tree.

In some implementations, the generated expression language statement may be a common table expressions (CTE) language statement. The received query may be a structured query language (SQL) statement.

In some implementations, the generated expression language statement may be executed by a database system (e.g., HANA system as developed by SAP SE, Walldorf, Germany) for the purposes of retrieving data queried by the received query.

DETAILED DESCRIPTION

To address these and potentially other deficiencies of currently available solutions, one or more implementations of the current subject matter relate to methods, systems, articles of manufacture, and the like that can, among other possible advantages, provide an ability to rewrite complex query statements to reduce memory consumption and/or improve execution times of such queries, whereby complex queries may be rewritten using common table expressions syntax.

In some implementations, the current subject matter may rewrite complex query statements (e.g., structured query language (SQL) statements) at a syntactic level, which may provide reduced memory consumption, shorter execution times, and lower total cost of ownership. To access various data that may be stored in various database systems, a user may access a user interface, where the user may enter one or more search parameters, keywords, and/or any other information. The entered information may then be converted into SQL statements by a backend system (that may be communicatively coupled to the user interface). The generated SQL statements may be then transmitted to the database systems (e.g., High Performance Analytic Appliance (“HANA”) system as developed by SAP SE, Walldorf, Germany) for execution. Since user input may be unrestricted, the models generated by the user in the user interface and, hence, the corresponding SQL statements may become very long and complex. The longer and more complex an SQL statement is, the more processing time and memory may be required in the database to successfully execute the generated SQL statements. Further, it may be difficult for query optimization components (e.g., a database optimizer) of the database system, receiving these statements, to determine and select an optimal query execution plan. This is due to an exponentially rising number of possible execution paths that may result from the complexity of the generated query statements.

On the user side, such large amount of memory and processing time required by complex query statement may lead to long wait times, timeouts, out-of-memory errors, and/or any other problems. Moreover, database system may need to provision large amounts of memory to ensure coverage of memory usage spikes that may be caused by the execution of the complex query statements. This leads to a high total cost of ownership as a lot of memory may need to be constantly provisioned to ensure coverage for occasionally occurring complex query statements.

However, for execution of these statements much less memory may actually be used and/or needed. As such, the current subject matter may be configured to reduce runtime and memory requirements of complex query statements. The reduction of complexity of problematic query statements may be configured to occur at a different level of abstraction than is the user created model, e.g., after the query statement is generated based on a user model. This level may correspond to the abstract syntax representation of a query statement, e.g., the generated query statement may be parsed and transformed into a syntax tree representation where a tree of nodes may represent an actual query string with its different language elements, operators, operands, etc. This tree may then be manipulated to transform the query statement syntactically, while preserving semantic equivalence. The computed result of the syntactically changed query statement may be the same as the original, more complex query statement.

In some exemplary implementations, the original query statement may be transformed and/or simplified at a syntactic level using a common table expressions (CTE) language element(s). In some implementations, the current subject matter may be configured to identify identical subqueries within the main original query statement, based on its syntax tree representation, and group them into groups of identical subqueries. For each subquery in the groups, their parent subquery within the syntax tree may be collected and again grouped into identical subqueries, until no more parent candidate subqueries are found. This way, commencing with the most fine-grained subqueries, larger, and larger identical subqueries may be detected. The identical subqueries of each group may then be transferred into a clause of a rewritten query (e.g., WITH clause according to the CTE syntax) and assigned an alias. In the main body of the rewritten query (e.g., SELECT statement), the identical subqueries may be removed and replaced by the reference to this assigned alias. Thus, this approach may allow for reuse of identical subqueries, which may lead to a reduction in complexity of the original query statement. Further, the actual query statement string may be obtained from the syntax tree, which may then be transmitted to the database system (e.g., HANA) for execution.

FIG. 1illustrates an exemplary system100for processing of queries, according to some implementations of the current subject matter. The system100may include one or more users (user1, user2, . . . user n)102, a query rewriting component or engine104, and a database system106. The users102, the query engine104, and the database system106may be communicatively coupled with one another using any type of network, including but not limited to, wired, wireless, and/or a combination of both. The users102may include at least one of the following: computer processors, computing networks, software applications, servers, user interfaces, and/or any combination of hardware and/or software components. Database system106may include at least one of the following: databases, storage locations, memory locations, and/or any combination of hardware and/or software components. In some implementations, the database system106may be a High Performance Analytic Appliance (“HANA”) system as developed by SAP SE, Walldorf, Germany, as will be described below.

The engine104may include any combination of software and/or hardware components and may be configured to receive an original query from one or more users102to obtain data from the database system106. The query may be already converted into a query language statements, e.g., SQL statements. The engine104may then be configured to execute rewriting of the SQL query using common table expressions syntax to generate a rewritten query that may then be transmitted to the database system106for execution, as shown inFIG. 2.

FIG. 2illustrates an exemplary process200for rewriting a query, according to some implementations of the current subject matter. At202, a query may be received. The query may be generated by the user102(as shown inFIG. 1) and may be transformed into query language expressions (e.g., SQL statements). The query may include various parameters that may identify the data being sought and stored in the database system106.

At204, the engine104may be configured to parse the received query statements and generate a syntax tree. An exemplary syntax tree300is illustrated inFIG. 3a. The syntax tree may include one or more nodes, where each node may include a subquery (e.g., subquery302, as shown inFIG. 3a).FIG. 3billustrates an exemplary syntax310that may be used in the generation of the syntax tree300shown inFIG. 3a. Each subquery node may also include further child subquery nodes (e.g., node304), as shown inFIG. 3a.

At206, elementary subqueries may be identified and collected.FIG. 4illustrates exemplary elementary subqueries402and404. Any identical subqueries may be grouped into groups of identical subqueries, at208. For example, a subquery statement “select a from complex_query” may be repeated (or be identical to) other parts of the elementary subqueries402and404and, hence, may be part of a first identical subqueries group406. A subquery statement “select b from complex_query2” may be also repeated in (or be identical to) other parts of the elementary subqueries402,404, and thus, may part of a second identical subqueries group408, as shown inFIG. 4.

At210, parents of each subquery groups may be identified and groups of identical parent subqueries may be identified.FIG. 5illustrates identification and grouping of identical parent subqueries.FIG. 5illustrates elementary subqueries402,404shown inFIG. 4along with subquery groups406,408, where subquery group406may include a parent subquery502starting with “select a from” and subquery group408may include a parent subquery504starting with “select b from”. As shown inFIG. 5, parent subqueries502and504(similar to their child nodes) may be repeated in other parents of the elementary subqueries402,404and hence may be grouped together.

At212, a determination may be made whether there are any more identical parent subqueries (e.g., such as identical parent subqueries602as shown inFIG. 6). If there are additional identical parent subqueries, the process200may return to210and repeated until there are no more identical parent subqueries left for grouping. Otherwise, the processing may proceed to214, where lower identical subqueries that appear only once per upper subquery may be disregarded, as shown inFIG. 7(e.g., parent subqueries502and504as shown inFIG. 5) during alias assignment process as well as replacement of identical parent subqueries with aliases (as discussed below).

At216, aliases may be assigned to one or more identical (parent) subqueries for addition to common table expression (CTE) syntax statement (e.g., WITH statement). At218, a CTE syntax tree that may include identical subqueries and assigned aliases may be generated. The assignment of aliases may begin with the lowest subquery in the group. For example, a first subquery406(as shown inFIG. 4) may be added to the CTE statement800, where the first subquery406(i.e., “select a from complex_query”) may be replaced with alias “_WSQ1”801and rewritten as statement802(shown in dashed lines), as shown inFIG. 8. WhileFIG. 8illustrates use of the WITH statement, any other CTE syntax statement may be used. Similarly, the second subquery408(as shown inFIG. 4) may be also added to the CTE statement, where the second subquery408(i.e., “select b from complex_query”) may be replaced with alias “_WSQ2”803and rewritten as statement804(shown in dashed lines). The parent subquery602may be also included in the WITH statement800and rewritten as statement806including alias “_WSQ3”805, as shown inFIG. 8(dashed lines). The aliases801,803,805may be reused in the WITH statement800every time the respective identical query statements need to be executed, as shown by dashed boxes inFIG. 8.

At220, the previously aliased subqueries may then be removed from the main body of the syntax tree and replaced with their alias names. As shown inFIG. 8, the main body of the SELECT statement shows two such replacements using alias “_WSQ3”805(shown in dashed lines).

At222, the generated syntax tree may be converted to a query language (e.g., SQL) string (e.g., string800shown inFIG. 8) that may be executed by the database system106(shown inFIG. 1). The string may include assigned aliases801,803,805and may be transmitted to the database system106for execution, at224.

In some implementations, the current subject matter may be implemented in various in-memory database systems, such as a High Performance Analytic Appliance (“HANA”) system as developed by SAP SE, Walldorf, Germany. Various systems, such as, enterprise resource planning (“ERP”) system, supply chain management system (“SCM”) system, supplier relationship management (“SRM”) system, customer relationship management (“CRM”) system, and/or others, may interact with the in-memory system for the purposes of accessing data, for example. Other systems and/or combinations of systems may be used for implementations of the current subject matter. The following is a discussion of an exemplary in-memory system.

FIG. 9illustrates an exemplary system900in which a computing system902, which may include one or more programmable processors that may be collocated, linked over one or more networks, etc., executes one or more modules, software components, or the like of a data storage application904, according to some implementations of the current subject matter. The data storage application904may include one or more of a database, an enterprise resource program, a distributed storage system (e.g. NetApp Filer available from NetApp of Sunnyvale, Calif.), or the like.

The one or more modules, software components, or the like may be accessible to local users of the computing system902as well as to remote users accessing the computing system902from one or more client machines906over a network connection910. One or more user interface screens produced by the one or more first modules may be displayed to a user, either via a local display or via a display associated with one of the client machines906. Data units of the data storage application904may be transiently stored in a persistence layer912(e.g., a page buffer or other type of temporary persistency layer), which may write the data, in the form of storage pages, to one or more storages914, for example via an input/output component916. The one or more storages914may include one or more physical storage media or devices (e.g. hard disk drives, persistent flash memory, random access memory, optical media, magnetic media, and the like) configured for writing data for longer term storage. It should be noted that the storage914and the input/output component916may be included in the computing system902despite their being shown as external to the computing system902inFIG. 9.

Data retained at the longer term storage914may be organized in pages, each of which has allocated to it a defined amount of storage space. In some implementations, the amount of storage space allocated to each page may be constant and fixed. However, other implementations in which the amount of storage space allocated to each page may vary are also within the scope of the current subject matter.

FIG. 10illustrates exemplary software architecture1000, according to some implementations of the current subject matter. A data storage application904, which may be implemented in one or more of hardware and software, may include one or more of a database application, a network-attached storage system, or the like. According to at least some implementations of the current subject matter, such a data storage application904may include or otherwise interface with a persistence layer912or other type of memory buffer, for example via a persistence interface1002. A page buffer1004within the persistence layer912may store one or more logical pages1006, and optionally may include shadow pages, active pages, and the like. The logical pages1006retained in the persistence layer912may be written to a storage (e.g. a longer term storage, etc.)914via an input/output component916, which may be a software module, a sub-system implemented in one or more of software and hardware, or the like. The storage914may include one or more data volumes1010where stored pages1012are allocated at physical memory blocks.

In some implementations, the data storage application904may include or be otherwise in communication with a page manager1014and/or a savepoint manager1016. The page manager1014may communicate with a page management module1020at the persistence layer912that may include a free block manager1022that monitors page status information1024, for example the status of physical pages within the storage914and logical pages in the persistence layer912(and optionally in the page buffer1004). The savepoint manager1016may communicate with a savepoint coordinator1026at the persistence layer912to handle savepoints, which are used to create a consistent persistent state of the database for restart after a possible crash.

In some implementations of a data storage application904, the page management module of the persistence layer912may implement a shadow paging. The free block manager1022within the page management module1020may maintain the status of physical pages. The page buffer1004may include a fixed page status buffer that operates as discussed herein. A converter component1040, which may be part of or in communication with the page management module1020, may be responsible for mapping between logical and physical pages written to the storage914. The converter1040may maintain the current mapping of logical pages to the corresponding physical pages in a converter table1042. The converter1040may maintain a current mapping of logical pages1006to the corresponding physical pages in one or more converter tables1042. When a logical page1006is read from storage914, the storage page to be loaded may be looked up from the one or more converter tables1042using the converter1040. When a logical page is written to storage914the first time after a savepoint, a new free physical page is assigned to the logical page. The free block manager1022marks the new physical page as “used” and the new mapping is stored in the one or more converter tables1042.

The persistence layer912may ensure that changes made in the data storage application904are durable and that the data storage application904may be restored to a most recent committed state after a restart. Writing data to the storage914need not be synchronized with the end of the writing transaction. As such, uncommitted changes may be written to disk and committed changes may not yet be written to disk when a writing transaction is finished. After a system crash, changes made by transactions that were not finished may be rolled back. Changes occurring by already committed transactions should not be lost in this process. A logger component1044may also be included to store the changes made to the data of the data storage application in a linear log. The logger component1044may be used during recovery to replay operations since a last savepoint to ensure that all operations are applied to the data and that transactions with a logged “commit” record are committed before rolling back still-open transactions at the end of a recovery process.

With some data storage applications, writing data to a disk is not necessarily synchronized with the end of the writing transaction. Situations may occur in which uncommitted changes are written to disk and while, at the same time, committed changes are not yet written to disk when the writing transaction is finished. After a system crash, changes made by transactions that were not finished must be rolled back and changes by committed transaction must not be lost.

To ensure that committed changes are not lost, redo log information may be written by the logger component1044whenever a change is made. This information may be written to disk at latest when the transaction ends. The log entries may be persisted in separate log volumes while normal data is written to data volumes. With a redo log, committed changes may be restored even if the corresponding data pages were not written to disk. For undoing uncommitted changes, the persistence layer912may use a combination of undo log entries (from one or more logs) and shadow paging.

The persistence interface1002may handle read and write requests of stores (e.g., in-memory stores, etc.). The persistence interface1002may also provide write methods for writing data both with logging and without logging. If the logged write operations are used, the persistence interface1002invokes the logger1044. In addition, the logger1044provides an interface that allows stores (e.g., in-memory stores, etc.) to directly add log entries into a log queue. The logger interface also provides methods to request that log entries in the in-memory log queue are flushed to disk.

Log entries contain a log sequence number, the type of the log entry and the identifier of the transaction. Depending on the operation type additional information is logged by the logger1044. For an entry of type “update”, for example, this would be the identification of the affected record and the after image of the modified data.

When the data application904is restarted, the log entries need to be processed. To speed up this process the redo log is not always processed from the beginning. Instead, as stated above, savepoints may be periodically performed that write all changes to disk that were made (e.g., in memory, etc.) since the last savepoint. When starting up the system, only the logs created after the last savepoint need to be processed. After the next backup operation the old log entries before the savepoint position may be removed.

When the logger1044is invoked for writing log entries, it does not immediately write to disk. Instead it may put the log entries into a log queue in memory. The entries in the log queue may be written to disk at the latest when the corresponding transaction is finished (committed or aborted). To guarantee that the committed changes are not lost, the commit operation is not successfully finished before the corresponding log entries are flushed to disk. Writing log queue entries to disk may also be triggered by other events, for example when log queue pages are full or when a savepoint is performed.

With the current subject matter, the logger1044may write a database log (or simply referred to herein as a “log”) sequentially into a memory buffer in natural order (e.g., sequential order, etc.). If several physical hard disks/storage devices are used to store log data, several log partitions may be defined. Thereafter, the logger1044(which as stated above acts to generate and organize log data) may load-balance writing to log buffers over all available log partitions. In some cases, the load-balancing is according to a round-robin distributions scheme in which various writing operations are directed to log buffers in a sequential and continuous manner. With this arrangement, log buffers written to a single log segment of a particular partition of a multi-partition log are not consecutive. However, the log buffers may be reordered from log segments of all partitions during recovery to the proper order.

As stated above, the data storage application904may use shadow paging so that the savepoint manager1016may write a transactionally-consistent savepoint. With such an arrangement, a data backup comprises a copy of all data pages contained in a particular savepoint, which was done as the first step of the data backup process. The current subject matter may be also applied to other types of data page storage.

In some implementations, the current subject matter may be configured to be implemented in a system1100, as shown inFIG. 11. The system1100may include a processor1110, a memory1120, a storage device1130, and an input/output device1140. Each of the components1110,1120,1130and1140may be interconnected using a system bus1150. The processor1110may be configured to process instructions for execution within the system1100. In some implementations, the processor1110may be a single-threaded processor. In alternate implementations, the processor1110may be a multi-threaded processor. The processor1110may be further configured to process instructions stored in the memory1120or on the storage device1130, including receiving or sending information through the input/output device1140. The memory1120may store information within the system1100. In some implementations, the memory1120may be a computer-readable medium. In alternate implementations, the memory1120may be a volatile memory unit. In yet some implementations, the memory1120may be a non-volatile memory unit. The storage device1130may be capable of providing mass storage for the system1100. In some implementations, the storage device1130may be a computer-readable medium. In alternate implementations, the storage device1130may be a floppy disk device, a hard disk device, an optical disk device, a tape device, non-volatile solid state memory, or any other type of storage device. The input/output device1140may be configured to provide input/output operations for the system1100. In some implementations, the input/output device1140may include a keyboard and/or pointing device. In alternate implementations, the input/output device1140may include a display unit for displaying graphical user interfaces.

FIG. 12illustrates an exemplary method1200for rewriting queries (such as to reduce execution time, reduce memory consumption, etc.), according to some implementations of the current subject matter. At1202, a received query may be parsed into a plurality of subqueries (e.g., as shown inFIGS. 4-8). Each subquery in the plurality of subqueries may have one or more query elements (e.g., its own query elements, language, parameters, operands, etc.). At1204, one or more identical subqueries in the plurality of subqueries may be identified and then grouped into one or more groups (e.g., as shown inFIGS. 4-8). At1206, an alias parameter may be assigned to each identical subquery in the plurality of subqueries based on the one or more groups of subqueries (e.g., “_WSQ1”801, “_WSQ2”803, “_WSQ3”805, as shown in FIG.8). At1208, one or more identical subqueries may be replaced with their alias names in the main body of the received query. At1210, an expression language statement may be generated based on the received query (e.g., as shown by the WITH statement inFIG. 8). Each identical subquery may be replaced with corresponding assigned alias parameter in the expression language. At1212, the generated expression language statement may be executed (e.g., transmitted to the database system and then executed therein).

In some implementations, the current subject matter may include one or more of the following optional features. The parsing of the query (e.g., may be performed by a parser) may include generation of a syntactical tree having a plurality nodes (e.g., syntactical tree300as shown inFIG. 3a). Each node in the plurality of nodes may correspond to a subquery in the plurality of subqueries (as for example is shown inFIGS. 4-8). Further, the generated expression language statement may be generated using the generated syntactical tree. The syntactical tree may be configured to define a structure of the received query (e.g., as shown inFIG. 3a). Moreover, the identified identical subqueries may be grouped into one or more groups (e.g., as shown inFIGS. 4-8) using one or more parent nodes in the syntactical tree of nodes including the identical queries. Further, the alias parameters (e.g., “_WSQ1”801, “_WSQ2”803, “_WSQ3”805, as shown inFIG. 8) may be configured to be assigned using parent nodes in the syntactical tree.

In some implementations, the generated expression language statement may be a common table expressions (CTE) language statement. The received query may be a structured query language (SQL) statement.

In some implementations, the generated expression language statement may be executed by a database system (e.g., HANA system as developed by SAP SE, Walldorf, Germany) for the purposes of retrieving data queried by the received query.