public class RexBuilder
extends java.lang.Object
Some common literal values (NULL, TRUE, FALSE, 0, 1, '') are cached.
Modifier and Type | Field and Description |
---|---|
private RexLiteral |
booleanFalse |
private RexLiteral |
booleanTrue |
private RexLiteral |
charEmpty |
private RexLiteral |
constantNull |
static SqlSpecialOperator |
GET_OPERATOR
Special operator that accesses an unadvertised field of an input record.
|
private static java.math.BigDecimal |
INT_MAX
The largest valid
int value, as a BigDecimal . |
private static java.math.BigDecimal |
INT_MIN
The smallest valid
int value, as a BigDecimal . |
private SqlStdOperatorTable |
opTab |
protected RelDataTypeFactory |
typeFactory |
Constructor and Description |
---|
RexBuilder(RelDataTypeFactory typeFactory)
Creates a RexBuilder.
|
Modifier and Type | Method and Description |
---|---|
RexNode |
addAggCall(AggregateCall aggCall,
int groupCount,
boolean indicator,
java.util.List<AggregateCall> aggCalls,
java.util.Map<AggregateCall,RexNode> aggCallMapping,
java.util.List<RelDataType> aggArgTypes)
Deprecated.
|
RexNode |
addAggCall(AggregateCall aggCall,
int groupCount,
java.util.List<AggregateCall> aggCalls,
java.util.Map<AggregateCall,RexNode> aggCallMapping,
java.util.List<RelDataType> aggArgTypes)
Creates a reference to an aggregate call, checking for repeated calls.
|
protected static org.apache.calcite.avatica.util.TimeUnit |
baseUnit(SqlTypeName unit)
Returns the lowest granularity unit for the given unit.
|
(package private) boolean |
canRemoveCastFromLiteral(RelDataType toType,
java.lang.Comparable value,
SqlTypeName fromTypeName) |
private static java.lang.Object |
clean(java.lang.Object o,
RelDataType type)
Converts the type of a value to comply with
RexLiteral.valueMatchesType(java.lang.Comparable, org.apache.calcite.sql.type.SqlTypeName, boolean) . |
RexLiteral |
constantNull()
Creates a constant for the SQL
NULL value. |
RexNode |
copy(RexNode expr)
Creates a copy of an expression, which may have been created using a
different RexBuilder and/or
RelDataTypeFactory , using this
RexBuilder. |
RexNode |
decodeIntervalOrDecimal(RexNode node)
Retrieves an interval or decimal node's integer representation
|
RelDataType |
deriveReturnType(SqlOperator op,
java.util.List<? extends RexNode> exprs)
Derives the return type of a call to an operator.
|
RexNode |
encodeIntervalOrDecimal(RexNode value,
RelDataType type,
boolean checkOverflow)
Casts a decimal's integer representation to a decimal node.
|
RexNode |
ensureType(RelDataType type,
RexNode node,
boolean matchNullability)
Ensures expression is interpreted as a specified type.
|
SqlStdOperatorTable |
getOpTab()
Returns this RexBuilder's operator table
|
RelDataTypeFactory |
getTypeFactory()
Returns this RexBuilder's type factory
|
private RelDataType |
guessType(java.lang.Object value) |
java.util.List<? extends RexNode> |
identityProjects(RelDataType rowType)
Creates a list of
RexInputRef expressions,
projecting the fields of a given record type. |
RexNode |
makeAbstractCast(RelDataType type,
RexNode exp)
Creates a call to the CAST operator.
|
RexLiteral |
makeApproxLiteral(java.math.BigDecimal bd)
Creates a double-precision literal.
|
RexLiteral |
makeApproxLiteral(java.math.BigDecimal bd,
RelDataType type)
Creates an approximate numeric literal (double or float).
|
RexLiteral |
makeBigintLiteral(java.math.BigDecimal bd)
Creates a BIGINT literal.
|
RexLiteral |
makeBinaryLiteral(org.apache.calcite.avatica.util.ByteString byteString)
Creates a byte array literal.
|
RexNode |
makeCall(RelDataType returnType,
SqlOperator op,
java.util.List<RexNode> exprs)
Creates a call with a list of arguments and a predetermined type.
|
RexNode |
makeCall(SqlOperator op,
java.util.List<? extends RexNode> exprs)
Creates a call with an array of arguments.
|
RexNode |
makeCall(SqlOperator op,
RexNode... exprs)
Creates a call with a list of arguments.
|
RexNode |
makeCast(RelDataType type,
RexNode exp)
Creates a call to the CAST operator.
|
RexNode |
makeCast(RelDataType type,
RexNode exp,
boolean matchNullability)
Creates a call to the CAST operator, expanding if possible, and optionally
also preserving nullability.
|
private RexNode |
makeCastBooleanToExact(RelDataType toType,
RexNode exp) |
private RexNode |
makeCastExactToBoolean(RelDataType toType,
RexNode exp) |
private RexNode |
makeCastIntervalToExact(RelDataType toType,
RexNode exp) |
RexLiteral |
makeCharLiteral(NlsString str)
Creates a character string literal from an
NlsString . |
RexNode |
makeCorrel(RelDataType type,
CorrelationId id)
Creates an expression referencing a correlation variable.
|
RexLiteral |
makeDateLiteral(java.util.Calendar calendar)
Deprecated.
|
RexLiteral |
makeDateLiteral(DateString date)
Creates a Date literal.
|
RexDynamicParam |
makeDynamicParam(RelDataType type,
int index)
Creates a reference to a dynamic parameter
|
RexLiteral |
makeExactLiteral(java.math.BigDecimal bd)
Creates a numeric literal.
|
RexLiteral |
makeExactLiteral(java.math.BigDecimal bd,
RelDataType type)
Creates a numeric literal.
|
RexNode |
makeFieldAccess(RexNode expr,
int i)
Creates an expression accessing a field with a given ordinal from a
record.
|
RexNode |
makeFieldAccess(RexNode expr,
java.lang.String fieldName,
boolean caseSensitive)
Creates an expression accessing a given named field from a record.
|
private RexNode |
makeFieldAccessInternal(RexNode expr,
RelDataTypeField field)
Creates an expression accessing a given field from a record.
|
RexLiteral |
makeFlag(java.lang.Enum flag)
Creates a literal representing a flag.
|
RexInputRef |
makeInputRef(RelDataType type,
int i)
Creates a reference to a given field of the input record.
|
RexInputRef |
makeInputRef(RelNode input,
int i)
Creates a reference to a given field of the input relational expression.
|
RexLiteral |
makeIntervalLiteral(java.math.BigDecimal v,
SqlIntervalQualifier intervalQualifier)
Creates a literal representing an interval value, for example
INTERVAL '3-7' YEAR TO MONTH . |
RexLiteral |
makeIntervalLiteral(SqlIntervalQualifier intervalQualifier)
Creates a literal representing an interval type, for example
YEAR TO MONTH or DOW . |
RexLiteral |
makeLiteral(boolean b)
Creates a boolean literal.
|
protected RexLiteral |
makeLiteral(java.lang.Comparable o,
RelDataType type,
SqlTypeName typeName)
Internal method to create a call to a literal.
|
RexNode |
makeLiteral(java.lang.Object value,
RelDataType type,
boolean allowCast)
Creates a literal of a given type.
|
RexLiteral |
makeLiteral(java.lang.String s)
Creates a character string literal.
|
RexNode |
makeNewInvocation(RelDataType type,
java.util.List<RexNode> exprs)
Creates an invocation of the NEW operator.
|
RexNode |
makeNotNull(RexNode exp)
Makes a cast of a value to NOT NULL;
no-op if the type already has NOT NULL.
|
RexLiteral |
makeNullLiteral(RelDataType type)
Creates a literal whose value is NULL, with a particular type.
|
RexNode |
makeNullLiteral(SqlTypeName typeName)
Deprecated.
|
RexNode |
makeNullLiteral(SqlTypeName typeName,
int precision)
Deprecated.
|
RexNode |
makeOver(RelDataType type,
SqlAggFunction operator,
java.util.List<RexNode> exprs,
java.util.List<RexNode> partitionKeys,
com.google.common.collect.ImmutableList<RexFieldCollation> orderKeys,
RexWindowBound lowerBound,
RexWindowBound upperBound,
boolean physical,
boolean allowPartial,
boolean nullWhenCountZero,
boolean distinct)
Deprecated.
|
RexNode |
makeOver(RelDataType type,
SqlAggFunction operator,
java.util.List<RexNode> exprs,
java.util.List<RexNode> partitionKeys,
com.google.common.collect.ImmutableList<RexFieldCollation> orderKeys,
RexWindowBound lowerBound,
RexWindowBound upperBound,
boolean physical,
boolean allowPartial,
boolean nullWhenCountZero,
boolean distinct,
boolean ignoreNulls)
Creates a call to a windowed agg.
|
RexPatternFieldRef |
makePatternFieldRef(java.lang.String alpha,
RelDataType type,
int i)
Creates a reference to a given field of the pattern.
|
protected RexLiteral |
makePreciseStringLiteral(org.apache.calcite.avatica.util.ByteString value,
java.lang.String charsetName,
SqlCollation collation)
Creates a character string literal with type CHAR.
|
protected RexLiteral |
makePreciseStringLiteral(java.lang.String s)
Creates a character string literal with type CHAR and default charset and
collation.
|
RexRangeRef |
makeRangeReference(RelDataType type,
int offset,
boolean nullable)
Creates a reference to all the fields in the row.
|
RexNode |
makeRangeReference(RelNode input)
Creates a reference to all the fields in the row.
|
RexNode |
makeReinterpretCast(RelDataType type,
RexNode exp,
RexNode checkOverflow)
Makes a reinterpret cast.
|
RexLiteral |
makeTimeLiteral(java.util.Calendar calendar,
int precision)
Deprecated.
|
RexLiteral |
makeTimeLiteral(TimeString time,
int precision)
Creates a Time literal.
|
RexLiteral |
makeTimestampLiteral(java.util.Calendar calendar,
int precision)
Deprecated.
|
RexLiteral |
makeTimestampLiteral(TimestampString timestamp,
int precision)
Creates a Timestamp literal.
|
RexLiteral |
makeTimestampWithLocalTimeZoneLiteral(TimestampString timestamp,
int precision)
Creates a Timestamp with local time-zone literal.
|
RexLiteral |
makeTimeWithLocalTimeZoneLiteral(TimeString time,
int precision)
Creates a Time with local time-zone literal.
|
RexWindow |
makeWindow(java.util.List<RexNode> partitionKeys,
com.google.common.collect.ImmutableList<RexFieldCollation> orderKeys,
RexWindowBound lowerBound,
RexWindowBound upperBound,
boolean isRows)
Creates a window specification.
|
RexNode |
makeZeroLiteral(RelDataType type)
Creates a literal of the default value for the given type.
|
RelDataType |
matchNullability(RelDataType type,
RexNode value)
Ensures that a type's nullability matches a value's nullability.
|
RexNode |
multiplyDivide(RexNode e,
java.math.BigDecimal multiplier,
java.math.BigDecimal divider) |
private static java.util.List<java.lang.Integer> |
nullableArgs(java.util.List<java.lang.Integer> list0,
java.util.List<RelDataType> types) |
private static org.apache.calcite.avatica.util.ByteString |
padRight(org.apache.calcite.avatica.util.ByteString s,
int length)
Returns a byte-string padded with zero bytes to make it at least a given
length,
|
private static NlsString |
padRight(NlsString s,
int length)
Returns an
NlsString with spaces to make it at least a given
length. |
private static java.lang.String |
padRight(java.lang.String s,
int length)
Returns a string padded with spaces to make it at least a given length.
|
private static java.lang.Comparable |
zeroValue(RelDataType type) |
public static final SqlSpecialOperator GET_OPERATOR
private static final java.math.BigDecimal INT_MIN
int
value, as a BigDecimal
.private static final java.math.BigDecimal INT_MAX
int
value, as a BigDecimal
.protected final RelDataTypeFactory typeFactory
private final RexLiteral booleanTrue
private final RexLiteral booleanFalse
private final RexLiteral charEmpty
private final RexLiteral constantNull
private final SqlStdOperatorTable opTab
public RexBuilder(RelDataTypeFactory typeFactory)
typeFactory
- Type factorypublic java.util.List<? extends RexNode> identityProjects(RelDataType rowType)
RexInputRef
expressions,
projecting the fields of a given record type.public RelDataTypeFactory getTypeFactory()
public SqlStdOperatorTable getOpTab()
public RexNode makeFieldAccess(RexNode expr, java.lang.String fieldName, boolean caseSensitive)
NOTE: Be careful choosing the value of caseSensitive
.
If the field name was supplied by an end-user (e.g. as a column alias in
SQL), use your session's case-sensitivity setting.
Only hard-code true
if you are sure that the field name is
internally generated.
Hard-coding false
is almost certainly wrong.
expr
- Expression yielding a recordfieldName
- Name of field in recordcaseSensitive
- Whether match is case-sensitivepublic RexNode makeFieldAccess(RexNode expr, int i)
expr
- Expression yielding a recordi
- Ordinal of fieldprivate RexNode makeFieldAccessInternal(RexNode expr, RelDataTypeField field)
expr
- Expression yielding a recordfield
- Fieldpublic RexNode makeCall(RelDataType returnType, SqlOperator op, java.util.List<RexNode> exprs)
public RexNode makeCall(SqlOperator op, java.util.List<? extends RexNode> exprs)
If you already know the return type of the call, then
makeCall(org.apache.calcite.rel.type.RelDataType, org.apache.calcite.sql.SqlOperator, java.util.List)
is preferred.
public final RexNode makeCall(SqlOperator op, RexNode... exprs)
Equivalent to
makeCall(op, exprList.toArray(new RexNode[exprList.size()]))
.
public RelDataType deriveReturnType(SqlOperator op, java.util.List<? extends RexNode> exprs)
op
- the operator being calledexprs
- actual operandspublic RexNode addAggCall(AggregateCall aggCall, int groupCount, java.util.List<AggregateCall> aggCalls, java.util.Map<AggregateCall,RexNode> aggCallMapping, java.util.List<RelDataType> aggArgTypes)
Argument types help to optimize for repeated aggregates. For instance count(42) is equivalent to count(*).
aggCall
- aggregate call to be addedgroupCount
- number of groups in the aggregate relationaggCalls
- destination list of aggregate callsaggCallMapping
- the dictionary of already added callsaggArgTypes
- Argument types, not null@Deprecated public RexNode addAggCall(AggregateCall aggCall, int groupCount, boolean indicator, java.util.List<AggregateCall> aggCalls, java.util.Map<AggregateCall,RexNode> aggCallMapping, java.util.List<RelDataType> aggArgTypes)
private static java.util.List<java.lang.Integer> nullableArgs(java.util.List<java.lang.Integer> list0, java.util.List<RelDataType> types)
@Deprecated public RexNode makeOver(RelDataType type, SqlAggFunction operator, java.util.List<RexNode> exprs, java.util.List<RexNode> partitionKeys, com.google.common.collect.ImmutableList<RexFieldCollation> orderKeys, RexWindowBound lowerBound, RexWindowBound upperBound, boolean physical, boolean allowPartial, boolean nullWhenCountZero, boolean distinct)
public RexNode makeOver(RelDataType type, SqlAggFunction operator, java.util.List<RexNode> exprs, java.util.List<RexNode> partitionKeys, com.google.common.collect.ImmutableList<RexFieldCollation> orderKeys, RexWindowBound lowerBound, RexWindowBound upperBound, boolean physical, boolean allowPartial, boolean nullWhenCountZero, boolean distinct, boolean ignoreNulls)
public RexWindow makeWindow(java.util.List<RexNode> partitionKeys, com.google.common.collect.ImmutableList<RexFieldCollation> orderKeys, RexWindowBound lowerBound, RexWindowBound upperBound, boolean isRows)
partitionKeys
- Partition keysorderKeys
- Order keyslowerBound
- Lower boundupperBound
- Upper boundisRows
- Whether physical. True if row-based, false if
range-basedpublic RexLiteral constantNull()
NULL
value.public RexNode makeCorrel(RelDataType type, CorrelationId id)
id
- Name of variabletype
- Type of variablepublic RexNode makeNewInvocation(RelDataType type, java.util.List<RexNode> exprs)
type
- Type to be instantiatedexprs
- Arguments to NEW operatorpublic RexNode makeCast(RelDataType type, RexNode exp)
type
- Type to cast toexp
- Expression being castpublic RexNode makeCast(RelDataType type, RexNode exp, boolean matchNullability)
Tries to expand the cast, and therefore the result may be something
other than a RexCall
to the CAST operator, such as a
RexLiteral
.
type
- Type to cast toexp
- Expression being castmatchNullability
- Whether to ensure the result has the same
nullability as type
protected static org.apache.calcite.avatica.util.TimeUnit baseUnit(SqlTypeName unit)
boolean canRemoveCastFromLiteral(RelDataType toType, java.lang.Comparable value, SqlTypeName fromTypeName)
private RexNode makeCastExactToBoolean(RelDataType toType, RexNode exp)
private RexNode makeCastBooleanToExact(RelDataType toType, RexNode exp)
private RexNode makeCastIntervalToExact(RelDataType toType, RexNode exp)
public RexNode multiplyDivide(RexNode e, java.math.BigDecimal multiplier, java.math.BigDecimal divider)
public RexNode encodeIntervalOrDecimal(RexNode value, RelDataType type, boolean checkOverflow)
An overflow check may be requested to ensure the internal value does not exceed the maximum value of the decimal type.
value
- integer representation of decimaltype
- type integer will be reinterpreted ascheckOverflow
- indicates whether an overflow check is required
when reinterpreting this particular value as the
decimal type. A check usually not required for
arithmetic, but is often required for rounding and
explicit casts.public RexNode decodeIntervalOrDecimal(RexNode node)
node
- the interval or decimal value as an opaque typepublic RexNode makeAbstractCast(RelDataType type, RexNode exp)
type
- Type to cast toexp
- Expression being castpublic RexNode makeReinterpretCast(RelDataType type, RexNode exp, RexNode checkOverflow)
type
- type returned by the castexp
- expression to be castedcheckOverflow
- whether an overflow check is requiredpublic RexNode makeNotNull(RexNode exp)
public RexNode makeRangeReference(RelNode input)
input
- Input relational expressionpublic RexRangeRef makeRangeReference(RelDataType type, int offset, boolean nullable)
For example, if the input row has type T{f0,f1,f2,f3,f4}
then makeRangeReference(T{f0,f1,f2,f3,f4}, S{f3,f4}, 3)
is
an expression which yields the last 2 fields.
type
- Type of the resulting range record.offset
- Index of first field.nullable
- Whether the record is nullable.public RexInputRef makeInputRef(RelDataType type, int i)
type
- Type of fieldi
- Ordinal of fieldpublic RexInputRef makeInputRef(RelNode input, int i)
input
- Input relational expressioni
- Ordinal of fieldidentityProjects(RelDataType)
public RexPatternFieldRef makePatternFieldRef(java.lang.String alpha, RelDataType type, int i)
alpha
- the pattern nametype
- Type of fieldi
- Ordinal of fieldpublic RexLiteral makeFlag(java.lang.Enum flag)
flag
- Flag valueprotected RexLiteral makeLiteral(java.lang.Comparable o, RelDataType type, SqlTypeName typeName)
makeDateLiteral(DateString)
, makeLiteral(boolean)
,
makeLiteral(String)
.o
- Value of literal, must be appropriate for the typetype
- Type of literaltypeName
- SQL type of literalpublic RexLiteral makeLiteral(boolean b)
public RexLiteral makeExactLiteral(java.math.BigDecimal bd)
public RexLiteral makeBigintLiteral(java.math.BigDecimal bd)
public RexLiteral makeExactLiteral(java.math.BigDecimal bd, RelDataType type)
public RexLiteral makeBinaryLiteral(org.apache.calcite.avatica.util.ByteString byteString)
public RexLiteral makeApproxLiteral(java.math.BigDecimal bd)
public RexLiteral makeApproxLiteral(java.math.BigDecimal bd, RelDataType type)
bd
- literal valuetype
- approximate numeric typepublic RexLiteral makeLiteral(java.lang.String s)
protected RexLiteral makePreciseStringLiteral(java.lang.String s)
s
- String valueprotected RexLiteral makePreciseStringLiteral(org.apache.calcite.avatica.util.ByteString value, java.lang.String charsetName, SqlCollation collation)
value
- String value in bytescharsetName
- SQL-level charset namecollation
- Sql collationpublic RexNode ensureType(RelDataType type, RexNode node, boolean matchNullability)
type
- desired typenode
- expressionmatchNullability
- whether to correct nullability of specified
type to match the expression; this usually should
be true, except for explicit casts which can
override default nullabilitypublic RelDataType matchNullability(RelDataType type, RexNode value)
public RexLiteral makeCharLiteral(NlsString str)
NlsString
.
If the string's charset and collation are not set, uses the system defaults.
@Deprecated public RexLiteral makeDateLiteral(java.util.Calendar calendar)
makeDateLiteral(DateString)
.public RexLiteral makeDateLiteral(DateString date)
@Deprecated public RexLiteral makeTimeLiteral(java.util.Calendar calendar, int precision)
makeTimeLiteral(TimeString, int)
.public RexLiteral makeTimeLiteral(TimeString time, int precision)
public RexLiteral makeTimeWithLocalTimeZoneLiteral(TimeString time, int precision)
@Deprecated public RexLiteral makeTimestampLiteral(java.util.Calendar calendar, int precision)
makeTimestampLiteral(TimestampString, int)
.public RexLiteral makeTimestampLiteral(TimestampString timestamp, int precision)
public RexLiteral makeTimestampWithLocalTimeZoneLiteral(TimestampString timestamp, int precision)
public RexLiteral makeIntervalLiteral(SqlIntervalQualifier intervalQualifier)
YEAR TO MONTH
or DOW
.public RexLiteral makeIntervalLiteral(java.math.BigDecimal v, SqlIntervalQualifier intervalQualifier)
INTERVAL '3-7' YEAR TO MONTH
.public RexDynamicParam makeDynamicParam(RelDataType type, int index)
type
- Type of dynamic parameterindex
- Index of dynamic parameterpublic RexLiteral makeNullLiteral(RelDataType type)
The typing is necessary because RexNodes are strictly typed. For
example, in the Rex world the NULL
parameter to
SUBSTRING(NULL FROM 2 FOR 4)
must have a valid VARCHAR type so
that the result type can be determined.
type
- Type to cast NULL to@Deprecated public RexNode makeNullLiteral(SqlTypeName typeName, int precision)
makeNullLiteral(RelDataType)
@Deprecated public RexNode makeNullLiteral(SqlTypeName typeName)
makeNullLiteral(RelDataType)
public RexNode copy(RexNode expr)
RelDataTypeFactory
, using this
RexBuilder.expr
- ExpressionRelDataTypeFactory.copyType(RelDataType)
public RexNode makeZeroLiteral(RelDataType type)
This value is:
type
- Typeprivate static java.lang.Comparable zeroValue(RelDataType type)
public RexNode makeLiteral(java.lang.Object value, RelDataType type, boolean allowCast)
value
- Valuetype
- TypeallowCast
- Whether to allow a cast. If false, value is always a
RexLiteral
but may not be the exact typeprivate static java.lang.Object clean(java.lang.Object o, RelDataType type)
RexLiteral.valueMatchesType(java.lang.Comparable, org.apache.calcite.sql.type.SqlTypeName, boolean)
.private RelDataType guessType(java.lang.Object value)
private static NlsString padRight(NlsString s, int length)
NlsString
with spaces to make it at least a given
length.private static java.lang.String padRight(java.lang.String s, int length)
private static org.apache.calcite.avatica.util.ByteString padRight(org.apache.calcite.avatica.util.ByteString s, int length)
Copyright © 2012–2019 The Apache Software Foundation. All rights reserved.