apache / apache/datafusion

Integer interval inference can incorrectly remove ORDER BY

Open
#25,232 1 comment 0 reactions 1 assignee Claimed by @haohuaijin 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

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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.