Class RexLiteral
There are several methods for creating literals in RexBuilder
:
RexBuilder.makeLiteral(boolean)
and so forth.
How is the value stored? In that respect, the class is somewhat of a black
box. There is a getValue()
method which returns the value as an
object, but the type of that value is implementation detail, and it is best
that your code does not depend upon that knowledge. It is better to use
task-oriented methods such as getValue2()
and
toJavaString(java.lang.Comparable, org.apache.calcite.sql.type.SqlTypeName, org.apache.calcite.rel.type.RelDataType, org.apache.calcite.rex.RexDigestIncludeType)
.
The allowable types and combinations are:
TypeName | Meaning | Value type |
---|---|---|
SqlTypeName.NULL |
The null value. It has its own special type. | null |
SqlTypeName.BOOLEAN |
Boolean, namely TRUE , FALSE or
UNKNOWN . |
Boolean , or null represents the UNKNOWN value |
SqlTypeName.DECIMAL |
Exact number, for example 0 , -.5 ,
12345 . |
BigDecimal |
SqlTypeName.DOUBLE ,
SqlTypeName.REAL ,
SqlTypeName.FLOAT |
Approximate number, for example 6.023E-23 . |
Double . |
SqlTypeName.DATE |
Date, for example DATE '1969-04'29' |
Calendar ;
also Calendar (UTC time zone)
and Integer (days since POSIX epoch) |
SqlTypeName.TIME |
Time, for example TIME '18:37:42.567' |
Calendar ;
also Calendar (UTC time zone)
and Integer (milliseconds since midnight) |
SqlTypeName.TIMESTAMP |
Timestamp, for example TIMESTAMP '1969-04-29
18:37:42.567' |
TimestampString ;
also Calendar (UTC time zone)
and Long (milliseconds since POSIX epoch) |
SqlTypeName.INTERVAL_DAY ,
SqlTypeName.INTERVAL_DAY_HOUR ,
SqlTypeName.INTERVAL_DAY_MINUTE ,
SqlTypeName.INTERVAL_DAY_SECOND ,
SqlTypeName.INTERVAL_HOUR ,
SqlTypeName.INTERVAL_HOUR_MINUTE ,
SqlTypeName.INTERVAL_HOUR_SECOND ,
SqlTypeName.INTERVAL_MINUTE ,
SqlTypeName.INTERVAL_MINUTE_SECOND ,
SqlTypeName.INTERVAL_SECOND |
Interval, for example INTERVAL '4:3:2' HOUR TO SECOND |
BigDecimal ;
also Long (milliseconds) |
SqlTypeName.INTERVAL_YEAR ,
SqlTypeName.INTERVAL_YEAR_MONTH ,
SqlTypeName.INTERVAL_MONTH |
Interval, for example INTERVAL '2-3' YEAR TO MONTH |
BigDecimal ;
also Integer (months) |
SqlTypeName.CHAR |
Character constant, for example 'Hello, world!' ,
'' , _N'Bonjour' , _ISO-8859-1'It''s superman!'
COLLATE SHIFT_JIS$ja_JP$2 . These are always CHAR, never VARCHAR. |
NlsString ;
also String |
SqlTypeName.BINARY |
Binary constant, for example X'7F34' . (The number of hexits
must be even; see above.) These constants are always BINARY, never
VARBINARY. |
ByteBuffer ;
also byte[] |
SqlTypeName.SYMBOL |
A symbol is a special type used to make parsing easier; it is not part of
the SQL standard, and is not exposed to end-users. It is used to hold a flag,
such as the LEADING flag in a call to the function
TRIM([LEADING|TRAILING|BOTH] chars FROM string) . |
An enum class |
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescription<R,
P> R accept
(RexBiVisitor<R, P> visitor, P arg) Accepts a visitor with a payload, dispatching to the right overloadedRexBiVisitor.visitInputRef(RexInputRef, Object)
visitXxx} method.<R> R
accept
(RexVisitor<R> visitor) Accepts a visitor, dispatching to the right overloadedvisitXxx
method.static boolean
booleanValue
(RexNode node) final String
computeDigest
(RexDigestIncludeType includeType) Returns a string which concisely describes the definition of this rex literal.boolean
static @PolyNull RexLiteral
fromJdbcString
(RelDataType type, SqlTypeName typeName, @PolyNull String literal) Converts a Jdbc string into a RexLiteral.getKind()
Returns the kind of node this is.getType()
@Nullable Comparable
getValue()
Returns the value of this literal.@Nullable Object
Returns the value of this literal, in the form that the calculator program builder wants it.@Nullable Object
Returns the value of this literal, in the form that the rex-to-lix translator wants it.@Nullable Comparable
Returns the value of this literal, in the form thatRexInterpreter
wants it.<T> @Nullable T
getValueAs
(Class<T> clazz) Returns the value of this literal as an instance of the specified class.int
hashCode()
static int
Returns the value of a literal, cast, or unary minus, as an int; never null.boolean
Returns whether this expression always returns false.boolean
Returns whether this expression always returns true.boolean
isNull()
Returns whether this literal's value is null.static boolean
isNullLiteral
(RexNode node) static Number
numberValue
(RexNode node) Returns the value of a literal, cast, or unary minus, as a number; never null.void
Prints the value this literal as a Java string constant.static SqlTypeName
strictTypeName
(RelDataType type) Returns the strict literal type for a given type.static @Nullable String
stringValue
(RexNode node) static boolean
validConstant
(@Nullable Object o, Litmus litmus) Returns whether a value is valid as a constant value, using the same criteria asvalueMatchesType(java.lang.Comparable, org.apache.calcite.sql.type.SqlTypeName, boolean)
.static @Nullable Comparable
static boolean
valueMatchesType
(@Nullable Comparable value, SqlTypeName typeName, boolean strict) Returns whether a value is appropriate for its type.
-
Method Details
-
computeDigest
@RequiresNonNull({"typeName","type"}) public final String computeDigest(@UnknownInitialization RexLiteral this, RexDigestIncludeType includeType) Returns a string which concisely describes the definition of this rex literal. Two literals are equivalent if and only if their digests are the same.The digest does not contain the expression's identity, but does include the identity of children.
Technically speaking 1:INT differs from 1:FLOAT, so we need data type in the literal's digest, however we want to avoid extra verbosity of the
RelNode.getDigest()
for readability purposes, so we omit type info in certain cases. For instance, 1:INT becomes 1 (INT is implied by default), however 1:BIGINT always holds the typeHere's a non-exhaustive list of the "well known cases":
- Hide "NOT NULL" for not null literals
- Hide INTEGER, BOOLEAN, SYMBOL, TIME(0), TIMESTAMP(0), DATE(0) types
- Hide collation when it matches IMPLICIT/COERCIBLE
- Hide charset when it matches default
- Hide CHAR(xx) when literal length is equal to the precision of the type. In other words, use 'Bob' instead of 'Bob':CHAR(3)
- Hide BOOL for AND/OR arguments. In other words, AND(true, null) means null is BOOL.
- Hide types for literals in simple binary operations (e.g. +, -, *, /,
comparison) when type of the other argument is clear.
See
RexCall.computeDigest(boolean)
For instance: =(true. null) means null is BOOL. =($0, null) means the type of null matches the type of $0.
- Parameters:
includeType
- whether the digest should include type or not- Returns:
- digest
-
valueMatchesType
public static boolean valueMatchesType(@Nullable Comparable value, SqlTypeName typeName, boolean strict) Returns whether a value is appropriate for its type. (We have rules about these things!) -
strictTypeName
Returns the strict literal type for a given type. The rules should keep sync with whatRexBuilder.makeLiteral(java.lang.Comparable, org.apache.calcite.rel.type.RelDataType, org.apache.calcite.sql.type.SqlTypeName)
defines. -
validConstant
Returns whether a value is valid as a constant value, using the same criteria asvalueMatchesType(java.lang.Comparable, org.apache.calcite.sql.type.SqlTypeName, boolean)
. -
printAsJava
Prints the value this literal as a Java string constant. -
fromJdbcString
public static @PolyNull RexLiteral fromJdbcString(RelDataType type, SqlTypeName typeName, @PolyNull String literal) Converts a Jdbc string into a RexLiteral. This method accepts a string, as returned by the Jdbc method ResultSet.getString(), and restores the string into an equivalent RexLiteral. It allows one to use Jdbc strings as a common format for data.Returns null if and only if
literal
is null.- Parameters:
type
- data type of literal to be readtypeName
- type family of literalliteral
- the (non-SQL encoded) string representation, as returned by the Jdbc call to return a column as a string- Returns:
- a typed RexLiteral, or null
-
getTypeName
-
getType
-
getKind
Description copied from class:RexNode
Returns the kind of node this is. -
isNull
public boolean isNull()Returns whether this literal's value is null. -
getValue
Returns the value of this literal.For backwards compatibility, returns DATE. TIME and TIMESTAMP as a
Calendar
value in UTC time zone. -
getValue2
Returns the value of this literal, in the form that the calculator program builder wants it. -
getValue3
Returns the value of this literal, in the form that the rex-to-lix translator wants it. -
getValue4
Returns the value of this literal, in the form thatRexInterpreter
wants it. -
getValueAs
Returns the value of this literal as an instance of the specified class.The following SQL types allow more than one form:
- CHAR as
NlsString
orString
- TIME as
TimeString
,Integer
(milliseconds since midnight),Calendar
(in UTC) - DATE as
DateString
,Integer
(days since 1970-01-01),Calendar
- TIMESTAMP as
TimestampString
,Long
(milliseconds since 1970-01-01 00:00:00),Calendar
- DECIMAL as
BigDecimal
orLong
Called with
clazz
=Comparable
, returns the value in its native form.- Type Parameters:
T
- Return type- Parameters:
clazz
- Desired return type- Returns:
- Value of this literal in the desired type
- CHAR as
-
booleanValue
-
isAlwaysTrue
public boolean isAlwaysTrue()Description copied from class:RexNode
Returns whether this expression always returns true. (Such as if this expression is equal to the literalTRUE
.)- Overrides:
isAlwaysTrue
in classRexNode
-
isAlwaysFalse
public boolean isAlwaysFalse()Description copied from class:RexNode
Returns whether this expression always returns false. (Such as if this expression is equal to the literalFALSE
.)- Overrides:
isAlwaysFalse
in classRexNode
-
equals
Description copied from class:RexNode
Every node must implement
RexNode.equals(java.lang.Object)
based on its content -
hashCode
public int hashCode()Description copied from class:RexNode
Every node must implement
RexNode.hashCode()
consistent withRexNode.equals(java.lang.Object)
-
value
-
numberValue
Returns the value of a literal, cast, or unary minus, as a number; never null. -
intValue
Returns the value of a literal, cast, or unary minus, as an int; never null. -
stringValue
-
isNullLiteral
-
accept
Description copied from class:RexNode
Accepts a visitor, dispatching to the right overloadedvisitXxx
method.Also see
RexUtil.apply(RexVisitor, java.util.List, RexNode)
, which applies a visitor to several expressions simultaneously. -
accept
Description copied from class:RexNode
Accepts a visitor with a payload, dispatching to the right overloadedRexBiVisitor.visitInputRef(RexInputRef, Object)
visitXxx} method.
-