apache / apache/datafusion

UNION ALL with ORDER BY and duplicate projection fails sanity checker

Open
#16,171 1 comment 0 reactions 1 assignee Claimed by @chenkovsky View on GitHub
bug
Dominant language
Rust
Stars
9.3k
Forks
2.4k
Avg merge
3d 7h
Merged PRs (30d)
344

Description

### Describe the bug

A query that should run successfully fails during planning.

### To Reproduce

This can easily be reproduced in `datafusion-cli` by running:
```sql
CREATE TABLE t (x INTEGER);
(SELECT x, x as __x FROM t) UNION ALL (SELECT x, x as __x FROM t) ORDER BY __x;
```

The query seems perfectly valid, but it currently errors during planning with:
```
SortExec: expr=[__x@1 ASC NULLS LAST], preserve_partitioning=[false]
ProjectionExec: expr=[x@0 as x, x@0 as __x]
DataSourceExec: partitions=1, partition_sizes=[0]

SortExec: expr=[__x@1 ASC NULLS LAST], preserve_partitioning=[false]
ProjectionExec: expr=[x@0 as x, x@0 as __x]
DataSourceExec: partitions=1, partition_sizes=[0]

Error: Execution plan does not satisfy required ordering: [__x@1 ASC NULLS LAST].
Child-0 order: [[x@0 ASC NULLS LAST]]
```

### Expected behavior

The query runs without errors.

### Additional context

We encountered this when attempting to inject projections into user queries.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.