Package org.apache.calcite.sql.type
Class SqlTypeUtil
- java.lang.Object
-
- org.apache.calcite.sql.type.SqlTypeUtil
-
public abstract class SqlTypeUtil extends java.lang.Object
Contains utility methods used during SQL validation or type derivation.
-
-
Constructor Summary
Constructors Constructor Description SqlTypeUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static RelDataType
addCharsetAndCollation(RelDataType type, RelDataTypeFactory typeFactory)
Adds collation and charset to a character type, returns other types unchanged.static boolean
allNullable(java.util.List<RelDataType> types)
Returns whether all of array of types are nullable.static boolean
areCharacterSetsMismatched(RelDataType t1, RelDataType t2)
Determines whether two types both have different character sets.static boolean
areSameFamily(java.lang.Iterable<RelDataType> types)
Returns whether all types in a collection have the same family, as determined byisSameFamily(RelDataType, RelDataType)
.static boolean
canAssignFrom(RelDataType toType, RelDataType fromType)
Tests whether a value can be assigned 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.private static boolean
canConvertStringInCompare(RelDataTypeFamily family)
Returns whether a character data type can be implicitly converted to a given family in a compare operation.static int
comparePrecision(int p0, int p1)
Returns whether a precision is greater or equal than another, treatingRelDataType.PRECISION_NOT_SPECIFIED
as infinity.static boolean
containsNullable(java.util.List<RelDataType> types)
Returns whether one or more of an array of types is nullable.static boolean
containsNullable(RelDataType type)
Determines whether a type or any of its fields (if a structured type) are nullable.static SqlDataTypeSpec
convertTypeToSpec(RelDataType type)
Converts an instance of RelDataType to an instance of SqlDataTypeSpec.static RelDataType
createArrayType(RelDataTypeFactory typeFactory, RelDataType type, boolean nullable)
static RelDataType
createEmptyStructType(RelDataTypeFactory typeFactory)
Records a struct type with no fields.static RelDataType
createMapType(RelDataTypeFactory typeFactory, RelDataType keyType, RelDataType valueType, boolean nullable)
static RelDataType
createMultisetType(RelDataTypeFactory typeFactory, RelDataType type, boolean nullable)
static java.util.List<RelDataType>
deriveAndCollectTypes(SqlValidator validator, SqlValidatorScope scope, java.util.List<SqlNode> operands)
Iterates over all operands, derives their types, and collects them into a list.static boolean
equalSansNullability(RelDataTypeFactory factory, RelDataType type1, RelDataType type2)
Returns whether two types are equal, ignoring nullability.protected static RelDataTypeFamily
family(RelDataType type)
static int
findField(RelDataType type, java.lang.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, java.util.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 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 typestatic long
getMinValue(RelDataType type)
Determines the minimum unscaled value of a numeric typestatic java.lang.String
getNumericJavaClassName(RelDataType type)
Deprecated.static java.lang.String
getPrimitiveWrapperJavaClassName(RelDataType type)
Deprecated.static boolean
hasScale(RelDataType type)
Returns whether a type's scale is set.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)
private static boolean
isAny(RelDataType t)
static boolean
isApproximateNumeric(RelDataType type)
static boolean
isArray(RelDataType type)
static boolean
isBigint(RelDataType type)
static boolean
isBoundedVariableWidth(RelDataType type)
static boolean
isCharTypeComparable(java.util.List<RelDataType> argTypes)
Checks whether two types or more are char comparable.static boolean
isCharTypeComparable(SqlCallBinding binding, java.util.List<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
isFlat(RelDataType type)
Returns whether a type is flat.static boolean
isInterval(RelDataType type)
static boolean
isIntType(RelDataType type)
static boolean
isJavaPrimitive(RelDataType type)
Deprecated.static boolean
isLob(RelDataType type)
static boolean
isNumeric(RelDataType type)
static boolean
isOfSameTypeName(java.util.Collection<SqlTypeName> typeNames, RelDataType type)
Returns true if any element intypeNames
matches type.getSqlTypeName().static boolean
isOfSameTypeName(SqlTypeName typeName, RelDataType type)
Returns typeName.equals(type.getSqlTypeName()).private static boolean
isSameFamily(RelDataType type1, RelDataType type2)
Returns whether two types are scalar types of the same family, or struct types whose fields are pairwise of the same family.static boolean
isUnicode(RelDataType type)
Checks whether a type represents Unicode character data.static RelDataType
leastRestrictiveForComparison(RelDataTypeFactory typeFactory, RelDataType type1, RelDataType type2)
Returns the least restrictive type T, such that a value of type T can be compared with values of typetype0
andtype1
using=
.static RelDataType
makeNullableIfOperandsAre(RelDataTypeFactory typeFactory, java.util.List<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 nullability iff any of the operands of a call are nullable.static int
maxPrecision(int p0, int p1)
Returns the larger of two precisions, treatingRelDataType.PRECISION_NOT_SPECIFIED
as infinity.static long
maxValue(RelDataType type)
Returns the maximum value of an integral type, as a long valuestatic boolean
needsNullIndicator(RelDataType recordType)
static java.util.List<RelDataType>
projectTypes(RelDataType rowType, java.util.List<? extends java.lang.Number> requiredFields)
Selects data types of the specified fields from an input row type.static RelDataType
promoteToRowType(RelDataTypeFactory typeFactory, RelDataType type, java.lang.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.
-
-
-
Method Detail
-
isCharTypeComparable
public static boolean isCharTypeComparable(java.util.List<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
-
isCharTypeComparable
public static boolean isCharTypeComparable(SqlCallBinding binding, java.util.List<SqlNode> operands, boolean throwOnFailure)
Returns whether the operands to a call are char type-comparable.- Parameters:
binding
- Binding of call to operandsoperands
- Operands to check for compatibility; usually the operands of the bound call, but not alwaysthrowOnFailure
- Whether to throw an exception on failure- Returns:
- whether operands are valid
-
deriveAndCollectTypes
public static java.util.List<RelDataType> deriveAndCollectTypes(SqlValidator validator, SqlValidatorScope scope, java.util.List<SqlNode> operands)
Iterates over all operands, derives their types, and collects them into a list.
-
promoteToRowType
public static RelDataType promoteToRowType(RelDataTypeFactory typeFactory, RelDataType type, java.lang.String fieldName)
Promotes a type to a row type (does nothing if it already is one).- Parameters:
type
- type to be promotedfieldName
- 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 nullability iff any of the operands of a call are nullable.
-
makeNullableIfOperandsAre
public static RelDataType makeNullableIfOperandsAre(RelDataTypeFactory typeFactory, java.util.List<RelDataType> argTypes, RelDataType type)
Recreates a given RelDataType with nullability iff any of the param argTypes are nullable.
-
allNullable
public static boolean allNullable(java.util.List<RelDataType> types)
Returns whether all of array of types are nullable.
-
containsNullable
public static boolean containsNullable(java.util.List<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(java.util.Collection<SqlTypeName> typeNames, RelDataType type)
Returns true if any element intypeNames
matches type.getSqlTypeName().
-
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
-
hasScale
public static boolean hasScale(RelDataType type)
Returns whether a type's scale is set.
-
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
@Deprecated public static boolean isJavaPrimitive(RelDataType type)
Deprecated.- Returns:
- true if type has a representation as a Java primitive (ignoring nullability)
-
getPrimitiveWrapperJavaClassName
@Deprecated public static java.lang.String getPrimitiveWrapperJavaClassName(RelDataType type)
Deprecated.- Returns:
- class name of the wrapper for the primitive data type.
-
getNumericJavaClassName
@Deprecated public static java.lang.String getNumericJavaClassName(RelDataType type)
Deprecated.- Returns:
- class name of the numeric data type.
-
isAny
private static boolean isAny(RelDataType t)
-
canAssignFrom
public static boolean canAssignFrom(RelDataType toType, RelDataType fromType)
Tests whether a value can be assigned to a site.- Parameters:
toType
- type of the target sitefromType
- 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 typet2
- 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(java.util.List<org.apache.calcite.rel.type.RelDataType>)
isn't complete enough yet. Once it is, this param (and the non-coerce rules ofSqlTypeAssignmentRules
) should go away.- Parameters:
toType
- target of assignmentfromType
- source of assignmentcoerce
- 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
-
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 typerecordType
- type with possible nestingflatteningMap
- if non-null, receives map from unflattened ordinal to flattened ordinal (must have length at least recordType.getFieldList().size())- Returns:
- flattened equivalent
-
needsNullIndicator
public static boolean needsNullIndicator(RelDataType recordType)
-
flattenFields
private static boolean flattenFields(RelDataTypeFactory typeFactory, RelDataType type, java.util.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)
-
createArrayType
public static RelDataType createArrayType(RelDataTypeFactory typeFactory, RelDataType type, boolean nullable)
-
createMapType
public static RelDataType createMapType(RelDataTypeFactory typeFactory, RelDataType keyType, RelDataType valueType, 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
- TypetypeFactory
- 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 factorytype1
- First typetype2
- Second type- Returns:
- whether types are equal, ignoring nullability
-
findField
public static int findField(RelDataType type, java.lang.String fieldName)
Returns the ordinal of a given field in a record type, or -1 if the field is not found.- Parameters:
type
- Record typefieldName
- Name of field- Returns:
- Ordinal of field
-
projectTypes
public static java.util.List<RelDataType> projectTypes(RelDataType rowType, java.util.List<? extends java.lang.Number> requiredFields)
Selects data types of the specified fields from an input row type. This is useful when identifying data types of a function that is going to operate on inputs that are specified as field ordinals (e.g. aggregate calls).- Parameters:
rowType
- input row typerequiredFields
- ordinals of the projected fields- Returns:
- list of data types that are requested by requiredFields
-
createEmptyStructType
public static RelDataType createEmptyStructType(RelDataTypeFactory typeFactory)
Records a struct type with no fields.- Parameters:
typeFactory
- Type factory- Returns:
- Struct type with no fields
-
isFlat
public static boolean isFlat(RelDataType type)
Returns whether a type is flat. It is not flat if it is a record type that has one or more fields that are themselves record types.
-
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 typetype2
- Second type- Returns:
- Whether types are comparable
-
leastRestrictiveForComparison
public static RelDataType leastRestrictiveForComparison(RelDataTypeFactory typeFactory, RelDataType type1, RelDataType type2)
Returns the least restrictive type T, such that a value of type T can be compared with values of typetype0
andtype1
using=
.
-
family
protected static RelDataTypeFamily family(RelDataType type)
-
areSameFamily
public static boolean areSameFamily(java.lang.Iterable<RelDataType> types)
Returns whether all types in a collection have the same family, as determined byisSameFamily(RelDataType, RelDataType)
.- Parameters:
types
- Types to check- Returns:
- true if all types are of the same family
-
isSameFamily
private static boolean isSameFamily(RelDataType type1, RelDataType type2)
Returns whether two types are scalar types of the same family, or struct types whose fields are pairwise of the same family.- Parameters:
type1
- First typetype2
- Second type- Returns:
- Whether types have the same family
-
canConvertStringInCompare
private static boolean canConvertStringInCompare(RelDataTypeFamily family)
Returns whether a character data type can be implicitly converted to a given family in a compare operation.
-
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
-
maxPrecision
public static int maxPrecision(int p0, int p1)
Returns the larger of two precisions, treatingRelDataType.PRECISION_NOT_SPECIFIED
as infinity.
-
comparePrecision
public static int comparePrecision(int p0, int p1)
Returns whether a precision is greater or equal than another, treatingRelDataType.PRECISION_NOT_SPECIFIED
as infinity.
-
isArray
public static boolean isArray(RelDataType type)
-
-