@@ -986,17 +986,26 @@ statement ok
986
986
create table t(a0 int, a int, b int, c int) as values (1, 2, 3, 4), (5, 6, 7, 8);
987
987
988
988
# 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
+
989
999
query III
990
1000
select * from (select c, a, NULL::int as a0 from t order by a, c) t1
991
1001
union all
992
1002
select * 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
994
1004
limit 2;
995
1005
----
996
1006
4 2 NULL
997
1007
4 NULL 1
998
1008
999
-
1000
1009
# Casting from numeric to string types breaks the ordering
1001
1010
statement ok
1002
1011
CREATE EXTERNAL TABLE ordered_table (
@@ -1232,43 +1241,41 @@ physical_plan
1232
1241
1233
1242
# Test: inputs into union with different orderings
1234
1243
query 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
1236
1245
union 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
1238
1247
order by d, c, a, a0, b
1239
1248
limit 2;
1240
1249
----
1241
1250
logical_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]
1251
1259
physical_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
1261
1268
1262
1269
# 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
1265
1272
union 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
1267
1274
order by d, c, a, a0, b
1268
1275
limit 2;
1269
1276
----
1270
- 0 0 0 NULL
1271
- 0 0 NULL 1
1277
+ 0 0 0 NULL 0
1278
+ 0 0 NULL 1 0
1272
1279
1273
1280
1274
1281
statement ok
0 commit comments