From eae00691cd2d3d78f451c488c42b590e6be0df87 Mon Sep 17 00:00:00 2001 From: w41ter Date: Fri, 2 Aug 2024 11:37:20 +0000 Subject: [PATCH] [fix](gson) Fix Expr deserialize compatibility Since GsonBuilder.create() adds all registered factories to GSON in reverse order, and ExprAdapterFactory is registered before the RuntimeTypeAdapterFactory for Expr, ExprAdapterFactory will not be executed. This PR adjusts their registration order. Now, it will first check in ExprAdapterFactory whether to use the pre-134 deserialize method, and then attempt to use the RuntimeTypeAdapterFactory for Expr.class. --- .../apache/doris/persist/gson/GsonUtils.java | 12 ++++- .../doris/persist/gson/GsonUtils134.java | 12 ++++- .../org/apache/doris/persist/ExprTest.java | 52 +++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 fe/fe-core/src/test/java/org/apache/doris/persist/ExprTest.java diff --git a/fe/fe-core/src/main/java/org/apache/doris/persist/gson/GsonUtils.java b/fe/fe-core/src/main/java/org/apache/doris/persist/gson/GsonUtils.java index 9f365808a6f25a..0a6043ac685b73 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/persist/gson/GsonUtils.java +++ b/fe/fe-core/src/main/java/org/apache/doris/persist/gson/GsonUtils.java @@ -579,6 +579,11 @@ public class GsonUtils { // the builder of GSON instance. // Add any other adapters if necessary. + // + // ATTN: + // Since GsonBuilder.create() adds all registered factories to GSON in reverse order, if you + // need to ensure the search order of two RuntimeTypeAdapterFactory instances, be sure to + // register them in reverse priority order. private static final GsonBuilder GSON_BUILDER = new GsonBuilder() .setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE) .addSerializationExclusionStrategy( @@ -589,8 +594,8 @@ public class GsonUtils { .registerTypeHierarchyAdapter(Multimap.class, new GuavaMultimapAdapter()) .registerTypeAdapterFactory(new PostProcessTypeAdapterFactory()) .registerTypeAdapterFactory(new PreProcessTypeAdapterFactory()) - .registerTypeAdapterFactory(new ExprAdapterFactory()) .registerTypeAdapterFactory(exprAdapterFactory) + .registerTypeAdapterFactory(new ExprAdapterFactory()) .registerTypeAdapterFactory(columnTypeAdapterFactory) .registerTypeAdapterFactory(distributionInfoTypeAdapterFactory) .registerTypeAdapterFactory(resourceTypeAdapterFactory) @@ -776,6 +781,11 @@ public TypeAdapter create(Gson gson, TypeToken type) { final Class rawType = (Class) type.getRawType(); final TypeAdapter delegate = gson.getDelegateAdapter(this, type); + if (!Expr.class.isAssignableFrom(rawType)) { + // reduce the stack depth. + return null; + } + return new TypeAdapter() { public void write(JsonWriter out, T value) throws IOException { delegate.write(out, value); diff --git a/fe/fe-core/src/main/java/org/apache/doris/persist/gson/GsonUtils134.java b/fe/fe-core/src/main/java/org/apache/doris/persist/gson/GsonUtils134.java index 1359bb170dff15..147c403869e977 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/persist/gson/GsonUtils134.java +++ b/fe/fe-core/src/main/java/org/apache/doris/persist/gson/GsonUtils134.java @@ -473,6 +473,11 @@ public class GsonUtils134 { // the builder of GSON instance. // Add any other adapters if necessary. + // + // ATTN: + // Since GsonBuilder.create() adds all registered factories to GSON in reverse order, if you + // need to ensure the search order of two RuntimeTypeAdapterFactory instances, be sure to + // register them in reverse priority order. private static final GsonBuilder GSON_BUILDER = new GsonBuilder().addSerializationExclusionStrategy( new HiddenAnnotationExclusionStrategy()).enableComplexMapKeySerialization() .addReflectionAccessFilter(ReflectionAccessFilter.BLOCK_INACCESSIBLE_JAVA) @@ -480,8 +485,8 @@ public class GsonUtils134 { // .registerTypeHierarchyAdapter(Expr.class, new ExprAdapter()) .registerTypeHierarchyAdapter(Multimap.class, new GuavaMultimapAdapter()) .registerTypeAdapterFactory(new PostProcessTypeAdapterFactory()) - .registerTypeAdapterFactory(new ExprAdapterFactory()) .registerTypeAdapterFactory(exprAdapterFactory) + .registerTypeAdapterFactory(new ExprAdapterFactory()) .registerTypeAdapterFactory(columnTypeAdapterFactory) .registerTypeAdapterFactory(distributionInfoTypeAdapterFactory) .registerTypeAdapterFactory(resourceTypeAdapterFactory) @@ -660,6 +665,11 @@ public TypeAdapter create(Gson gson, TypeToken type) { final Class rawType = (Class) type.getRawType(); final TypeAdapter delegate = gson.getDelegateAdapter(this, type); + if (!Expr.class.isAssignableFrom(rawType)) { + // reduce the stack depth. + return null; + } + return new TypeAdapter() { public void write(JsonWriter out, T value) throws IOException { delegate.write(out, value); diff --git a/fe/fe-core/src/test/java/org/apache/doris/persist/ExprTest.java b/fe/fe-core/src/test/java/org/apache/doris/persist/ExprTest.java new file mode 100644 index 00000000000000..d9fca2f7fd81da --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/persist/ExprTest.java @@ -0,0 +1,52 @@ +// 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.persist; + +import org.apache.doris.analysis.Expr; +import org.apache.doris.common.FeMetaVersion; +import org.apache.doris.meta.MetaContext; +import org.apache.doris.persist.gson.GsonUtils; +import org.apache.doris.persist.gson.GsonUtils134; + +import com.google.gson.annotations.SerializedName; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.Test; + +public class ExprTest { + private static final Logger LOG = LogManager.getLogger(ExprTest.class); + + private static class ExprWrapper { + @SerializedName("expr") + private Expr expr; + } + + @Test + public void testExprDeserializeCompatibility() { + LOG.info("run testDeserializeExprWithMetaVersion134 test"); + + String serializedString = "{\"expr\":{\"expr\": \"AAAADAAAAAAKZGF0ZV90cnVuYwAAAQAAAAIAAAABAAAAAANkYXkAAAAIAAAABW1vbnRoAAA\\u003d\"}}"; + + MetaContext ctx = new MetaContext(); + ctx.setMetaVersion(FeMetaVersion.VERSION_129); + ctx.setThreadLocalInfo(); + GsonUtils134.GSON.fromJson(serializedString, ExprWrapper.class); + GsonUtils.GSON.fromJson(serializedString, ExprWrapper.class); + MetaContext.remove(); + } +}