Search in sources :

Example 1 with FunctionNamespaceTransactionHandle

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

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

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

the class FunctionAndTypeManager method resolveFunctionInternal.

private FunctionHandle resolveFunctionInternal(Optional<TransactionId> transactionId, QualifiedObjectName functionName, List<TypeSignatureProvider> parameterTypes) {
    FunctionNamespaceManager<?> functionNamespaceManager = getServingFunctionNamespaceManager(functionName.getCatalogSchemaName()).orElse(null);
    if (functionNamespaceManager == null) {
        throw new PrestoException(FUNCTION_NOT_FOUND, constructFunctionNotFoundErrorMessage(functionName, parameterTypes, ImmutableList.of()));
    }
    Optional<FunctionNamespaceTransactionHandle> transactionHandle = transactionId.map(id -> transactionManager.getFunctionNamespaceTransaction(id, functionName.getCatalogName()));
    if (functionNamespaceManager.canResolveFunction()) {
        return functionNamespaceManager.resolveFunction(transactionHandle, functionName, parameterTypes.stream().map(TypeSignatureProvider::getTypeSignature).collect(toImmutableList()));
    }
    Collection<? extends SqlFunction> candidates = functionNamespaceManager.getFunctions(transactionHandle, functionName);
    Optional<Signature> match = functionSignatureMatcher.match(candidates, parameterTypes, true);
    if (match.isPresent()) {
        return functionNamespaceManager.getFunctionHandle(transactionHandle, match.get());
    }
    if (functionName.getObjectName().startsWith(MAGIC_LITERAL_FUNCTION_PREFIX)) {
        // extract type from function functionName
        String typeName = functionName.getObjectName().substring(MAGIC_LITERAL_FUNCTION_PREFIX.length());
        // lookup the type
        Type type = getType(parseTypeSignature(typeName));
        // verify we have one parameter of the proper type
        checkArgument(parameterTypes.size() == 1, "Expected one argument to literal function, but got %s", parameterTypes);
        return new BuiltInFunctionHandle(getMagicLiteralFunctionSignature(type));
    }
    throw new PrestoException(FUNCTION_NOT_FOUND, constructFunctionNotFoundErrorMessage(functionName, parameterTypes, candidates));
}
Also used : FunctionNamespaceTransactionHandle(com.facebook.presto.spi.function.FunctionNamespaceTransactionHandle) TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) UserDefinedType(com.facebook.presto.common.type.UserDefinedType) CastType.toOperatorType(com.facebook.presto.metadata.CastType.toOperatorType) ParametricType(com.facebook.presto.common.type.ParametricType) DistinctType(com.facebook.presto.common.type.DistinctType) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) TypeSignature(com.facebook.presto.common.type.TypeSignature) LiteralEncoder.getMagicLiteralFunctionSignature(com.facebook.presto.sql.planner.LiteralEncoder.getMagicLiteralFunctionSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) PrestoException(com.facebook.presto.spi.PrestoException)

Example 4 with FunctionNamespaceTransactionHandle

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

the class TestSqlInvokedFunctionNamespaceManager method testTransactionalGetFunction.

@Test
public void testTransactionalGetFunction() {
    InMemoryFunctionNamespaceManager functionNamespaceManager = new InMemoryFunctionNamespaceManager(TEST_CATALOG, new SqlFunctionExecutors(ImmutableMap.of(SQL, FunctionImplementationType.SQL), new NoopSqlFunctionExecutor()), new SqlInvokedFunctionNamespaceManagerConfig().setFunctionCacheExpiration(new Duration(0, MILLISECONDS)).setFunctionInstanceCacheExpiration(new Duration(0, MILLISECONDS)));
    // begin first transaction
    FunctionNamespaceTransactionHandle transaction1 = functionNamespaceManager.beginTransaction();
    assertEquals(functionNamespaceManager.getFunctions(Optional.of(transaction1), POWER_TOWER).size(), 0);
    // create function, first transaction still sees no functions
    functionNamespaceManager.createFunction(FUNCTION_POWER_TOWER_DOUBLE, false);
    assertEquals(functionNamespaceManager.getFunctions(Optional.of(transaction1), POWER_TOWER).size(), 0);
    // second transaction sees newly created function
    FunctionNamespaceTransactionHandle transaction2 = functionNamespaceManager.beginTransaction();
    Collection<SqlInvokedFunction> functions2 = functionNamespaceManager.getFunctions(Optional.of(transaction2), POWER_TOWER);
    assertEquals(functions2.size(), 1);
    assertEquals(getOnlyElement(functions2), FUNCTION_POWER_TOWER_DOUBLE.withVersion("1"));
    // update the function, second transaction still sees the old functions
    functionNamespaceManager.createFunction(FUNCTION_POWER_TOWER_DOUBLE_UPDATED, true);
    functions2 = functionNamespaceManager.getFunctions(Optional.of(transaction2), POWER_TOWER);
    assertEquals(functions2.size(), 1);
    assertEquals(getOnlyElement(functions2), FUNCTION_POWER_TOWER_DOUBLE.withVersion("1"));
    // third transaction sees the updated function
    FunctionNamespaceTransactionHandle transaction3 = functionNamespaceManager.beginTransaction();
    Collection<SqlInvokedFunction> functions3 = functionNamespaceManager.getFunctions(Optional.of(transaction3), POWER_TOWER);
    assertEquals(functions3.size(), 1);
    assertEquals(getOnlyElement(functions3), FUNCTION_POWER_TOWER_DOUBLE_UPDATED.withVersion("2"));
    functionNamespaceManager.commit(transaction1);
    functionNamespaceManager.commit(transaction2);
    functionNamespaceManager.commit(transaction3);
}
Also used : FunctionNamespaceTransactionHandle(com.facebook.presto.spi.function.FunctionNamespaceTransactionHandle) NoopSqlFunctionExecutor(com.facebook.presto.functionNamespace.execution.NoopSqlFunctionExecutor) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) Duration(io.airlift.units.Duration) SqlFunctionExecutors(com.facebook.presto.functionNamespace.execution.SqlFunctionExecutors) InMemoryFunctionNamespaceManager(com.facebook.presto.functionNamespace.testing.InMemoryFunctionNamespaceManager) Test(org.testng.annotations.Test)

Example 5 with FunctionNamespaceTransactionHandle

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

the class TestMySqlFunctionNamespaceManager method assertGetFunctions.

private void assertGetFunctions(QualifiedObjectName functionName, SqlInvokedFunction... functions) {
    FunctionNamespaceTransactionHandle transactionHandle = functionNamespaceManager.beginTransaction();
    assertEquals(ImmutableSet.copyOf(functionNamespaceManager.getFunctions(Optional.of(transactionHandle), functionName)), ImmutableSet.copyOf(Arrays.asList(functions)));
    functionNamespaceManager.commit(transactionHandle);
}
Also used : FunctionNamespaceTransactionHandle(com.facebook.presto.spi.function.FunctionNamespaceTransactionHandle)

Aggregations

FunctionNamespaceTransactionHandle (com.facebook.presto.spi.function.FunctionNamespaceTransactionHandle)5 SqlInvokedFunction (com.facebook.presto.spi.function.SqlInvokedFunction)3 Test (org.testng.annotations.Test)3 TypeSignature (com.facebook.presto.common.type.TypeSignature)2 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)2 InMemoryFunctionNamespaceManager (com.facebook.presto.functionNamespace.testing.InMemoryFunctionNamespaceManager)2 PrestoException (com.facebook.presto.spi.PrestoException)2 FunctionMetadata (com.facebook.presto.spi.function.FunctionMetadata)2 Bootstrap (com.facebook.airlift.bootstrap.Bootstrap)1 LifeCycleManager (com.facebook.airlift.bootstrap.LifeCycleManager)1 Closeables.closeQuietly (com.facebook.airlift.testing.Closeables.closeQuietly)1 DriftNettyClientModule (com.facebook.drift.transport.netty.client.DriftNettyClientModule)1 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)1 OperatorType (com.facebook.presto.common.function.OperatorType)1 DistinctType (com.facebook.presto.common.type.DistinctType)1 ParametricType (com.facebook.presto.common.type.ParametricType)1 DOUBLE (com.facebook.presto.common.type.StandardTypes.DOUBLE)1 Type (com.facebook.presto.common.type.Type)1 UserDefinedType (com.facebook.presto.common.type.UserDefinedType)1 NoopSqlFunctionExecutor (com.facebook.presto.functionNamespace.execution.NoopSqlFunctionExecutor)1