dawidwys commented on a change in pull request #8865: [FLINK-12924][table] Introduce basic type inference interfaces URL: https://github.com/apache/flink/pull/8865#discussion_r297044741
########## File path: flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/TypeInferenceUtil.java ########## @@ -0,0 +1,307 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.types.inference; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.table.api.TableException; +import org.apache.flink.table.api.ValidationException; +import org.apache.flink.table.functions.FunctionDefinition; +import org.apache.flink.table.functions.FunctionKind; +import org.apache.flink.table.types.DataType; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +/** + * Utility for performing type inference. + */ +@Internal +public class TypeInferenceUtil { + + public static Result runTypeInference(TypeInference typeInference, CallContext callContext) { + try { + return runTypeInferenceInternal(typeInference, callContext); + } catch (ValidationException e) { + throw new ValidationException( + String.format( + "Invalid call to function '%s'. Given arguments: %s", + callContext.getName(), + callContext.getArgumentDataTypes().stream() + .map(DataType::toString) + .collect(Collectors.joining())), + e); + } catch (Throwable t) { + throw new TableException( + String.format( + "Unexpected error in type inference logic of function '%s'. This is a bug.", + callContext.getName()), + t); + } + } + + /** + * The result of a type inference run. It contains information about how arguments need to be + * adapted in order to comply with the function's signature. + * + * <p>This includes casts that need to be inserted, reordering of arguments (*), or insertion of default + * values (*) where (*) is future work. + */ + public static class Result { + + private final List<DataType> expectedArgumentTypes; + + private final DataType accumulatorDataType; + + private final DataType outputDataType; + + public Result( + List<DataType> expectedArgumentTypes, + DataType accumulatorDataType, + DataType outputDataType) { + this.expectedArgumentTypes = expectedArgumentTypes; + this.accumulatorDataType = accumulatorDataType; + this.outputDataType = outputDataType; + } + + public List<DataType> getExpectedArgumentTypes() { + return expectedArgumentTypes; + } + + public DataType getAccumulatorDataType() { Review comment: nit: How about `Optional<DataType>` for accumulator type? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
