org.eigenbase.rex
Class RexUtil

java.lang.Object
  extended by org.eigenbase.rex.RexUtil

public class RexUtil
extends Object

Utility methods concerning row-expressions.

Since:
Nov 23, 2003
Version:
$Id: //open/dev/farrago/src/org/eigenbase/rex/RexUtil.java#40 $
Author:
jhyde

Nested Class Summary
private static class RexUtil.ExpressionNormalizer
          Walks over expressions and builds a bank of common sub-expressions.
static class RexUtil.FieldAccessFinder
          Visitor which builds a bitmap of the inputs used by an expression.
private static class RexUtil.ForwardRefFinder
          Walks over an expression and throws an exception if it finds an RexInputRef with an ordinal beyond the number of fields in the input row type, or a RexLocalRef with ordinal greater than that set using RexUtil.ForwardRefFinder.setLimit(int).
 
Field Summary
static RexNode[] emptyExpressionArray
           
 
Constructor Summary
RexUtil()
           
 
Method Summary
static RexNode andRexNodeList(RexBuilder rexBuilder, List<RexNode> rexList)
          Creates an AND expression from a list of RexNodes
static boolean canReinterpretOverflow(RexCall call)
           
static RexNode[] clone(RexNode[] exps)
          Returns a copy of an array of row-expressions.
static RelDataType[] collectTypes(RexNode[] exprs)
          Collects the types of an array of row expressions.
static boolean compatibleTypes(RexNode[] exprs, RelDataType type, boolean fail)
          Returns whether the type of an array of expressions is compatible with a struct type.
static boolean containCommonExprs(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(RexNode[] exprs, RelDataType inputRowType, boolean fail)
          Returns whether an array of expressions contains a forward reference.
static boolean containIdentity(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.
(package private) static boolean containNonTrivialAggs(RexNode[] exprs, boolean fail)
          Returns whether an array of exp contains aggregate function calls whose arguments are not RexInputRef.s
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 RexInputRef[] createIdentityArray(RexNode[] exprs)
          Creates an array of RexInputRef objects referencing fields {0 ..
static RelDataType createStructType(RelDataTypeFactory typeFactory, RexNode[] exprs)
          Creates a record type with anonymous field names.
static RelDataType createStructType(RelDataTypeFactory typeFactory, RexNode[] exprs, String[] names)
          Creates a record type with specified field names.
static RexCall findOperatorCall(SqlOperator operator, RexNode node)
          Returns whether a given node contains a RexCall with a specified operator
static RexNode[] generateCastExpressions(RexBuilder rexBuilder, RelDataType lhsRowType, RelDataType rhsRowType)
          Generates a cast from one row type to another
static RexNode[] generateCastExpressions(RexBuilder rexBuilder, RelDataType lhsRowType, RexNode[] rhsExps)
          Generates a cast for a row type.
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)
          Returns whether a node is a call to a given operator.
static boolean isNull(RexNode node)
          Returns whether a node represents the NULL value or a series of nested CAST(NULL as ) calls
For Example:
isNull(CAST(CAST(NULL as INTEGER) AS VARCHAR(1))) returns true
static boolean isNullLiteral(RexNode node, boolean allowCast)
          Returns whether a node represents the NULL value.
static 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 maybeCast(RexBuilder rexBuilder, RelDataType lhsType, RexNode expr)
          Casts an expression to desired type, or returns the expression unchanged if it is already the correct type.
static RexNode orRexNodeList(RexBuilder rexBuilder, List<RexNode> rexList)
          Creates an OR expression from a list of RexNodes
static RexCall replaceOperands(RexCall call, RexNode[] operands)
          Replaces the operands of a call.
static boolean requiresDecimalExpansion(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 RexInputRef[] toInputRefs(int[] args, RelDataType rowType)
          Creates an array of RexInputRef objects, one for each field of a given rowtype, according to a permutation.
static RexInputRef[] toInputRefs(RelDataType rowType)
          Creates an array of RexInputRef, one for each field of a given rowtype.
static RexLocalRef[] toLocalRefs(RelDataType rowType)
          Creates an array of RexLocalRef objects, one for each field of a given rowtype.
static Integer[] toOrdinalArray(RexNode[] rexNodes)
          Converts an array of RexNode to an array of Integer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

emptyExpressionArray

public static final RexNode[] emptyExpressionArray
Constructor Detail

RexUtil

public RexUtil()
Method Detail

getSelectivity

public static double getSelectivity(RexNode exp)
Returns a guess for the selectivity of an expression.

Parameters:
exp - expression of interest, or null for none (implying a selectivity of 1.0)
Returns:
guessed selectivity

clone

public static RexNode[] clone(RexNode[] exps)
Returns a copy of an array of row-expressions.


generateCastExpressions

public static RexNode[] generateCastExpressions(RexBuilder rexBuilder,
                                                RelDataType lhsRowType,
                                                RelDataType rhsRowType)
Generates a cast from one row type to another

Parameters:
rexBuilder - RexBuilder to use for constructing casts
lhsRowType - target row type
rhsRowType - source row type; fields must be 1-to-1 with lhsRowType, in same order
Returns:
cast expressions

generateCastExpressions

public static RexNode[] generateCastExpressions(RexBuilder rexBuilder,
                                                RelDataType lhsRowType,
                                                RexNode[] rhsExps)
Generates a cast for a row type.

Parameters:
rexBuilder - RexBuilder to use for constructing casts
lhsRowType - target row type
rhsExps - expressions to be cast
Returns:
cast expressions

maybeCast

public static RexNode maybeCast(RexBuilder rexBuilder,
                                RelDataType lhsType,
                                RexNode expr)
Casts an expression to desired type, or returns the expression unchanged if it is already the correct type.

Parameters:
rexBuilder - Rex builder
lhsType - Desired type
expr - Expression
Returns:
Expression cast to desired type

isNullLiteral

public static boolean isNullLiteral(RexNode node,
                                    boolean allowCast)
Returns whether a node represents the NULL value.

Examples:


isNull

public static boolean isNull(RexNode node)
Returns whether a node represents the NULL value or a series of nested CAST(NULL as ) calls
For Example:
isNull(CAST(CAST(NULL as INTEGER) AS VARCHAR(1))) returns true


findOperatorCall

public static RexCall findOperatorCall(SqlOperator operator,
                                       RexNode node)
Returns whether a given node contains a RexCall with a specified operator

Parameters:
operator - to look for
node - a RexNode tree

containsInputRef

public static boolean containsInputRef(RexNode node)
Returns whether a given tree contains any {link RexInputRef} nodes.

Parameters:
node - a RexNode tree

containsFieldAccess

public static boolean containsFieldAccess(RexNode node)
Returns whether a given tree contains any RexFieldAccess nodes.

Parameters:
node - a RexNode tree

toInputRefs

public static RexInputRef[] toInputRefs(RelDataType rowType)
Creates an array of RexInputRef, one for each field of a given rowtype.


toLocalRefs

public static RexLocalRef[] toLocalRefs(RelDataType rowType)
Creates an array of RexLocalRef objects, one for each field of a given rowtype.


toInputRefs

public static RexInputRef[] toInputRefs(int[] args,
                                        RelDataType rowType)
Creates an array of RexInputRef objects, one for each field of a given rowtype, according to a permutation.

Parameters:
args - Permutation
rowType - Input row type
Returns:
Array of input refs

toOrdinalArray

public static Integer[] toOrdinalArray(RexNode[] rexNodes)
Converts an array of RexNode to an array of Integer. Every node must be a RexLocalRef.


collectTypes

public static RelDataType[] collectTypes(RexNode[] exprs)
Collects the types of an array of row expressions.

Parameters:
exprs - array of row expressions
Returns:
array of types

requiresDecimalExpansion

public static boolean requiresDecimalExpansion(RexNode expr,
                                               boolean recurse)
Determines whether a RexCall requires decimal expansion. It usually requires expansion if it has decimal operands.

Exceptions to this rule are:

Parameters:
expr - expression possibly in need of expansion
recurse - whether to check nested calls
Returns:
whether the expression requires expansion

requiresDecimalExpansion

public static boolean requiresDecimalExpansion(RexNode[] operands,
                                               boolean recurse)
Determines whether any operand of a set requires decimal expansion


requiresDecimalExpansion

public static boolean requiresDecimalExpansion(RexProgram program,
                                               boolean recurse)
Returns whether a RexProgram contains expressions which require decimal expansion.


canReinterpretOverflow

public static boolean canReinterpretOverflow(RexCall call)

createIdentityArray

public static RexInputRef[] createIdentityArray(RexNode[] exprs)
Creates an array of RexInputRef objects referencing fields {0 .. N} and having types {exprs[0].getType() .. exprs[N].getType()}.

Parameters:
exprs - Expressions whose types to mimic
Returns:
An array of input refs of the same length and types as exprs.

containCommonExprs

public static boolean containCommonExprs(RexNode[] exprs,
                                         boolean fail)
Returns whether an array of expressions has any common sub-expressions.


containForwardRefs

public static boolean containForwardRefs(RexNode[] exprs,
                                         RelDataType inputRowType,
                                         boolean fail)
Returns whether an array of expressions contains a forward reference. That is, if expression #i contains a RexInputRef referencing field i or greater.

Parameters:
exprs - Array of expressions
inputRowType -
fail - Whether to assert if there is a forward reference
Returns:
Whether there is a forward reference

containNonTrivialAggs

static boolean containNonTrivialAggs(RexNode[] exprs,
                                     boolean fail)
Returns whether an array of exp contains aggregate function calls whose arguments are not RexInputRef.s

Parameters:
exprs - Expressions
fail - Whether to assert if there is such a function call

containComplexExprs

public 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.


replaceOperands

public static RexCall replaceOperands(RexCall call,
                                      RexNode[] operands)
Replaces the operands of a call. The new operands' types must match the old operands' types.


isAtomic

public static boolean isAtomic(RexNode expr)

isCallTo

public static boolean isCallTo(RexNode expr,
                               SqlOperator op)
Returns whether a node is a call to a given operator.


createStructType

public static RelDataType createStructType(RelDataTypeFactory typeFactory,
                                           RexNode[] exprs)
Creates a record type with anonymous field names.


createStructType

public static RelDataType createStructType(RelDataTypeFactory typeFactory,
                                           RexNode[] exprs,
                                           String[] names)
Creates a record type with specified field 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.


compatibleTypes

public static boolean compatibleTypes(RexNode[] exprs,
                                      RelDataType type,
                                      boolean fail)
Returns whether the type of an array of expressions is compatible with a struct type.

Parameters:
exprs - Array of expressions
type - Type
fail - Whether to fail if there is a mismatch
Returns:
Whether every expression has the same type as the corresponding member of the struct type
See Also:
RelOptUtil.eq(String, RelDataType, String, RelDataType, boolean)

makeKey

public static 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. For example, "10" integer and "10" bigint result in different keys.


containIdentity

public static boolean containIdentity(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.


andRexNodeList

public static RexNode andRexNodeList(RexBuilder rexBuilder,
                                     List<RexNode> rexList)
Creates an AND expression from a list of RexNodes

Parameters:
rexList - list of RexNodes
Returns:
AND'd expression

orRexNodeList

public static RexNode orRexNodeList(RexBuilder rexBuilder,
                                    List<RexNode> rexList)
Creates an OR expression from a list of RexNodes

Parameters:
rexList - list of RexNodes
Returns:
OR'd expression