Search in sources :

Example 21 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class TestArraySqlFunctions method testArrayFrequencyVarchar.

@Test
public void testArrayFrequencyVarchar() {
    FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
    MapType type = new MapType(VARCHAR, INTEGER, methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation"));
    TypeSignature typeSignature = TypeSignature.parseTypeSignature(type.getDisplayName());
    assertFunction("array_frequency(cast(null as array(varchar)))", functionAndTypeManager.getType(typeSignature), null);
    assertFunction("array_frequency(cast(array[] as array(varchar)))", functionAndTypeManager.getType(typeSignature), ImmutableMap.of());
    assertFunction("array_frequency(array[cast(null as varchar), cast(null as varchar), cast(null as varchar)])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of());
    assertFunction("array_frequency(array[varchar 'z', cast(null as varchar)])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of("z", 1));
    assertFunction("array_frequency(array[varchar 'a', cast(null as varchar), varchar 'b', cast(null as varchar), cast(null as varchar) ])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of("a", 1, "b", 1));
    assertFunction("array_frequency(array[varchar 'a', varchar 'b', varchar 'a', varchar 'a', varchar 'a'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of("a", 4, "b", 1));
    assertFunction("array_frequency(array[varchar 'a', varchar 'b', varchar 'a', varchar 'b', varchar 'c'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of("a", 2, "b", 2, "c", 1));
    assertFunction("array_frequency(array[varchar 'y', varchar 'p'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of("p", 1, "y", 1));
    assertFunction("array_frequency(array[varchar 'a', varchar 'a', varchar 'p'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of("p", 1, "a", 2));
    assertFunction("array_frequency(array[varchar 'z'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of("z", 1));
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) TestRowType(com.facebook.presto.common.type.TestRowType) MapType(com.facebook.presto.common.type.MapType) Test(org.testng.annotations.Test)

Example 22 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class ThriftSqlFunctionExecutor method executeFunction.

@Override
public CompletableFuture<SqlFunctionResult> executeFunction(String source, RemoteScalarFunctionImplementation functionImplementation, Page input, List<Integer> channels, List<Type> argumentTypes, Type returnType) {
    ThriftUdfPage page = buildThriftPage(functionImplementation, input, channels, argumentTypes);
    SqlFunctionHandle functionHandle = functionImplementation.getFunctionHandle();
    SqlFunctionId functionId = functionHandle.getFunctionId();
    ThriftFunctionHandle thriftFunctionHandle = new ThriftFunctionHandle(functionId.getFunctionName().toString(), functionId.getArgumentTypes().stream().map(TypeSignature::toString).collect(toImmutableList()), returnType.toString(), functionHandle.getVersion());
    ThriftUdfService thriftUdfService = thriftUdfClient.get(Optional.of(functionImplementation.getLanguage().getLanguage()));
    return invokeUdfWithRetry(thriftUdfService, new ThriftUdfRequest(source, thriftFunctionHandle, page)).thenApply(thriftResult -> toSqlFunctionResult(thriftResult, returnType));
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) SqlFunctionId(com.facebook.presto.spi.function.SqlFunctionId) ThriftFunctionHandle(com.facebook.presto.thrift.api.udf.ThriftFunctionHandle) ThriftUdfRequest(com.facebook.presto.thrift.api.udf.ThriftUdfRequest) SqlFunctionHandle(com.facebook.presto.spi.function.SqlFunctionHandle) ThriftUdfPage(com.facebook.presto.thrift.api.udf.ThriftUdfPage) ThriftUdfService(com.facebook.presto.thrift.api.udf.ThriftUdfService)

Example 23 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature 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 24 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class OperatorValidator method validateOperator.

public static void validateOperator(OperatorType operatorType, TypeSignature returnType, List<TypeSignature> argumentTypes) {
    switch(operatorType) {
        case ADD:
        case SUBTRACT:
        case MULTIPLY:
        case DIVIDE:
        case MODULUS:
            validateOperatorSignature(operatorType, returnType, argumentTypes, 2);
            break;
        case NEGATION:
            validateOperatorSignature(operatorType, returnType, argumentTypes, 1);
            break;
        case EQUAL:
        case NOT_EQUAL:
        case LESS_THAN:
        case LESS_THAN_OR_EQUAL:
        case GREATER_THAN:
        case GREATER_THAN_OR_EQUAL:
            validateComparisonOperatorSignature(operatorType, returnType, argumentTypes, 2);
            break;
        case BETWEEN:
            validateComparisonOperatorSignature(operatorType, returnType, argumentTypes, 3);
            break;
        case CAST:
            validateOperatorSignature(operatorType, returnType, argumentTypes, 1);
            break;
        case SUBSCRIPT:
            validateOperatorSignature(operatorType, returnType, argumentTypes, 2);
            checkArgument(argumentTypes.get(0).getBase().equals(StandardTypes.ARRAY) || argumentTypes.get(0).getBase().equals(StandardTypes.MAP), "First argument must be an ARRAY or MAP");
            if (argumentTypes.get(0).getBase().equals(StandardTypes.ARRAY)) {
                checkArgument(argumentTypes.get(1).getBase().equals(StandardTypes.BIGINT), "Second argument must be a BIGINT");
                TypeSignature elementType = argumentTypes.get(0).getTypeParametersAsTypeSignatures().get(0);
                checkArgument(returnType.equals(elementType), "[] return type does not match ARRAY element type");
            } else {
                TypeSignature valueType = argumentTypes.get(0).getTypeParametersAsTypeSignatures().get(1);
                checkArgument(returnType.equals(valueType), "[] return type does not match MAP value type");
            }
            break;
        case HASH_CODE:
            validateOperatorSignature(operatorType, returnType, argumentTypes, 1);
            checkArgument(returnType.getBase().equals(StandardTypes.BIGINT), "%s operator must return a BIGINT: %s", operatorType, formatSignature(operatorType, returnType, argumentTypes));
            break;
        case SATURATED_FLOOR_CAST:
            validateOperatorSignature(operatorType, returnType, argumentTypes, 1);
            break;
    }
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature)

Example 25 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class MySqlFunctionNamespaceManager method createFunction.

@Override
public void createFunction(SqlInvokedFunction function, boolean replace) {
    checkCatalog(function);
    checkFunctionLanguageSupported(function);
    checkArgument(!function.hasVersion(), "function '%s' is already versioned", function);
    QualifiedObjectName functionName = function.getFunctionId().getFunctionName();
    checkFieldLength("Catalog name", functionName.getCatalogName(), MAX_CATALOG_NAME_LENGTH);
    checkFieldLength("Schema name", functionName.getSchemaName(), MAX_SCHEMA_NAME_LENGTH);
    if (!functionNamespaceDao.functionNamespaceExists(functionName.getCatalogName(), functionName.getSchemaName())) {
        throw new PrestoException(NOT_FOUND, format("Function namespace not found: %s", functionName.getCatalogSchemaName()));
    }
    checkFieldLength("Function name", functionName.getObjectName(), MAX_FUNCTION_NAME_LENGTH);
    if (function.getParameters().size() > MAX_PARAMETER_COUNT) {
        throw new PrestoException(GENERIC_USER_ERROR, format("Function has more than %s parameters: %s", MAX_PARAMETER_COUNT, function.getParameters().size()));
    }
    for (Parameter parameter : function.getParameters()) {
        checkFieldLength("Parameter name", parameter.getName(), MAX_PARAMETER_NAME_LENGTH);
    }
    checkFieldLength("Parameter type list", function.getFunctionId().getArgumentTypes().stream().map(TypeSignature::toString).collect(joining(",")), MAX_PARAMETER_TYPES_LENGTH);
    checkFieldLength("Return type", function.getSignature().getReturnType().toString(), MAX_RETURN_TYPE_LENGTH);
    jdbi.useTransaction(handle -> {
        FunctionNamespaceDao transactionDao = handle.attach(functionNamespaceDaoClass);
        Optional<SqlInvokedFunctionRecord> latestVersion = transactionDao.getLatestRecordForUpdate(hash(function.getFunctionId()), function.getFunctionId());
        if (!replace && latestVersion.isPresent() && !latestVersion.get().isDeleted()) {
            throw new PrestoException(ALREADY_EXISTS, "Function already exists: " + function.getFunctionId());
        }
        if (!latestVersion.isPresent() || !latestVersion.get().getFunction().hasSameDefinitionAs(function)) {
            long newVersion = latestVersion.map(SqlInvokedFunctionRecord::getFunction).map(MySqlFunctionNamespaceManager::getLongVersion).orElse(0L) + 1;
            insertSqlInvokedFunction(transactionDao, function, newVersion);
        } else if (latestVersion.get().isDeleted()) {
            SqlInvokedFunction latest = latestVersion.get().getFunction();
            checkState(latest.hasVersion(), "Function version missing: %s", latest.getFunctionId());
            transactionDao.setDeletionStatus(hash(latest.getFunctionId()), latest.getFunctionId(), getLongVersion(latest), false);
        }
    });
    refreshFunctionsCache(functionName);
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) Parameter(com.facebook.presto.spi.function.Parameter) PrestoException(com.facebook.presto.spi.PrestoException) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName)

Aggregations

TypeSignature (com.facebook.presto.common.type.TypeSignature)60 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)25 Signature (com.facebook.presto.spi.function.Signature)20 NamedTypeSignature (com.facebook.presto.common.type.NamedTypeSignature)19 ImmutableList (com.google.common.collect.ImmutableList)19 List (java.util.List)17 PrestoException (com.facebook.presto.spi.PrestoException)15 Type (com.facebook.presto.common.type.Type)14 Test (org.testng.annotations.Test)14 TypeSignatureParameter (com.facebook.presto.common.type.TypeSignatureParameter)13 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)13 Objects.requireNonNull (java.util.Objects.requireNonNull)12 RowFieldName (com.facebook.presto.common.type.RowFieldName)10 StandardTypes (com.facebook.presto.common.type.StandardTypes)9 Optional (java.util.Optional)9 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)8 DecimalType (com.facebook.presto.common.type.DecimalType)8 ImmutableSet (com.google.common.collect.ImmutableSet)8 RowType (com.facebook.presto.common.type.RowType)7 ArrayType (com.facebook.presto.common.type.ArrayType)6