Class ReduceExpressionsRule<C extends ReduceExpressionsRule.Config>
- Type Parameters:
C
- Configuration type
- All Implemented Interfaces:
SubstitutionRule
,TransformationRule
- Direct Known Subclasses:
ReduceExpressionsRule.CalcReduceExpressionsRule
,ReduceExpressionsRule.FilterReduceExpressionsRule
,ReduceExpressionsRule.JoinReduceExpressionsRule
,ReduceExpressionsRule.ProjectReduceExpressionsRule
,ReduceExpressionsRule.WindowReduceExpressionsRule
- 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
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Rule that reduces constants inside aCalc
.protected static class
Shuttle that pushes predicates into a CASE.static interface
Rule configuration.static class
Rule that reduces constants inside aFilter
.static class
Rule that reduces constants inside aJoin
.static class
Rule that reduces constants inside aProject
.protected static class
Helper class used to locate expressions that either can be reduced to literals or contain redundant casts.protected static class
Replaces expressions with their reductions.static class
Rule that reduces constants inside aWindow
.Nested classes/interfaces inherited from class org.apache.calcite.plan.RelRule
RelRule.Done, RelRule.MatchHandler<R extends RelOptRule>, RelRule.OperandBuilder, RelRule.OperandDetailBuilder<R extends RelNode>, RelRule.OperandTransform
Nested classes/interfaces inherited from class org.apache.calcite.plan.RelOptRule
RelOptRule.ConverterRelOptRuleOperand
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Pattern
Regular expression that matches the description of all instances of this rule andValuesReduceRule
also.Fields inherited from class org.apache.calcite.plan.RelOptRule
description, operands, relBuilderFactory
-
Constructor Summary
ModifierConstructorDescriptionprotected
ReduceExpressionsRule
(C config) Creates a ReduceExpressionsRule. -
Method Summary
Modifier and TypeMethodDescriptionprotected 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.predicateConstants
(Class<C> clazz, RexBuilder rexBuilder, RelOptPredicateList predicates) Deprecated.static RexCall
pushPredicateIntoCase
(RexCall call) Pushes predicates into a CASE.protected static boolean
reduceExpressions
(RelNode rel, List<RexNode> expList, RelOptPredicateList predicates) Reduces a list of expressions.protected static boolean
reduceExpressions
(RelNode rel, List<RexNode> expList, RelOptPredicateList predicates, boolean unknownAsFalse) Deprecated.protected static boolean
reduceExpressions
(RelNode rel, List<RexNode> expList, RelOptPredicateList predicates, boolean unknownAsFalse, boolean matchNullability, boolean treatDynamicCallsAsConstant) Reduces a list of expressions.protected static boolean
reduceExpressionsInternal
(RelNode rel, RexSimplify simplify, RexUnknownAs unknownAs, List<RexNode> expList, RelOptPredicateList predicates, boolean treatDynamicCallsAsConstant) protected static RexNode
substitute
(RexCall call, int ordinal, RexNode node) Converts op(arg0, ..., argOrdinal, ..., argN) to op(arg0,..., node, ..., argN).Methods inherited from class org.apache.calcite.plan.RelOptRule
any, convert, convert, convert, convert, convertList, convertOperand, convertOperand, equals, equals, getOperand, getOperands, getOutConvention, getOutTrait, hashCode, matches, none, onMatch, operand, operand, operand, operand, operand, operandJ, operandJ, some, toString, unordered
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.apache.calcite.rel.rules.SubstitutionRule
autoPruneOld
-
Field Details
-
EXCLUSION_PATTERN
Regular expression that matches the description of all instances of this rule andValuesReduceRule
also. Use it to prevent the planner from invoking these rules.
-
-
Constructor Details
-
ReduceExpressionsRule
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 expressionexpList
- List of expressions, modified in placepredicates
- 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 expressionCASE WHEN 'a' = 'a' THEN 1 ELSE NULL END
. Before reduction the type isINTEGER
(nullable), but after reduction the literal 1 has typeINTEGER 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 toINTEGER
(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 expressionexpList
- List of expressions, modified in placepredicates
- Constraints known to hold on input expressionsunknownAsFalse
- Whether UNKNOWN will be treated as FALSEmatchNullability
- 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 NULLtreatDynamicCallsAsConstant
- 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 factoryexps
- list of candidate expressions to be examined for reductionconstants
- List of expressions known to be constantconstExps
- returns the list of expressions that can be constant reducedaddCasts
- indicator for each expression that can be constant reduced, whether a cast of the resulting reduced expression is potentially necessarytreatDynamicCallsAsConstant
- 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) Deprecated.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, orRexNode
to useRexUtil.isConstant(RexNode)
- Parameters:
clazz
- Class of expression that is considered constantrexBuilder
- Rex builderpredicates
- Predicate list- Returns:
- Map from values to constants
-
pushPredicateIntoCase
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
Converts op(arg0, ..., argOrdinal, ..., argN) to op(arg0,..., node, ..., argN).
-
RelOptPredicateList.constantMap