Class RexSimplify

  • public class RexSimplify
    extends java.lang.Object
    Context required to simplify a row-expression.
    • Field Detail

      • paranoid

        private final boolean paranoid
      • defaultUnknownAs

        final RexUnknownAs defaultUnknownAs
        How to treat UNKNOWN values, if one of the deprecated simplify methods without an unknownAs argument is called.
      • predicateElimination

        final boolean predicateElimination
      • strong

        private final Strong strong
    • Constructor Detail

      • RexSimplify

        public RexSimplify​(RexBuilder rexBuilder,
                           RelOptPredicateList predicates,
                           RexExecutor executor)
        Creates a RexSimplify.
        rexBuilder - Rex builder
        predicates - Predicates known to hold on input fields
        executor - Executor for constant reduction, not null
      • RexSimplify

        public RexSimplify​(RexBuilder rexBuilder,
                           boolean unknownAsFalse,
                           RexExecutor executor)
    • Method Detail

      • simplifyPreservingType

        public RexNode simplifyPreservingType​(RexNode e)
        Simplifies a boolean expression, always preserving its type and its nullability.

        This is useful if you are simplifying expressions in a Project.

      • simplifyPreservingType

        public RexNode simplifyPreservingType​(RexNode e,
                                              RexUnknownAs unknownAs,
                                              boolean matchNullability)
      • simplify

        public RexNode simplify​(RexNode e)
        Simplifies a boolean expression.

        In particular:

        • simplify(x = 1 AND y = 2 AND NOT x = 1) returns y = 2
        • simplify(x = 1 AND FALSE) returns FALSE

        Handles UNKNOWN values using the policy specified when you created this RexSimplify. Unless you used a deprecated constructor, that policy is RexUnknownAs.UNKNOWN.

        If the expression is a predicate in a WHERE clause, consider instead using simplifyUnknownAsFalse(RexNode).

        e - Expression to simplify
      • simplifyUnknownAsFalse

        public final RexNode simplifyUnknownAsFalse​(RexNode e)
        As simplify(RexNode), but for a boolean expression for which a result of UNKNOWN will be treated as FALSE.

        Use this form for expressions on a WHERE, ON, HAVING or FILTER(WHERE) clause.

        This may allow certain additional simplifications. A result of UNKNOWN may yield FALSE, however it may still yield UNKNOWN. (If the simplified expression has type BOOLEAN NOT NULL, then of course it can only return FALSE.)

      • simplifyUnknownAs

        public RexNode simplifyUnknownAs​(RexNode e,
                                         RexUnknownAs unknownAs)
        As simplify(RexNode), but specifying how UNKNOWN values are to be treated.

        If UNKNOWN is treated as FALSE, this may allow certain additional simplifications. A result of UNKNOWN may yield FALSE, however it may still yield UNKNOWN. (If the simplified expression has type BOOLEAN NOT NULL, then of course it can only return FALSE.)

      • simplifyGenericNode

        private RexNode simplifyGenericNode​(RexCall e)
        Runs simplification inside a non-specialized node.
      • simplifyComparison

        private <C extends java.lang.Comparable<C>> RexNode simplifyComparison​(RexCall e,
                                                                               RexUnknownAs unknownAs,
                                                                               java.lang.Class<C> clazz)
      • simplifyAnds

        public RexNode simplifyAnds​(java.lang.Iterable<? extends RexNode> nodes)
        Simplifies a conjunction of boolean expressions.
      • simplifyList

        private void simplifyList​(java.util.List<RexNode> terms,
                                  RexUnknownAs unknownAs)
      • simplifyAndTerms

        private void simplifyAndTerms​(java.util.List<RexNode> terms,
                                      RexUnknownAs unknownAs)
      • simplifyOrTerms

        private void simplifyOrTerms​(java.util.List<RexNode> terms,
                                     RexUnknownAs unknownAs)
      • simplifyIsNotNull

        private RexNode simplifyIsNotNull​(RexNode a)
      • simplifyCoalesce

        private RexNode simplifyCoalesce​(RexCall call)
      • sameTypeOrNarrowsNullability

        private boolean sameTypeOrNarrowsNullability​(RelDataType oldType,
                                                     RelDataType newType)
        Return if the new type is the same and at most narrows the nullability.
      • isSafeExpression

        static boolean isSafeExpression​(RexNode r)
        Analyzes a given RexNode and decides whenever it is safe to unwind.

        "Safe" means that it only contains a combination of known good operators.

        Division is an unsafe operator; consider the following:

        case when a > 0 then 1 / a else null end
      • simplifyBooleanCaseGeneric

        private static RexNode simplifyBooleanCaseGeneric​(RexBuilder rexBuilder,
                                                          java.util.List<RexSimplify.CaseBranch> branches,
                                                          RelDataType outputType)
        Generic boolean case simplification.


           WHEN p1 THEN x
           WHEN p2 THEN y
           ELSE z
        (p1 and x) or (p2 and y and not(p1)) or (true and z and not(p1) and not(p2))
      • simplifyAnd

        public RexNode simplifyAnd​(RexCall e)
      • simplifyAnd2

        RexNode simplifyAnd2​(java.util.List<RexNode> terms,
                             java.util.List<RexNode> notTerms)
      • simplifyAnd2ForUnknownAsFalse

        RexNode simplifyAnd2ForUnknownAsFalse​(java.util.List<RexNode> terms,
                                              java.util.List<RexNode> notTerms)
        As simplifyAnd2(List, List) but we assume that if the expression returns UNKNOWN it will be interpreted as FALSE.
      • simplifyAnd2ForUnknownAsFalse

        private <C extends java.lang.Comparable<C>> RexNode simplifyAnd2ForUnknownAsFalse​(java.util.List<RexNode> terms,
                                                                                          java.util.List<RexNode> notTerms,
                                                                                          java.lang.Class<C> clazz)
      • simplifyUsingPredicates

        private <C extends java.lang.Comparable<C>> RexNode simplifyUsingPredicates​(RexNode e,
                                                                                    java.lang.Class<C> clazz)
      • residue

        private <C extends java.lang.Comparable<C>><C> residue​(RexNode ref,
                                                                                     <C> r0,
                                                                                               java.util.List<RexNode> predicates,
                                                                                               java.lang.Class<C> clazz)
        Weakens a term so that it checks only what is not implied by predicates.

        The term is broken into "ref comparison constant", for example "$0 < 5".


        • residue($0 < 10, [$0 < 5]) returns true
        • residue($0 < 10, [$0 < 20, $0 > 0]) returns $0 < 10
      • simplifyOr

        public RexNode simplifyOr​(RexCall call)
        Simplifies OR(x, x) into x, and similar. The simplified expression returns UNKNOWN values as is (not as FALSE).
      • simplifyOrs

        public RexNode simplifyOrs​(java.util.List<RexNode> terms)
        Simplifies a list of terms and combines them into an OR. Modifies the list in place. The simplified expression returns UNKNOWN values as is (not as FALSE).
      • ensureParanoidOff

        private void ensureParanoidOff()
      • simplifyOrs

        private RexNode simplifyOrs​(java.util.List<RexNode> terms,
                                    RexUnknownAs unknownAs)
        Simplifies a list of terms and combines them into an OR. Modifies the list in place.
      • simplifyCeilFloor

        private RexNode simplifyCeilFloor​(RexCall e)
        Tries to simplify CEIL/FLOOR function on top of CEIL/FLOOR.


        • floor(floor($0, flag(hour)), flag(day)) returns floor($0, flag(day))
        • ceil(ceil($0, flag(second)), flag(day)) returns ceil($0, flag(day))
        • floor(floor($0, flag(day)), flag(second)) does not change
      • canRollUp

        private static boolean canRollUp​(org.apache.calcite.avatica.util.TimeUnit outer,
                                         org.apache.calcite.avatica.util.TimeUnit inner)
        Method that returns whether we can rollup from inner time unit to outer time unit.
      • removeNullabilityCast

        public RexNode removeNullabilityCast​(RexNode e)
        Removes any casts that change nullability but not type.

        For example, CAST(1 = 0 AS BOOLEAN) becomes 1 = 0.

      • range

        private static <C extends java.lang.Comparable<C>><C> range​(SqlKind comparison,
                                                                                                    C c)
      • isUpperBound

        private static boolean isUpperBound​(RexNode e)
      • isLowerBound

        private static boolean isLowerBound​(RexNode e)
      • simplifyFilterPredicates

        public RexNode simplifyFilterPredicates​(java.lang.Iterable<? extends RexNode> predicates)
        Combines predicates AND, optimizes, and returns null if the result is always false.

        The expression is simplified on the assumption that an UNKNOWN value is always treated as FALSE. Therefore the simplified expression may sometimes evaluate to FALSE where the original expression would evaluate to UNKNOWN.

        predicates - Filter condition predicates
        simplified conjunction of predicates for the filter, null if always false
      • replaceLast

        private static <E> boolean replaceLast​(java.util.List<E> list,
                                               E oldVal,
                                               E newVal)
        Replaces the last occurrence of one specified value in a list with another.

        Does not change the size of the list.

        Returns whether the value was found.