use of com.facebook.presto.spi.function.Signature in project presto by prestodb.
the class FunctionSignatureMatcher method identifyApplicableFunctions.
private List<ApplicableFunction> identifyApplicableFunctions(Collection<? extends SqlFunction> candidates, List<TypeSignatureProvider> actualParameters, boolean allowCoercion) {
ImmutableList.Builder<ApplicableFunction> applicableFunctions = ImmutableList.builder();
for (SqlFunction function : candidates) {
Signature declaredSignature = function.getSignature();
Optional<Signature> boundSignature = new SignatureBinder(functionAndTypeManager, declaredSignature, allowCoercion).bind(actualParameters);
if (boundSignature.isPresent()) {
applicableFunctions.add(new ApplicableFunction(declaredSignature, boundSignature.get(), function.isCalledOnNullInput()));
}
}
return applicableFunctions.build();
}
use of com.facebook.presto.spi.function.Signature in project presto by prestodb.
the class SignatureBinder method applyBoundVariables.
public static Signature applyBoundVariables(Signature signature, BoundVariables boundVariables, int arity) {
List<TypeSignature> argumentSignatures;
if (signature.isVariableArity()) {
argumentSignatures = expandVarargFormalTypeSignature(signature.getArgumentTypes(), arity);
} else {
checkArgument(signature.getArgumentTypes().size() == arity);
argumentSignatures = signature.getArgumentTypes();
}
List<TypeSignature> boundArgumentSignatures = applyBoundVariables(argumentSignatures, boundVariables);
TypeSignature boundReturnTypeSignature = applyBoundVariables(signature.getReturnType(), boundVariables);
return new Signature(signature.getName(), signature.getKind(), ImmutableList.of(), ImmutableList.of(), boundReturnTypeSignature, boundArgumentSignatures, false);
}
use of com.facebook.presto.spi.function.Signature in project presto by prestodb.
the class ParametricScalar method specialize.
@Override
public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
Signature boundSignature = applyBoundVariables(getSignature(), boundVariables, arity);
if (implementations.getExactImplementations().containsKey(boundSignature)) {
ParametricScalarImplementation implementation = implementations.getExactImplementations().get(boundSignature);
Optional<BuiltInScalarFunctionImplementation> scalarFunctionImplementation = implementation.specialize(boundSignature, boundVariables, functionAndTypeManager);
checkCondition(scalarFunctionImplementation.isPresent(), FUNCTION_IMPLEMENTATION_ERROR, String.format("Exact implementation of %s do not match expected java types.", boundSignature.getNameSuffix()));
return scalarFunctionImplementation.get();
}
BuiltInScalarFunctionImplementation selectedImplementation = null;
for (ParametricScalarImplementation implementation : implementations.getSpecializedImplementations()) {
Optional<BuiltInScalarFunctionImplementation> scalarFunctionImplementation = implementation.specialize(boundSignature, boundVariables, functionAndTypeManager);
if (scalarFunctionImplementation.isPresent()) {
checkCondition(selectedImplementation == null, AMBIGUOUS_FUNCTION_IMPLEMENTATION, "Ambiguous implementation for %s with bindings %s", getSignature(), boundVariables.getTypeVariables());
selectedImplementation = scalarFunctionImplementation.get();
}
}
if (selectedImplementation != null) {
return selectedImplementation;
}
for (ParametricScalarImplementation implementation : implementations.getGenericImplementations()) {
Optional<BuiltInScalarFunctionImplementation> scalarFunctionImplementation = implementation.specialize(boundSignature, boundVariables, functionAndTypeManager);
if (scalarFunctionImplementation.isPresent()) {
checkCondition(selectedImplementation == null, AMBIGUOUS_FUNCTION_IMPLEMENTATION, "Ambiguous implementation for %s with bindings %s", getSignature(), boundVariables.getTypeVariables());
selectedImplementation = scalarFunctionImplementation.get();
}
}
if (selectedImplementation != null) {
return selectedImplementation;
}
throw new PrestoException(FUNCTION_IMPLEMENTATION_MISSING, format("Unsupported type parameters (%s) for %s", boundVariables, getSignature()));
}
use of com.facebook.presto.spi.function.Signature in project presto by prestodb.
the class TestAnnotationEngineForAggregates method testPartiallyFixedTypeParameterInjectionAggregateFunctionParse.
@Test
public void testPartiallyFixedTypeParameterInjectionAggregateFunctionParse() {
Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "partially_fixed_type_parameter_injection"), FunctionKind.AGGREGATE, ImmutableList.of(typeVariable("T1"), typeVariable("T2")), ImmutableList.of(), DoubleType.DOUBLE.getTypeSignature(), ImmutableList.of(DoubleType.DOUBLE.getTypeSignature()), false);
ParametricAggregation aggregation = parseFunctionDefinition(PartiallyFixedTypeParameterInjectionAggregateFunction.class);
assertEquals(aggregation.getDescription(), "Simple aggregate with fixed parameter type injected");
assertTrue(aggregation.isDeterministic());
assertEquals(aggregation.getSignature(), expectedSignature);
ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
assertImplementationCount(implementations, 0, 0, 1);
AggregationImplementation implementationDouble = implementations.getGenericImplementations().stream().filter(impl -> impl.getStateClass() == NullableDoubleState.class).collect(toImmutableList()).get(0);
assertFalse(implementationDouble.getStateSerializerFactory().isPresent());
assertEquals(implementationDouble.getDefinitionClass(), PartiallyFixedTypeParameterInjectionAggregateFunction.class);
assertDependencyCount(implementationDouble, 1, 1, 1);
assertFalse(implementationDouble.hasSpecializedTypeParameters());
List<AggregationMetadata.ParameterMetadata.ParameterType> expectedMetadataTypes = ImmutableList.of(AggregationMetadata.ParameterMetadata.ParameterType.STATE, AggregationMetadata.ParameterMetadata.ParameterType.INPUT_CHANNEL);
assertTrue(implementationDouble.getInputParameterMetadataTypes().equals(expectedMetadataTypes));
assertEquals(implementationDouble.getStateClass(), NullableDoubleState.class);
InternalAggregationFunction specialized = aggregation.specialize(BoundVariables.builder().setTypeVariable("T1", DoubleType.DOUBLE).setTypeVariable("T2", DoubleType.DOUBLE).build(), 1, FUNCTION_AND_TYPE_MANAGER);
assertEquals(specialized.getFinalType(), DoubleType.DOUBLE);
assertTrue(specialized.getParameterTypes().equals(ImmutableList.of(DoubleType.DOUBLE)));
assertTrue(specialized.isDecomposable());
assertEquals(specialized.name(), "partially_fixed_type_parameter_injection");
}
use of com.facebook.presto.spi.function.Signature in project presto by prestodb.
the class TestAnnotationEngineForAggregates method testStateOnDifferentThanFirstPositionAggregationParse.
@Test
public void testStateOnDifferentThanFirstPositionAggregationParse() {
Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "simple_exact_aggregate_aggregation_state_moved"), FunctionKind.AGGREGATE, DoubleType.DOUBLE.getTypeSignature(), ImmutableList.of(DoubleType.DOUBLE.getTypeSignature()));
ParametricAggregation aggregation = parseFunctionDefinition(StateOnDifferentThanFirstPositionAggregationFunction.class);
assertEquals(aggregation.getSignature(), expectedSignature);
AggregationImplementation implementation = getOnlyElement(aggregation.getImplementations().getExactImplementations().values());
assertEquals(implementation.getDefinitionClass(), StateOnDifferentThanFirstPositionAggregationFunction.class);
List<AggregationMetadata.ParameterMetadata.ParameterType> expectedMetadataTypes = ImmutableList.of(AggregationMetadata.ParameterMetadata.ParameterType.INPUT_CHANNEL, AggregationMetadata.ParameterMetadata.ParameterType.STATE);
assertTrue(implementation.getInputParameterMetadataTypes().equals(expectedMetadataTypes));
}
Aggregations