use of com.facebook.presto.metadata.SessionFunctionHandle.SESSION_NAMESPACE 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