apache / apache/datafusion

Overflow happened on: -2147483648 % -1

Open
#14,771 7 comments 1 reaction 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
9.3k
Forks
2.4k
Avg merge
3d 7h
Merged PRs (30d)
344

Description

### Describe the bug

Initially reported in Comet https://github.com/apache/datafusion-comet/issues/1412

```
$datafusion-cli
DataFusion CLI v44.0.0
> create table t1(c1 int, c2 int);
0 row(s) fetched.
Elapsed 0.016 seconds.

> insert into t1 values(-2147483648, -1);
+-------+
| count |
+-------+
| 1 |
+-------+
1 row(s) fetched.
Elapsed 0.027 seconds.

> select c1 % c2 from t1;
Arrow error: Arithmetic overflow: Overflow happened on: -2147483648 % -1
```

It looks Postgres returns 0 for this case

### To Reproduce

_No response_

### Expected behavior

`-2147483648 % -1` should return `0`

### Additional context

rust -2147483648 + -1 also overflows
```
>> let r = -2147483648 + -1;
[arithmetic_overflow] Error: this arithmetic operation will overflow
╭─[command:1:1]

1 │ let r = -2147483648 + -1;
│ ────────┬───────
│ ╰───────── attempt to compute `i32::MIN + -1_i32`, which would overflow
───╯
```
But DataFusion is allowing the overflow
```
> select c1 + c2 from t1;
+---------------+
| t1.c1 + t1.c2 |
+---------------+
| 2147483647 |
+---------------+
1 row(s) fetched.
Elapsed 0.010 seconds.
```

So we should be able to do similar handling for `-2147483648 % -1`?

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the query in datafusion-cli using the table and values shown in the issue, then trace how integer remainder overflow is handled. The work is done when `-2147483648 % -1` returns `0` without an arithmetic-overflow error, consistently with the expected behavior described.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.