In a relational database management system utilizing a data processor for storing data in the form of at least one table comprised of tuples having one or more columns, wherein the data contained in the relational database management is retrievable by means of SQL aggregation queries, a data processor implemented method for producing a model of an SQL query having a GROUP BY clause limited by a set of grouping-sets, in which nodes of the model represent query operations and nodes are identified by labels, each label representing a GROUP BY operation that results from the application of a GROUP BY operation to all inputs of a represented node, the method comprising:
- placing a base table T in the model;
- defining a variable that represents the GROUP BY of a maximal grouping set in the set of grouping sets;
- finding a first node in the model representing an operation that is a superset of the GROUP BY operation represented by the variable;
- adding the variable to the model with the first node as its input;
- removing the maximal set from the set of grouping sets;
- and
- for each node in the model other than the first node:
- determining if the label of the first node with the operation of a second node produces any remaining GROUP BYs in the set of grouping sets and only remaining GROUP BYs in the set of grouping sets;
- responsive to determining that the label of the first node with the operation of the second node produces remaining GROUP BYs in the set of grouping sets and only remaining GROUP BYs in the set of grouping sets, adding a data flow arc from the first node to the second node, the data flow arc defining an intersection;
- adding new GROUP BYs produced by the intersection to the label of the second node;
- and
- removing new GROUP BYs produced by the intersection from the set of grouping sets.