qzsee commented on code in PR #12151:
URL: https://github.com/apache/doris/pull/12151#discussion_r963205097


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/Literal.java:
##########
@@ -82,6 +107,90 @@ public <R, C> R accept(ExpressionVisitor<R, C> visitor, C 
context) {
         return visitor.visitLiteral(this, context);
     }
 
+    public boolean isNull() {
+        return this instanceof NullLiteral;
+    }
+
+    /**
+     * literal expr compare.
+     */
+    public int compareTo(Literal other) {
+        if (isNull() && other.isNull()) {
+            return 0;
+        }
+        DataType oType = other.getDataType();
+        DataType type = getDataType();
+        if (!type.equals(oType)) {
+            throw new RuntimeException("data type not equal!");
+        } else if (type.isBooleanType()) {
+            return Boolean.compare((boolean) getValue(), (boolean) 
other.getValue());
+        } else if (type.isTinyIntType()) {
+            return Byte.compare((byte) getValue(), (byte) other.getValue());
+        } else if (type.isSmallIntType()) {
+            return Short.compare((short) getValue(), (short) other.getValue());
+        } else if (type.isIntType()) {
+            return Integer.compare((int) getValue(), (int) other.getValue());
+        } else if (type.isBigIntType()) {
+            return Long.compare((long) getValue(), (long) other.getValue());
+        } else if (type.isLargeIntType()) {
+            return ((BigInteger) getValue()).compareTo((BigInteger) 
other.getValue());
+        } else if (type.isFloatType()) {
+            return Float.compare((float) getValue(), (float) other.getValue());
+        } else if (type.isDoubleType()) {
+            return Double.compare((double) getValue(), (double) 
other.getValue());
+        } else if (type.isDecimalType()) {
+            return Long.compare((Long) getValue(), (Long) other.getValue());
+        } else if (type.isDateType()) {
+            // todo process date
+        } else if (type.isDecimalType()) {
+            return ((BigDecimal) getValue()).compareTo((BigDecimal) 
other.getValue());
+        } else if (type instanceof StringType) {
+            return StringUtils.compare((String) getValue(), (String) 
other.getValue());
+        }
+        return -1;
+    }
+
+    @Override
+    protected Expression uncheckedCastTo(DataType targetType) throws 
AnalysisException {
+        String desc = getStringValue();
+        if (targetType.isBooleanType()) {
+            if ("0".equals(desc) || 
"false".equals(desc.toLowerCase(Locale.ROOT))) {
+                return Literal.of(false);
+            }
+            if ("1".equals(desc) || 
"true".equals(desc.toLowerCase(Locale.ROOT))) {
+                return Literal.of(true);
+            }
+        }
+        if (targetType.isTinyIntType()) {
+            return Literal.of(Double.valueOf(desc).byteValue());
+        } else if (targetType.isSmallIntType()) {
+            return Literal.of(Double.valueOf(desc).shortValue());
+        } else if (targetType.isIntType()) {
+            return Literal.of(Double.valueOf(desc).intValue());
+        } else if (targetType.isBigIntType()) {
+            return Literal.of(Double.valueOf(desc).longValue());
+        } else if (targetType.isLargeIntType()) {
+            return Literal.of(new BigInteger(desc));
+        } else if (targetType.isFloatType()) {
+            return Literal.of(Float.parseFloat(desc));
+        } else if (targetType.isDoubleType()) {
+            return Literal.of(Double.parseDouble(desc));
+        } else if (targetType.isCharType()) {
+            return new CharLiteral(desc, ((CharType) targetType).getLen());
+        } else if (targetType.isVarcharType()) {
+            return new VarcharLiteral(desc, ((VarcharType) 
targetType).getLen());
+        } else if (targetType.isStringType()) {
+            return Literal.of(desc);
+        } else if (targetType.isDate()) {
+            return new DateLiteral(desc);
+        } else if (targetType.isDateTime()) {
+            return new DateTimeLiteral(desc);
+        } else if (targetType.isDecimalType()) {
+            return new 
DecimalLiteral(BigDecimal.valueOf(Double.parseDouble(desc)));

Review Comment:
   Literal generic create functions, so if you put them in each literal there's 
redundant code



-- 
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