Skip to content

Wrong results: ORDER BY <double>, ... LIMIT n drops -0.0 rows when 0.0 arrives in an earlier batch (TopK dynamic filter) #26618

Description

@alpaylan

Describe the bug

ORDER BY x, y LIMIT 2 can leave out a row that belongs in the top 2 when x is a DOUBLE column holding both
0.0 and -0.0. It happens when the rows come in more than one batch. Default settings.

The TopK heap orders -0.0 before 0.0, the same way the full sort does. After the first batch, TopK builds a
dynamic filter such as x < 0.0 OR (x = 0.0 AND y < 1). Since #22835, comparisons treat -0.0 = 0.0, so the
filter throws away -0.0 rows that the heap would have kept.

To Reproduce

Run in datafusion-cli. The two INSERTs give the table two batches.

CREATE TABLE t (x DOUBLE, y INT);
INSERT INTO t VALUES (0.0, 1), (-0.0, 7);
INSERT INTO t VALUES (-0.0, 5);
SELECT x, y FROM t ORDER BY x, y;
SELECT x, y FROM t ORDER BY x, y LIMIT 2;

Actual output:

+------+---+
| x    | y |
+------+---+
| -0.0 | 5 |
| -0.0 | 7 |
| 0.0  | 1 |
+------+---+
+------+---+
| x    | y |
+------+---+
| -0.0 | 7 |
| 0.0  | 1 |
+------+---+

Expected behavior

The LIMIT 2 result should be the first two rows of the full ORDER BY result:

SELECT x, y FROM t ORDER BY x, y LIMIT 2;
# DuckDB 1.5.6:  (0.0, 1), (0.0, 5)   -- DuckDB stores -0.0 as 0.0
# DataFusion:    (-0.0, 7), (0.0, 1)
# expected:      (-0.0, 5), (-0.0, 7)

Under any ordering, (-0.0, 5) comes before (-0.0, 7): the x values are identical and 5 < 7. So the result
cannot contain (-0.0, 7) and leave out (-0.0, 5). If all three rows are inserted in one INSERT (one batch),
the query returns (-0.0, 5), (-0.0, 7).

A single-key query shows the same thing. After INSERT INTO t VALUES (0.0, 1); INSERT INTO t VALUES (-0.0, 2);,
SELECT x FROM t ORDER BY x LIMIT 1 returns 0.0, but SELECT x FROM t ORDER BY x lists -0.0 first.

Additional context

Found with an automated differential/metamorphic testing tool; reproduced on current main.

Willingness to contribute

I would like to contribute a fix for this bug, but request guidance from the DataFusion community

Activity

  1. added theissue type on Oct 23, 2026
  2. LiuYC3213687107 commented on Oct 24, 2026

    @LiuYC3213687107

    I'd like to work on this.

    Since the TopK dynamic filter is only a pruning hint, my plan is to make it conservative when the threshold of a floating-point sort key is ±0.0: use a non-strict bound for that column (e.g. x <= 0.0 instead of x < 0.0 OR (x = 0.0 AND ...)), so -0.0/0.0 rows are never filtered out and the heap's total ordering decides. I'll add a sqllogictest reproducing the multi-batch case from this issue.

  3. added a commit that references this issue on Oct 24, 2026
    4e7b69e
  4. added
    Prolific Authorauthor opened PRs or issues in more than 20 repositories in the last 30 days
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Prolific Authorauthor opened PRs or issues in more than 20 repositories in the last 30 daysbugSomething isn't working

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions