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));
}
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);
}
}
}
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);
}
}
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'");
}
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);
}
Aggregations