From 2d6e975d5a81ff460a0972d0133b19d98d8258dd Mon Sep 17 00:00:00 2001 From: camby Date: Tue, 5 Mar 2024 18:58:03 +0800 Subject: [PATCH] [fix](cast) fix wrong result while cast string to float (#31781) Issue Number: close #31518 --- .../doris/nereids/trees/expressions/literal/FloatLiteral.java | 2 +- .../sql_functions/cast_function/test_cast_function.out | 3 +++ .../sql_functions/cast_function/test_cast_function.out | 3 +++ .../sql_functions/cast_function/test_cast_function.groovy | 1 + .../sql_functions/cast_function/test_cast_function.groovy | 1 + 5 files changed, 9 insertions(+), 1 deletion(-) 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 55d9c98e2d..df75bbcfc5 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 @@ -46,6 +46,6 @@ public class FloatLiteral extends FractionalLiteral { @Override public LiteralExpr toLegacyLiteral() { - return new org.apache.doris.analysis.FloatLiteral((double) value, Type.FLOAT); + return new org.apache.doris.analysis.FloatLiteral(getDouble(), Type.FLOAT); } } diff --git a/regression-test/data/nereids_p0/sql_functions/cast_function/test_cast_function.out b/regression-test/data/nereids_p0/sql_functions/cast_function/test_cast_function.out index f62e9e8675..1eed141184 100644 --- a/regression-test/data/nereids_p0/sql_functions/cast_function/test_cast_function.out +++ b/regression-test/data/nereids_p0/sql_functions/cast_function/test_cast_function.out @@ -20,6 +20,9 @@ -- !sql -- 10000.00001 +-- !sql -- +123.123 + -- !sql_null_cast_bitmap -- true diff --git a/regression-test/data/query_p0/sql_functions/cast_function/test_cast_function.out b/regression-test/data/query_p0/sql_functions/cast_function/test_cast_function.out index 8430f51a8b..8b3214cfa7 100644 --- a/regression-test/data/query_p0/sql_functions/cast_function/test_cast_function.out +++ b/regression-test/data/query_p0/sql_functions/cast_function/test_cast_function.out @@ -20,6 +20,9 @@ -- !sql -- 10000.00001 +-- !sql -- +123.123 + -- !select1 -- 0 2022-12-01T22:23:24.123 2022-12-01 22:23:24.123 2022-12-01 2022-12-01 2022-12-01 2022-12-01 78.123 78.123 78.12345 78.12345 1 2022-12-01T22:23:24.123 2022-12-01 22:23:24.123456789 2022-12-01 2022-12-01 2022-12-01 2022-12-01 78.123 78.123 78.12345 78.12345 diff --git a/regression-test/suites/nereids_p0/sql_functions/cast_function/test_cast_function.groovy b/regression-test/suites/nereids_p0/sql_functions/cast_function/test_cast_function.groovy index 9f348449d1..c601449e31 100644 --- a/regression-test/suites/nereids_p0/sql_functions/cast_function/test_cast_function.groovy +++ b/regression-test/suites/nereids_p0/sql_functions/cast_function/test_cast_function.groovy @@ -25,6 +25,7 @@ suite("test_cast_function") { qt_sql """ select cast (NULL AS CHAR(1)); """ qt_sql """ select cast ('20190101' AS CHAR(2)); """ qt_sql """ select cast(cast(10000.00001 as double) as string); """ + qt_sql """ select cast('123.123' as float); """ qt_sql_null_cast_bitmap """ select cast (case when BITMAP_EMPTY() is NULL then null else null end as bitmap) is NULL; """ qt_sql_to_tiny """ select cast('1212.31' as tinyint);""" qt_sql_to_small """ select cast('1212.31' as smallint);""" diff --git a/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_function.groovy b/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_function.groovy index fe85ba43dc..7f4bbe40eb 100644 --- a/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_function.groovy +++ b/regression-test/suites/query_p0/sql_functions/cast_function/test_cast_function.groovy @@ -23,6 +23,7 @@ suite("test_cast_function") { qt_sql """ select cast (NULL AS CHAR(1)); """ qt_sql """ select cast ('20190101' AS CHAR(2)); """ qt_sql """ select cast(cast(10000.00001 as double) as string); """ + qt_sql """ select cast('123.123' as float); """ def tableName = "test_cast_function_nullable" sql "DROP TABLE IF EXISTS ${tableName}"