twalthr commented on code in PR #26638: URL: https://github.com/apache/flink/pull/26638#discussion_r2132142649
########## flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/StructuredFunctionsITCase.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.planner.functions; + +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.functions.BuiltInFunctionDefinitions; +import org.apache.flink.table.functions.ScalarFunction; +import org.apache.flink.table.types.logical.StructuredType; + +import java.util.stream.Stream; + +/** Tests for functions dealing with {@link StructuredType}. */ +public class StructuredFunctionsITCase extends BuiltInFunctionTestBase { + + @Override + Stream<TestSetSpec> getTestSetSpecs() { + return Stream.of( + TestSetSpec.forFunction(BuiltInFunctionDefinitions.EQUALS) + .onFieldsWithData(14, "Bob") + .andDataTypes(DataTypes.INT(), DataTypes.STRING()) + .withFunction(Type1.Type1Constructor.class) + .withFunction(Type2.Type2Constructor.class) + // Same value from function + .testSqlResult( + "Type1Constructor(f0, f1) = Type1Constructor(14, 'Bob')", + true, + DataTypes.BOOLEAN()) + // Same value from CAST + .testSqlResult( + "Type1Constructor(f0, f1) = CAST((14, 'Bob') AS " + + Type1.TYPE + + ")", + true, + DataTypes.BOOLEAN()) + // Different value from function + .testSqlResult( + "Type1Constructor(f0, f1) = Type1Constructor(15, 'Alice')", + false, + DataTypes.BOOLEAN()) + // Different value from CAST + .testSqlResult( + "Type1Constructor(f0, f1) = CAST((15, 'Alice') AS " + + Type1.TYPE + + ")", + false, + DataTypes.BOOLEAN()) + // Different class name from function + .testSqlValidationError( + "Type1Constructor(f0, f1) = Type2Constructor(14, 'Bob')", + "Incompatible structured types") + // Different class name from CAST + .testSqlValidationError( + "Type1Constructor(f0, f1) = CAST((14, 'Bob') AS " + + Type2.TYPE + + ")", + "Incompatible structured types") + // Same class name but different fields + .testSqlValidationError( + "Type1Constructor(f0, f1) = CAST((14, 'Bob') AS STRUCTURED<'" + + Type1.class.getName() + + "', a BOOLEAN, b BOOLEAN>)", + "Cannot apply '=' to arguments")); + } + + // -------------------------------------------------------------------------------------------- + // Helpers + // -------------------------------------------------------------------------------------------- + + /** Structured type Type1. */ + public static class Type1 { + public static final String TYPE = + "STRUCTURED<'" + Type1.class.getName() + "', a INT, b STRING>"; + + public Integer a; Review Comment: yes because there is a mismatch in extracted and explicit type. The function will return the extracted one. But in general it is up to the user to ensure that data type matches class. Usually, users should not declare the structured types manually but use reflective extraction by just using the POJO in a function signature. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
