public class RexUtil extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
RexUtil.FieldAccessFinder
Visitor which builds a bitmap of the inputs used by an expression.
|
| Modifier and Type | Method and Description |
|---|---|
static <E> boolean |
all(List<? extends E> list,
Predicate1<E> predicate)
Returns whether
predicate is true for all elements of
list. |
static Iterable<RexNode> |
apply(Mappings.TargetMapping mapping,
Iterable<? extends RexNode> nodes)
Applies a mapping to an iterable over expressions.
|
static List<RelCollation> |
apply(Mappings.TargetMapping mapping,
List<RelCollation> collationList)
Applies a mapping to a collation list.
|
static RelCollation |
apply(Mappings.TargetMapping mapping,
RelCollation collation)
Applies a mapping to a collation.
|
static RelFieldCollation |
apply(Mappings.TargetMapping mapping,
RelFieldCollation fieldCollation)
Applies a mapping to a field collation.
|
static RexNode |
apply(Mappings.TargetMapping mapping,
RexNode node)
Applies a mapping to an expression.
|
static <T extends RexNode> |
apply(RexVisitor<T> shuttle,
T[] exprs)
Applies a shuttle to an array of expressions.
|
static void |
apply(RexVisitor<Void> visitor,
List<? extends RexNode> exprs,
RexNode expr)
Applies a visitor to a list of expressions and, if specified, a single
expression.
|
static void |
apply(RexVisitor<Void> visitor,
RexNode[] exprs,
RexNode expr)
Applies a visitor to an array of expressions and, if specified, a single
expression.
|
static List<RelFieldCollation> |
applyFields(Mappings.TargetMapping mapping,
List<RelFieldCollation> fieldCollations)
Applies a mapping to a list of field collations.
|
static boolean |
canReinterpretOverflow(RexCall call) |
static boolean |
compatibleTypes(List<RexNode> exprs,
RelDataType type,
boolean fail)
Returns whether the type of an array of expressions is compatible with a
struct type.
|
static RexNode |
composeConjunction(RexBuilder rexBuilder,
Iterable<? extends RexNode> nodes,
boolean nullOnEmpty)
Converts a collection of expressions into an AND.
|
static RexNode |
composeDisjunction(RexBuilder rexBuilder,
Iterable<? extends RexNode> nodes,
boolean nullOnEmpty)
Converts a collection of expressions into an OR.
|
static boolean |
containCommonExprs(List<RexNode> exprs,
boolean fail)
Returns whether an array of expressions has any common sub-expressions.
|
static boolean |
containComplexExprs(List<RexNode> exprs)
Returns whether a list of expressions contains complex expressions, that
is, a call whose arguments are not
RexVariable (or a subtype such
as RexInputRef) or RexLiteral. |
static boolean |
containForwardRefs(List<RexNode> exprs,
RelDataType inputRowType,
boolean fail)
Returns whether an array of expressions contains a forward reference.
|
static boolean |
containIdentity(List<? extends RexNode> exprs,
RelDataType rowType,
boolean fail)
Returns whether the leading edge of a given array of expressions is
wholly
RexInputRef objects with types corresponding to the
underlying datatype. |
static boolean |
containsFieldAccess(RexNode node)
Returns whether a given tree contains any
RexFieldAccess nodes. |
static boolean |
containsInputRef(RexNode node)
Returns whether a given tree contains any {link RexInputRef} nodes.
|
static RelDataType |
createStructType(RelDataTypeFactory typeFactory,
List<? extends RexNode> exprs,
List<String> names)
Creates a record type with specified field names.
|
static RelDataType |
createStructType(RelDataTypeFactory typeFactory,
List<RexNode> exprs)
Creates a record type with anonymous field names.
|
static <E> boolean |
exists(List<? extends E> list,
Predicate1<E> predicate)
Returns whether there is an element in
list for which
predicate is true. |
static List<RelDataTypeFamily> |
families(List<RelDataType> types) |
static RexCall |
findOperatorCall(SqlOperator operator,
RexNode node)
Returns whether a given node contains a RexCall with a specified operator
|
static List<RexNode> |
fixUp(RexBuilder rexBuilder,
List<RexNode> nodes,
RelDataType rowType)
Fixes up the type of all
RexInputRefs in an
expression to match differences in nullability. |
static List<RexNode> |
flatten(List<? extends RexNode> exprs,
SqlOperator op)
Converts a list of operands into a list that is flat with respect to
the given operator.
|
static RexNode |
flatten(RexBuilder rexBuilder,
RexNode node)
Flattens an expression.
|
static com.google.common.collect.ImmutableList<RexNode> |
flattenAnd(Iterable<? extends RexNode> nodes)
Flattens a list of AND nodes.
|
static com.google.common.collect.ImmutableList<RexNode> |
flattenOr(Iterable<? extends RexNode> nodes)
Flattens a list of OR nodes.
|
static List<RexNode> |
generateCastExpressions(RexBuilder rexBuilder,
RelDataType lhsRowType,
List<RexNode> rhsExps)
Generates a cast for a row type.
|
static List<RexNode> |
generateCastExpressions(RexBuilder rexBuilder,
RelDataType lhsRowType,
RelDataType rhsRowType)
Generates a cast from one row type to another
|
static double |
getSelectivity(RexNode exp)
Returns a guess for the selectivity of an expression.
|
static boolean |
isAtomic(RexNode expr) |
static boolean |
isCallTo(RexNode expr,
SqlOperator op)
|
static boolean |
isFlat(RexNode expr)
Returns false if the expression can be optimized by flattening
calls to an associative operator such as AND and OR.
|
static boolean |
isNull(RexNode expr)
Returns whether a node represents the NULL value or a series of nested
CAST(NULL AS type) calls. |
static boolean |
isNullabilityCast(RelDataTypeFactory typeFactory,
RexNode node)
Returns whether an expression is a cast just for the purposes of
nullability, not changing any other aspect of the type.
|
static boolean |
isNullLiteral(RexNode node,
boolean allowCast)
Returns whether a node represents the NULL value.
|
static Pair<String,String> |
makeKey(RexNode expr)
Creates a key for
RexNode which is the same as another key of
another RexNode only if the two have both the same type and textual
representation. |
static RexNode |
pullFactors(RexBuilder rexBuilder,
RexNode node)
Creates an equivalent version of a node where common factors among ORs
are pulled up.
|
static boolean |
requiresDecimalExpansion(List<RexNode> operands,
boolean recurse)
Determines whether any operand of a set requires decimal expansion
|
static boolean |
requiresDecimalExpansion(RexNode expr,
boolean recurse)
Determines whether a
RexCall requires decimal expansion. |
static boolean |
requiresDecimalExpansion(RexProgram program,
boolean recurse)
Returns whether a
RexProgram contains expressions which require
decimal expansion. |
static Iterable<RexNode> |
shift(Iterable<RexNode> nodes,
int offset)
Shifts every
RexInputRef in an expression by offset. |
static RexNode |
shift(RexNode node,
int offset)
Shifts every
RexInputRef in an expression by offset. |
static RexNode |
shift(RexNode node,
int start,
int offset)
|
static RexNode |
toCnf(RexBuilder rexBuilder,
RexNode rex)
Converts an expression to conjunctive normal form (CNF).
|
static List<RelDataType> |
types(List<? extends RexNode> nodes)
Transforms a list of expressions into a list of their types.
|
public static double getSelectivity(RexNode exp)
exp - expression of interest, or null for none (implying a
selectivity of 1.0)public static List<RexNode> generateCastExpressions(RexBuilder rexBuilder, RelDataType lhsRowType, RelDataType rhsRowType)
rexBuilder - RexBuilder to use for constructing castslhsRowType - target row typerhsRowType - source row type; fields must be 1-to-1 with lhsRowType,
in same orderpublic static List<RexNode> generateCastExpressions(RexBuilder rexBuilder, RelDataType lhsRowType, List<RexNode> rhsExps)
rexBuilder - RexBuilder to use for constructing castslhsRowType - target row typerhsExps - expressions to be castpublic static boolean isNullLiteral(RexNode node, boolean allowCast)
Examples:
RexLiteral Unknown, returns false.
CAST(NULL AS type), returns true if
allowCast is true, false otherwise.
CAST(CAST(NULL AS type) AS type)),
returns false.
public static boolean isNull(RexNode expr)
CAST(NULL AS type) calls. For example:
isNull(CAST(CAST(NULL as INTEGER) AS VARCHAR(1)))
returns true.public static boolean isNullabilityCast(RelDataTypeFactory typeFactory, RexNode node)
public static RexCall findOperatorCall(SqlOperator operator, RexNode node)
operator - to look fornode - a RexNode treepublic static boolean containsInputRef(RexNode node)
node - a RexNode treepublic static boolean containsFieldAccess(RexNode node)
RexFieldAccess nodes.node - a RexNode treepublic static boolean requiresDecimalExpansion(RexNode expr, boolean recurse)
RexCall requires decimal expansion. It
usually requires expansion if it has decimal operands.
Exceptions to this rule are:
expr - expression possibly in need of expansionrecurse - whether to check nested callspublic static boolean requiresDecimalExpansion(List<RexNode> operands, boolean recurse)
public static boolean requiresDecimalExpansion(RexProgram program, boolean recurse)
RexProgram contains expressions which require
decimal expansion.public static boolean canReinterpretOverflow(RexCall call)
public static boolean containCommonExprs(List<RexNode> exprs, boolean fail)
public static boolean containForwardRefs(List<RexNode> exprs, RelDataType inputRowType, boolean fail)
RexInputRef referencing
field i or greater.exprs - Array of expressionsinputRowType - Input row typefail - Whether to assert if there is a forward referencepublic static boolean containComplexExprs(List<RexNode> exprs)
RexVariable (or a subtype such
as RexInputRef) or RexLiteral.public static boolean isAtomic(RexNode expr)
public static boolean isCallTo(RexNode expr, SqlOperator op)
public static RelDataType createStructType(RelDataTypeFactory typeFactory, List<RexNode> exprs)
typeFactory - Type factoryexprs - Expressionspublic static RelDataType createStructType(RelDataTypeFactory typeFactory, List<? extends RexNode> exprs, List<String> names)
The array of field names may be null, or any of the names within it can be null. We recommend using explicit names where possible, because it makes it much easier to figure out the intent of fields when looking at planner output.
typeFactory - Type factoryexprs - Expressionsnames - Field names, may be null, or elements may be nullpublic static boolean compatibleTypes(List<RexNode> exprs, RelDataType type, boolean fail)
exprs - Array of expressionstype - Typefail - Whether to fail if there is a mismatchRelOptUtil.eq(String, RelDataType, String, RelDataType, boolean)public static Pair<String,String> makeKey(RexNode expr)
RexNode which is the same as another key of
another RexNode only if the two have both the same type and textual
representation. For example, "10" integer and "10" bigint result in
different keys.public static boolean containIdentity(List<? extends RexNode> exprs, RelDataType rowType, boolean fail)
RexInputRef objects with types corresponding to the
underlying datatype.public static RexNode composeConjunction(RexBuilder rexBuilder, Iterable<? extends RexNode> nodes, boolean nullOnEmpty)
nullOnEmpty and expression is TRUE.public static com.google.common.collect.ImmutableList<RexNode> flattenAnd(Iterable<? extends RexNode> nodes)
Treats null nodes as literal TRUE (i.e. ignores them).
public static RexNode composeDisjunction(RexBuilder rexBuilder, Iterable<? extends RexNode> nodes, boolean nullOnEmpty)
public static com.google.common.collect.ImmutableList<RexNode> flattenOr(Iterable<? extends RexNode> nodes)
public static List<RelCollation> apply(Mappings.TargetMapping mapping, List<RelCollation> collationList)
mapping - MappingcollationList - Collation listpublic static RelCollation apply(Mappings.TargetMapping mapping, RelCollation collation)
mapping - Mappingcollation - Collationpublic static RelFieldCollation apply(Mappings.TargetMapping mapping, RelFieldCollation fieldCollation)
If the field is not mapped, returns null.
mapping - MappingfieldCollation - Field collationpublic static List<RelFieldCollation> applyFields(Mappings.TargetMapping mapping, List<RelFieldCollation> fieldCollations)
mapping - MappingfieldCollations - Field collationspublic static RexNode apply(Mappings.TargetMapping mapping, RexNode node)
public static Iterable<RexNode> apply(Mappings.TargetMapping mapping, Iterable<? extends RexNode> nodes)
public static <T extends RexNode> T[] apply(RexVisitor<T> shuttle, T[] exprs)
shuttle - Shuttleexprs - Array of expressionspublic static void apply(RexVisitor<Void> visitor, RexNode[] exprs, RexNode expr)
visitor - Visitorexprs - Array of expressionsexpr - Single expression, may be nullpublic static void apply(RexVisitor<Void> visitor, List<? extends RexNode> exprs, RexNode expr)
visitor - Visitorexprs - List of expressionsexpr - Single expression, may be nullpublic static RexNode flatten(RexBuilder rexBuilder, RexNode node)
Returns the same expression if it is already flat.
public static List<RexNode> flatten(List<? extends RexNode> exprs, SqlOperator op)
public static boolean isFlat(RexNode expr)
public static RexNode toCnf(RexBuilder rexBuilder, RexNode rex)
The following expression is in CNF:
(a OR b) AND (c OR d)
The following expression is not in CNF:
(a AND b) OR cbut can be converted to CNF:
(a OR c) AND (b OR c)
The following expression is not in CNF:
NOT (a OR NOT b)but can be converted to CNF by applying de Morgan's theorem:
NOT a AND b
Expressions not involving AND, OR or NOT at the top level are in CNF.
public static <E> boolean exists(List<? extends E> list, Predicate1<E> predicate)
list for which
predicate is true.public static <E> boolean all(List<? extends E> list, Predicate1<E> predicate)
predicate is true for all elements of
list.public static RexNode shift(RexNode node, int offset)
RexInputRef in an expression by offset.public static Iterable<RexNode> shift(Iterable<RexNode> nodes, int offset)
RexInputRef in an expression by offset.public static RexNode pullFactors(RexBuilder rexBuilder, RexNode node)
For example,
(a AND b) OR (a AND c AND d)
becomes
a AND (b OR (c AND d))
Note that this result is not in CNF
(see toCnf(RexBuilder, RexNode)) because there is an AND inside an
OR.
This form is useful if, say, a contains columns from only the
left-hand side of a join, and can be pushed to the left input.
rexBuilder - Rex buildernode - Expression to transformpublic static List<RexNode> fixUp(RexBuilder rexBuilder, List<RexNode> nodes, RelDataType rowType)
RexInputRefs in an
expression to match differences in nullability.
Such differences in nullability occur when expressions are moved through outer joins.
Throws if there any greater inconsistencies of type.
public static List<RelDataType> types(List<? extends RexNode> nodes)
public static List<RelDataTypeFamily> families(List<RelDataType> types)
Copyright © 2012–2015 The Apache Software Foundation. All rights reserved.