Skip to content

DataFusion incorrectly handle precision overflow when casting from integer to decimal #5825

@viirya

Description

@viirya

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions