[enhance](serde) expr serde support gson (#34180)

In the future, it can be easier to change to Expression to avoid metadata compatibility issues
This commit is contained in:
zhangdong
2024-04-28 12:24:33 +08:00
committed by yiguolei
parent e840102e99
commit 2b5bac3b60
2 changed files with 61 additions and 0 deletions

View File

@ -20,6 +20,7 @@ package org.apache.doris.persist.gson;
import org.apache.doris.alter.AlterJobV2;
import org.apache.doris.alter.RollupJobV2;
import org.apache.doris.alter.SchemaChangeJobV2;
import org.apache.doris.analysis.Expr;
import org.apache.doris.catalog.AggStateType;
import org.apache.doris.catalog.ArrayType;
import org.apache.doris.catalog.DatabaseIf;
@ -137,10 +138,15 @@ import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import org.apache.commons.lang3.reflect.TypeUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Base64;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@ -299,6 +305,7 @@ public class GsonUtils {
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(columnTypeAdapterFactory)
@ -465,6 +472,50 @@ public class GsonUtils {
}
}
private static class ExprAdapter
implements JsonSerializer<Expr>, JsonDeserializer<Expr> {
private static String EXPR_PROP = "expr";
@Override
public JsonElement serialize(Expr src, Type typeOfSrc, JsonSerializationContext context) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
try {
Expr.writeTo(src, dataOutputStream);
String base64Str = Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());
JsonObject exprJsonObject = new JsonObject();
exprJsonObject.addProperty(EXPR_PROP, base64Str);
return exprJsonObject;
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
dataOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Override
public Expr deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
String base64Str = json.getAsJsonObject().get(EXPR_PROP).getAsString();
DataInputStream dataInputStream = new DataInputStream(
new ByteArrayInputStream(Base64.getDecoder().decode(base64Str)));
try {
return Expr.readIn(dataInputStream);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
dataInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
/*
* The json adapter for Guava Multimap.
* Current support: