Skip to content

Commit ef4fe8e

Browse files
authored
fix(query): fix shrink_d256 for negative value (#15574)
1 parent 1dad976 commit ef4fe8e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/query/expression/src/utils/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,16 @@ fn shrink_d256(decimal: i256, size: DecimalSize) -> Scalar {
174174
let valid_bits = 256 - decimal.saturating_abs().leading_zeros();
175175
let log10_2 = std::f64::consts::LOG10_2;
176176
let mut precision = ((valid_bits as f64) * log10_2).floor() as u8;
177-
if decimal >= i256::from(10).pow(precision as u32) {
177+
178+
if decimal.saturating_abs() >= i256::from(10).pow(precision as u32) {
178179
precision += 1;
179180
}
180-
precision = precision.clamp(1, MAX_DECIMAL256_PRECISION).max(size.scale);
181+
182+
// adjust precision to the maximum scale of the decimal type
183+
if precision < size.scale {
184+
precision = size.scale;
185+
}
186+
precision = precision.clamp(1, MAX_DECIMAL256_PRECISION);
181187

182188
let size = DecimalSize { precision, ..size };
183189
let decimal_ty = DecimalDataType::from_size(size).unwrap();

tests/sqllogictests/suites/base/11_data_type/11_0006_data_type_decimal.test

+12
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ SELECT CAST(987654321.34 AS DECIMAL(76, 2)) / CAST(1.23 AS DECIMAL(76, 2)) AS re
262262
----
263263
802970992.95934959
264264

265+
265266
## negative
266267

267268
query I
@@ -371,6 +372,17 @@ DECIMAL(41, 3) DECIMAL(41, 3) DECIMAL(76, 5) DECIMAL(48, 8)
371372

372373
## compare
373374

375+
## issue: https://github.com/datafuselabs/databend/issues/15568
376+
query IIII
377+
SELECT (0.123)>(-1.1), (0.1234)>(-1.1), (0.1234)>(-12.1), (0.1234)>(-123.1)
378+
----
379+
1 1 1 1
380+
381+
query IIII
382+
SELECT (0.12)>(1.1), (0.1)>(-1.1), (0.12)>(-1.12), (0.12)>(-1);
383+
----
384+
0 1 1 1
385+
374386
query IIIII
375387
select a > b, a < b, a = b, a != b, a <= b, a >= b from (select 3::Decimal(13,2) a , 3.1::Decimal(8,2) b);
376388
----

0 commit comments

Comments
 (0)