Class 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.

    we support complex type expressions like:

    ROW(
    foo NUMBER(5, 2) NOT NULL,
    rec ROW(b BOOLEAN, i MyUDT NOT NULL))

    Internally we use SqlRowTypeNameSpec to specify row data type name.

    We support simple data types like CHAR, VARCHAR and DOUBLE, with optional precision and scale.

    Internally we use SqlBasicTypeNameSpec to specify basic sql data type name.

    • Field Detail

      • 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 as x.

    • Constructor Detail

      • SqlDataTypeSpec

        public SqlDataTypeSpec​(SqlTypeNameSpec typeNameSpec,
                               SqlParserPos pos)
        Creates a type specification representing a type.
        Parameters:
        typeNameSpec - The type name can be basic sql type, row type, collections type and user defined type.
      • SqlDataTypeSpec

        public SqlDataTypeSpec​(SqlTypeNameSpec typeNameSpec,
                               java.util.TimeZone timeZone,
                               SqlParserPos pos)
        Creates a type specification representing a type, with time zone specified.
        Parameters:
        typeNameSpec - The type name can be basic sql type, row type, collections type and user defined type.
        timeZone - Specified time zone.
      • SqlDataTypeSpec

        public SqlDataTypeSpec​(SqlTypeNameSpec typeNameSpec,
                               java.util.TimeZone timeZone,
                               java.lang.Boolean nullable,
                               SqlParserPos pos)
        Creates a type specification representing a type, with time zone and nullability specified.
        Parameters:
        typeNameSpec - The type name can be basic sql type, row type, collections type and user defined type.
        timeZone - Specified time zone.
        nullable - The nullability.
      • SqlDataTypeSpec

        public SqlDataTypeSpec​(SqlTypeNameSpec typeNameSpec,
                               SqlTypeNameSpec baseTypeName,
                               java.util.TimeZone timeZone,
                               java.lang.Boolean nullable,
                               SqlParserPos pos)
        Creates a type specification representing a type, with time zone, nullability and base type name specified.
        Parameters:
        typeNameSpec - The type name can be basic sql type, row type, collections type and user defined type.
        baseTypeName - The base type name.
        timeZone - Specified time zone.
        nullable - The nullability.
    • Method Detail

      • getCollectionsTypeName

        public SqlIdentifier getCollectionsTypeName()
      • 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 are ARRAY and MULTISET.
      • 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 and rightPrec 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).

        Specified by:
        unparse in class SqlNode
        Parameters:
        writer - Target writer
        leftPrec - The precedence of the SqlNode immediately preceding this node in a depth-first scan of the parse tree
        rightPrec - The precedence of the SqlNode immediately
      • 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 the visitor object.

        The type parameter R must be consistent with the type parameter of the visitor.

        Specified by:
        accept in class SqlNode
      • 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 class SqlNode
      • deriveType

        public RelDataType deriveType​(SqlValidator validator,
                                      boolean nullable)
        Converts this type specification to a RelDataType.

        Throws an error if the type is not found.

        Parameters:
        nullable - Whether the type is nullable if the type specification does not explicitly state.
      • fixUpNullability

        private RelDataType fixUpNullability​(RelDataTypeFactory typeFactory,
                                             RelDataType type,
                                             boolean nullable)
        Fix up the nullability of the type.
        Parameters:
        typeFactory - Type factory.
        type - The type to coerce nullability.
        nullable - Default nullability to use if this type specification does not specify nullability.
        Returns:
        type with specified nullability or the default.