dawidwys commented on a change in pull request #12649: URL: https://github.com/apache/flink/pull/12649#discussion_r440794777
########## File path: flink-table/flink-table-planner-blink/src/test/java/org/apache/flink/table/planner/expressions/CompositeTypeAccessExpressionITCase.java ########## @@ -0,0 +1,264 @@ +/* + * 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.expressions; + +import org.apache.flink.table.annotation.DataTypeHint; +import org.apache.flink.table.annotation.FunctionHint; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.api.EnvironmentSettings; +import org.apache.flink.table.api.TableEnvironment; +import org.apache.flink.table.api.TableResult; +import org.apache.flink.table.api.TableSchema; +import org.apache.flink.table.api.ValidationException; +import org.apache.flink.table.functions.BuiltInFunctionDefinitions; +import org.apache.flink.table.functions.ScalarFunction; +import org.apache.flink.table.functions.TableFunction; +import org.apache.flink.table.types.logical.LogicalTypeRoot; +import org.apache.flink.types.Row; +import org.apache.flink.util.CloseableIterator; + +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.Suite; + +import java.util.Arrays; +import java.util.List; + +import static java.util.Collections.singletonMap; +import static org.apache.flink.table.api.DataTypes.ARRAY; +import static org.apache.flink.table.api.DataTypes.BIGINT; +import static org.apache.flink.table.api.DataTypes.FIELD; +import static org.apache.flink.table.api.DataTypes.MAP; +import static org.apache.flink.table.api.DataTypes.ROW; +import static org.apache.flink.table.api.DataTypes.STRING; +import static org.apache.flink.table.api.Expressions.$; +import static org.apache.flink.table.api.Expressions.call; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; + +/** + * Tests for functions that access nested fields/elements of composite/collection types. + */ +@RunWith(Suite.class) [email protected]( + { + CompositeTypeAccessExpressionITCase.TableFieldAccess.class, + CompositeTypeAccessExpressionITCase.CallFieldAccess.class + } +) +public class CompositeTypeAccessExpressionITCase { + + /** + * Regular tests. See also {@link CallFieldAccess} for tests that access a nested field of an expression or + * for {@link BuiltInFunctionDefinitions#FLATTEN} which produces multiple columns from a single one. + */ + public static class TableFieldAccess extends BuiltInFunctionTestBase { + @Parameterized.Parameters(name = "{index}: {0}") + public static List<TestSpec> testData() { + return Arrays.asList( + + // Actually in case of SQL it does not use the GET method, but + // a custom logic for accessing nested fields of a Table. + TestSpec.forFunction(BuiltInFunctionDefinitions.GET) + .onFieldsWithData(null, Row.of(1)) + .andDataTypes( + ROW(FIELD("nested", BIGINT().notNull())).nullable(), + ROW(FIELD("nested", BIGINT().notNull())).notNull() + ) + .testTableApiResult($("f0").get("nested"), null, BIGINT().nullable()) + .testTableApiResult($("f1").get("nested"), 1L, BIGINT().notNull()) + .testSqlResult("f0.nested", null, BIGINT().nullable()) + .testSqlResult("f1.nested", 1L, BIGINT().notNull()), + + // In Calcite it maps to FlinkSqlOperatorTable.ITEM + TestSpec.forFunction(BuiltInFunctionDefinitions.AT) + .onFieldsWithData(null, new int[] {1}, null, singletonMap("nested", 1), null, Row.of(1)) + .andDataTypes( + ARRAY(BIGINT().notNull()).nullable(), + ARRAY(BIGINT().notNull()).notNull(), + MAP(STRING(), BIGINT().notNull()).nullable(), + MAP(STRING(), BIGINT().notNull()).notNull(), + ROW(FIELD("nested", BIGINT().notNull())).nullable(), + ROW(FIELD("nested", BIGINT().notNull())).notNull() + ) + // accessing elements of MAP or ARRAY is a runtime operations, + // we do not know about the size or contents during the inference + // therefore the results are always nullable + .testSqlResult("f0[1]", null, BIGINT().nullable()) Review comment: I have it in a different branch. I found this PR already quite big. ---------------------------------------------------------------- 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]
