Class ReduceExpressionsRule
- java.lang.Object
-
- org.apache.calcite.plan.RelOptRule
-
- org.apache.calcite.rel.rules.ReduceExpressionsRule
-
- Direct Known Subclasses:
ReduceExpressionsRule.CalcReduceExpressionsRule
,ReduceExpressionsRule.FilterReduceExpressionsRule
,ReduceExpressionsRule.JoinReduceExpressionsRule
,ReduceExpressionsRule.ProjectReduceExpressionsRule
public abstract class ReduceExpressionsRule extends RelOptRule
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ReduceExpressionsRule.CalcReduceExpressionsRule
Rule that reduces constants inside aCalc
.protected static class
ReduceExpressionsRule.CaseShuttle
Shuttle that pushes predicates into a CASE.static class
ReduceExpressionsRule.FilterReduceExpressionsRule
Rule that reduces constants inside aFilter
.static class
ReduceExpressionsRule.JoinReduceExpressionsRule
Rule that reduces constants inside aJoin
.static class
ReduceExpressionsRule.ProjectReduceExpressionsRule
Rule that reduces constants inside aProject
.protected static class
ReduceExpressionsRule.ReducibleExprLocator
Helper class used to locate expressions that either can be reduced to literals or contain redundant casts.protected static class
ReduceExpressionsRule.RexReplacer
Replaces expressions with their reductions.
-
Field Summary
Fields Modifier and Type Field Description static ReduceExpressionsRule
CALC_INSTANCE
Singleton rule that reduces constants inside aLogicalCalc
.static java.util.regex.Pattern
EXCLUSION_PATTERN
Regular expression that matches the description of all instances of this rule andValuesReduceRule
also.static ReduceExpressionsRule
FILTER_INSTANCE
Singleton rule that reduces constants inside aLogicalFilter
.static ReduceExpressionsRule
JOIN_INSTANCE
Singleton rule that reduces constants inside aJoin
.protected boolean
matchNullability
static ReduceExpressionsRule
PROJECT_INSTANCE
Singleton rule that reduces constants inside aLogicalProject
.-
Fields inherited from class org.apache.calcite.plan.RelOptRule
description, operands, relBuilderFactory
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ReduceExpressionsRule(java.lang.Class<? extends RelNode> clazz, boolean matchNullability, RelBuilderFactory relBuilderFactory, java.lang.String description)
Creates a ReduceExpressionsRule.protected
ReduceExpressionsRule(java.lang.Class<? extends RelNode> clazz, RelBuilderFactory relBuilderFactory, java.lang.String description)
Deprecated.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description 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.static <C extends RexNode>
com.google.common.collect.ImmutableMap<RexNode,C>predicateConstants(java.lang.Class<C> clazz, RexBuilder rexBuilder, RelOptPredicateList predicates)
Deprecated.static RexCall
pushPredicateIntoCase(RexCall call)
Pushes predicates into a CASE.protected static boolean
reduceExpressions(RelNode rel, java.util.List<RexNode> expList, RelOptPredicateList predicates)
Reduces a list of expressions.protected static boolean
reduceExpressions(RelNode rel, java.util.List<RexNode> expList, RelOptPredicateList predicates, boolean unknownAsFalse)
Deprecated.protected static boolean
reduceExpressions(RelNode rel, java.util.List<RexNode> expList, RelOptPredicateList predicates, boolean unknownAsFalse, boolean matchNullability)
Reduces a list of expressions.protected static boolean
reduceExpressionsInternal(RelNode rel, RexSimplify simplify, RexUnknownAs unknownAs, java.util.List<RexNode> expList, RelOptPredicateList predicates)
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, convertList, convertOperand, convertOperand, equals, equals, getOperand, getOperands, getOutConvention, getOutTrait, hashCode, matches, none, onMatch, operand, operand, operand, operand, operand, operandJ, operandJ, some, toString, unordered
-
-
-
-
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 andValuesReduceRule
also. Use it to prevent the planner from invoking these rules.
-
FILTER_INSTANCE
public static final ReduceExpressionsRule FILTER_INSTANCE
Singleton rule that reduces constants inside aLogicalFilter
.
-
PROJECT_INSTANCE
public static final ReduceExpressionsRule PROJECT_INSTANCE
Singleton rule that reduces constants inside aLogicalProject
.
-
JOIN_INSTANCE
public static final ReduceExpressionsRule JOIN_INSTANCE
Singleton rule that reduces constants inside aJoin
.
-
CALC_INSTANCE
public static final ReduceExpressionsRule CALC_INSTANCE
Singleton rule that reduces constants inside aLogicalCalc
.
-
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 applymatchNullability
- 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 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, 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 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 NULL- Returns:
- whether reduction found something to change, and succeeded
-
reduceExpressionsInternal
protected static boolean reduceExpressionsInternal(RelNode rel, RexSimplify simplify, RexUnknownAs unknownAs, java.util.List<RexNode> expList, RelOptPredicateList predicates)
-
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 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 necessaryremovableCasts
- 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)
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
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'.
-
-