Search in sources :

Example 1 with DEFAULT_NAMESPACE

use of com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE 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 2 with DEFAULT_NAMESPACE

use of com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE in project presto by prestodb.

the class SqlInvokedScalarFromAnnotationsParser method createSqlInvokedFunctions.

private static List<SqlInvokedFunction> createSqlInvokedFunctions(Method method, Optional<SqlInvokedScalarFunction> header, Optional<String> description) {
    SqlInvokedScalarFunction functionHeader = header.orElse(method.getAnnotation(SqlInvokedScalarFunction.class));
    String functionDescription = description.orElse(method.isAnnotationPresent(Description.class) ? method.getAnnotation(Description.class).value() : "");
    TypeSignature returnType = parseTypeSignature(method.getAnnotation(SqlType.class).value());
    // Parameter
    checkCondition(!method.isAnnotationPresent(SqlParameter.class) || !method.isAnnotationPresent(SqlParameters.class), FUNCTION_IMPLEMENTATION_ERROR, "Function-defining method [%s] is annotated with both @SqlParameter and @SqlParameters", method);
    List<Parameter> parameters;
    if (method.isAnnotationPresent(SqlParameter.class)) {
        parameters = ImmutableList.of(getParameterFromAnnotation(method.getAnnotation(SqlParameter.class)));
    } else if (method.isAnnotationPresent(SqlParameters.class)) {
        parameters = stream(method.getAnnotation(SqlParameters.class).value()).map(SqlInvokedScalarFromAnnotationsParser::getParameterFromAnnotation).collect(toImmutableList());
    } else {
        parameters = ImmutableList.of();
    }
    // Routine characteristics
    RoutineCharacteristics routineCharacteristics = RoutineCharacteristics.builder().setLanguage(RoutineCharacteristics.Language.SQL).setDeterminism(functionHeader.deterministic() ? DETERMINISTIC : NOT_DETERMINISTIC).setNullCallClause(functionHeader.calledOnNullInput() ? CALLED_ON_NULL_INPUT : RETURNS_NULL_ON_NULL_INPUT).build();
    String body;
    try {
        body = (String) method.invoke(null);
    } catch (ReflectiveOperationException e) {
        throw new PrestoException(FUNCTION_IMPLEMENTATION_ERROR, format("Failed to get function body for method [%s]", method), e);
    }
    return Stream.concat(Stream.of(functionHeader.value()), stream(functionHeader.alias())).map(name -> new SqlInvokedFunction(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, name), parameters, returnType, functionDescription, routineCharacteristics, body, notVersioned())).collect(toImmutableList());
}
Also used : Parameter(com.facebook.presto.spi.function.Parameter) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) PrestoException(com.facebook.presto.spi.PrestoException) TypeSignature(com.facebook.presto.common.type.TypeSignature) SqlParameters(com.facebook.presto.spi.function.SqlParameters) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) CodegenScalarFunction(com.facebook.presto.spi.function.CodegenScalarFunction) DEFAULT_NAMESPACE(com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) ImmutableList(com.google.common.collect.ImmutableList) Description(com.facebook.presto.spi.function.Description) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) Method(java.lang.reflect.Method) RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) SqlInvokedScalarFunction(com.facebook.presto.spi.function.SqlInvokedScalarFunction) FUNCTION_IMPLEMENTATION_ERROR(com.facebook.presto.spi.StandardErrorCode.FUNCTION_IMPLEMENTATION_ERROR) CALLED_ON_NULL_INPUT(com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.CALLED_ON_NULL_INPUT) Failures.checkCondition(com.facebook.presto.util.Failures.checkCondition) FunctionsParserHelper.findPublicStaticMethods(com.facebook.presto.operator.annotations.FunctionsParserHelper.findPublicStaticMethods) SqlParameter(com.facebook.presto.spi.function.SqlParameter) ImmutableSet(com.google.common.collect.ImmutableSet) NOT_DETERMINISTIC(com.facebook.presto.spi.function.RoutineCharacteristics.Determinism.NOT_DETERMINISTIC) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) DETERMINISTIC(com.facebook.presto.spi.function.RoutineCharacteristics.Determinism.DETERMINISTIC) Set(java.util.Set) String.format(java.lang.String.format) List(java.util.List) Stream(java.util.stream.Stream) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) RETURNS_NULL_ON_NULL_INPUT(com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.RETURNS_NULL_ON_NULL_INPUT) FunctionVersion.notVersioned(com.facebook.presto.spi.function.FunctionVersion.notVersioned) Optional(java.util.Optional) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Arrays.stream(java.util.Arrays.stream) SqlType(com.facebook.presto.spi.function.SqlType) Description(com.facebook.presto.spi.function.Description) SqlParameter(com.facebook.presto.spi.function.SqlParameter) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) PrestoException(com.facebook.presto.spi.PrestoException) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) SqlParameters(com.facebook.presto.spi.function.SqlParameters) RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) SqlInvokedScalarFunction(com.facebook.presto.spi.function.SqlInvokedScalarFunction) Parameter(com.facebook.presto.spi.function.Parameter) SqlParameter(com.facebook.presto.spi.function.SqlParameter)

Aggregations

QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)2 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)2 DEFAULT_NAMESPACE (com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE)2 Description (com.facebook.presto.spi.function.Description)2 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)2 SqlType (com.facebook.presto.spi.function.SqlType)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 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 TypeSignature (com.facebook.presto.common.type.TypeSignature)1 BoundVariables (com.facebook.presto.metadata.BoundVariables)1 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)1 FunctionAndTypeManager.createTestFunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager)1 SqlScalarFunction (com.facebook.presto.metadata.SqlScalarFunction)1 FunctionsParserHelper.findPublicStaticMethods (com.facebook.presto.operator.annotations.FunctionsParserHelper.findPublicStaticMethods)1