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