Class ReduceExpressionsRule<C extends ReduceExpressionsRule.Config>

java.lang.Object
org.apache.calcite.plan.RelOptRule
org.apache.calcite.plan.RelRule<C>
org.apache.calcite.rel.rules.ReduceExpressionsRule<C>
Type Parameters:
C - Configuration type
All Implemented Interfaces:
SubstitutionRule, TransformationRule
Direct Known Subclasses:
ReduceExpressionsRule.CalcReduceExpressionsRule, ReduceExpressionsRule.FilterReduceExpressionsRule, ReduceExpressionsRule.JoinReduceExpressionsRule, ReduceExpressionsRule.ProjectReduceExpressionsRule, ReduceExpressionsRule.WindowReduceExpressionsRule

public abstract class ReduceExpressionsRule<C extends ReduceExpressionsRule.Config> extends RelRule<C> implements SubstitutionRule
Collection of planner rules that apply various simplifying transformations on RexNode trees. Currently, there are two transformations:
  • Constant reduction, which evaluates constant subtrees, replacing them with a corresponding RexLiteral
  • Removal of redundant casts, which occurs when the argument into the cast is the same as the type of the resulting cast expression
  • Field Details

    • EXCLUSION_PATTERN

      public static final 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.
  • Constructor Details

    • ReduceExpressionsRule

      protected ReduceExpressionsRule(C config)
      Creates a ReduceExpressionsRule.
  • Method Details

    • reduceExpressions

      protected static boolean reduceExpressions(RelNode rel, 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, List<RexNode> expList, RelOptPredicateList predicates, boolean unknownAsFalse)
      Deprecated.
    • reduceExpressions

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

      The matchNullability flag comes into play when reducing an 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
      treatDynamicCallsAsConstant - Whether to treat dynamic functions as constants
      Returns:
      whether reduction found something to change, and succeeded
    • reduceExpressionsInternal

      protected static boolean reduceExpressionsInternal(RelNode rel, RexSimplify simplify, RexUnknownAs unknownAs, List<RexNode> expList, RelOptPredicateList predicates, boolean treatDynamicCallsAsConstant)
    • findReducibleExps

      protected static void findReducibleExps(RelDataTypeFactory typeFactory, List<RexNode> exps, com.google.common.collect.ImmutableMap<RexNode,RexNode> constants, List<RexNode> constExps, List<Boolean> addCasts, boolean treatDynamicCallsAsConstant)
      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
      treatDynamicCallsAsConstant - Whether to treat dynamic functions as constants
    • predicateConstants

      @Deprecated public static <C extends RexNode> com.google.common.collect.ImmutableMap<RexNode,C> predicateConstants(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).