Search in sources :

Example 1 with LongTimestamp

use of io.trino.spi.type.LongTimestamp in project trino by trinodb.

the class DateTimes method toLocalDateTime.

public static LocalDateTime toLocalDateTime(TimestampType type, Block block, int position) {
    int precision = type.getPrecision();
    long epochMicros;
    int picosOfMicro = 0;
    if (precision <= MAX_SHORT_PRECISION) {
        epochMicros = type.getLong(block, position);
    } else {
        LongTimestamp timestamp = (LongTimestamp) type.getObject(block, position);
        epochMicros = timestamp.getEpochMicros();
        picosOfMicro = timestamp.getPicosOfMicro();
    }
    long epochSecond = scaleEpochMicrosToSeconds(epochMicros);
    int nanoFraction = getMicrosOfSecond(epochMicros) * NANOSECONDS_PER_MICROSECOND + (int) (roundToNearest(picosOfMicro, PICOSECONDS_PER_NANOSECOND) / PICOSECONDS_PER_NANOSECOND);
    Instant instant = Instant.ofEpochSecond(epochSecond, nanoFraction);
    return LocalDateTime.ofInstant(instant, UTC);
}
Also used : LongTimestamp(io.trino.spi.type.LongTimestamp) Instant(java.time.Instant)

Example 2 with LongTimestamp

use of io.trino.spi.type.LongTimestamp in project trino by trinodb.

the class TestStatsUtil method testToStatsRepresentation.

@Test
public void testToStatsRepresentation() {
    assertToStatsRepresentation(BIGINT, 123456L, 123456);
    assertToStatsRepresentation(INTEGER, 12345L, 12345);
    assertToStatsRepresentation(SMALLINT, 1234L, 1234);
    assertToStatsRepresentation(TINYINT, 123L, 123);
    assertToStatsRepresentation(DOUBLE, 0.1d, 0.1);
    assertToStatsRepresentation(REAL, (long) floatToIntBits(0.2f), 0.2f);
    assertToStatsRepresentation(createDecimalType(5, 2), 12345L, 123.45);
    assertToStatsRepresentation(createDecimalType(25, 5), Decimals.valueOf(new BigDecimal("12345678901234567890.12345")), 12345678901234567890.12345);
    assertToStatsRepresentation(DATE, 1L, 1);
    assertToStatsRepresentation(createTimestampType(0), 3_000_000L, 3_000_000.);
    assertToStatsRepresentation(createTimestampType(3), 3_000L, 3_000.);
    assertToStatsRepresentation(createTimestampType(6), 3L, 3.);
    assertToStatsRepresentation(createTimestampType(9), new LongTimestamp(3, 0), 3.);
    assertToStatsRepresentation(createTimestampType(12), new LongTimestamp(3, 999), 3.);
    assertToStatsRepresentation(createTimestampWithTimeZoneType(0), packDateTimeWithZone(3000, getTimeZoneKey("Europe/Warsaw")), 3000.);
    assertToStatsRepresentation(createTimestampWithTimeZoneType(3), packDateTimeWithZone(3, getTimeZoneKey("Europe/Warsaw")), 3.);
    assertToStatsRepresentation(createTimestampWithTimeZoneType(6), LongTimestampWithTimeZone.fromEpochMillisAndFraction(3, 999999999, getTimeZoneKey("Europe/Warsaw")), 3.);
    assertToStatsRepresentation(createTimestampWithTimeZoneType(9), LongTimestampWithTimeZone.fromEpochMillisAndFraction(3, 999999999, getTimeZoneKey("Europe/Warsaw")), 3.);
    assertToStatsRepresentation(createTimestampWithTimeZoneType(12), LongTimestampWithTimeZone.fromEpochMillisAndFraction(3, 999999999, getTimeZoneKey("Europe/Warsaw")), 3.);
}
Also used : LongTimestamp(io.trino.spi.type.LongTimestamp) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 3 with LongTimestamp

use of io.trino.spi.type.LongTimestamp in project trino by trinodb.

the class TestTupleDomainParquetPredicate method testTimestampInt64.

@Test(dataProvider = "testTimestampInt64DataProvider")
public void testTimestampInt64(TimeUnit timeUnit, int precision, LocalDateTime baseTime, Object baseDomainValue) throws ParquetCorruptionException {
    int parquetPrecision;
    switch(timeUnit) {
        case MILLIS:
            parquetPrecision = 3;
            break;
        case MICROS:
            parquetPrecision = 6;
            break;
        case NANOS:
            parquetPrecision = 9;
            break;
        default:
            throw new IllegalArgumentException("Unknown Parquet TimeUnit " + timeUnit);
    }
    PrimitiveType type = Types.required(INT64).as(LogicalTypeAnnotation.timestampType(false, timeUnit)).named("TimestampColumn");
    ColumnDescriptor columnDescriptor = new ColumnDescriptor(new String[] {}, type, 0, 0);
    TimestampType timestampType = createTimestampType(precision);
    assertEquals(getDomain(columnDescriptor, timestampType, 0, null, ID, UTC), all(timestampType));
    LocalDateTime maxTime = baseTime.plus(Duration.ofMillis(50));
    Object maxDomainValue;
    if (baseDomainValue instanceof Long) {
        maxDomainValue = (long) baseDomainValue + 50 * MICROSECONDS_PER_MILLISECOND;
    } else if (baseDomainValue instanceof LongTimestamp) {
        LongTimestamp longTimestamp = ((LongTimestamp) baseDomainValue);
        maxDomainValue = new LongTimestamp(longTimestamp.getEpochMicros() + 50 * MICROSECONDS_PER_MILLISECOND, longTimestamp.getPicosOfMicro());
    } else {
        throw new IllegalArgumentException("Unknown Timestamp domain type " + baseDomainValue);
    }
    long minValue = toEpochWithPrecision(baseTime, parquetPrecision);
    long maxValue = toEpochWithPrecision(maxTime, parquetPrecision);
    assertEquals(getDomain(columnDescriptor, timestampType, 10, longColumnStats(minValue, minValue), ID, UTC), singleValue(timestampType, baseDomainValue));
    assertEquals(getDomain(columnDescriptor, timestampType, 10, longColumnStats(minValue, maxValue), ID, UTC), Domain.create(ValueSet.ofRanges(range(timestampType, baseDomainValue, true, maxDomainValue, true)), false));
}
Also used : LocalDateTime(java.time.LocalDateTime) LongTimestamp(io.trino.spi.type.LongTimestamp) ColumnDescriptor(org.apache.parquet.column.ColumnDescriptor) TimestampType(io.trino.spi.type.TimestampType) TimestampType.createTimestampType(io.trino.spi.type.TimestampType.createTimestampType) PrimitiveType(org.apache.parquet.schema.PrimitiveType) Test(org.testng.annotations.Test)

Example 4 with LongTimestamp

use of io.trino.spi.type.LongTimestamp in project trino by trinodb.

the class TimestampNanosValueWriter method write.

@Override
public void write(Block block) {
    for (int i = 0; i < block.getPositionCount(); i++) {
        if (!block.isNull(i)) {
            LongTimestamp value = (LongTimestamp) type.getObject(block, i);
            long epochNanos = multiplyExact(value.getEpochMicros(), NANOSECONDS_PER_MICROSECOND) + LongMath.divide(value.getPicosOfMicro(), PICOSECONDS_PER_NANOSECOND, UNNECESSARY);
            getValueWriter().writeLong(epochNanos);
            getStatistics().updateStats(epochNanos);
        }
    }
}
Also used : LongTimestamp(io.trino.spi.type.LongTimestamp)

Example 5 with LongTimestamp

use of io.trino.spi.type.LongTimestamp in project trino by trinodb.

the class OrcTester method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else {
        if (BOOLEAN.equals(type)) {
            type.writeBoolean(blockBuilder, (Boolean) value);
        } else if (TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type)) {
            type.writeLong(blockBuilder, ((Number) value).longValue());
        } else if (Decimals.isShortDecimal(type)) {
            type.writeLong(blockBuilder, ((SqlDecimal) value).toBigDecimal().unscaledValue().longValue());
        } else if (Decimals.isLongDecimal(type)) {
            type.writeObject(blockBuilder, Int128.valueOf(((SqlDecimal) value).toBigDecimal().unscaledValue()));
        } else if (DOUBLE.equals(type)) {
            type.writeDouble(blockBuilder, ((Number) value).doubleValue());
        } else if (REAL.equals(type)) {
            float floatValue = ((Number) value).floatValue();
            type.writeLong(blockBuilder, Float.floatToIntBits(floatValue));
        } else if (type instanceof VarcharType) {
            Slice slice = truncateToLength(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (type instanceof CharType) {
            Slice slice = truncateToLengthAndTrimSpaces(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (VARBINARY.equals(type)) {
            type.writeSlice(blockBuilder, Slices.wrappedBuffer(((SqlVarbinary) value).getBytes()));
        } else if (DATE.equals(type)) {
            long days = ((SqlDate) value).getDays();
            type.writeLong(blockBuilder, days);
        } else if (TIMESTAMP_MILLIS.equals(type)) {
            type.writeLong(blockBuilder, ((SqlTimestamp) value).getEpochMicros());
        } else if (TIMESTAMP_MICROS.equals(type)) {
            long micros = ((SqlTimestamp) value).getEpochMicros();
            type.writeLong(blockBuilder, micros);
        } else if (TIMESTAMP_NANOS.equals(type)) {
            SqlTimestamp ts = (SqlTimestamp) value;
            type.writeObject(blockBuilder, new LongTimestamp(ts.getEpochMicros(), ts.getPicosOfMicros()));
        } else if (TIMESTAMP_TZ_MILLIS.equals(type)) {
            long millis = ((SqlTimestampWithTimeZone) value).getEpochMillis();
            type.writeLong(blockBuilder, packDateTimeWithZone(millis, UTC_KEY));
        } else if (TIMESTAMP_TZ_MICROS.equals(type) || TIMESTAMP_TZ_NANOS.equals(type)) {
            SqlTimestampWithTimeZone ts = (SqlTimestampWithTimeZone) value;
            type.writeObject(blockBuilder, LongTimestampWithTimeZone.fromEpochMillisAndFraction(ts.getEpochMillis(), ts.getPicosOfMilli(), UTC_KEY));
        } else {
            if (type instanceof ArrayType) {
                List<?> array = (List<?>) value;
                Type elementType = type.getTypeParameters().get(0);
                BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
                for (Object elementValue : array) {
                    writeValue(elementType, arrayBlockBuilder, elementValue);
                }
                blockBuilder.closeEntry();
            } else if (type instanceof MapType) {
                Map<?, ?> map = (Map<?, ?>) value;
                Type keyType = type.getTypeParameters().get(0);
                Type valueType = type.getTypeParameters().get(1);
                BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
                for (Entry<?, ?> entry : map.entrySet()) {
                    writeValue(keyType, mapBlockBuilder, entry.getKey());
                    writeValue(valueType, mapBlockBuilder, entry.getValue());
                }
                blockBuilder.closeEntry();
            } else if (type instanceof RowType) {
                List<?> array = (List<?>) value;
                List<Type> fieldTypes = type.getTypeParameters();
                BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
                for (int fieldId = 0; fieldId < fieldTypes.size(); fieldId++) {
                    Type fieldType = fieldTypes.get(fieldId);
                    writeValue(fieldType, rowBlockBuilder, array.get(fieldId));
                }
                blockBuilder.closeEntry();
            } else {
                throw new IllegalArgumentException("Unsupported type " + type);
            }
        }
    }
}
Also used : LongTimestamp(io.trino.spi.type.LongTimestamp) VarcharType(io.trino.spi.type.VarcharType) SqlVarbinary(io.trino.spi.type.SqlVarbinary) RowType(io.trino.spi.type.RowType) SqlDecimal(io.trino.spi.type.SqlDecimal) SqlTimestamp(io.trino.spi.type.SqlTimestamp) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) OrcType(io.trino.orc.metadata.OrcType) MapType(io.trino.spi.type.MapType) VarbinaryType(io.trino.spi.type.VarbinaryType) CharType(io.trino.spi.type.CharType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) Entry(java.util.Map.Entry) SqlTimestampWithTimeZone(io.trino.spi.type.SqlTimestampWithTimeZone) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) Arrays.asList(java.util.Arrays.asList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) CharType(io.trino.spi.type.CharType) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) BlockBuilder(io.trino.spi.block.BlockBuilder)

Aggregations

LongTimestamp (io.trino.spi.type.LongTimestamp)16 BlockBuilder (io.trino.spi.block.BlockBuilder)4 CharType (io.trino.spi.type.CharType)4 Type (io.trino.spi.type.Type)4 VarcharType (io.trino.spi.type.VarcharType)4 Test (org.testng.annotations.Test)4 ImmutableList (com.google.common.collect.ImmutableList)3 DecimalType (io.trino.spi.type.DecimalType)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Slice (io.airlift.slice.Slice)2 ArrayType (io.trino.spi.type.ArrayType)2 TimestampType (io.trino.spi.type.TimestampType)2 VarbinaryType (io.trino.spi.type.VarbinaryType)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 MoreObjects.toStringHelper (com.google.common.base.MoreObjects.toStringHelper)1 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)1