You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2021/01/07 11:00:44 UTC

[GitHub] [flink] twalthr commented on a change in pull request #14378: [FLINK-20522][table] Make implementing a built-in function straightforward

twalthr commented on a change in pull request #14378:
URL: https://github.com/apache/flink/pull/14378#discussion_r553258199



##########
File path: flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/SpecializedFunction.java
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.functions;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.configuration.ReadableConfig;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.catalog.DataTypeFactory;
+import org.apache.flink.table.types.inference.CallContext;
+
+/**
+ * A {@link FunctionDefinition} that can provide a runtime implementation (i.e. the function's body)
+ * that is specialized for the given call and session.
+ *
+ * <p>The planner tries to defer the specialization until shortly before code generation, where the
+ * information given by a {@link FunctionDefinition} is not enough anymore and a subclass of
+ * {@link UserDefinedFunction} is required for runtime.
+ *
+ * <p>This interface is useful when the runtime code should know about information that is only available
+ * after planning (e.g. local session time zone or precision/scale of decimal return type).
+ *
+ * <p>A {@link UserDefinedFunction} that is registered in the API is implicitly specialized but can
+ * also implement this interface to reconfigure itself before runtime.
+ */
+@PublicEvolving
+public interface SpecializedFunction extends FunctionDefinition {
+
+	/**
+	 * Provides a runtime implementation that is specialized for the given call and session.
+	 *
+	 * <p>The method must return an instance of {@link UserDefinedFunction} or throw a {@link TableException}
+	 * if the given call is not supported. The returned instance must have the same {@link FunctionDefinition}
+	 * semantics but can have a different {@link #getTypeInference(DataTypeFactory)} implementation.

Review comment:
       I cannot reproduce your concern. Given the following simplified code:
   ```
       @Test
       public void testSpecializedFunction() {
           final List<Row> sourceData =
                   Arrays.asList(
                           Row.of("Bob", 1, new BigDecimal("123.45")),
                           Row.of("Alice", 2, new BigDecimal("123.456")));
   
           TestCollectionTableFactory.reset();
           TestCollectionTableFactory.initData(sourceData);
   
           tEnv().executeSql(
                           "CREATE TABLE SourceTable("
                                   + "  s STRING, "
                                   + "  i INT,"
                                   + "  d DECIMAL(6, 3)"
                                   + ")"
                                   + "WITH ("
                                   + "  'connector' = 'COLLECTION'"
                                   + ")");
   
           tEnv().createTemporarySystemFunction("Innner", NumberScalarFunction.class);
           tEnv().createTemporarySystemFunction("Outerr", DoubleScalarFunction.class);
   
           final TableResult result =
                   tEnv().executeSql("SELECT " + " Outerr(Innner(s)) " + "FROM SourceTable");
       }
   
       /** A specialized "compile time" function for returning the argument's data type. */
       public static class NumberScalarFunction extends ScalarFunction implements SpecializedFunction {
   
           private final boolean doubleNumber;
   
           public NumberScalarFunction() {
               this.doubleNumber = false;
           }
   
           public NumberScalarFunction(boolean doubleNumber) {
               this.doubleNumber = doubleNumber;
           }
   
           @SuppressWarnings("unused")
           public Object eval(@DataTypeHint(inputGroup = InputGroup.ANY) Object unused) {
               if (doubleNumber) {
                   return 0D;
               } else {
                   return 0;
               }
           }
   
           @Override
           public TypeInference getTypeInference(DataTypeFactory typeFactory) {
               return TypeInference.newBuilder()
                       .outputTypeStrategy(
                               doubleNumber
                                       ? TypeStrategies.explicit(DataTypes.DOUBLE())
                                       : TypeStrategies.explicit(DataTypes.INT()))
                       .build();
           }
   
           @Override
           public NumberScalarFunction specialize(SpecializedContext context) {
               return new NumberScalarFunction(true);
           }
       }
   
       public static class DoubleScalarFunction extends ScalarFunction {
   
           public String eval(double unused) {
               return Double.toString(unused);
           }
   
           public int eval(int number) {
               return number;
           }
   
           @Override
           public TypeInference getTypeInference(DataTypeFactory typeFactory) {
               Map<InputTypeStrategy, TypeStrategy> map = new HashMap<>();
               map.put(
                       InputTypeStrategies.sequence(
                               InputTypeStrategies.logical(LogicalTypeRoot.DOUBLE)),
                       TypeStrategies.explicit(DataTypes.DOUBLE()));
               map.put(
                       InputTypeStrategies.sequence(
                               InputTypeStrategies.logical(LogicalTypeRoot.INTEGER)),
                       TypeStrategies.explicit(DataTypes.INT()));
               return TypeInference.newBuilder()
                       .inputTypeStrategy(
                               InputTypeStrategies.sequence(
                                       InputTypeStrategies.or(
                                               InputTypeStrategies.logical(LogicalTypeRoot.DOUBLE),
                                               InputTypeStrategies.logical(LogicalTypeRoot.INTEGER))))
                       .outputTypeStrategy(TypeStrategies.mapping(map))
                       .build();
           }
       }
   ```




----------------------------------------------------------------
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:
users@infra.apache.org