-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
Describe the bug
When casting from integer to decimal, if casted decimal exceeds the precision, e.g. cast(1234567 as decimal(7,3)), DataFusion doesn't handle it so the returned decimal is invalid. Whilst in Spark, the cast expression will return a null value indicating it's overflowing.
This is due to current behavior of cast kernel in arrow-rs. Although it checks if division or multiplication overflows in such cast operation, it doesn't check precision overflow. This is somehow related to decimal operation convention in arrow-rs, that is let user handle invalid values in decimal arrays. But for cast kernel, because it provides CastOptions which defines null behavior for invalid case, the behavior of not checking precision overflow becomes a bit inconsistent.
Proposed a fix at the upstream: apache/arrow-rs#3996
We can add some tests in DataFusion after new arrow-rs is released.
To Reproduce
In datafusion-cli, run the following query:
❯ select cast(1234567 as decimal(7,3));
+----------------+
| Int64(1234567) |
+----------------+
| 1234.567 |
+----------------+
1 row in set. Query took 0.016 seconds.
Expected behavior
cast(1234567 as decimal(7,3)) should return null.
Additional context
No response