This is an automated email from the ASF dual-hosted git repository.
lijibing 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 58561467912 [fix](cast)Update float/double to string format. (#55322)
58561467912 is described below
commit 585614679127e13ec64b8593f32f6b60ff886b5b
Author: James <[email protected]>
AuthorDate: Mon Sep 1 18:28:57 2025 +0800
[fix](cast)Update float/double to string format. (#55322)
### What problem does this PR solve?
Update float and double to string cast format and the mysql output
format.
---
.../org/apache/doris/analysis/FloatLiteral.java | 14 ++-
.../trees/expressions/literal/DoubleLiteral.java | 6 +-
.../trees/expressions/literal/FloatLiteral.java | 6 +-
.../expressions/literal/FractionalLiteral.java | 37 ++------
.../literal/format/FractionalFormat.java | 67 +++++++++++++
.../apache/doris/analysis/FloatLiteralTest.java | 104 +++++++++++++++++++++
.../expressions/literal/DoubleLiteralTest.java | 64 ++++++++-----
.../expressions/literal/FloatLiteralTest.java | 37 ++++++++
.../data/correctness_p0/test_cast_null.out | Bin 165 -> 169 bytes
regression-test/data/query_p0/cast/test_cast.out | Bin 1184 -> 1214 bytes
.../suites/query_p0/cast/test_cast.groovy | 2 +-
11 files changed, 271 insertions(+), 66 deletions(-)
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/analysis/FloatLiteral.java
b/fe/fe-core/src/main/java/org/apache/doris/analysis/FloatLiteral.java
index 2383c9bf8a7..23da538bcbd 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/FloatLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/FloatLiteral.java
@@ -25,6 +25,7 @@ import org.apache.doris.catalog.Type;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.FormatOptions;
import org.apache.doris.common.NotImplementedException;
+import
org.apache.doris.nereids.trees.expressions.literal.format.FractionalFormat;
import org.apache.doris.thrift.TExprNode;
import org.apache.doris.thrift.TExprNodeType;
import org.apache.doris.thrift.TFloatLiteral;
@@ -167,10 +168,17 @@ public class FloatLiteral extends NumericLiteralExpr {
String timeStr = getStringValue();
return timeStr.substring(1, timeStr.length() - 1);
} else {
- if (Double.isInfinite(getValue()) || Double.isNaN(getValue())) {
- return Double.toString(getValue());
+ if (type == Type.FLOAT) {
+ Float fValue = (float) value;
+ if (fValue.equals(Float.POSITIVE_INFINITY)) {
+ value = Double.POSITIVE_INFINITY;
+ }
+ if (fValue.equals(Float.NEGATIVE_INFINITY)) {
+ value = Double.NEGATIVE_INFINITY;
+ }
}
- return
BigDecimal.valueOf(getValue()).stripTrailingZeros().toPlainString();
+ return FractionalFormat.getFormatStringValue(value, type ==
Type.DOUBLE ? 16 : 7,
+ type == Type.DOUBLE ? "%.15E" : "%.6E");
}
}
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DoubleLiteral.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DoubleLiteral.java
index 270b8511537..964aa06ff6c 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DoubleLiteral.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DoubleLiteral.java
@@ -91,14 +91,14 @@ public class DoubleLiteral extends FractionalLiteral {
return new
org.apache.doris.nereids.trees.expressions.literal.FloatLiteral(
Float.parseFloat(String.valueOf(value)));
} else if (targetType.isStringType()) {
- return new StringLiteral(getStringValue());
+ return new StringLiteral(castToString());
} else if (targetType.isCharType()) {
- String desc = getStringValue();
+ String desc = castToString();
if (((CharType) targetType).getLen() >= desc.length()) {
return new CharLiteral(desc, ((CharType) targetType).getLen());
}
} else if (targetType.isVarcharType()) {
- String desc = getStringValue();
+ String desc = castToString();
return new VarcharLiteral(desc, ((VarcharType)
targetType).getLen());
} else if (targetType.isDecimalV2Type() ||
targetType.isDecimalV3Type()) {
if (Double.isInfinite(value) || Double.isNaN(value)) {
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/FloatLiteral.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/FloatLiteral.java
index 72ead60c994..83f84f5bd7b 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/FloatLiteral.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/FloatLiteral.java
@@ -70,14 +70,14 @@ public class FloatLiteral extends FractionalLiteral {
if (targetType.isDoubleType()) {
return new
DoubleLiteral(Double.parseDouble(String.valueOf(value)));
} else if (targetType.isStringType()) {
- return new StringLiteral(getStringValue());
+ return new StringLiteral(castToString());
} else if (targetType.isCharType()) {
- String desc = getStringValue();
+ String desc = castToString();
if (((CharType) targetType).getLen() >= desc.length()) {
return new CharLiteral(desc, ((CharType) targetType).getLen());
}
} else if (targetType.isVarcharType()) {
- String desc = getStringValue();
+ String desc = castToString();
return new VarcharLiteral(desc, ((VarcharType)
targetType).getLen());
} else if (targetType.isDecimalV2Type() ||
targetType.isDecimalV3Type()) {
if (Float.isInfinite(value) || Float.isNaN(value)) {
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/FractionalLiteral.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/FractionalLiteral.java
index 129271c9baa..f39ff821940 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/FractionalLiteral.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/FractionalLiteral.java
@@ -20,10 +20,10 @@ package org.apache.doris.nereids.trees.expressions.literal;
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.exceptions.CastException;
import org.apache.doris.nereids.trees.expressions.Expression;
+import
org.apache.doris.nereids.trees.expressions.literal.format.FractionalFormat;
import org.apache.doris.nereids.types.DataType;
import java.math.BigDecimal;
-import java.math.MathContext;
import java.math.RoundingMode;
/**
@@ -106,38 +106,13 @@ public abstract class FractionalLiteral extends
NumericLiteral {
return super.uncheckedCastTo(targetType);
}
- @Override
- public String getStringValue() {
+ protected String castToString() {
Object object = getValue();
if (object instanceof BigDecimal) {
- return super.getStringValue();
- }
- double num = object instanceof Double ? (Double) object : new
Double((Float) object);
- if (Double.isNaN(num)) {
- return "NaN";
- }
- if (Double.isInfinite(num)) {
- return num > 0 ? "Infinity" : "-Infinity";
- }
- if (Double.compare(num, 0.0) == 0) {
- return "0";
- }
- if (Double.compare(num, -0.0) == 0) {
- return "-0";
- }
- int precision = this instanceof DoubleLiteral ? 17 : 9;
- int expUpper = this instanceof DoubleLiteral ? 17 : 9;
- String decimalFormat = this instanceof DoubleLiteral ? "%.17f" :
"%.9f";
- String sciFormat = this instanceof DoubleLiteral ? "%.16E" : "%.5E";
- MathContext mc = new MathContext(precision, RoundingMode.HALF_UP);
- BigDecimal bd = new BigDecimal(String.valueOf(num)).round(mc);
- double value = bd.doubleValue();
- int exponent = (int) Math.floor(Math.log10(Math.abs(value)));
- if (exponent < expUpper && exponent >= -4) {
- return String.format(decimalFormat, bd).replaceAll("0+$",
"").replaceAll("\\.$", "");
- } else {
- return String.format(sciFormat,
bd).replaceAll("(\\.\\d*?[1-9])0*E", "$1E")
- .replaceAll("\\.0*E", "E").replaceAll("E", "e");
+ return getStringValue();
}
+ double value = object instanceof Double ? (Double) object : new
Double(String.valueOf(object));
+ return FractionalFormat.getFormatStringValue(value, this instanceof
DoubleLiteral ? 16 : 7,
+ this instanceof DoubleLiteral ? "%.15E" : "%.6E");
}
}
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/format/FractionalFormat.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/format/FractionalFormat.java
new file mode 100644
index 00000000000..8a711f38b46
--- /dev/null
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/format/FractionalFormat.java
@@ -0,0 +1,67 @@
+// 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.format;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+
+/**
+ * Util class for float/double to string.
+ */
+public class FractionalFormat {
+
+ /**
+ * Get string of double/float value for cast to string and output to mysql.
+ *
+ * @param value The double/float value.
+ * @param precision precision
+ * @param sciFormat format for string with scientific form.
+ * @return string value.
+ */
+ public static String getFormatStringValue(double value, int precision,
String sciFormat) {
+ if (Double.isNaN(value)) {
+ return "NaN";
+ }
+ if (Double.isInfinite(value)) {
+ return value > 0 ? "Infinity" : "-Infinity";
+ }
+ if (Double.compare(value, 0.0) == 0) {
+ return "0";
+ }
+ if (Double.compare(value, -0.0) == 0) {
+ return "-0";
+ }
+ int expLower = -4;
+ int exponent = (int) Math.floor(Math.log10(Math.abs(value)));
+ if (exponent < precision && exponent >= expLower) {
+ BigDecimal bd = new BigDecimal(value);
+ bd = bd.setScale(precision - bd.precision() + bd.scale(),
RoundingMode.HALF_UP);
+ String result = bd.toPlainString();
+ if (result.contains(".")) {
+ result = result.replaceAll("0+$", "");
+ if (result.endsWith(".")) {
+ result = result.substring(0, result.length() - 1);
+ }
+ }
+ return result;
+ } else {
+ return String.format(sciFormat,
value).replaceAll("(\\.\\d*?[1-9])0*E", "$1E")
+ .replaceAll("\\.0*E", "E").replaceAll("E", "e");
+ }
+ }
+}
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/analysis/FloatLiteralTest.java
b/fe/fe-core/src/test/java/org/apache/doris/analysis/FloatLiteralTest.java
new file mode 100644
index 00000000000..8d0ef45fdf5
--- /dev/null
+++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/FloatLiteralTest.java
@@ -0,0 +1,104 @@
+// 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.analysis;
+
+import org.apache.doris.catalog.Type;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class FloatLiteralTest {
+
+ @Test
+ public void testDoubleGetStringValue() {
+ Assertions.assertEquals("0", new FloatLiteral(0d,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("0", new FloatLiteral(0.0,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("-0", new FloatLiteral(-0d,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("1", new FloatLiteral(1d,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("1", new FloatLiteral(1.0,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("-1", new FloatLiteral(-1d,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("1.554", new FloatLiteral(1.554,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("0.338", new FloatLiteral(0.338,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("-1", new FloatLiteral(-1.0,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("1e+100", new FloatLiteral(1e100,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("1e-100", new FloatLiteral(1e-100,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("1000000000000000", new FloatLiteral(1.0E15,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("-1000000000000000", new FloatLiteral(-1.0E15,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("1e+16", new FloatLiteral(1.0E16,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("-1e+16", new FloatLiteral(-1.0E16,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("0.0001", new FloatLiteral(0.0001,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("1e-05", new FloatLiteral(0.00001,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("1e+308", new FloatLiteral(1e308,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("-1e+308", new FloatLiteral(-1e308,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("Infinity", new
FloatLiteral(Double.POSITIVE_INFINITY,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("-Infinity", new
FloatLiteral(Double.NEGATIVE_INFINITY,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("NaN", new FloatLiteral(Double.NaN,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("1234567890123456", new
FloatLiteral(1234567890123456.12345, Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("1.234567890123457e+16", new
FloatLiteral(12345678901234567.12345,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("0.0001234567890123457", new
FloatLiteral(0.0001234567890123456789,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("1.234567890123456e-15", new
FloatLiteral(0.000000000000001234567890123456,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("123.456", new FloatLiteral(123.456000,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("123", new FloatLiteral(123.000,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("-1234567890123456", new
FloatLiteral(-1234567890123456.12345,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("-1.234567890123457e+16", new
FloatLiteral(-12345678901234567.12345,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("-0.0001234567890123457", new
FloatLiteral(-0.0001234567890123456789,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("-1.234567890123456e-15", new
FloatLiteral(-0.000000000000001234567890123456,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("-123.456", new FloatLiteral(-123.456000,
Type.DOUBLE).getStringValueForQuery(null));
+ Assertions.assertEquals("-123", new FloatLiteral(-123.000,
Type.DOUBLE).getStringValueForQuery(null));
+ }
+
+ @Test
+ public void testFloatGetStringValue() {
+ Assertions.assertEquals("0", new FloatLiteral(0d,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("0", new FloatLiteral(0.0,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("-0", new FloatLiteral(-0d,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("1", new FloatLiteral(1d,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("1", new FloatLiteral(1.0d,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("-1", new FloatLiteral(-1d,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("1.554", new FloatLiteral(1.554d,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("0.338", new FloatLiteral(0.338d,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("-1", new FloatLiteral(-1.0d,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("1e+38", new FloatLiteral(1e38,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("1e-38", new FloatLiteral(1e-38,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("1000000", new
FloatLiteral(1.0E6).getStringValueForQuery(null));
+ Assertions.assertEquals("-1000000", new FloatLiteral(-1.0E6,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("1e+07", new FloatLiteral(1.0E7,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("-1e+07", new FloatLiteral(-1.0E7,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("0.0001", new FloatLiteral(0.0001,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("1e-05", new FloatLiteral(0.00001,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("3.402823e+38", new FloatLiteral((double)
Float.MAX_VALUE, Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("Infinity", new FloatLiteral(1e39,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("-Infinity", new FloatLiteral(-1e39,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("Infinity", new
FloatLiteral(Double.POSITIVE_INFINITY,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("-Infinity", new
FloatLiteral(Double.NEGATIVE_INFINITY,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("NaN", new FloatLiteral(Double.NaN,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("123456.1", new FloatLiteral(123456.12345,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("1234567", new FloatLiteral(1234567.12345,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("1.234568e+07", new
FloatLiteral(12345678.12345, Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("0.0001234568", new
FloatLiteral(0.0001234567890123456789,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("1.234568e-15", new
FloatLiteral(0.000000000000001234567890123456,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("123.456", new FloatLiteral(123.456000,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("123", new FloatLiteral(123.000,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("-1234567", new FloatLiteral(-1234567.12345,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("-1.234568e+07", new
FloatLiteral(-12345678.12345, Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("-0.0001234568", new
FloatLiteral(-0.0001234567890123456789,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("-1.234568e-15", new
FloatLiteral(-0.000000000000001234567890123456,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("-123.456", new FloatLiteral(-123.456000,
Type.FLOAT).getStringValueForQuery(null));
+ Assertions.assertEquals("-123", new FloatLiteral(-123.000,
Type.FLOAT).getStringValueForQuery(null));
+ }
+}
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/DoubleLiteralTest.java
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/DoubleLiteralTest.java
index 4c7c4010517..bc3984d8fa9 100644
---
a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/DoubleLiteralTest.java
+++
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/DoubleLiteralTest.java
@@ -28,6 +28,7 @@ import org.apache.doris.nereids.types.FloatType;
import org.apache.doris.nereids.types.IntegerType;
import org.apache.doris.nereids.types.LargeIntType;
import org.apache.doris.nereids.types.SmallIntType;
+import org.apache.doris.nereids.types.StringType;
import org.apache.doris.nereids.types.TinyIntType;
import org.junit.jupiter.api.Assertions;
@@ -35,31 +36,6 @@ import org.junit.jupiter.api.Test;
class DoubleLiteralTest {
- @Test
- public void testGetStringValue() {
- Assertions.assertEquals("0", new DoubleLiteral(0).getStringValue());
- Assertions.assertEquals("0", new DoubleLiteral(0.0).getStringValue());
- Assertions.assertEquals("0", new DoubleLiteral(-0).getStringValue());
- Assertions.assertEquals("1", new DoubleLiteral(1).getStringValue());
- Assertions.assertEquals("1", new DoubleLiteral(1.0).getStringValue());
- Assertions.assertEquals("-1", new DoubleLiteral(-1).getStringValue());
- Assertions.assertEquals("1.554", new
DoubleLiteral(1.554).getStringValue());
- Assertions.assertEquals("0.338", new
DoubleLiteral(0.338).getStringValue());
- Assertions.assertEquals("-1", new
DoubleLiteral(-1.0).getStringValue());
- Assertions.assertEquals("1e+100", new
DoubleLiteral(1e100).getStringValue());
- Assertions.assertEquals("1e-100", new
DoubleLiteral(1e-100).getStringValue());
- Assertions.assertEquals("10000000000000000", new
DoubleLiteral(1.0E16).getStringValue());
- Assertions.assertEquals("-10000000000000000", new
DoubleLiteral(-1.0E16).getStringValue());
- Assertions.assertEquals("1e+17", new
DoubleLiteral(1.0E17).getStringValue());
- Assertions.assertEquals("-1e+17", new
DoubleLiteral(-1.0E17).getStringValue());
- Assertions.assertEquals("0.0001", new
DoubleLiteral(0.0001).getStringValue());
- Assertions.assertEquals("1e+308", new
DoubleLiteral(1e308).getStringValue());
- Assertions.assertEquals("-1e+308", new
DoubleLiteral(-1e308).getStringValue());
- Assertions.assertEquals("Infinity", new
DoubleLiteral(Double.POSITIVE_INFINITY).getStringValue());
- Assertions.assertEquals("-Infinity", new
DoubleLiteral(Double.NEGATIVE_INFINITY).getStringValue());
- Assertions.assertEquals("NaN", new
DoubleLiteral(Double.NaN).getStringValue());
- }
-
@Test
void testUncheckedCastTo() {
// To boolean
@@ -344,4 +320,42 @@ class DoubleLiteralTest {
DoubleLiteral finalF9 = f1;
Assertions.assertThrows(CastException.class, () ->
finalF9.uncheckedCastTo(DateType.INSTANCE));
}
+
+ @Test
+ public void testDoubleGetStringValueFor() {
+ Assertions.assertEquals("0", ((StringLiteral) (new
DoubleLiteral(0d).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("0", ((StringLiteral) (new
DoubleLiteral(0.0).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-0", ((StringLiteral) (new
DoubleLiteral(-0d).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1", ((StringLiteral) (new
DoubleLiteral(1d).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1", ((StringLiteral) (new
DoubleLiteral(1.0).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1", ((StringLiteral) (new
DoubleLiteral(-1d).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1.554", ((StringLiteral) (new
DoubleLiteral(1.554).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("0.338", ((StringLiteral) (new
DoubleLiteral(0.338).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1", ((StringLiteral) (new
DoubleLiteral(-1.0).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1e+100", ((StringLiteral) (new
DoubleLiteral(1e100).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1e-100", ((StringLiteral) (new
DoubleLiteral(1e-100).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1000000000000000", ((StringLiteral) (new
DoubleLiteral(1.0E15).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1000000000000000", ((StringLiteral) (new
DoubleLiteral(-1.0E15).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1e+16", ((StringLiteral) (new
DoubleLiteral(1.0E16).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1e+16", ((StringLiteral) (new
DoubleLiteral(-1.0E16).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("0.0001", ((StringLiteral) (new
DoubleLiteral(0.0001).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1e-05", ((StringLiteral) (new
DoubleLiteral(0.00001).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1e+308", ((StringLiteral) (new
DoubleLiteral(1e308).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1e+308", ((StringLiteral) (new
DoubleLiteral(-1e308).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("Infinity", ((StringLiteral) (new
DoubleLiteral(Double.POSITIVE_INFINITY).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-Infinity", ((StringLiteral) (new
DoubleLiteral(Double.NEGATIVE_INFINITY).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("NaN", ((StringLiteral) (new
DoubleLiteral(Double.NaN).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1234567890123456", ((StringLiteral) (new
DoubleLiteral(1234567890123456.12345).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1.234567890123457e+16", ((StringLiteral) (new
DoubleLiteral(12345678901234567.12345).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("0.0001234567890123457", ((StringLiteral) (new
DoubleLiteral(0.0001234567890123456789).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1.234567890123456e-15", ((StringLiteral) (new
DoubleLiteral(0.000000000000001234567890123456).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("123.456", ((StringLiteral) (new
DoubleLiteral(123.456000).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("123", ((StringLiteral) (new
DoubleLiteral(123.000).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1234567890123456", ((StringLiteral) (new
DoubleLiteral(-1234567890123456.12345).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1.234567890123457e+16", ((StringLiteral)
(new
DoubleLiteral(-12345678901234567.12345).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-0.0001234567890123457", ((StringLiteral)
(new
DoubleLiteral(-0.0001234567890123456789).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1.234567890123456e-15", ((StringLiteral)
(new
DoubleLiteral(-0.000000000000001234567890123456).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-123.456", ((StringLiteral) (new
DoubleLiteral(-123.456000).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-123", ((StringLiteral) (new
DoubleLiteral(-123.000).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ }
}
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/FloatLiteralTest.java
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/FloatLiteralTest.java
index 8442a5736db..50696a249a6 100644
---
a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/FloatLiteralTest.java
+++
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/literal/FloatLiteralTest.java
@@ -29,6 +29,7 @@ import org.apache.doris.nereids.types.DoubleType;
import org.apache.doris.nereids.types.IntegerType;
import org.apache.doris.nereids.types.LargeIntType;
import org.apache.doris.nereids.types.SmallIntType;
+import org.apache.doris.nereids.types.StringType;
import org.apache.doris.nereids.types.TinyIntType;
import org.junit.jupiter.api.Assertions;
@@ -285,4 +286,40 @@ public class FloatLiteralTest {
Assertions.assertThrows(CastException.class, () ->
finalF9.uncheckedCastTo(DateType.INSTANCE));
}
+
+ @Test
+ public void testFloatGetStringValue() {
+ Assertions.assertEquals("0", ((StringLiteral) (new
FloatLiteral(0f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("0", ((StringLiteral) (new
FloatLiteral(0.0f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-0", ((StringLiteral) (new
FloatLiteral(-0f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1", ((StringLiteral) (new
FloatLiteral(1f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1", ((StringLiteral) (new
FloatLiteral(1.0f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1", ((StringLiteral) (new
FloatLiteral(-1f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1.554", ((StringLiteral) (new
FloatLiteral(1.554f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("0.338", ((StringLiteral) (new
FloatLiteral(0.338f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1", ((StringLiteral) (new
FloatLiteral(-1.0f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1e+38", ((StringLiteral) (new
FloatLiteral((float) 1e38).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1e+38", ((StringLiteral) (new
FloatLiteral((float) -1e38).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1000000", ((StringLiteral) (new
FloatLiteral((float) 1.0E6).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1000000", ((StringLiteral) (new
FloatLiteral((float) -1.0E6).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1e+07", ((StringLiteral) (new
FloatLiteral((float) 1.0E7).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1e+07", ((StringLiteral) (new
FloatLiteral((float) -1.0E7).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("0.0001", ((StringLiteral) (new
FloatLiteral(0.0001f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1e-05", ((StringLiteral) (new
FloatLiteral(0.00001f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("Infinity", ((StringLiteral) (new
FloatLiteral(Float.POSITIVE_INFINITY).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-Infinity", ((StringLiteral) (new
FloatLiteral(Float.NEGATIVE_INFINITY).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("NaN", ((StringLiteral) (new
FloatLiteral(Float.NaN).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1234567", ((StringLiteral) (new
FloatLiteral(1234567.12345f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1.234568e+07", ((StringLiteral) (new
FloatLiteral(12345678.12345f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("0.0001234568", ((StringLiteral) (new
FloatLiteral(0.0001234567890123456789f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("1.234568e-15", ((StringLiteral) (new
FloatLiteral(0.000000000000001234567890123456f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("123.456", ((StringLiteral) (new
FloatLiteral(123.456000f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("123", ((StringLiteral) (new
FloatLiteral(123.000f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1234567", ((StringLiteral) (new
FloatLiteral(-1234567.12345f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1.234568e+07", ((StringLiteral) (new
FloatLiteral(-12345678.12345f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-0.0001234568", ((StringLiteral) (new
FloatLiteral(-0.0001234567890123456789f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-1.234568e-15", ((StringLiteral) (new
FloatLiteral(-0.000000000000001234567890123456f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-123.456", ((StringLiteral) (new
FloatLiteral(-123.456000f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ Assertions.assertEquals("-123", ((StringLiteral) (new
FloatLiteral(-123.000f).uncheckedCastTo(StringType.INSTANCE))).getValue());
+ }
}
diff --git a/regression-test/data/correctness_p0/test_cast_null.out
b/regression-test/data/correctness_p0/test_cast_null.out
index 89eb3fe0a32..d7381af02f8 100644
Binary files a/regression-test/data/correctness_p0/test_cast_null.out and
b/regression-test/data/correctness_p0/test_cast_null.out differ
diff --git a/regression-test/data/query_p0/cast/test_cast.out
b/regression-test/data/query_p0/cast/test_cast.out
index c1472adc809..c6d84582b2a 100644
Binary files a/regression-test/data/query_p0/cast/test_cast.out and
b/regression-test/data/query_p0/cast/test_cast.out differ
diff --git a/regression-test/suites/query_p0/cast/test_cast.groovy
b/regression-test/suites/query_p0/cast/test_cast.groovy
index cca654cfc88..2f3d72b7cac 100644
--- a/regression-test/suites/query_p0/cast/test_cast.groovy
+++ b/regression-test/suites/query_p0/cast/test_cast.groovy
@@ -29,7 +29,7 @@ suite('test_cast', "arrow_flight_sql") {
}
test {
sql "select cast(${datetime} as bigint), cast(${datetime} as float),
cast(${datetime} as double)"
- result([[20200101123445l, ((float) 20200101123445l), ((double)
20200101123445l)]])
+ result([[20200101123445l, ((float) 20200100000000l), ((double)
20200101123445l)]])
}
test {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]