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

Decimal multiplied by Float produces incorrect results #4035

Closed
andygrove opened this issue Oct 30, 2022 · 3 comments · Fixed by #4038
Closed

Decimal multiplied by Float produces incorrect results #4035

andygrove opened this issue Oct 30, 2022 · 3 comments · Fixed by #4038
Labels
bug Something isn't working
Milestone

Comments

@andygrove
Copy link
Member

andygrove commented Oct 30, 2022

Describe the bug
Decimal multiplied by Float produces incorrect results

To Reproduce

This is fine:

❯ select cast(400420638.54 as decimal(12,2));
+-----------------------+
| Float64(400420638.54) |
+-----------------------+
| 400420638.54          |
+-----------------------+
1 row in set. Query took 0.000 seconds.

This is not fine:

❯ select cast(400420638.54 as decimal(12,2)) * 1.0;
+------------------------------------+
| Float64(400420638.54) * Float64(1) |
+------------------------------------+
| 10377.38413171004264709            |
+------------------------------------+
1 row in set. Query took 0.000 seconds.

More info:

❯ explain select cast(400420638.54 as decimal(12,2)) * 1.0;
+---------------+--------------------------------------------------------------------------------------------------+
| plan_type     | plan                                                                                             |
+---------------+--------------------------------------------------------------------------------------------------+
| logical_plan  | Projection: Decimal128(Some(1037738413171004264709),38,17) AS Float64(400420638.54) * Float64(1) |
|               |   EmptyRelation                                                                                  |
| physical_plan | ProjectionExec: expr=[Some(1037738413171004264709),38,17 as Float64(400420638.54) * Float64(1)]  |
|               |   EmptyExec: produce_one_row=true                                                                |
|               |                                                                                                  |
+---------------+--------------------------------------------------------------------------------------------------+
2 rows in set. Query took 0.001 seconds.

Expected behavior

Looks like type coercion needs to cast both sides to decimal.

❯ select cast(400420638.54 as decimal(12,2)) * cast(1.0 as decimal(12,2));
+------------------------------------+
| Float64(400420638.54) * Float64(1) |
+------------------------------------+
| 400420638.54                       |
+------------------------------------+
1 row in set. Query took 0.000 seconds.

Additional context
Add any other context about the problem here.

@andygrove andygrove added the bug Something isn't working label Oct 30, 2022
@andygrove andygrove added this to the 14.0.0 milestone Oct 30, 2022
@andygrove
Copy link
Member Author

andygrove commented Oct 30, 2022

Here is some more odd behavior:

❯ select cast(400420638.54 as decimal(32,15)) * cast(0.1 as decimal(32,15));
+--------------------------------------+
| Float64(400420638.54) * Float64(0.1) |
+--------------------------------------+
| 40042063.854000005133107             |
+--------------------------------------+
1 row in set. Query took 0.001 seconds.
❯ select cast(400420638.54 as decimal(32,16)) * cast(0.1 as decimal(32,16));
+--------------------------------------+
| Float64(400420638.54) * Float64(0.1) |
+--------------------------------------+
| -791820.1765126158512068             |
+--------------------------------------+
1 row in set. Query took 0.000 seconds.

Postgres for reference:

postgres=# select cast(400420638.54 as decimal(32,16)) * cast(0.1 as decimal(32,16));
                 ?column?                  
-------------------------------------------
 40042063.85400000000000000000000000000000
(1 row)

@andygrove
Copy link
Member Author

@liukun4515 are you familiar with this area of code?

@liukun4515
Copy link
Contributor

@liukun4515 are you familiar with this area of code?

are you familiar with this area of code?

@andygrove

sorry for the later reply.

Recently, I am busy for some internal business.
Will be back soon. 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants