Class RexLiteral
- java.lang.Object
-
- org.apache.calcite.rex.RexNode
-
- org.apache.calcite.rex.RexLiteral
-
public class RexLiteral extends RexNode
Constant value in a row-expression.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 asgetValue2()
andtoJavaString(java.lang.Comparable, org.apache.calcite.sql.type.SqlTypeName)
.The allowable types and combinations are:
Allowable types for RexLiteral instances TypeName Meaning Value type SqlTypeName.NULL
The null value. It has its own special type. null SqlTypeName.BOOLEAN
Boolean, namely TRUE
,FALSE
orUNKNOWN
.Boolean
, or null represents the UNKNOWN valueSqlTypeName.DECIMAL
Exact number, for example 0
,-.5
,12345
.BigDecimal
SqlTypeName.DOUBLE
Approximate number, for example 6.023E-23
.BigDecimal
SqlTypeName.DATE
Date, for example DATE '1969-04'29'
Calendar
; alsoCalendar
(UTC time zone) andInteger
(days since POSIX epoch)SqlTypeName.TIME
Time, for example TIME '18:37:42.567'
Calendar
; alsoCalendar
(UTC time zone) andInteger
(milliseconds since midnight)SqlTypeName.TIMESTAMP
Timestamp, for example TIMESTAMP '1969-04-29 18:37:42.567'
TimestampString
; alsoCalendar
(UTC time zone) andLong
(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
; alsoLong
(milliseconds)SqlTypeName.INTERVAL_YEAR
,SqlTypeName.INTERVAL_YEAR_MONTH
,SqlTypeName.INTERVAL_MONTH
Interval, for example INTERVAL '2-3' YEAR TO MONTH
BigDecimal
; alsoInteger
(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
; alsoString
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
; alsobyte[]
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
Fields Modifier and Type Field Description private static com.google.common.collect.ImmutableList<org.apache.calcite.avatica.util.TimeUnit>
TIME_UNITS
private RelDataType
type
The real type of this literal, as reported bygetType()
.private SqlTypeName
typeName
An indication of the broad type of this literal -- even if its type isn't a SQL type.private java.lang.Comparable
value
The value of this literal.
-
Constructor Summary
Constructors Constructor Description RexLiteral(java.lang.Comparable value, RelDataType type, SqlTypeName typeName)
Creates aRexLiteral
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <R,P>
Raccept(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)
boolean
equals(java.lang.Object obj)
private static boolean
equals(java.lang.Object o1, java.lang.Object o2)
private static java.lang.Comparable
findValue(RexNode node)
static RexLiteral
fromJdbcString(RelDataType type, SqlTypeName typeName, java.lang.String literal)
Converts a Jdbc string into a RexLiteral.private static java.lang.String
getCalendarFormat(SqlTypeName typeName)
SqlKind
getKind()
Returns the kind of node this is.private static java.util.List<org.apache.calcite.avatica.util.TimeUnit>
getTimeUnits(SqlTypeName typeName)
Returns a list of the time units covered by an interval type such as HOUR TO SECOND.RelDataType
getType()
SqlTypeName
getTypeName()
java.lang.Comparable
getValue()
Returns the value of this literal.java.lang.Object
getValue2()
Returns the value of this literal, in the form that the calculator program builder wants it.java.lang.Object
getValue3()
Returns the value of this literal, in the form that the rex-to-lix translator wants it.java.lang.Comparable
getValue4()
Returns the value of this literal, in the form thatRexInterpreter
wants it.<T> T
getValueAs(java.lang.Class<T> clazz)
Returns the value of this literal as an instance of the specified class.int
hashCode()
private java.lang.String
intervalString(java.math.BigDecimal v)
static int
intValue(RexNode node)
boolean
isAlwaysFalse()
Returns whether this expression always returns false.boolean
isAlwaysTrue()
Returns whether this expression always returns true.boolean
isNull()
Returns whether this literal's value is null.static boolean
isNullLiteral(RexNode node)
private static void
pad(java.lang.StringBuilder b, java.lang.String s, int width)
void
printAsJava(java.io.PrintWriter pw)
Prints the value this literal as a Java string constant.private static void
printAsJava(java.lang.Comparable value, java.io.PrintWriter pw, SqlTypeName typeName, boolean java)
Prints a value as a Java string.static java.lang.String
stringValue(RexNode node)
private static java.lang.String
toJavaString(java.lang.Comparable value, SqlTypeName typeName)
static boolean
validConstant(java.lang.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 java.lang.Comparable
value(RexNode node)
static boolean
valueMatchesType(java.lang.Comparable value, SqlTypeName typeName, boolean strict)
private static int
width(org.apache.calcite.avatica.util.TimeUnit timeUnit)
-
-
-
Field Detail
-
value
private final java.lang.Comparable value
The value of this literal. Must be consistent with its type, as pervalueMatchesType(java.lang.Comparable, org.apache.calcite.sql.type.SqlTypeName, boolean)
. For example, you can't store anInteger
value here just because you feel like it -- all numbers are represented by aBigDecimal
. But since this field is private, it doesn't really matter how the values are stored.
-
type
private final RelDataType type
The real type of this literal, as reported bygetType()
.
-
typeName
private final SqlTypeName typeName
An indication of the broad type of this literal -- even if its type isn't a SQL type. Sometimes this will be different than the SQL type; for example, all exact numbers, including integers have typeNameSqlTypeName.DECIMAL
. SeevalueMatchesType(java.lang.Comparable, org.apache.calcite.sql.type.SqlTypeName, boolean)
for the definitive story.
-
TIME_UNITS
private static final com.google.common.collect.ImmutableList<org.apache.calcite.avatica.util.TimeUnit> TIME_UNITS
-
-
Constructor Detail
-
RexLiteral
RexLiteral(java.lang.Comparable value, RelDataType type, SqlTypeName typeName)
Creates aRexLiteral
.
-
-
Method Detail
-
valueMatchesType
public static boolean valueMatchesType(java.lang.Comparable value, SqlTypeName typeName, boolean strict)
- Returns:
- whether value is appropriate for its type (we have rules about these things)
-
toJavaString
private static java.lang.String toJavaString(java.lang.Comparable value, SqlTypeName typeName)
-
validConstant
public static boolean validConstant(java.lang.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)
.
-
getTimeUnits
private static java.util.List<org.apache.calcite.avatica.util.TimeUnit> getTimeUnits(SqlTypeName typeName)
Returns a list of the time units covered by an interval type such as HOUR TO SECOND. Adds MILLISECOND if the end is SECOND, to deal with fractional seconds.
-
intervalString
private java.lang.String intervalString(java.math.BigDecimal v)
-
pad
private static void pad(java.lang.StringBuilder b, java.lang.String s, int width)
-
width
private static int width(org.apache.calcite.avatica.util.TimeUnit timeUnit)
-
printAsJava
public void printAsJava(java.io.PrintWriter pw)
Prints the value this literal as a Java string constant.
-
printAsJava
private static void printAsJava(java.lang.Comparable value, java.io.PrintWriter pw, SqlTypeName typeName, boolean java)
Prints a value as a Java string. The value must be consistent with the type, as pervalueMatchesType(java.lang.Comparable, org.apache.calcite.sql.type.SqlTypeName, boolean)
.Typical return values:
- true
- null
- "Hello, world!"
- 1.25
- 1234ABCD
- Parameters:
value
- Valuepw
- Writer to write totypeName
- Type family
-
fromJdbcString
public static RexLiteral fromJdbcString(RelDataType type, SqlTypeName typeName, java.lang.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.If a null literal is provided, then a null pointer will be returned.
- 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
-
getCalendarFormat
private static java.lang.String getCalendarFormat(SqlTypeName typeName)
-
getTypeName
public SqlTypeName getTypeName()
-
getType
public RelDataType getType()
-
getKind
public SqlKind 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
public java.lang.Comparable getValue()
Returns the value of this literal.For backwards compatibility, returns DATE. TIME and TIMESTAMP as a
Calendar
value in UTC time zone.
-
getValue2
public java.lang.Object getValue2()
Returns the value of this literal, in the form that the calculator program builder wants it.
-
getValue3
public java.lang.Object getValue3()
Returns the value of this literal, in the form that the rex-to-lix translator wants it.
-
getValue4
public java.lang.Comparable getValue4()
Returns the value of this literal, in the form thatRexInterpreter
wants it.
-
getValueAs
public <T> T getValueAs(java.lang.Class<T> clazz)
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
public static boolean booleanValue(RexNode node)
-
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
public boolean equals(java.lang.Object obj)
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
public static java.lang.Comparable value(RexNode node)
-
intValue
public static int intValue(RexNode node)
-
stringValue
public static java.lang.String stringValue(RexNode node)
-
findValue
private static java.lang.Comparable findValue(RexNode node)
-
isNullLiteral
public static boolean isNullLiteral(RexNode node)
-
equals
private static boolean equals(java.lang.Object o1, java.lang.Object o2)
-
accept
public <R> R accept(RexVisitor<R> visitor)
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
public <R,P> R accept(RexBiVisitor<R,P> visitor, P arg)
Description copied from class:RexNode
Accepts a visitor with a payload, dispatching to the right overloadedRexBiVisitor.visitInputRef(RexInputRef, Object)
visitXxx} method.
-
-