MartijnVisser commented on code in PR #351:
URL: 
https://github.com/apache/flink-connector-kafka/pull/351#discussion_r4692192090


##########
flink-connector-kafka/src/test/java/org/apache/flink/streaming/connectors/kafka/table/UpsertKafkaTableITCase.java:
##########
@@ -530,6 +530,89 @@ void 
testUpsertKafkaSourceSinkWithBoundedSpecificOffsets(final String format) th
         deleteTestTopic(topic);
     }
 
+    @ParameterizedTest(name = "format: {0}")
+    @MethodSource("formats")
+    void testUpsertKafkaSourceSinkWithKeyOnlyDeletes(final String format) 
throws Exception {
+        final String sourceTopic = "key_only_deletes_source_" + format + "_" + 
UUID.randomUUID();
+        final String sinkTopic = "key_only_deletes_sink_" + format + "_" + 
UUID.randomUUID();
+        createTestTopic(sourceTopic, 1, 1);
+        createTestTopic(sinkTopic, 1, 1);
+
+        final List<Row> changelog =
+                Arrays.asList(
+                        changelogRow("+I", 1L, "payload 1"),
+                        changelogRow("+I", 2L, "payload 2"),
+                        changelogRow("+U", 1L, "payload 1-new"),
+                        changelogRow("-D", 2L, null));
+        tEnv.executeSql(
+                String.format(
+                        "CREATE TABLE changelog_values (\n"
+                                + "  `id` BIGINT,\n"
+                                + "  `payload` STRING,\n"
+                                + "  PRIMARY KEY (id) NOT ENFORCED\n"
+                                + ") WITH (\n"
+                                + "  'connector' = 'values',\n"
+                                + "  'data-id' = '%s',\n"
+                                + "  'changelog-mode' = 'I,UA,D',\n"
+                                + "  'source.produces-delete-by-key' = 
'true'\n"
+                                + ")",
+                        TestValuesTableFactory.registerData(changelog)));
+        createBoundedUpsertKafkaTable("upsert_source", sourceTopic, format);
+        createBoundedUpsertKafkaTable("upsert_sink", sinkTopic, format);
+        tEnv.executeSql("INSERT INTO upsert_source SELECT * FROM 
changelog_values").await();
+
+        final String insertSql = "INSERT INTO upsert_sink SELECT * FROM 
upsert_source";
+        
assertThat(tEnv.explainSql(insertSql)).doesNotContain("ChangelogNormalize");
+        tEnv.executeSql(insertSql).await();
+
+        final String keyOnlyDeletesSink = "key_only_deletes_" + format;
+        tEnv.executeSql(
+                String.format(
+                        "CREATE TABLE %s (\n"
+                                + "  `id` BIGINT,\n"
+                                + "  `payload` STRING,\n"
+                                + "  PRIMARY KEY (id) NOT ENFORCED\n"
+                                + ") WITH (\n"
+                                + "  'connector' = 'values',\n"
+                                + "  'sink-insert-only' = 'false',\n"
+                                + "  'sink.supports-delete-by-key' = 'true'\n"
+                                + ")",
+                        keyOnlyDeletesSink));
+        tEnv.executeSql("INSERT INTO " + keyOnlyDeletesSink + " SELECT * FROM 
upsert_sink")

Review Comment:
   Spotless fails on this line, which cancelled the rest of CI. `mvn 
spotless:apply` should fix it.



##########
flink-connector-kafka/src/main/java/org/apache/flink/streaming/connectors/kafka/table/UpsertKafkaDynamicTableFactory.java:
##########
@@ -254,6 +259,30 @@ public DynamicTableSink createDynamicTableSink(Context 
context) {
                 tableOptions.get(TRANSACTION_NAMING_STRATEGY));
     }
 
+    /**
+     * A tombstone only needs the key, as long as the sink knows the target 
topic. With a topic list
+     * or pattern, each row picks its topic from the {@code topic} metadata 
column, which a key-only
+     * delete does not guarantee to carry. This makes sinks writing to 
multiple topics incompatible
+     * with key-only deletes.
+     */
+    private static boolean supportsKeyOnlyDeletes(
+            ReadableConfig tableOptions, ResolvedSchema schema) {
+        return isSingleTopic(tableOptions) && 
hasOnlyNullableValueColumns(schema);

Review Comment:
   Upgrading from 5.1.0: a savepoint restores for a nullable single-topic sink, 
but not for a NOT NULL value column or a topic list, because ChangelogNormalize 
comes back and changes the source's operator id. With allowNonRestoredState the 
source topic is reread from the start. I think that needs a release note, and 
"checkpointed state: no" in the description doesn't hold.



##########
flink-connector-kafka/src/main/java/org/apache/flink/streaming/connectors/kafka/table/UpsertKafkaDynamicTableFactory.java:
##########
@@ -254,6 +259,30 @@ public DynamicTableSink createDynamicTableSink(Context 
context) {
                 tableOptions.get(TRANSACTION_NAMING_STRATEGY));
     }
 
+    /**
+     * A tombstone only needs the key, as long as the sink knows the target 
topic. With a topic list
+     * or pattern, each row picks its topic from the {@code topic} metadata 
column, which a key-only
+     * delete does not guarantee to carry. This makes sinks writing to 
multiple topics incompatible
+     * with key-only deletes.
+     */
+    private static boolean supportsKeyOnlyDeletes(
+            ReadableConfig tableOptions, ResolvedSchema schema) {
+        return isSingleTopic(tableOptions) && 
hasOnlyNullableValueColumns(schema);
+    }
+
+    /**
+     * Key-only deletes leave the value columns NULL, which the NOT NULL 
enforcer of Flink versions
+     * without FLINK-40477 rejects. Remove once the minimum Flink version 
supported by the connector
+     * contains this fix (Flink 2.4 and later).
+     */
+    private static boolean hasOnlyNullableValueColumns(ResolvedSchema schema) {
+        final List<String> keyColumns = 
schema.getPrimaryKey().get().getColumns();
+        return schema.getColumns().stream()
+                .filter(Column::isPersisted)

Review Comment:
   Dropping the `isPersisted` filter keeps every test green. Could you add a 
case with a NOT NULL computed column?



##########
flink-connector-kafka/src/main/java/org/apache/flink/streaming/connectors/kafka/table/UpsertKafkaDynamicTableFactory.java:
##########
@@ -254,6 +258,35 @@ public DynamicTableSink createDynamicTableSink(Context 
context) {
                 tableOptions.get(TRANSACTION_NAMING_STRATEGY));
     }
 
+    private static boolean supportsKeyOnlyDeletes(
+            ReadableConfig tableOptions, ResolvedSchema schema) {
+        return writesToSingleTopic(tableOptions) && 
hasOnlyNullableValueColumns(schema);

Review Comment:
   I ran the ROW(k, arr) case: it fails on 2.2.1 and 2.3.0 with this PR and 
passes on 2.4-SNAPSHOT. 5.1.0 fails the same way; main only passes because it 
keeps ChangelogNormalize. A 2.3 backport helps once the floor is that patch 
release, so I'd settle this on the Jira.



##########
flink-connector-kafka/src/main/java/org/apache/flink/streaming/connectors/kafka/table/UpsertKafkaDynamicTableFactory.java:
##########
@@ -254,6 +258,35 @@ public DynamicTableSink createDynamicTableSink(Context 
context) {
                 tableOptions.get(TRANSACTION_NAMING_STRATEGY));
     }
 
+    private static boolean supportsKeyOnlyDeletes(
+            ReadableConfig tableOptions, ResolvedSchema schema) {
+        return writesToSingleTopic(tableOptions) && 
hasOnlyNullableValueColumns(schema);
+    }
+
+    /**
+     * A tombstone only needs the key, as long as the sink knows the target 
topic. With a topic list

Review Comment:
   Source metadata keeps ChangelogNormalize, so only derived metadata loses its 
timestamp on tombstones, as in 5.1.0. Fine by me.



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