Search in sources :

Example 6 with FunctionMetadata

use of com.facebook.presto.spi.function.FunctionMetadata in project presto by prestodb.

the class SpatialJoinUtils method getFlippedFunctionHandle.

public static FunctionHandle getFlippedFunctionHandle(CallExpression callExpression, FunctionAndTypeManager functionAndTypeManager) {
    FunctionMetadata callExpressionMetadata = functionAndTypeManager.getFunctionMetadata(callExpression.getFunctionHandle());
    checkArgument(callExpressionMetadata.getOperatorType().isPresent());
    OperatorType operatorType = flip(callExpressionMetadata.getOperatorType().get());
    List<TypeSignatureProvider> typeProviderList = fromTypes(callExpression.getArguments().stream().map(RowExpression::getType).collect(toImmutableList()));
    checkArgument(typeProviderList.size() == 2, "Expected there to be only two arguments in type provider");
    return functionAndTypeManager.resolveOperator(operatorType, ImmutableList.of(typeProviderList.get(1), typeProviderList.get(0)));
}
Also used : TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) RowExpression(com.facebook.presto.spi.relation.RowExpression) OperatorType(com.facebook.presto.common.function.OperatorType)

Example 7 with FunctionMetadata

use of com.facebook.presto.spi.function.FunctionMetadata in project presto by prestodb.

the class RowExpressionVerifier method visitArithmeticBinary.

@Override
protected Boolean visitArithmeticBinary(ArithmeticBinaryExpression expected, RowExpression actual) {
    if (actual instanceof CallExpression) {
        FunctionMetadata functionMetadata = metadata.getFunctionAndTypeManager().getFunctionMetadata(((CallExpression) actual).getFunctionHandle());
        if (!functionMetadata.getOperatorType().isPresent() || !functionMetadata.getOperatorType().get().isArithmeticOperator()) {
            return false;
        }
        OperatorType actualOperatorType = functionMetadata.getOperatorType().get();
        OperatorType expectedOperatorType = getOperatorType(expected.getOperator());
        if (expectedOperatorType.equals(actualOperatorType)) {
            return process(expected.getLeft(), ((CallExpression) actual).getArguments().get(0)) && process(expected.getRight(), ((CallExpression) actual).getArguments().get(1));
        }
    }
    return false;
}
Also used : FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) CallExpression(com.facebook.presto.spi.relation.CallExpression) OperatorType(com.facebook.presto.common.function.OperatorType)

Example 8 with FunctionMetadata

use of com.facebook.presto.spi.function.FunctionMetadata in project presto by prestodb.

the class TranslatorAnnotationParser method methodToFunctionMetadata.

private static FunctionMetadata methodToFunctionMetadata(ScalarTranslationHeader header, Method method) {
    requireNonNull(header, "header is null");
    // return type
    SqlType annotatedReturnType = method.getAnnotation(SqlType.class);
    checkArgument(annotatedReturnType != null, format("Method [%s] is missing @SqlType annotation", method));
    TypeSignature returnType = parseTypeSignature(annotatedReturnType.value(), ImmutableSet.of());
    // argument type
    ImmutableList.Builder<TypeSignature> argumentTypes = new ImmutableList.Builder<>();
    for (Parameter parameter : method.getParameters()) {
        Annotation[] annotations = parameter.getAnnotations();
        SqlType type = Stream.of(annotations).filter(SqlType.class::isInstance).map(SqlType.class::cast).findFirst().orElseThrow(() -> new IllegalArgumentException(format("Method [%s] is missing @SqlType annotation for parameter", method)));
        TypeSignature typeSignature = parseTypeSignature(type.value(), ImmutableSet.of());
        argumentTypes.add(typeSignature);
    }
    if (header.getOperatorType().isPresent()) {
        return new FunctionMetadata(header.getOperatorType().get(), argumentTypes.build(), returnType, SCALAR, JAVA, header.isDeterministic(), header.isCalledOnNullInput());
    }
    return new FunctionMetadata(header.getName(), argumentTypes.build(), returnType, SCALAR, JAVA, header.isDeterministic(), header.isCalledOnNullInput());
}
Also used : FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) ImmutableList(com.google.common.collect.ImmutableList) Parameter(java.lang.reflect.Parameter) SqlType(com.facebook.presto.spi.function.SqlType) Annotation(java.lang.annotation.Annotation)

Example 9 with FunctionMetadata

use of com.facebook.presto.spi.function.FunctionMetadata in project presto by prestodb.

the class TestSqlInvokedFunctionNamespaceManager method testCaching.

@Test
public void testCaching() {
    InMemoryFunctionNamespaceManager functionNamespaceManager = createFunctionNamespaceManager();
    functionNamespaceManager.createFunction(FUNCTION_POWER_TOWER_DOUBLE, false);
    // fetchFunctionsDirect does not produce the same function reference
    SqlInvokedFunction function1 = getOnlyElement(functionNamespaceManager.fetchFunctionsDirect(POWER_TOWER));
    SqlInvokedFunction function2 = getOnlyElement(functionNamespaceManager.fetchFunctionsDirect(POWER_TOWER));
    assertEquals(function1, function2);
    assertNotSame(function1, function2);
    // fetchFunctionMetadataDirect does not produce the same metadata reference
    FunctionMetadata metadata1 = functionNamespaceManager.fetchFunctionMetadataDirect(function1.getRequiredFunctionHandle());
    FunctionMetadata metadata2 = functionNamespaceManager.fetchFunctionMetadataDirect(function2.getRequiredFunctionHandle());
    assertEquals(metadata1, metadata2);
    assertNotSame(metadata1, metadata2);
    // getFunctionMetadata produces the same metadata reference
    metadata1 = functionNamespaceManager.getFunctionMetadata(function1.getRequiredFunctionHandle());
    metadata2 = functionNamespaceManager.getFunctionMetadata(function2.getRequiredFunctionHandle());
    assertSame(metadata1, metadata2);
    // getFunctions produces the same function collection reference
    functionNamespaceManager.createFunction(FUNCTION_POWER_TOWER_INT, false);
    FunctionNamespaceTransactionHandle transaction1 = functionNamespaceManager.beginTransaction();
    FunctionNamespaceTransactionHandle transaction2 = functionNamespaceManager.beginTransaction();
    Collection<SqlInvokedFunction> functions1 = functionNamespaceManager.getFunctions(Optional.of(transaction1), POWER_TOWER);
    Collection<SqlInvokedFunction> functions2 = functionNamespaceManager.getFunctions(Optional.of(transaction2), POWER_TOWER);
    assertEquals(functions1.size(), 2);
    assertSame(functions1, functions2);
}
Also used : FunctionNamespaceTransactionHandle(com.facebook.presto.spi.function.FunctionNamespaceTransactionHandle) FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) InMemoryFunctionNamespaceManager(com.facebook.presto.functionNamespace.testing.InMemoryFunctionNamespaceManager) Test(org.testng.annotations.Test)

Example 10 with FunctionMetadata

use of com.facebook.presto.spi.function.FunctionMetadata in project presto by prestodb.

the class TestMySqlFunctionNamespaceManager method assertGetFunctionMetadata.

private void assertGetFunctionMetadata(FunctionHandle functionHandle, SqlInvokedFunction expectFunction) {
    FunctionMetadata functionMetadata = functionNamespaceManager.getFunctionMetadata(functionHandle);
    assertEquals(functionMetadata.getName(), expectFunction.getSignature().getName());
    assertFalse(functionMetadata.getOperatorType().isPresent());
    assertEquals(functionMetadata.getArgumentTypes(), expectFunction.getSignature().getArgumentTypes());
    assertEquals(functionMetadata.getReturnType(), expectFunction.getSignature().getReturnType());
    assertEquals(functionMetadata.getFunctionKind(), SCALAR);
    assertEquals(functionMetadata.isDeterministic(), expectFunction.isDeterministic());
    assertEquals(functionMetadata.isCalledOnNullInput(), expectFunction.isCalledOnNullInput());
}
Also used : FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata)

Aggregations

FunctionMetadata (com.facebook.presto.spi.function.FunctionMetadata)19 OperatorType (com.facebook.presto.common.function.OperatorType)9 TypeSignature (com.facebook.presto.common.type.TypeSignature)4 PrestoException (com.facebook.presto.spi.PrestoException)4 FunctionHandle (com.facebook.presto.spi.function.FunctionHandle)4 CallExpression (com.facebook.presto.spi.relation.CallExpression)4 ImmutableList (com.google.common.collect.ImmutableList)4 Type (com.facebook.presto.common.type.Type)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 List (java.util.List)3 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)2 ColumnHandle (com.facebook.presto.spi.ColumnHandle)2 SqlInvokedFunction (com.facebook.presto.spi.function.SqlInvokedFunction)2 RowExpression (com.facebook.presto.spi.relation.RowExpression)2 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 String.format (java.lang.String.format)2 MethodHandle (java.lang.invoke.MethodHandle)2 Map (java.util.Map)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2