autophagy commented on code in PR #29130: URL: https://github.com/apache/flink/pull/29130#discussion_r4452876612
########## flink-table/flink-table-test-utils/src/test/java/org/apache/flink/table/runtime/functions/ProcessTableFunctionTestHarnessChangelogModeTest.java: ########## @@ -0,0 +1,408 @@ +/* + * 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.runtime.functions; + +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.api.TableRuntimeException; +import org.apache.flink.table.connector.ChangelogMode; +import org.apache.flink.table.runtime.functions.ProcessTableFunctionTestHarness.TableArgument; +import org.apache.flink.types.Row; +import org.apache.flink.types.RowKind; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.Optional; + +import static org.apache.flink.table.runtime.functions.PtfTestFunctions.KEY_VALUE_ROW; +import static org.apache.flink.table.runtime.functions.PtfTestFunctions.TIMED_ROW; +import static org.apache.flink.table.runtime.functions.PtfTestFunctions.VALUE_ROW; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** + * Runtime changelog-mode behavior of the harness. Build-time validation is covered in {@link + * PtfChangelogModeValidatorTest}. + */ +class ProcessTableFunctionTestHarnessChangelogModeTest { + + // ------------------------------------------------------------------------- + // Table argument (input) changelog mode + // ------------------------------------------------------------------------- + + @Test + void testTableArgumentChangelogModeIsPerArgument() throws Exception { + // A configured updating mode must not leak into a sibling argument. + try (ProcessTableFunctionTestHarness<Row> h = + ProcessTableFunctionTestHarness.ofClass( + PtfTestFunctions.TwoTableChangelogModePTF.class) + .withTableArgument( + TableArgument.forName("retracting") + .type(DataTypes.of(VALUE_ROW)) + .changelogMode(ChangelogMode.all()) + .build()) + .withTableArgument( + TableArgument.forName("plain") + .type(DataTypes.of(VALUE_ROW)) + .build()) + .build()) { + h.processElementForTable("retracting", Row.of(1)); + + Row row = h.getOutput().get(0); + assertThat(row.<String>getFieldAs(0)).isEqualTo(ChangelogMode.all().toString()); + // "plain" is unconfigured and lacks SUPPORT_UPDATES, so it reports the default. + assertThat(row.<String>getFieldAs(1)).isEqualTo(ChangelogMode.insertOnly().toString()); + } + } + + @Test + void testConfiguredUpsertKeysReachEval() throws Exception { + try (ProcessTableFunctionTestHarness<Row> h = + ProcessTableFunctionTestHarness.ofClass( + PtfTestFunctions.TableSemanticsProbePTF.class) + .withTableArgument( + TableArgument.forName("input") + .type(DataTypes.of("ROW<a INT, b INT, c INT>")) + .partitionBy("a", "b") + .upsertKey("a", "b") + .changelogMode(ChangelogMode.upsert(true)) + .build()) + .build()) { + h.processElement(RowKind.UPDATE_AFTER, 1, 2, 99); + + // Output layout is [partition_a, partition_b, mode, upsertKeys]. Upsert-key column + // names must arrive as resolved field indices, grouped as a single key. + assertThat(h.getOutput().get(0).<String>getFieldAs(3)).isEqualTo("[[0, 1]]"); + } + } + + @Test + void testMultipleUpsertKeyCallsDeclareSeparateCandidates() throws Exception { + // Retract mode, so partition columns needn't match a key. + try (ProcessTableFunctionTestHarness<Row> h = + ProcessTableFunctionTestHarness.ofClass( + PtfTestFunctions.TableSemanticsProbePTF.class) + .withTableArgument( + TableArgument.forName("input") + .type(DataTypes.of("ROW<a INT, b INT, c INT>")) + .partitionBy("a") + .upsertKey("a") + .upsertKey("b") + .changelogMode(ChangelogMode.all()) Review Comment: Yeah, upsert keys come from metadata (like the PK for a scan) and not the changelog mode, so a retract stream can still have them I think. Its also what a ptf sees when the partition isnt the upsert key, like `PARTITION BY a` over `PK (a, b)` gets a retract stream from a `ChangelogNormalize(key=[a, b])` which still carries it. `all` lets the partition differ from the candidates -- 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]
