tustvold opened a new issue, #6828:
URL: https://github.com/apache/arrow-datafusion/issues/6828

   ### Describe the bug
   
   Currently when performing decimal division 
`coercion_decimal_mathematics_type` as called by `BinaryExpr::evaluate` will 
coerce both inputs to the wider precision type. The division kernel will then 
scale the left hand side by the output scale. 
   
   To see why this is an issue consider
   
   ```
   1 DECIMAL(38, 20) / 5 DECIMAL(38, 0)
   ```
   
   This computation shouldn't overflow as it can just perform `10e20 / 5`
   
   The issue is that the coercion logic will scale the right hand side to 
`5e20` requiring it to also scale the left hand side to 10e40 which will 
overflow
   
   ### To Reproduce
   
   ```
   ❯ create table foo (a DECIMAL(38, 20), b DECIMAL(38, 0));
   0 rows in set. Query took 0.001 seconds.
   ❯ insert into foo VALUES (1, 1000000000000);
   +-------+
   | count |
   +-------+
   | 1     |
   +-------+
   1 row in set. Query took 0.003 seconds.
   ❯ select a / b from foo;
   Decimal128(38, 20) vs Decimal128(38, 20)
   +------------------------------------------+
   | foo.a / foo.b                            |
   +------------------------------------------+
   | 0.00000000000000000000000000000000743222 |
   +------------------------------------------+
   1 row in set. Query took 0.004 seconds.
   ```
   
   ### Expected behavior
   
   _No response_
   
   ### Additional context
   
   https://github.com/apache/arrow-datafusion/issues/6794


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to