yuqi1129 commented on code in PR #13142: URL: https://github.com/apache/gravitino/pull/13142#discussion_r4013357665
########## 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: [P2] Avoid classifying a bare expression solely by its first keyword. Trino allows `return`, `begin`, and `function` as unquoted identifiers. For a parameter named `return` and a bare SQL body `return + 1`, this branch emits `return + 1` unchanged instead of `RETURN return + 1`. I verified with the Trino 440 parser that the emitted routine contains `ReturnStatement{value=+1}`, whereas the expected routine contains `ReturnStatement{value=(return + 1)}`. This silently returns a constant instead of using the argument. Similarly, `begin + 1` is mistaken for a block, and `function + 1` is rejected by the earlier full-specification check. Could we disambiguate expression bodies from statements and add regression tests for these parameter names, including an assertion that the parameter reference is preserved? -- 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]
