use of io.trino.spi.type.TimestampType 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.TimestampType in project trino by trinodb.
the class FormatFunction method valueConverter.
private static BiFunction<ConnectorSession, Block, Object> valueConverter(FunctionDependencies functionDependencies, Type type, int position) {
if (type.equals(UNKNOWN)) {
return (session, block) -> null;
}
if (type.equals(BOOLEAN)) {
return (session, block) -> type.getBoolean(block, position);
}
if (type.equals(TINYINT) || type.equals(SMALLINT) || type.equals(INTEGER) || type.equals(BIGINT)) {
return (session, block) -> type.getLong(block, position);
}
if (type.equals(REAL)) {
return (session, block) -> intBitsToFloat(toIntExact(type.getLong(block, position)));
}
if (type.equals(DOUBLE)) {
return (session, block) -> type.getDouble(block, position);
}
if (type.equals(DATE)) {
return (session, block) -> LocalDate.ofEpochDay(type.getLong(block, position));
}
if (type instanceof TimestampWithTimeZoneType) {
return (session, block) -> toZonedDateTime(((TimestampWithTimeZoneType) type), block, position);
}
if (type instanceof TimestampType) {
return (session, block) -> toLocalDateTime(((TimestampType) type), block, position);
}
if (type instanceof TimeType) {
return (session, block) -> toLocalTime(type.getLong(block, position));
}
// TODO: support TIME WITH TIME ZONE by https://github.com/trinodb/trino/issues/191 + mapping to java.time.OffsetTime
if (type.equals(JSON)) {
MethodHandle handle = functionDependencies.getFunctionInvoker(QualifiedName.of("json_format"), ImmutableList.of(JSON), simpleConvention(FAIL_ON_NULL, NEVER_NULL)).getMethodHandle();
return (session, block) -> convertToString(handle, type.getSlice(block, position));
}
if (isShortDecimal(type)) {
int scale = ((DecimalType) type).getScale();
return (session, block) -> BigDecimal.valueOf(type.getLong(block, position), scale);
}
if (isLongDecimal(type)) {
int scale = ((DecimalType) type).getScale();
return (session, block) -> new BigDecimal(((Int128) type.getObject(block, position)).toBigInteger(), scale);
}
if (type instanceof VarcharType) {
return (session, block) -> type.getSlice(block, position).toStringUtf8();
}
if (type instanceof CharType) {
CharType charType = (CharType) type;
return (session, block) -> padSpaces(type.getSlice(block, position), charType).toStringUtf8();
}
BiFunction<ConnectorSession, Block, Object> function;
if (type.getJavaType() == long.class) {
function = (session, block) -> type.getLong(block, position);
} else if (type.getJavaType() == double.class) {
function = (session, block) -> type.getDouble(block, position);
} else if (type.getJavaType() == boolean.class) {
function = (session, block) -> type.getBoolean(block, position);
} else if (type.getJavaType() == Slice.class) {
function = (session, block) -> type.getSlice(block, position);
} else {
function = (session, block) -> type.getObject(block, position);
}
MethodHandle handle = functionDependencies.getCastInvoker(type, VARCHAR, simpleConvention(FAIL_ON_NULL, NEVER_NULL)).getMethodHandle();
return (session, block) -> convertToString(handle, function.apply(session, block));
}
use of io.trino.spi.type.TimestampType in project trino by trinodb.
the class TestAccumulatorCompiler method testAccumulatorCompilerForTypeSpecificObjectParameter.
@Test
public void testAccumulatorCompilerForTypeSpecificObjectParameter() {
TimestampType parameterType = TimestampType.TIMESTAMP_NANOS;
assertThat(parameterType.getJavaType()).isEqualTo(LongTimestamp.class);
assertGenerateAccumulator(LongTimestampAggregation.class, LongTimestampAggregationState.class);
}
use of io.trino.spi.type.TimestampType in project trino by trinodb.
the class TestAccumulatorCompiler method testAccumulatorCompilerForTypeSpecificObjectParameterSeparateClassLoader.
@Test
public void testAccumulatorCompilerForTypeSpecificObjectParameterSeparateClassLoader() throws Exception {
TimestampType parameterType = TimestampType.TIMESTAMP_NANOS;
assertThat(parameterType.getJavaType()).isEqualTo(LongTimestamp.class);
ClassLoader pluginClassLoader = PluginManager.createClassLoader("test", ImmutableList.of());
DynamicClassLoader classLoader = new DynamicClassLoader(pluginClassLoader);
Class<? extends AccumulatorState> stateInterface = IsolatedClass.isolateClass(classLoader, AccumulatorState.class, LongTimestampAggregationState.class, LongTimestampAggregation.class);
assertThat(stateInterface.getCanonicalName()).isEqualTo(LongTimestampAggregationState.class.getCanonicalName());
assertThat(stateInterface).isNotSameAs(LongTimestampAggregationState.class);
Class<?> aggregation = classLoader.loadClass(LongTimestampAggregation.class.getCanonicalName());
assertThat(aggregation.getCanonicalName()).isEqualTo(LongTimestampAggregation.class.getCanonicalName());
assertThat(aggregation).isNotSameAs(LongTimestampAggregation.class);
assertGenerateAccumulator(aggregation, stateInterface);
}
use of io.trino.spi.type.TimestampType in project trino by trinodb.
the class StatsUtil method toStatsRepresentation.
public static OptionalDouble toStatsRepresentation(Type type, Object value) {
requireNonNull(type, "type is null");
requireNonNull(value, "value is null");
if (type == BOOLEAN) {
return OptionalDouble.of((boolean) value ? 1 : 0);
}
if (type == TINYINT || type == SMALLINT || type == INTEGER || type == BIGINT) {
return OptionalDouble.of((long) value);
}
if (type == REAL) {
return OptionalDouble.of(intBitsToFloat(toIntExact((Long) value)));
}
if (type == DOUBLE) {
return OptionalDouble.of((double) value);
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
if (decimalType.isShort()) {
return OptionalDouble.of(shortDecimalToDouble((long) value, longTenToNth(decimalType.getScale())));
}
return OptionalDouble.of(longDecimalToDouble((Int128) value, decimalType.getScale()));
}
if (type == DATE) {
return OptionalDouble.of((long) value);
}
if (type instanceof TimestampType) {
if (((TimestampType) type).isShort()) {
return OptionalDouble.of((long) value);
}
return OptionalDouble.of(((LongTimestamp) value).getEpochMicros());
}
if (type instanceof TimestampWithTimeZoneType) {
if (((TimestampWithTimeZoneType) type).isShort()) {
return OptionalDouble.of(unpackMillisUtc((long) value));
}
return OptionalDouble.of(((LongTimestampWithTimeZone) value).getEpochMillis());
}
return OptionalDouble.empty();
}
Aggregations