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 eb9d68a6406 [opt](Nereids) add json literal for constant folding 
(#24878)
eb9d68a6406 is described below

commit eb9d68a6406f060ca820132b5d3a21b407406c1a
Author: morrySnow <101034200+morrys...@users.noreply.github.com>
AuthorDate: Tue Sep 26 11:37:53 2023 +0800

    [opt](Nereids) add json literal for constant folding (#24878)
---
 .../trees/expressions/literal/JsonLiteral.java     | 72 ++++++++++++++++++++++
 .../nereids/trees/expressions/literal/Literal.java |  4 ++
 2 files changed, 76 insertions(+)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/JsonLiteral.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/JsonLiteral.java
new file mode 100644
index 00000000000..779be0ca541
--- /dev/null
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/JsonLiteral.java
@@ -0,0 +1,72 @@
+// 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.nereids.trees.expressions.literal;
+
+import org.apache.doris.analysis.LiteralExpr;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.types.JsonType;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+/**
+ * literal for json type.
+ */
+public class JsonLiteral extends Literal {
+
+    private static final ObjectMapper MAPPER = new ObjectMapper();
+
+    private final JsonNode jsonNode;
+    private final String value;
+
+    /**
+     * constructor, will check value.
+     */
+    public JsonLiteral(String value) {
+        super(JsonType.INSTANCE);
+        try {
+            jsonNode = MAPPER.readTree(value);
+        } catch (JsonProcessingException e) {
+            throw new AnalysisException("Invalid jsonb literal: '" + value + 
"'. because " + e.getMessage());
+        }
+        if (jsonNode.isMissingNode()) {
+            throw new AnalysisException("Invalid jsonb literal: ''");
+        }
+        this.value = jsonNode.toString();
+    }
+
+    @Override
+    public String getStringValue() {
+        return value;
+    }
+
+    @Override
+    public JsonNode getValue() {
+        return jsonNode;
+    }
+
+    @Override
+    public LiteralExpr toLegacyLiteral() {
+        try {
+            return new org.apache.doris.analysis.JsonLiteral(value);
+        } catch (Throwable t) {
+            throw new AnalysisException(t.getMessage(), t);
+        }
+    }
+}
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java
index 605f236ef7a..dbd8382aa90 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java
@@ -234,6 +234,8 @@ public abstract class Literal extends Expression implements 
LeafExpression, Comp
             return new DateV2Literal(desc);
         } else if (targetType.isDateTimeV2Type()) {
             return new DateTimeV2Literal((DateTimeV2Type) targetType, desc);
+        } else if (targetType.isJsonType()) {
+            return new JsonLiteral(desc);
         }
         throw new AnalysisException("cannot cast " + desc + " from type " + 
this.dataType + " to type " + targetType);
     }
@@ -277,6 +279,8 @@ public abstract class Literal extends Expression implements 
LeafExpression, Comp
             return new DateTimeLiteral(stringValue);
         } else if (dataType.isDateTimeV2Type()) {
             return new DateTimeV2Literal(stringValue);
+        } else if (dataType.isJsonType()) {
+            return new JsonLiteral(stringValue);
         } else {
             throw new AnalysisException("Unsupported convert the " + 
literalExpr.getType()
                     + " of legacy literal to nereids literal");


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to