Skip to content

ORDER BY can return wrong results across individually sorted Parquet row groups #27162

Open

Description

@rank-p

Describe the bug

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
import tempfile
from pathlib import Path

import pyarrow as pa
import pyarrow.parquet as pq
from datafusion import SessionConfig, SessionContext

with tempfile.TemporaryDirectory() as directory:
    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))
    for suffix in ["", " LIMIT 2"]:
        result = ctx.sql("SELECT x FROM t ORDER BY x ASC NULLS FIRST" + suffix)
        print([row["x"] for batch in result.collect() for row in batch.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

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions