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 a8688509c98 [opt](Nereids) support cast bewteen numeric and  boolean 
in FE (#29006)
a8688509c98 is described below

commit a8688509c984b949ba55a2019c78c6a982de2feb
Author: morrySnow <[email protected]>
AuthorDate: Tue Dec 26 14:11:34 2023 +0800

    [opt](Nereids) support cast bewteen numeric and  boolean in FE (#29006)
---
 .../nereids/trees/expressions/literal/Literal.java | 29 +++++++++++++++++-----
 1 file changed, 23 insertions(+), 6 deletions(-)

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 0d4ffc86ba9..29a3a3287cc 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
@@ -185,15 +185,15 @@ public abstract class Literal extends Expression 
implements LeafExpression, Comp
                 minVal = new BigDecimal(LargeIntType.MIN_VALUE);
             } else if (targetType.isFloatType()) {
                 maxVal = new BigDecimal(Float.MAX_VALUE);
-                minVal = new BigDecimal(-Float.MAX_VALUE);
+                minVal = BigDecimal.valueOf(-Float.MAX_VALUE);
             } else if (targetType.isDoubleType()) {
                 maxVal = new BigDecimal(Double.MAX_VALUE);
-                minVal = new BigDecimal(-Double.MAX_VALUE);
+                minVal = BigDecimal.valueOf(-Double.MAX_VALUE);
             }
 
             if (val.compareTo(maxVal) > 0 || val.compareTo(minVal) < 0) {
                 throw new AnalysisException(
-                        String.format("{} can't cast to {}", desc, 
targetType));
+                        String.format("%s can't cast to %s", desc, 
targetType));
             }
         }
         return uncheckedCastTo(targetType);
@@ -209,7 +209,24 @@ public abstract class Literal extends Expression 
implements LeafExpression, Comp
         }
         // TODO support string to complex
         String desc = getStringValue();
+        // convert boolean to byte string value to support cast boolean to 
numeric in FE.
+        if (this.equals(BooleanLiteral.TRUE)) {
+            desc = "1";
+        } else if (this.equals(BooleanLiteral.FALSE)) {
+            desc = "0";
+        }
         if (targetType.isBooleanType()) {
+            try {
+                // convert any non-zero numeric literal to true if target type 
is boolean
+                long value = Long.parseLong(desc);
+                if (value == 0) {
+                    return Literal.of(false);
+                } else {
+                    return Literal.of(true);
+                }
+            } catch (Exception e) {
+                // ignore
+            }
             if ("0".equals(desc) || 
"false".equals(desc.toLowerCase(Locale.ROOT))) {
                 return Literal.of(false);
             }
@@ -218,11 +235,11 @@ public abstract class Literal extends Expression 
implements LeafExpression, Comp
             }
         }
         if (targetType.isTinyIntType()) {
-            return Literal.of(Byte.valueOf(desc).byteValue());
+            return Literal.of(Byte.valueOf(desc));
         } else if (targetType.isSmallIntType()) {
-            return Literal.of(Short.valueOf(desc).shortValue());
+            return Literal.of(Short.valueOf(desc));
         } else if (targetType.isIntegerType()) {
-            return Literal.of(Integer.valueOf(desc).intValue());
+            return Literal.of(Integer.valueOf(desc));
         } else if (targetType.isBigIntType()) {
             return Literal.of(Long.valueOf(desc));
         } else if (targetType.isLargeIntType()) {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to