org.eigenbase.sql.type
Class SqlTypeUtil

java.lang.Object
  extended by org.eigenbase.sql.type.SqlTypeUtil

public abstract class SqlTypeUtil
extends Object

Contains utility methods used during SQL validation or type derivation.

Since:
Sep 3, 2004
Version:
$Id: //open/dev/farrago/src/org/eigenbase/sql/type/SqlTypeUtil.java#43 $
Author:
Wael Chatila

Nested Class Summary
static class SqlTypeUtil.TypeBuilder
          Convenience class for building a struct type with several fields.
 
Constructor Summary
SqlTypeUtil()
           
 
Method Summary
static RelDataType addCharsetAndCollation(RelDataType type, RelDataTypeFactory typeFactory)
          Adds collation and charset to a character type, returns other types unchanged.
static RelDataType addField(RelDataTypeFactory typeFactory, RelDataType type, int at, String fieldName, RelDataType fieldType)
          Adds a field to a record type at a specified position.
static boolean areCharacterSetsMismatched(RelDataType t1, RelDataType t2)
          Determines whether two types both have different character sets.
static boolean canAssignFrom(RelDataType toType, RelDataType fromType)
          Tests assignability of a value to a site.
static boolean canCastFrom(RelDataType toType, RelDataType fromType, boolean coerce)
          Compares two types and returns true if fromType can be cast to toType.
static RelDataType[] collectTypes(RelNode[] rels)
          Collects the row types of an array of relational expressions.
static boolean containsNullable(RelDataType type)
          Determines whether a type or any of its fields (if a structured type) are nullable.
static boolean containsNullable(RelDataType[] types)
          Returns whether one or more of an array of types is nullable.
static SqlDataTypeSpec convertTypeToSpec(RelDataType type)
          Converts an instance of RelDataType to an instance of SqlDataTypeSpec.
static RelDataType createEmptyStructType(RelDataTypeFactory typeFactory)
          Records a struct type with no fields.
static RelDataType createMultisetType(RelDataTypeFactory typeFactory, RelDataType type, boolean nullable)
           
static RelDataType createStructType(RelDataTypeFactory typeFactory, RelDataType[] types)
          Creates a record type with anonymous field names.
static RelDataType[] deriveAndCollectTypes(SqlValidator validator, SqlValidatorScope scope, SqlNode[] operands)
          Iterates over all operands, derives their types, and collects them into an array.
static boolean equalSansNullability(RelDataTypeFactory factory, RelDataType type1, RelDataType type2)
          Returns whether two types are equal, ignoring nullability.
static int findField(RelDataType type, String fieldName)
          Returns the ordinal of a given field in a record type, or -1 if the field is not found.
private static boolean flattenFields(RelDataTypeFactory typeFactory, RelDataType type, List<RelDataTypeField> list, int[] flatteningMap)
           
static RelDataType flattenRecordType(RelDataTypeFactory typeFactory, RelDataType recordType, int[] flatteningMap)
          Flattens a record type by recursively expanding any fields which are themselves record types.
static String[] getFieldNames(RelDataType type)
           
static RelDataType[] getFieldTypes(RelDataType type)
           
static int getMaxByteSize(RelDataType type)
          Computes the maximum number of bytes required to represent a value of a type having user-defined precision.
static long getMaxValue(RelDataType type)
          Determines the maximum unscaled value of a numeric type
static long getMinValue(RelDataType type)
          Determines the minimum unscaled value of a numeric type
static String getNumericJavaClassName(RelDataType type)
           
static String getPrimitiveWrapperJavaClassName(RelDataType type)
           
static boolean inBooleanFamily(RelDataType type)
           
static boolean inCharFamily(RelDataType type)
           
static boolean inCharFamily(SqlTypeName typeName)
           
static boolean inCharOrBinaryFamilies(RelDataType type)
           
static boolean inSameFamily(RelDataType t1, RelDataType t2)
           
static boolean inSameFamilyOrNull(RelDataType t1, RelDataType t2)
           
static boolean isApproximateNumeric(RelDataType type)
           
static boolean isBigint(RelDataType type)
           
static boolean isBoundedVariableWidth(RelDataType type)
           
static boolean isCharTypeComparable(RelDataType[] argTypes)
          Checks whether two types or more are char comparable.
static boolean isCharTypeComparable(RelDataType[] argTypes, int start, int stop)
           
static boolean isCharTypeComparable(SqlCallBinding binding, SqlNode[] operands, boolean throwOnFailure)
          Returns whether the operands to a call are char type-comparable.
static boolean isComparable(RelDataType type1, RelDataType type2)
          Returns whether two types are comparable.
static boolean isDatetime(RelDataType type)
           
static boolean isDecimal(RelDataType type)
           
static boolean isExactNumeric(RelDataType type)
           
static boolean isInterval(RelDataType type)
           
static boolean isIntType(RelDataType type)
           
static boolean isJavaPrimitive(RelDataType type)
           
static boolean isLob(RelDataType type)
           
static boolean isNumeric(RelDataType type)
           
static boolean isOfSameTypeName(SqlTypeName[] typeNames, RelDataType type)
          Returns true if any element in typeNames matches type.getSqlTypeName().
static boolean isOfSameTypeName(SqlTypeName typeName, RelDataType type)
          Returns typeName.equals(type.getSqlTypeName()).
static boolean isUnicode(RelDataType type)
          Checks whether a type represents Unicode character data.
static RelDataType makeNullableIfOperandsAre(RelDataTypeFactory typeFactory, RelDataType[] argTypes, RelDataType type)
          Recreates a given RelDataType with nullability iff any of the param argTypes are nullable.
static RelDataType makeNullableIfOperandsAre(SqlValidator validator, SqlValidatorScope scope, SqlCall call, RelDataType type)
          Recreates a given RelDataType with nullablility iff any of a calls operand types are nullable.
static long maxValue(RelDataType type)
          Returns the maximum value of an integral type, as a long value
static boolean needsNullIndicator(RelDataType recordType)
           
static RelDataType promoteToRowType(RelDataTypeFactory typeFactory, RelDataType type, String fieldName)
          Promotes a type to a row type (does nothing if it already is one).
static boolean sameNamedType(RelDataType t1, RelDataType t2)
          Tests whether two types have the same name and structure, possibly with differing modifiers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlTypeUtil

public SqlTypeUtil()
Method Detail

isCharTypeComparable

public static boolean isCharTypeComparable(RelDataType[] argTypes)
Checks whether two types or more are char comparable.

Returns:
Returns true if all operands are of char type and if they are comparable, i.e. of the same charset and collation of same charset
"Precondition:"
argTypes != null, argTypes.length >= 2

isCharTypeComparable

public static boolean isCharTypeComparable(RelDataType[] argTypes,
                                           int start,
                                           int stop)
Parameters:
start - zero based index
stop - zero based index

isCharTypeComparable

public static boolean isCharTypeComparable(SqlCallBinding binding,
                                           SqlNode[] operands,
                                           boolean throwOnFailure)
Returns whether the operands to a call are char type-comparable.

Parameters:
binding - Binding of call to operands
operands - Operands to check for compatibility; usually the operands of the bound call, but not always
throwOnFailure - Whether to throw an exception on failure
Returns:
whether operands are valid
"Precondition:"
null != operands, 2 <= operands.length

deriveAndCollectTypes

public static RelDataType[] deriveAndCollectTypes(SqlValidator validator,
                                                  SqlValidatorScope scope,
                                                  SqlNode[] operands)
Iterates over all operands, derives their types, and collects them into an array.


collectTypes

public static RelDataType[] collectTypes(RelNode[] rels)
Collects the row types of an array of relational expressions.

Parameters:
rels - array of relational expressions
Returns:
array of row types

promoteToRowType

public static RelDataType promoteToRowType(RelDataTypeFactory typeFactory,
                                           RelDataType type,
                                           String fieldName)
Promotes a type to a row type (does nothing if it already is one).

Parameters:
type - type to be promoted
fieldName - name to give field in row type; null for default of "ROW_VALUE"
Returns:
row type

makeNullableIfOperandsAre

public static RelDataType makeNullableIfOperandsAre(SqlValidator validator,
                                                    SqlValidatorScope scope,
                                                    SqlCall call,
                                                    RelDataType type)
Recreates a given RelDataType with nullablility iff any of a calls operand types are nullable.


makeNullableIfOperandsAre

public static RelDataType makeNullableIfOperandsAre(RelDataTypeFactory typeFactory,
                                                    RelDataType[] argTypes,
                                                    RelDataType type)
Recreates a given RelDataType with nullability iff any of the param argTypes are nullable.


containsNullable

public static boolean containsNullable(RelDataType[] types)
Returns whether one or more of an array of types is nullable.


containsNullable

public static boolean containsNullable(RelDataType type)
Determines whether a type or any of its fields (if a structured type) are nullable.


isOfSameTypeName

public static boolean isOfSameTypeName(SqlTypeName typeName,
                                       RelDataType type)
Returns typeName.equals(type.getSqlTypeName()). If typeName.equals(SqlTypeName.Any) true is always returned.


isOfSameTypeName

public static boolean isOfSameTypeName(SqlTypeName[] typeNames,
                                       RelDataType type)
Returns true if any element in typeNames matches type.getSqlTypeName().

See Also:
isOfSameTypeName(SqlTypeName, RelDataType)

isDatetime

public static boolean isDatetime(RelDataType type)
Returns:
true if type is DATE, TIME, or TIMESTAMP

isInterval

public static boolean isInterval(RelDataType type)
Returns:
true if type is some kind of INTERVAL

inCharFamily

public static boolean inCharFamily(RelDataType type)
Returns:
true if type is in SqlTypeFamily.Character

inCharFamily

public static boolean inCharFamily(SqlTypeName typeName)
Returns:
true if type is in SqlTypeFamily.Character

inBooleanFamily

public static boolean inBooleanFamily(RelDataType type)
Returns:
true if type is in SqlTypeFamily.Boolean

inSameFamily

public static boolean inSameFamily(RelDataType t1,
                                   RelDataType t2)
Returns:
true if two types are in same type family

inSameFamilyOrNull

public static boolean inSameFamilyOrNull(RelDataType t1,
                                         RelDataType t2)
Returns:
true if two types are in same type family, or one or the other is of type SqlTypeName.Null

inCharOrBinaryFamilies

public static boolean inCharOrBinaryFamilies(RelDataType type)
Returns:
true if type family is either character or binary

isLob

public static boolean isLob(RelDataType type)
Returns:
true if type is a LOB of some kind

isBoundedVariableWidth

public static boolean isBoundedVariableWidth(RelDataType type)
Returns:
true if type is variable width with bounded precision

isIntType

public static boolean isIntType(RelDataType type)
Returns:
true if type is one of the integer types

isDecimal

public static boolean isDecimal(RelDataType type)
Returns:
true if type is decimal

isBigint

public static boolean isBigint(RelDataType type)
Returns:
true if type is bigint

isExactNumeric

public static boolean isExactNumeric(RelDataType type)
Returns:
true if type is numeric with exact precision

maxValue

public static long maxValue(RelDataType type)
Returns the maximum value of an integral type, as a long value


isApproximateNumeric

public static boolean isApproximateNumeric(RelDataType type)
Returns:
true if type is numeric with approximate precision

isNumeric

public static boolean isNumeric(RelDataType type)
Returns:
true if type is numeric

sameNamedType

public static boolean sameNamedType(RelDataType t1,
                                    RelDataType t2)
Tests whether two types have the same name and structure, possibly with differing modifiers. For example, VARCHAR(1) and VARCHAR(10) are considered the same, while VARCHAR(1) and CHAR(1) are considered different. Likewise, VARCHAR(1) MULTISET and VARCHAR(10) MULTISET are considered the same.

Returns:
true if types have same name and structure

getMaxByteSize

public static int getMaxByteSize(RelDataType type)
Computes the maximum number of bytes required to represent a value of a type having user-defined precision. This computation assumes no overhead such as length indicators and NUL-terminators. Complex types for which multiple representations are possible (e.g. DECIMAL or TIMESTAMP) return 0.

Parameters:
type - type for which to compute storage
Returns:
maximum bytes, or 0 for a fixed-width type or type with unknown maximum

getMinValue

public static long getMinValue(RelDataType type)
Determines the minimum unscaled value of a numeric type

Parameters:
type - a numeric type

getMaxValue

public static long getMaxValue(RelDataType type)
Determines the maximum unscaled value of a numeric type

Parameters:
type - a numeric type

isJavaPrimitive

public static boolean isJavaPrimitive(RelDataType type)
Returns:
true if type has a representation as a Java primitive (ignoring nullability)

getPrimitiveWrapperJavaClassName

public static String getPrimitiveWrapperJavaClassName(RelDataType type)
Returns:
class name of the wrapper for the primitive data type.

getNumericJavaClassName

public static String getNumericJavaClassName(RelDataType type)
Returns:
class name of the numeric data type.

canAssignFrom

public static boolean canAssignFrom(RelDataType toType,
                                    RelDataType fromType)
Tests assignability of a value to a site.

Parameters:
toType - type of the target site
fromType - type of the source value
Returns:
true iff assignable

areCharacterSetsMismatched

public static boolean areCharacterSetsMismatched(RelDataType t1,
                                                 RelDataType t2)
Determines whether two types both have different character sets. If one or the other type has no character set (e.g. in cast from INT to VARCHAR), that is not a mismatch.

Parameters:
t1 - first type
t2 - second type
Returns:
true iff mismatched

canCastFrom

public static boolean canCastFrom(RelDataType toType,
                                  RelDataType fromType,
                                  boolean coerce)
Compares two types and returns true if fromType can be cast to toType.

REVIEW jvs 17-Dec-2004: the coerce param below shouldn't really be necessary. We're using it as a hack because SqlTypeFactoryImpl.leastRestrictiveSqlType(org.eigenbase.reltype.RelDataType[]) isn't complete enough yet. Once it is, this param (and the non-coerce rules of SqlTypeAssignmentRules) should go away.

Parameters:
toType - target of assignment
fromType - source of assignment
coerce - if true, the SQL rules for CAST are used; if false, the rules are similar to Java; e.g. you can't assign short x = (int) y, and you can't assign int x = (String) z.
Returns:
true iff cast is legal

getFieldNames

public static String[] getFieldNames(RelDataType type)
Returns:
the field names of a struct type

getFieldTypes

public static RelDataType[] getFieldTypes(RelDataType type)
Returns:
the field types of a struct type

flattenRecordType

public static RelDataType flattenRecordType(RelDataTypeFactory typeFactory,
                                            RelDataType recordType,
                                            int[] flatteningMap)
Flattens a record type by recursively expanding any fields which are themselves record types. For each record type, a representative null value field is also prepended (with state NULL for a null value and FALSE for non-null), and all component types are asserted to be nullable, since SQL doesn't allow NOT NULL to be specified on attributes.

Parameters:
typeFactory - factory which should produced flattened type
recordType - type with possible nesting
flatteningMap - if non-null, receives map from unflattened ordinal to flattened ordinal (must have length at least recordType.getFieldList().size())
Returns:
flattened equivalent

createStructType

public static RelDataType createStructType(RelDataTypeFactory typeFactory,
                                           RelDataType[] types)
Creates a record type with anonymous field names.


needsNullIndicator

public static boolean needsNullIndicator(RelDataType recordType)

flattenFields

private static boolean flattenFields(RelDataTypeFactory typeFactory,
                                     RelDataType type,
                                     List<RelDataTypeField> list,
                                     int[] flatteningMap)

convertTypeToSpec

public static SqlDataTypeSpec convertTypeToSpec(RelDataType type)
Converts an instance of RelDataType to an instance of SqlDataTypeSpec.

Parameters:
type - type descriptor
Returns:
corresponding parse representation

createMultisetType

public static RelDataType createMultisetType(RelDataTypeFactory typeFactory,
                                             RelDataType type,
                                             boolean nullable)

addCharsetAndCollation

public static RelDataType addCharsetAndCollation(RelDataType type,
                                                 RelDataTypeFactory typeFactory)
Adds collation and charset to a character type, returns other types unchanged.

Parameters:
type - Type
typeFactory - Type factory
Returns:
Type with added charset and collation, or unchanged type if it is not a char type.

equalSansNullability

public static boolean equalSansNullability(RelDataTypeFactory factory,
                                           RelDataType type1,
                                           RelDataType type2)
Returns whether two types are equal, ignoring nullability.

They need not come from the same factory.

Parameters:
factory - Type factory
type1 - First type
type2 - Second type
Returns:
whether types are equal, ignoring nullability

addField

public static RelDataType addField(RelDataTypeFactory typeFactory,
                                   RelDataType type,
                                   int at,
                                   String fieldName,
                                   RelDataType fieldType)
Adds a field to a record type at a specified position.

For example, if type is (A integer, B boolean), and fieldType is varchar(10), then prepend(typeFactory, type, 0, "Z", fieldType) will return (Z varchar(10), A integer, B boolean).

Parameters:
typeFactory - Type factory
type - Record type
at - Ordinal to add field
fieldName - Name of new field
fieldType - Type of new field
Returns:
Extended record type

findField

public static int findField(RelDataType type,
                            String fieldName)
Returns the ordinal of a given field in a record type, or -1 if the field is not found.

Parameters:
type - Record type
fieldName - Name of field
Returns:
Ordinal of field

createEmptyStructType

public static RelDataType createEmptyStructType(RelDataTypeFactory typeFactory)
Records a struct type with no fields.

Parameters:
typeFactory - Type factory
Returns:
Struct type with no fields

isComparable

public static boolean isComparable(RelDataType type1,
                                   RelDataType type2)
Returns whether two types are comparable. They need to be scalar types of the same family, or struct types whose fields are pairwise comparable.

Parameters:
type1 - First type
type2 - Second type
Returns:
Whether types are comparable

isUnicode

public static boolean isUnicode(RelDataType type)
Checks whether a type represents Unicode character data.

Parameters:
type - type to test
Returns:
whether type represents Unicode character data