autophagy commented on code in PR #29130:
URL: https://github.com/apache/flink/pull/29130#discussion_r4453358892


##########
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())
+                                        .build())
+                        .build()) {
+            h.processElement(RowKind.UPDATE_AFTER, 1, 2, 99);
+
+            // Output layout is [partition_a, mode, upsertKeys]. Both 
candidates must reach eval()
+            // as resolved field indices: {a} -> [0] and {b} -> [1].
+            
assertThat(h.getOutput().get(0).<String>getFieldAs(2)).isEqualTo("[[0], [1]]");
+        }
+    }
+
+    @Test
+    void testUpsertInputAcceptsPartitionMatchingAnyCandidate() throws 
Exception {
+        // Partition columns need only match one candidate key.
+        try (ProcessTableFunctionTestHarness<Row> h =
+                ProcessTableFunctionTestHarness.ofClass(
+                                PtfTestFunctions.TableSemanticsProbePTF.class)
+                        .withTableArgument(
+                                TableArgument.forName("input")
+                                        .type(DataTypes.of(KEY_VALUE_ROW))
+                                        .partitionBy("key")
+                                        .upsertKey("key")
+                                        .upsertKey("value")
+                                        
.changelogMode(ChangelogMode.upsert(true))
+                                        .build())
+                        .build()) {
+            h.processElement(RowKind.UPDATE_AFTER, 1, 100);
+
+            // Output layout is [partition_key, mode, upsertKeys].
+            
assertThat(h.getOutput().get(0).<String>getFieldAs(2)).isEqualTo("[[0], [1]]");
+        }
+    }
+
+    @Test
+    void testProcessElementRejectsRowKindNotInConfiguredInputMode() throws 
Exception {
+        // insertOnly() is trait-consistent for SUPPORT_UPDATES, so the 
rejection happens at
+        // processElement() time rather than at build() time.
+        try (ProcessTableFunctionTestHarness<Row> h =
+                ProcessTableFunctionTestHarness.ofClass(
+                                
PtfTestFunctions.PlainUpdatingPassthroughPTF.class)
+                        .withTableArgument(
+                                TableArgument.forName("input")
+                                        .type(DataTypes.of(VALUE_ROW))
+                                        
.changelogMode(ChangelogMode.insertOnly())
+                                        .build())
+                        .build()) {
+            assertThatThrownBy(() -> h.processElement(RowKind.UPDATE_AFTER, 
10))

Review Comment:
   The input. The output check is a separate path. Will rename this test to 
make it a bit clearer



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

Reply via email to