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

yiguolei pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.2 by this push:
     new bcc204e0aac branch-4.2 [fix](constraint) Fix NPE in 
PrimaryKeyConstraint when loading old metadata (#68206)
bcc204e0aac is described below

commit bcc204e0aac3053cc634247a0c3402264311fc39
Author: minghong <[email protected]>
AuthorDate: Sun Sep 20 11:44:22 2026 +0800

    branch-4.2 [fix](constraint) Fix NPE in PrimaryKeyConstraint when loading 
old metadata (#68206)
    
    Cherry-pick of #61342 to branch-4.2
    
    [fix](constraint) Fix NPE in PrimaryKeyConstraint when loading old
    metadata
    
    On branch-4.2 the class only has the `foreignTables` collection (the
    newer
    `foreignTableNameStrs`/`foreignTableInfos` fields come from a later
    refactor that is
    not part of this branch), so only that field is guarded. A unit test
    covering
    deserialization of metadata without the `ft` field was added.
    
    ---------
    
    Signed-off-by: AurĂ©lien Pupier <[email protected]>
    Co-authored-by: AurĂ©lien Pupier <[email protected]>
---
 .../catalog/constraint/PrimaryKeyConstraint.java   | 13 +++++-
 .../constraint/PrimaryKeyConstraintTest.java       | 49 ++++++++++++++++++++++
 2 files changed, 60 insertions(+), 2 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/PrimaryKeyConstraint.java
 
b/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/PrimaryKeyConstraint.java
index 6fc888be820..0f3515a1cd5 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/PrimaryKeyConstraint.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/catalog/constraint/PrimaryKeyConstraint.java
@@ -19,6 +19,7 @@ package org.apache.doris.catalog.constraint;
 
 import org.apache.doris.catalog.Column;
 import org.apache.doris.catalog.TableIf;
+import org.apache.doris.persist.gson.GsonPostProcessable;
 
 import com.google.common.base.Objects;
 import com.google.common.collect.ImmutableList;
@@ -27,11 +28,12 @@ import com.google.gson.annotations.SerializedName;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+import java.io.IOException;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
-public class PrimaryKeyConstraint extends Constraint {
+public class PrimaryKeyConstraint extends Constraint implements 
GsonPostProcessable {
     public static final Logger LOG = 
LogManager.getLogger(PrimaryKeyConstraint.class);
 
     @SerializedName(value = "cols")
@@ -39,7 +41,7 @@ public class PrimaryKeyConstraint extends Constraint {
 
     // record the foreign table which references the primary key
     @SerializedName(value = "ft")
-    private final Set<TableIdentifier> foreignTables = new HashSet<>();
+    private Set<TableIdentifier> foreignTables = new HashSet<>();
 
     public PrimaryKeyConstraint(String name, Set<String> columns) {
         super(ConstraintType.PRIMARY_KEY, name);
@@ -74,6 +76,13 @@ public class PrimaryKeyConstraint extends Constraint {
         foreignTables.remove(tableIdentifier);
     }
 
+    @Override
+    public void gsonPostProcess() throws IOException {
+        if (foreignTables == null) {
+            foreignTables = new HashSet<>();
+        }
+    }
+
     @Override
     public String toString() {
         return "PRIMARY KEY (" + String.join(", ", columns) + ")";
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/catalog/constraint/PrimaryKeyConstraintTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/catalog/constraint/PrimaryKeyConstraintTest.java
new file mode 100644
index 00000000000..de9bc88af71
--- /dev/null
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/catalog/constraint/PrimaryKeyConstraintTest.java
@@ -0,0 +1,49 @@
+// 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.doris.catalog.constraint;
+
+import org.apache.doris.persist.gson.GsonUtils;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test that a PrimaryKeyConstraint serialized by an old version (without the
+ * "ft" collection field) can be deserialized without throwing a
+ * NullPointerException.
+ */
+public class PrimaryKeyConstraintTest {
+
+    @Test
+    public void testDeserializeOldMetadataWithoutForeignTables() {
+        PrimaryKeyConstraint constraint = new PrimaryKeyConstraint("pk", 
ImmutableSet.of("c1"));
+        JsonObject json = 
JsonParser.parseString(GsonUtils.GSON.toJson(constraint)).getAsJsonObject();
+        // Old journal entries were written before the foreign table 
collection existed, so the
+        // field is absent from the serialized form. Gson uses Unsafe to 
instantiate the object and
+        // bypasses field initializers, so the collection is null unless 
gsonPostProcess fixes it.
+        json.remove("ft");
+        PrimaryKeyConstraint restored = 
GsonUtils.GSON.fromJson(json.toString(), PrimaryKeyConstraint.class);
+        Assertions.assertNotNull(restored);
+        Assertions.assertEquals(ImmutableSet.of("c1"), 
restored.getPrimaryKeyNames());
+        // Must not throw NullPointerException on old metadata.
+        Assertions.assertTrue(restored.getForeignTables().isEmpty());
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to