Casts this expr to a specific target type. It checks the validity of the cast according to 'compatibility' and calls uncheckedCastTo(). @param targetType type to be cast to @param compatibility compatibility level that defines the relation between 'targetType' and the type
(Type targetType, TypeCompatibility compatibility)
| 1610 | * failure to convert a string literal to a date literal |
| 1611 | */ |
| 1612 | public final Expr castTo(Type targetType, TypeCompatibility compatibility) |
| 1613 | throws AnalysisException { |
| 1614 | Type type = Type.getAssignmentCompatibleType(this.type_, targetType, compatibility); |
| 1615 | Preconditions.checkState(type.isValid(), "cast %s to %s", this.type_, targetType); |
| 1616 | // If the targetType is NULL_TYPE then ignore the cast because NULL_TYPE |
| 1617 | // is compatible with all types and no cast is necessary. |
| 1618 | if (targetType.isNull()) return this; |
| 1619 | // If decimal, cast to the target type. |
| 1620 | if (targetType.isDecimal()) return uncheckedCastTo(targetType, compatibility); |
| 1621 | // If they match, cast to the type both values can be assigned to (the definition of |
| 1622 | // getAssignmentCompatibleType), which implies no loss of precision. Note that |
| 1623 | // getAssignmentCompatibleType always returns a "real" (not wildcard) type. |
| 1624 | if (type.matchesType(targetType)) return uncheckedCastTo(type, compatibility); |
| 1625 | throw new SqlCastException("targetType=" + targetType + " type=" + type); |
| 1626 | } |
| 1627 | |
| 1628 | public final Expr castTo(Type targetType) throws AnalysisException { |
| 1629 | return castTo(targetType, TypeCompatibility.DEFAULT); |