Skip to content
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

fix: find the right wider decimal datatype for comparison operation #2241

Merged
merged 5 commits into from
Apr 16, 2022

Conversation

liukun4515
Copy link
Contributor

Which issue does this PR close?

Closes #2232

For decimal with the comparison operation, we use the error algorithm to calculate the precision/scale.

Before: max(p1,p2),max(s1,s2)

Now: max(38,max(s1, s2) + max(p1-s1, p2-s2)), max(s1, s2, 38)

Rationale for this change

What changes are included in this PR?

schema

CREATE EXTERNAL TABLE food (a DECIMAL(10,6), b double, c boolean) STORED AS CSV LOCATION 'aggregate_simple.csv';

Before this fix, we got the plan #2200 (comment)

After this fix, we got the right converted type in the filter expr

❯ explain select * from food where a >= CAST(0.00002 as Decimal(10,8));
+---------------+----------------------------------------------------------------------------------------------------------+
| plan_type     | plan                                                                                                     |
+---------------+----------------------------------------------------------------------------------------------------------+
| logical_plan  | Projection: #food.a, #food.b, #food.c                                                                    |
|               |   Filter: #food.a >= Decimal128(Some(2000),10,8)                                                         |
|               |     TableScan: food projection=Some([0, 1, 2]), partial_filters=[#food.a >= Decimal128(Some(2000),10,8)] |
| physical_plan | ProjectionExec: expr=[a@0 as a, b@1 as b, c@2 as c]                                                      |
|               |   CoalesceBatchesExec: target_batch_size=4096                                                            |
|               |     FilterExec: CAST(a@0 AS Decimal(12, 8)) >= CAST(Some(2000),10,8 AS Decimal(12, 8))                   |
|               |       RepartitionExec: partitioning=RoundRobinBatch(16)                                                  |
|               |         CsvExec: files=[aggregate_simple.csv], has_header=false, limit=None, projection=[a, b, c]        |
|               |                                                                                                          |
+---------------+----------------------------------------------------------------------------------------------------------+

Are there any user-facing changes?

cc @alamb

@github-actions github-actions bot added the datafusion Changes in the datafusion crate label Apr 15, 2022
@liukun4515 liukun4515 force-pushed the fix_bug_decimaltype_comparison branch from 3d2b41c to acc768f Compare April 15, 2022 06:29
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @liukun4515

(DataType::Decimal(p1, s1), DataType::Decimal(p2, s2)) => {
// max(s1, s2) + max(p1-s1, p2-s2), max(s1, s2)
let s = *s1.max(s2);
let range = (p1 - s1).max(p2 - s2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I see that the precision includes the scale 👍

DataType::Decimal(38, 10),
DataType::Decimal(25, 8),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inspired by your comments #2200 (comment)
Thanks

liukun4515 and others added 2 commits April 16, 2022 09:10
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
@alamb alamb merged commit 441cf09 into apache:master Apr 16, 2022
@liukun4515 liukun4515 deleted the fix_bug_decimaltype_comparison branch April 16, 2022 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
datafusion Changes in the datafusion crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error precision and scale for decimal coercion in logic comparison
2 participants