This is an automated email from the ASF dual-hosted git repository.
morrysnow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new da85469de3f [enhance](serde) expr serde support gson (#34180)
da85469de3f is described below
commit da85469de3f7238282a2c548aed1e6ac53958f2f
Author: zhangdong <[email protected]>
AuthorDate: Sun Apr 28 12:24:33 2024 +0800
[enhance](serde) expr serde support gson (#34180)
In the future, it can be easier to change to Expression to avoid metadata
compatibility issues
---
.../org/apache/doris/persist/gson/GsonUtils.java | 51 ++++++++++++++++++++++
.../java/org/apache/doris/analysis/ExprTest.java | 10 +++++
2 files changed, 61 insertions(+)
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 f5e58592d0e..b27bca76d1a 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
@@ -22,6 +22,7 @@ import org.apache.doris.alter.CloudRollupJobV2;
import org.apache.doris.alter.CloudSchemaChangeJobV2;
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;
@@ -149,10 +150,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;
@@ -337,6 +343,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)
@@ -506,6 +513,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:
diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/ExprTest.java
b/fe/fe-core/src/test/java/org/apache/doris/analysis/ExprTest.java
index a91199189ac..4baa73b860b 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/analysis/ExprTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/ExprTest.java
@@ -24,6 +24,7 @@ import org.apache.doris.catalog.Type;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.jmockit.Deencapsulation;
import org.apache.doris.datasource.InternalCatalog;
+import org.apache.doris.persist.gson.GsonUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@@ -270,4 +271,13 @@ public class ExprTest {
dis.close();
file.delete();
}
+
+ @Test
+ public void testGson() {
+ IntLiteral intLiteral = new IntLiteral(3);
+ String json = GsonUtils.GSON.toJson(intLiteral, Expr.class);
+ Expr expr = GsonUtils.GSON.fromJson(json, Expr.class);
+ Assert.assertTrue(expr instanceof IntLiteral);
+ Assert.assertEquals(intLiteral, expr);
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]