use of com.facebook.presto.spi.function.RoutineCharacteristics 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);
}
use of com.facebook.presto.spi.function.RoutineCharacteristics in project presto by prestodb.
the class SqlInvokedFunctionRowMapper method map.
@Override
public SqlInvokedFunction map(ResultSet rs, StatementContext ctx) throws SQLException {
String catalog = rs.getString("catalog_name");
String schema = rs.getString("schema_name");
String functionName = rs.getString("function_name");
List<Parameter> parameters = PARAMETERS_CODEC.fromJson(rs.getString("parameters"));
String returnType = rs.getString("return_type");
String description = rs.getString("description");
RoutineCharacteristics routineCharacteristics = ROUTINE_CHARACTERISTICS_CODEC.fromJson(rs.getString("routine_characteristics"));
String body = rs.getString("body");
String version = String.valueOf(rs.getLong("version"));
return new SqlInvokedFunction(QualifiedObjectName.valueOf(catalog, schema, functionName), parameters, parseTypeSignature(returnType), description, routineCharacteristics, body, withVersion(version));
}
use of com.facebook.presto.spi.function.RoutineCharacteristics in project presto by prestodb.
the class SqlInvokedScalarFromAnnotationsParser method createSqlInvokedFunctions.
private static List<SqlInvokedFunction> createSqlInvokedFunctions(Method method, Optional<SqlInvokedScalarFunction> header, Optional<String> description) {
SqlInvokedScalarFunction functionHeader = header.orElse(method.getAnnotation(SqlInvokedScalarFunction.class));
String functionDescription = description.orElse(method.isAnnotationPresent(Description.class) ? method.getAnnotation(Description.class).value() : "");
TypeSignature returnType = parseTypeSignature(method.getAnnotation(SqlType.class).value());
// Parameter
checkCondition(!method.isAnnotationPresent(SqlParameter.class) || !method.isAnnotationPresent(SqlParameters.class), FUNCTION_IMPLEMENTATION_ERROR, "Function-defining method [%s] is annotated with both @SqlParameter and @SqlParameters", method);
List<Parameter> parameters;
if (method.isAnnotationPresent(SqlParameter.class)) {
parameters = ImmutableList.of(getParameterFromAnnotation(method.getAnnotation(SqlParameter.class)));
} else if (method.isAnnotationPresent(SqlParameters.class)) {
parameters = stream(method.getAnnotation(SqlParameters.class).value()).map(SqlInvokedScalarFromAnnotationsParser::getParameterFromAnnotation).collect(toImmutableList());
} else {
parameters = ImmutableList.of();
}
// Routine characteristics
RoutineCharacteristics routineCharacteristics = RoutineCharacteristics.builder().setLanguage(RoutineCharacteristics.Language.SQL).setDeterminism(functionHeader.deterministic() ? DETERMINISTIC : NOT_DETERMINISTIC).setNullCallClause(functionHeader.calledOnNullInput() ? CALLED_ON_NULL_INPUT : RETURNS_NULL_ON_NULL_INPUT).build();
String body;
try {
body = (String) method.invoke(null);
} catch (ReflectiveOperationException e) {
throw new PrestoException(FUNCTION_IMPLEMENTATION_ERROR, format("Failed to get function body for method [%s]", method), e);
}
return Stream.concat(Stream.of(functionHeader.value()), stream(functionHeader.alias())).map(name -> new SqlInvokedFunction(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, name), parameters, returnType, functionDescription, routineCharacteristics, body, notVersioned())).collect(toImmutableList());
}
use of com.facebook.presto.spi.function.RoutineCharacteristics in project presto by prestodb.
the class CreateFunctionTask method createSqlInvokedFunction.
private SqlInvokedFunction createSqlInvokedFunction(CreateFunction statement, Metadata metadata, Analysis analysis) {
QualifiedObjectName functionName = statement.isTemporary() ? QualifiedObjectName.valueOf(SESSION_NAMESPACE, statement.getFunctionName().getSuffix()) : qualifyObjectName(statement.getFunctionName());
List<Parameter> parameters = statement.getParameters().stream().map(parameter -> new Parameter(parameter.getName().toString(), parseTypeSignature(parameter.getType()))).collect(toImmutableList());
TypeSignature returnType = parseTypeSignature(statement.getReturnType());
String description = statement.getComment().orElse("");
RoutineCharacteristics routineCharacteristics = RoutineCharacteristics.builder().setLanguage(new RoutineCharacteristics.Language(statement.getCharacteristics().getLanguage().getLanguage())).setDeterminism(RoutineCharacteristics.Determinism.valueOf(statement.getCharacteristics().getDeterminism().name())).setNullCallClause(RoutineCharacteristics.NullCallClause.valueOf(statement.getCharacteristics().getNullCallClause().name())).build();
RoutineBody body = statement.getBody();
if (statement.getBody() instanceof Return) {
Expression bodyExpression = ((Return) statement.getBody()).getExpression();
Type bodyType = analysis.getType(bodyExpression);
// Coerce expressions in body if necessary
bodyExpression = ExpressionTreeRewriter.rewriteWith(new ExpressionRewriter<Void>() {
@Override
public Expression rewriteExpression(Expression expression, Void context, ExpressionTreeRewriter<Void> treeRewriter) {
Expression rewritten = treeRewriter.defaultRewrite(expression, null);
Type coercion = analysis.getCoercion(expression);
if (coercion != null) {
return new Cast(rewritten, coercion.getTypeSignature().toString(), false, analysis.isTypeOnlyCoercion(expression));
}
return rewritten;
}
}, bodyExpression, null);
if (!bodyType.equals(metadata.getType(returnType))) {
// Casting is safe here, since we have verified at analysis time that the actual type of the body is coercible to declared return type.
bodyExpression = new Cast(bodyExpression, statement.getReturnType());
}
body = new Return(bodyExpression);
}
return new SqlInvokedFunction(functionName, parameters, returnType, description, routineCharacteristics, formatSql(body, Optional.empty()), notVersioned());
}
Aggregations