You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "dawidwys (via GitHub)" <gi...@apache.org> on 2023/11/20 13:47:57 UTC
[PR] [FLINK-26782] Remove PlannerExpression and related [flink]
dawidwys opened a new pull request, #23761:
URL: https://github.com/apache/flink/pull/23761
<!--
*Thank you very much for contributing to Apache Flink - we are happy that you want to help us improve Flink. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
*Please understand that we do not do this to make contributions to Flink a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
## Contribution Checklist
- Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
- Name the pull request in the form "[FLINK-XXXX] [component] Title of the pull request", where *FLINK-XXXX* should be replaced by the actual issue number. Skip *component* if you are unsure about which is the best component.
Typo fixes that have no associated JIRA issue should be named following this pattern: `[hotfix] [docs] Fix typo in event time introduction` or `[hotfix] [javadocs] Expand JavaDoc for PuncuatedWatermarkGenerator`.
- Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
- Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes. You can set up Azure Pipelines CI to do that following [this guide](https://cwiki.apache.org/confluence/display/FLINK/Azure+Pipelines#AzurePipelines-Tutorial:SettingupAzurePipelinesforaforkoftheFlinkrepository).
- Each pull request should address only one issue, not mix up code from multiple issues.
- Each commit in the pull request has a meaningful commit message (including the JIRA id)
- Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
**(The sections below can be removed for hotfixes of typos)**
-->
## What is the purpose of the change
*(For example: This pull request makes task deployment go through the blob server, rather than through RPC. That way we avoid re-transferring them on each deployment (during recovery).)*
## Brief change log
*(for example:)*
- *The TaskInfo is stored in the blob store on job creation time as a persistent artifact*
- *Deployments RPC transmits only the blob storage reference*
- *TaskManagers retrieve the TaskInfo from the blob cache*
## Verifying this change
Please make sure both new and modified tests in this PR follows the conventions defined in our code quality guide: https://flink.apache.org/contributing/code-style-and-quality-common.html#testing
*(Please pick either of the following options)*
This change is a trivial rework / code cleanup without any test coverage.
*(or)*
This change is already covered by existing tests, such as *(please describe tests)*.
*(or)*
This change added tests and can be verified as follows:
*(example:)*
- *Added integration tests for end-to-end deployment with large payloads (100MB)*
- *Extended integration test for recovery after master (JobManager) failure*
- *Added test that validates that TaskInfo is transferred only once across recoveries*
- *Manually verified the change by running a 4 node cluster with 2 JobManagers and 4 TaskManagers, a stateful streaming program, and killing one JobManager and two TaskManagers during the execution, verifying that recovery happens correctly.*
## Does this pull request potentially affect one of the following parts:
- Dependencies (does it add or upgrade a dependency): (yes / no)
- The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (yes / no)
- The serializers: (yes / no / don't know)
- The runtime per-record code paths (performance sensitive): (yes / no / don't know)
- Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (yes / no / don't know)
- The S3 file system connector: (yes / no / don't know)
## Documentation
- Does this pull request introduce a new feature? (yes / no)
- If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "jnh5y (via GitHub)" <gi...@apache.org>.
jnh5y commented on code in PR #23761:
URL: https://github.com/apache/flink/pull/23761#discussion_r1401353983
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/LegacyUserDefinedFunctionInference.java:
##########
@@ -0,0 +1,529 @@
+/*
+ * 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.Internal;
+import org.apache.flink.api.common.functions.InvalidTypesException;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.ArgumentCount;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.ConstantArgumentCount;
+import org.apache.flink.table.types.inference.InputTypeStrategy;
+import org.apache.flink.table.types.inference.Signature;
+import org.apache.flink.table.types.inference.TypeStrategy;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.utils.TypeConversions;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.guava31.com.google.common.primitives.Primitives;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+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.logical.utils.LogicalTypeUtils.toInternalConversionClass;
+import static org.apache.flink.table.types.utils.TypeConversions.fromLegacyInfoToDataType;
+
+/**
+ * Ported {@code UserDefinedFunctionUtils} to run some of the type inference for legacy functions in
+ * Table API.
+ */
+@Internal
+@Deprecated
+public class LegacyUserDefinedFunctionInference {
+
+ public static InputTypeStrategy getInputTypeStrategy(ImperativeAggregateFunction<?, ?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final DataType accType = getAccumulatorType(func);
+ final LogicalType[] input =
+ Stream.concat(Stream.of(accType), argumentDataTypes.stream())
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Optional<Method>> foundMethod =
+ Stream.of(
+ logicalTypesToInternalClasses(input),
+ logicalTypesToExternalClasses(input))
+ .map(
+ signature ->
+ getUserDefinedMethod(
+ func,
+ "accumulate",
+ signature,
+ input,
+ cls ->
+ Stream.concat(
+ Stream.of(accType),
+ Arrays.stream(cls)
+ .skip(1)
Review Comment:
Ok, I have to ask... why are we skipping the first class?
It'd be lovely if there's any way to make this more clear. (I don't have suggestions, and I imagine you have tried.)
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys commented on code in PR #23761:
URL: https://github.com/apache/flink/pull/23761#discussion_r1407464091
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/LegacyUserDefinedFunctionInference.java:
##########
@@ -0,0 +1,529 @@
+/*
+ * 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.Internal;
+import org.apache.flink.api.common.functions.InvalidTypesException;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.ArgumentCount;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.ConstantArgumentCount;
+import org.apache.flink.table.types.inference.InputTypeStrategy;
+import org.apache.flink.table.types.inference.Signature;
+import org.apache.flink.table.types.inference.TypeStrategy;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.utils.TypeConversions;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.guava31.com.google.common.primitives.Primitives;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+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.logical.utils.LogicalTypeUtils.toInternalConversionClass;
+import static org.apache.flink.table.types.utils.TypeConversions.fromLegacyInfoToDataType;
+
+/**
+ * Ported {@code UserDefinedFunctionUtils} to run some of the type inference for legacy functions in
+ * Table API.
+ */
+@Internal
+@Deprecated
+public class LegacyUserDefinedFunctionInference {
+
+ public static InputTypeStrategy getInputTypeStrategy(ImperativeAggregateFunction<?, ?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final DataType accType = getAccumulatorType(func);
+ final LogicalType[] input =
+ Stream.concat(Stream.of(accType), argumentDataTypes.stream())
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Optional<Method>> foundMethod =
+ Stream.of(
+ logicalTypesToInternalClasses(input),
+ logicalTypesToExternalClasses(input))
+ .map(
+ signature ->
+ getUserDefinedMethod(
+ func,
+ "accumulate",
+ signature,
+ input,
+ cls ->
+ Stream.concat(
+ Stream.of(accType),
+ Arrays.stream(cls)
+ .skip(1)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(
+ c))))
+ .toArray(DataType[]::new)))
+ .filter(Optional::isPresent)
+ .findAny();
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(throwOnFailure, "");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "accumulate");
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(TableFunction<?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ argumentDataTypes.stream()
+ .map(DataType::getConversionClass)
+ .toArray(Class<?>[]::new),
+ input,
+ cls ->
+ Arrays.stream(cls)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(c)))
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ public static TypeStrategy getOutputTypeStrategy(ScalarFunction func) {
+ return callContext -> {
+ final LogicalType[] params =
+ callContext.getArgumentDataTypes().stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+ Optional<Class<?>[]> evalParams = getEvalMethodSignature(func, params);
+ if (!evalParams.isPresent()) {
+ return Optional.empty();
+ }
+
+ final TypeInformation<?> userDefinedTypeInfo = func.getResultType(evalParams.get());
+ if (userDefinedTypeInfo != null) {
+ return Optional.of(fromLegacyInfoToDataType(userDefinedTypeInfo));
+ } else {
+ final Optional<Method> eval =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(params),
+ params,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+ return eval.flatMap(m -> TypeConversions.fromClassToDataType(m.getReturnType()));
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(ScalarFunction func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(input),
+ input,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ private static Optional<Class<?>[]> getEvalMethodSignature(
+ ScalarFunction func, LogicalType[] expectedTypes) {
+ return getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(expectedTypes),
+ expectedTypes,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new))
+ .map(
+ m ->
+ getParamClassesConsiderVarArgs(
+ m.isVarArgs(),
+ m.getParameterTypes(),
+ expectedTypes.length));
+ }
+
+ private static Class<?>[] getParamClassesConsiderVarArgs(
+ boolean isVarArgs, Class<?>[] matchingSignature, int expectedLength) {
+ return IntStream.range(0, expectedLength)
Review Comment:
Not sure if that's a better approach.
To be honest I like the single chain approach a bit more. What you're suggesting would look:
```
final Class<?>[] array = new Class<?>[types.length];
Arrays.setAll(array, i -> {
final LogicalType t = types[i];
if (t == null) {
return null;
} else {
return TypeConversions.fromLogicalToDataType(t)
.getConversionClass();
}
});
return array;
```
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys merged PR #23761:
URL: https://github.com/apache/flink/pull/23761
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys commented on code in PR #23761:
URL: https://github.com/apache/flink/pull/23761#discussion_r1407464091
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/LegacyUserDefinedFunctionInference.java:
##########
@@ -0,0 +1,529 @@
+/*
+ * 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.Internal;
+import org.apache.flink.api.common.functions.InvalidTypesException;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.ArgumentCount;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.ConstantArgumentCount;
+import org.apache.flink.table.types.inference.InputTypeStrategy;
+import org.apache.flink.table.types.inference.Signature;
+import org.apache.flink.table.types.inference.TypeStrategy;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.utils.TypeConversions;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.guava31.com.google.common.primitives.Primitives;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+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.logical.utils.LogicalTypeUtils.toInternalConversionClass;
+import static org.apache.flink.table.types.utils.TypeConversions.fromLegacyInfoToDataType;
+
+/**
+ * Ported {@code UserDefinedFunctionUtils} to run some of the type inference for legacy functions in
+ * Table API.
+ */
+@Internal
+@Deprecated
+public class LegacyUserDefinedFunctionInference {
+
+ public static InputTypeStrategy getInputTypeStrategy(ImperativeAggregateFunction<?, ?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final DataType accType = getAccumulatorType(func);
+ final LogicalType[] input =
+ Stream.concat(Stream.of(accType), argumentDataTypes.stream())
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Optional<Method>> foundMethod =
+ Stream.of(
+ logicalTypesToInternalClasses(input),
+ logicalTypesToExternalClasses(input))
+ .map(
+ signature ->
+ getUserDefinedMethod(
+ func,
+ "accumulate",
+ signature,
+ input,
+ cls ->
+ Stream.concat(
+ Stream.of(accType),
+ Arrays.stream(cls)
+ .skip(1)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(
+ c))))
+ .toArray(DataType[]::new)))
+ .filter(Optional::isPresent)
+ .findAny();
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(throwOnFailure, "");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "accumulate");
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(TableFunction<?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ argumentDataTypes.stream()
+ .map(DataType::getConversionClass)
+ .toArray(Class<?>[]::new),
+ input,
+ cls ->
+ Arrays.stream(cls)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(c)))
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ public static TypeStrategy getOutputTypeStrategy(ScalarFunction func) {
+ return callContext -> {
+ final LogicalType[] params =
+ callContext.getArgumentDataTypes().stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+ Optional<Class<?>[]> evalParams = getEvalMethodSignature(func, params);
+ if (!evalParams.isPresent()) {
+ return Optional.empty();
+ }
+
+ final TypeInformation<?> userDefinedTypeInfo = func.getResultType(evalParams.get());
+ if (userDefinedTypeInfo != null) {
+ return Optional.of(fromLegacyInfoToDataType(userDefinedTypeInfo));
+ } else {
+ final Optional<Method> eval =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(params),
+ params,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+ return eval.flatMap(m -> TypeConversions.fromClassToDataType(m.getReturnType()));
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(ScalarFunction func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(input),
+ input,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ private static Optional<Class<?>[]> getEvalMethodSignature(
+ ScalarFunction func, LogicalType[] expectedTypes) {
+ return getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(expectedTypes),
+ expectedTypes,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new))
+ .map(
+ m ->
+ getParamClassesConsiderVarArgs(
+ m.isVarArgs(),
+ m.getParameterTypes(),
+ expectedTypes.length));
+ }
+
+ private static Class<?>[] getParamClassesConsiderVarArgs(
+ boolean isVarArgs, Class<?>[] matchingSignature, int expectedLength) {
+ return IntStream.range(0, expectedLength)
Review Comment:
Not sure if that's a better approach.
To be honest I like the single chain approach a bit more. What you're suggesting would look:
```
final Class<?>[] array = new Class<?>[expectedLength];
Arrays.setAll(array, i -> {
if (i < matchingSignature.length - 1) {
return matchingSignature[i];
} else if (isVarArgs) {
return matchingSignature[matchingSignature.length - 1]
.getComponentType();
} else {
// last argument is not an array type
return matchingSignature[matchingSignature.length - 1];
}
});
return array;
```
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys commented on code in PR #23761:
URL: https://github.com/apache/flink/pull/23761#discussion_r1400387732
##########
flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/plan/stream/table/validation/AggregateValidationTest.scala:
##########
@@ -83,21 +83,6 @@ class AggregateValidationTest extends TableTestBase {
.select('d.start))
}
- @Test
- def testTableFunctionInSelection(): Unit = {
Review Comment:
The test did not verify what it says it does. The stacktrace is as follows (run from master):
```
org.apache.flink.table.api.TableException: Functions implemented for the old type system are not supported.
at org.apache.flink.table.functions.TableFunctionDefinition.getTypeInference(TableFunctionDefinition.java:71)
at org.apache.flink.table.planner.expressions.converter.FunctionDefinitionConvertRule.convert(FunctionDefinitionConvertRule.java:52)
at org.apache.flink.table.planner.expressions.converter.ExpressionConverter.visit(ExpressionConverter.java:97)
at org.apache.flink.table.planner.expressions.converter.ExpressionConverter.visit(ExpressionConverter.java:71)
at org.apache.flink.table.expressions.CallExpression.accept(CallExpression.java:226)
at org.apache.flink.table.planner.plan.QueryOperationConverter.convertExprToRexNode(QueryOperationConverter.java:836)
at org.apache.flink.table.planner.plan.QueryOperationConverter.access$800(QueryOperationConverter.java:137)
at org.apache.flink.table.planner.plan.QueryOperationConverter$SingleRelVisitor.lambda$convertToRexNodes$6(QueryOperationConverter.java:623)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
at org.apache.flink.table.planner.plan.QueryOperationConverter$SingleRelVisitor.convertToRexNodes(QueryOperationConverter.java:624)
at org.apache.flink.table.planner.plan.QueryOperationConverter$SingleRelVisitor.visit(QueryOperationConverter.java:163)
at org.apache.flink.table.planner.plan.QueryOperationConverter$SingleRelVisitor.visit(QueryOperationConverter.java:159)
at org.apache.flink.table.operations.ProjectQueryOperation.accept(ProjectQueryOperation.java:76)
at org.apache.flink.table.planner.plan.QueryOperationConverter.defaultMethod(QueryOperationConverter.java:156)
at org.apache.flink.table.planner.plan.QueryOperationConverter.defaultMethod(QueryOperationConverter.java:136)
at org.apache.flink.table.operations.utils.QueryOperationDefaultVisitor.visit(QueryOperationDefaultVisitor.java:47)
at org.apache.flink.table.operations.ProjectQueryOperation.accept(ProjectQueryOperation.java:76)
at org.apache.flink.table.planner.calcite.FlinkRelBuilder.queryOperation(FlinkRelBuilder.java:261)
at org.apache.flink.table.planner.utils.TableTestUtil$.toRelNode(TableTestBase.scala:1711)
at org.apache.flink.table.planner.utils.TableTestUtilBase.doVerifyPlan(TableTestBase.scala:950)
at org.apache.flink.table.planner.utils.TableTestUtilBase.verifyExecPlan(TableTestBase.scala:681)
at org.apache.flink.table.planner.plan.stream.table.validation.AggregateValidationTest.testTableFunctionInSelection(AggregateValidationTest.scala:97)
```
Since it a) verifies legacy behaviour b) it's impossible to tell when it stopped verifying what it was supposed to check I decided to just remove it.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "jnh5y (via GitHub)" <gi...@apache.org>.
jnh5y commented on code in PR #23761:
URL: https://github.com/apache/flink/pull/23761#discussion_r1406305482
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/LegacyUserDefinedFunctionInference.java:
##########
@@ -0,0 +1,529 @@
+/*
+ * 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.Internal;
+import org.apache.flink.api.common.functions.InvalidTypesException;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.ArgumentCount;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.ConstantArgumentCount;
+import org.apache.flink.table.types.inference.InputTypeStrategy;
+import org.apache.flink.table.types.inference.Signature;
+import org.apache.flink.table.types.inference.TypeStrategy;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.utils.TypeConversions;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.guava31.com.google.common.primitives.Primitives;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+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.logical.utils.LogicalTypeUtils.toInternalConversionClass;
+import static org.apache.flink.table.types.utils.TypeConversions.fromLegacyInfoToDataType;
+
+/**
+ * Ported {@code UserDefinedFunctionUtils} to run some of the type inference for legacy functions in
+ * Table API.
+ */
+@Internal
+@Deprecated
+public class LegacyUserDefinedFunctionInference {
+
+ public static InputTypeStrategy getInputTypeStrategy(ImperativeAggregateFunction<?, ?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final DataType accType = getAccumulatorType(func);
+ final LogicalType[] input =
+ Stream.concat(Stream.of(accType), argumentDataTypes.stream())
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Optional<Method>> foundMethod =
+ Stream.of(
+ logicalTypesToInternalClasses(input),
+ logicalTypesToExternalClasses(input))
+ .map(
+ signature ->
+ getUserDefinedMethod(
+ func,
+ "accumulate",
+ signature,
+ input,
+ cls ->
+ Stream.concat(
+ Stream.of(accType),
+ Arrays.stream(cls)
+ .skip(1)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(
+ c))))
+ .toArray(DataType[]::new)))
+ .filter(Optional::isPresent)
+ .findAny();
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(throwOnFailure, "");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "accumulate");
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(TableFunction<?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ argumentDataTypes.stream()
+ .map(DataType::getConversionClass)
+ .toArray(Class<?>[]::new),
+ input,
+ cls ->
+ Arrays.stream(cls)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(c)))
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ public static TypeStrategy getOutputTypeStrategy(ScalarFunction func) {
+ return callContext -> {
+ final LogicalType[] params =
+ callContext.getArgumentDataTypes().stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+ Optional<Class<?>[]> evalParams = getEvalMethodSignature(func, params);
+ if (!evalParams.isPresent()) {
+ return Optional.empty();
+ }
+
+ final TypeInformation<?> userDefinedTypeInfo = func.getResultType(evalParams.get());
+ if (userDefinedTypeInfo != null) {
+ return Optional.of(fromLegacyInfoToDataType(userDefinedTypeInfo));
+ } else {
+ final Optional<Method> eval =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(params),
+ params,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+ return eval.flatMap(m -> TypeConversions.fromClassToDataType(m.getReturnType()));
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(ScalarFunction func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(input),
+ input,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ private static Optional<Class<?>[]> getEvalMethodSignature(
+ ScalarFunction func, LogicalType[] expectedTypes) {
+ return getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(expectedTypes),
+ expectedTypes,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new))
+ .map(
+ m ->
+ getParamClassesConsiderVarArgs(
+ m.isVarArgs(),
+ m.getParameterTypes(),
+ expectedTypes.length));
+ }
+
+ private static Class<?>[] getParamClassesConsiderVarArgs(
+ boolean isVarArgs, Class<?>[] matchingSignature, int expectedLength) {
+ return IntStream.range(0, expectedLength)
+ .mapToObj(
+ i -> {
+ if (i < matchingSignature.length - 1) {
+ return matchingSignature[i];
+ } else if (isVarArgs) {
+ return matchingSignature[matchingSignature.length - 1]
+ .getComponentType();
+ } else {
+ // last argument is not an array type
+ return matchingSignature[matchingSignature.length - 1];
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static DataType getAccumulatorType(ImperativeAggregateFunction<?, ?> func) {
+ final TypeInformation<?> accType = func.getAccumulatorType();
+ if (accType != null) {
+ return fromLegacyInfoToDataType(accType);
+ } else {
+ try {
+ return fromLegacyInfoToDataType(
+ TypeExtractor.createTypeInfo(
+ func, ImperativeAggregateFunction.class, func.getClass(), 1));
+ } catch (InvalidTypesException ite) {
+ throw new TableException(
+ String.format(
+ "Cannot infer generic type of %s}. You can override"
+ + " ImperativeAggregateFunction.getAccumulatorType()"
+ + " to specify the type.",
+ func.getClass()),
+ ite);
+ }
+ }
+ }
+
+ private static Class<?>[] logicalTypesToExternalClasses(LogicalType[] types) {
+ return Arrays.stream(types)
+ .map(
+ t -> {
+ if (t == null) {
+ return null;
+ } else {
+ return TypeConversions.fromLogicalToDataType(t)
+ .getConversionClass();
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static Class<?>[] logicalTypesToInternalClasses(LogicalType[] types) {
+ return Arrays.stream(types)
+ .map(
+ t -> {
+ if (t == null) {
+ return null;
+ } else {
+ return toInternalConversionClass(t);
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static boolean parameterClassEquals(Class<?> candidate, Class<?> expected) {
+ return candidate == null
+ || candidate == expected
+ || expected == Object.class
+ || candidate == Object.class
+ || // Special case when we don't know the type
+ expected.isPrimitive() && Primitives.wrap(expected) == candidate
+ || candidate == Date.class && (expected == Integer.class || expected == int.class)
+ || candidate == Time.class && (expected == Integer.class || expected == int.class)
+ || candidate == StringData.class && expected == String.class
+ || candidate == String.class && expected == StringData.class
+ || candidate == TimestampData.class && expected == LocalDateTime.class
+ || candidate == Timestamp.class && expected == TimestampData.class
+ || candidate == TimestampData.class && expected == Timestamp.class
+ || candidate == LocalDateTime.class && expected == TimestampData.class
Review Comment:
(nit) Reorder:
```
|| candidate == TimestampData.class && expected == LocalDateTime.class
|| candidate == LocalDateTime.class && expected == TimestampData.class
|| candidate == Timestamp.class && expected == TimestampData.class
|| candidate == TimestampData.class && expected == Timestamp.class
```
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "jnh5y (via GitHub)" <gi...@apache.org>.
jnh5y commented on code in PR #23761:
URL: https://github.com/apache/flink/pull/23761#discussion_r1406314626
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/LegacyUserDefinedFunctionInference.java:
##########
@@ -0,0 +1,529 @@
+/*
+ * 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.Internal;
+import org.apache.flink.api.common.functions.InvalidTypesException;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.ArgumentCount;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.ConstantArgumentCount;
+import org.apache.flink.table.types.inference.InputTypeStrategy;
+import org.apache.flink.table.types.inference.Signature;
+import org.apache.flink.table.types.inference.TypeStrategy;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.utils.TypeConversions;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.guava31.com.google.common.primitives.Primitives;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+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.logical.utils.LogicalTypeUtils.toInternalConversionClass;
+import static org.apache.flink.table.types.utils.TypeConversions.fromLegacyInfoToDataType;
+
+/**
+ * Ported {@code UserDefinedFunctionUtils} to run some of the type inference for legacy functions in
+ * Table API.
+ */
+@Internal
+@Deprecated
+public class LegacyUserDefinedFunctionInference {
+
+ public static InputTypeStrategy getInputTypeStrategy(ImperativeAggregateFunction<?, ?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final DataType accType = getAccumulatorType(func);
+ final LogicalType[] input =
+ Stream.concat(Stream.of(accType), argumentDataTypes.stream())
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Optional<Method>> foundMethod =
+ Stream.of(
+ logicalTypesToInternalClasses(input),
+ logicalTypesToExternalClasses(input))
+ .map(
+ signature ->
+ getUserDefinedMethod(
+ func,
+ "accumulate",
+ signature,
+ input,
+ cls ->
+ Stream.concat(
+ Stream.of(accType),
+ Arrays.stream(cls)
+ .skip(1)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(
+ c))))
+ .toArray(DataType[]::new)))
+ .filter(Optional::isPresent)
+ .findAny();
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(throwOnFailure, "");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "accumulate");
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(TableFunction<?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ argumentDataTypes.stream()
+ .map(DataType::getConversionClass)
+ .toArray(Class<?>[]::new),
+ input,
+ cls ->
+ Arrays.stream(cls)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(c)))
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ public static TypeStrategy getOutputTypeStrategy(ScalarFunction func) {
+ return callContext -> {
+ final LogicalType[] params =
+ callContext.getArgumentDataTypes().stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+ Optional<Class<?>[]> evalParams = getEvalMethodSignature(func, params);
+ if (!evalParams.isPresent()) {
+ return Optional.empty();
+ }
+
+ final TypeInformation<?> userDefinedTypeInfo = func.getResultType(evalParams.get());
+ if (userDefinedTypeInfo != null) {
+ return Optional.of(fromLegacyInfoToDataType(userDefinedTypeInfo));
+ } else {
+ final Optional<Method> eval =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(params),
+ params,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+ return eval.flatMap(m -> TypeConversions.fromClassToDataType(m.getReturnType()));
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(ScalarFunction func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(input),
+ input,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ private static Optional<Class<?>[]> getEvalMethodSignature(
+ ScalarFunction func, LogicalType[] expectedTypes) {
+ return getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(expectedTypes),
+ expectedTypes,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new))
+ .map(
+ m ->
+ getParamClassesConsiderVarArgs(
+ m.isVarArgs(),
+ m.getParameterTypes(),
+ expectedTypes.length));
+ }
+
+ private static Class<?>[] getParamClassesConsiderVarArgs(
+ boolean isVarArgs, Class<?>[] matchingSignature, int expectedLength) {
+ return IntStream.range(0, expectedLength)
+ .mapToObj(
+ i -> {
+ if (i < matchingSignature.length - 1) {
+ return matchingSignature[i];
+ } else if (isVarArgs) {
+ return matchingSignature[matchingSignature.length - 1]
+ .getComponentType();
+ } else {
+ // last argument is not an array type
+ return matchingSignature[matchingSignature.length - 1];
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static DataType getAccumulatorType(ImperativeAggregateFunction<?, ?> func) {
+ final TypeInformation<?> accType = func.getAccumulatorType();
+ if (accType != null) {
+ return fromLegacyInfoToDataType(accType);
+ } else {
+ try {
+ return fromLegacyInfoToDataType(
+ TypeExtractor.createTypeInfo(
+ func, ImperativeAggregateFunction.class, func.getClass(), 1));
+ } catch (InvalidTypesException ite) {
+ throw new TableException(
+ String.format(
+ "Cannot infer generic type of %s}. You can override"
+ + " ImperativeAggregateFunction.getAccumulatorType()"
+ + " to specify the type.",
+ func.getClass()),
+ ite);
+ }
+ }
+ }
+
+ private static Class<?>[] logicalTypesToExternalClasses(LogicalType[] types) {
+ return Arrays.stream(types)
+ .map(
+ t -> {
+ if (t == null) {
+ return null;
+ } else {
+ return TypeConversions.fromLogicalToDataType(t)
+ .getConversionClass();
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static Class<?>[] logicalTypesToInternalClasses(LogicalType[] types) {
+ return Arrays.stream(types)
+ .map(
+ t -> {
+ if (t == null) {
+ return null;
+ } else {
+ return toInternalConversionClass(t);
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static boolean parameterClassEquals(Class<?> candidate, Class<?> expected) {
+ return candidate == null
+ || candidate == expected
+ || expected == Object.class
+ || candidate == Object.class
+ || // Special case when we don't know the type
+ expected.isPrimitive() && Primitives.wrap(expected) == candidate
+ || candidate == Date.class && (expected == Integer.class || expected == int.class)
+ || candidate == Time.class && (expected == Integer.class || expected == int.class)
+ || candidate == StringData.class && expected == String.class
+ || candidate == String.class && expected == StringData.class
+ || candidate == TimestampData.class && expected == LocalDateTime.class
+ || candidate == Timestamp.class && expected == TimestampData.class
+ || candidate == TimestampData.class && expected == Timestamp.class
+ || candidate == LocalDateTime.class && expected == TimestampData.class
+ || candidate == TimestampData.class && expected == Instant.class
+ || candidate == Instant.class && expected == TimestampData.class
+ || RowData.class.isAssignableFrom(candidate) && expected == Row.class
+ || candidate == Row.class && RowData.class.isAssignableFrom(expected)
+ || RowData.class.isAssignableFrom(candidate) && expected == RowData.class
+ || candidate == RowData.class && RowData.class.isAssignableFrom(expected)
+ || candidate == DecimalData.class && expected == BigDecimal.class
+ || candidate == BigDecimal.class && expected == DecimalData.class
+ || (candidate.isArray()
+ && expected.isArray()
+ && candidate.getComponentType() != null
+ && expected.getComponentType() == Object.class);
+ }
+
+ private static boolean parameterDataTypeEquals(LogicalType internal, DataType parameterType) {
+ if (internal.is(LogicalTypeRoot.RAW)
+ && parameterType.getLogicalType().is(LogicalTypeRoot.RAW)) {
+ return TypeConversions.fromLogicalToDataType(internal).getConversionClass()
+ == parameterType.getConversionClass();
+ } else {
+ // There is a special equal to GenericType. We need rewrite type extract to RowData
+ // etc...
+ return parameterType.getLogicalType() == internal
+ || toInternalConversionClass(internal)
+ == toInternalConversionClass(parameterType.getLogicalType());
+ }
+ }
+
+ private static Optional<Method> getUserDefinedMethod(
+ UserDefinedFunction function,
+ String methodName,
+ Class<?>[] methodSignature,
+ LogicalType[] internalTypes,
+ Function<Class<?>[], DataType[]> parameterTypesFunc) {
+ final List<Method> methods = checkAndExtractMethods(function, methodName);
+
+ final List<Method> filteredMethods =
+ methods.stream()
+ .filter(
+ cur -> {
+ final Class<?>[] parameterTypes = cur.getParameterTypes();
+ final DataType[] parameterDataTypes =
+ parameterTypesFunc.apply(parameterTypes);
+ if (cur.isVarArgs()) {
+ return varArgsMethodMatch(
+ methodSignature,
+ internalTypes,
+ parameterTypes,
+ parameterDataTypes);
+
+ } else {
+ return methodMatch(
+ methodSignature,
+ internalTypes,
+ parameterTypes,
+ parameterDataTypes);
+ }
+ })
+ .collect(Collectors.toList());
+
+ // if there is a fixed method, compiler will call this method preferentially
+
+ final List<Method> found =
+ filteredMethods.stream()
+ .sorted(Comparator.comparing(Method::isVarArgs, Boolean::compareTo))
+ .filter(cur -> !Modifier.isVolatile(cur.getModifiers()))
Review Comment:
Could this check be moved to `checkAndExtractMethods`?
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys commented on code in PR #23761:
URL: https://github.com/apache/flink/pull/23761#discussion_r1407474016
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/LegacyUserDefinedFunctionInference.java:
##########
@@ -0,0 +1,529 @@
+/*
+ * 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.Internal;
+import org.apache.flink.api.common.functions.InvalidTypesException;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.ArgumentCount;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.ConstantArgumentCount;
+import org.apache.flink.table.types.inference.InputTypeStrategy;
+import org.apache.flink.table.types.inference.Signature;
+import org.apache.flink.table.types.inference.TypeStrategy;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.utils.TypeConversions;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.guava31.com.google.common.primitives.Primitives;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+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.logical.utils.LogicalTypeUtils.toInternalConversionClass;
+import static org.apache.flink.table.types.utils.TypeConversions.fromLegacyInfoToDataType;
+
+/**
+ * Ported {@code UserDefinedFunctionUtils} to run some of the type inference for legacy functions in
+ * Table API.
+ */
+@Internal
+@Deprecated
+public class LegacyUserDefinedFunctionInference {
+
+ public static InputTypeStrategy getInputTypeStrategy(ImperativeAggregateFunction<?, ?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final DataType accType = getAccumulatorType(func);
+ final LogicalType[] input =
+ Stream.concat(Stream.of(accType), argumentDataTypes.stream())
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Optional<Method>> foundMethod =
+ Stream.of(
+ logicalTypesToInternalClasses(input),
+ logicalTypesToExternalClasses(input))
+ .map(
+ signature ->
+ getUserDefinedMethod(
+ func,
+ "accumulate",
+ signature,
+ input,
+ cls ->
+ Stream.concat(
+ Stream.of(accType),
+ Arrays.stream(cls)
+ .skip(1)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(
+ c))))
+ .toArray(DataType[]::new)))
+ .filter(Optional::isPresent)
+ .findAny();
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(throwOnFailure, "");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "accumulate");
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(TableFunction<?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ argumentDataTypes.stream()
+ .map(DataType::getConversionClass)
+ .toArray(Class<?>[]::new),
+ input,
+ cls ->
+ Arrays.stream(cls)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(c)))
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ public static TypeStrategy getOutputTypeStrategy(ScalarFunction func) {
+ return callContext -> {
+ final LogicalType[] params =
+ callContext.getArgumentDataTypes().stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+ Optional<Class<?>[]> evalParams = getEvalMethodSignature(func, params);
+ if (!evalParams.isPresent()) {
+ return Optional.empty();
+ }
+
+ final TypeInformation<?> userDefinedTypeInfo = func.getResultType(evalParams.get());
+ if (userDefinedTypeInfo != null) {
+ return Optional.of(fromLegacyInfoToDataType(userDefinedTypeInfo));
+ } else {
+ final Optional<Method> eval =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(params),
+ params,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+ return eval.flatMap(m -> TypeConversions.fromClassToDataType(m.getReturnType()));
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(ScalarFunction func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(input),
+ input,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ private static Optional<Class<?>[]> getEvalMethodSignature(
+ ScalarFunction func, LogicalType[] expectedTypes) {
+ return getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(expectedTypes),
+ expectedTypes,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new))
+ .map(
+ m ->
+ getParamClassesConsiderVarArgs(
+ m.isVarArgs(),
+ m.getParameterTypes(),
+ expectedTypes.length));
+ }
+
+ private static Class<?>[] getParamClassesConsiderVarArgs(
+ boolean isVarArgs, Class<?>[] matchingSignature, int expectedLength) {
+ return IntStream.range(0, expectedLength)
+ .mapToObj(
+ i -> {
+ if (i < matchingSignature.length - 1) {
+ return matchingSignature[i];
+ } else if (isVarArgs) {
+ return matchingSignature[matchingSignature.length - 1]
+ .getComponentType();
+ } else {
+ // last argument is not an array type
+ return matchingSignature[matchingSignature.length - 1];
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static DataType getAccumulatorType(ImperativeAggregateFunction<?, ?> func) {
+ final TypeInformation<?> accType = func.getAccumulatorType();
+ if (accType != null) {
+ return fromLegacyInfoToDataType(accType);
+ } else {
+ try {
+ return fromLegacyInfoToDataType(
+ TypeExtractor.createTypeInfo(
+ func, ImperativeAggregateFunction.class, func.getClass(), 1));
+ } catch (InvalidTypesException ite) {
+ throw new TableException(
+ String.format(
+ "Cannot infer generic type of %s}. You can override"
+ + " ImperativeAggregateFunction.getAccumulatorType()"
+ + " to specify the type.",
+ func.getClass()),
+ ite);
+ }
+ }
+ }
+
+ private static Class<?>[] logicalTypesToExternalClasses(LogicalType[] types) {
+ return Arrays.stream(types)
+ .map(
+ t -> {
+ if (t == null) {
+ return null;
+ } else {
+ return TypeConversions.fromLogicalToDataType(t)
+ .getConversionClass();
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static Class<?>[] logicalTypesToInternalClasses(LogicalType[] types) {
+ return Arrays.stream(types)
+ .map(
+ t -> {
+ if (t == null) {
+ return null;
+ } else {
+ return toInternalConversionClass(t);
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static boolean parameterClassEquals(Class<?> candidate, Class<?> expected) {
+ return candidate == null
+ || candidate == expected
+ || expected == Object.class
+ || candidate == Object.class
+ || // Special case when we don't know the type
+ expected.isPrimitive() && Primitives.wrap(expected) == candidate
+ || candidate == Date.class && (expected == Integer.class || expected == int.class)
+ || candidate == Time.class && (expected == Integer.class || expected == int.class)
+ || candidate == StringData.class && expected == String.class
+ || candidate == String.class && expected == StringData.class
+ || candidate == TimestampData.class && expected == LocalDateTime.class
+ || candidate == Timestamp.class && expected == TimestampData.class
+ || candidate == TimestampData.class && expected == Timestamp.class
+ || candidate == LocalDateTime.class && expected == TimestampData.class
+ || candidate == TimestampData.class && expected == Instant.class
+ || candidate == Instant.class && expected == TimestampData.class
+ || RowData.class.isAssignableFrom(candidate) && expected == Row.class
+ || candidate == Row.class && RowData.class.isAssignableFrom(expected)
+ || RowData.class.isAssignableFrom(candidate) && expected == RowData.class
+ || candidate == RowData.class && RowData.class.isAssignableFrom(expected)
+ || candidate == DecimalData.class && expected == BigDecimal.class
+ || candidate == BigDecimal.class && expected == DecimalData.class
+ || (candidate.isArray()
+ && expected.isArray()
+ && candidate.getComponentType() != null
+ && expected.getComponentType() == Object.class);
+ }
+
+ private static boolean parameterDataTypeEquals(LogicalType internal, DataType parameterType) {
+ if (internal.is(LogicalTypeRoot.RAW)
+ && parameterType.getLogicalType().is(LogicalTypeRoot.RAW)) {
+ return TypeConversions.fromLogicalToDataType(internal).getConversionClass()
+ == parameterType.getConversionClass();
+ } else {
+ // There is a special equal to GenericType. We need rewrite type extract to RowData
+ // etc...
+ return parameterType.getLogicalType() == internal
+ || toInternalConversionClass(internal)
+ == toInternalConversionClass(parameterType.getLogicalType());
+ }
+ }
+
+ private static Optional<Method> getUserDefinedMethod(
+ UserDefinedFunction function,
+ String methodName,
+ Class<?>[] methodSignature,
+ LogicalType[] internalTypes,
+ Function<Class<?>[], DataType[]> parameterTypesFunc) {
+ final List<Method> methods = checkAndExtractMethods(function, methodName);
+
+ final List<Method> filteredMethods =
+ methods.stream()
+ .filter(
+ cur -> {
+ final Class<?>[] parameterTypes = cur.getParameterTypes();
+ final DataType[] parameterDataTypes =
+ parameterTypesFunc.apply(parameterTypes);
+ if (cur.isVarArgs()) {
+ return varArgsMethodMatch(
+ methodSignature,
+ internalTypes,
+ parameterTypes,
+ parameterDataTypes);
+
+ } else {
+ return methodMatch(
+ methodSignature,
+ internalTypes,
+ parameterTypes,
+ parameterDataTypes);
+ }
+ })
+ .collect(Collectors.toList());
+
+ // if there is a fixed method, compiler will call this method preferentially
+
+ final List<Method> found =
+ filteredMethods.stream()
+ .sorted(Comparator.comparing(Method::isVarArgs, Boolean::compareTo))
+ .filter(cur -> !Modifier.isVolatile(cur.getModifiers()))
Review Comment:
> Could this check be moved to checkAndExtractMethods?
I'd rather keep the logic as similar to `UserDefinedFunctionUtils` as possible. Especially in such a subtle areas as this one. I don't want to analyze all possible corner cases in a class that should be removed any time soon.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #23761:
URL: https://github.com/apache/flink/pull/23761#issuecomment-1819106461
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "991a9e26507256844791ab24228ece51b27682b8",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "991a9e26507256844791ab24228ece51b27682b8",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* 991a9e26507256844791ab24228ece51b27682b8 UNKNOWN
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run azure` re-run the last Azure build
</details>
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "jnh5y (via GitHub)" <gi...@apache.org>.
jnh5y commented on code in PR #23761:
URL: https://github.com/apache/flink/pull/23761#discussion_r1407844330
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/LegacyUserDefinedFunctionInference.java:
##########
@@ -0,0 +1,529 @@
+/*
+ * 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.Internal;
+import org.apache.flink.api.common.functions.InvalidTypesException;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.ArgumentCount;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.ConstantArgumentCount;
+import org.apache.flink.table.types.inference.InputTypeStrategy;
+import org.apache.flink.table.types.inference.Signature;
+import org.apache.flink.table.types.inference.TypeStrategy;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.utils.TypeConversions;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.guava31.com.google.common.primitives.Primitives;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+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.logical.utils.LogicalTypeUtils.toInternalConversionClass;
+import static org.apache.flink.table.types.utils.TypeConversions.fromLegacyInfoToDataType;
+
+/**
+ * Ported {@code UserDefinedFunctionUtils} to run some of the type inference for legacy functions in
+ * Table API.
+ */
+@Internal
+@Deprecated
+public class LegacyUserDefinedFunctionInference {
+
+ public static InputTypeStrategy getInputTypeStrategy(ImperativeAggregateFunction<?, ?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final DataType accType = getAccumulatorType(func);
+ final LogicalType[] input =
+ Stream.concat(Stream.of(accType), argumentDataTypes.stream())
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Optional<Method>> foundMethod =
+ Stream.of(
+ logicalTypesToInternalClasses(input),
+ logicalTypesToExternalClasses(input))
+ .map(
+ signature ->
+ getUserDefinedMethod(
+ func,
+ "accumulate",
+ signature,
+ input,
+ cls ->
+ Stream.concat(
+ Stream.of(accType),
+ Arrays.stream(cls)
+ .skip(1)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(
+ c))))
+ .toArray(DataType[]::new)))
+ .filter(Optional::isPresent)
+ .findAny();
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(throwOnFailure, "");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "accumulate");
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(TableFunction<?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ argumentDataTypes.stream()
+ .map(DataType::getConversionClass)
+ .toArray(Class<?>[]::new),
+ input,
+ cls ->
+ Arrays.stream(cls)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(c)))
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ public static TypeStrategy getOutputTypeStrategy(ScalarFunction func) {
+ return callContext -> {
+ final LogicalType[] params =
+ callContext.getArgumentDataTypes().stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+ Optional<Class<?>[]> evalParams = getEvalMethodSignature(func, params);
+ if (!evalParams.isPresent()) {
+ return Optional.empty();
+ }
+
+ final TypeInformation<?> userDefinedTypeInfo = func.getResultType(evalParams.get());
+ if (userDefinedTypeInfo != null) {
+ return Optional.of(fromLegacyInfoToDataType(userDefinedTypeInfo));
+ } else {
+ final Optional<Method> eval =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(params),
+ params,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+ return eval.flatMap(m -> TypeConversions.fromClassToDataType(m.getReturnType()));
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(ScalarFunction func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(input),
+ input,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ private static Optional<Class<?>[]> getEvalMethodSignature(
+ ScalarFunction func, LogicalType[] expectedTypes) {
+ return getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(expectedTypes),
+ expectedTypes,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new))
+ .map(
+ m ->
+ getParamClassesConsiderVarArgs(
+ m.isVarArgs(),
+ m.getParameterTypes(),
+ expectedTypes.length));
+ }
+
+ private static Class<?>[] getParamClassesConsiderVarArgs(
+ boolean isVarArgs, Class<?>[] matchingSignature, int expectedLength) {
+ return IntStream.range(0, expectedLength)
Review Comment:
Here it won't matter (much).
In the old days, Scala could generate tons of intermediate objects to handle things like this. That got me used to looking for when objects were being created just to perform iteration.
This code just reminded me of situations like that!
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "jnh5y (via GitHub)" <gi...@apache.org>.
jnh5y commented on code in PR #23761:
URL: https://github.com/apache/flink/pull/23761#discussion_r1406286431
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/LegacyUserDefinedFunctionInference.java:
##########
@@ -0,0 +1,529 @@
+/*
+ * 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.Internal;
+import org.apache.flink.api.common.functions.InvalidTypesException;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.ArgumentCount;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.ConstantArgumentCount;
+import org.apache.flink.table.types.inference.InputTypeStrategy;
+import org.apache.flink.table.types.inference.Signature;
+import org.apache.flink.table.types.inference.TypeStrategy;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.utils.TypeConversions;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.guava31.com.google.common.primitives.Primitives;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+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.logical.utils.LogicalTypeUtils.toInternalConversionClass;
+import static org.apache.flink.table.types.utils.TypeConversions.fromLegacyInfoToDataType;
+
+/**
+ * Ported {@code UserDefinedFunctionUtils} to run some of the type inference for legacy functions in
+ * Table API.
+ */
+@Internal
+@Deprecated
+public class LegacyUserDefinedFunctionInference {
+
+ public static InputTypeStrategy getInputTypeStrategy(ImperativeAggregateFunction<?, ?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final DataType accType = getAccumulatorType(func);
+ final LogicalType[] input =
+ Stream.concat(Stream.of(accType), argumentDataTypes.stream())
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Optional<Method>> foundMethod =
+ Stream.of(
+ logicalTypesToInternalClasses(input),
+ logicalTypesToExternalClasses(input))
+ .map(
+ signature ->
+ getUserDefinedMethod(
+ func,
+ "accumulate",
+ signature,
+ input,
+ cls ->
+ Stream.concat(
+ Stream.of(accType),
+ Arrays.stream(cls)
+ .skip(1)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(
+ c))))
+ .toArray(DataType[]::new)))
+ .filter(Optional::isPresent)
+ .findAny();
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(throwOnFailure, "");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "accumulate");
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(TableFunction<?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ argumentDataTypes.stream()
+ .map(DataType::getConversionClass)
+ .toArray(Class<?>[]::new),
+ input,
+ cls ->
+ Arrays.stream(cls)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(c)))
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ public static TypeStrategy getOutputTypeStrategy(ScalarFunction func) {
+ return callContext -> {
+ final LogicalType[] params =
+ callContext.getArgumentDataTypes().stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+ Optional<Class<?>[]> evalParams = getEvalMethodSignature(func, params);
+ if (!evalParams.isPresent()) {
+ return Optional.empty();
+ }
+
+ final TypeInformation<?> userDefinedTypeInfo = func.getResultType(evalParams.get());
+ if (userDefinedTypeInfo != null) {
+ return Optional.of(fromLegacyInfoToDataType(userDefinedTypeInfo));
+ } else {
+ final Optional<Method> eval =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(params),
+ params,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+ return eval.flatMap(m -> TypeConversions.fromClassToDataType(m.getReturnType()));
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(ScalarFunction func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(input),
+ input,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ private static Optional<Class<?>[]> getEvalMethodSignature(
+ ScalarFunction func, LogicalType[] expectedTypes) {
+ return getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(expectedTypes),
+ expectedTypes,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new))
+ .map(
+ m ->
+ getParamClassesConsiderVarArgs(
+ m.isVarArgs(),
+ m.getParameterTypes(),
+ expectedTypes.length));
+ }
+
+ private static Class<?>[] getParamClassesConsiderVarArgs(
+ boolean isVarArgs, Class<?>[] matchingSignature, int expectedLength) {
+ return IntStream.range(0, expectedLength)
Review Comment:
(nit) No need to create a stream to iterate and then make an array.
Looks like https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#setAll-T:A-java.util.function.IntFunction- may be an option for doing this in one step / re-using the lambda.
(I don't necessarily think this needs to be addressed.)
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys commented on code in PR #23761:
URL: https://github.com/apache/flink/pull/23761#discussion_r1400387732
##########
flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/plan/stream/table/validation/AggregateValidationTest.scala:
##########
@@ -83,21 +83,6 @@ class AggregateValidationTest extends TableTestBase {
.select('d.start))
}
- @Test
- def testTableFunctionInSelection(): Unit = {
Review Comment:
The test did not verify what it says it does. The stacktrace is as follows:
```
org.apache.flink.table.api.TableException: Functions implemented for the old type system are not supported.
at org.apache.flink.table.functions.TableFunctionDefinition.getTypeInference(TableFunctionDefinition.java:71)
at org.apache.flink.table.planner.expressions.converter.FunctionDefinitionConvertRule.convert(FunctionDefinitionConvertRule.java:52)
at org.apache.flink.table.planner.expressions.converter.ExpressionConverter.visit(ExpressionConverter.java:97)
at org.apache.flink.table.planner.expressions.converter.ExpressionConverter.visit(ExpressionConverter.java:71)
at org.apache.flink.table.expressions.CallExpression.accept(CallExpression.java:226)
at org.apache.flink.table.planner.plan.QueryOperationConverter.convertExprToRexNode(QueryOperationConverter.java:836)
at org.apache.flink.table.planner.plan.QueryOperationConverter.access$800(QueryOperationConverter.java:137)
at org.apache.flink.table.planner.plan.QueryOperationConverter$SingleRelVisitor.lambda$convertToRexNodes$6(QueryOperationConverter.java:623)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
at org.apache.flink.table.planner.plan.QueryOperationConverter$SingleRelVisitor.convertToRexNodes(QueryOperationConverter.java:624)
at org.apache.flink.table.planner.plan.QueryOperationConverter$SingleRelVisitor.visit(QueryOperationConverter.java:163)
at org.apache.flink.table.planner.plan.QueryOperationConverter$SingleRelVisitor.visit(QueryOperationConverter.java:159)
at org.apache.flink.table.operations.ProjectQueryOperation.accept(ProjectQueryOperation.java:76)
at org.apache.flink.table.planner.plan.QueryOperationConverter.defaultMethod(QueryOperationConverter.java:156)
at org.apache.flink.table.planner.plan.QueryOperationConverter.defaultMethod(QueryOperationConverter.java:136)
at org.apache.flink.table.operations.utils.QueryOperationDefaultVisitor.visit(QueryOperationDefaultVisitor.java:47)
at org.apache.flink.table.operations.ProjectQueryOperation.accept(ProjectQueryOperation.java:76)
at org.apache.flink.table.planner.calcite.FlinkRelBuilder.queryOperation(FlinkRelBuilder.java:261)
at org.apache.flink.table.planner.utils.TableTestUtil$.toRelNode(TableTestBase.scala:1711)
at org.apache.flink.table.planner.utils.TableTestUtilBase.doVerifyPlan(TableTestBase.scala:950)
at org.apache.flink.table.planner.utils.TableTestUtilBase.verifyExecPlan(TableTestBase.scala:681)
at org.apache.flink.table.planner.plan.stream.table.validation.AggregateValidationTest.testTableFunctionInSelection(AggregateValidationTest.scala:97)
```
Since it a) verifies legacy behaviour b) it's impossible to tell when it stopped verifying what it was supposed to check I decided to just remove it.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys commented on code in PR #23761:
URL: https://github.com/apache/flink/pull/23761#discussion_r1407454211
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/LegacyUserDefinedFunctionInference.java:
##########
@@ -0,0 +1,529 @@
+/*
+ * 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.Internal;
+import org.apache.flink.api.common.functions.InvalidTypesException;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.ArgumentCount;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.ConstantArgumentCount;
+import org.apache.flink.table.types.inference.InputTypeStrategy;
+import org.apache.flink.table.types.inference.Signature;
+import org.apache.flink.table.types.inference.TypeStrategy;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.utils.TypeConversions;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.guava31.com.google.common.primitives.Primitives;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+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.logical.utils.LogicalTypeUtils.toInternalConversionClass;
+import static org.apache.flink.table.types.utils.TypeConversions.fromLegacyInfoToDataType;
+
+/**
+ * Ported {@code UserDefinedFunctionUtils} to run some of the type inference for legacy functions in
+ * Table API.
+ */
+@Internal
+@Deprecated
+public class LegacyUserDefinedFunctionInference {
+
+ public static InputTypeStrategy getInputTypeStrategy(ImperativeAggregateFunction<?, ?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final DataType accType = getAccumulatorType(func);
+ final LogicalType[] input =
+ Stream.concat(Stream.of(accType), argumentDataTypes.stream())
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Optional<Method>> foundMethod =
+ Stream.of(
+ logicalTypesToInternalClasses(input),
+ logicalTypesToExternalClasses(input))
+ .map(
+ signature ->
+ getUserDefinedMethod(
+ func,
+ "accumulate",
+ signature,
+ input,
+ cls ->
+ Stream.concat(
+ Stream.of(accType),
+ Arrays.stream(cls)
+ .skip(1)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(
+ c))))
+ .toArray(DataType[]::new)))
+ .filter(Optional::isPresent)
+ .findAny();
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(throwOnFailure, "");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "accumulate");
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(TableFunction<?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ argumentDataTypes.stream()
+ .map(DataType::getConversionClass)
+ .toArray(Class<?>[]::new),
+ input,
+ cls ->
+ Arrays.stream(cls)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(c)))
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ public static TypeStrategy getOutputTypeStrategy(ScalarFunction func) {
+ return callContext -> {
+ final LogicalType[] params =
+ callContext.getArgumentDataTypes().stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+ Optional<Class<?>[]> evalParams = getEvalMethodSignature(func, params);
+ if (!evalParams.isPresent()) {
+ return Optional.empty();
+ }
+
+ final TypeInformation<?> userDefinedTypeInfo = func.getResultType(evalParams.get());
+ if (userDefinedTypeInfo != null) {
+ return Optional.of(fromLegacyInfoToDataType(userDefinedTypeInfo));
+ } else {
+ final Optional<Method> eval =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(params),
+ params,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+ return eval.flatMap(m -> TypeConversions.fromClassToDataType(m.getReturnType()));
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(ScalarFunction func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(input),
+ input,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ private static Optional<Class<?>[]> getEvalMethodSignature(
+ ScalarFunction func, LogicalType[] expectedTypes) {
+ return getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(expectedTypes),
+ expectedTypes,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new))
+ .map(
+ m ->
+ getParamClassesConsiderVarArgs(
+ m.isVarArgs(),
+ m.getParameterTypes(),
+ expectedTypes.length));
+ }
+
+ private static Class<?>[] getParamClassesConsiderVarArgs(
+ boolean isVarArgs, Class<?>[] matchingSignature, int expectedLength) {
+ return IntStream.range(0, expectedLength)
+ .mapToObj(
+ i -> {
+ if (i < matchingSignature.length - 1) {
+ return matchingSignature[i];
+ } else if (isVarArgs) {
+ return matchingSignature[matchingSignature.length - 1]
+ .getComponentType();
+ } else {
+ // last argument is not an array type
+ return matchingSignature[matchingSignature.length - 1];
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static DataType getAccumulatorType(ImperativeAggregateFunction<?, ?> func) {
+ final TypeInformation<?> accType = func.getAccumulatorType();
+ if (accType != null) {
+ return fromLegacyInfoToDataType(accType);
+ } else {
+ try {
+ return fromLegacyInfoToDataType(
+ TypeExtractor.createTypeInfo(
+ func, ImperativeAggregateFunction.class, func.getClass(), 1));
+ } catch (InvalidTypesException ite) {
+ throw new TableException(
+ String.format(
+ "Cannot infer generic type of %s}. You can override"
+ + " ImperativeAggregateFunction.getAccumulatorType()"
+ + " to specify the type.",
+ func.getClass()),
+ ite);
+ }
+ }
+ }
+
+ private static Class<?>[] logicalTypesToExternalClasses(LogicalType[] types) {
+ return Arrays.stream(types)
+ .map(
+ t -> {
+ if (t == null) {
+ return null;
+ } else {
+ return TypeConversions.fromLogicalToDataType(t)
+ .getConversionClass();
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static Class<?>[] logicalTypesToInternalClasses(LogicalType[] types) {
+ return Arrays.stream(types)
+ .map(
+ t -> {
+ if (t == null) {
+ return null;
+ } else {
+ return toInternalConversionClass(t);
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static boolean parameterClassEquals(Class<?> candidate, Class<?> expected) {
+ return candidate == null
+ || candidate == expected
+ || expected == Object.class
+ || candidate == Object.class
+ || // Special case when we don't know the type
+ expected.isPrimitive() && Primitives.wrap(expected) == candidate
+ || candidate == Date.class && (expected == Integer.class || expected == int.class)
+ || candidate == Time.class && (expected == Integer.class || expected == int.class)
+ || candidate == StringData.class && expected == String.class
+ || candidate == String.class && expected == StringData.class
+ || candidate == TimestampData.class && expected == LocalDateTime.class
+ || candidate == Timestamp.class && expected == TimestampData.class
+ || candidate == TimestampData.class && expected == Timestamp.class
+ || candidate == LocalDateTime.class && expected == TimestampData.class
Review Comment:
I'd rather keep the original order from `UserDefinedFunctionUtils`
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "jnh5y (via GitHub)" <gi...@apache.org>.
jnh5y commented on code in PR #23761:
URL: https://github.com/apache/flink/pull/23761#discussion_r1406291625
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/LegacyUserDefinedFunctionInference.java:
##########
@@ -0,0 +1,529 @@
+/*
+ * 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.Internal;
+import org.apache.flink.api.common.functions.InvalidTypesException;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.ArgumentCount;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.ConstantArgumentCount;
+import org.apache.flink.table.types.inference.InputTypeStrategy;
+import org.apache.flink.table.types.inference.Signature;
+import org.apache.flink.table.types.inference.TypeStrategy;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.utils.TypeConversions;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.guava31.com.google.common.primitives.Primitives;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+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.logical.utils.LogicalTypeUtils.toInternalConversionClass;
+import static org.apache.flink.table.types.utils.TypeConversions.fromLegacyInfoToDataType;
+
+/**
+ * Ported {@code UserDefinedFunctionUtils} to run some of the type inference for legacy functions in
+ * Table API.
+ */
+@Internal
+@Deprecated
+public class LegacyUserDefinedFunctionInference {
+
+ public static InputTypeStrategy getInputTypeStrategy(ImperativeAggregateFunction<?, ?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final DataType accType = getAccumulatorType(func);
+ final LogicalType[] input =
+ Stream.concat(Stream.of(accType), argumentDataTypes.stream())
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Optional<Method>> foundMethod =
+ Stream.of(
+ logicalTypesToInternalClasses(input),
+ logicalTypesToExternalClasses(input))
+ .map(
+ signature ->
+ getUserDefinedMethod(
+ func,
+ "accumulate",
+ signature,
+ input,
+ cls ->
+ Stream.concat(
+ Stream.of(accType),
+ Arrays.stream(cls)
+ .skip(1)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(
+ c))))
+ .toArray(DataType[]::new)))
+ .filter(Optional::isPresent)
+ .findAny();
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(throwOnFailure, "");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "accumulate");
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(TableFunction<?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ argumentDataTypes.stream()
+ .map(DataType::getConversionClass)
+ .toArray(Class<?>[]::new),
+ input,
+ cls ->
+ Arrays.stream(cls)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(c)))
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ public static TypeStrategy getOutputTypeStrategy(ScalarFunction func) {
+ return callContext -> {
+ final LogicalType[] params =
+ callContext.getArgumentDataTypes().stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+ Optional<Class<?>[]> evalParams = getEvalMethodSignature(func, params);
+ if (!evalParams.isPresent()) {
+ return Optional.empty();
+ }
+
+ final TypeInformation<?> userDefinedTypeInfo = func.getResultType(evalParams.get());
+ if (userDefinedTypeInfo != null) {
+ return Optional.of(fromLegacyInfoToDataType(userDefinedTypeInfo));
+ } else {
+ final Optional<Method> eval =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(params),
+ params,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+ return eval.flatMap(m -> TypeConversions.fromClassToDataType(m.getReturnType()));
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(ScalarFunction func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(input),
+ input,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ private static Optional<Class<?>[]> getEvalMethodSignature(
+ ScalarFunction func, LogicalType[] expectedTypes) {
+ return getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(expectedTypes),
+ expectedTypes,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new))
+ .map(
+ m ->
+ getParamClassesConsiderVarArgs(
+ m.isVarArgs(),
+ m.getParameterTypes(),
+ expectedTypes.length));
+ }
+
+ private static Class<?>[] getParamClassesConsiderVarArgs(
+ boolean isVarArgs, Class<?>[] matchingSignature, int expectedLength) {
+ return IntStream.range(0, expectedLength)
+ .mapToObj(
+ i -> {
+ if (i < matchingSignature.length - 1) {
+ return matchingSignature[i];
+ } else if (isVarArgs) {
+ return matchingSignature[matchingSignature.length - 1]
+ .getComponentType();
+ } else {
+ // last argument is not an array type
+ return matchingSignature[matchingSignature.length - 1];
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static DataType getAccumulatorType(ImperativeAggregateFunction<?, ?> func) {
+ final TypeInformation<?> accType = func.getAccumulatorType();
+ if (accType != null) {
+ return fromLegacyInfoToDataType(accType);
+ } else {
+ try {
+ return fromLegacyInfoToDataType(
+ TypeExtractor.createTypeInfo(
+ func, ImperativeAggregateFunction.class, func.getClass(), 1));
+ } catch (InvalidTypesException ite) {
+ throw new TableException(
+ String.format(
+ "Cannot infer generic type of %s}. You can override"
+ + " ImperativeAggregateFunction.getAccumulatorType()"
+ + " to specify the type.",
+ func.getClass()),
+ ite);
+ }
+ }
+ }
+
+ private static Class<?>[] logicalTypesToExternalClasses(LogicalType[] types) {
+ return Arrays.stream(types)
Review Comment:
See the comment about skipping on making a stream and using Arrays.setAll.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "jnh5y (via GitHub)" <gi...@apache.org>.
jnh5y commented on code in PR #23761:
URL: https://github.com/apache/flink/pull/23761#discussion_r1406316719
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/LegacyUserDefinedFunctionInference.java:
##########
@@ -0,0 +1,529 @@
+/*
+ * 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.Internal;
+import org.apache.flink.api.common.functions.InvalidTypesException;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.ArgumentCount;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.ConstantArgumentCount;
+import org.apache.flink.table.types.inference.InputTypeStrategy;
+import org.apache.flink.table.types.inference.Signature;
+import org.apache.flink.table.types.inference.TypeStrategy;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.utils.TypeConversions;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.guava31.com.google.common.primitives.Primitives;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+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.logical.utils.LogicalTypeUtils.toInternalConversionClass;
+import static org.apache.flink.table.types.utils.TypeConversions.fromLegacyInfoToDataType;
+
+/**
+ * Ported {@code UserDefinedFunctionUtils} to run some of the type inference for legacy functions in
+ * Table API.
+ */
+@Internal
+@Deprecated
+public class LegacyUserDefinedFunctionInference {
+
+ public static InputTypeStrategy getInputTypeStrategy(ImperativeAggregateFunction<?, ?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final DataType accType = getAccumulatorType(func);
+ final LogicalType[] input =
+ Stream.concat(Stream.of(accType), argumentDataTypes.stream())
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Optional<Method>> foundMethod =
+ Stream.of(
+ logicalTypesToInternalClasses(input),
+ logicalTypesToExternalClasses(input))
+ .map(
+ signature ->
+ getUserDefinedMethod(
+ func,
+ "accumulate",
+ signature,
+ input,
+ cls ->
+ Stream.concat(
+ Stream.of(accType),
+ Arrays.stream(cls)
+ .skip(1)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(
+ c))))
+ .toArray(DataType[]::new)))
+ .filter(Optional::isPresent)
+ .findAny();
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(throwOnFailure, "");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "accumulate");
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(TableFunction<?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ argumentDataTypes.stream()
+ .map(DataType::getConversionClass)
+ .toArray(Class<?>[]::new),
+ input,
+ cls ->
+ Arrays.stream(cls)
+ .map(
+ c ->
+ fromLegacyInfoToDataType(
+ TypeExtractor
+ .createTypeInfo(c)))
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ public static TypeStrategy getOutputTypeStrategy(ScalarFunction func) {
+ return callContext -> {
+ final LogicalType[] params =
+ callContext.getArgumentDataTypes().stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+ Optional<Class<?>[]> evalParams = getEvalMethodSignature(func, params);
+ if (!evalParams.isPresent()) {
+ return Optional.empty();
+ }
+
+ final TypeInformation<?> userDefinedTypeInfo = func.getResultType(evalParams.get());
+ if (userDefinedTypeInfo != null) {
+ return Optional.of(fromLegacyInfoToDataType(userDefinedTypeInfo));
+ } else {
+ final Optional<Method> eval =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(params),
+ params,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+ return eval.flatMap(m -> TypeConversions.fromClassToDataType(m.getReturnType()));
+ }
+ };
+ }
+
+ public static InputTypeStrategy getInputTypeStrategy(ScalarFunction func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final LogicalType[] input =
+ argumentDataTypes.stream()
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Method> foundMethod =
+ getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(input),
+ input,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new));
+
+ if (foundMethod.isPresent()) {
+ return Optional.of(argumentDataTypes);
+ } else {
+ return callContext.fail(
+ throwOnFailure, "Given parameters do not match any signature.");
+ }
+ }
+
+ @Override
+ public List<Signature> getExpectedSignatures(FunctionDefinition definition) {
+ return getSignatures(func, "eval");
+ }
+ };
+ }
+
+ private static Optional<Class<?>[]> getEvalMethodSignature(
+ ScalarFunction func, LogicalType[] expectedTypes) {
+ return getUserDefinedMethod(
+ func,
+ "eval",
+ logicalTypesToExternalClasses(expectedTypes),
+ expectedTypes,
+ (paraClasses) ->
+ Arrays.stream(func.getParameterTypes(paraClasses))
+ .map(TypeConversions::fromLegacyInfoToDataType)
+ .toArray(DataType[]::new))
+ .map(
+ m ->
+ getParamClassesConsiderVarArgs(
+ m.isVarArgs(),
+ m.getParameterTypes(),
+ expectedTypes.length));
+ }
+
+ private static Class<?>[] getParamClassesConsiderVarArgs(
+ boolean isVarArgs, Class<?>[] matchingSignature, int expectedLength) {
+ return IntStream.range(0, expectedLength)
+ .mapToObj(
+ i -> {
+ if (i < matchingSignature.length - 1) {
+ return matchingSignature[i];
+ } else if (isVarArgs) {
+ return matchingSignature[matchingSignature.length - 1]
+ .getComponentType();
+ } else {
+ // last argument is not an array type
+ return matchingSignature[matchingSignature.length - 1];
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static DataType getAccumulatorType(ImperativeAggregateFunction<?, ?> func) {
+ final TypeInformation<?> accType = func.getAccumulatorType();
+ if (accType != null) {
+ return fromLegacyInfoToDataType(accType);
+ } else {
+ try {
+ return fromLegacyInfoToDataType(
+ TypeExtractor.createTypeInfo(
+ func, ImperativeAggregateFunction.class, func.getClass(), 1));
+ } catch (InvalidTypesException ite) {
+ throw new TableException(
+ String.format(
+ "Cannot infer generic type of %s}. You can override"
+ + " ImperativeAggregateFunction.getAccumulatorType()"
+ + " to specify the type.",
+ func.getClass()),
+ ite);
+ }
+ }
+ }
+
+ private static Class<?>[] logicalTypesToExternalClasses(LogicalType[] types) {
+ return Arrays.stream(types)
+ .map(
+ t -> {
+ if (t == null) {
+ return null;
+ } else {
+ return TypeConversions.fromLogicalToDataType(t)
+ .getConversionClass();
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static Class<?>[] logicalTypesToInternalClasses(LogicalType[] types) {
+ return Arrays.stream(types)
+ .map(
+ t -> {
+ if (t == null) {
+ return null;
+ } else {
+ return toInternalConversionClass(t);
+ }
+ })
+ .toArray(Class<?>[]::new);
+ }
+
+ private static boolean parameterClassEquals(Class<?> candidate, Class<?> expected) {
+ return candidate == null
+ || candidate == expected
+ || expected == Object.class
+ || candidate == Object.class
+ || // Special case when we don't know the type
+ expected.isPrimitive() && Primitives.wrap(expected) == candidate
+ || candidate == Date.class && (expected == Integer.class || expected == int.class)
+ || candidate == Time.class && (expected == Integer.class || expected == int.class)
+ || candidate == StringData.class && expected == String.class
+ || candidate == String.class && expected == StringData.class
+ || candidate == TimestampData.class && expected == LocalDateTime.class
+ || candidate == Timestamp.class && expected == TimestampData.class
+ || candidate == TimestampData.class && expected == Timestamp.class
+ || candidate == LocalDateTime.class && expected == TimestampData.class
+ || candidate == TimestampData.class && expected == Instant.class
+ || candidate == Instant.class && expected == TimestampData.class
+ || RowData.class.isAssignableFrom(candidate) && expected == Row.class
+ || candidate == Row.class && RowData.class.isAssignableFrom(expected)
+ || RowData.class.isAssignableFrom(candidate) && expected == RowData.class
+ || candidate == RowData.class && RowData.class.isAssignableFrom(expected)
+ || candidate == DecimalData.class && expected == BigDecimal.class
+ || candidate == BigDecimal.class && expected == DecimalData.class
+ || (candidate.isArray()
+ && expected.isArray()
+ && candidate.getComponentType() != null
+ && expected.getComponentType() == Object.class);
+ }
+
+ private static boolean parameterDataTypeEquals(LogicalType internal, DataType parameterType) {
+ if (internal.is(LogicalTypeRoot.RAW)
+ && parameterType.getLogicalType().is(LogicalTypeRoot.RAW)) {
+ return TypeConversions.fromLogicalToDataType(internal).getConversionClass()
+ == parameterType.getConversionClass();
+ } else {
+ // There is a special equal to GenericType. We need rewrite type extract to RowData
+ // etc...
+ return parameterType.getLogicalType() == internal
+ || toInternalConversionClass(internal)
+ == toInternalConversionClass(parameterType.getLogicalType());
+ }
+ }
+
+ private static Optional<Method> getUserDefinedMethod(
+ UserDefinedFunction function,
+ String methodName,
+ Class<?>[] methodSignature,
+ LogicalType[] internalTypes,
+ Function<Class<?>[], DataType[]> parameterTypesFunc) {
+ final List<Method> methods = checkAndExtractMethods(function, methodName);
+
+ final List<Method> filteredMethods =
+ methods.stream()
+ .filter(
+ cur -> {
+ final Class<?>[] parameterTypes = cur.getParameterTypes();
+ final DataType[] parameterDataTypes =
+ parameterTypesFunc.apply(parameterTypes);
+ if (cur.isVarArgs()) {
+ return varArgsMethodMatch(
+ methodSignature,
+ internalTypes,
+ parameterTypes,
+ parameterDataTypes);
+
+ } else {
+ return methodMatch(
+ methodSignature,
+ internalTypes,
+ parameterTypes,
+ parameterDataTypes);
+ }
+ })
+ .collect(Collectors.toList());
+
+ // if there is a fixed method, compiler will call this method preferentially
+
+ final List<Method> found =
+ filteredMethods.stream()
+ .sorted(Comparator.comparing(Method::isVarArgs, Boolean::compareTo))
+ .filter(cur -> !Modifier.isVolatile(cur.getModifiers()))
Review Comment:
Seems like no volatile method are ever returned?
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-26782] Remove PlannerExpression and related [flink]
Posted by "dawidwys (via GitHub)" <gi...@apache.org>.
dawidwys commented on code in PR #23761:
URL: https://github.com/apache/flink/pull/23761#discussion_r1404465452
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/LegacyUserDefinedFunctionInference.java:
##########
@@ -0,0 +1,529 @@
+/*
+ * 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.Internal;
+import org.apache.flink.api.common.functions.InvalidTypesException;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.TypeExtractor;
+import org.apache.flink.table.api.TableException;
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.ArgumentCount;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.ConstantArgumentCount;
+import org.apache.flink.table.types.inference.InputTypeStrategy;
+import org.apache.flink.table.types.inference.Signature;
+import org.apache.flink.table.types.inference.TypeStrategy;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.utils.TypeConversions;
+import org.apache.flink.types.Row;
+
+import org.apache.flink.shaded.guava31.com.google.common.primitives.Primitives;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.List;
+import java.util.Optional;
+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.logical.utils.LogicalTypeUtils.toInternalConversionClass;
+import static org.apache.flink.table.types.utils.TypeConversions.fromLegacyInfoToDataType;
+
+/**
+ * Ported {@code UserDefinedFunctionUtils} to run some of the type inference for legacy functions in
+ * Table API.
+ */
+@Internal
+@Deprecated
+public class LegacyUserDefinedFunctionInference {
+
+ public static InputTypeStrategy getInputTypeStrategy(ImperativeAggregateFunction<?, ?> func) {
+ return new InputTypeStrategy() {
+ @Override
+ public ArgumentCount getArgumentCount() {
+ return ConstantArgumentCount.any();
+ }
+
+ @Override
+ public Optional<List<DataType>> inferInputTypes(
+ CallContext callContext, boolean throwOnFailure) {
+ final List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
+ final DataType accType = getAccumulatorType(func);
+ final LogicalType[] input =
+ Stream.concat(Stream.of(accType), argumentDataTypes.stream())
+ .map(DataType::getLogicalType)
+ .toArray(LogicalType[]::new);
+
+ final Optional<Optional<Method>> foundMethod =
+ Stream.of(
+ logicalTypesToInternalClasses(input),
+ logicalTypesToExternalClasses(input))
+ .map(
+ signature ->
+ getUserDefinedMethod(
+ func,
+ "accumulate",
+ signature,
+ input,
+ cls ->
+ Stream.concat(
+ Stream.of(accType),
+ Arrays.stream(cls)
+ .skip(1)
Review Comment:
Because of
> A no-goal of the PR is to make the logic in LegacyUserDefinedFunctionInference more maintainable.
and
https://github.com/apache/flink/blob/977463cce3ea0f88e2f184c30720bf4e8e97fd4a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/functions/utils/UserDefinedFunctionUtils.scala#L256
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org