Class DateRangeRules


  • public abstract class DateRangeRules
    extends java.lang.Object
    Collection of planner rules that convert EXTRACT(timeUnit FROM dateTime) = constant, FLOOR(dateTime to timeUnit = constant} and CEIL(dateTime to timeUnit = constant} to dateTime BETWEEN lower AND upper.

    The rules allow conversion of queries on time dimension tables, such as

    SELECT ... FROM sales JOIN time_by_day USING (time_id) WHERE time_by_day.the_year = 1997 AND time_by_day.the_month IN (4, 5, 6)

    into

    SELECT ... FROM sales JOIN time_by_day USING (time_id) WHERE the_date BETWEEN DATE '2016-04-01' AND DATE '2016-06-30'

    and is especially useful for Druid, which has a single timestamp column.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  DateRangeRules.ExtractFinder
      Visitor that searches for calls to EXTRACT, FLOOR or CEIL, building a list of distinct time units.
      (package private) static class  DateRangeRules.ExtractShuttle
      Walks over an expression, replacing calls to EXTRACT, FLOOR and CEIL with date ranges.
      static class  DateRangeRules.FilterDateRangeRule
      Rule that converts EXTRACT, FLOOR and CEIL in a Filter into a date range.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static RelOptRule FILTER_INSTANCE  
      private static java.util.function.Predicate<Filter> FILTER_PREDICATE  
      private static java.util.Map<org.apache.calcite.avatica.util.TimeUnitRange,​java.lang.Integer> TIME_UNIT_CODES  
      private static java.util.Map<org.apache.calcite.avatica.util.TimeUnitRange,​org.apache.calcite.avatica.util.TimeUnitRange> TIME_UNIT_PARENTS  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private DateRangeRules()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static com.google.common.collect.ImmutableSortedSet<org.apache.calcite.avatica.util.TimeUnitRange> extractTimeUnits​(RexNode e)
      Tests whether an expression contains one or more calls to the EXTRACT function, and if so, returns the time units used.
      static RexNode replaceTimeUnits​(RexBuilder rexBuilder, RexNode e, java.lang.String timeZone)
      Replaces calls to EXTRACT, FLOOR and CEIL in an expression.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • FILTER_PREDICATE

        private static final java.util.function.Predicate<Filter> FILTER_PREDICATE
      • FILTER_INSTANCE

        public static final RelOptRule FILTER_INSTANCE
      • TIME_UNIT_CODES

        private static final java.util.Map<org.apache.calcite.avatica.util.TimeUnitRange,​java.lang.Integer> TIME_UNIT_CODES
      • TIME_UNIT_PARENTS

        private static final java.util.Map<org.apache.calcite.avatica.util.TimeUnitRange,​org.apache.calcite.avatica.util.TimeUnitRange> TIME_UNIT_PARENTS
    • Constructor Detail

      • DateRangeRules

        private DateRangeRules()
    • Method Detail

      • extractTimeUnits

        static com.google.common.collect.ImmutableSortedSet<org.apache.calcite.avatica.util.TimeUnitRange> extractTimeUnits​(RexNode e)
        Tests whether an expression contains one or more calls to the EXTRACT function, and if so, returns the time units used.

        The result is an immutable set in natural order. This is important, because code relies on the collection being sorted (so YEAR comes before MONTH before HOUR) and unique. A predicate on MONTH is not useful if there is no predicate on YEAR. Then when we apply the predicate on DAY it doesn't generate hundreds of ranges we'll later throw away.

      • replaceTimeUnits

        public static RexNode replaceTimeUnits​(RexBuilder rexBuilder,
                                               RexNode e,
                                               java.lang.String timeZone)
        Replaces calls to EXTRACT, FLOOR and CEIL in an expression.