xiaokang commented on code in PR #10322:
URL: https://github.com/apache/doris/pull/10322#discussion_r923967565


##########
fe/fe-core/src/main/java/org/apache/doris/analysis/JsonLiteral.java:
##########
@@ -0,0 +1,197 @@
+// 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.analysis;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.util.Objects;
+
+import com.google.common.base.Preconditions;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonSyntaxException;
+
+import org.apache.doris.common.io.Text;
+import org.apache.doris.thrift.TExprNode;
+import org.apache.doris.thrift.TExprNodeType;
+import org.apache.doris.thrift.TJsonLiteral;
+import org.apache.logging.log4j.LogManager;
+import org.apache.doris.catalog.Type;
+import org.apache.logging.log4j.Logger;
+
+import org.apache.doris.catalog.PrimitiveType;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.DdlException;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.qe.VariableVarConverters;
+
+public class JsonLiteral extends LiteralExpr {
+    private static final Logger LOG = LogManager.getLogger(JsonLiteral.class);
+    private JsonParser parser = new JsonParser();
+    private String value;
+    // Means the converted session variable need to be cast to int, such as 
"cast 'STRICT_TRANS_TABLES' to Integer".
+    private String beConverted = "";
+
+    public JsonLiteral() {
+        super();
+        type = Type.JSON;
+    }
+
+    public JsonLiteral(String value) throws AnalysisException {
+        try {
+            parser.parse(value);
+        } catch (JsonSyntaxException e) {
+            throw new AnalysisException("Invalid json literal: " + 
e.getMessage());
+        }
+        this.value = value;
+        type = Type.JSON;
+        analysisDone();
+    }
+
+    protected JsonLiteral(JsonLiteral other) {
+        super(other);
+        value = other.value;
+    }
+
+    public void setBeConverted(String val) {
+        this.beConverted = val;
+    }
+
+    @Override
+    public Expr clone() {
+        return new JsonLiteral(this);
+    }
+
+    @Override
+    public int compareLiteral(LiteralExpr expr) {
+        if (expr instanceof NullLiteral) {
+            return 1;
+        }
+        if (expr == MaxLiteral.MAX_VALUE) {
+            return -1;
+        }
+        // compare string with utf-8 byte array, same with DM,BE,StorageEngine
+        byte[] thisBytes = null;
+        byte[] otherBytes = null;
+        try {
+            thisBytes = value.getBytes("UTF-8");
+            otherBytes = expr.getStringValue().getBytes("UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            Preconditions.checkState(false);
+        }
+
+        int minLength = Math.min(thisBytes.length, otherBytes.length);
+        int i = 0;
+        for (i = 0; i < minLength; i++) {
+            if (thisBytes[i] < otherBytes[i]) {
+                return -1;
+            } else if (thisBytes[i] > otherBytes[i]) {
+                return 1;
+            }
+        }
+        if (thisBytes.length > otherBytes.length) {
+            if (thisBytes[i] == 0x00) {
+                return 0;
+            } else {
+                return 1;
+            }
+        } else if (thisBytes.length < otherBytes.length) {
+            if (otherBytes[i] == 0x00) {
+                return 0;
+            } else {
+                return -1;
+            }
+        } else {
+            return 0;
+        }
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    @Override
+    public boolean isMinValue() {
+        return false;
+    }
+
+    @Override
+    public String toSqlImpl() {
+        return "'" + value.replaceAll("'", "''") + "'";
+    }
+
+    @Override
+    protected void toThrift(TExprNode msg) {
+        msg.node_type = TExprNodeType.JSON_LITERAL;
+        msg.json_literal = new TJsonLiteral(getUnescapedValue());
+    }
+
+    public String getUnescapedValue() {
+        // Unescape string exactly like Hive does. Hive's method assumes
+        // quotes so we add them here to reuse Hive's code.
+        return value;
+    }
+
+    public String getJsonValue() {
+        return value;
+    }
+
+    @Override
+    public long getLongValue() {
+        return 0;

Review Comment:
   always return 0 is a hidden bug that is not easy to find, just throw a 
RuntimeException



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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

Reply via email to