kevalds51 commented on issue #27745:
URL: https://github.com/apache/datafusion/issues/27745#issuecomment-7180520914
Plus one on this!
> reproduced this through the `COPY` path with Python 3.12.8,
`datafusion==55.0.0`, and `pyarrow==26.0.0`:
>
> ```python
> import tempfile
> from pathlib import Path
>
> import pyarrow as pa
> import pyarrow.parquet as pq
> from datafusion import SessionConfig, SessionContext
>
> with tempfile.TemporaryDirectory() as d:
> output = Path(d) / "ordered.parquet"
> ctx = SessionContext(SessionConfig().with_target_partitions(1))
> payload_type = pa.struct([("a", pa.int64()), ("b", pa.int64())])
> batch = pa.record_batch(
> [
> pa.array(
> [{"a": 0, "b": 30}, {"a": 0, "b": 20}, {"a": 0, "b": 10}],
> type=payload_type,
> ),
> pa.array([30, 20, 10], type=pa.int64()),
> pa.array([1, 2, 3], type=pa.int64()),
> ],
> names=["payload", "b", "id"],
> )
> ctx.register_record_batches("input_data", [[batch]])
> ctx.sql(
> f"COPY (SELECT payload, b, id FROM input_data "
> f"ORDER BY id ASC NULLS FIRST) TO '{output}' STORED AS PARQUET"
> ).collect()
>
> pf = pq.ParquetFile(output)
> print([pf.schema.column(i).path for i in range(len(pf.schema))])
> print(pf.metadata.row_group(0).sorting_columns)
>
> read_ctx = SessionContext(SessionConfig().with_target_partitions(1))
> read_ctx.register_parquet("t", str(output))
> result = read_ctx.sql(
> "SELECT b, id FROM t ORDER BY b ASC NULLS FIRST"
> ).collect()
> print([(row["b"], row["id"]) for batch in result for row in
batch.to_pylist()])
> ```
>
> Output:
>
> ```text
> ['payload.a', 'payload.b', 'b', 'id']
> (SortingColumn(column_index=2, descending=False, nulls_first=True),)
> [(30, 1), (20, 2), (10, 3)]
> ```
>
> The COPY input is sorted by `id`, so the expected `sorting_columns` index
is `3` (`id`), but the file records index `2` (`b`) as ascending. A fresh
DataFusion session trusts that metadata: `EXPLAIN` shows `DataSourceExec` with
`output_ordering=[b@0 ASC]` and no `SortExec`. The `ORDER BY b ASC` query
therefore returns `(30, 1), (20, 2), (10, 3)` instead of `(10, 3), (20, 2),
(30, 1)`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]