Search in sources :

Example 41 with Signature

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();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Signature(com.facebook.presto.spi.function.Signature) SqlFunction(com.facebook.presto.spi.function.SqlFunction)

Example 42 with Signature

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);
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) Signature(com.facebook.presto.spi.function.Signature)

Example 43 with Signature

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()));
}
Also used : ParametricScalarImplementation(com.facebook.presto.operator.scalar.annotations.ParametricScalarImplementation) Signature(com.facebook.presto.spi.function.Signature) PrestoException(com.facebook.presto.spi.PrestoException)

Example 44 with Signature

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");
}
Also used : AggregationImplementation(com.facebook.presto.operator.aggregation.AggregationImplementation) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) AggregationMetadata(com.facebook.presto.operator.aggregation.AggregationMetadata) ParametricAggregation(com.facebook.presto.operator.aggregation.ParametricAggregation) NullableDoubleState(com.facebook.presto.operator.aggregation.state.NullableDoubleState) InternalAggregationFunction(com.facebook.presto.operator.aggregation.InternalAggregationFunction) Test(org.testng.annotations.Test)

Example 45 with Signature

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));
}
Also used : AggregationImplementation(com.facebook.presto.operator.aggregation.AggregationImplementation) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) AggregationMetadata(com.facebook.presto.operator.aggregation.AggregationMetadata) ParametricAggregation(com.facebook.presto.operator.aggregation.ParametricAggregation) Test(org.testng.annotations.Test)

Aggregations

Signature (com.facebook.presto.spi.function.Signature)90 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)79 TypeSignature (com.facebook.presto.common.type.TypeSignature)73 Test (org.testng.annotations.Test)64 SqlScalarFunction (com.facebook.presto.metadata.SqlScalarFunction)16 ParametricAggregation (com.facebook.presto.operator.aggregation.ParametricAggregation)14 ImmutableList (com.google.common.collect.ImmutableList)14 AggregationImplementation (com.facebook.presto.operator.aggregation.AggregationImplementation)13 AggregationMetadata (com.facebook.presto.operator.aggregation.AggregationMetadata)13 StandardTypes (com.facebook.presto.common.type.StandardTypes)12 InternalAggregationFunction (com.facebook.presto.operator.aggregation.InternalAggregationFunction)12 ParametricScalar (com.facebook.presto.operator.scalar.ParametricScalar)12 PrestoException (com.facebook.presto.spi.PrestoException)12 SCALAR (com.facebook.presto.spi.function.FunctionKind.SCALAR)12 ImmutableSet (com.google.common.collect.ImmutableSet)12 Slice (io.airlift.slice.Slice)12 BuiltInScalarFunctionImplementation (com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation)11 ADD (com.facebook.presto.common.function.OperatorType.ADD)10 FunctionAndTypeManager.createTestFunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager)10 Math.toIntExact (java.lang.Math.toIntExact)10