Search in sources :

Example 11 with LongTimestamp

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

the class TestTupleDomainOrcPredicate method testTimestampNanos.

@Test
public void testTimestampNanos() {
    assertThat(getDomain(TIMESTAMP_NANOS, 0, null)).isEqualTo(none(TIMESTAMP_NANOS));
    assertThat(getDomain(TIMESTAMP_NANOS, 10, null)).isEqualTo(all(TIMESTAMP_NANOS));
    assertThat(getDomain(TIMESTAMP_NANOS, 0, timeStampColumnStats(null, null, null))).isEqualTo(none(TIMESTAMP_NANOS));
    assertThat(getDomain(TIMESTAMP_NANOS, 0, timeStampColumnStats(0L, null, null))).isEqualTo(none(TIMESTAMP_NANOS));
    assertThat(getDomain(TIMESTAMP_NANOS, 0, timeStampColumnStats(0L, 100L, 100L))).isEqualTo(none(TIMESTAMP_NANOS));
    assertThat(getDomain(TIMESTAMP_NANOS, 10, timeStampColumnStats(0L, null, null))).isEqualTo(onlyNull(TIMESTAMP_NANOS));
    assertThat(getDomain(TIMESTAMP_NANOS, 10, timeStampColumnStats(10L, null, null))).isEqualTo(notNull(TIMESTAMP_NANOS));
    LongTimestamp low13 = new LongTimestamp(13_000L, 0);
    LongTimestamp low100 = new LongTimestamp(100_000L, 0);
    LongTimestamp high100 = new LongTimestamp(101_000L, 0);
    assertThat(getDomain(TIMESTAMP_NANOS, 10, timeStampColumnStats(10L, 100L, 100L))).isEqualTo(create(ValueSet.ofRanges(range(TIMESTAMP_NANOS, low100, true, high100, true)), false));
    assertThat(getDomain(TIMESTAMP_NANOS, 10, timeStampColumnStats(10L, 13L, 100L))).isEqualTo(create(ValueSet.ofRanges(range(TIMESTAMP_NANOS, low13, true, high100, true)), false));
    assertThat(getDomain(TIMESTAMP_NANOS, 10, timeStampColumnStats(10L, null, 100L))).isEqualTo(create(ValueSet.ofRanges(lessThanOrEqual(TIMESTAMP_NANOS, high100)), false));
    assertThat(getDomain(TIMESTAMP_NANOS, 10, timeStampColumnStats(10L, 13L, null))).isEqualTo(create(ValueSet.ofRanges(greaterThanOrEqual(TIMESTAMP_NANOS, low13)), false));
    assertThat(getDomain(TIMESTAMP_NANOS, 10, timeStampColumnStats(5L, 13L, 100L))).isEqualTo(create(ValueSet.ofRanges(range(TIMESTAMP_NANOS, low13, true, high100, true)), true));
    assertThat(getDomain(TIMESTAMP_NANOS, 10, timeStampColumnStats(5L, null, 100L))).isEqualTo(create(ValueSet.ofRanges(lessThanOrEqual(TIMESTAMP_NANOS, high100)), true));
    assertThat(getDomain(TIMESTAMP_NANOS, 10, timeStampColumnStats(5L, 13L, null))).isEqualTo(create(ValueSet.ofRanges(greaterThanOrEqual(TIMESTAMP_NANOS, low13)), true));
}
Also used : LongTimestamp(io.trino.spi.type.LongTimestamp) Test(org.testng.annotations.Test)

Example 12 with LongTimestamp

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

the class TimestampColumnWriter method writeTimestampNanos.

private void writeTimestampNanos(Block block) {
    for (int i = 0; i < block.getPositionCount(); i++) {
        if (!block.isNull(i)) {
            LongTimestamp timestamp = (LongTimestamp) type.getObject(block, i);
            // ORC erroneously uses normal division for seconds
            long seconds = timestamp.getEpochMicros() / MICROSECONDS_PER_SECOND;
            long microsFraction = floorMod(timestamp.getEpochMicros(), MICROSECONDS_PER_SECOND);
            long nanosFraction = (microsFraction * NANOSECONDS_PER_MICROSECOND) + // no rounding since the data has nanosecond precision, at most
            (timestamp.getPicosOfMicro() / PICOSECONDS_PER_NANOSECOND);
            long millis = floorDiv(timestamp.getEpochMicros(), MICROSECONDS_PER_MILLISECOND);
            writeValues(seconds, nanosFraction);
            statisticsBuilder.addValue(millis);
        }
    }
}
Also used : LongTimestamp(io.trino.spi.type.LongTimestamp) BooleanStreamCheckpoint(io.trino.orc.checkpoint.BooleanStreamCheckpoint) LongStreamCheckpoint(io.trino.orc.checkpoint.LongStreamCheckpoint)

Example 13 with LongTimestamp

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

the class MaterializedResult method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else if (BIGINT.equals(type)) {
        type.writeLong(blockBuilder, (Long) value);
    } else if (INTEGER.equals(type)) {
        type.writeLong(blockBuilder, (Integer) value);
    } else if (SMALLINT.equals(type)) {
        type.writeLong(blockBuilder, (Short) value);
    } else if (TINYINT.equals(type)) {
        type.writeLong(blockBuilder, (Byte) value);
    } else if (REAL.equals(type)) {
        type.writeLong(blockBuilder, floatToRawIntBits(((Float) value)));
    } else if (DOUBLE.equals(type)) {
        type.writeDouble(blockBuilder, (Double) value);
    } else if (BOOLEAN.equals(type)) {
        type.writeBoolean(blockBuilder, (Boolean) value);
    } else if (JSON.equals(type)) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof VarcharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof CharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (VARBINARY.equals(type)) {
        type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) value));
    } else if (DATE.equals(type)) {
        int days = ((SqlDate) value).getDays();
        type.writeLong(blockBuilder, days);
    } else if (type instanceof TimeType) {
        SqlTime time = (SqlTime) value;
        type.writeLong(blockBuilder, time.getPicos());
    } else if (type instanceof TimeWithTimeZoneType) {
        long nanos = roundDiv(((SqlTimeWithTimeZone) value).getPicos(), PICOSECONDS_PER_NANOSECOND);
        int offsetMinutes = ((SqlTimeWithTimeZone) value).getOffsetMinutes();
        type.writeLong(blockBuilder, packTimeWithTimeZone(nanos, offsetMinutes));
    } else if (type instanceof TimestampType) {
        long micros = ((SqlTimestamp) value).getEpochMicros();
        if (((TimestampType) type).getPrecision() <= TimestampType.MAX_SHORT_PRECISION) {
            type.writeLong(blockBuilder, micros);
        } else {
            type.writeObject(blockBuilder, new LongTimestamp(micros, ((SqlTimestamp) value).getPicosOfMicros()));
        }
    } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimestampWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimestampWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (type instanceof ArrayType) {
        List<?> list = (List<?>) value;
        Type elementType = ((ArrayType) type).getElementType();
        BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object element : list) {
            writeValue(elementType, arrayBlockBuilder, element);
        }
        blockBuilder.closeEntry();
    } else if (type instanceof MapType) {
        Map<?, ?> map = (Map<?, ?>) value;
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        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<?> row = (List<?>) value;
        List<Type> fieldTypes = type.getTypeParameters();
        BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
        for (int field = 0; field < row.size(); field++) {
            writeValue(fieldTypes.get(field), rowBlockBuilder, row.get(field));
        }
        blockBuilder.closeEntry();
    } else {
        throw new IllegalArgumentException("Unsupported type " + type);
    }
}
Also used : VarcharType(io.trino.spi.type.VarcharType) SqlTime(io.trino.spi.type.SqlTime) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) TimeType(io.trino.spi.type.TimeType) ArrayType(io.trino.spi.type.ArrayType) TimestampType(io.trino.spi.type.TimestampType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) TimeZoneKey(io.trino.spi.type.TimeZoneKey) BlockBuilder(io.trino.spi.block.BlockBuilder) LongTimestamp(io.trino.spi.type.LongTimestamp) SqlTimeWithTimeZone(io.trino.spi.type.SqlTimeWithTimeZone) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) MapType(io.trino.spi.type.MapType) CharType(io.trino.spi.type.CharType) SqlTimestampWithTimeZone(io.trino.spi.type.SqlTimestampWithTimeZone) SqlDate(io.trino.spi.type.SqlDate) OptionalLong(java.util.OptionalLong) CharType(io.trino.spi.type.CharType) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 14 with LongTimestamp

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

the class TestLiteralEncoder method testEncodeTimestamp.

@Test
public void testEncodeTimestamp() {
    for (int precision = 0; precision <= 12; precision++) {
        assertEncode(null, createTimestampType(precision), format("CAST(null AS timestamp(%s))", precision));
    }
    assertEncode(1603710138_000000L, createTimestampType(0), "TIMESTAMP '2020-10-26 11:02:18'");
    assertEncode(1603710138_100000L, createTimestampType(1), "TIMESTAMP '2020-10-26 11:02:18.1'");
    assertEncode(1603710138_120000L, createTimestampType(2), "TIMESTAMP '2020-10-26 11:02:18.12'");
    assertEncode(1603710138_123000L, createTimestampType(3), "TIMESTAMP '2020-10-26 11:02:18.123'");
    assertEncode(1603710138_123400L, createTimestampType(4), "TIMESTAMP '2020-10-26 11:02:18.1234'");
    assertEncode(1603710138_123450L, createTimestampType(5), "TIMESTAMP '2020-10-26 11:02:18.12345'");
    assertEncode(1603710138_123456L, createTimestampType(6), "TIMESTAMP '2020-10-26 11:02:18.123456'");
    assertEncode(new LongTimestamp(1603710138_123456L, 100000), createTimestampType(7), "TIMESTAMP '2020-10-26 11:02:18.1234561'");
    assertEncode(new LongTimestamp(1603710138_123456L, 120000), createTimestampType(8), "TIMESTAMP '2020-10-26 11:02:18.12345612'");
    assertEncode(new LongTimestamp(1603710138_123456L, 123000), createTimestampType(9), "TIMESTAMP '2020-10-26 11:02:18.123456123'");
    assertEncode(new LongTimestamp(1603710138_123456L, 123400), createTimestampType(10), "TIMESTAMP '2020-10-26 11:02:18.1234561234'");
    assertEncode(new LongTimestamp(1603710138_123456L, 123450), createTimestampType(11), "TIMESTAMP '2020-10-26 11:02:18.12345612345'");
    assertEncode(new LongTimestamp(1603710138_123456L, 123456), createTimestampType(12), "TIMESTAMP '2020-10-26 11:02:18.123456123456'");
    assertEncode(1603710138_000000L, createTimestampType(1), "TIMESTAMP '2020-10-26 11:02:18.0'");
    assertEncode(1603710138_000000L, createTimestampType(2), "TIMESTAMP '2020-10-26 11:02:18.00'");
    assertEncode(1603710138_000000L, createTimestampType(3), "TIMESTAMP '2020-10-26 11:02:18.000'");
    assertEncode(1603710138_000000L, createTimestampType(4), "TIMESTAMP '2020-10-26 11:02:18.0000'");
    assertEncode(1603710138_000000L, createTimestampType(5), "TIMESTAMP '2020-10-26 11:02:18.00000'");
    assertEncode(1603710138_000000L, createTimestampType(6), "TIMESTAMP '2020-10-26 11:02:18.000000'");
    assertEncode(new LongTimestamp(1603710138_000000L, 0), createTimestampType(7), "TIMESTAMP '2020-10-26 11:02:18.0000000'");
    assertEncode(new LongTimestamp(1603710138_000000L, 0), createTimestampType(8), "TIMESTAMP '2020-10-26 11:02:18.00000000'");
    assertEncode(new LongTimestamp(1603710138_000000L, 0), createTimestampType(9), "TIMESTAMP '2020-10-26 11:02:18.000000000'");
    assertEncode(new LongTimestamp(1603710138_000000L, 0), createTimestampType(10), "TIMESTAMP '2020-10-26 11:02:18.0000000000'");
    assertEncode(new LongTimestamp(1603710138_000000L, 0), createTimestampType(11), "TIMESTAMP '2020-10-26 11:02:18.00000000000'");
    assertEncode(new LongTimestamp(1603710138_000000L, 0), createTimestampType(12), "TIMESTAMP '2020-10-26 11:02:18.000000000000'");
}
Also used : LongTimestamp(io.trino.spi.type.LongTimestamp) Test(org.testng.annotations.Test)

Example 15 with LongTimestamp

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

the class TestScalarFunctionAdapter method getTestValue.

private static Object getTestValue(Type argumentType) {
    // BOOLEAN, BIGINT, DOUBLE, VARCHAR, ARRAY_TYPE, createCharType(7), createTimestampType(9)
    if (argumentType.equals(BOOLEAN)) {
        return true;
    }
    if (argumentType.equals(DOUBLE)) {
        return 33.33;
    }
    if (argumentType.equals(BIGINT)) {
        return 42L;
    }
    if (argumentType.equals(VARCHAR)) {
        return Slices.utf8Slice("test");
    }
    if (argumentType.equals(ARRAY_TYPE)) {
        BlockBuilder blockBuilder = BIGINT.createBlockBuilder(null, 4);
        blockBuilder.appendNull();
        blockBuilder.writeLong(99);
        blockBuilder.appendNull();
        blockBuilder.writeLong(100);
        return blockBuilder.build();
    }
    if (argumentType.equals(CHAR_TYPE)) {
        return Slices.utf8Slice("1234567");
    }
    if (argumentType.equals(TIMESTAMP_TYPE)) {
        return new LongTimestamp(5678, 123_000);
    }
    throw new IllegalArgumentException("Unsupported argument type: " + argumentType);
}
Also used : LongTimestamp(io.trino.spi.type.LongTimestamp) 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