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();
}
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);
}
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));
}
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);
}
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);
}
Aggregations