This is an automated email from the ASF dual-hosted git repository.

JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new 52805164f2 [flink][cdc] Fix missing primary keys for Debezium records 
(#9465)
52805164f2 is described below

commit 52805164f2a6ab318593fed394764deda1cd8c66
Author: Arnav Balyan <[email protected]>
AuthorDate: Sun Aug 30 07:29:37 2026 +0530

    [flink][cdc] Fix missing primary keys for Debezium records (#9465)
---
 .../format/debezium/DebeziumJsonRecordParser.java  | 39 ++++++++++
 .../debezium/DebeziumJsonRecordParserTest.java     | 91 ++++++++++++++++++++++
 2 files changed, 130 insertions(+)

diff --git 
a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/format/debezium/DebeziumJsonRecordParser.java
 
b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/format/debezium/DebeziumJsonRecordParser.java
index 14dd00ee3d..9551973f35 100644
--- 
a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/format/debezium/DebeziumJsonRecordParser.java
+++ 
b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/format/debezium/DebeziumJsonRecordParser.java
@@ -218,6 +218,45 @@ public class DebeziumJsonRecordParser extends 
AbstractJsonRecordParser {
         return resultMap;
     }
 
+    @Override
+    protected List<String> extractPrimaryKeys() {
+        List<String> primaryKeys = super.extractPrimaryKeys();
+        if (!primaryKeys.isEmpty()) {
+            return primaryKeys;
+        }
+
+        Object key = currentRecord.getKey();
+        if (!(key instanceof JsonNode)) {
+            return Collections.emptyList();
+        }
+
+        JsonNode keyNode = (JsonNode) key;
+        JsonNode keySchema = keyNode.get(FIELD_SCHEMA);
+        if (!isNull(keySchema)
+                && keySchema.isObject()
+                && keySchema.has("fields")
+                && keySchema.get("fields").isArray()
+                && keyNode.has(FIELD_PAYLOAD)) {
+            ArrayNode fields = getNodeAs(keySchema, "fields", ArrayNode.class);
+            List<String> fieldNames = new ArrayList<>(fields.size());
+            for (JsonNode field : fields) {
+                String fieldName = getString(field, "field");
+                if (fieldName != null) {
+                    fieldNames.add(fieldName);
+                }
+            }
+            return fieldNames;
+        }
+
+        if (!keyNode.isObject()) {
+            return Collections.emptyList();
+        }
+
+        List<String> fieldNames = new ArrayList<>();
+        keyNode.fieldNames().forEachRemaining(fieldNames::add);
+        return fieldNames;
+    }
+
     @Override
     protected String primaryField() {
         return FIELD_PRIMARY;
diff --git 
a/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/action/cdc/format/debezium/DebeziumJsonRecordParserTest.java
 
b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/action/cdc/format/debezium/DebeziumJsonRecordParserTest.java
new file mode 100644
index 0000000000..747c56d5b5
--- /dev/null
+++ 
b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/action/cdc/format/debezium/DebeziumJsonRecordParserTest.java
@@ -0,0 +1,91 @@
+/*
+ * 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.paimon.flink.action.cdc.format.debezium;
+
+import org.apache.paimon.flink.action.cdc.CdcSourceRecord;
+import org.apache.paimon.flink.action.cdc.TypeMapping;
+import org.apache.paimon.schema.Schema;
+
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.JsonNode;
+import 
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Tests for {@link DebeziumJsonRecordParser}. */
+public class DebeziumJsonRecordParserTest {
+
+    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+
+    @Test
+    public void testPrimaryKeysFromSchemaEnabledKey() throws Exception {
+        JsonNode key =
+                OBJECT_MAPPER.readTree(
+                        "{\"schema\":{\"type\":\"struct\",\"fields\":["
+                                + 
"{\"type\":\"int64\",\"optional\":false,\"field\":\"id\"},"
+                                + 
"{\"type\":\"string\",\"optional\":false,\"field\":\"tenant\"}]},"
+                                + "\"payload\":{\"id\":1,\"tenant\":\"A\"}}");
+
+        assertPrimaryKeys(key, value(null), "id", "tenant");
+    }
+
+    @Test
+    public void testPrimaryKeysFromSchemaLessKey() throws Exception {
+        JsonNode key = OBJECT_MAPPER.readTree("{\"id\":1,\"tenant\":\"A\"}");
+
+        assertPrimaryKeys(key, value(null), "id", "tenant");
+    }
+
+    @Test
+    public void testPrimaryKeysFromValueTakePrecedence() throws Exception {
+        JsonNode key = OBJECT_MAPPER.readTree("{\"id\":1}");
+
+        assertPrimaryKeys(key, value("[\"tenant\"]"), "tenant");
+    }
+
+    @Test
+    public void testEmptyPrimaryKeysFallBackToKey() throws Exception {
+        JsonNode key = OBJECT_MAPPER.readTree("{\"id\":1}");
+
+        assertPrimaryKeys(key, value("[]"), "id");
+    }
+
+    private static JsonNode value(String primaryKeys) throws Exception {
+        String primaryKeyField = primaryKeys == null ? "" : "\"pkNames\":" + 
primaryKeys + ",";
+        return OBJECT_MAPPER.readTree(
+                "{"
+                        + primaryKeyField
+                        + "\"before\":null,"
+                        + 
"\"after\":{\"id\":1,\"tenant\":\"A\",\"name\":\"Alice\"},"
+                        + "\"source\":{\"db\":\"test\",\"table\":\"users\"},"
+                        + "\"op\":\"c\"}");
+    }
+
+    private static void assertPrimaryKeys(JsonNode key, JsonNode value, 
String... primaryKeys) {
+        DebeziumJsonRecordParser parser =
+                new DebeziumJsonRecordParser(TypeMapping.defaultMapping(), 
Collections.emptyList());
+        Schema schema = parser.buildSchema(new CdcSourceRecord("users", key, 
value));
+
+        assertThat(schema).isNotNull();
+        assertThat(schema.primaryKeys()).containsExactly(primaryKeys);
+    }
+}

Reply via email to