Search in sources :

Example 11 with BoundSignature

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

the class TestAnnotationEngineForAggregates method testInjectTypeAggregateParse.

@Test
public void testInjectTypeAggregateParse() {
    Signature expectedSignature = new Signature("inject_type_aggregate", ImmutableList.of(typeVariable("T")), ImmutableList.of(), new TypeSignature("T"), ImmutableList.of(new TypeSignature("T")), false);
    ParametricAggregation aggregation = getOnlyElement(parseFunctionDefinitions(InjectTypeAggregateFunction.class));
    assertEquals(aggregation.getFunctionMetadata().getDescription(), "Simple aggregate with type injected");
    assertTrue(aggregation.getFunctionMetadata().isDeterministic());
    assertEquals(aggregation.getFunctionMetadata().getSignature(), expectedSignature);
    ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
    assertEquals(implementations.getGenericImplementations().size(), 1);
    AggregationImplementation implementation = implementations.getGenericImplementations().get(0);
    assertEquals(implementation.getDefinitionClass(), InjectTypeAggregateFunction.class);
    assertDependencyCount(implementation, 1, 1, 1);
    assertTrue(implementation.getInputDependencies().get(0) instanceof TypeImplementationDependency);
    assertTrue(implementation.getCombineDependencies().get(0) instanceof TypeImplementationDependency);
    assertTrue(implementation.getOutputDependencies().get(0) instanceof TypeImplementationDependency);
    assertFalse(implementation.hasSpecializedTypeParameters());
    assertEquals(implementation.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL));
    BoundSignature boundSignature = new BoundSignature(aggregation.getFunctionMetadata().getSignature().getName(), DoubleType.DOUBLE, ImmutableList.of(DoubleType.DOUBLE));
    specializeAggregationFunction(boundSignature, aggregation);
}
Also used : AggregationImplementation(io.trino.operator.aggregation.AggregationImplementation) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) BoundSignature(io.trino.metadata.BoundSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricAggregation(io.trino.operator.aggregation.ParametricAggregation) TypeImplementationDependency(io.trino.operator.annotations.TypeImplementationDependency) Test(org.testng.annotations.Test)

Example 12 with BoundSignature

use of io.trino.metadata.BoundSignature 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)

Example 13 with BoundSignature

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

the class FormatFunction method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundSignature boundSignature, FunctionDependencies functionDependencies) {
    Type rowType = boundSignature.getArgumentType(1);
    List<BiFunction<ConnectorSession, Block, Object>> converters = mapWithIndex(rowType.getTypeParameters().stream(), (type, index) -> converter(functionDependencies, type, toIntExact(index))).collect(toImmutableList());
    return new ChoicesScalarFunctionImplementation(boundSignature, FAIL_ON_NULL, ImmutableList.of(NEVER_NULL, NEVER_NULL), METHOD_HANDLE.bindTo(converters));
}
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) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) DecimalType(io.trino.spi.type.DecimalType) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) CharType(io.trino.spi.type.CharType) BiFunction(java.util.function.BiFunction)

Example 14 with BoundSignature

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

the class TestParametricScalarImplementationValidation method testConnectorSessionPosition.

@Test
public void testConnectorSessionPosition() {
    // Without cached instance factory
    MethodHandle validFunctionMethodHandle = methodHandle(TestParametricScalarImplementationValidation.class, "validConnectorSessionParameterPosition", ConnectorSession.class, long.class, long.class);
    ChoicesScalarFunctionImplementation validFunction = new ChoicesScalarFunctionImplementation(new BoundSignature("test", BIGINT, ImmutableList.of(BIGINT, BIGINT)), FAIL_ON_NULL, ImmutableList.of(NEVER_NULL, NEVER_NULL), validFunctionMethodHandle);
    assertEquals(validFunction.getChoices().get(0).getMethodHandle(), validFunctionMethodHandle);
    assertThatThrownBy(() -> new ChoicesScalarFunctionImplementation(new BoundSignature("test", BIGINT, ImmutableList.of(BIGINT, BIGINT)), FAIL_ON_NULL, ImmutableList.of(NEVER_NULL, NEVER_NULL), methodHandle(TestParametricScalarImplementationValidation.class, "invalidConnectorSessionParameterPosition", long.class, long.class, ConnectorSession.class))).isInstanceOf(IllegalArgumentException.class).hasMessage("ConnectorSession must be the first argument when instanceFactory is not present");
    // With cached instance factory
    MethodHandle validFunctionWithInstanceFactoryMethodHandle = methodHandle(TestParametricScalarImplementationValidation.class, "validConnectorSessionParameterPosition", Object.class, ConnectorSession.class, long.class, long.class);
    ChoicesScalarFunctionImplementation validFunctionWithInstanceFactory = new ChoicesScalarFunctionImplementation(new BoundSignature("test", BIGINT, ImmutableList.of(BIGINT, BIGINT)), FAIL_ON_NULL, ImmutableList.of(NEVER_NULL, NEVER_NULL), validFunctionWithInstanceFactoryMethodHandle, Optional.of(STATE_FACTORY));
    assertEquals(validFunctionWithInstanceFactory.getChoices().get(0).getMethodHandle(), validFunctionWithInstanceFactoryMethodHandle);
    assertThatThrownBy(() -> new ChoicesScalarFunctionImplementation(new BoundSignature("test", BIGINT, ImmutableList.of(BIGINT, BIGINT)), FAIL_ON_NULL, ImmutableList.of(NEVER_NULL, NEVER_NULL), methodHandle(TestParametricScalarImplementationValidation.class, "invalidConnectorSessionParameterPosition", Object.class, long.class, long.class, ConnectorSession.class), Optional.of(STATE_FACTORY))).isInstanceOf(IllegalArgumentException.class).hasMessage("ConnectorSession must be the second argument when instanceFactory is present");
}
Also used : BoundSignature(io.trino.metadata.BoundSignature) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 15 with BoundSignature

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

the class PushAggregationIntoTableScan method toAggregateFunction.

private static AggregateFunction toAggregateFunction(Metadata metadata, Context context, AggregationNode.Aggregation aggregation) {
    String canonicalName = metadata.getFunctionMetadata(aggregation.getResolvedFunction()).getCanonicalName();
    BoundSignature signature = aggregation.getResolvedFunction().getSignature();
    ImmutableList.Builder<ConnectorExpression> arguments = ImmutableList.builder();
    for (int i = 0; i < aggregation.getArguments().size(); i++) {
        SymbolReference argument = (SymbolReference) aggregation.getArguments().get(i);
        arguments.add(new Variable(argument.getName(), signature.getArgumentTypes().get(i)));
    }
    Optional<OrderingScheme> orderingScheme = aggregation.getOrderingScheme();
    Optional<List<SortItem>> sortBy = orderingScheme.map(OrderingScheme::toSortItems);
    Optional<ConnectorExpression> filter = aggregation.getFilter().map(symbol -> new Variable(symbol.getName(), context.getSymbolAllocator().getTypes().get(symbol)));
    return new AggregateFunction(canonicalName, signature.getReturnType(), arguments.build(), sortBy.orElse(ImmutableList.of()), aggregation.isDistinct(), filter);
}
Also used : OrderingScheme(io.trino.sql.planner.OrderingScheme) Variable(io.trino.spi.expression.Variable) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ConnectorExpression(io.trino.spi.expression.ConnectorExpression) SymbolReference(io.trino.sql.tree.SymbolReference) BoundSignature(io.trino.metadata.BoundSignature) AggregateFunction(io.trino.spi.connector.AggregateFunction) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List)

Aggregations

BoundSignature (io.trino.metadata.BoundSignature)27 Signature (io.trino.metadata.Signature)20 TypeSignature (io.trino.spi.type.TypeSignature)18 Test (org.testng.annotations.Test)17 ParametricAggregation (io.trino.operator.aggregation.ParametricAggregation)13 AggregationFunctionMetadata (io.trino.metadata.AggregationFunctionMetadata)12 AggregationImplementation (io.trino.operator.aggregation.AggregationImplementation)12 MethodHandle (java.lang.invoke.MethodHandle)8 FunctionMetadata (io.trino.metadata.FunctionMetadata)7 ImmutableList (com.google.common.collect.ImmutableList)6 FunctionDependencies (io.trino.metadata.FunctionDependencies)6 List (java.util.List)6 SqlScalarFunction (io.trino.metadata.SqlScalarFunction)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 FunctionNullability (io.trino.metadata.FunctionNullability)4 Type (io.trino.spi.type.Type)4 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)3 FunctionDependencyDeclaration (io.trino.metadata.FunctionDependencyDeclaration)3 ChoicesScalarFunctionImplementation (io.trino.operator.scalar.ChoicesScalarFunctionImplementation)3 TrinoException (io.trino.spi.TrinoException)3