@Internal public final class LogicalTypeCasts extends Object
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.
Modifier and Type | Method and Description |
---|---|
static boolean |
supportsAvoidingCast(List<LogicalType> sourceTypes,
List<LogicalType> targetTypes)
|
static boolean |
supportsAvoidingCast(LogicalType sourceType,
LogicalType targetType)
Returns whether the source type can be safely interpreted as the target type.
|
static boolean |
supportsExplicitCast(LogicalType sourceType,
LogicalType targetType)
Returns whether the source type can be casted to the target type.
|
static boolean |
supportsImplicitCast(LogicalType sourceType,
LogicalType targetType)
Returns whether the source type can be safely casted to the target type without loosing
information.
|
public static boolean supportsAvoidingCast(LogicalType sourceType, LogicalType targetType)
LogicalType.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.
public static boolean supportsAvoidingCast(List<LogicalType> sourceTypes, List<LogicalType> targetTypes)
public static boolean supportsImplicitCast(LogicalType sourceType, LogicalType targetType)
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
).
public static boolean supportsExplicitCast(LogicalType sourceType, LogicalType targetType)
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.
Copyright © 2014–2024 The Apache Software Foundation. All rights reserved.