use of io.trino.metadata.BoundSignature in project trino by trinodb.
the class SimplifyCountOverConstant method isCountOverConstant.
private boolean isCountOverConstant(Session session, AggregationNode.Aggregation aggregation, Assignments inputs) {
BoundSignature signature = aggregation.getResolvedFunction().getSignature();
if (!signature.getName().equals("count") || signature.getArgumentTypes().size() != 1) {
return false;
}
Expression argument = aggregation.getArguments().get(0);
if (argument instanceof SymbolReference) {
argument = inputs.get(Symbol.from(argument));
}
if (isEffectivelyLiteral(plannerContext, session, argument)) {
Object value = evaluateConstantExpression(argument, ImmutableMap.of(), ImmutableSet.of(), plannerContext, session, new AllowAllAccessControl(), ImmutableSet.of(), ImmutableMap.of());
verify(!(value instanceof Expression));
return value != null;
}
return false;
}
use of io.trino.metadata.BoundSignature in project trino by trinodb.
the class AbstractGreatestLeast method specialize.
@Override
public ScalarFunctionImplementation specialize(BoundSignature boundSignature, FunctionDependencies functionDependencies) {
Type type = boundSignature.getReturnType();
checkArgument(type.isOrderable(), "Type must be orderable");
MethodHandle compareMethod = getMinMaxCompare(functionDependencies, type, simpleConvention(FAIL_ON_NULL, NEVER_NULL, NEVER_NULL), min);
List<Class<?>> javaTypes = IntStream.range(0, boundSignature.getArity()).mapToObj(i -> wrap(type.getJavaType())).collect(toImmutableList());
Class<?> clazz = generate(javaTypes, compareMethod);
MethodHandle methodHandle = methodHandle(clazz, getFunctionMetadata().getSignature().getName(), javaTypes.toArray(new Class<?>[0]));
return new ChoicesScalarFunctionImplementation(boundSignature, NULLABLE_RETURN, nCopies(javaTypes.size(), BOXED_NULLABLE), methodHandle);
}
use of io.trino.metadata.BoundSignature in project trino by trinodb.
the class ParametricAggregation method specialize.
@Override
public AggregationMetadata specialize(BoundSignature boundSignature, FunctionDependencies functionDependencies) {
// Find implementation matching arguments
AggregationImplementation concreteImplementation = findMatchingImplementation(boundSignature);
// Build state factory and serializer
AccumulatorStateDescriptor<?> accumulatorStateDescriptor = generateAccumulatorStateDescriptor(stateClass);
// Bind provided dependencies to aggregation method handlers
FunctionMetadata metadata = getFunctionMetadata();
FunctionBinding functionBinding = SignatureBinder.bindFunction(metadata.getFunctionId(), metadata.getSignature(), boundSignature);
MethodHandle inputHandle = bindDependencies(concreteImplementation.getInputFunction(), concreteImplementation.getInputDependencies(), functionBinding, functionDependencies);
Optional<MethodHandle> removeInputHandle = concreteImplementation.getRemoveInputFunction().map(removeInputFunction -> bindDependencies(removeInputFunction, concreteImplementation.getRemoveInputDependencies(), functionBinding, functionDependencies));
Optional<MethodHandle> combineHandle = concreteImplementation.getCombineFunction();
if (getAggregationMetadata().isDecomposable()) {
checkArgument(combineHandle.isPresent(), "Decomposable method %s does not have a combine method", boundSignature.getName());
combineHandle = combineHandle.map(combineFunction -> bindDependencies(combineFunction, concreteImplementation.getCombineDependencies(), functionBinding, functionDependencies));
} else {
checkArgument(concreteImplementation.getCombineFunction().isEmpty(), "Decomposable method %s does not have a combine method", boundSignature.getName());
}
MethodHandle outputHandle = bindDependencies(concreteImplementation.getOutputFunction(), concreteImplementation.getOutputDependencies(), functionBinding, functionDependencies);
List<AggregationParameterKind> inputParameterKinds = concreteImplementation.getInputParameterKinds();
inputHandle = normalizeInputMethod(inputHandle, boundSignature, inputParameterKinds);
removeInputHandle = removeInputHandle.map(function -> normalizeInputMethod(function, boundSignature, inputParameterKinds));
return new AggregationMetadata(inputHandle, removeInputHandle, combineHandle, outputHandle, ImmutableList.of(accumulatorStateDescriptor));
}
use of io.trino.metadata.BoundSignature in project trino by trinodb.
the class QuantileDigestAggregationFunction method specialize.
@Override
public AggregationMetadata specialize(BoundSignature boundSignature) {
QuantileDigestType outputType = (QuantileDigestType) boundSignature.getReturnType();
Type valueType = outputType.getValueType();
int arity = boundSignature.getArity();
QuantileDigestStateSerializer stateSerializer = new QuantileDigestStateSerializer(valueType);
MethodHandle inputFunction = getMethodHandle(valueType, arity);
inputFunction = normalizeInputMethod(inputFunction, boundSignature, ImmutableList.<AggregationParameterKind>builder().add(STATE).addAll(getInputTypes(valueType, arity).stream().map(ignored -> INPUT_CHANNEL).collect(Collectors.toList())).build());
return new AggregationMetadata(inputFunction, Optional.empty(), Optional.of(COMBINE_FUNCTION), OUTPUT_FUNCTION.bindTo(stateSerializer), ImmutableList.of(new AccumulatorStateDescriptor<>(QuantileDigestState.class, stateSerializer, new QuantileDigestStateFactory())));
}
use of io.trino.metadata.BoundSignature in project trino by trinodb.
the class TestAnnotationEngineForAggregates method testSimpleBlockInputAggregationParse.
@Test
public void testSimpleBlockInputAggregationParse() {
Signature expectedSignature = new Signature("block_input_aggregate", DoubleType.DOUBLE.getTypeSignature(), ImmutableList.of(DoubleType.DOUBLE.getTypeSignature()));
ParametricAggregation aggregation = getOnlyElement(parseFunctionDefinitions(BlockInputAggregationFunction.class));
assertEquals(aggregation.getFunctionMetadata().getDescription(), "Simple aggregate with @BlockPosition usage");
assertTrue(aggregation.getFunctionMetadata().isDeterministic());
assertEquals(aggregation.getFunctionMetadata().getSignature(), expectedSignature);
ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
assertImplementationCount(implementations, 1, 0, 0);
AggregationImplementation implementation = getOnlyElement(implementations.getExactImplementations().values());
assertEquals(implementation.getDefinitionClass(), BlockInputAggregationFunction.class);
assertDependencyCount(implementation, 0, 0, 0);
assertFalse(implementation.hasSpecializedTypeParameters());
assertEquals(implementation.getInputParameterKinds(), ImmutableList.of(STATE, BLOCK_INPUT_CHANNEL, BLOCK_INDEX));
BoundSignature boundSignature = new BoundSignature(aggregation.getFunctionMetadata().getSignature().getName(), DoubleType.DOUBLE, ImmutableList.of(DoubleType.DOUBLE));
AggregationFunctionMetadata aggregationMetadata = aggregation.getAggregationMetadata();
assertFalse(aggregationMetadata.isOrderSensitive());
assertFalse(aggregationMetadata.getIntermediateTypes().isEmpty());
aggregation.specialize(boundSignature, NO_FUNCTION_DEPENDENCIES);
}
Aggregations