Search in sources :

Example 6 with SqlInvokedFunction

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

the class TestMySqlFunctionNamespaceManager method getLatestFunctionHandle.

private FunctionHandle getLatestFunctionHandle(SqlFunctionId functionId) {
    FunctionNamespaceTransactionHandle transactionHandle = functionNamespaceManager.beginTransaction();
    Optional<SqlInvokedFunction> function = functionNamespaceManager.getFunctions(Optional.of(transactionHandle), functionId.getFunctionName()).stream().filter(candidate -> candidate.getFunctionId().equals(functionId)).max(comparing(SqlInvokedFunction::getRequiredVersion));
    assertTrue(function.isPresent());
    functionNamespaceManager.commit(transactionHandle);
    return function.get().getRequiredFunctionHandle();
}
Also used : FunctionNamespaceTransactionHandle(com.facebook.presto.spi.function.FunctionNamespaceTransactionHandle) Parameter(com.facebook.presto.spi.function.Parameter) Arrays(java.util.Arrays) TestingMySqlServer(com.facebook.presto.testing.mysql.TestingMySqlServer) Throwables.throwIfUnchecked(com.google.common.base.Throwables.throwIfUnchecked) Test(org.testng.annotations.Test) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) AfterMethod(org.testng.annotations.AfterMethod) TypeSignature(com.facebook.presto.common.type.TypeSignature) POWER_TOWER(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.POWER_TOWER) DriftNettyClientModule(com.facebook.drift.transport.netty.client.DriftNettyClientModule) Handle(org.jdbi.v3.core.Handle) Map(java.util.Map) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) FUNCTION_POWER_TOWER_DOUBLE_UPDATED(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.FUNCTION_POWER_TOWER_DOUBLE_UPDATED) FunctionVersion.withVersion(com.facebook.presto.spi.function.FunctionVersion.withVersion) SqlFunctionHandle(com.facebook.presto.spi.function.SqlFunctionHandle) Assert.assertFalse(org.testng.Assert.assertFalse) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Collections.nCopies(java.util.Collections.nCopies) BeforeClass(org.testng.annotations.BeforeClass) BeforeMethod(org.testng.annotations.BeforeMethod) DETERMINISTIC(com.facebook.presto.spi.function.RoutineCharacteristics.Determinism.DETERMINISTIC) TANGENT(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.TANGENT) SCALAR(com.facebook.presto.spi.function.FunctionKind.SCALAR) String.format(java.lang.String.format) FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) List(java.util.List) RETURNS_NULL_ON_NULL_INPUT(com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.RETURNS_NULL_ON_NULL_INPUT) Optional(java.util.Optional) ErrorCodeSupplier(com.facebook.presto.spi.ErrorCodeSupplier) FUNCTION_POWER_TOWER_INT(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.FUNCTION_POWER_TOWER_INT) AlterRoutineCharacteristics(com.facebook.presto.spi.function.AlterRoutineCharacteristics) Joiner(com.google.common.base.Joiner) Bootstrap(com.facebook.airlift.bootstrap.Bootstrap) Assert.assertEquals(org.testng.Assert.assertEquals) LifeCycleManager(com.facebook.airlift.bootstrap.LifeCycleManager) FUNCTION_POWER_TOWER_DOUBLE(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.FUNCTION_POWER_TOWER_DOUBLE) PrestoException(com.facebook.presto.spi.PrestoException) ImmutableList(com.google.common.collect.ImmutableList) ALREADY_EXISTS(com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) TEST_SCHEMA(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.TEST_SCHEMA) Comparator.comparing(java.util.Comparator.comparing) RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) CALLED_ON_NULL_INPUT(com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.CALLED_ON_NULL_INPUT) Jdbi(org.jdbi.v3.core.Jdbi) AfterClass(org.testng.annotations.AfterClass) TEST_CATALOG(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.TEST_CATALOG) Assert.fail(org.testng.Assert.fail) Closeables.closeQuietly(com.facebook.airlift.testing.Closeables.closeQuietly) Injector(com.google.inject.Injector) DOUBLE(com.facebook.presto.common.type.StandardTypes.DOUBLE) FUNCTION_TANGENT(com.facebook.presto.functionNamespace.testing.SqlInvokedFunctionTestUtils.FUNCTION_TANGENT) SqlFunctionId(com.facebook.presto.spi.function.SqlFunctionId) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) FunctionVersion.notVersioned(com.facebook.presto.spi.function.FunctionVersion.notVersioned) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) Assert.assertTrue(org.testng.Assert.assertTrue) SimpleAddressSqlFunctionExecutorsModule(com.facebook.presto.functionNamespace.execution.SimpleAddressSqlFunctionExecutorsModule) FunctionNamespaceTransactionHandle(com.facebook.presto.spi.function.FunctionNamespaceTransactionHandle) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction)

Example 7 with SqlInvokedFunction

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

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

Example 9 with SqlInvokedFunction

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

the class MySqlFunctionNamespaceManager method alterFunction.

@Override
public void alterFunction(QualifiedObjectName functionName, Optional<List<TypeSignature>> parameterTypes, AlterRoutineCharacteristics alterRoutineCharacteristics) {
    checkCatalog(functionName);
    jdbi.useTransaction(handle -> {
        FunctionNamespaceDao transactionDao = handle.attach(functionNamespaceDaoClass);
        List<SqlInvokedFunction> functions = getSqlFunctions(transactionDao, functionName, parameterTypes);
        checkUnique(functions, functionName);
        checkExists(functions, functionName, parameterTypes);
        SqlInvokedFunction latest = functions.get(0);
        RoutineCharacteristics.Builder routineCharacteristics = RoutineCharacteristics.builder(latest.getRoutineCharacteristics());
        alterRoutineCharacteristics.getNullCallClause().ifPresent(routineCharacteristics::setNullCallClause);
        SqlInvokedFunction altered = new SqlInvokedFunction(latest.getFunctionId().getFunctionName(), latest.getParameters(), latest.getSignature().getReturnType(), latest.getDescription(), routineCharacteristics.build(), latest.getBody(), notVersioned());
        if (!altered.hasSameDefinitionAs(latest)) {
            checkState(latest.hasVersion(), "Function version missing: %s", latest.getFunctionId());
            insertSqlInvokedFunction(transactionDao, altered, getLongVersion(latest) + 1);
        }
    });
    refreshFunctionsCache(functionName);
}
Also used : RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) AlterRoutineCharacteristics(com.facebook.presto.spi.function.AlterRoutineCharacteristics) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction)

Example 10 with SqlInvokedFunction

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

the class MySqlFunctionNamespaceManager method dropFunction.

@Override
public void dropFunction(QualifiedObjectName functionName, Optional<List<TypeSignature>> parameterTypes, boolean exists) {
    checkCatalog(functionName);
    jdbi.useTransaction(handle -> {
        FunctionNamespaceDao transactionDao = handle.attach(functionNamespaceDaoClass);
        List<SqlInvokedFunction> functions = getSqlFunctions(transactionDao, functionName, parameterTypes);
        checkExists(functions, functionName, parameterTypes);
        if (!parameterTypes.isPresent()) {
            transactionDao.setDeleted(functionName.getCatalogName(), functionName.getSchemaName(), functionName.getObjectName());
        } else {
            SqlInvokedFunction latest = getOnlyElement(functions);
            checkState(latest.hasVersion(), "Function version missing: %s", latest.getFunctionId());
            transactionDao.setDeletionStatus(hash(latest.getFunctionId()), latest.getFunctionId(), getLongVersion(latest), true);
        }
    });
    refreshFunctionsCache(functionName);
}
Also used : SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction)

Aggregations

SqlInvokedFunction (com.facebook.presto.spi.function.SqlInvokedFunction)22 SqlFunctionId (com.facebook.presto.spi.function.SqlFunctionId)12 PrestoException (com.facebook.presto.spi.PrestoException)10 List (java.util.List)8 Map (java.util.Map)8 Parameter (com.facebook.presto.spi.function.Parameter)7 ImmutableList (com.google.common.collect.ImmutableList)7 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)7 Optional (java.util.Optional)7 PRIVATE (com.facebook.presto.bytecode.Access.PRIVATE)4 PUBLIC (com.facebook.presto.bytecode.Access.PUBLIC)4 Access.a (com.facebook.presto.bytecode.Access.a)4 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)4 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)4 CallSiteBinder (com.facebook.presto.bytecode.CallSiteBinder)4 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)4 FieldDefinition (com.facebook.presto.bytecode.FieldDefinition)4 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)4 Parameter (com.facebook.presto.bytecode.Parameter)4 Parameter.arg (com.facebook.presto.bytecode.Parameter.arg)4