twalthr commented on a change in pull request #10606: [FLINK-15009][table-common] Add a utility for creating type inference logic via reflection URL: https://github.com/apache/flink/pull/10606#discussion_r364627748
########## File path: flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/extraction/TypeInferenceExtractorTest.java ########## @@ -0,0 +1,679 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.types.extraction; + +import org.apache.flink.table.annotation.DataTypeHint; +import org.apache.flink.table.annotation.FunctionHint; +import org.apache.flink.table.annotation.InputGroup; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.api.ValidationException; +import org.apache.flink.table.catalog.DataTypeLookup; +import org.apache.flink.table.catalog.UnresolvedIdentifier; +import org.apache.flink.table.functions.AggregateFunction; +import org.apache.flink.table.functions.ScalarFunction; +import org.apache.flink.table.functions.TableAggregateFunction; +import org.apache.flink.table.functions.TableFunction; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.inference.ArgumentTypeStrategy; +import org.apache.flink.table.types.inference.InputTypeStrategies; +import org.apache.flink.table.types.inference.InputTypeStrategy; +import org.apache.flink.table.types.inference.TypeInference; +import org.apache.flink.table.types.inference.TypeStrategies; +import org.apache.flink.table.types.inference.TypeStrategy; +import org.apache.flink.table.types.logical.utils.LogicalTypeParser; +import org.apache.flink.table.types.utils.TypeConversions; +import org.apache.flink.types.Row; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +import javax.annotation.Nullable; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Supplier; + +import static org.apache.flink.util.CoreMatchers.containsCause; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + +/** + * Tests for {@link TypeInferenceExtractor}. + */ +@RunWith(Parameterized.class) +@SuppressWarnings("unused") +public class TypeInferenceExtractorTest { + + @Parameters + public static List<TestSpec> testData() { + return Arrays.asList( + // function hint defines everything + TestSpec + .forScalarFunction(FullFunctionHint.class) + .expectTypedArguments(DataTypes.INT(), DataTypes.STRING()) + .expectNamedArguments("i", "s") + .expectOutputMapping( + InputTypeStrategies.sequence( + new String[] {"i", "s"}, + new ArgumentTypeStrategy[] { + InputTypeStrategies.explicit(DataTypes.INT()), + InputTypeStrategies.explicit(DataTypes.STRING())} + ), + TypeStrategies.explicit(DataTypes.BOOLEAN())), + + // function hint defines everything with overloading + TestSpec + .forScalarFunction(FullFunctionHints.class) + .expectOutputMapping( + InputTypeStrategies.sequence(InputTypeStrategies.explicit(DataTypes.INT())), + TypeStrategies.explicit(DataTypes.INT())) + .expectOutputMapping( + InputTypeStrategies.sequence(InputTypeStrategies.explicit(DataTypes.BIGINT())), + TypeStrategies.explicit(DataTypes.BIGINT())), + + // global output hint with local input overloading + TestSpec + .forScalarFunction(GlobalOutputFunctionHint.class) + .expectOutputMapping( + InputTypeStrategies.sequence(InputTypeStrategies.explicit(DataTypes.INT())), + TypeStrategies.explicit(DataTypes.INT())) + .expectOutputMapping( + InputTypeStrategies.sequence(InputTypeStrategies.explicit(DataTypes.STRING())), + TypeStrategies.explicit(DataTypes.INT())), + + // unsupported output overloading + TestSpec + .forScalarFunction(InvalidSingleOutputFunctionHint.class) + .expectErrorMessage("Function hints that lead to ambiguous results are not allowed."), + + // global and local overloading + TestSpec + .forScalarFunction(SplitFullFunctionHints.class) + .expectOutputMapping( + InputTypeStrategies.sequence(InputTypeStrategies.explicit(DataTypes.INT())), + TypeStrategies.explicit(DataTypes.INT())) + .expectOutputMapping( + InputTypeStrategies.sequence(InputTypeStrategies.explicit(DataTypes.BIGINT())), + TypeStrategies.explicit(DataTypes.BIGINT())), + + // global and local overloading with unsupported output overloading + TestSpec + .forScalarFunction(InvalidFullOutputFunctionHint.class) + .expectErrorMessage("Function hints with same input definition but different result types are not allowed."), + + // invalid data type hint + TestSpec + .forScalarFunction(IncompleteFunctionHint.class) + .expectErrorMessage("Data type hint does neither specify a data type nor input group for use as function argument."), + + // varargs and ANY input group + TestSpec + .forScalarFunction(ComplexFunctionHint.class) + .expectOutputMapping( + InputTypeStrategies.varyingSequence( + new String[]{"myInt", "myAny"}, + new ArgumentTypeStrategy[]{InputTypeStrategies.explicit(DataTypes.INT()), InputTypeStrategies.ANY}), + TypeStrategies.explicit(DataTypes.BOOLEAN())), + + // ignore argument names during overloading + TestSpec + .forScalarFunction(InvalidOutputWithArgNamesFunctionHint.class) + .expectErrorMessage("Function hints with same input definition but different result types are not allowed."), + + // global input hints and local output hints + TestSpec + .forScalarFunction(GlobalInputFunctionHints.class) + .expectOutputMapping( + InputTypeStrategies.sequence(InputTypeStrategies.explicit(DataTypes.INT())), + TypeStrategies.explicit(DataTypes.INT())) + .expectOutputMapping( + InputTypeStrategies.sequence(InputTypeStrategies.explicit(DataTypes.BIGINT())), + TypeStrategies.explicit(DataTypes.INT())), + + // no arguments + TestSpec + .forScalarFunction(ZeroArgFunction.class) + .expectOutputMapping( + InputTypeStrategies.sequence(new String[0], new ArgumentTypeStrategy[0]), + TypeStrategies.explicit(DataTypes.INT())), + + // test primitive arguments extraction + TestSpec + .forScalarFunction(MixedArgFunction.class) + .expectOutputMapping( + InputTypeStrategies.sequence( + new String[]{"i", "d"}, Review comment: When a `FunctionHint` defines an input, we disable the entire reflective input extraction. This includes the argument names as it would otherwise be pretty complex to make sure that argument names and argument types stay in sync. Argument names are a "nice-to-have" feature, they must not be present therefore, I would suggest to make the logic here not too complex but add a note to the JavaDoc of `FunctionHint#input`. ---------------------------------------------------------------- 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
