Skip to content

Commit a5cc031

Browse files
authored
fix(physical-expr): Remove empty constants check when ordering is satisfied (#14829)
* fix: Remove empty constants check when ordering is satisfied * fix: Update failing UNION [ALL] BY NAME SLTs
1 parent 7299d0e commit a5cc031

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

datafusion/physical-expr/src/equivalence/properties.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2259,7 +2259,7 @@ impl UnionEquivalentOrderingBuilder {
22592259
) -> AddedOrdering {
22602260
if ordering.is_empty() {
22612261
AddedOrdering::Yes
2262-
} else if constants.is_empty() && properties.ordering_satisfy(ordering.as_ref()) {
2262+
} else if properties.ordering_satisfy(ordering.as_ref()) {
22632263
// If the ordering satisfies the target properties, no need to
22642264
// augment it with constants.
22652265
self.orderings.push(ordering);

datafusion/sqllogictest/test_files/union_by_name.slt

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,16 @@ NULL 1
9292
NULL 3
9393
5 NULL
9494

95-
# TODO: This should pass, but the sanity checker isn't allowing it.
96-
# Commenting out the ordering check in the sanity checker produces the correct result.
97-
query error
95+
query II
9896
(SELECT x FROM t1 UNION ALL SELECT x FROM t1) UNION ALL BY NAME SELECT 5 ORDER BY x;
9997
----
100-
DataFusion error: SanityCheckPlan
101-
caused by
102-
Error during planning: Plan: ["SortPreservingMergeExec: [x@1 ASC NULLS LAST]", " UnionExec", " SortExec: expr=[x@1 ASC NULLS LAST], preserve_partitioning=[true]", " ProjectionExec: expr=[NULL as Int64(5), x@0 as x]", " UnionExec", " DataSourceExec: partitions=1, partition_sizes=[1]", " DataSourceExec: partitions=1, partition_sizes=[1]", " ProjectionExec: expr=[5 as Int64(5), NULL as x]", " PlaceholderRowExec"] does not satisfy order requirements: [x@1 ASC NULLS LAST]. Child-0 order: []
103-
98+
NULL 1
99+
NULL 1
100+
NULL 3
101+
NULL 3
102+
NULL 3
103+
NULL 3
104+
5 NULL
104105

105106
query II
106107
(SELECT x FROM t1 UNION ALL SELECT y FROM t1) UNION BY NAME SELECT 5 ORDER BY x;
@@ -109,15 +110,16 @@ NULL 1
109110
NULL 3
110111
5 NULL
111112

112-
# TODO: This should pass, but the sanity checker isn't allowing it.
113-
# Commenting out the ordering check in the sanity checker produces the correct result.
114-
query error
113+
query II
115114
(SELECT x FROM t1 UNION ALL SELECT y FROM t1) UNION ALL BY NAME SELECT 5 ORDER BY x;
116115
----
117-
DataFusion error: SanityCheckPlan
118-
caused by
119-
Error during planning: Plan: ["SortPreservingMergeExec: [x@1 ASC NULLS LAST]", " UnionExec", " SortExec: expr=[x@1 ASC NULLS LAST], preserve_partitioning=[true]", " ProjectionExec: expr=[NULL as Int64(5), x@0 as x]", " UnionExec", " DataSourceExec: partitions=1, partition_sizes=[1]", " ProjectionExec: expr=[y@0 as x]", " DataSourceExec: partitions=1, partition_sizes=[1]", " ProjectionExec: expr=[5 as Int64(5), NULL as x]", " PlaceholderRowExec"] does not satisfy order requirements: [x@1 ASC NULLS LAST]. Child-0 order: []
120-
116+
NULL 1
117+
NULL 1
118+
NULL 3
119+
NULL 3
120+
NULL 3
121+
NULL 3
122+
5 NULL
121123

122124

123125
# Ambiguous name
@@ -158,15 +160,14 @@ NULL NULL 4
158160
NULL NULL 5
159161
NULL NULL 6
160162

161-
# TODO: This should pass, but the sanity checker isn't allowing it.
162-
# Commenting out the ordering check in the sanity checker produces the correct result.
163-
query error
163+
query III
164164
SELECT 1 UNION ALL BY NAME SELECT * FROM unnest(range(2, 100)) UNION ALL BY NAME SELECT 999 ORDER BY 3, 1 LIMIT 5;
165165
----
166-
DataFusion error: SanityCheckPlan
167-
caused by
168-
Error during planning: Plan: ["SortPreservingMergeExec: [UNNEST(range(Int64(2),Int64(100)))@2 ASC NULLS LAST, Int64(1)@0 ASC NULLS LAST], fetch=5", " UnionExec", " SortExec: TopK(fetch=5), expr=[UNNEST(range(Int64(2),Int64(100)))@2 ASC NULLS LAST], preserve_partitioning=[true]", " ProjectionExec: expr=[Int64(1)@0 as Int64(1), NULL as Int64(999), UNNEST(range(Int64(2),Int64(100)))@1 as UNNEST(range(Int64(2),Int64(100)))]", " UnionExec", " ProjectionExec: expr=[1 as Int64(1), NULL as UNNEST(range(Int64(2),Int64(100)))]", " PlaceholderRowExec", " ProjectionExec: expr=[NULL as Int64(1), __unnest_placeholder(range(Int64(2),Int64(100)),depth=1)@0 as UNNEST(range(Int64(2),Int64(100)))]", " UnnestExec", " ProjectionExec: expr=[[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99] as __unnest_placeholder(range(Int64(2),Int64(100)))]", " PlaceholderRowExec", " ProjectionExec: expr=[NULL as Int64(1), 999 as Int64(999), NULL as UNNEST(range(Int64(2),Int64(100)))]", " PlaceholderRowExec"] does not satisfy order requirements: [UNNEST(range(Int64(2),Int64(100)))@2 ASC NULLS LAST, Int64(1)@0 ASC NULLS LAST]. Child-0 order: []
169-
166+
NULL NULL 2
167+
NULL NULL 3
168+
NULL NULL 4
169+
NULL NULL 5
170+
NULL NULL 6
170171

171172
# Order by
172173

0 commit comments

Comments
 (0)