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 0d4ffc86ba91a9..29a3a3287ccc51 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 Expression checkedCastTo(DataType targetType) throws AnalysisException { 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 @@ protected Expression uncheckedCastTo(DataType targetType) throws AnalysisExcepti } // 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 @@ protected Expression uncheckedCastTo(DataType targetType) throws AnalysisExcepti } } 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()) {