diqiu50 commented on code in PR #13142: URL: https://github.com/apache/gravitino/pull/13142#discussion_r4013842335
########## trino-connector/trino-connector/src/main/java/org/apache/gravitino/trino/connector/util/TrinoRoutineSpecification.java: ########## @@ -0,0 +1,133 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.trino.connector.util; + +import io.trino.spi.TrinoException; +import io.trino.spi.type.Type; +import java.util.Arrays; +import java.util.stream.Collectors; +import org.apache.gravitino.function.Function; +import org.apache.gravitino.function.FunctionDefinition; +import org.apache.gravitino.trino.connector.GravitinoErrorCode; + +/** + * Builds the SQL routine specification Trino expects for a language function from a Gravitino + * function definition and its stored SQL body: + * + * <pre>FUNCTION name(params) RETURNS type [NOT] DETERMINISTIC SECURITY INVOKER body</pre> + * + * <p>The stored body is either a bare expression, which is wrapped in a {@code RETURN} statement, + * or a control statement ({@code RETURN ...} / {@code BEGIN ... END}) that is used as-is. {@code + * SECURITY INVOKER} is always declared because the function has no owner identity for Trino's + * {@code SECURITY DEFINER} default. + * + * <p>Identifiers are always quoted. Trino resolves routine and parameter names case-insensitively + * regardless of quoting, so this is equivalent to plain identifiers while also covering reserved + * words and names with special characters. + */ +public final class TrinoRoutineSpecification { + + private TrinoRoutineSpecification() {} + + /** + * Builds the routine specification for one definition of a function. + * + * @param function the Gravitino function + * @param definition the definition whose parameters and return type describe the routine + * @param sql the stored SQL body + * @param typeTransformer converts Gravitino types to Trino types + * @return the complete routine specification + * @throws TrinoException if the definition or body is not supported or a type cannot be mapped + */ + public static String build( + Function function, + FunctionDefinition definition, + String sql, + GeneralDataTypeTransformer typeTransformer) { + if (definition.returnType() == null) { + throw new TrinoException( + GravitinoErrorCode.GRAVITINO_ILLEGAL_ARGUMENT, + "Function " + function.name() + " has a definition without a return type"); + } + String body = stripLeadingComments(sql); + if (startsWithKeyword(body, "FUNCTION")) { + throw new TrinoException( + GravitinoErrorCode.GRAVITINO_ILLEGAL_ARGUMENT, + "The SQL body of function " + + function.name() + + " must be an expression or a RETURN/BEGIN statement, not a full FUNCTION" + + " specification"); + } + + String parameters = + Arrays.stream(definition.parameters()) + .map( + param -> + quoteIdentifier(param.name()) + + " " + + formatType(typeTransformer.getTrinoType(param.dataType()))) + .collect(Collectors.joining(", ", "(", ")")); + String statement = + startsWithKeyword(body, "RETURN") || startsWithKeyword(body, "BEGIN") + ? body + : "RETURN " + body; Review Comment: Good catch. A parameter name now shadows the keyword: when the first token of the body is `return`, `begin` or `function` and a parameter with that name exists, the body is treated as an expression, so `return + 1` becomes `RETURN return + 1`. The consequence is that a parameter with one of these names cannot use the statement form for the body, which the docs now state. `testParameterNamedLikeKeywordShadowsTheKeyword` covers all three names (plus the upper-case variant) and formats the parsed routine back with `SqlFormatter` to assert the statement is `RETURN (return + 1)`, i.e. the parameter reference is preserved. Fixed in f51496d47. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
