Search in sources :

Example 1 with Parameter

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

the class TestSqlFunctions method createSessionWithTempFunctionFoo.

private Session createSessionWithTempFunctionFoo() {
    SqlFunctionId bigintSignature = new SqlFunctionId(QualifiedObjectName.valueOf("presto.session.foo"), ImmutableList.of(parseTypeSignature("bigint")));
    SqlInvokedFunction bigintFunction = new SqlInvokedFunction(bigintSignature.getFunctionName(), ImmutableList.of(new Parameter("x", parseTypeSignature("bigint"))), parseTypeSignature("bigint"), "", RoutineCharacteristics.builder().build(), "RETURN x * 2", notVersioned());
    return testSessionBuilder().addSessionFunction(bigintSignature, bigintFunction).build();
}
Also used : SqlFunctionId(com.facebook.presto.spi.function.SqlFunctionId) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) Parameter(com.facebook.presto.spi.function.Parameter) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter)

Example 2 with Parameter

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

the class BuiltInTypeAndFunctionNamespaceManager method getFunctionMetadata.

@Override
public FunctionMetadata getFunctionMetadata(FunctionHandle functionHandle) {
    checkArgument(functionHandle instanceof BuiltInFunctionHandle, "Expect BuiltInFunctionHandle");
    Signature signature = ((BuiltInFunctionHandle) functionHandle).getSignature();
    SpecializedFunctionKey functionKey;
    try {
        functionKey = specializedFunctionKeyCache.getUnchecked(signature);
    } catch (UncheckedExecutionException e) {
        throwIfInstanceOf(e.getCause(), PrestoException.class);
        throw e;
    }
    SqlFunction function = functionKey.getFunction();
    Optional<OperatorType> operatorType = tryGetOperatorType(signature.getName());
    if (operatorType.isPresent()) {
        return new FunctionMetadata(operatorType.get(), signature.getArgumentTypes(), signature.getReturnType(), signature.getKind(), JAVA, function.isDeterministic(), function.isCalledOnNullInput());
    } else if (function instanceof SqlInvokedFunction) {
        SqlInvokedFunction sqlFunction = (SqlInvokedFunction) function;
        List<String> argumentNames = sqlFunction.getParameters().stream().map(Parameter::getName).collect(toImmutableList());
        return new FunctionMetadata(signature.getName(), signature.getArgumentTypes(), argumentNames, signature.getReturnType(), signature.getKind(), sqlFunction.getRoutineCharacteristics().getLanguage(), SQL, function.isDeterministic(), function.isCalledOnNullInput(), sqlFunction.getVersion());
    } else {
        return new FunctionMetadata(signature.getName(), signature.getArgumentTypes(), signature.getReturnType(), signature.getKind(), JAVA, function.isDeterministic(), function.isCalledOnNullInput());
    }
}
Also used : FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) TypeSignature(com.facebook.presto.common.type.TypeSignature) Parameter(com.facebook.presto.spi.function.Parameter) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) TypeParameter(com.facebook.presto.common.type.TypeParameter) PrestoException(com.facebook.presto.spi.PrestoException) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) OperatorType.tryGetOperatorType(com.facebook.presto.common.function.OperatorType.tryGetOperatorType) OperatorType(com.facebook.presto.common.function.OperatorType) SqlFunction(com.facebook.presto.spi.function.SqlFunction)

Example 3 with Parameter

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

the class TestFunctionAndTypeManager method testSessionFunctions.

@Test
public void testSessionFunctions() {
    FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
    SqlFunctionId bigintSignature = new SqlFunctionId(QualifiedObjectName.valueOf("presto.default.foo"), ImmutableList.of(parseTypeSignature("bigint")));
    SqlInvokedFunction bigintFunction = new SqlInvokedFunction(bigintSignature.getFunctionName(), ImmutableList.of(new Parameter("x", parseTypeSignature("bigint"))), parseTypeSignature("bigint"), "", RoutineCharacteristics.builder().build(), "", notVersioned());
    SqlFunctionId varcharSignature = new SqlFunctionId(QualifiedObjectName.valueOf("presto.default.foo"), ImmutableList.of(parseTypeSignature("varchar")));
    SqlInvokedFunction varcharFunction = new SqlInvokedFunction(bigintSignature.getFunctionName(), ImmutableList.of(new Parameter("x", parseTypeSignature("varchar"))), parseTypeSignature("varchar"), "", RoutineCharacteristics.builder().build(), "", notVersioned());
    Map<SqlFunctionId, SqlInvokedFunction> sessionFunctions = ImmutableMap.of(bigintSignature, bigintFunction, varcharSignature, varcharFunction);
    assertEquals(functionAndTypeManager.resolveFunction(Optional.of(sessionFunctions), Optional.empty(), bigintSignature.getFunctionName(), ImmutableList.of(new TypeSignatureProvider(parseTypeSignature("bigint")))), new SessionFunctionHandle(bigintFunction));
    assertEquals(functionAndTypeManager.resolveFunction(Optional.of(sessionFunctions), Optional.empty(), varcharSignature.getFunctionName(), ImmutableList.of(new TypeSignatureProvider(parseTypeSignature("varchar")))), new SessionFunctionHandle(varcharFunction));
    assertEquals(functionAndTypeManager.resolveFunction(Optional.of(sessionFunctions), Optional.empty(), bigintSignature.getFunctionName(), ImmutableList.of(new TypeSignatureProvider(parseTypeSignature("int")))), new SessionFunctionHandle(bigintFunction));
}
Also used : TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) SqlFunctionId(com.facebook.presto.spi.function.SqlFunctionId) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) Parameter(com.facebook.presto.spi.function.Parameter) Test(org.testng.annotations.Test)

Example 4 with Parameter

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

the class TestMySqlFunctionNamespaceManager method testAlterFunction.

@Test
public void testAlterFunction() {
    createFunction(FUNCTION_POWER_TOWER_DOUBLE, false);
    createFunction(FUNCTION_POWER_TOWER_INT, false);
    createFunction(FUNCTION_TANGENT, false);
    // Alter a specific function by name and parameter types
    alterFunction(POWER_TOWER, Optional.of(ImmutableList.of(parseTypeSignature(DOUBLE))), new AlterRoutineCharacteristics(Optional.of(RETURNS_NULL_ON_NULL_INPUT)));
    assertGetFunctions(POWER_TOWER, FUNCTION_POWER_TOWER_INT.withVersion("1"), new SqlInvokedFunction(POWER_TOWER, ImmutableList.of(new Parameter("x", parseTypeSignature(DOUBLE))), parseTypeSignature(DOUBLE), "power tower", RoutineCharacteristics.builder().setDeterminism(DETERMINISTIC).setNullCallClause(RETURNS_NULL_ON_NULL_INPUT).build(), "RETURN pow(x, x)", withVersion("2")));
    // Drop function and alter function by name
    dropFunction(POWER_TOWER, Optional.of(ImmutableList.of(parseTypeSignature(DOUBLE))), false);
    alterFunction(POWER_TOWER, Optional.empty(), new AlterRoutineCharacteristics(Optional.of(CALLED_ON_NULL_INPUT)));
    // Alter function by name
    alterFunction(TANGENT, Optional.empty(), new AlterRoutineCharacteristics(Optional.of(CALLED_ON_NULL_INPUT)));
    SqlInvokedFunction tangentV2 = new SqlInvokedFunction(TANGENT, FUNCTION_TANGENT.getParameters(), FUNCTION_TANGENT.getSignature().getReturnType(), FUNCTION_TANGENT.getDescription(), RoutineCharacteristics.builder().setDeterminism(DETERMINISTIC).build(), FUNCTION_TANGENT.getBody(), withVersion("2"));
    assertGetFunctions(TANGENT, tangentV2);
    // Alter function with no change
    alterFunction(TANGENT, Optional.empty(), new AlterRoutineCharacteristics(Optional.of(CALLED_ON_NULL_INPUT)));
    assertGetFunctions(TANGENT, tangentV2);
}
Also used : AlterRoutineCharacteristics(com.facebook.presto.spi.function.AlterRoutineCharacteristics) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) Parameter(com.facebook.presto.spi.function.Parameter) Test(org.testng.annotations.Test)

Example 5 with Parameter

use of com.facebook.presto.spi.function.Parameter 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

Parameter (com.facebook.presto.spi.function.Parameter)14 SqlInvokedFunction (com.facebook.presto.spi.function.SqlInvokedFunction)8 Test (org.testng.annotations.Test)5 TypeSignature (com.facebook.presto.common.type.TypeSignature)4 PrestoException (com.facebook.presto.spi.PrestoException)4 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)3 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)3 RoutineCharacteristics (com.facebook.presto.spi.function.RoutineCharacteristics)3 SqlFunctionId (com.facebook.presto.spi.function.SqlFunctionId)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 List (java.util.List)3 TypeSignatureParameter (com.facebook.presto.common.type.TypeSignatureParameter)2 FunctionVersion.notVersioned (com.facebook.presto.spi.function.FunctionVersion.notVersioned)2 String.format (java.lang.String.format)2 Optional (java.util.Optional)2 Session (com.facebook.presto.Session)1 OperatorType (com.facebook.presto.common.function.OperatorType)1 OperatorType.tryGetOperatorType (com.facebook.presto.common.function.OperatorType.tryGetOperatorType)1 Type (com.facebook.presto.common.type.Type)1 TypeParameter (com.facebook.presto.common.type.TypeParameter)1