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