Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix](gson) Fix Expr deserialize compatibility #38799

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -776,6 +781,11 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final Class<T> rawType = (Class<T>) type.getRawType();
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

if (!Expr.class.isAssignableFrom(rawType)) {
// reduce the stack depth.
return null;
}

return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,20 @@ 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)
.registerTypeHierarchyAdapter(Table.class, new GuavaTableAdapter())
// .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)
Expand Down Expand Up @@ -660,6 +665,11 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final Class<T> rawType = (Class<T>) type.getRawType();
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

if (!Expr.class.isAssignableFrom(rawType)) {
// reduce the stack depth.
return null;
}

return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
Expand Down
52 changes: 52 additions & 0 deletions fe/fe-core/src/test/java/org/apache/doris/persist/ExprTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading