Class RexSimplify
- java.lang.Object
-
- org.apache.calcite.rex.RexSimplify
-
public class RexSimplify extends java.lang.Object
Context required to simplify a row-expression.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
RexSimplify.CaseBranch
Object to describe a Case branchprivate static class
RexSimplify.Comparison
Comparison between aRexInputRef
orRexFieldAccess
and a literal.private static class
RexSimplify.IsPredicate
Represents an IS Predicate.private static interface
RexSimplify.Predicate
Marker interface for predicates (expressions that evaluate to BOOLEAN).(package private) static class
RexSimplify.SafeRexVisitor
Decides whether it is safe to flatten the given case part into AND/ORs
-
Field Summary
Fields Modifier and Type Field Description (package private) RexUnknownAs
defaultUnknownAs
How to treat UNKNOWN values, if one of the deprecatedsimplify
methods without anunknownAs
argument is called.private RexExecutor
executor
private boolean
paranoid
(package private) boolean
predicateElimination
private RelOptPredicateList
predicates
RexBuilder
rexBuilder
private Strong
strong
-
Constructor Summary
Constructors Modifier Constructor Description RexSimplify(RexBuilder rexBuilder, boolean unknownAsFalse, RexExecutor executor)
Deprecated.RexSimplify(RexBuilder rexBuilder, RelOptPredicateList predicates, boolean unknownAsFalse, RexExecutor executor)
Deprecated.RexSimplify(RexBuilder rexBuilder, RelOptPredicateList predicates, RexExecutor executor)
Creates a RexSimplify.private
RexSimplify(RexBuilder rexBuilder, RelOptPredicateList predicates, RexUnknownAs defaultUnknownAs, boolean predicateElimination, boolean paranoid, RexExecutor executor)
Internal constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description 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.private RexSimplify.CaseBranch
generateBranch(boolean simplifyCond, RexSimplify simplifier, RexSimplify.CaseBranch branch)
If boolean is true, simplify cond in input branch and return new branch.private static boolean
isLowerBound(RexNode e)
(package private) static boolean
isSafeExpression(RexNode r)
Analyzes a givenRexNode
and decides whenever it is safe to unwind.private static boolean
isUpperBound(RexNode e)
private static <C extends java.lang.Comparable<C>>
RexNodeprocessRange(RexBuilder rexBuilder, java.util.List<RexNode> terms, java.util.Map<RexNode,Pair<com.google.common.collect.Range<C>,java.util.List<RexNode>>> rangeTerms, RexNode term, RexNode ref, C v0, SqlKind comparison)
private static <C extends java.lang.Comparable<C>>
com.google.common.collect.Range<C>range(SqlKind comparison, C c)
RexNode
removeNullabilityCast(RexNode e)
Removes any casts that change nullability but not type.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.private <C extends java.lang.Comparable<C>>
com.google.common.collect.Range<C>residue(RexNode ref, com.google.common.collect.Range<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.private boolean
sameTypeOrNarrowsNullability(RelDataType oldType, RelDataType newType)
Return if the new type is the same and at most narrows the nullability.RexNode
simplify(RexNode e)
Simplifies a boolean expression.(package private) RexNode
simplify(RexNode e, RexUnknownAs unknownAs)
Internal method to simplify an expression.RexNode
simplifyAnd(RexCall e)
Deprecated.(package private) RexNode
simplifyAnd(RexCall e, RexUnknownAs unknownAs)
(package private) RexNode
simplifyAnd2(java.util.List<RexNode> terms, java.util.List<RexNode> notTerms)
(package private) RexNode
simplifyAnd2ForUnknownAsFalse(java.util.List<RexNode> terms, java.util.List<RexNode> notTerms)
AssimplifyAnd2(List, List)
but we assume that if the expression returns UNKNOWN it will be interpreted as FALSE.private <C extends java.lang.Comparable<C>>
RexNodesimplifyAnd2ForUnknownAsFalse(java.util.List<RexNode> terms, java.util.List<RexNode> notTerms, java.lang.Class<C> clazz)
RexNode
simplifyAnds(java.lang.Iterable<? extends RexNode> nodes)
Simplifies a conjunction of boolean expressions.(package private) RexNode
simplifyAnds(java.lang.Iterable<? extends RexNode> nodes, RexUnknownAs unknownAs)
private void
simplifyAndTerms(java.util.List<RexNode> terms)
private static RexNode
simplifyBooleanCase(RexBuilder rexBuilder, java.util.List<RexSimplify.CaseBranch> inputBranches, RexUnknownAs unknownAs, RelDataType branchType)
private static RexNode
simplifyBooleanCaseGeneric(RexBuilder rexBuilder, java.util.List<RexSimplify.CaseBranch> branches, RelDataType outputType)
Generic boolean case simplification.private RexNode
simplifyCase(RexCall call, RexUnknownAs unknownAs)
private RexNode
simplifyCast(RexCall e)
private RexNode
simplifyCeilFloor(RexCall e)
Tries to simplify CEIL/FLOOR function on top of CEIL/FLOOR.private RexNode
simplifyCoalesce(RexCall call)
private RexNode
simplifyComparison(RexCall e, RexUnknownAs unknownAs)
private <C extends java.lang.Comparable<C>>
RexNodesimplifyComparison(RexCall e, RexUnknownAs unknownAs, java.lang.Class<C> clazz)
RexNode
simplifyFilterPredicates(java.lang.Iterable<? extends RexNode> predicates)
Combines predicates AND, optimizes, and returns null if the result is always false.private RexNode
simplifyIs(RexCall call)
private RexNode
simplifyIs2(SqlKind kind, RexNode a)
private RexNode
simplifyIsNotNull(RexNode a)
private RexNode
simplifyIsNull(RexNode a)
private RexNode
simplifyIsPredicate(SqlKind kind, RexNode a)
private void
simplifyList(java.util.List<RexNode> terms, RexUnknownAs unknownAs)
private RexNode
simplifyNot(RexCall call, RexUnknownAs unknownAs)
RexNode
simplifyOr(RexCall call)
Simplifies OR(x, x) into x, and similar.private RexNode
simplifyOr(RexCall call, RexUnknownAs unknownAs)
RexNode
simplifyOrs(java.util.List<RexNode> terms)
Simplifies a list of terms and combines them into an OR.private RexNode
simplifyOrs(java.util.List<RexNode> terms, RexUnknownAs unknownAs)
Simplifies a list of terms and combines them into an OR.private void
simplifyOrTerms(java.util.List<RexNode> terms)
RexNode
simplifyPreservingType(RexNode e)
Simplifies a boolean expression, always preserving its type and its nullability.RexNode
simplifyPreservingType(RexNode e, RexUnknownAs unknownAs, boolean matchNullability)
RexNode
simplifyUnknownAs(RexNode e, RexUnknownAs unknownAs)
Assimplify(RexNode)
, but specifying how UNKNOWN values are to be treated.RexNode
simplifyUnknownAsFalse(RexNode e)
Assimplify(RexNode)
, but for a boolean expression for which a result of UNKNOWN will be treated as FALSE.private <C extends java.lang.Comparable<C>>
RexNodesimplifyUsingPredicates(RexNode e, java.lang.Class<C> clazz)
private RexNode
verify(RexNode before, RexUnknownAs unknownAs, java.util.function.Function<RexSimplify,RexNode> simplifier)
RexSimplify
withParanoid(boolean paranoid)
Returns a RexSimplify the same as this but which verifies that the expression before and after simplification are equivalent.private RexSimplify
withPredicateElimination(boolean predicateElimination)
Returns a RexSimplify the same as this but with a specifiedpredicateElimination
value.RexSimplify
withPredicates(RelOptPredicateList predicates)
Returns a RexSimplify the same as this but with a specifiedpredicates
value.RexSimplify
withUnknownAsFalse(boolean unknownAsFalse)
Deprecated.Use methods with aRexUnknownAs
argument, such assimplify(RexNode, RexUnknownAs)
.
-
-
-
Field Detail
-
paranoid
private final boolean paranoid
-
rexBuilder
public final RexBuilder rexBuilder
-
predicates
private final RelOptPredicateList predicates
-
defaultUnknownAs
final RexUnknownAs defaultUnknownAs
How to treat UNKNOWN values, if one of the deprecatedsimplify
methods without anunknownAs
argument is called.
-
predicateElimination
final boolean predicateElimination
-
executor
private final RexExecutor executor
-
strong
private final Strong strong
-
-
Constructor Detail
-
RexSimplify
public RexSimplify(RexBuilder rexBuilder, RelOptPredicateList predicates, RexExecutor executor)
Creates a RexSimplify.- Parameters:
rexBuilder
- Rex builderpredicates
- Predicates known to hold on input fieldsexecutor
- Executor for constant reduction, not null
-
RexSimplify
private RexSimplify(RexBuilder rexBuilder, RelOptPredicateList predicates, RexUnknownAs defaultUnknownAs, boolean predicateElimination, boolean paranoid, RexExecutor executor)
Internal constructor.
-
RexSimplify
@Deprecated public RexSimplify(RexBuilder rexBuilder, boolean unknownAsFalse, RexExecutor executor)
Deprecated.
-
RexSimplify
@Deprecated public RexSimplify(RexBuilder rexBuilder, RelOptPredicateList predicates, boolean unknownAsFalse, RexExecutor executor)
Deprecated.
-
-
Method Detail
-
withUnknownAsFalse
@Deprecated public RexSimplify withUnknownAsFalse(boolean unknownAsFalse)
Deprecated.Use methods with aRexUnknownAs
argument, such assimplify(RexNode, RexUnknownAs)
.Returns a RexSimplify the same as this but with a specifieddefaultUnknownAs
value.
-
withPredicates
public RexSimplify withPredicates(RelOptPredicateList predicates)
Returns a RexSimplify the same as this but with a specifiedpredicates
value.
-
withParanoid
public RexSimplify withParanoid(boolean paranoid)
Returns a RexSimplify the same as this but which verifies that the expression before and after simplification are equivalent.
-
withPredicateElimination
private RexSimplify withPredicateElimination(boolean predicateElimination)
Returns a RexSimplify the same as this but with a specifiedpredicateElimination
value.This is introduced temporarily, until
[CALCITE-2401] is fixed
.
-
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)
returnsy = 2
simplify(x = 1 AND FALSE)
returnsFALSE
Handles UNKNOWN values using the policy specified when you created this
RexSimplify
. Unless you used a deprecated constructor, that policy isRexUnknownAs.UNKNOWN
.If the expression is a predicate in a WHERE clause, consider instead using
simplifyUnknownAsFalse(RexNode)
.- Parameters:
e
- Expression to simplify
-
simplifyUnknownAsFalse
public final RexNode simplifyUnknownAsFalse(RexNode e)
Assimplify(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)
Assimplify(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.)
-
simplify
RexNode simplify(RexNode e, RexUnknownAs unknownAs)
Internal method to simplify an expression.Unlike the public
simplify(RexNode)
andsimplifyUnknownAsFalse(RexNode)
methods, never callsverify(RexNode, RexUnknownAs, Function)
. Verify adds an overhead that is only acceptable for a top-level call.
-
simplifyComparison
private RexNode simplifyComparison(RexCall e, RexUnknownAs unknownAs)
-
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.
-
simplifyAnds
RexNode simplifyAnds(java.lang.Iterable<? extends RexNode> nodes, RexUnknownAs unknownAs)
-
simplifyList
private void simplifyList(java.util.List<RexNode> terms, RexUnknownAs unknownAs)
-
simplifyAndTerms
private void simplifyAndTerms(java.util.List<RexNode> terms)
-
simplifyOrTerms
private void simplifyOrTerms(java.util.List<RexNode> terms)
-
simplifyNot
private RexNode simplifyNot(RexCall call, RexUnknownAs unknownAs)
-
simplifyCase
private RexNode simplifyCase(RexCall call, RexUnknownAs unknownAs)
-
generateBranch
private RexSimplify.CaseBranch generateBranch(boolean simplifyCond, RexSimplify simplifier, RexSimplify.CaseBranch branch)
If boolean is true, simplify cond in input branch and return new branch. Otherwise, simply return input branch.
-
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 givenRexNode
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
-
simplifyBooleanCase
private static RexNode simplifyBooleanCase(RexBuilder rexBuilder, java.util.List<RexSimplify.CaseBranch> inputBranches, RexUnknownAs unknownAs, RelDataType branchType)
-
simplifyBooleanCaseGeneric
private static RexNode simplifyBooleanCaseGeneric(RexBuilder rexBuilder, java.util.List<RexSimplify.CaseBranch> branches, RelDataType outputType)
Generic boolean case simplification.Rewrites:
CASE WHEN p1 THEN x WHEN p2 THEN y ELSE z END
to(p1 and x) or (p2 and y and not(p1)) or (true and z and not(p1) and not(p2))
-
simplifyAnd
RexNode simplifyAnd(RexCall e, RexUnknownAs unknownAs)
-
simplifyAnd2ForUnknownAsFalse
RexNode simplifyAnd2ForUnknownAsFalse(java.util.List<RexNode> terms, java.util.List<RexNode> notTerms)
AssimplifyAnd2(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>> com.google.common.collect.Range<C> residue(RexNode ref, com.google.common.collect.Range<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".
Examples:
residue($0 < 10, [$0 < 5])
returnstrue
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).
-
simplifyOr
private RexNode simplifyOr(RexCall call, RexUnknownAs unknownAs)
-
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).
-
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.
-
verify
private RexNode verify(RexNode before, RexUnknownAs unknownAs, java.util.function.Function<RexSimplify,RexNode> simplifier)
-
simplifyCeilFloor
private RexNode simplifyCeilFloor(RexCall e)
Tries to simplify CEIL/FLOOR function on top of CEIL/FLOOR.Examples:
floor(floor($0, flag(hour)), flag(day))
returnsfloor($0, flag(day))
ceil(ceil($0, flag(second)), flag(day))
returnsceil($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)
becomes1 = 0
.
-
processRange
private static <C extends java.lang.Comparable<C>> RexNode processRange(RexBuilder rexBuilder, java.util.List<RexNode> terms, java.util.Map<RexNode,Pair<com.google.common.collect.Range<C>,java.util.List<RexNode>>> rangeTerms, RexNode term, RexNode ref, C v0, SqlKind comparison)
-
range
private static <C extends java.lang.Comparable<C>> com.google.common.collect.Range<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.
- Parameters:
predicates
- Filter condition predicates- Returns:
- 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.
-
-