Class LogicalTypeCasts
LogicalType.
This class aims to be compatible with the SQL standard. It is inspired by Apache Calcite's
SqlTypeUtil#canCastFrom method.
Casts can be performed in two ways: implicit or explicit.
Explicit casts correspond to the SQL cast specification and represent the logic behind a
CAST(sourceType AS targetType) operation. For example, it allows for converting most
types of the LogicalTypeFamily.PREDEFINED family to types of the LogicalTypeFamily.CHARACTER_STRING family.
Implicit casts are used for safe type widening and type generalization (finding a common
supertype for a set of types) without loss of information. Implicit casts are similar to the Java
semantics (e.g. this is not possible: int x = (String) z).
Conversions that are defined by the LogicalType (e.g. interpreting a DateType
as integer value) are not considered here. They are an internal bridging feature that is not
standard compliant. If at all, CONVERT methods should make such conversions available.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleansupportsAvoidingCast(List<LogicalType> sourceTypes, List<LogicalType> targetTypes) static booleansupportsAvoidingCast(LogicalType sourceType, LogicalType targetType) Returns whether the source type can be safely interpreted as the target type.static booleansupportsExplicitCast(LogicalType sourceType, LogicalType targetType) Returns whether the source type can be casted to the target type.static booleansupportsImplicitCast(LogicalType sourceType, LogicalType targetType) Returns whether the source type can be safely casted to the target type without loosing information.static booleansupportsReinterpretCast(LogicalType sourceType, LogicalType targetType) Returns whether the source type can be reinterpreted as the target type.
-
Method Details
-
supportsAvoidingCast
Returns whether the source type can be safely interpreted as the target type. This allows avoiding casts by ignoring some logical properties. This is basically a relaxedLogicalType.equals(Object).In particular this means:
Atomic, non-string types (INT, BOOLEAN, ...) and user-defined structured types must be fully equal (i.e.
LogicalType.equals(Object)). However, a NOT NULL type can be stored in NULL type but not vice versa.Atomic, string types must be contained in the target type (e.g. CHAR(2) is contained in VARCHAR(3), but VARCHAR(2) is not contained in CHAR(3)). Same for binary strings.
Constructed types (ARRAY, ROW, MAP, etc.) and user-defined distinct type must be of same kind but ignore field names and other logical attributes. Structured and row kinds are compatible. However, all the children types (
LogicalType.getChildren()) must be compatible. -
supportsAvoidingCast
public static boolean supportsAvoidingCast(List<LogicalType> sourceTypes, List<LogicalType> targetTypes) -
supportsImplicitCast
Returns whether the source type can be safely casted to the target type without loosing information.Implicit casts are used for type widening and type generalization (finding a common supertype for a set of types). Implicit casts are similar to the Java semantics (e.g. this is not possible:
int x = (String) z). -
supportsExplicitCast
Returns whether the source type can be casted to the target type.Explicit casts correspond to the SQL cast specification and represent the logic behind a
CAST(sourceType AS targetType)operation. For example, it allows for converting most types of theLogicalTypeFamily.PREDEFINEDfamily to types of theLogicalTypeFamily.CHARACTER_STRINGfamily. -
supportsReinterpretCast
Returns whether the source type can be reinterpreted as the target type.Reinterpret casts correspond to the SQL reinterpret_cast and represent the logic behind a
REINTERPRET_CAST(sourceType AS targetType)operation.
-