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