Search in sources :

Example 6 with LongTimestampWithTimeZone

use of io.trino.spi.type.LongTimestampWithTimeZone 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);
}
Also used : VarcharType(io.trino.spi.type.VarcharType) StandardColumnMappings.fromTrinoTimestamp(io.trino.plugin.jdbc.StandardColumnMappings.fromTrinoTimestamp) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) Date(java.sql.Date) ArrayType(io.trino.spi.type.ArrayType) Slice(io.airlift.slice.Slice) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) DecimalType(io.trino.spi.type.DecimalType) BigInteger(java.math.BigInteger) TimestampType(io.trino.spi.type.TimestampType) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType) LongTimestampWithTimeZone(io.trino.spi.type.LongTimestampWithTimeZone) Int128(io.trino.spi.type.Int128)

Aggregations

LongTimestampWithTimeZone (io.trino.spi.type.LongTimestampWithTimeZone)6 Test (org.testng.annotations.Test)2 Slice (io.airlift.slice.Slice)1 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)1 BooleanStreamCheckpoint (io.trino.orc.checkpoint.BooleanStreamCheckpoint)1 LongStreamCheckpoint (io.trino.orc.checkpoint.LongStreamCheckpoint)1 StandardColumnMappings.fromTrinoTimestamp (io.trino.plugin.jdbc.StandardColumnMappings.fromTrinoTimestamp)1 TrinoException (io.trino.spi.TrinoException)1 BlockBuilder (io.trino.spi.block.BlockBuilder)1 ArrayType (io.trino.spi.type.ArrayType)1 CharType (io.trino.spi.type.CharType)1 DecimalType (io.trino.spi.type.DecimalType)1 Int128 (io.trino.spi.type.Int128)1 TimestampType (io.trino.spi.type.TimestampType)1 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)1 VarcharType (io.trino.spi.type.VarcharType)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 MathContext (java.math.MathContext)1 Date (java.sql.Date)1