Class ReduceExpressionsRule

    • Field Detail

      • EXCLUSION_PATTERN

        public static final java.util.regex.Pattern EXCLUSION_PATTERN
        Regular expression that matches the description of all instances of this rule and ValuesReduceRule also. Use it to prevent the planner from invoking these rules.
      • JOIN_INSTANCE

        public static final ReduceExpressionsRule JOIN_INSTANCE
        Singleton rule that reduces constants inside a Join.
      • matchNullability

        protected final boolean matchNullability
    • Constructor Detail

      • ReduceExpressionsRule

        protected ReduceExpressionsRule​(java.lang.Class<? extends RelNode> clazz,
                                        boolean matchNullability,
                                        RelBuilderFactory relBuilderFactory,
                                        java.lang.String description)
        Creates a ReduceExpressionsRule.
        Parameters:
        clazz - class of rels to which this rule should apply
        matchNullability - Whether to add a CAST when a nullable expression reduces to a NOT NULL literal
      • ReduceExpressionsRule

        @Deprecated
        protected ReduceExpressionsRule​(java.lang.Class<? extends RelNode> clazz,
                                        RelBuilderFactory relBuilderFactory,
                                        java.lang.String description)
        Deprecated.
    • Method Detail

      • reduceExpressions

        protected static boolean reduceExpressions​(RelNode rel,
                                                   java.util.List<RexNode> expList,
                                                   RelOptPredicateList predicates)
        Reduces a list of expressions.
        Parameters:
        rel - Relational expression
        expList - List of expressions, modified in place
        predicates - Constraints known to hold on input expressions
        Returns:
        whether reduction found something to change, and succeeded
      • reduceExpressions

        @Deprecated
        protected static boolean reduceExpressions​(RelNode rel,
                                                   java.util.List<RexNode> expList,
                                                   RelOptPredicateList predicates,
                                                   boolean unknownAsFalse)
        Deprecated.
      • reduceExpressions

        protected static boolean reduceExpressions​(RelNode rel,
                                                   java.util.List<RexNode> expList,
                                                   RelOptPredicateList predicates,
                                                   boolean unknownAsFalse,
                                                   boolean matchNullability)
        Reduces a list of expressions.

        The matchNullability flag comes into play when reducing a expression whose type is nullable. Suppose we are reducing an expression CASE WHEN 'a' = 'a' THEN 1 ELSE NULL END. Before reduction the type is INTEGER (nullable), but after reduction the literal 1 has type INTEGER NOT NULL.

        In some situations it is more important to preserve types; in this case you should use matchNullability = true (which used to be the default behavior of this method), and it will cast the literal to INTEGER (nullable).

        In other situations, you would rather propagate the new stronger type, because it may allow further optimizations later; pass matchNullability = false and no cast will be added, but you may need to adjust types elsewhere in the expression tree.

        Parameters:
        rel - Relational expression
        expList - List of expressions, modified in place
        predicates - Constraints known to hold on input expressions
        unknownAsFalse - Whether UNKNOWN will be treated as FALSE
        matchNullability - Whether Calcite should add a CAST to a literal resulting from simplification and expression if the expression had nullable type and the literal is NOT NULL
        Returns:
        whether reduction found something to change, and succeeded
      • findReducibleExps

        protected static void findReducibleExps​(RelDataTypeFactory typeFactory,
                                                java.util.List<RexNode> exps,
                                                com.google.common.collect.ImmutableMap<RexNode,​RexNode> constants,
                                                java.util.List<RexNode> constExps,
                                                java.util.List<java.lang.Boolean> addCasts,
                                                java.util.List<RexNode> removableCasts)
        Locates expressions that can be reduced to literals or converted to expressions with redundant casts removed.
        Parameters:
        typeFactory - Type factory
        exps - list of candidate expressions to be examined for reduction
        constants - List of expressions known to be constant
        constExps - returns the list of expressions that can be constant reduced
        addCasts - indicator for each expression that can be constant reduced, whether a cast of the resulting reduced expression is potentially necessary
        removableCasts - returns the list of cast expressions where the cast
      • predicateConstants

        @Deprecated
        public static <C extends RexNode> com.google.common.collect.ImmutableMap<RexNode,​C> predicateConstants​(java.lang.Class<C> clazz,
                                                                                                                     RexBuilder rexBuilder,
                                                                                                                     RelOptPredicateList predicates)
        Creates a map containing each (e, constant) pair that occurs within a predicate list.
        Type Parameters:
        C - what to consider a constant: RexLiteral to use a narrow definition of constant, or RexNode to use RexUtil.isConstant(RexNode)
        Parameters:
        clazz - Class of expression that is considered constant
        rexBuilder - Rex builder
        predicates - Predicate list
        Returns:
        Map from values to constants
      • pushPredicateIntoCase

        public static RexCall pushPredicateIntoCase​(RexCall call)
        Pushes predicates into a CASE.

        We have a loose definition of 'predicate': any boolean expression will do, except CASE. For example '(CASE ...) = 5' or '(CASE ...) IS NULL'.

      • substitute

        protected static RexNode substitute​(RexCall call,
                                            int ordinal,
                                            RexNode node)
        Converts op(arg0, ..., argOrdinal, ..., argN) to op(arg0,..., node, ..., argN).