Search in sources :

Example 1 with FunctionNullability

use of io.trino.metadata.FunctionNullability in project trino by trinodb.

the class MapToMapCast method buildProcessor.

/**
 * The signature of the returned MethodHandle is (Block fromMap, int position, ConnectorSession session, BlockBuilder mapBlockBuilder)void.
 * The processor will get the value from fromMap, cast it and write to toBlock.
 */
private MethodHandle buildProcessor(FunctionDependencies functionDependencies, Type fromType, Type toType, boolean isKey) {
    // Get block position cast, with optional connector session
    FunctionNullability functionNullability = functionDependencies.getCastNullability(fromType, toType);
    InvocationConvention invocationConvention = new InvocationConvention(ImmutableList.of(BLOCK_POSITION), functionNullability.isReturnNullable() ? NULLABLE_RETURN : FAIL_ON_NULL, true, false);
    MethodHandle cast = functionDependencies.getCastInvoker(fromType, toType, invocationConvention).getMethodHandle();
    // Normalize cast to have connector session as first argument
    if (cast.type().parameterArray()[0] != ConnectorSession.class) {
        cast = MethodHandles.dropArguments(cast, 0, ConnectorSession.class);
    }
    // Change cast signature to (Block.class, int.class, ConnectorSession.class):T
    cast = permuteArguments(cast, methodType(cast.type().returnType(), Block.class, int.class, ConnectorSession.class), 2, 0, 1);
    // If the key cast function is nullable, check the result is not null
    if (isKey && functionNullability.isReturnNullable()) {
        cast = compose(nullChecker(cast.type().returnType()), cast);
    }
    // get write method with signature: (T, BlockBuilder.class):void
    MethodHandle writer = nativeValueWriter(toType);
    writer = permuteArguments(writer, methodType(void.class, writer.type().parameterArray()[1], BlockBuilder.class), 1, 0);
    // ensure cast returns type expected by the writer
    cast = cast.asType(methodType(writer.type().parameterType(0), cast.type().parameterArray()));
    return compose(writer, cast);
}
Also used : InvocationConvention(io.trino.spi.function.InvocationConvention) ConnectorSession(io.trino.spi.connector.ConnectorSession) FunctionNullability(io.trino.metadata.FunctionNullability) MethodHandle(java.lang.invoke.MethodHandle)

Example 2 with FunctionNullability

use of io.trino.metadata.FunctionNullability in project trino by trinodb.

the class TestAccumulatorCompiler method assertGenerateAccumulator.

private static <S extends AccumulatorState, A> void assertGenerateAccumulator(Class<A> aggregation, Class<S> stateInterface) {
    AccumulatorStateSerializer<S> stateSerializer = StateCompiler.generateStateSerializer(stateInterface);
    AccumulatorStateFactory<S> stateFactory = StateCompiler.generateStateFactory(stateInterface);
    BoundSignature signature = new BoundSignature("longTimestampAggregation", RealType.REAL, ImmutableList.of(TIMESTAMP_PICOS));
    MethodHandle inputFunction = methodHandle(aggregation, "input", stateInterface, LongTimestamp.class);
    inputFunction = normalizeInputMethod(inputFunction, signature, STATE, INPUT_CHANNEL);
    MethodHandle combineFunction = methodHandle(aggregation, "combine", stateInterface, stateInterface);
    MethodHandle outputFunction = methodHandle(aggregation, "output", stateInterface, BlockBuilder.class);
    AggregationMetadata metadata = new AggregationMetadata(inputFunction, Optional.empty(), Optional.of(combineFunction), outputFunction, ImmutableList.of(new AggregationMetadata.AccumulatorStateDescriptor<>(stateInterface, stateSerializer, stateFactory)));
    FunctionNullability functionNullability = new FunctionNullability(false, ImmutableList.of(false));
    // test if we can compile aggregation
    AccumulatorFactory accumulatorFactory = AccumulatorCompiler.generateAccumulatorFactory(signature, metadata, functionNullability);
    assertThat(accumulatorFactory).isNotNull();
    assertThat(AccumulatorCompiler.generateWindowAccumulatorClass(signature, metadata, functionNullability)).isNotNull();
    TestingAggregationFunction aggregationFunction = new TestingAggregationFunction(ImmutableList.of(TIMESTAMP_PICOS), ImmutableList.of(BIGINT), BIGINT, accumulatorFactory);
    assertThat(AggregationTestUtils.aggregation(aggregationFunction, createPage(1234))).isEqualTo(1234L);
}
Also used : TIMESTAMP_PICOS(io.trino.spi.type.TimestampType.TIMESTAMP_PICOS) BoundSignature(io.trino.metadata.BoundSignature) FunctionNullability(io.trino.metadata.FunctionNullability) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

FunctionNullability (io.trino.metadata.FunctionNullability)2 MethodHandle (java.lang.invoke.MethodHandle)2 BoundSignature (io.trino.metadata.BoundSignature)1 ConnectorSession (io.trino.spi.connector.ConnectorSession)1 InvocationConvention (io.trino.spi.function.InvocationConvention)1 TIMESTAMP_PICOS (io.trino.spi.type.TimestampType.TIMESTAMP_PICOS)1