Integer interval inference can incorrectly remove ORDER BY
- Dominant language
- Rust
- Stars
- 9.3k
- Forks
- 2.4k
- Avg merge
- 3d 7h
- Merged PRs (30d)
- 344
Description
### Describe the bug
Filter interval inference uses mathematical inverse operations that are invalid for wrapping multiplication and truncating integer division. It can incorrectly mark a column as constant and remove a required sort.
### To Reproduce
Reproduced on main at `bb21f51013`, without the statistics-ordering feature.
```sql
SET datafusion.execution.target_partitions = 1;
CREATE TABLE wrap_test(a INT) AS VALUES (-2147483647), (1);
SELECT a FROM wrap_test
WHERE a * 2::INT = 2::INT ORDER BY a DESC;
-- Expected: 1, -2147483647
-- Actual: -2147483647, 1
CREATE TABLE division_test(a INT) AS VALUES (2), (3);
SELECT a FROM division_test
WHERE a / 2::INT = 1::INT ORDER BY a DESC;
-- Expected: 3, 2
-- Actual: 2, 3
```
### Expected behavior
Inferred intervals must include every value that can satisfy the predicate under runtime integer semantics. These columns are not constant, so their sorts must be retained.
### Additional context
find this when working on #25220
Contributor guide
Assessment
This issue has not been assessed yet.