Numeric overflow should result in query error
Describe the bug
In programming languages it's rather typical that numeric overflows are not a runtime error.
In SQL, it's rather typical that numeric overflows are identified during query execution and are treated as an error.
DataFusion is a SQL engine and therefore should report overflows as query errors. Continuing execution after overflows happened is a correctness issue (query results a wrong result).
To Reproduce
cargo run --bin datafusion-cli -- --command "SELECT 10000000000 * 10000000000;"
datafusion main$ cargo run --bin datafusion-cli -- --command "SELECT 10000000000 * 10000000000;"
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.17s
Running `target/debug/datafusion-cli --command 'SELECT 10000000000 * 10000000000;'`
DataFusion CLI v50.0.0
+-----------------------------------------+
| Int64(10000000000) * Int64(10000000000) |
+-----------------------------------------+
| 7766279631452241920 |
+-----------------------------------------+
Expected behavior
PostgreSQL
postgres=# SELECT 10000000000 * 10000000000;
ERROR: bigint out of range
Trino
trino> SELECT 10000000000 * 10000000000;
Query 20250912_093345_00000_kgiyz failed: bigint multiplication overflow: 10000000000 * 10000000000
Snowflake
SELECT 10000000000 * 10000000000 * 10000000000 * 10000000000;
-- Number out of representable range: type FIXED[SB16](38,0){not null}, value 1e+40
Additional context
No response
This is not entirely true for some systems including Spark before ANSI mode was enabled by default. I've been meaning to submit a PR to add an 'ANSI' flag to the config that when disabled (default would be enabled) would allow these type of errors (numeric overflow, string -> date parsing, etc) to result in a NULL value rather than an error.
I can see how it may be desirable for some use cases to keep numeric overflow behavior. Having a flag for transition period sounds useful, but may also complicate implementation a lot.
in the long term, I can hardly see how someone would want silent overflow behavior in their queries. In regular programming it might be useful feature in powerful hands. in SQL, the query author has little control of final plan, so overflow may happen in a different place, even if somewhat anticipated.
For the primitives such as arithmetics currently implemented in Arrow, do you think Arrow wants to have numeric overflow behavior and use checked arithmetics only conditionally? cc @alamb @tustvold
I did some digging into how other databases handle this and wanted to share what I found.
Other databases consistently treat overflow as an error:
- MySQL (strict mode): "out of range" errors - this is SQL standard behavior
- Oracle: "Numeric Overflow" exceptions
- SQL Server: "Arithmetic overflow error"
- PostgreSQL: Already mentioned in the issue
So yeah, DataFusion is definitely the outlier here by silently wrapping.
I tested a few more cases locally: SELECT 9223372036854775807 + 1; -- Returns: -9223372036854775808 (yikes!) SELECT -9223372036854775808 - 1; -- Returns: 9223372036854775807 (also wrong)
The good news: I poked around the codebase and found that BinaryExpr already has a fail_on_overflow field! It's just hardcoded to false right now. The plumbing is basically there.
Also, Arrow already has all the checked arithmetic we need (add_checked, multiply_checked, etc.). So this might not be as big a lift as it first seemed.
Thinking about implementation:
- Could start with just multiplication since that's the most obvious case
- Maybe add a session config flag for the transition period that @Omega359 mentioned
- Then gradually expand to other operations
Happy to take a crack at this if no one else is already working on it. Seems like a good mix of correctness improvement + leveraging existing infrastructure.
awesome write up @EeshanBembi , thank you! I don't think anyone is working on this yet, so you can go ahead
take
This config should probably also be used in aggregates like sum that can also overflow.