Class SqlDataTypeSpec
- java.lang.Object
-
- org.apache.calcite.sql.SqlNode
-
- org.apache.calcite.sql.SqlDataTypeSpec
-
- All Implemented Interfaces:
java.lang.Cloneable
public class SqlDataTypeSpec extends SqlNode
Represents a SQL data type specification in a parse tree.A
SqlDataTypeSpec
is immutable; once created, you cannot change any of the fields.todo: This should really be a subtype of
SqlCall
.In its full glory, we will have to support complex type expressions like:
ROW(
NUMBER(5, 2) NOT NULL AS foo,
ROW(BOOLEAN AS b, MyUDT NOT NULL AS i) AS rec)Currently it only supports simple datatypes like CHAR, VARCHAR and DOUBLE, with optional precision and scale.
-
-
Field Summary
Fields Modifier and Type Field Description private SqlIdentifier
baseTypeName
private java.lang.String
charSetName
private SqlIdentifier
collectionsTypeName
private java.lang.Boolean
nullable
Whether data type is allows nulls.private int
precision
private int
scale
private java.util.TimeZone
timeZone
private SqlIdentifier
typeName
-
Fields inherited from class org.apache.calcite.sql.SqlNode
EMPTY_ARRAY, pos
-
-
Constructor Summary
Constructors Constructor Description SqlDataTypeSpec(SqlIdentifier typeName, int precision, int scale, java.lang.String charSetName, java.util.TimeZone timeZone, SqlParserPos pos)
Creates a type specification representing a regular, non-collection type.SqlDataTypeSpec(SqlIdentifier collectionsTypeName, SqlIdentifier typeName, int precision, int scale, java.lang.String charSetName, java.util.TimeZone timeZone, java.lang.Boolean nullable, SqlParserPos pos)
Creates a type specification that has no base type.SqlDataTypeSpec(SqlIdentifier collectionsTypeName, SqlIdentifier typeName, int precision, int scale, java.lang.String charSetName, SqlParserPos pos)
Creates a type specification representing a collection type.SqlDataTypeSpec(SqlIdentifier collectionsTypeName, SqlIdentifier typeName, SqlIdentifier baseTypeName, int precision, int scale, java.lang.String charSetName, java.util.TimeZone timeZone, java.lang.Boolean nullable, SqlParserPos pos)
Creates a type specification.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <R> R
accept(SqlVisitor<R> visitor)
Accepts a generic visitor.SqlNode
clone(SqlParserPos pos)
Clones a SqlNode with a different position.RelDataType
deriveType(RelDataTypeFactory typeFactory)
Does not throw an error if the type is not built-in.RelDataType
deriveType(RelDataTypeFactory typeFactory, boolean nullable)
Converts this type specification to aRelDataType
.RelDataType
deriveType(SqlValidator validator)
Throws an error if the type is not found.boolean
equalsDeep(SqlNode node, Litmus litmus)
Returns whether this node is structurally equivalent to another node.java.lang.String
getCharSetName()
SqlIdentifier
getCollectionsTypeName()
SqlDataTypeSpec
getComponentTypeSpec()
Returns a new SqlDataTypeSpec corresponding to the component type if the type spec is a collections type spec.
Collection types areARRAY
andMULTISET
.SqlMonotonicity
getMonotonicity(SqlValidatorScope scope)
Returns whether expression is always ascending, descending or constant.java.lang.Boolean
getNullable()
int
getPrecision()
int
getScale()
java.util.TimeZone
getTimeZone()
SqlIdentifier
getTypeName()
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.SqlDataTypeSpec
withNullable(java.lang.Boolean nullable)
Returns a copy of this data type specification with a given nullability.-
Methods inherited from class org.apache.calcite.sql.SqlNode
clone, clone, cloneArray, equalDeep, equalDeep, equalsDeep, findValidOptions, getKind, getParserPosition, isA, toSqlString, toSqlString, toString, validateExpr
-
-
-
-
Field Detail
-
collectionsTypeName
private final SqlIdentifier collectionsTypeName
-
typeName
private final SqlIdentifier typeName
-
baseTypeName
private final SqlIdentifier baseTypeName
-
scale
private final int scale
-
precision
private final int precision
-
charSetName
private final java.lang.String charSetName
-
timeZone
private final java.util.TimeZone timeZone
-
nullable
private java.lang.Boolean nullable
Whether data type is allows nulls.Nullable is nullable! Null means "not specified". E.g.
CAST(x AS INTEGER)
preserves has the same nullability asx
.
-
-
Constructor Detail
-
SqlDataTypeSpec
public SqlDataTypeSpec(SqlIdentifier typeName, int precision, int scale, java.lang.String charSetName, java.util.TimeZone timeZone, SqlParserPos pos)
Creates a type specification representing a regular, non-collection type.
-
SqlDataTypeSpec
public SqlDataTypeSpec(SqlIdentifier collectionsTypeName, SqlIdentifier typeName, int precision, int scale, java.lang.String charSetName, SqlParserPos pos)
Creates a type specification representing a collection type.
-
SqlDataTypeSpec
public SqlDataTypeSpec(SqlIdentifier collectionsTypeName, SqlIdentifier typeName, int precision, int scale, java.lang.String charSetName, java.util.TimeZone timeZone, java.lang.Boolean nullable, SqlParserPos pos)
Creates a type specification that has no base type.
-
SqlDataTypeSpec
public SqlDataTypeSpec(SqlIdentifier collectionsTypeName, SqlIdentifier typeName, SqlIdentifier baseTypeName, int precision, int scale, java.lang.String charSetName, java.util.TimeZone timeZone, java.lang.Boolean nullable, SqlParserPos pos)
Creates a type specification.
-
-
Method Detail
-
clone
public SqlNode clone(SqlParserPos pos)
Description copied from class:SqlNode
Clones a SqlNode with a different position.
-
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
-
getCollectionsTypeName
public SqlIdentifier getCollectionsTypeName()
-
getTypeName
public SqlIdentifier getTypeName()
-
getScale
public int getScale()
-
getPrecision
public int getPrecision()
-
getCharSetName
public java.lang.String getCharSetName()
-
getTimeZone
public java.util.TimeZone getTimeZone()
-
getNullable
public java.lang.Boolean getNullable()
-
withNullable
public SqlDataTypeSpec withNullable(java.lang.Boolean nullable)
Returns a copy of this data type specification with a given nullability.
-
getComponentTypeSpec
public SqlDataTypeSpec getComponentTypeSpec()
Returns a new SqlDataTypeSpec corresponding to the component type if the type spec is a collections type spec.
Collection types areARRAY
andMULTISET
.
-
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)
.
-
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
-
deriveType
public RelDataType deriveType(SqlValidator validator)
Throws an error if the type is not found.
-
deriveType
public RelDataType deriveType(RelDataTypeFactory typeFactory)
Does not throw an error if the type is not built-in.
-
deriveType
public RelDataType deriveType(RelDataTypeFactory typeFactory, boolean nullable)
Converts this type specification to aRelDataType
.Does not throw an error if the type is not built-in.
- Parameters:
nullable
- Whether the type is nullable if the type specification does not explicitly state
-
-