LadyForest commented on code in PR #22904:
URL: https://github.com/apache/flink/pull/22904#discussion_r1259336884
##########
flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/extraction/TypeInferenceExtractorTest.java:
##########
@@ -577,6 +839,18 @@ static TestSpec forTableAggregateFunction(
new DataTypeFactoryMock(), function));
}
+ static TestSpec forProcedure(Class<? extends Procedure> procedure) {
+ return forProcedure(null, procedure);
+ }
+
+ static TestSpec forProcedure(String description, Class<? extends
Procedure> procedure) {
Review Comment:
Nit: add `@Nullable` annotation
##########
flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/extraction/TypeInferenceExtractorTest.java:
##########
@@ -927,4 +1201,238 @@ private static class DataTypeHintOnScalarFunction
extends ScalarFunction {
return null;
}
}
+
+ @ProcedureHint(
+ input = {@DataTypeHint("INT"), @DataTypeHint("STRING")},
+ argumentNames = {"i", "s"},
+ output = @DataTypeHint("BOOLEAN"))
+ private static class FullProcedureHint implements Procedure {
+ public Boolean[] call(Object procedureContext, Integer i, String s) {
+ return null;
+ }
+ }
+
+ private static class ComplexProcedureHint implements Procedure {
+ @ProcedureHint(
+ input = {@DataTypeHint("ARRAY<INT>"), @DataTypeHint(inputGroup
= InputGroup.ANY)},
+ argumentNames = {"myInt", "myAny"},
+ output = @DataTypeHint("BOOLEAN"),
+ isVarArgs = true)
+ public Boolean[] call(Object procedureContext, Object... o) {
+ return null;
+ }
+ }
+
+ @ProcedureHint(input = @DataTypeHint("INT"), output = @DataTypeHint("INT"))
+ @ProcedureHint(input = @DataTypeHint("BIGINT"), output =
@DataTypeHint("BIGINT"))
+ private static class FullProcedureHints implements Procedure {
+ public Number[] call(Object procedureContext, Number n) {
+ return null;
+ }
+ }
+
+ @ProcedureHint(output = @DataTypeHint("INT"))
+ private static class GlobalOutputProcedureHint implements Procedure {
+ @ProcedureHint(input = @DataTypeHint("INT"))
+ public Integer[] call(Object procedureContext, Integer n) {
+ return null;
+ }
+
+ @ProcedureHint(input = @DataTypeHint("STRING"))
+ public Integer[] call(Object procedureContext, String n) {
+ return null;
+ }
+ }
+
+ @ProcedureHint(output = @DataTypeHint("INT"))
+ private static class InvalidSingleOutputProcedureHint implements Procedure
{
+ @ProcedureHint(output = @DataTypeHint("TINYINT"))
+ public Integer call(Object procedureContext, Number n) {
+ return null;
+ }
+ }
+
+ @ProcedureHint(input = @DataTypeHint("INT"), output = @DataTypeHint("INT"))
+ private static class SplitFullProcedureHints implements Procedure {
+ @ProcedureHint(input = @DataTypeHint("BIGINT"), output =
@DataTypeHint("BIGINT"))
+ public Number[] call(Object procedureContext, Number n) {
+ return null;
+ }
+ }
+
+ @ProcedureHint(input = @DataTypeHint("INT"), output = @DataTypeHint("INT"))
+ private static class InvalidFullOutputProcedureHint implements Procedure {
+ @ProcedureHint(input = @DataTypeHint("INT"), output =
@DataTypeHint("BIGINT"))
+ public Number[] call(Object procedureContext, Integer i) {
+ return null;
+ }
+ }
+
+ @ProcedureHint(input = @DataTypeHint("INT"), argumentNames = "a", output =
@DataTypeHint("INT"))
+ private static class InvalidFullOutputProcedureWithArgNamesHint implements
Procedure {
+ @ProcedureHint(
+ input = @DataTypeHint("INT"),
+ argumentNames = "b",
+ output = @DataTypeHint("BIGINT"))
+ public Number[] call(Object procedureContext, Integer i) {
+ return null;
+ }
+ }
+
+ @ProcedureHint(input = @DataTypeHint("INT"))
+ private static class InvalidLocalOutputProcedureHint implements Procedure {
+ @ProcedureHint(output = @DataTypeHint("INT"))
+ public Integer[] call(Object procedureContext, Integer n) {
+ return null;
+ }
+
+ @ProcedureHint(output = @DataTypeHint("STRING"))
+ public Integer[] call(Object procedureContext, String n) {
+ return null;
+ }
+ }
+
+ @ProcedureHint(
+ input = {@DataTypeHint("INT"), @DataTypeHint()},
+ output = @DataTypeHint("BOOLEAN"))
+ private static class IncompleteProcedureHint implements Procedure {
+ public Boolean[] call(Object procedureContext, Integer i1, Integer i2)
{
+ return null;
+ }
+ }
+
+ @ProcedureHint(input = @DataTypeHint("INT"))
+ @ProcedureHint(input = @DataTypeHint("BIGINT"))
+ private static class GlobalInputProcedureHints implements Procedure {
+ @ProcedureHint(output = @DataTypeHint("INT"))
+ public Integer[] call(Object procedureContext, Number n) {
+ return null;
+ }
+ }
+
+ private static class ZeroArgProcedure implements Procedure {
+ public Integer[] call(Object procedureContext) {
+ return null;
+ }
+ }
+
+ private static class MixedArgProcedure implements Procedure {
+ public Integer[] call(Object procedureContext, int i, Double d) {
+ return null;
+ }
+ }
+
+ private static class OverloadedProcedure implements Procedure {
+ public Integer[] call(Object procedureContext, int i, Double d) {
+ return null;
+ }
+
+ public long[] call(Object procedureContext, String s) {
+ return null;
+ }
+ }
+
+ private static class VarArgProcedure implements Procedure {
+ public String[] call(Object procedureContext, int i, int... more) {
+ return null;
+ }
+ }
+
+ private static class VarArgWithByteProcedure implements Procedure {
+ public String[] call(Object procedureContext, byte... bytes) {
+ return null;
+ }
+ }
+
+ @ProcedureHint(output = @DataTypeHint("INT"))
+ private static class ExtractWithOutputHintProcedure implements Procedure {
+ public Object[] call(Object procedureContext, Integer i) {
+ return null;
+ }
+ }
+
+ @ProcedureHint(
+ input = {@DataTypeHint("INT"), @DataTypeHint("BOOLEAN")},
+ argumentNames = {"i", "b"})
+ private static class ExtractWithInputHintProcedure implements Procedure {
+ public double[] call(Object procedureContext, Object... o) {
+ return new double[] {0.0};
+ }
+ }
+
+ @ProcedureHint(output = @DataTypeHint("STRING"))
+ private static class InvalidMethodProcedure implements Procedure {
+ public Long[] call(Object procedureContext, int[] i) {
+ return null;
+ }
+ }
+
+ private static class NamedArgumentsProcedure implements Procedure {
+ public Integer[] call(Object procedureContext, int n) {
+ return null;
+ }
+
+ public Integer[] call(Object procedureContext, long n) {
+ return null;
+ }
+
+ public Integer[] call(Object procedureContext,
@DataTypeHint("DECIMAL(10, 2)") Object n) {
+ return null;
+ }
+ }
+
+ private static class InputGroupProcedure implements Procedure {
+ public String[] call(
+ Object procedureContext, @DataTypeHint(inputGroup =
InputGroup.ANY) Object o) {
+ return null;
+ }
+ }
+
+ private static class VarArgInputGroupProcedure implements Procedure {
+ public String[] call(
+ Object procedureContext, @DataTypeHint(inputGroup =
InputGroup.ANY) Object... o) {
+ return null;
+ }
+ }
+
+ // extracted order is f(INT) || f(BIGINT) due to method signature sorting
+ private static class OrderedProcedure implements Procedure {
+ public Long[] call(Object procedureContext, Long l) {
+ return null;
+ }
+
+ public Integer[] call(Object procedureContext, Integer i) {
+ return null;
+ }
+ }
+
+ // extracted order is f(BIGINT) || f(INT)
+ @ProcedureHint(input = @DataTypeHint("BIGINT"), output =
@DataTypeHint("BIGINT"))
+ @ProcedureHint(input = @DataTypeHint("INT"), output = @DataTypeHint("INT"))
+ private static class OrderedProcedure2 implements Procedure {
+ public Number[] call(Object procedureContext, Number n) {
+ return null;
+ }
+ }
+
+ // extracted order is f(BIGINT) || f(INT)
+ private static class OrderedProcedure3 implements Procedure {
+ @ProcedureHint(input = @DataTypeHint("BIGINT"), output =
@DataTypeHint("BIGINT"))
+ @ProcedureHint(input = @DataTypeHint("INT"), output =
@DataTypeHint("INT"))
+ public Number[] call(Object procedureContext, Number n) {
+ return null;
+ }
+ }
+
+ private static class DataTypeHintOnProcedure implements Procedure {
+ public @DataTypeHint("ROW<i INT>") RowData[] call(Object
procedureContext) {
+ return null;
+ }
+ }
+
+ private static class MissMethodProcedure implements Procedure {
Review Comment:
Nit: `MissingMethodPrecedure`
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/extraction/FunctionTemplate.java:
##########
@@ -133,10 +149,19 @@ private static class DefaultAnnotationHelper {
// no implementation
}
+ @ProcedureHint
Review Comment:
Maybe we can simplify the changes like
```java
Index:
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/extraction/FunctionTemplate.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git
a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/extraction/FunctionTemplate.java
b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/extraction/FunctionTemplate.java
---
a/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/extraction/FunctionTemplate.java
(revision 0dae399acf5aaf04cdc3cfddada4a7b38d7f2395)
+++
b/flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/extraction/FunctionTemplate.java
(date 1689241074609)
@@ -26,6 +26,7 @@
import javax.annotation.Nullable;
+import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Function;
@@ -42,8 +43,6 @@
@Internal
final class FunctionTemplate {
- private static final FunctionHint DEFAULT_ANNOTATION =
getDefaultAnnotation();
-
private final @Nullable FunctionSignatureTemplate signatureTemplate;
private final @Nullable FunctionResultTemplate accumulatorTemplate;
@@ -144,35 +143,27 @@
//
--------------------------------------------------------------------------------------------
+ @ProcedureHint
@FunctionHint
private static class DefaultAnnotationHelper {
// no implementation
}
- @ProcedureHint
- private static class DefaultProcedureAnnotationHelper {
- // no implementation
- }
-
- private static FunctionHint getDefaultAnnotation() {
- return
DefaultAnnotationHelper.class.getAnnotation(FunctionHint.class);
- }
-
- private static ProcedureHint getDefaultProcedureHintAnnotation() {
- return
DefaultProcedureAnnotationHelper.class.getAnnotation(ProcedureHint.class);
+ private static <H extends Annotation> H getDefaultAnnotation(Class<H>
hClass) {
+ return DefaultAnnotationHelper.class.getAnnotation(hClass);
}
private static <T> T defaultAsNull(FunctionHint hint,
Function<FunctionHint, T> accessor) {
- final T defaultValue = accessor.apply(DEFAULT_ANNOTATION);
- final T actualValue = accessor.apply(hint);
- if (Objects.deepEquals(defaultValue, actualValue)) {
- return null;
- }
- return actualValue;
+ return defaultAsNull(hint,
getDefaultAnnotation(FunctionHint.class), accessor);
}
private static <T> T defaultAsNull(ProcedureHint hint,
Function<ProcedureHint, T> accessor) {
- final T defaultValue =
accessor.apply(getDefaultProcedureHintAnnotation());
+ return defaultAsNull(hint,
getDefaultAnnotation(ProcedureHint.class), accessor);
+ }
+
+ private static <T, H extends Annotation> T defaultAsNull(
+ H hint, H defaultHint, Function<H, T> accessor) {
+ final T defaultValue = accessor.apply(defaultHint);
final T actualValue = accessor.apply(hint);
if (Objects.deepEquals(defaultValue, actualValue)) {
return null;
```
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/extraction/TemplateUtils.java:
##########
@@ -69,6 +85,21 @@ static Set<FunctionTemplate> asFunctionTemplates(
.collect(Collectors.toCollection(LinkedHashSet::new));
}
+ /** Converts {@link ProcedureHint}s to {@link FunctionTemplate}. */
+ static Set<FunctionTemplate> asFunctionTemplatesForProcedure(
+ DataTypeFactory typeFactory, Set<ProcedureHint> hints) {
+ return hints.stream()
+ .map(
+ hint -> {
+ try {
+ return
FunctionTemplate.fromAnnotation(typeFactory, hint);
+ } catch (Throwable t) {
+ throw extractionError(t, "Error in function
hint annotation.");
Review Comment:
Nit: Error in procedure hint annotation
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/extraction/BaseMappingExtractor.java:
##########
@@ -0,0 +1,433 @@
+/*
+ * 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.types.extraction;
+
+import org.apache.flink.table.annotation.DataTypeHint;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.catalog.DataTypeFactory;
+import org.apache.flink.table.functions.UserDefinedFunction;
+import org.apache.flink.table.procedures.Procedure;
+import org.apache.flink.table.types.CollectionDataType;
+import org.apache.flink.table.types.DataType;
+
+import javax.annotation.Nullable;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Parameter;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
+import static
org.apache.flink.table.types.extraction.ExtractionUtils.createMethodSignatureString;
+import static
org.apache.flink.table.types.extraction.ExtractionUtils.extractionError;
+import static
org.apache.flink.table.types.extraction.TemplateUtils.findInputOnlyTemplates;
+import static
org.apache.flink.table.types.extraction.TemplateUtils.findResultMappingTemplates;
+import static
org.apache.flink.table.types.extraction.TemplateUtils.findResultOnlyTemplate;
+import static
org.apache.flink.table.types.extraction.TemplateUtils.findResultOnlyTemplates;
+
+/**
+ * Base utility for extracting function/procedure mappings from signature to
result, e.g. from (INT,
+ * STRING) to BOOLEAN.
+ *
+ * <p>It can not only be used for {@link UserDefinedFunction}, but also for
{@link Procedure} which
+ * is almost same to {@link UserDefinedFunction} with regarding to extracting
the mapping from
Review Comment:
Nit: either use `is almost same to {@link UserDefinedFunction} with regard
to extracting ...` or `is almost same to {@link UserDefinedFunction} regarding
extracting ...`
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/extraction/FunctionTemplate.java:
##########
@@ -146,6 +171,15 @@ private static <T> T defaultAsNull(FunctionHint hint,
Function<FunctionHint, T>
return actualValue;
}
+ private static <T> T defaultAsNull(ProcedureHint hint,
Function<ProcedureHint, T> accessor) {
+ final T defaultValue =
accessor.apply(getDefaultProcedureHintAnnotation());
+ final T actualValue = accessor.apply(hint);
+ if (Objects.deepEquals(defaultValue, actualValue)) {
+ return null;
+ }
+ return actualValue;
+ }
+
Review Comment:
Nit: What about
```java
private static <T> T defaultAsNull(FunctionHint hint,
Function<FunctionHint, T> accessor) {
return defaultAsNull(hint, DEFAULT_ANNOTATION, accessor);
}
private static <T> T defaultAsNull(ProcedureHint hint,
Function<ProcedureHint, T> accessor) {
return defaultAsNull(hint, getDefaultProcedureHintAnnotation(),
accessor);
}
private static <T, H> T defaultAsNull(H hint, H defaultHint, Function<H,
T> accessor) {
final T defaultValue = accessor.apply(defaultHint);
final T actualValue = accessor.apply(hint);
if (Objects.deepEquals(defaultValue, actualValue)) {
return null;
}
return actualValue;
}
``
--
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]