You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ORDER BY can return wrong results across individually sorted Parquet row groups #27162
Reading a single Parquet file with individually sorted row groups can return unsorted rows for ORDER BY. Adding LIMIT can also select the wrong rows because DataFusion treats the whole file as sorted and removes a required sort.
To Reproduce
Reproduced with Python 3.11, datafusion==55.0.0, and pyarrow==24.0.0.
The file below has two row groups: [1, 4] and [2, 3]. Each group correctly declares ascending order, but the combined rows are not sorted.
Save as repro.py and run:
uv run --no-project --python 3.11 --with datafusion==55.0.0 --with pyarrow==24.0.0 repro.py
importtempfilefrompathlibimportPathimportpyarrowaspaimportpyarrow.parquetaspqfromdatafusionimportSessionConfig, SessionContextwithtempfile.TemporaryDirectory() asdirectory:
path=Path(directory) /"data.parquet"pq.write_table(
pa.table({"x": pa.array([1, 4, 2, 3], type=pa.int64())}),
path,
row_group_size=2,
sorting_columns=[pq.SortingColumn(0, nulls_first=True)],
)
ctx=SessionContext(SessionConfig().with_target_partitions(1))
ctx.register_parquet("t", str(path))
forsuffixin ["", " LIMIT 2"]:
result=ctx.sql("SELECT x FROM t ORDER BY x ASC NULLS FIRST"+suffix)
print([row["x"] forbatchinresult.collect() forrowinbatch.to_pylist()])
Actual results, without and with LIMIT 2:
[1, 4, 2, 3]
[1, 4]
Expected behavior
[1, 2, 3, 4]
[1, 2]
A row group's sort declaration does not establish ordering across row-group boundaries.
Additional context
The current ordering_from_parquet_metadata uses the first row group's sorting_columns to declare an ordering for the file, without checking the other groups or their boundaries.
Related to lakehq/sail#2730. This is separate from #26278, which covered NULL bounds between files.
A fix and regression tests are ready locally.
Willingness to contribute
I plan to contribute a fix for this bug independently.
Activity