Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions be/src/vec/data_types/data_type_decimal.h
Original file line numberDiff line numberDiff line change
Expand Up@@ -685,15 +685,20 @@ void convert_to_decimal(typename ToDataType::FieldType* dst,

if constexpr (std::is_floating_point_v<FromFieldType>) {
auto multiplier = ToDataType::get_scale_multiplier(to_scale);
// For decimal256, we need to use long double to avoid overflow when
// static casting the multiplier to floating type, and also to be as precise as possible;
// For other decimal types, we use double to be as precise as possible.
using DoubleType = std::conditional_t<IsDecimal256<typename ToDataType::FieldType>,
long double, double>;
if constexpr (narrow_integral) {
for (size_t i = 0; i < size; ++i) {
if (!std::isfinite(src[i])) {
throw Exception(
ErrorCode::ARITHMETIC_OVERFLOW_ERRROR,
"Decimal convert overflow. Cannot convert infinity or NaN to decimal");
}
FromFieldType tmp = src[i] * multiplier;
if (tmp <= FromFieldType(min_result) || tmp >= FromFieldType(max_result)) {
DoubleType tmp = src[i] * static_cast<DoubleType>(multiplier.value);
if (tmp <= DoubleType(min_result.value) || tmp >= DoubleType(max_result.value)) {
ToDataType to_data_type(to_precision, to_scale);
throw Exception(
ErrorCode::ARITHMETIC_OVERFLOW_ERRROR,
Expand All@@ -703,8 +708,9 @@ void convert_to_decimal(typename ToDataType::FieldType* dst,
}
}
for (size_t i = 0; i < size; ++i) {
dst[i].value = typename ToDataType::FieldType::NativeType(
FromFieldType(src[i] * multiplier.value + ((src[i] >= 0) ? 0.5 : -0.5)));
dst[i].value = static_cast<ToDataType::FieldType::NativeType>(
static_cast<double>(src[i] * static_cast<DoubleType>(multiplier.value) +
((src[i] >= 0) ? 0.5 : -0.5)));
}
} else {
using DecimalFrom =
Expand Down
202 changes: 202 additions & 0 deletions regression-test/data/datatype_p0/decimalv3/test_decimal256_cast.out
Original file line numberDiff line numberDiff line change
Expand Up@@ -51,3 +51,205 @@
3 2.507947739348449E-7
4 1.0

-- !cast_float_to_decimal256_const_1_1 --
219.77717590332030098495757448650589208640

-- !cast_float_to_decimal256_const_1_2 --
219.77718000000000000000000000000000000000

-- !cast_float_to_decimal256_const_2_1 --
-219.77717590332030098495757448650589208640

-- !cast_float_to_decimal256_const_2_2 --
-219.77718000000000000000000000000000000000

-- !cast_float_to_decimal256_const_3_1 --
99999996802856929887877846998705567504.08319368450610608065324841755887360000

-- !cast_float_to_decimal256_const_3_2 --
99999996802856929887877846998705567504.08319368450610608065324841755887360000

-- !cast_float_to_decimal256_const_4_1 --
-99999996802856929887877846998705567504.08319368450610608065324841755887360000

-- !cast_float_to_decimal256_const_4_2 --
-99999996802856929887877846998705567504.08319368450610608065324841755887360000

-- !cast_float_to_decimal256_const_5_1 --
9999999848243207687231096732081.517441445022649282668861247346191519797952000

-- !cast_float_to_decimal256_const_5_2 --
9999999848243207687231096732081.517441445022649282668861247346191519797952000

-- !cast_float_to_decimal256_const_6_1 --
-9999999848243207687231096732081.517441445022649282668861247346191519797952000

-- !cast_float_to_decimal256_const_6_2 --
-9999999848243207687231096732081.517441445022649282668861247346191519797952000

-- !cast_float_to_decimal256_const_7_1 --
0.8999999761581421686088389507648326470906229208913952822970533869391577408000

-- !cast_float_to_decimal256_const_7_2 --
0.9000000000000000000000000000000000000000000000000000000000000000000000000000

-- !cast_float_to_decimal256_const_8_1 --
-0.8999999761581421686088389507648326470906229208913952822970533869391577408000

-- !cast_float_to_decimal256_const_8_2 --
-0.9000000000000000000000000000000000000000000000000000000000000000000000000000

-- !cast_float_to_decimal256_const_9_1 --
340200000055538043127555627967637430108.2770577350592535491936868262950128000

-- !cast_float_to_decimal256_const_9_2 --
340200000000000000000000000000000000000

-- !cast_float_to_decimal256_const_10_1 --
-340200000055538043127555627967637430108.2770577350592535491936868262950128000

-- !cast_float_to_decimal256_const_10_2 --
-340200000000000000000000000000000000000

-- !cast_float_to_decimal256_1 --
0 220
1 -220
2 99999996802856924654458622116917936128
3 -99999996802856924654458622116917936128
4 9999999848243207295117249609728
5 -9999999848243207295117249609728
6 1
7 -1
8 1
9 -1
10 340200000055538034029787422761016098815
11 -340200000055538034029787422761016098815

-- !cast_float_to_decimal256_2 --
0 219.77717590332030098495757448650589208640
1 -219.77717590332030098495757448650589208640
2 99999996802856929887877846998705567504.08319368450610608065324841755887360000
3 -99999996802856929887877846998705567504.08319368450610608065324841755887360000
4 9999999848243206484209837188033.94655282897607351708798609489962906025
5 -9999999848243206484209837188033.94655282897607351708798609489962906025
6 0.99999999999999997752612184630461283328
7 -0.99999999999999997752612184630461283328
8 0.97718000411987307697345873505444123648
9 -0.97718000411987307697345873505444123648

-- !cast_float_to_decimal256_3 --
0 219.777175903320326853463377056255521940905600575
1 -219.777175903320326853463377056255521940905600575
4 9999999848243207687231096732081.517441445022649282668861247346191519797952000
5 -9999999848243207687231096732081.517441445022649282668861247346191519797952000
6 0.999999999999999929648868823465823162872944495
7 -0.999999999999999929648868823465823162872944495
8 0.977180004119873082053229390165939041981944185
9 0.977180004119873082053229390165939041981944185

-- !cast_float_to_decimal256_4 --
6 0.8999999761581421686088389507648326470906229208913952822970533869391577408000
7 -0.8999999761581421686088389507648326470906229208913952822970533869391577408000

-- !cast_double_to_decimal256_const_1_1 --
219.77717999999997235813377081619481624640

-- !cast_double_to_decimal256_const_1_2 --
219.77718000000000000000000000000000000000

-- !cast_double_to_decimal256_const_2_1 --
-219.77717999999997235813377081619481624640

-- !cast_double_to_decimal256_const_2_2 --
-219.77718000000000000000000000000000000000

-- !cast_double_to_decimal256_const_3_1 --
100000000000000004689750417003264391641.02534195992280967396974317317789248000

-- !cast_double_to_decimal256_const_3_2 --
100000000000000004689750417003264391641.02534195992280967396974317317789248000

-- !cast_double_to_decimal256_const_4_1 --
-100000000000000004689750417003264391641.02534195992280967396974317317789248000

-- !cast_double_to_decimal256_const_4_2 --
-100000000000000004689750417003264391641.02534195992280967396974317317789248000

-- !cast_double_to_decimal256_const_5_1 --
9999999999999998862036997441336.163883477299650110418370898579593514004704000

-- !cast_double_to_decimal256_const_5_2 --
9999999999999998862036997441336.163883477299650110418370898579593514004704000

-- !cast_double_to_decimal256_const_6_1 --
-9999999999999998862036997441336.163883477299650110418370898579593514004704000

-- !cast_double_to_decimal256_const_6_2 --
-9999999999999998862036997441336.163883477299650110418370898579593514004704000

-- !cast_double_to_decimal256_const_7_1 --
0.9899999999999999532261225613108815109698872817420877851753935634338416320000

-- !cast_double_to_decimal256_const_7_2 --
0.9900000000000000000000000000000000000000000000000000000000000000000000000000

-- !cast_double_to_decimal256_const_8_1 --
-0.9899999999999999532261225613108815109698872817420877851753935634338416320000

-- !cast_double_to_decimal256_const_8_2 --
-0.9900000000000000000000000000000000000000000000000000000000000000000000000000

-- !cast_double_to_decimal256_const_9_1 --
9898999999999999715726652763315471902829864349140987332177312614365076736000

-- !cast_double_to_decimal256_const_9_2 --
9899000000000000000000000000000000000000000000000000000000000000000000000000

-- !cast_double_to_decimal256_const_10_1 --
-9898999999999999715726652763315471902829864349140987332177312614365076736000

-- !cast_double_to_decimal256_const_10_2 --
-9899000000000000000000000000000000000000000000000000000000000000000000000000

-- !cast_double_to_decimal256_1 --
0 220
1 -220
2 99999999999999997752612184630461283328
3 -99999999999999997752612184630461283328
4 9999999999999999635903949692894
5 -9999999999999999635903949692894
6 1
7 -1
8 1
9 -1
10 340199999999999981876691358478025097215
11 -340199999999999981876691358478025097215
12 9898999999999999715726652763315471902829864349140987332177312614365076736000
13 9898999999999999715726652763315471902829864349140987332177312614365076736000

-- !cast_double_to_decimal256_2 --
0 219.77717999999997235813377081619481624640
1 -219.77717999999997235813377081619481624640
2 100000000000000004689750417003264391641.02534195992280967396974317317789248000
3 -100000000000000004689750417003264391641.02534195992280967396974317317789248000
4 9999999999999998808068634474203.85625841180974495274405359169525688375
5 -9999999999999998808068634474203.85625841180974495274405359169525688375
6 0.99999999999999997752612184630461283328
7 -0.99999999999999997752612184630461283328
8 0.97718000000000008819311215209494821888
9 -0.97718000000000008819311215209494821888

-- !cast_double_to_decimal256_3 --
0 219.777179999999995742093914318825305628536153675
1 -219.777179999999995742093914318825305628536153675
4 9999999999999998862036997441336.163883477299650110418370898579593514004704000
5 -9999999999999998862036997441336.163883477299650110418370898579593514004704000
6 0.999999999999999929648868823465823162872944495
7 -0.999999999999999929648868823465823162872944495
8 0.977180000000000074669932294976532285011775865
9 0.977180000000000074669932294976532285011775865

-- !cast_double_to_decimal256_4 --
6 0.9000000000000000743465146382091850303817327685569425389956955830346767232000
7 -0.9000000000000000743465146382091850303817327685569425389956955830346767232000

Loading
Loading