diff --git a/be/src/vec/data_types/data_type_decimal.h b/be/src/vec/data_types/data_type_decimal.h index a65352aa3710a1..4703ceef105965 100644 --- a/be/src/vec/data_types/data_type_decimal.h +++ b/be/src/vec/data_types/data_type_decimal.h @@ -604,7 +604,13 @@ void convert_from_decimal(typename ToDataType::FieldType* dst, } else { auto multiplier = FromDataType::get_scale_multiplier(scale); for (size_t i = 0; i < size; ++i) { - dst[i] = static_cast(src[i].value) / multiplier.value; + if constexpr (IsDecimal256) { + dst[i] = static_cast(static_cast(src[i].value) / + static_cast(multiplier.value)); + } else { + dst[i] = static_cast(static_cast(src[i].value) / + static_cast(multiplier.value)); + } } } if constexpr (narrow_integral) { diff --git a/regression-test/data/datatype_p0/decimalv3/test_decimal256_cast.out b/regression-test/data/datatype_p0/decimalv3/test_decimal256_cast.out index ac1f3da16dd4fe..3ba2863a6fde33 100644 --- a/regression-test/data/datatype_p0/decimalv3/test_decimal256_cast.out +++ b/regression-test/data/datatype_p0/decimalv3/test_decimal256_cast.out @@ -33,6 +33,21 @@ -- !decimal256_cast10 -- 10 0 --- !decimal256_cast_to_double_1 -- +-- !decimal256_cast_to_float1 -- +1.2345678E7 + +-- !decimal256_cast_to_float2 -- 1.2345678E7 +-- !decimal256_cast_to_float3 -- +1 1.2345678E7 +2 1.00000003E16 +3 2.5079478E-7 +4 1.0 + +-- !decimal256_cast_to_double_1 -- +1 1.2345678E7 +2 1.0E16 +3 2.507947739348449E-7 +4 1.0 + diff --git a/regression-test/data/datatype_p0/decimalv3/test_decimalv3_cast4.out b/regression-test/data/datatype_p0/decimalv3/test_decimalv3_cast4.out index 657ca3072042bc..49e526cd663aa0 100644 --- a/regression-test/data/datatype_p0/decimalv3/test_decimalv3_cast4.out +++ b/regression-test/data/datatype_p0/decimalv3/test_decimalv3_cast4.out @@ -1,6 +1,6 @@ -- This file is automatically generated. You should know what you did if you want to edit this -- !decimal128_to_float_1 -- --9.999999E34 +-1.0E35 -32769.79 -32768.79 -9999.999 @@ -9,9 +9,9 @@ 99.999 127.789 9999.999 -32767.787 -32768.125 -9.999999E34 +32767.79 +32768.12 +1.0E35 -- !decimal128_to_double_1 -- -1.0E35 diff --git a/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy b/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy index 0132e74010dbcc..64c5d87cac02fa 100644 --- a/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy +++ b/regression-test/suites/datatype_p0/decimalv3/test_decimal256_cast.groovy @@ -74,42 +74,84 @@ suite("test_decimal256_cast") { select k1, cast(v1 as decimalv3(76, 0)) from cast_to_dec256 order by k1, v1; """ + qt_decimal256_cast_to_float1 """ + select /*+SET_VAR(enable_fold_constant_by_be = true) */cast(cast("12345678.000000000000000000000000000000001" as decimalv3(76, 60)) as float); + """ + qt_decimal256_cast_to_float2 """ + select /*+SET_VAR(enable_fold_constant_by_be = false) */cast(cast("12345678.000000000000000000000000000000001" as decimalv3(76, 60)) as float); + """ + + sql """ + drop table if exists dec256cast_to_float; + """ + sql """ + create table dec256cast_to_float ( + k1 int, + v1 decimalv3(76, 60) + ) distributed by hash(k1) + properties ( + 'replication_num' = '1' + ); + """ + sql """ + insert into dec256cast_to_float values + (1, "12345678.000000000000000000000000000000001"), + (2, "9999999999999999.999999999999999999999999999999999999999999999999999999999999"), + (3, "0.000000250794773934844880991039000000000000000000000000000000"), + (4, "0.999999999999999999999999999999999999999999999999999999999999"); + """ + + qt_decimal256_cast_to_float3 """ + select k1, cast(v1 as float) from dec256cast_to_float order by 1; + """ + qt_decimal256_cast_to_double_1 """ + select k1, cast(v1 as double) from dec256cast_to_float order by 1; + """ + test { - sql """ - select /*+SET_VAR(enable_fold_constant_by_be = true) */cast(cast("12345678.000000000000000000000000000000001" as decimalv3(76, 60)) as float); - """ + sql """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("1000000000000000000000000000000000000000000000000000000000000000000000.111111" as decimalv3(76, 6)) as float);""" exception "Arithmetic overflow" } test { - sql """ - select /*+SET_VAR(enable_fold_constant_by_be = false) */cast(cast("12345678.000000000000000000000000000000001" as decimalv3(76, 60)) as float); - """ + sql """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("1000000000000000000000000000000000000000000000000000000000000000000000.111111" as decimalv3(76, 6)) as float);""" exception "Arithmetic overflow" } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = false) */cast(cast("9999999999999999999999999999999999999999999999999999999999999999999999.999999" as decimalv3(76, 6)) as float);""" + exception "Arithmetic overflow" + } + test { + sql """select /*+SET_VAR(debug_skip_fold_constant = true) */cast(cast("9999999999999999999999999999999999999999999999999999999999999999999999.999999" as decimalv3(76, 6)) as float);""" + exception "Arithmetic overflow" + } + + sql "drop table if exists dec256cast_to_float_overflow" sql """ - drop table if exists dec256cast_to_float; - """ - sql """ - create table dec256cast_to_float ( + create table dec256cast_to_float_overflow ( k1 int, - v1 decimalv3(76, 60) + v1 decimalv3(76, 6) ) distributed by hash(k1) properties ( 'replication_num' = '1' ); """ sql """ - insert into dec256cast_to_float values (1, "12345678.000000000000000000000000000000001"); + insert into dec256cast_to_float_overflow values (1, "1000000000000000000000000000000000000000000000000000000000000000000000.111111"); """ test { - sql """ - select cast(v1 as float) from dec256cast_to_float; - """ + sql "select cast(v1 as float) from dec256cast_to_float_overflow;" exception "Arithmetic overflow" } - qt_decimal256_cast_to_double_1 """ - select cast(v1 as double) from dec256cast_to_float; + sql """ + truncate table dec256cast_to_float_overflow; """ + sql """ + insert into dec256cast_to_float_overflow values (1, "9999999999999999999999999999999999999999999999999999999999999999999999.999999"); + """ + test { + sql "select cast(v1 as float) from dec256cast_to_float_overflow;" + exception "Arithmetic overflow" + } } \ No newline at end of file