We would like to convert predicates on date dimension columns into date ranges. This is particularly useful for Druid, which has a single timestamp column.
Consider the case of a materialized view
that corresponds to a Druid table
And suppose we have the following check constraints:
- CHECK the_year = EXTRACT(YEAR FROM the_timestamp)
- CHECK the_month = EXTRACT(MONTH FROM the_timestamp)
Given a query
we would like to transform it into the following query to be run against Druid:
Druid can handle timestamp ranges (or disjoint sets of ranges) very efficiently.
I believe we can write a rule that knows the check constraints and also knows the properties of the EXTRACT function:
1. Apply check constraints to convert WHERE year = ... to WHERE EXTRACT(YEAR FROM the_timestamp) = ..., etc.
2. EXTRACT(YEAR FROM ...) is monotonic, therefore we can deduce the range of the_timestamp values such that EXTRACT(YEAR FROM the_timestamp) returns 2016.
3. Then we need to use the fact that EXTRACT(MONTH FROM the_timestamp) is monotonic if the_timestamp is bounded within a particular year.
4. And we need to merge month ranges somehow.