Lattices
A lattice is a framework for creating and populating materialized views, and for recognizing that a materialized view can be used to solve a particular query.
Concept
A lattice represents a star (or snowflake) schema, not a general schema. In particular, all relationships must be many-to-one, heading from a fact table at the center of the star.
The name derives from the mathematics: a lattice is a partially ordered set where any two elements have a unique greatest lower bound and least upper bound.
[HRU96] observed that the set of possible materializations of a data cube forms a lattice, and presented an algorithm to choose a good set of materializations. Calcite’s recommendation algorithm is derived from this.
The lattice definition uses a SQL statement to represent the star. SQL is a useful short-hand to represent several tables joined together, and assigning aliases to the column names (it more convenient than inventing a new language to represent relationships, join conditions and cardinalities).
Unlike regular SQL, the order is important. If you put A before B in the FROM clause, and make a join between A and B, you are saying that there is a many-to-one foreign key relationship from A to B. (E.g. in the example lattice, the Sales fact table occurs before the Time dimension table, and before the Product dimension table. The Product dimension table occurs before the ProductClass outer dimension table, further down an arm of a snowflake.)
A lattice implies constraints. In the A to B relationship, there is a foreign key on A (i.e. every value of A’s foreign key has a corresponding value in B’s key), and a unique key on B (i.e. no key value occurs more than once). These constraints are really important, because it allows the planner to remove joins to tables whose columns are not being used, and know that the query results will not change.
Calcite does not check these constraints. If they are violated, Calcite will return wrong results.
A lattice is a big, virtual join view. It is not materialized (it would be several times larger than the star schema, because of denormalization) and you probably wouldn’t want to query it (far too many columns). So what is it useful for? As we said above, (a) the lattice declares some very useful primary and foreign key constraints, (b) it helps the query planner map user queries onto filter-join-aggregate materialized views (the most useful kind of materialized view for DW queries), (c) gives Calcite a framework within which to gather stats about data volumes and user queries, (d) allows Calcite to automatically design and populate materialized views.
Most star schema models force you to choose whether a column is a dimension or a measure. In a lattice, every column is a dimension column. (That is, it can become one of the columns in the GROUP BY clause to query the star schema at a particular dimensionality). Any column can also be used in a measure; you define measures by giving the column and an aggregate function.
If “unit_sales” tends to be used much more often as a measure rather than a dimension, that’s fine. Calcite’s algorithm should notice that it is rarely aggregated, and not be inclined to create tiles that aggregate on it. (By “should” I mean “could and one day will”. The algorithm does not currently take query history into account when designing tiles.)
But someone might want to know whether orders with fewer than 5 items were more or less profitable than orders with more than 100. All of a sudden, “unit_sales” has become a dimension. If there’s virtually zero cost to declaring a column a dimension column, I figured let’s make them all dimension columns.
The model allows for a particular table to be used more than once, with a different table alias. You could use this to model say OrderDate and ShipDate, with two uses to the Time dimension table.
Most SQL systems require that the column names in a view are unique. This is hard to achieve in a lattice, because you often include primary and foreign key columns in a join. So Calcite lets you refer to columns in two ways. If the column is unique, you can use its name, [“unit_sales”]. Whether or not it is unique in the lattice, it will be unique in its table, so you can use it qualified by its table alias. Examples:
- [“sales”, “unit_sales”]
- [“ship_date”, “time_id”]
- [“order_date”, “time_id”]
A “tile” is a materialized table in a lattice, with a particular dimensionality. The “tiles” attribute of the lattice JSON element defines an initial set of tiles to materialize.
Demonstration
Create a model that includes a lattice:
This is a cut-down version of hsqldb-foodmart-lattice-model.json that does not include the “tiles” attribute, because we are going to generate tiles automatically. Let’s log into sqlline and connect to this schema:
You’ll notice that it takes a few seconds to connect. Calcite is running the optimization algorithm, and creating and populating materialized views. Let’s run a query and check out its plan:
The query gives the right answer, but the plan is somewhat surprising.
It doesn’t read the sales_fact_1997
or time_by_day
tables, but instead
reads from a table called m{16, 17, 27, 31, 32, 36, 37}
. This is one of the
tiles created at the start of the connection.
It’s a real table, and you can even query it directly. It has only 120 rows, so is a more efficient way to answer the query:
Let’s list the tables, and you will see several more tiles. There are also
tables of the foodmart
schema, and the system tables TABLES
and COLUMNS
,
and the lattice itself, which appears as a table called star
.
Statistics
The algorithm that chooses which tiles of a lattice to materialize depends on
a lot of statistics. It needs to know select count(distinct a, b, c) from star
for each combination of columns (a, b, c
) it is considering materializing. As
a result the algorithm takes a long time on schemas with many rows and columns.
We are working on a data profiler to address this.
Lattice suggester
If you have defined a lattice, Calcite will self-tune within that lattice. But what if you have not defined a lattice?
Enter the Lattice Suggester, which builds lattices based on incoming queries.
Create a model with a schema that has "autoLattice": true
:
This is a cut-down version of hsqldb-foodmart-lattice-model.json
As you run queries, Calcite will start to build lattices based on those queries. Each lattice is based on a particular fact table. As it sees more queries on that fact table, it will evolve the lattice, joining more dimension tables to the star, and adding measures.
Each lattice will then optimize itself based on both the data and the queries. The goal is to create summary tables (tiles) that are reasonably small but are based on more frequently used attributes and measures.
This feature is still experimental, but has the potential to make databases more “self-tuning” than before.
Further directions
Here are some ideas that have not yet been implemented:
- The algorithm that builds tiles takes into account a log of past queries.
- Materialized view manager sees incoming queries and builds tiles for them.
- Materialized view manager drops tiles that are not actively used.
- Lattice suggester adds lattices based on incoming queries, transfers tiles from existing lattices to new lattices, and drops lattices that are no longer being used.
- Tiles that cover a horizontal slice of a table; and a rewrite algorithm that can answer a query by stitching together several tiles and going to the raw data to fill in the holes.
- API to invalidate tiles, or horizontal slices of tiles, when the underlying data is changed.
References
- [HRU96] V. Harinarayan, A. Rajaraman and J. Ullman. Implementing data cubes efficiently. In Proc. ACM SIGMOD Conf., Montreal, 1996.