dawidwys commented on a change in pull request #10606: [FLINK-15009][table-common] Add a utility for creating type inference logic via reflection URL: https://github.com/apache/flink/pull/10606#discussion_r363333418
########## File path: flink-table/flink-table-common/src/main/java/org/apache/flink/table/annotation/FunctionHint.java ########## @@ -0,0 +1,163 @@ +/* + * 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.flink.table.annotation; + +import org.apache.flink.annotation.PublicEvolving; +import org.apache.flink.table.functions.UserDefinedFunction; +import org.apache.flink.table.types.inference.TypeInference; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Repeatable; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * A hint that influences the reflection-based extraction of input types, accumulator types, and output + * types for constructing the {@link TypeInference} logic of a {@link UserDefinedFunction}. + * + * <p>One or more annotations can be declared on top of a {@link UserDefinedFunction} class or individually + * for each {@code eval()/accumulate()} method for overloading function signatures. All hint parameters + * are optional. If a parameter is not defined, the default reflection-based extraction is used. Hint + * parameters defined on top of a {@link UserDefinedFunction} class are inherited by all {@code eval()/accumulate()} + * methods. + * + * <p>The following examples show how to explicitly specify function signatures as a whole or in part + * and let the default extraction do the rest: + * + * <pre> + * {@code + * // accepts (INT, STRING) and returns BOOLEAN + * @FunctionHint( + * input = [@DataTypeHint("INT"), @DataTypeHint("STRING")], + * output = @DataTypeHint("BOOLEAN") + * ) + * class X extends ScalarFunction { ... } + * + * // accepts (INT, STRING) or (BOOLEAN) and returns BOOLEAN + * @FunctionHint( + * input = [@DataTypeHint("INT"), @DataTypeHint("STRING")], + * output = @DataTypeHint("BOOLEAN") + * ) + * @FunctionHint( + * input = [@DataTypeHint("BOOLEAN")], + * output = @DataTypeHint("BOOLEAN") + * ) + * class X extends ScalarFunction { ... } + * + * // accepts (INT, STRING) or (BOOLEAN) and always returns BOOLEAN + * @FunctionHint( + * output = @DataTypeHint("BOOLEAN") + * ) + * class X extends ScalarFunction { + * @FunctionHint( + * input = [@DataTypeHint("INT"), @DataTypeHint("STRING")] + * ) + * @FunctionHint( + * input = [@DataTypeHint("BOOLEAN")] + * ) + * Object eval(Object... o) { ... } + * } + * + * // accepts (INT) or (BOOLEAN) and always returns ROW<f0 BOOLEAN, f1 INT> + * @FunctionHint( + * output = @DataTypeHint("ROW<f0 BOOLEAN, f1 INT>") + * ) + * class X extends ScalarFunction { + * Row eval(int i) { ... } + * Row eval(boolean b) { ... } + * } + * + * // accepts (ROW<f BOOLEAN>...) or (BOOLEAN...) and returns INT + * class X extends ScalarFunction { + * @FunctionHint( + * input = [@DataTypeHint("ROW<f BOOLEAN>")], + * isVarArgs = true + * ) + * int eval(Row... r) { ... } + * + * int eval(boolean... b) { ... } + * } + * + * // accepts (INT) and returns INT but allows RAW types in the accumulator type + * @FunctionHint( + * accumulator = @DataTypeHint(bridgedTo = MyClass.class, allowRawGlobally = TRUE) Review comment: Imo `allowRawPattern` should always be preferred over `allowRawGlobally`. Maybe let's point to the preferred option in the examples. ```suggestion * accumulator = @DataTypeHint(bridgedTo = my.package.MyClass.class, allowRawPattern = "my.package") ``` ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
