@@ -986,17 +986,26 @@ statement ok
986986create table t(a0 int, a int, b int, c int) as values (1, 2, 3, 4), (5, 6, 7, 8);
987987
988988# expect this query to run successfully, not error
989+ query IIII
990+ select * from (select c, a, NULL::int as a0, b from t order by a, c) t1
991+ union all
992+ select * from (select c, NULL::int as a, a0, b from t order by a0, c) t2
993+ order by c, a, a0, b
994+ limit 2;
995+ ----
996+ 4 2 NULL 3
997+ 4 NULL 1 3
998+
989999query III
9901000select * from (select c, a, NULL::int as a0 from t order by a, c) t1
9911001union all
9921002select * from (select c, NULL::int as a, a0 from t order by a0, c) t2
993- order by c, a, a0, b
1003+ order by c, a, a0
9941004limit 2;
9951005----
99610064 2 NULL
99710074 NULL 1
9981008
999-
10001009# Casting from numeric to string types breaks the ordering
10011010statement ok
10021011CREATE EXTERNAL TABLE ordered_table (
@@ -1232,43 +1241,41 @@ physical_plan
12321241
12331242# Test: inputs into union with different orderings
12341243query TT
1235- explain select * from (select b, c, a, NULL::int as a0 from ordered_table order by a, c) t1
1244+ explain select * from (select b, c, a, NULL::int as a0, d from ordered_table order by a, c) t1
12361245union all
1237- select * from (select b, c, NULL::int as a, a0 from ordered_table order by a0, c) t2
1246+ select * from (select b, c, NULL::int as a, a0, d from ordered_table order by a0, c) t2
12381247order by d, c, a, a0, b
12391248limit 2;
12401249----
12411250logical_plan
1242- 01)Projection: t1.b, t1.c, t1.a, t1.a0
1243- 02)--Sort: t1.d ASC NULLS LAST, t1.c ASC NULLS LAST, t1.a ASC NULLS LAST, t1.a0 ASC NULLS LAST, t1.b ASC NULLS LAST, fetch=2
1244- 03)----Union
1245- 04)------SubqueryAlias: t1
1246- 05)--------Projection: ordered_table.b, ordered_table.c, ordered_table.a, Int32(NULL) AS a0, ordered_table.d
1247- 06)----------TableScan: ordered_table projection=[a, b, c, d]
1248- 07)------SubqueryAlias: t2
1249- 08)--------Projection: ordered_table.b, ordered_table.c, Int32(NULL) AS a, ordered_table.a0, ordered_table.d
1250- 09)----------TableScan: ordered_table projection=[a0, b, c, d]
1251+ 01)Sort: t1.d ASC NULLS LAST, t1.c ASC NULLS LAST, t1.a ASC NULLS LAST, t1.a0 ASC NULLS LAST, t1.b ASC NULLS LAST, fetch=2
1252+ 02)--Union
1253+ 03)----SubqueryAlias: t1
1254+ 04)------Projection: ordered_table.b, ordered_table.c, ordered_table.a, Int32(NULL) AS a0, ordered_table.d
1255+ 05)--------TableScan: ordered_table projection=[a, b, c, d]
1256+ 06)----SubqueryAlias: t2
1257+ 07)------Projection: ordered_table.b, ordered_table.c, Int32(NULL) AS a, ordered_table.a0, ordered_table.d
1258+ 08)--------TableScan: ordered_table projection=[a0, b, c, d]
12511259physical_plan
1252- 01)ProjectionExec: expr=[b@0 as b, c@1 as c, a@2 as a, a0@3 as a0]
1253- 02)--SortPreservingMergeExec: [d@4 ASC NULLS LAST, c@1 ASC NULLS LAST, a@2 ASC NULLS LAST, a0@3 ASC NULLS LAST, b@0 ASC NULLS LAST], fetch=2
1254- 03)----UnionExec
1255- 04)------SortExec: TopK(fetch=2), expr=[d@4 ASC NULLS LAST, c@1 ASC NULLS LAST, a@2 ASC NULLS LAST, b@0 ASC NULLS LAST], preserve_partitioning=[false]
1256- 05)--------ProjectionExec: expr=[b@1 as b, c@2 as c, a@0 as a, NULL as a0, d@3 as d]
1257- 06)----------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[a, b, c, d], output_ordering=[c@2 ASC NULLS LAST], file_type=csv, has_header=true
1258- 07)------SortExec: TopK(fetch=2), expr=[d@4 ASC NULLS LAST, c@1 ASC NULLS LAST, a0@3 ASC NULLS LAST, b@0 ASC NULLS LAST], preserve_partitioning=[false]
1259- 08)--------ProjectionExec: expr=[b@1 as b, c@2 as c, NULL as a, a0@0 as a0, d@3 as d]
1260- 09)----------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[a0, b, c, d], output_ordering=[c@2 ASC NULLS LAST], file_type=csv, has_header=true
1260+ 01)SortPreservingMergeExec: [d@4 ASC NULLS LAST, c@1 ASC NULLS LAST, a@2 ASC NULLS LAST, a0@3 ASC NULLS LAST, b@0 ASC NULLS LAST], fetch=2
1261+ 02)--UnionExec
1262+ 03)----SortExec: TopK(fetch=2), expr=[d@4 ASC NULLS LAST, c@1 ASC NULLS LAST, a@2 ASC NULLS LAST, b@0 ASC NULLS LAST], preserve_partitioning=[false]
1263+ 04)------ProjectionExec: expr=[b@1 as b, c@2 as c, a@0 as a, NULL as a0, d@3 as d]
1264+ 05)--------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[a, b, c, d], output_ordering=[c@2 ASC NULLS LAST], file_type=csv, has_header=true
1265+ 06)----SortExec: TopK(fetch=2), expr=[d@4 ASC NULLS LAST, c@1 ASC NULLS LAST, a0@3 ASC NULLS LAST, b@0 ASC NULLS LAST], preserve_partitioning=[false]
1266+ 07)------ProjectionExec: expr=[b@1 as b, c@2 as c, NULL as a, a0@0 as a0, d@3 as d]
1267+ 08)--------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[a0, b, c, d], output_ordering=[c@2 ASC NULLS LAST], file_type=csv, has_header=true
12611268
12621269# Test: run the query from above
1263- query IIII
1264- select * from (select b, c, a, NULL::int as a0 from ordered_table order by a, c) t1
1270+ query IIIII
1271+ select * from (select b, c, a, NULL::int as a0, d from ordered_table order by a, c) t1
12651272union all
1266- select * from (select b, c, NULL::int as a, a0 from ordered_table order by a0, c) t2
1273+ select * from (select b, c, NULL::int as a, a0, d from ordered_table order by a0, c) t2
12671274order by d, c, a, a0, b
12681275limit 2;
12691276----
1270- 0 0 0 NULL
1271- 0 0 NULL 1
1277+ 0 0 0 NULL 0
1278+ 0 0 NULL 1 0
12721279
12731280
12741281statement ok
0 commit comments