Search in sources :

Example 11 with SqlScalarFunction

use of com.facebook.presto.metadata.SqlScalarFunction in project presto by prestodb.

the class CodegenScalarFromAnnotationsParser method createSqlScalarFunction.

private static SqlScalarFunction createSqlScalarFunction(Method method) {
    CodegenScalarFunction codegenScalarFunction = method.getAnnotation(CodegenScalarFunction.class);
    Signature signature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, codegenScalarFunction.value()), FunctionKind.SCALAR, Arrays.stream(method.getAnnotationsByType(TypeParameter.class)).map(t -> withVariadicBound(t.value(), t.boundedBy().isEmpty() ? null : t.boundedBy())).collect(toImmutableList()), ImmutableList.of(), parseTypeSignature(method.getAnnotation(SqlType.class).value()), Arrays.stream(method.getParameters()).map(p -> parseTypeSignature(p.getAnnotation(SqlType.class).value())).collect(toImmutableList()), false);
    return new SqlScalarFunction(signature) {

        @Override
        public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
            Signature boundSignature = applyBoundVariables(signature, boundVariables, arity);
            MethodHandle handle;
            try {
                handle = (MethodHandle) method.invoke(null, boundSignature.getArgumentTypes().stream().map(t -> functionAndTypeManager.getType(t)).toArray());
            } catch (Exception e) {
                throw new PrestoException(FUNCTION_IMPLEMENTATION_ERROR, format("Method %s does not return valid MethodHandle", method), e);
            }
            return new BuiltInScalarFunctionImplementation(method.getAnnotation(SqlNullable.class) != null, getArgumentProperties(method), handle, Optional.empty());
        }

        @Override
        public SqlFunctionVisibility getVisibility() {
            return codegenScalarFunction.visibility();
        }

        @Override
        public boolean isDeterministic() {
            return codegenScalarFunction.deterministic();
        }

        @Override
        public String getDescription() {
            Description description = method.getAnnotation(Description.class);
            return description == null ? "" : description.value();
        }

        @Override
        public boolean isCalledOnNullInput() {
            return codegenScalarFunction.calledOnNullInput();
        }
    };
}
Also used : SignatureBinder.applyBoundVariables(com.facebook.presto.metadata.SignatureBinder.applyBoundVariables) BoundVariables(com.facebook.presto.metadata.BoundVariables) Description(com.facebook.presto.spi.function.Description) CodegenScalarFunction(com.facebook.presto.spi.function.CodegenScalarFunction) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) BuiltInScalarFunctionImplementation(com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) SqlType(com.facebook.presto.spi.function.SqlType) PrestoException(com.facebook.presto.spi.PrestoException) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) PrestoException(com.facebook.presto.spi.PrestoException) MethodHandle(java.lang.invoke.MethodHandle)

Example 12 with SqlScalarFunction

use of com.facebook.presto.metadata.SqlScalarFunction in project presto by prestodb.

the class TestAnnotationEngineForScalars method testSingleImplementationScalarParse.

@Test
public void testSingleImplementationScalarParse() {
    Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "single_implementation_parametric_scalar"), FunctionKind.SCALAR, DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature()));
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(SingleImplementationScalarFunction.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 single implementation based on class");
    assertImplementationCount(scalar, 1, 0, 0);
    BuiltInScalarFunctionImplementation specialized = scalar.specialize(BoundVariables.builder().build(), 1, FUNCTION_AND_TYPE_MANAGER);
    assertFalse(specialized.getInstanceFactory().isPresent());
    assertEquals(specialized.getArgumentProperty(0).getNullConvention(), RETURN_NULL_ON_NULL);
}
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)

Example 13 with SqlScalarFunction

use of com.facebook.presto.metadata.SqlScalarFunction in project presto by prestodb.

the class TestAnnotationEngineForScalars method testStaticMethodScalarParse.

@Test
public void testStaticMethodScalarParse() {
    Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "static_method_scalar"), FunctionKind.SCALAR, DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature()));
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinitions(StaticMethodScalarFunction.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 single implementation based on method");
}
Also used : 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 14 with SqlScalarFunction

use of com.facebook.presto.metadata.SqlScalarFunction in project presto by prestodb.

the class TestAnnotationEngineForScalars method testParametricScalarParse.

@Test
public void testParametricScalarParse() {
    Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "parametric_scalar"), FunctionKind.SCALAR, ImmutableList.of(typeVariable("T")), ImmutableList.of(), parseTypeSignature("T"), ImmutableList.of(parseTypeSignature("T")), false);
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(ParametricScalarFunction.class);
    assertEquals(functions.size(), 1);
    ParametricScalar scalar = (ParametricScalar) functions.get(0);
    assertImplementationCount(scalar, 0, 2, 0);
    assertEquals(scalar.getSignature(), expectedSignature);
    assertTrue(scalar.isDeterministic());
    assertEquals(scalar.getVisibility(), PUBLIC);
    assertEquals(scalar.getDescription(), "Parametric scalar description");
}
Also used : 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 15 with SqlScalarFunction

use of com.facebook.presto.metadata.SqlScalarFunction in project presto by prestodb.

the class TestAnnotationEngineForScalars method testNonDeterministicScalarParse.

@Test
public void testNonDeterministicScalarParse() {
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(NonDeterministicScalarFunction.class);
    assertEquals(functions.size(), 1);
    ParametricScalar scalar = (ParametricScalar) functions.get(0);
    assertFalse(scalar.isDeterministic());
    assertEquals(scalar.getVisibility(), PUBLIC);
}
Also used : ParametricScalar(com.facebook.presto.operator.scalar.ParametricScalar) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) Test(org.testng.annotations.Test)

Aggregations

SqlScalarFunction (com.facebook.presto.metadata.SqlScalarFunction)21 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)16 Signature (com.facebook.presto.spi.function.Signature)16 Test (org.testng.annotations.Test)15 ParametricScalar (com.facebook.presto.operator.scalar.ParametricScalar)14 ImmutableList (com.google.common.collect.ImmutableList)7 PrestoException (com.facebook.presto.spi.PrestoException)6 SqlType (com.facebook.presto.spi.function.SqlType)6 List (java.util.List)6 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)5 StandardTypes (com.facebook.presto.common.type.StandardTypes)5 TypeSignature (com.facebook.presto.common.type.TypeSignature)5 ADD (com.facebook.presto.common.function.OperatorType.ADD)4 DIVIDE (com.facebook.presto.common.function.OperatorType.DIVIDE)4 HASH_CODE (com.facebook.presto.common.function.OperatorType.HASH_CODE)4 INDETERMINATE (com.facebook.presto.common.function.OperatorType.INDETERMINATE)4 MODULUS (com.facebook.presto.common.function.OperatorType.MODULUS)4 MULTIPLY (com.facebook.presto.common.function.OperatorType.MULTIPLY)4 NEGATION (com.facebook.presto.common.function.OperatorType.NEGATION)4 SUBTRACT (com.facebook.presto.common.function.OperatorType.SUBTRACT)4