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


##########
flink-table/flink-table-test-utils/src/main/java/org/apache/flink/table/runtime/functions/PtfChangelogRowRules.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.annotation.Internal;
+import org.apache.flink.table.api.TableRuntimeException;
+import org.apache.flink.table.connector.ChangelogMode;
+import org.apache.flink.table.functions.ProcessTableFunction;
+import org.apache.flink.table.types.inference.StaticArgumentTrait;
+import org.apache.flink.types.Row;
+import org.apache.flink.types.RowKind;
+
+/**
+ * Changelog-mode rules applied to each row entering or leaving the {@link 
ProcessTableFunction}
+ * under test.
+ */
+@Internal
+final class PtfChangelogRowRules {
+
+    private PtfChangelogRowRules() {}
+
+    /** Validates the row kind, then reduces key-only deletes to their key 
columns. */
+    static Row prepareInputRow(
+            ProcessTableFunctionTestHarness.TableArgumentInfo tableArg, Row 
row) {
+        validateInputRowKind(tableArg, row.getKind());
+        return stripNonKeyFieldsForKeyOnlyDelete(tableArg, row);
+    }
+
+    static void validateOutputRowKind(ChangelogMode outputMode, RowKind 
rowKind) {
+        if (!outputMode.contains(rowKind)) {
+            throw new TableRuntimeException(
+                    String.format(
+                            "Invalid row kind received: %s. Expected produced 
changelog mode: %s",
+                            rowKind, outputMode));
+        }
+    }
+
+    private static void validateInputRowKind(
+            ProcessTableFunctionTestHarness.TableArgumentInfo tableArg, 
RowKind rowKind) {
+        ChangelogMode mode = tableArg.effectiveChangelogMode();
+        if (mode.contains(rowKind)) {
+            return;
+        }
+
+        if (!tableArg.is(StaticArgumentTrait.SUPPORT_UPDATES)) {
+            throw new IllegalArgumentException(
+                    String.format(
+                            "Row kind %s is not permitted on table argument 
'%s'. "
+                                    + "This argument does not declare 
SUPPORT_UPDATES.",
+                            rowKind, tableArg.name));
+        }
+
+        throw new IllegalArgumentException(
+                String.format(
+                        "Row kind %s is not permitted on table argument '%s'. "
+                                + "Expected consumed changelog mode: %s",
+                        rowKind, tableArg.name, mode));
+    }
+
+    private static Row stripNonKeyFieldsForKeyOnlyDelete(
+            ProcessTableFunctionTestHarness.TableArgumentInfo tableArg, Row 
row) {
+        if (row.getKind() != RowKind.DELETE
+                || !tableArg.effectiveChangelogMode().keyOnlyDeletes()) {
+            return row;
+        }
+
+        // A key-only delete carries only the columns the stream is 
co-partitioned by.
+        int[] keyIndices = 
ProcessTableFunctionTestHarness.getPartitionColumnIndices(tableArg);

Review Comment:
   Given the PTF strict equality between PKs and UKs, this means that the 
partition keys are the upsert key. I think this needs better clarification (and 
also it means the `keyIndices` len check is overly defensive since it cant 
happen)
   
   wrt supporting multiple candidate keys, `upsertKeyColumns` in 
`TableSemantics` returns a `List<int[]>` so PTFs in prod can see several too



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