Algorithm Search⌘K

Definitioncore

Data cube

Also called CUBE operator, OLAP cube.

The aggregates of a fact table over all subsets of a set of grouping attributes, as defined by the CUBE operator of Gray et al.

The data cube of a relation RR over grouping attributes A1,…,ADA_1, \ldots, A_D is the union of the group-by aggregations of RR over all 2D2^D subsets of these attributes. Gray et al. (1996, 1997) defined it as the relational operator CUBE, with a special value ALL in the attributes that a row does not group by. So one table holds the detail rows, all subtotals and the grand total. ROLLUP is the part of the cube that keeps only the prefixes {A1,…,Ak}\{A_1, \ldots, A_k\}.

Definition

Let RR be a relation, let A1,…,ADA_1, \ldots, A_D be attributes of RR, called the dimensions, and let f1,…,fqf_1, \ldots, f_q be aggregates on other attributes, called the measures. For a subset S⊆{A1,…,AD}S \subseteq \{A_1, \ldots, A_D\}, the group-by on SS is γS; f1,…,fq(R)\gamma_{S;\, f_1, \ldots, f_q}(R), one row per value combination of SS that occurs in RR. Pad each of its rows to the schema (A1,…,AD,f1,…,fq)(A_1, \ldots, A_D, f_1, \ldots, f_q) by putting ALL in every Ai∉SA_i \notin S. The data cube is

CUBE(R)=⋃S⊆{A1,…,AD}pad(γS; f1,…,fq(R)).\text{CUBE}(R) = \bigcup_{S \subseteq \{A_1, \ldots, A_D\}} \text{pad}\big(\gamma_{S;\, f_1, \ldots, f_q}(R)\big).

Each row is a cell. A cell cc fixes the dimensions where ci≠ALLc_i \ne \text{ALL}, and its group is the set of rows of RR that agree with cc on them. ALL is not a value of any domain, so a cell with ALL is never confused with a detail row. For S=∅S = \emptyset, the group-by has one row, the grand total, even when RR is empty, as in SQL.

Size. If AiA_i has CiC_i distinct values in RR, the cube has at most ∏i=1D(Ci+1)\prod_{i=1}^{D} (C_i + 1) cells, since each dimension of a cell is one of its CiC_i values or ALL. Each row of RR lies in exactly one cell of each group-by, so the cube also has at most 1+(2D−1) n1 + (2^D - 1)\, n cells for nn rows. A cube is dense when most of the ∏iCi\prod_i C_i finest cells occur, and sparse when few do.

The lattice. Order the group-bys by inclusion of their sets SS. This gives the lattice of all subsets of {A1,…,AD}\{A_1, \ldots, A_D\}, with the group-by on all dimensions at one end and the grand total at the other. If S⊆TS \subseteq T, the group-by on SS can be computed from the group-by on TT for the first two classes of aggregates below, which is the basis of view selection for cubes.

Classes of aggregates. Gray et al. sort the aggregates by what is needed to combine partial results. Let the multiset XX be split into parts X1,…,XmX_1, \ldots, X_m.

  • An aggregate FF is distributive if there is a GG with F(X)=G(F(X1),…,F(Xm))F(X) = G(F(X_1), \ldots, F(X_m)). COUNT, SUM, MIN and MAX are distributive; COUNT is combined with SUM.
  • FF is algebraic if there are a GG with values in tuples of a fixed size MM and an HH with F(X)=H(G(X1),…,G(Xm))F(X) = H(G(X_1), \ldots, G(X_m)). AVG is algebraic with G=(SUM,COUNT)G = (\text{SUM}, \text{COUNT}), and so is the standard deviation, with the sum of squares added.
  • FF is holistic if no fixed MM is enough for its partial results. MEDIAN, MODE and RANK are holistic.

ROLLUP and grouping sets. ROLLUP(A1,…,AD)\text{ROLLUP}(A_1, \ldots, A_D) is the union of the group-bys on the D+1D + 1 prefixes {A1,…,Ak}\{A_1, \ldots, A_k\}, 0≤k≤D0 \le k \le D. A grouping set is any chosen family of subsets; CUBE takes all of them. SQL writes these as GROUP BY CUBE (...), GROUP BY ROLLUP (...) and GROUP BY GROUPING SETS (...), shows ALL as a null, and gives the function GROUPING to tell such a null from a null of the data.

Examples and non-examples

A full cube. Take the sales (tea, 2023, 4), (tea, 2024, 9), (coffee, 2023, 5), (coffee, 2024, 6), (coffee, 2024, 1), with dimensions product and year and the measure SUM(amount).

product year SUM group-by
coffee 2023 5 {product, year}
coffee 2024 7 {product, year}
tea 2023 4 {product, year}
tea 2024 9 {product, year}
coffee ALL 12 {product}
tea ALL 13 {product}
ALL 2023 9 {year}
ALL 2024 16 {year}
ALL ALL 25 {}

Every pair occurs, so the cube has (2+1)(2+1)=9(2 + 1)(2 + 1) = 9 cells. ROLLUP(product,year)\text{ROLLUP}(\text{product}, \text{year}) is the same table without the two rows of the group-by on {year}, 7 rows. Drawn as a two-way table with products as rows and years as columns, the cube is a cross-tab with its row totals, column totals and grand total.

A sparse cube. If the sale (tea, 2024, 9) is removed, the cell (tea, 2024) does not occur, and the cube has 8 cells, fewer than the bound 9. There is no row with SUM 0 for a missing pair, because a group-by only has groups that occur.

Non-example: one GROUP BY. GROUP BY product, year gives only the first four rows. It is one group-by of the cube, not the cube.

Non-example: the subtotals of a holistic aggregate. The MEDIAN of (coffee, ALL) cannot be found from the medians of (coffee, 2023) and (coffee, 2024): the medians 5 and 3.5 come from the parts {5} and {6, 1}, while the median of {5, 6, 1} is 5. Such a cell must be computed from its rows, not from finer cells.

Non-example: null as ALL. If a sale has a null year, the group-by on {product, year} has a cell (coffee, null). This is a detail cell of the data, not the subtotal (coffee, ALL). Writing ALL as a null without GROUPING makes the two look the same.

  • Group-by aggregation. One element of the cube; the cube is the union over all subsets of the dimensions. It is computed by hash aggregation or sort-based aggregation.
  • Iceberg cube. The cells of the cube whose group has at least a given number of rows, which is CUBE with a HAVING COUNT(*) condition. BUC computes it and skips small groups early.
  • Cuboid. Another name for the group-by on one subset SS, seen as one node of the lattice.
  • Materialized view. A stored query result. A system can store some group-bys of the cube as materialized views and answer the others from them; greedy view selection picks which ones.
  • Null semantics. The rules of SQL nulls decide how nulls in a dimension group, and why ALL needs to be told apart from them.

References

  1. Jim Gray, Surajit Chaudhuri, Adam Bosworth, Andrew Layman, Don Reichart, Murali Venkatrao, Frank Pellow and Hamid Pirahesh, Data cube, a relational aggregation operator generalizing group-by, cross-tab, and sub-totals, Data Mining and Knowledge Discovery 1(1), 29 to 53 (1997), the CUBE and ROLLUP operators, the ALL value, and the distributive, algebraic and holistic classes of aggregate functions
  2. Jim Gray, Adam Bosworth, Andrew Layman and Hamid Pirahesh, Data cube, a relational aggregation operator generalizing group-by, cross-tab, and sub-totals, Proceedings of the 12th International Conference on Data Engineering (ICDE), 1996, 152 to 159, the first version of the CUBE operator
  3. Venky Harinarayan, Anand Rajaraman and Jeffrey D. Ullman, Implementing data cubes efficiently, Proceedings of ACM SIGMOD 1996, 205 to 216, the lattice of group-bys of a cube

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/8ETAEP/text.yaml