Search in sources :

Example 1 with ParametricScalar

use of com.facebook.presto.operator.scalar.ParametricScalar in project presto by prestodb.

the class ScalarFromAnnotationsParser method parseParametricScalar.

private static SqlScalarFunction parseParametricScalar(ScalarHeaderAndMethods scalar, Map<Set<TypeParameter>, Constructor<?>> constructors) {
    ImmutableMap.Builder<Signature, ScalarImplementation> exactImplementations = ImmutableMap.builder();
    ImmutableList.Builder<ScalarImplementation> specializedImplementations = ImmutableList.builder();
    ImmutableList.Builder<ScalarImplementation> genericImplementations = ImmutableList.builder();
    Optional<Signature> signature = Optional.empty();
    ScalarImplementationHeader header = scalar.getHeader();
    checkArgument(!header.getName().isEmpty());
    for (Method method : scalar.getMethods()) {
        ScalarImplementation implementation = ScalarImplementation.Parser.parseImplementation(header.getName(), method, constructors);
        if (implementation.getSignature().getTypeVariableConstraints().isEmpty() && implementation.getSignature().getArgumentTypes().stream().noneMatch(TypeSignature::isCalculated) && !implementation.getSignature().getReturnType().isCalculated()) {
            exactImplementations.put(implementation.getSignature(), implementation);
            continue;
        }
        if (implementation.hasSpecializedTypeParameters()) {
            specializedImplementations.add(implementation);
        } else {
            genericImplementations.add(implementation);
        }
        signature = signature.isPresent() ? signature : Optional.of(implementation.getSignature());
        validateSignature(signature, implementation.getSignature());
    }
    Signature scalarSignature = signature.orElseGet(() -> getOnlyElement(exactImplementations.build().keySet()));
    header.getOperatorType().ifPresent(operatorType -> validateOperator(operatorType, scalarSignature.getReturnType(), scalarSignature.getArgumentTypes()));
    ScalarImplementations implementations = new ScalarImplementations(exactImplementations.build(), specializedImplementations.build(), genericImplementations.build());
    return new ParametricScalar(scalarSignature, header.getHeader(), implementations);
}
Also used : TypeSignature(com.facebook.presto.spi.type.TypeSignature) ImmutableList(com.google.common.collect.ImmutableList) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Signature(com.facebook.presto.metadata.Signature) Method(java.lang.reflect.Method) ParametricScalar(com.facebook.presto.operator.scalar.ParametricScalar) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with ParametricScalar

use of com.facebook.presto.operator.scalar.ParametricScalar in project presto by prestodb.

the class TestAnnotationEngineForScalars method testMultiScalarParse.

@Test
public void testMultiScalarParse() {
    Signature expectedSignature1 = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "static_method_scalar_1"), FunctionKind.SCALAR, DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature()));
    Signature expectedSignature2 = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "static_method_scalar_2"), FunctionKind.SCALAR, BIGINT.getTypeSignature(), ImmutableList.of(BIGINT.getTypeSignature()));
    Signature expectedSignature3 = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "static_method_scalar_3"), FunctionKind.SCALAR, BIGINT.getTypeSignature(), ImmutableList.of(BIGINT.getTypeSignature()));
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinitions(MultiScalarFunction.class);
    assertEquals(functions.size(), 3);
    ParametricScalar scalar1 = (ParametricScalar) functions.stream().filter(signature -> signature.getSignature().equals(expectedSignature1)).collect(toImmutableList()).get(0);
    ParametricScalar scalar2 = (ParametricScalar) functions.stream().filter(signature -> signature.getSignature().equals(expectedSignature2)).collect(toImmutableList()).get(0);
    ParametricScalar scalar3 = (ParametricScalar) functions.stream().filter(signature -> signature.getSignature().equals(expectedSignature3)).collect(toImmutableList()).get(0);
    assertImplementationCount(scalar1, 1, 0, 0);
    assertImplementationCount(scalar2, 1, 0, 0);
    assertEquals(scalar1.getSignature(), expectedSignature1);
    assertTrue(scalar1.isDeterministic());
    assertEquals(scalar1.getVisibility(), PUBLIC);
    assertEquals(scalar1.getDescription(), "Simple scalar with single implementation based on method 1");
    assertEquals(scalar2.getSignature(), expectedSignature2);
    assertFalse(scalar2.isDeterministic());
    assertEquals(scalar2.getVisibility(), HIDDEN);
    assertEquals(scalar2.getDescription(), "Simple scalar with single implementation based on method 2");
    assertEquals(scalar3.getSignature(), expectedSignature3);
    assertFalse(scalar3.isDeterministic());
    assertEquals(scalar3.getVisibility(), EXPERIMENTAL);
    assertEquals(scalar3.getDescription(), "Simple scalar with single implementation based on method 3");
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) EXPERIMENTAL(com.facebook.presto.spi.function.SqlFunctionVisibility.EXPERIMENTAL) StandardTypes(com.facebook.presto.common.type.StandardTypes) TypeParameter(com.facebook.presto.spi.function.TypeParameter) FunctionKind(com.facebook.presto.spi.function.FunctionKind) Slice(io.airlift.slice.Slice) ParametricScalar(com.facebook.presto.operator.scalar.ParametricScalar) ParametricScalarImplementationChoice(com.facebook.presto.operator.scalar.annotations.ParametricScalarImplementation.ParametricScalarImplementationChoice) TypeImplementationDependency(com.facebook.presto.operator.annotations.TypeImplementationDependency) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) DEFAULT_NAMESPACE(com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE) Signature.typeVariable(com.facebook.presto.spi.function.Signature.typeVariable) ImmutableList(com.google.common.collect.ImmutableList) Description(com.facebook.presto.spi.function.Description) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) RETURN_NULL_ON_NULL(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.RETURN_NULL_ON_NULL) Assert.assertFalse(org.testng.Assert.assertFalse) Type(com.facebook.presto.common.type.Type) LiteralParameter(com.facebook.presto.type.LiteralParameter) BuiltInScalarFunctionImplementation(com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) ImmutableSet(com.google.common.collect.ImmutableSet) BoundVariables(com.facebook.presto.metadata.BoundVariables) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) HIDDEN(com.facebook.presto.spi.function.SqlFunctionVisibility.HIDDEN) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) USE_BOXED_TYPE(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.USE_BOXED_TYPE) USE_NULL_FLAG(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.USE_NULL_FLAG) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) ArgumentProperty.valueTypeArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty.valueTypeArgumentProperty) PUBLIC(com.facebook.presto.spi.function.SqlFunctionVisibility.PUBLIC) List(java.util.List) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) ScalarFromAnnotationsParser(com.facebook.presto.operator.scalar.annotations.ScalarFromAnnotationsParser) IsNull(com.facebook.presto.spi.function.IsNull) SqlNullable(com.facebook.presto.spi.function.SqlNullable) Signature(com.facebook.presto.spi.function.Signature) Assert.assertTrue(org.testng.Assert.assertTrue) Block(com.facebook.presto.common.block.Block) ImplementationDependency(com.facebook.presto.operator.annotations.ImplementationDependency) LiteralImplementationDependency(com.facebook.presto.operator.annotations.LiteralImplementationDependency) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) ParametricScalar(com.facebook.presto.operator.scalar.ParametricScalar) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) Test(org.testng.annotations.Test)

Example 3 with ParametricScalar

use of com.facebook.presto.operator.scalar.ParametricScalar in project presto by prestodb.

the class TestAnnotationEngineForScalars method testPartiallyFixedTypeParameterParse.

@Test
public void testPartiallyFixedTypeParameterParse() {
    Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "partially_fixed_type_parameter_scalar_function"), FunctionKind.SCALAR, ImmutableList.of(typeVariable("T1"), typeVariable("T2")), ImmutableList.of(), BIGINT.getTypeSignature(), ImmutableList.of(BIGINT.getTypeSignature()), false);
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(PartiallyFixedTypeParameterScalarFunction.class);
    assertEquals(functions.size(), 1);
    ParametricScalar scalar = (ParametricScalar) functions.get(0);
    assertImplementationCount(scalar, 0, 0, 1);
    List<ParametricScalarImplementationChoice> parametricScalarImplementationChoices = scalar.getImplementations().getGenericImplementations().get(0).getChoices();
    assertEquals(parametricScalarImplementationChoices.size(), 1);
    List<ImplementationDependency> dependencies = parametricScalarImplementationChoices.get(0).getDependencies();
    assertEquals(dependencies.size(), 1);
    assertEquals(scalar.getSignature(), expectedSignature);
    assertTrue(scalar.isDeterministic());
    assertEquals(scalar.getVisibility(), PUBLIC);
    assertEquals(scalar.getDescription(), "Parametric scalar that uses TypeParameter with partially fixed type");
}
Also used : TypeImplementationDependency(com.facebook.presto.operator.annotations.TypeImplementationDependency) ImplementationDependency(com.facebook.presto.operator.annotations.ImplementationDependency) LiteralImplementationDependency(com.facebook.presto.operator.annotations.LiteralImplementationDependency) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) ParametricScalarImplementationChoice(com.facebook.presto.operator.scalar.annotations.ParametricScalarImplementation.ParametricScalarImplementationChoice) ParametricScalar(com.facebook.presto.operator.scalar.ParametricScalar) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) Test(org.testng.annotations.Test)

Example 4 with ParametricScalar

use of com.facebook.presto.operator.scalar.ParametricScalar in project presto by prestodb.

the class TestAnnotationEngineForScalars method testSimpleInjectionScalarParse.

@Test
public void testSimpleInjectionScalarParse() {
    Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "parametric_scalar_inject"), FunctionKind.SCALAR, ImmutableList.of(), ImmutableList.of(), BIGINT.getTypeSignature(), ImmutableList.of(parseTypeSignature("varchar(x)", ImmutableSet.of("x"))), false);
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(SimpleInjectionScalarFunction.class);
    assertEquals(functions.size(), 1);
    ParametricScalar scalar = (ParametricScalar) functions.get(0);
    assertImplementationCount(scalar, 0, 0, 1);
    List<ParametricScalarImplementationChoice> parametricScalarImplementationChoices = scalar.getImplementations().getGenericImplementations().get(0).getChoices();
    assertEquals(parametricScalarImplementationChoices.size(), 1);
    List<ImplementationDependency> dependencies = parametricScalarImplementationChoices.get(0).getDependencies();
    assertEquals(dependencies.size(), 1);
    assertTrue(dependencies.get(0) instanceof LiteralImplementationDependency);
    assertEquals(scalar.getSignature(), expectedSignature);
    assertTrue(scalar.isDeterministic());
    assertEquals(scalar.getVisibility(), PUBLIC);
    assertEquals(scalar.getDescription(), "Parametric scalar with literal injected");
}
Also used : TypeImplementationDependency(com.facebook.presto.operator.annotations.TypeImplementationDependency) ImplementationDependency(com.facebook.presto.operator.annotations.ImplementationDependency) LiteralImplementationDependency(com.facebook.presto.operator.annotations.LiteralImplementationDependency) LiteralImplementationDependency(com.facebook.presto.operator.annotations.LiteralImplementationDependency) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) ParametricScalarImplementationChoice(com.facebook.presto.operator.scalar.annotations.ParametricScalarImplementation.ParametricScalarImplementationChoice) ParametricScalar(com.facebook.presto.operator.scalar.ParametricScalar) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) Test(org.testng.annotations.Test)

Example 5 with ParametricScalar

use of com.facebook.presto.operator.scalar.ParametricScalar in project presto by prestodb.

the class TestAnnotationEngineForScalars method testWithNullablePrimitiveArgScalarParse.

@Test
public void testWithNullablePrimitiveArgScalarParse() {
    Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "scalar_with_nullable"), FunctionKind.SCALAR, DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature(), DOUBLE.getTypeSignature()));
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(WithNullablePrimitiveArgScalarFunction.class);
    assertEquals(functions.size(), 1);
    ParametricScalar scalar = (ParametricScalar) functions.get(0);
    assertEquals(scalar.getSignature(), expectedSignature);
    assertTrue(scalar.isDeterministic());
    assertEquals(scalar.getVisibility(), PUBLIC);
    assertEquals(scalar.getDescription(), "Simple scalar with nullable primitive");
    BuiltInScalarFunctionImplementation specialized = scalar.specialize(BoundVariables.builder().build(), 2, FUNCTION_AND_TYPE_MANAGER);
    assertFalse(specialized.getInstanceFactory().isPresent());
    assertEquals(specialized.getArgumentProperty(0), valueTypeArgumentProperty(RETURN_NULL_ON_NULL));
    assertEquals(specialized.getArgumentProperty(1), valueTypeArgumentProperty(USE_NULL_FLAG));
}
Also used : BuiltInScalarFunctionImplementation(com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) ParametricScalar(com.facebook.presto.operator.scalar.ParametricScalar) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) Test(org.testng.annotations.Test)

Aggregations

ParametricScalar (com.facebook.presto.operator.scalar.ParametricScalar)16 SqlScalarFunction (com.facebook.presto.metadata.SqlScalarFunction)14 Test (org.testng.annotations.Test)14 Signature (com.facebook.presto.spi.function.Signature)12 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)11 ImplementationDependency (com.facebook.presto.operator.annotations.ImplementationDependency)4 LiteralImplementationDependency (com.facebook.presto.operator.annotations.LiteralImplementationDependency)4 TypeImplementationDependency (com.facebook.presto.operator.annotations.TypeImplementationDependency)4 BuiltInScalarFunctionImplementation (com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation)4 ParametricScalarImplementationChoice (com.facebook.presto.operator.scalar.annotations.ParametricScalarImplementation.ParametricScalarImplementationChoice)4 ImmutableList (com.google.common.collect.ImmutableList)2 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)1 Block (com.facebook.presto.common.block.Block)1 BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)1 BOOLEAN (com.facebook.presto.common.type.BooleanType.BOOLEAN)1 DOUBLE (com.facebook.presto.common.type.DoubleType.DOUBLE)1 StandardTypes (com.facebook.presto.common.type.StandardTypes)1 Type (com.facebook.presto.common.type.Type)1 BoundVariables (com.facebook.presto.metadata.BoundVariables)1 DEFAULT_NAMESPACE (com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE)1