Search in sources :

Example 6 with TimestampType

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));
}
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 7 with TimestampType

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));
}
Also used : FunctionDependencies(io.trino.metadata.FunctionDependencies) SCALAR(io.trino.metadata.FunctionKind.SCALAR) FunctionDependencyDeclarationBuilder(io.trino.metadata.FunctionDependencyDeclaration.FunctionDependencyDeclarationBuilder) FAIL_ON_NULL(io.trino.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL) BiFunction(java.util.function.BiFunction) FunctionNullability(io.trino.metadata.FunctionNullability) UNKNOWN(io.trino.type.UnknownType.UNKNOWN) InvocationConvention.simpleConvention(io.trino.spi.function.InvocationConvention.simpleConvention) Timestamps.roundDiv(io.trino.spi.type.Timestamps.roundDiv) BigDecimal(java.math.BigDecimal) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) Block(io.trino.spi.block.Block) LocalTime(java.time.LocalTime) IllegalFormatException(java.util.IllegalFormatException) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) INTEGER(io.trino.spi.type.IntegerType.INTEGER) FunctionMetadata(io.trino.metadata.FunctionMetadata) SMALLINT(io.trino.spi.type.SmallintType.SMALLINT) TypeSignature(io.trino.spi.type.TypeSignature) DateTimes.toLocalDateTime(io.trino.type.DateTimes.toLocalDateTime) FunctionDependencyDeclaration(io.trino.metadata.FunctionDependencyDeclaration) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) PICOSECONDS_PER_NANOSECOND(io.trino.type.DateTimes.PICOSECONDS_PER_NANOSECOND) TrinoException(io.trino.spi.TrinoException) String.format(java.lang.String.format) List(java.util.List) BIGINT(io.trino.spi.type.BigintType.BIGINT) INVALID_FUNCTION_ARGUMENT(io.trino.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) LocalDate(java.time.LocalDate) DecimalType(io.trino.spi.type.DecimalType) NEVER_NULL(io.trino.spi.function.InvocationConvention.InvocationArgumentConvention.NEVER_NULL) DATE(io.trino.spi.type.DateType.DATE) REAL(io.trino.spi.type.RealType.REAL) MethodHandle(java.lang.invoke.MethodHandle) Slice(io.airlift.slice.Slice) TimeType(io.trino.spi.type.TimeType) Decimals.isLongDecimal(io.trino.spi.type.Decimals.isLongDecimal) Type(io.trino.spi.type.Type) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) DateTimes.toZonedDateTime(io.trino.type.DateTimes.toZonedDateTime) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) Chars.padSpaces(io.trino.spi.type.Chars.padSpaces) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.trino.metadata.Signature) Decimals.isShortDecimal(io.trino.spi.type.Decimals.isShortDecimal) Int128(io.trino.spi.type.Int128) Streams.mapWithIndex(com.google.common.collect.Streams.mapWithIndex) ConnectorSession(io.trino.spi.connector.ConnectorSession) Signature.withVariadicBound(io.trino.metadata.Signature.withVariadicBound) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) Failures.internalError(io.trino.util.Failures.internalError) QualifiedName(io.trino.sql.tree.QualifiedName) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) CharType(io.trino.spi.type.CharType) BoundSignature(io.trino.metadata.BoundSignature) TINYINT(io.trino.spi.type.TinyintType.TINYINT) JSON(io.trino.type.JsonType.JSON) Reflection.methodHandle(io.trino.util.Reflection.methodHandle) VarcharType(io.trino.spi.type.VarcharType) BigDecimal(java.math.BigDecimal) TimeType(io.trino.spi.type.TimeType) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) TimestampType(io.trino.spi.type.TimestampType) DecimalType(io.trino.spi.type.DecimalType) Block(io.trino.spi.block.Block) ConnectorSession(io.trino.spi.connector.ConnectorSession) CharType(io.trino.spi.type.CharType) MethodHandle(java.lang.invoke.MethodHandle)

Example 8 with TimestampType

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);
}
Also used : TimestampType(io.trino.spi.type.TimestampType) Test(org.testng.annotations.Test)

Example 9 with TimestampType

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);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) TimestampType(io.trino.spi.type.TimestampType) DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) Test(org.testng.annotations.Test)

Example 10 with TimestampType

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();
}
Also used : TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) DecimalType(io.trino.spi.type.DecimalType) TimestampType(io.trino.spi.type.TimestampType) Int128(io.trino.spi.type.Int128)

Aggregations

TimestampType (io.trino.spi.type.TimestampType)32 DecimalType (io.trino.spi.type.DecimalType)24 VarcharType (io.trino.spi.type.VarcharType)24 CharType (io.trino.spi.type.CharType)23 TrinoException (io.trino.spi.TrinoException)16 TimeType (io.trino.spi.type.TimeType)14 Type (io.trino.spi.type.Type)14 TimestampType.createTimestampType (io.trino.spi.type.TimestampType.createTimestampType)12 DecimalType.createDecimalType (io.trino.spi.type.DecimalType.createDecimalType)10 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)10 ArrayType (io.trino.spi.type.ArrayType)8 List (java.util.List)8 ImmutableList (com.google.common.collect.ImmutableList)7 BIGINT (io.trino.spi.type.BigintType.BIGINT)6 BOOLEAN (io.trino.spi.type.BooleanType.BOOLEAN)6 DATE (io.trino.spi.type.DateType.DATE)6 DOUBLE (io.trino.spi.type.DoubleType.DOUBLE)6 INTEGER (io.trino.spi.type.IntegerType.INTEGER)6 REAL (io.trino.spi.type.RealType.REAL)6 SMALLINT (io.trino.spi.type.SmallintType.SMALLINT)6