Class SqlLiteral
- java.lang.Object
-
- org.apache.calcite.sql.SqlNode
-
- org.apache.calcite.sql.SqlLiteral
-
- All Implemented Interfaces:
java.lang.Cloneable
- Direct Known Subclasses:
SqlAbstractDateTimeLiteral
,SqlAbstractStringLiteral
,SqlIntervalLiteral
,SqlNumericLiteral
public class SqlLiteral extends SqlNode
ASqlLiteral
is a constant. It is, appropriately, immutable.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 asSqlNode.toSqlString(SqlDialect)
andtoValue()
.If you really need to access the value directly, you should switch on the value of the
typeName
field, rather than making assumptions about the runtime type of thevalue
.The allowable types and combinations are:
Allowable types for SqlLiteral TypeName Meaing 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
SqlTypeName.TIME
Time, for example TIME '18:37:42.567'
Calendar
SqlTypeName.TIMESTAMP
Timestamp, for example TIMESTAMP '1969-04-29 18:37:42.567'
Calendar
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
SqlTypeName.BINARY
Binary constant, for example X'ABC'
,X'7F'
. Note that strings with an odd number of hexits will later become values of the BIT datatype, because they have an incomplete number of bytes. But here, they are all binary constants, because that's how they were written. These constants are always BINARY, never VARBINARY.BitString
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 symbol, such as the LEADING flag in a call to the function TRIM([LEADING|TRAILING|BOTH] chars FROM string)
.An Enum
SqlTypeName.INTERVAL_YEAR
..SqlTypeName.INTERVAL_SECOND
Interval, for example INTERVAL '1:34' HOUR
.SqlIntervalLiteral.IntervalValue
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
SqlLiteral.SqlSymbol
Deprecated.
-
Field Summary
Fields Modifier and Type Field Description private SqlTypeName
typeName
The type with which this literal was declared.protected java.lang.Object
value
The value of this literal.-
Fields inherited from class org.apache.calcite.sql.SqlNode
EMPTY_ARRAY, pos
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
SqlLiteral(java.lang.Object value, SqlTypeName typeName, SqlParserPos pos)
Creates aSqlLiteral
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <R> R
accept(SqlVisitor<R> visitor)
Accepts a generic visitor.java.math.BigDecimal
bigDecimalValue()
Returns a numeric literal's value as aBigDecimal
.boolean
booleanValue()
Returns the value as a boolean.SqlLiteral
clone(SqlParserPos pos)
Clones a SqlNode with a different position.static SqlNumericLiteral
createApproxNumeric(java.lang.String s, SqlParserPos pos)
static SqlBinaryStringLiteral
createBinaryString(byte[] bytes, SqlParserPos pos)
Creates a literal like X'ABAB' from an array of bytes.static SqlBinaryStringLiteral
createBinaryString(java.lang.String s, SqlParserPos pos)
Creates a literal like X'ABAB'.static SqlLiteral
createBoolean(boolean b, SqlParserPos pos)
Creates a boolean literal.static SqlCharStringLiteral
createCharString(java.lang.String s, java.lang.String charSet, SqlParserPos pos)
Creates a string literal, with optional character-set.static SqlCharStringLiteral
createCharString(java.lang.String s, SqlParserPos pos)
Creates a string literal in the system character set.static SqlDateLiteral
createDate(java.util.Calendar calendar, SqlParserPos pos)
Deprecated.static SqlDateLiteral
createDate(DateString date, SqlParserPos pos)
static SqlNumericLiteral
createExactNumeric(java.lang.String s, SqlParserPos pos)
static SqlIntervalLiteral
createInterval(int sign, java.lang.String intervalStr, SqlIntervalQualifier intervalQualifier, SqlParserPos pos)
Creates an interval literal.static SqlNumericLiteral
createNegative(SqlNumericLiteral num, SqlParserPos pos)
static SqlLiteral
createNull(SqlParserPos pos)
Creates a NULL literal.static SqlLiteral
createSample(SqlSampleSpec sampleSpec, SqlParserPos pos)
Creates a literal which represents a sample specification.RelDataType
createSqlType(RelDataTypeFactory typeFactory)
static SqlLiteral
createSymbol(java.lang.Enum<?> o, SqlParserPos pos)
Creates a literal which represents a parser symbol, for example theTRAILING
keyword in the callTrim(TRAILING 'x' FROM 'Hello world!')
.static SqlTimeLiteral
createTime(java.util.Calendar calendar, int precision, SqlParserPos pos)
Deprecated.static SqlTimeLiteral
createTime(TimeString t, int precision, SqlParserPos pos)
static SqlTimestampLiteral
createTimestamp(java.util.Calendar calendar, int precision, SqlParserPos pos)
Deprecated.static SqlTimestampLiteral
createTimestamp(TimestampString ts, int precision, SqlParserPos pos)
static SqlLiteral
createUnknown(SqlParserPos pos)
boolean
equals(java.lang.Object obj)
boolean
equalsDeep(SqlNode node, Litmus litmus)
Returns whether this node is structurally equivalent to another node.SqlKind
getKind()
Returns the type of node this is, orSqlKind.OTHER
if it's nothing special.SqlMonotonicity
getMonotonicity(SqlValidatorScope scope)
Returns whether expression is always ascending, descending or constant.java.lang.String
getStringValue()
Deprecated.SqlTypeName
getTypeName()
java.lang.Object
getValue()
Returns the value of this literal.<T> T
getValueAs(java.lang.Class<T> clazz)
int
hashCode()
int
intValue(boolean exact)
Returns the integer value of this literal.long
longValue(boolean exact)
Returns the long value of this literal.static SqlSampleSpec
sampleValue(SqlNode node)
Extracts theSqlSampleSpec
value from a symbol literal.int
signum()
Deprecated.static java.lang.String
stringValue(SqlNode node)
Deprecated.Usevalue(SqlNode)
<E extends java.lang.Enum<E>>
EsymbolValue(java.lang.Class<E> class_)
Returns the value as a symbol.<E extends java.lang.Enum<E>>
EsymbolValue_()
Deprecated.java.lang.String
toValue()
For calc program builder - value may be different thanunparse(org.apache.calcite.sql.SqlWriter, int, int)
Typical values: Hello, world! 12.34 {null} 1969-04-29static SqlLiteral
unchain(SqlNode node)
Converts a chained string literals into regular literals; returns regular literals unchanged.SqlLiteral
unescapeUnicode(char unicodeEscapeChar)
Transforms this literal (which must be of type character) into a new one in which 4-digit Unicode escape sequences have been replaced with the corresponding Unicode characters.void
unparse(SqlWriter writer, int leftPrec, int rightPrec)
Writes a SQL representation of this node to a writer.void
validate(SqlValidator validator, SqlValidatorScope scope)
Validates this node.static java.lang.Comparable
value(SqlNode node)
Extracts the value from a literal.static boolean
valueMatchesType(java.lang.Object value, SqlTypeName typeName)
-
Methods inherited from class org.apache.calcite.sql.SqlNode
clone, clone, cloneArray, equalDeep, equalDeep, equalsDeep, findValidOptions, getParserPosition, isA, toSqlString, toSqlString, toString, validateExpr
-
-
-
-
Field Detail
-
typeName
private final SqlTypeName typeName
The type with which this literal was declared. This type is very approximate: the literal may have a different type once validated. For example, all numeric literals have a type name ofSqlTypeName.DECIMAL
, but on validation may becomeSqlTypeName.INTEGER
.
-
value
protected final java.lang.Object value
The value of this literal. The type of the value must be appropriate for the typeName, as defined by thevalueMatchesType(java.lang.Object, org.apache.calcite.sql.type.SqlTypeName)
method.
-
-
Constructor Detail
-
SqlLiteral
protected SqlLiteral(java.lang.Object value, SqlTypeName typeName, SqlParserPos pos)
Creates aSqlLiteral
.
-
-
Method Detail
-
getTypeName
public SqlTypeName getTypeName()
- Returns:
- value of
typeName
-
valueMatchesType
public static boolean valueMatchesType(java.lang.Object value, SqlTypeName typeName)
- Returns:
- whether value is appropriate for its type (we have rules about these things)
-
clone
public SqlLiteral clone(SqlParserPos pos)
Description copied from class:SqlNode
Clones a SqlNode with a different position.
-
getKind
public SqlKind getKind()
Description copied from class:SqlNode
Returns the type of node this is, orSqlKind.OTHER
if it's nothing special.- Overrides:
getKind
in classSqlNode
- Returns:
- a
SqlKind
value, never null - See Also:
SqlNode.isA(java.util.Set<org.apache.calcite.sql.SqlKind>)
-
getValue
public java.lang.Object getValue()
Returns the value of this literal.Try not to use this method! There are so many different kinds of values, it's better to to let SqlLiteral do whatever it is you want to do.
- See Also:
booleanValue()
,symbolValue(Class)
-
getValueAs
public <T> T getValueAs(java.lang.Class<T> clazz)
-
symbolValue_
@Deprecated public <E extends java.lang.Enum<E>> E symbolValue_()
Deprecated.Returns the value as a symbol.
-
symbolValue
public <E extends java.lang.Enum<E>> E symbolValue(java.lang.Class<E> class_)
Returns the value as a symbol.
-
booleanValue
public boolean booleanValue()
Returns the value as a boolean.
-
sampleValue
public static SqlSampleSpec sampleValue(SqlNode node)
Extracts theSqlSampleSpec
value from a symbol literal.- Throws:
java.lang.ClassCastException
- if the value is not a symbol literal- See Also:
createSymbol(Enum, SqlParserPos)
-
value
public static java.lang.Comparable value(SqlNode node) throws java.lang.IllegalArgumentException
Extracts the value from a literal.Cases:
- If the node is a character literal, a chain of string
literals, or a CAST of a character literal, returns the value as a
NlsString
. - If the node is a numeric literal, or a negated numeric literal,
returns the value as a
BigDecimal
. - If the node is a
SqlIntervalQualifier
, returns itsTimeUnitRange
. - If the node is INTERVAL_DAY_TIME_ in
SqlTypeFamily
, returns its sign multiplied by its millisecond equivalent value - If the node is INTERVAL_YEAR_MONTH_ in
SqlTypeFamily
, returns its sign multiplied by its months equivalent value - Otherwise throws
IllegalArgumentException
.
- Throws:
java.lang.IllegalArgumentException
- If the node is a character literal, a chain of string
literals, or a CAST of a character literal, returns the value as a
-
stringValue
@Deprecated public static java.lang.String stringValue(SqlNode node)
Deprecated.Usevalue(SqlNode)
Extracts the string value from a string literal, a chain of string literals, or a CAST of a string literal.
-
unchain
public static SqlLiteral unchain(SqlNode node)
Converts a chained string literals into regular literals; returns regular literals unchanged.- Throws:
java.lang.IllegalArgumentException
- ifnode
is not a string literal and cannot be unchained.
-
toValue
public java.lang.String toValue()
For calc program builder - value may be different thanunparse(org.apache.calcite.sql.SqlWriter, int, int)
Typical values:- Hello, world!
- 12.34
- {null}
- 1969-04-29
- Returns:
- string representation of the value
-
validate
public void validate(SqlValidator validator, SqlValidatorScope scope)
Description copied from class:SqlNode
Validates this node.The typical implementation of this method will make a callback to the validator appropriate to the node type and context. The validator has methods such as
SqlValidator.validateLiteral(org.apache.calcite.sql.SqlLiteral)
for these purposes.
-
accept
public <R> R accept(SqlVisitor<R> visitor)
Description copied from class:SqlNode
Accepts a generic visitor.Implementations of this method in subtypes simply call the appropriate
visit
method on thevisitor object
.The type parameter
R
must be consistent with the type parameter of the visitor.
-
equalsDeep
public boolean equalsDeep(SqlNode node, Litmus litmus)
Description copied from class:SqlNode
Returns whether this node is structurally equivalent to another node. Some examples:- 1 + 2 is structurally equivalent to 1 + 2
- 1 + 2 + 3 is structurally equivalent to (1 + 2) + 3, but not to 1 + (2 + 3), because the '+' operator is left-associative
- Specified by:
equalsDeep
in classSqlNode
-
getMonotonicity
public SqlMonotonicity getMonotonicity(SqlValidatorScope scope)
Description copied from class:SqlNode
Returns whether expression is always ascending, descending or constant. This property is useful because it allows to safely aggregate infinite streams of values.The default implementation returns
SqlMonotonicity.NOT_MONOTONIC
.- Overrides:
getMonotonicity
in classSqlNode
- Parameters:
scope
- Scope
-
createNull
public static SqlLiteral createNull(SqlParserPos pos)
Creates a NULL literal.There's no singleton constant for a NULL literal. Instead, nulls must be instantiated via createNull(), because different instances have different context-dependent types.
-
createBoolean
public static SqlLiteral createBoolean(boolean b, SqlParserPos pos)
Creates a boolean literal.
-
createUnknown
public static SqlLiteral createUnknown(SqlParserPos pos)
-
createSymbol
public static SqlLiteral createSymbol(java.lang.Enum<?> o, SqlParserPos pos)
Creates a literal which represents a parser symbol, for example theTRAILING
keyword in the callTrim(TRAILING 'x' FROM 'Hello world!')
.- See Also:
symbolValue(Class)
-
createSample
public static SqlLiteral createSample(SqlSampleSpec sampleSpec, SqlParserPos pos)
Creates a literal which represents a sample specification.
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
intValue
public int intValue(boolean exact)
Returns the integer value of this literal.- Parameters:
exact
- Whether the value has to be exact. If true, and the literal is a fraction (e.g. 3.14), throws. If false, discards the fractional part of the value.- Returns:
- Integer value of this literal
-
longValue
public long longValue(boolean exact)
Returns the long value of this literal.- Parameters:
exact
- Whether the value has to be exact. If true, and the literal is a fraction (e.g. 3.14), throws. If false, discards the fractional part of the value.- Returns:
- Long value of this literal
-
signum
@Deprecated public int signum()
Deprecated.Returns sign of value.- Returns:
- -1, 0 or 1
-
bigDecimalValue
public java.math.BigDecimal bigDecimalValue()
Returns a numeric literal's value as aBigDecimal
.
-
getStringValue
@Deprecated public java.lang.String getStringValue()
Deprecated.
-
unparse
public void unparse(SqlWriter writer, int leftPrec, int rightPrec)
Description copied from class:SqlNode
Writes a SQL representation of this node to a writer.The
leftPrec
andrightPrec
parameters give us enough context to decide whether we need to enclose the expression in parentheses. For example, we need parentheses around "2 + 3" if preceded by "5 *". This is because the precedence of the "*" operator is greater than the precedence of the "+" operator.The algorithm handles left- and right-associative operators by giving them slightly different left- and right-precedence.
If
SqlWriter.isAlwaysUseParentheses()
is true, we use parentheses even when they are not required by the precedence rules.For the details of this algorithm, see
SqlCall.unparse(org.apache.calcite.sql.SqlWriter, int, int)
.
-
createSqlType
public RelDataType createSqlType(RelDataTypeFactory typeFactory)
-
createDate
@Deprecated public static SqlDateLiteral createDate(java.util.Calendar calendar, SqlParserPos pos)
Deprecated.
-
createDate
public static SqlDateLiteral createDate(DateString date, SqlParserPos pos)
-
createTimestamp
@Deprecated public static SqlTimestampLiteral createTimestamp(java.util.Calendar calendar, int precision, SqlParserPos pos)
Deprecated.
-
createTimestamp
public static SqlTimestampLiteral createTimestamp(TimestampString ts, int precision, SqlParserPos pos)
-
createTime
@Deprecated public static SqlTimeLiteral createTime(java.util.Calendar calendar, int precision, SqlParserPos pos)
Deprecated.
-
createTime
public static SqlTimeLiteral createTime(TimeString t, int precision, SqlParserPos pos)
-
createInterval
public static SqlIntervalLiteral createInterval(int sign, java.lang.String intervalStr, SqlIntervalQualifier intervalQualifier, SqlParserPos pos)
Creates an interval literal.- Parameters:
intervalStr
- input string of '1:23:04'intervalQualifier
- describes the interval type and precisionpos
- Parser position
-
createNegative
public static SqlNumericLiteral createNegative(SqlNumericLiteral num, SqlParserPos pos)
-
createExactNumeric
public static SqlNumericLiteral createExactNumeric(java.lang.String s, SqlParserPos pos)
-
createApproxNumeric
public static SqlNumericLiteral createApproxNumeric(java.lang.String s, SqlParserPos pos)
-
createBinaryString
public static SqlBinaryStringLiteral createBinaryString(java.lang.String s, SqlParserPos pos)
Creates a literal like X'ABAB'. Although it matters when we derive a type for this beastie, we don't care at this point whether the number of hexits is odd or even.
-
createBinaryString
public static SqlBinaryStringLiteral createBinaryString(byte[] bytes, SqlParserPos pos)
Creates a literal like X'ABAB' from an array of bytes.- Parameters:
bytes
- Contents of binary literalpos
- Parser position- Returns:
- Binary string literal
-
createCharString
public static SqlCharStringLiteral createCharString(java.lang.String s, SqlParserPos pos)
Creates a string literal in the system character set.- Parameters:
s
- a string (without the sql single quotes)pos
- Parser position
-
createCharString
public static SqlCharStringLiteral createCharString(java.lang.String s, java.lang.String charSet, SqlParserPos pos)
Creates a string literal, with optional character-set.- Parameters:
s
- a string (without the sql single quotes)charSet
- character set name, null means take system defaultpos
- Parser position- Returns:
- A string literal
- Throws:
java.nio.charset.UnsupportedCharsetException
- if charSet is not null but there is no character set with that name in this environment
-
unescapeUnicode
public SqlLiteral unescapeUnicode(char unicodeEscapeChar)
Transforms this literal (which must be of type character) into a new one in which 4-digit Unicode escape sequences have been replaced with the corresponding Unicode characters.- Parameters:
unicodeEscapeChar
- escape character (e.g. backslash) for Unicode numeric sequences; 0 implies no transformation- Returns:
- transformed literal
-
-