Skip to content

chore(query): round the results for decimal division #14109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 9 additions & 8 deletions src/query/functions/src/scalars/decimal/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,23 @@ macro_rules! binary_decimal {
let scale_a = $left.scale();
let scale_b = $right.scale();

let (scale_mul, scale_div) = if scale_b + $size.scale > scale_a {
(scale_b + $size.scale - scale_a, 0)
} else {
(0, scale_b + $size.scale - scale_a)
};

// Note: the result scale is larger the left's scale
let scale_mul = scale_b + $size.scale - scale_a;
let multiplier = T::e(scale_mul as u32);
let div = T::e(scale_div as u32);

let func = |a: T, b: T, result: &mut Vec<T>, ctx: &mut EvalContext| {
if std::intrinsics::unlikely(b == zero) {
ctx.set_error(result.len(), "divided by zero");
result.push(one);
} else if a.is_negative() == b.is_negative() {
result.push((a * multiplier + b / 2).div(b));
} else {
result.push((a * multiplier).div(b) / div);
result.push((a * multiplier - b / 2).div(b));
}
// 5 2 --> 3
// 5, -2 ---> -3
// -5, 2 --> -3
// -5, -2 --> 3
};

vectorize_with_builder_2_arg::<DecimalType<T>, DecimalType<T>, DecimalType<T>>(func)(
Expand Down
18 changes: 9 additions & 9 deletions src/query/functions/tests/it/scalars/testdata/arithmetic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ evaluation:
| Type | UInt32 | Decimal(10, 1) | Decimal(17, 6) |
| Domain | {10..=30} | {3.1..=188.8} | Unknown |
| Row 0 | 10 | 3.1 | 3.225806 |
| Row 1 | 20 | 33.5 | 0.597014 |
| Row 1 | 20 | 33.5 | 0.597015 |
| Row 2 | 30 | 188.8 | 0.158898 |
+--------+-----------+----------------+----------------+
evaluation (internal):
Expand All @@ -1151,7 +1151,7 @@ evaluation (internal):
+--------+--------------------------------------------+
| c | UInt32([10, 20, 30]) |
| e | Decimal128([3.1, 33.5, 188.8]) |
| Output | Decimal128([3.225806, 0.597014, 0.158898]) |
| Output | Decimal128([3.225806, 0.597015, 0.158898]) |
+--------+--------------------------------------------+


Expand Down Expand Up @@ -1187,17 +1187,17 @@ evaluation:
+--------+------------------+----------------+---------------------+
| Type | UInt8 NULL | Decimal(10, 1) | Decimal(10, 6) NULL |
| Domain | {0..=3} ∪ {NULL} | {3.1..=188.8} | Unknown |
| Row 0 | 1 | 3.1 | 0.322580 |
| Row 0 | 1 | 3.1 | 0.322581 |
| Row 1 | NULL | 33.5 | NULL |
| Row 2 | 3 | 188.8 | 0.015889 |
| Row 2 | 3 | 188.8 | 0.015890 |
+--------+------------------+----------------+---------------------+
evaluation (internal):
+--------+-----------------------------------------------------------------------------------------------+
| Column | Data |
+--------+-----------------------------------------------------------------------------------------------+
| d2 | NullableColumn { column: UInt8([1, 0, 3]), validity: [0b_____101] } |
| e | Decimal128([3.1, 33.5, 188.8]) |
| Output | NullableColumn { column: Decimal128([0.322580, 0.000000, 0.015889]), validity: [0b_____101] } |
| Output | NullableColumn { column: Decimal128([0.322581, 0.000000, 0.015890]), validity: [0b_____101] } |
+--------+-----------------------------------------------------------------------------------------------+


Expand All @@ -1212,15 +1212,15 @@ evaluation:
| Domain | {0..=3} ∪ {NULL} | {0.50..=12.34} | Unknown |
| Row 0 | 1 | 0.50 | 2.000000 |
| Row 1 | NULL | 0.92 | NULL |
| Row 2 | 3 | 12.34 | 0.243111 |
| Row 2 | 3 | 12.34 | 0.243112 |
+--------+------------------+----------------+---------------------+
evaluation (internal):
+--------+-----------------------------------------------------------------------------------------------+
| Column | Data |
+--------+-----------------------------------------------------------------------------------------------+
| d2 | NullableColumn { column: UInt8([1, 0, 3]), validity: [0b_____101] } |
| f | Decimal256([0.50, 0.92, 12.34]) |
| Output | NullableColumn { column: Decimal256([2.000000, 0.000000, 0.243111]), validity: [0b_____101] } |
| Output | NullableColumn { column: Decimal256([2.000000, 0.000000, 0.243112]), validity: [0b_____101] } |
+--------+-----------------------------------------------------------------------------------------------+


Expand All @@ -1234,7 +1234,7 @@ evaluation:
| Type | Decimal(10, 1) | Decimal(76, 2) | Decimal(39, 7) |
| Domain | {3.1..=188.8} | {0.50..=12.34} | Unknown |
| Row 0 | 3.1 | 0.50 | 6.2000000 |
| Row 1 | 33.5 | 0.92 | 36.4130434 |
| Row 1 | 33.5 | 0.92 | 36.4130435 |
| Row 2 | 188.8 | 12.34 | 15.2998379 |
+--------+----------------+----------------+----------------+
evaluation (internal):
Expand All @@ -1243,7 +1243,7 @@ evaluation (internal):
+--------+-------------------------------------------------+
| e | Decimal128([3.1, 33.5, 188.8]) |
| f | Decimal256([0.50, 0.92, 12.34]) |
| Output | Decimal256([6.2000000, 36.4130434, 15.2998379]) |
| Output | Decimal256([6.2000000, 36.4130435, 15.2998379]) |
+--------+-------------------------------------------------+


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,14 @@ SELECT CAST(4.56 AS DECIMAL(46, 2)) / CAST(1.23 AS DECIMAL(46, 2)) AS result;
3.70731707

query I
SELECT CAST(987654321.34 AS DECIMAL(20, 2)) / CAST(123456789.12 AS DECIMAL(20, 2)) AS result;
SELECT CAST(987654321.34 AS DECIMAL(20, 2)) / CAST(123456789.12 AS DECIMAL(20, 2)), CAST(-987654321.34 AS DECIMAL(20, 2)) / CAST(123456789.12 AS DECIMAL(20, 2)), CAST(-987654321.34 AS DECIMAL(20, 2)) / CAST(-123456789.12 AS DECIMAL(20, 2)), CAST(987654321.34 AS DECIMAL(20, 2)) / CAST(-123456789.12 AS DECIMAL(20, 2));
----
8.00000006
8.00000007 -8.00000007 8.00000007 -8.00000007

query I
SELECT CAST(987654321.34 AS DECIMAL(52, 2)) / CAST(123456789.12 AS DECIMAL(52, 2)) AS result;
----
8.00000006
8.00000007

query I
SELECT CAST(987654321.34 AS DECIMAL(20, 2)) / CAST(1.23 AS DECIMAL(6, 2)) AS result;
Expand Down