Skip to content

Wrong results: ORDER BY returns unsorted rows for a table with a PRIMARY KEY and WITH ORDER #27596

Closed

Description

@asolimando

Describe the bug

A query on a table with a PRIMARY KEY and a declared sort order (WITH ORDER) returns rows in the wrong order: the plan has no SortExec, although the input is not sorted on the requested columns. This happens when the scan reads only some of the table columns.

To Reproduce

SET datafusion.execution.target_partitions = 1;

COPY (VALUES (0, 3, 0, 10, 1), (0, 1, 0, 11, 1), (0, 2, 0, 12, 1), (0, 9, 0, 13, 2), (0, 5, 0, 14, 2))
TO 'pk_dropped_columns.csv' STORED AS CSV OPTIONS ('format.has_header' 'false');

CREATE EXTERNAL TABLE pk_dropped_columns (a0 INT, a INT, b INT, c INT, d INT, PRIMARY KEY (c))
STORED AS CSV WITH ORDER (d ASC)
OPTIONS ('format.has_header' 'false')
LOCATION 'pk_dropped_columns.csv';

SELECT a, b, c, d FROM pk_dropped_columns ORDER BY d, a;

Observed (a is not sorted within each d value):

+---+---+----+---+
| a | b | c  | d |
+---+---+----+---+
| 3 | 0 | 10 | 1 |
| 1 | 0 | 11 | 1 |
| 2 | 0 | 12 | 1 |
| 9 | 0 | 13 | 2 |
| 5 | 0 | 14 | 2 |
+---+---+----+---+

The physical plan has no SortExec, only the scan:

DataSourceExec: ..., projection=[a, b, c, d], output_ordering=[d@3 ASC NULLS LAST], ...

Expected behavior

Rows sorted by d, then a (a is 1, 2, 3 for d = 1, then 5, 9 for d = 2), with SortExec: expr=[d@3 ASC NULLS LAST, a@0 ASC NULLS LAST] above the scan.

Without PRIMARY KEY (c), the result is correct and the SortExec stays.

Additional context

Reproduced on main (c6c1cc31d); 55.2.0 and branch-56 have the same code. From SQL, the bug needs CREATE EXTERNAL TABLE with both a key and WITH ORDER; any TableProvider that reports constraints and an output ordering is also affected.

Willingness to contribute

I plan to contribute a fix for this bug independently

Activity

  1. asolimando commented on Nov 19, 2026

    @asolimando
    MemberAuthor

    take

  2. added a commit that references this issue on Nov 22, 2026
    77611de
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions