@@ -958,6 +958,24 @@ drop table foo;
958
958
statement ok
959
959
drop table ambiguity_test;
960
960
961
+ ## reproducer for https://github.com/apache/datafusion/issues/12446
962
+ # Ensure union ordering calculations with constants can be optimized
963
+
964
+ statement ok
965
+ create table t(a0 int, a int, b int, c int) as values (1, 2, 3, 4), (5, 6, 7, 8);
966
+
967
+ # expect this query to run successfully, not error
968
+ query III
969
+ select * from (select c, a, NULL::int as a0 from t order by a, c) t1
970
+ union all
971
+ select * from (select c, NULL::int as a, a0 from t order by a0, c) t2
972
+ order by c, a, a0, b
973
+ limit 2;
974
+ ----
975
+ 4 2 NULL
976
+ 4 NULL 1
977
+
978
+
961
979
# Casting from numeric to string types breaks the ordering
962
980
statement ok
963
981
CREATE EXTERNAL TABLE ordered_table (
@@ -1189,3 +1207,48 @@ physical_plan
1189
1207
02)--RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1
1190
1208
03)----SortExec: TopK(fetch=1), expr=[a@0 ASC NULLS LAST], preserve_partitioning=[false]
1191
1209
04)------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[a, b], has_header=true
1210
+
1211
+
1212
+ # Test: inputs into union with different orderings
1213
+ query TT
1214
+ explain select * from (select b, c, a, NULL::int as a0 from ordered_table order by a, c) t1
1215
+ union all
1216
+ select * from (select b, c, NULL::int as a, a0 from ordered_table order by a0, c) t2
1217
+ order by d, c, a, a0, b
1218
+ limit 2;
1219
+ ----
1220
+ logical_plan
1221
+ 01)Projection: t1.b, t1.c, t1.a, t1.a0
1222
+ 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
1223
+ 03)----Union
1224
+ 04)------SubqueryAlias: t1
1225
+ 05)--------Projection: ordered_table.b, ordered_table.c, ordered_table.a, Int32(NULL) AS a0, ordered_table.d
1226
+ 06)----------TableScan: ordered_table projection=[a, b, c, d]
1227
+ 07)------SubqueryAlias: t2
1228
+ 08)--------Projection: ordered_table.b, ordered_table.c, Int32(NULL) AS a, ordered_table.a0, ordered_table.d
1229
+ 09)----------TableScan: ordered_table projection=[a0, b, c, d]
1230
+ physical_plan
1231
+ 01)ProjectionExec: expr=[b@0 as b, c@1 as c, a@2 as a, a0@3 as a0]
1232
+ 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
1233
+ 03)----UnionExec
1234
+ 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]
1235
+ 05)--------ProjectionExec: expr=[b@1 as b, c@2 as c, a@0 as a, NULL as a0, d@3 as d]
1236
+ 06)----------CsvExec: 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], has_header=true
1237
+ 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]
1238
+ 08)--------ProjectionExec: expr=[b@1 as b, c@2 as c, NULL as a, a0@0 as a0, d@3 as d]
1239
+ 09)----------CsvExec: 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], has_header=true
1240
+
1241
+ # Test: run the query from above
1242
+ query IIII
1243
+ select * from (select b, c, a, NULL::int as a0 from ordered_table order by a, c) t1
1244
+ union all
1245
+ select * from (select b, c, NULL::int as a, a0 from ordered_table order by a0, c) t2
1246
+ order by d, c, a, a0, b
1247
+ limit 2;
1248
+ ----
1249
+ 0 0 0 NULL
1250
+ 0 0 NULL 1
1251
+
1252
+
1253
+ statement ok
1254
+ drop table ordered_table;
0 commit comments