use of io.trino.spi.type.TimestampWithTimeZoneType in project trino by trinodb.
the class TypeCoercion method compatibility.
private TypeCompatibility compatibility(Type fromType, Type toType) {
if (fromType.equals(toType)) {
return TypeCompatibility.compatible(toType, true);
}
if (fromType.equals(UnknownType.UNKNOWN)) {
return TypeCompatibility.compatible(toType, true);
}
if (toType.equals(UnknownType.UNKNOWN)) {
return TypeCompatibility.compatible(fromType, false);
}
String fromTypeBaseName = fromType.getBaseName();
String toTypeBaseName = toType.getBaseName();
if (fromTypeBaseName.equals(toTypeBaseName)) {
if (fromTypeBaseName.equals(StandardTypes.DECIMAL)) {
Type commonSuperType = getCommonSuperTypeForDecimal((DecimalType) fromType, (DecimalType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.VARCHAR)) {
Type commonSuperType = getCommonSuperTypeForVarchar((VarcharType) fromType, (VarcharType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.CHAR)) {
Type commonSuperType = getCommonSuperTypeForChar((CharType) fromType, (CharType) toType);
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.ROW)) {
return typeCompatibilityForRow((RowType) fromType, (RowType) toType);
}
if (fromTypeBaseName.equals(StandardTypes.TIMESTAMP)) {
Type commonSuperType = createTimestampType(Math.max(((TimestampType) fromType).getPrecision(), ((TimestampType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)) {
Type commonSuperType = createTimestampWithTimeZoneType(Math.max(((TimestampWithTimeZoneType) fromType).getPrecision(), ((TimestampWithTimeZoneType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.TIME)) {
Type commonSuperType = createTimeType(Math.max(((TimeType) fromType).getPrecision(), ((TimeType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (fromTypeBaseName.equals(StandardTypes.TIME_WITH_TIME_ZONE)) {
Type commonSuperType = createTimeWithTimeZoneType(Math.max(((TimeWithTimeZoneType) fromType).getPrecision(), ((TimeWithTimeZoneType) toType).getPrecision()));
return TypeCompatibility.compatible(commonSuperType, commonSuperType.equals(toType));
}
if (isCovariantParametrizedType(fromType)) {
return typeCompatibilityForCovariantParametrizedType(fromType, toType);
}
return TypeCompatibility.incompatible();
}
Optional<Type> coercedType = coerceTypeBase(fromType, toType.getBaseName());
if (coercedType.isPresent()) {
return compatibility(coercedType.get(), toType);
}
coercedType = coerceTypeBase(toType, fromType.getBaseName());
if (coercedType.isPresent()) {
TypeCompatibility typeCompatibility = compatibility(fromType, coercedType.get());
if (!typeCompatibility.isCompatible()) {
return TypeCompatibility.incompatible();
}
return TypeCompatibility.compatible(typeCompatibility.getCommonSuperType(), false);
}
return TypeCompatibility.incompatible();
}
use of io.trino.spi.type.TimestampWithTimeZoneType in project trino by trinodb.
the class TypeUtils method prestoNativeToJdbcObject.
private static Object prestoNativeToJdbcObject(ConnectorSession session, Type prestoType, Object prestoNative) throws SQLException {
if (prestoNative == null) {
return null;
}
if (DOUBLE.equals(prestoType) || BOOLEAN.equals(prestoType) || BIGINT.equals(prestoType)) {
return prestoNative;
}
if (prestoType instanceof DecimalType) {
DecimalType decimalType = (DecimalType) prestoType;
if (decimalType.isShort()) {
BigInteger unscaledValue = BigInteger.valueOf((long) prestoNative);
return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
}
BigInteger unscaledValue = ((Int128) prestoNative).toBigInteger();
return new BigDecimal(unscaledValue, decimalType.getScale(), new MathContext(decimalType.getPrecision()));
}
if (REAL.equals(prestoType)) {
return intBitsToFloat(toIntExact((long) prestoNative));
}
if (TINYINT.equals(prestoType)) {
return SignedBytes.checkedCast((long) prestoNative);
}
if (SMALLINT.equals(prestoType)) {
return Shorts.checkedCast((long) prestoNative);
}
if (INTEGER.equals(prestoType)) {
return toIntExact((long) prestoNative);
}
if (DATE.equals(prestoType)) {
// convert to midnight in default time zone
long millis = DAYS.toMillis((long) prestoNative);
return new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millis));
}
if (prestoType instanceof TimestampType && ((TimestampType) prestoType).isShort()) {
return toPgTimestamp(fromTrinoTimestamp((long) prestoNative));
}
if (prestoType instanceof TimestampWithTimeZoneType) {
// PostgreSQL does not store zone, only the point in time
int precision = ((TimestampWithTimeZoneType) prestoType).getPrecision();
if (precision <= TimestampWithTimeZoneType.MAX_SHORT_PRECISION) {
long millisUtc = unpackMillisUtc((long) prestoNative);
return new Timestamp(millisUtc);
} else {
LongTimestampWithTimeZone value = (LongTimestampWithTimeZone) prestoNative;
long epochSeconds = floorDiv(value.getEpochMillis(), MILLISECONDS_PER_SECOND);
long nanosOfSecond = floorMod(value.getEpochMillis(), MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND + value.getPicosOfMilli() / PICOSECONDS_PER_NANOSECOND;
return OffsetDateTime.ofInstant(Instant.ofEpochSecond(epochSeconds, nanosOfSecond), UTC_KEY.getZoneId());
}
}
if (prestoType instanceof VarcharType || prestoType instanceof CharType) {
return ((Slice) prestoNative).toStringUtf8();
}
if (prestoType instanceof ArrayType) {
// process subarray of multi-dimensional array
return getJdbcObjectArray(session, ((ArrayType) prestoType).getElementType(), (Block) prestoNative);
}
throw new TrinoException(NOT_SUPPORTED, "Unsupported type: " + prestoType);
}
Aggregations