You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2021/10/25 13:58:03 UTC

[GitHub] [flink] twalthr commented on a change in pull request #17549: [FLINK-16205][table-planner] Support JSON_OBJECTAGG

twalthr commented on a change in pull request #17549:
URL: https://github.com/apache/flink/pull/17549#discussion_r735621787



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/BuiltInAggregateFunctionTestBase.java
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.runtime.testutils.MiniClusterResourceConfiguration;
+import org.apache.flink.streaming.api.functions.source.SourceFunction;
+import org.apache.flink.table.api.EnvironmentSettings;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.TableDescriptor;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.TableResult;
+import org.apache.flink.table.connector.ChangelogMode;
+import org.apache.flink.table.connector.source.DynamicTableSource;
+import org.apache.flink.table.connector.source.SourceFunctionProvider;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinition;
+import org.apache.flink.table.planner.factories.TableFactoryHarness;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.test.util.MiniClusterWithClientResource;
+import org.apache.flink.types.Row;
+import org.apache.flink.types.RowKind;
+import org.apache.flink.util.CloseableIterator;
+import org.apache.flink.util.Preconditions;
+
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.table.types.DataType.getFieldCount;
+import static org.apache.flink.table.types.DataType.getFieldDataTypes;
+import static org.junit.Assert.assertEquals;
+
+/** Test base for testing aggregate {@link BuiltInFunctionDefinition built-in functions}. */
+@RunWith(Parameterized.class)
+public class BuiltInAggregateFunctionTestBase {
+
+    @ClassRule
+    public static MiniClusterWithClientResource miniClusterResource =
+            new MiniClusterWithClientResource(
+                    new MiniClusterResourceConfiguration.Builder()
+                            .setNumberTaskManagers(1)
+                            .setNumberSlotsPerTaskManager(1)
+                            .build());
+
+    @Parameter public TestSpec testSpec;
+
+    protected EnvironmentSettings getEnvironmentSettings() {

Review comment:
       we should not make this configurable, if we really want to make the test base more efficient in the future, we should not expose too many concepts of an `TableEnvironment`. streaming mode should be enough, no?

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/BuiltInAggregateFunctionTestBase.java
##########
@@ -0,0 +1,333 @@
+/*
+ * 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.runtime.testutils.MiniClusterResourceConfiguration;
+import org.apache.flink.streaming.api.functions.source.SourceFunction;
+import org.apache.flink.table.api.EnvironmentSettings;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.TableDescriptor;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.TableResult;
+import org.apache.flink.table.connector.ChangelogMode;
+import org.apache.flink.table.connector.source.DynamicTableSource;
+import org.apache.flink.table.connector.source.SourceFunctionProvider;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.functions.BuiltInFunctionDefinition;
+import org.apache.flink.table.planner.factories.TableFactoryHarness;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.test.util.MiniClusterWithClientResource;
+import org.apache.flink.types.Row;
+import org.apache.flink.types.RowKind;
+import org.apache.flink.util.CloseableIterator;
+import org.apache.flink.util.Preconditions;
+
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.table.types.DataType.getFieldCount;
+import static org.apache.flink.table.types.DataType.getFieldDataTypes;
+import static org.junit.Assert.assertEquals;
+
+/** Test base for testing aggregate {@link BuiltInFunctionDefinition built-in functions}. */
+@RunWith(Parameterized.class)
+public class BuiltInAggregateFunctionTestBase {
+
+    @ClassRule
+    public static MiniClusterWithClientResource miniClusterResource =
+            new MiniClusterWithClientResource(
+                    new MiniClusterResourceConfiguration.Builder()
+                            .setNumberTaskManagers(1)
+                            .setNumberSlotsPerTaskManager(1)
+                            .build());
+
+    @Parameter public TestSpec testSpec;
+
+    protected EnvironmentSettings getEnvironmentSettings() {
+        return EnvironmentSettings.inStreamingMode();
+    }
+
+    @Test
+    public void testFunction() throws Exception {
+        final TableEnvironment tEnv = TableEnvironment.create(getEnvironmentSettings());
+        final Table sourceTable = asTable(tEnv, testSpec.sourceRowType, testSpec.sourceRows);
+
+        for (final TestItem testItem : testSpec.testItems) {
+            testItem.execute(tEnv, sourceTable);
+        }
+    }
+
+    protected static Table asTable(TableEnvironment tEnv, DataType sourceRowType, List<Row> rows) {
+        final TableDescriptor descriptor =
+                TableFactoryHarness.newBuilder()
+                        .schema(Schema.newBuilder().fromRowDataType(sourceRowType).build())
+                        .source(asSource(rows, sourceRowType))
+                        .build();
+
+        return tEnv.from(descriptor);
+    }
+
+    protected static TableFactoryHarness.ScanSourceBase asSource(
+            List<Row> rows, DataType producedDataType) {
+        return new TableFactoryHarness.ScanSourceBase() {
+            @Override
+            public ChangelogMode getChangelogMode() {
+                final Set<RowKind> rowKinds =
+                        rows.stream().map(Row::getKind).collect(Collectors.toSet());
+                if (rowKinds.size() == 1 && rowKinds.contains(RowKind.INSERT)) {
+                    return ChangelogMode.insertOnly();
+                }
+
+                return ChangelogMode.all();
+            }
+
+            @Override
+            public ScanRuntimeProvider getScanRuntimeProvider(ScanContext context) {
+                final DataStructureConverter converter =
+                        context.createDataStructureConverter(producedDataType);
+
+                return SourceFunctionProvider.of(new Source(rows, converter), true);
+            }
+        };
+    }
+
+    protected static void assertRows(List<Row> expectedRows, TableResult tableResult) {
+        final List<Row> actualRows =
+                materializeResult(tableResult).stream()
+                        .sorted(Comparator.comparing(Objects::toString))
+                        .collect(Collectors.toList());
+        final List<Row> sortedExpectedRows =
+                expectedRows.stream()
+                        .sorted(Comparator.comparing(Objects::toString))
+                        .collect(Collectors.toList());
+
+        assertEquals(
+                String.format("%n%nExpected:%n%s%n%nActual:%n%s", sortedExpectedRows, actualRows),
+                sortedExpectedRows,
+                actualRows);
+    }
+
+    private static List<Row> materializeResult(TableResult tableResult) {
+        try (final CloseableIterator<Row> iterator = tableResult.collect()) {
+            final List<Row> actualRows = new ArrayList<>();
+            iterator.forEachRemaining(
+                    row -> {
+                        final RowKind kind = row.getKind();
+                        switch (kind) {
+                            case INSERT:
+                            case UPDATE_AFTER:
+                                row.setKind(RowKind.INSERT);
+                                actualRows.add(row);
+                                break;
+                            case UPDATE_BEFORE:
+                            case DELETE:
+                                row.setKind(RowKind.INSERT);
+                                actualRows.remove(row);
+                                break;
+                        }
+                    });
+
+            return actualRows;
+        } catch (Exception e) {
+            throw new RuntimeException("Could not collect results", e);
+        }
+    }
+
+    // ---------------------------------------------------------------------------------------------
+
+    /** Test specification. */
+    protected static class TestSpec {
+
+        private final BuiltInFunctionDefinition definition;
+        private final List<TestItem> testItems = new ArrayList<>();
+
+        private @Nullable String description;
+
+        private DataType sourceRowType;
+        private List<Row> sourceRows;
+
+        private TestSpec(BuiltInFunctionDefinition definition) {
+            this.definition = Preconditions.checkNotNull(definition);
+        }
+
+        static TestSpec forFunction(BuiltInFunctionDefinition definition) {
+            return new TestSpec(definition);
+        }
+
+        TestSpec as(String description) {

Review comment:
       nit: not sure if `as()` reads nicely and makes sense for description




-- 
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