NoahKusaba opened a new issue, #2942:
URL: https://github.com/apache/datafusion-ballista/issues/2942
**Describe the bug**
With the adaptive planner (the default), a `UNION ALL` with an `ORDER BY`
fails to plan when each branch counts as sorted only because its sort key is
constant in it. Examples are a branch filtered to `id = 1`, or a global
aggregate's one row. The job fails with:
```
SanityCheckPlan: order and distribution requirements stopped holding after
physical optimizer rule 'DistributedExchangeRule'
```
DataFusion doesn't sort such branches, because a constant column is already
sorted. It merges them with a `SortPreservingMergeExec` directly over the
`UnionExec`. `DistributedExchangeRule` then puts an `ExchangeExec` with
`partitioning=None` between the merge and the branches, and the exchange
reports no ordering (`Child-0 order: []`):
```
SortPreservingMergeExec: [id@0 ASC NULLS LAST]
ExchangeExec: partitioning=None, plan_id=0, stage_id=pending,
stage_resolved=false
UnionExec
FilterExec: id@0 = 1
DataSourceExec: ... projection=[id], file_type=parquet,
predicate=id@0 = 1, ...
FilterExec: id@0 = 2
DataSourceExec: ... projection=[id], file_type=parquet,
predicate=id@0 = 2, ...
```
`ExchangeExec::new_with_details`
(`ballista/scheduler/src/state/aqe/execution_plan/exchange.rs`) clears its
input's per-partition constants, because a constant within each partition stops
holding once rows are redistributed. With `partitioning=None`, though, the
exchange keeps its input's partitions as they are. `maintains_input_order`
already returns `true` for that case. Clearing the constants there drops the
only ordering these branches have, so `SortPreservingMergeExec`'s requirement
no longer holds.
**To Reproduce**
Standalone Ballista with default settings, and `test` registered as
`alltypes_plain.parquet`:
| Query | Adaptive planner | Static planner |
|---|---|---|
| `SELECT id FROM test WHERE id = 1 UNION ALL SELECT id FROM test WHERE id =
2 ORDER BY id` | `SanityCheckPlan` error | 2 rows |
| `SELECT * FROM (SELECT count(*) AS n FROM test UNION ALL SELECT count(*)
AS n FROM test WHERE id > 1) ORDER BY n` | `SanityCheckPlan` error | 2 rows |
In the second query, the exchange sits over the aggregate branch alone: the
unfiltered `count(*)` is answered from statistics as `8 as n` over a
`PlaceholderRowExec`.
The error doesn't depend on a stage being split between tasks. It's the same
with `ballista.scheduler.max_partitions_per_task = 1`, and on `9c0e89faf`, the
commit before #2914.
**Expected behavior**
The rows DataFusion returns: `1`, `2` for the first query and `6`, `8` for
the second.
**Additional context**
- Proposed fix: clear per-partition constants only when the exchange moves
rows between partitions, that is, when its `partitioning` is `Some` or it
broadcasts. The comment above the clearing gives that case as its reason: a
hash shuffle of a `UNION ALL` branch that projects a literal. One thing to
check is whether AQE's partition coalescing can merge the partitions of a
`partitioning=None` exchange; if it can, the constants and the ordering would
both stop holding there.
- The clearing came with #2315.
- Found while testing `UNION ALL` of Iceberg scans in ballista-iceberg, but
it doesn't involve Iceberg: the repro above uses Parquet.
- A test could add these queries to `ballista/client/tests/split_union.rs`,
which already compares unions against DataFusion under both planners.
--
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]