datafusion-optimizer 56.0.0

DataFusion Query Optimizer
Documentation
1
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements

//! Sort(fetch) → Join pushdown — a sub-module of `push_down_limit`.
//!
//! When a `Sort` with a fetch limit (TopK) sits above a join whose
//! preserved side is known (LEFT / RIGHT / LeftMark / RightMark) and all
//! sort expressions come from the preserved side, we insert a copy of the
//! `Sort(fetch)` onto that input to reduce rows entering the join. The
//! outer `Sort` is kept because a 1-to-many join can produce more than N
//! output rows from N preserved-side rows.
//!
//! CROSS JOIN is deliberately excluded, even though every row from both
//! sides appears in its output when *both* sides are non-empty: if the
//! *other* side turns out to be empty, the join output is empty and the
//! original (unpushed) Sort never evaluates its expression, whereas a
//! pushed Sort would evaluate it eagerly regardless — which can surface an
//! error from a fallible sort expression that a correct, unoptimized
//! execution would never have hit. Proving the other side non-empty at
//! logical-plan time isn't something this optimizer (or DataFusion's
//! optimizer generally) has machinery for, so this rule stays conservative
//! and skips CROSS JOIN entirely rather than risk changing whether a query
//! errors.
//!
//! Dispatched from `PushDownLimit::rewrite` when the plan node is
//! `LogicalPlan::Sort` with `fetch.is_some()`.

use std::collections::HashMap;
use std::sync::Arc;

use crate::utils::{has_all_column_refs, schema_columns};

use datafusion_common::tree_node::{Transformed, TreeNode};
use datafusion_common::{Column, Result, internal_err};
use datafusion_expr::logical_plan::{
    JoinType, LogicalPlan, Projection, Sort as SortPlan, SubqueryAlias,
};
use datafusion_expr::{Expr, SortExpr};

/// Which child of a join is being treated as the preserved side.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Side {
    Left,
    Right,
}

/// If `node` is a transparent node (`Projection` or `SubqueryAlias`),
/// returns its child together with `exprs` resolved through it (column
/// references rewritten to match the child's schema). Returns `None` for
/// any other node, signaling the walk should stop.
///
/// Shared by both tree walks in [`push_topk_through_join`]: peeling down
/// from the `Sort` to find the `Join`, and scanning inside the preserved
/// child for an existing inner `Sort`.
fn peel_transparent_layer<'a>(
    node: &'a LogicalPlan,
    exprs: &[SortExpr],
) -> Result<Option<(&'a LogicalPlan, Vec<SortExpr>)>> {
    match node {
        LogicalPlan::Projection(proj) => Ok(Some((
            proj.input.as_ref(),
            resolve_sort_exprs_through_projection(exprs, proj)?,
        ))),
        LogicalPlan::SubqueryAlias(sq) => Ok(Some((
            sq.input.as_ref(),
            resolve_sort_exprs_through_subquery_alias(exprs, sq)?,
        ))),
        _ => Ok(None),
    }
}

/// Top-level pushdown for `Sort(fetch) → ... → Join` patterns. The caller
/// (`PushDownLimit::rewrite`) has already matched `LogicalPlan::Sort(sort)`
/// with `sort.fetch.is_some()`; taking `Sort` by value here (rather than
/// re-wrapping it into a `LogicalPlan` just to immediately unwrap it) means
/// early exits can hand `sort` straight back via `LogicalPlan::Sort(sort)`
/// without cloning.
pub(super) fn push_topk_through_join(sort: SortPlan) -> Result<Transformed<LogicalPlan>> {
    let Some(fetch) = sort.fetch else {
        return Ok(Transformed::no(LogicalPlan::Sort(sort)));
    };

    // Don't push if any sort expression is non-deterministic (e.g.
    // `random()`). Duplicating such expressions would produce different
    // values at each evaluation point, potentially changing results.
    if sort.expr.iter().any(|se| se.expr.is_volatile()) {
        return Ok(Transformed::no(LogicalPlan::Sort(sort)));
    }

    // Peel through transparent nodes (SubqueryAlias, Projection) to find
    // the Join, resolving sort expressions through each layer along the
    // way so column references end up matching the join's schema. Track
    // intermediates so we can reconstruct the tree afterward.
    let mut current = sort.input.as_ref();
    let mut intermediates: Vec<&LogicalPlan> = Vec::new();
    let mut resolved_sort_exprs = sort.expr.clone();
    let join = loop {
        if let LogicalPlan::Join(join) = current {
            break join;
        }
        match peel_transparent_layer(current, &resolved_sort_exprs)? {
            Some((next, new_exprs)) => {
                intermediates.push(current);
                current = next;
                resolved_sort_exprs = new_exprs;
            }
            None => return Ok(Transformed::no(LogicalPlan::Sort(sort))),
        }
    };

    // Determine which side of the join is preserved.
    //
    // - LEFT / LeftMark: only left preserved.
    // - RIGHT / RightMark: symmetric.
    //
    // Non-equijoin filters in the ON clause are safe: outer joins guarantee
    // all preserved-side rows appear in the output regardless of the
    // filter.
    //
    // CROSS JOIN and other Inner joins are deliberately excluded — see the
    // module doc comment for why CROSS JOIN specifically isn't safe to
    // push through here despite superficially preserving every row.
    let preserved_side = match join.join_type {
        JoinType::Left | JoinType::LeftMark => Side::Left,
        JoinType::Right | JoinType::RightMark => Side::Right,
        _ => return Ok(Transformed::no(LogicalPlan::Sort(sort))),
    };

    // After resolving through projections, sort expressions may now
    // contain volatile functions (e.g. `random() AS col`). Duplicating
    // them would change results.
    if resolved_sort_exprs.iter().any(|se| se.expr.is_volatile()) {
        return Ok(Transformed::no(LogicalPlan::Sort(sort)));
    }

    // A sort key with no column references at all (e.g. `ORDER BY 'x'
    // LIMIT 3`) can't distinguish any row from any other on either side.
    // `has_all_column_refs` is vacuously true for such a key (there are no
    // column refs to fail to find), so without this check we'd still push
    // a Sort onto a preserved child even though its "top N" doesn't depend
    // on that child's data at all — a legal result (any N rows satisfy an
    // all-ties ORDER BY), but wasted work duplicating a Sort node for no
    // row-reduction benefit.
    if resolved_sort_exprs
        .iter()
        .all(|se| se.expr.column_refs().is_empty())
    {
        return Ok(Transformed::no(LogicalPlan::Sort(sort)));
    }

    // Confirm the preserved side's schema contains all referenced sort
    // columns.
    //
    // Caveat: `schema_columns` adds an unqualified entry for every field
    // alongside its qualified one, so an unqualified sort column matches as
    // long as the preserved side has a same-named field — even if an
    // identically-named field also exists on the *other* side, in which
    // case an unqualified reference is genuinely ambiguous and this could
    // wrongly treat a reference to the other side's column as if it were
    // the preserved side's. SQL can never produce this: the planner always
    // qualifies column references (or rejects the query as ambiguous)
    // before this rule runs. Only a hand-built `LogicalPlan` with a
    // deliberately unqualified `Column` can hit this. `push_down_filter`
    // has the exact same caveat, for the same reason (it also calls
    // `schema_columns` to accept unqualified filter columns).
    let schema = match preserved_side {
        Side::Left => join.left.schema(),
        Side::Right => join.right.schema(),
    };
    let cols = schema_columns(schema);
    if !resolved_sort_exprs
        .iter()
        .all(|se| has_all_column_refs(&se.expr, &cols))
    {
        return Ok(Transformed::no(LogicalPlan::Sort(sort)));
    }

    let preserved_child = match preserved_side {
        Side::Left => &join.left,
        Side::Right => &join.right,
    };

    // Scan deep inside the preserved child (through SubqueryAlias and
    // Projection layers) to find an existing Sort. If found with same
    // exprs, tighten its fetch in-place. Otherwise, insert a new Sort
    // directly below the join as the preserved child's wrapper.
    let mut inner_child = preserved_child.as_ref();
    let mut deep_resolved_exprs = resolved_sort_exprs.clone();
    while let Some((next, new_exprs)) =
        peel_transparent_layer(inner_child, &deep_resolved_exprs)?
    {
        inner_child = next;
        deep_resolved_exprs = new_exprs;
    }

    // If the inner child is a Limit, skip pushing down for now.
    //
    // This is only correct-but-conservative, not a correctness requirement:
    // inserting `Sort(fetch)` *above* the Limit (`Sort(fetch=N) -> Limit ->
    // ...`) would be sound regardless of what the Limit's rows are — it
    // only ever reduces which of the Limit's already-capped rows survive,
    // the same "insert a new Sort above the preserved child" move used
    // below when the exprs differ from an existing inner Sort. We decline
    // instead, which is a missed optimization, not a blocked one:
    // - If a `Sort` sits below the Limit (`Limit -> Sort`), `PushDownLimit`
    //   merges the two on a later pass into `Sort(fetch)`, and this rule
    //   revisits the node then and pushes down as normal.
    // - If there's no `Sort` at all (e.g. `LEFT JOIN (SELECT * FROM t LIMIT
    //   100)`), there is nothing for the Limit to ever merge with, so this
    //   case never gets pushed down at all, on any pass — a real gap, not
    //   just a later-pass one, but non-blocking: the join still runs
    //   correctly against the already-limited input, just without the
    //   extra row reduction a pushed Sort could have added.
    if matches!(inner_child, LogicalPlan::Limit(_)) {
        return Ok(Transformed::no(LogicalPlan::Sort(sort)));
    }

    // Determine action based on existing inner Sort:
    // - Same exprs, tighter fetch → skip (already optimal)
    // - Same exprs, larger/no fetch → tighten in-place
    // - Different exprs or no Sort → insert new Sort below the join
    //
    // If `deep_resolved_exprs` became volatile while resolving through
    // projections inside the preserved child (e.g. `random() AS col`),
    // structural equality with an existing inner Sort is unsound: two
    // identical `random()` exprs evaluate to different values. Fall
    // back to inserting a new Sort with `resolved_sort_exprs`.
    let deep_exprs_volatile = deep_resolved_exprs.iter().any(|se| se.expr.is_volatile());
    let inner_sort = match inner_child {
        LogicalPlan::Sort(s) if !deep_exprs_volatile => Some(s),
        _ => None,
    };
    let same_exprs_sort = inner_sort
        .filter(|child_sort| sort_exprs_equal(&child_sort.expr, &deep_resolved_exprs));

    let new_preserved_child = if let Some(child_sort) = same_exprs_sort {
        let child_fetch_tighter = match child_sort.fetch {
            Some(child_fetch) => child_fetch <= fetch,
            None => false,
        };
        if child_fetch_tighter {
            return Ok(Transformed::no(LogicalPlan::Sort(sort)));
        }
        rebuild_with_tightened_sort(
            preserved_child.as_ref(),
            &deep_resolved_exprs,
            fetch,
        )?
    } else {
        // Different exprs, or no existing inner Sort — insert a new Sort
        // above the preserved child. If an inner Sort exists with no
        // fetch, our pushed Sort is the only row reduction. If it has a
        // fetch, re-sorting a small set is cheap and still reduces join
        // input.
        Arc::new(LogicalPlan::Sort(SortPlan {
            expr: resolved_sort_exprs,
            input: Arc::clone(preserved_child),
            fetch: Some(fetch),
        }))
    };

    let mut new_join = join.clone();
    match preserved_side {
        Side::Left => new_join.left = new_preserved_child,
        Side::Right => new_join.right = new_preserved_child,
    }

    // Rebuild the tree: join → intermediate nodes → top-level sort.
    let mut new_sort_input = Arc::new(LogicalPlan::Join(new_join));
    for node in intermediates.into_iter().rev() {
        new_sort_input = Arc::new(match node {
            LogicalPlan::Projection(proj) => {
                let mut new_proj = proj.clone();
                new_proj.input = new_sort_input;
                LogicalPlan::Projection(new_proj)
            }
            LogicalPlan::SubqueryAlias(sq) => LogicalPlan::SubqueryAlias(
                SubqueryAlias::try_new(new_sort_input, sq.alias.clone())?,
            ),
            _ => {
                return internal_err!(
                    "push_topk_through_join: unexpected intermediate node: {}",
                    node.display()
                );
            }
        });
    }

    Ok(Transformed::yes(LogicalPlan::Sort(SortPlan {
        expr: sort.expr,
        input: new_sort_input,
        fetch: sort.fetch,
    })))
}

/// Replace column references in sort expressions using a structural
/// `Column`→expr map. Keying by `Column` (rather than `Column::flat_name()`)
/// avoids collisions between an unqualified column whose name happens to
/// contain a dot (e.g. a quoted alias `"t1.b"`) and a qualified column that
/// stringifies the same way (`t1.b`).
fn replace_columns_in_sort_exprs(
    sort_exprs: &[SortExpr],
    replace_map: &HashMap<Column, Expr>,
) -> Result<Vec<SortExpr>> {
    sort_exprs
        .iter()
        .map(|sort_expr| {
            let new_expr = sort_expr.expr.clone().transform(|expr| {
                let replacement = match &expr {
                    Expr::Column(col) => replace_map.get(col).cloned(),
                    _ => None,
                };
                Ok(replacement.map_or_else(|| Transformed::no(expr), Transformed::yes))
            })?;
            Ok(SortExpr {
                expr: new_expr.data,
                ..*sort_expr
            })
        })
        .collect()
}

/// Resolve sort expressions through a projection by replacing column
/// references with the underlying projection expressions.
fn resolve_sort_exprs_through_projection(
    sort_exprs: &[SortExpr],
    projection: &Projection,
) -> Result<Vec<SortExpr>> {
    let replace_map: HashMap<Column, Expr> = projection
        .schema
        .iter()
        .zip(projection.expr.iter())
        .map(|((qualifier, field), expr)| {
            let key = Column::from((qualifier, field));
            (key, expr.clone().unalias())
        })
        .collect();

    replace_columns_in_sort_exprs(sort_exprs, &replace_map)
}

/// Compare two slices of `SortExpr` for structural equality.
fn sort_exprs_equal(a: &[SortExpr], b: &[SortExpr]) -> bool {
    a.len() == b.len()
        && a.iter().zip(b.iter()).all(|(left, right)| {
            left.asc == right.asc
                && left.nulls_first == right.nulls_first
                && left.expr == right.expr
        })
}

/// Resolve sort expressions through a `SubqueryAlias` by replacing the
/// alias qualifier with the input schema's qualifier.
fn resolve_sort_exprs_through_subquery_alias(
    sort_exprs: &[SortExpr],
    subquery_alias: &SubqueryAlias,
) -> Result<Vec<SortExpr>> {
    let replace_map: HashMap<Column, Expr> = subquery_alias
        .schema
        .iter()
        .zip(subquery_alias.input.schema().iter())
        .map(|((alias_qual, alias_field), (input_qual, input_field))| {
            let alias_col = Column::from((alias_qual, alias_field));
            let input_col = Column::from((input_qual, input_field));
            (alias_col, Expr::Column(input_col))
        })
        .collect();

    replace_columns_in_sort_exprs(sort_exprs, &replace_map)
}

/// Rebuild the tree from `root` down to an existing Sort whose expressions
/// match `target_exprs`, tightening its fetch to `new_fetch`.
fn rebuild_with_tightened_sort(
    root: &LogicalPlan,
    target_exprs: &[SortExpr],
    new_fetch: usize,
) -> Result<Arc<LogicalPlan>> {
    match root {
        LogicalPlan::Sort(s) if sort_exprs_equal(&s.expr, target_exprs) => {
            Ok(Arc::new(LogicalPlan::Sort(SortPlan {
                expr: s.expr.clone(),
                input: Arc::clone(&s.input),
                fetch: Some(new_fetch),
            })))
        }
        LogicalPlan::Projection(proj) => {
            let new_input = rebuild_with_tightened_sort(
                proj.input.as_ref(),
                target_exprs,
                new_fetch,
            )?;
            let mut new_proj = proj.clone();
            new_proj.input = new_input;
            Ok(Arc::new(LogicalPlan::Projection(new_proj)))
        }
        LogicalPlan::SubqueryAlias(sq) => {
            let new_input =
                rebuild_with_tightened_sort(sq.input.as_ref(), target_exprs, new_fetch)?;
            Ok(Arc::new(LogicalPlan::SubqueryAlias(
                SubqueryAlias::try_new(new_input, sq.alias.clone())?,
            )))
        }
        _ => internal_err!(
            "rebuild_with_tightened_sort: unexpected node: {}",
            root.display()
        ),
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::OptimizerContext;
    use crate::assert_optimized_plan_eq_snapshot;
    use crate::push_down_limit::PushDownLimit;
    use crate::test::*;

    use datafusion_expr::col;
    use datafusion_expr::lit;
    use datafusion_expr::logical_plan::builder::LogicalPlanBuilder;

    macro_rules! assert_optimized_plan_equal {
        (
            $plan:expr,
            @ $expected:literal $(,)?
        ) => {{
            let optimizer_ctx = OptimizerContext::new().with_max_passes(1);
            let rules: Vec<Arc<dyn crate::OptimizerRule + Send + Sync>> =
                vec![Arc::new(PushDownLimit::new())];
            assert_optimized_plan_eq_snapshot!(
                optimizer_ctx,
                rules,
                $plan,
                @ $expected,
            )
        }};
    }

    /// TopK on left-side columns above a LEFT JOIN → pushed to left child.
    #[test]
    fn topk_pushed_to_left_of_left_join() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=3
          Left Join: t1.a = t2.a
            Sort: t1.b ASC NULLS LAST, fetch=3
              TableScan: t1
            TableScan: t2
        "
        )
    }

    /// TopK on right-side columns above a RIGHT JOIN → pushed to right child.
    #[test]
    fn topk_pushed_to_right_of_right_join() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Right,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t2.b").sort(true, false)], Some(5))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t2.b ASC NULLS LAST, fetch=5
          Right Join: t1.a = t2.a
            TableScan: t1
            Sort: t2.b ASC NULLS LAST, fetch=5
              TableScan: t2
        "
        )
    }

    /// TopK pushed through a Projection between Sort and Join.
    #[test]
    fn topk_pushed_through_projection() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .project(vec![col("t1.a"), col("t1.b"), col("t2.c")])?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=3
          Projection: t1.a, t1.b, t2.c
            Left Join: t1.a = t2.a
              Sort: t1.b ASC NULLS LAST, fetch=3
                TableScan: t1
              TableScan: t2
        "
        )
    }

    /// INNER JOIN → no pushdown.
    #[test]
    fn topk_not_pushed_for_inner_join() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Inner,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=3
          Inner Join: t1.a = t2.a
            TableScan: t1
            TableScan: t2
        "
        )
    }

    /// CROSS JOIN is never pushed through, even when the sort key comes
    /// entirely from one side: see the module doc comment for why (a
    /// pushed Sort would evaluate eagerly on a side whose relevance
    /// depends on the *other* side being non-empty, which this rule has no
    /// way to prove at logical-plan time).
    #[test]
    fn topk_not_pushed_for_cross_join_left_side_sort() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .cross_join(LogicalPlanBuilder::from(t2).build()?)?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=3
          Cross Join:
            TableScan: t1
            TableScan: t2
        "
        )
    }

    /// Symmetric to the left-side case above.
    #[test]
    fn topk_not_pushed_for_cross_join_right_side_sort() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .cross_join(LogicalPlanBuilder::from(t2).build()?)?
            .sort_with_limit(vec![col("t2.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t2.b ASC NULLS LAST, fetch=3
          Cross Join:
            TableScan: t1
            TableScan: t2
        "
        )
    }

    /// CROSS JOIN sorted by columns from both sides → no pushdown (would
    /// still be excluded even if CROSS JOIN were otherwise supported).
    #[test]
    fn topk_not_pushed_for_cross_join_mixed_side_sort() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .cross_join(LogicalPlanBuilder::from(t2).build()?)?
            .sort_with_limit(
                vec![(col("t1.b") + col("t2.b")).sort(true, false)],
                Some(3),
            )?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b + t2.b ASC NULLS LAST, fetch=3
          Cross Join:
            TableScan: t1
            TableScan: t2
        "
        )
    }

    /// Inner join with no equi-keys but a non-empty filter: filter can drop
    /// rows from either side, so pushing fetch=N is unsafe.
    #[test]
    fn topk_not_pushed_for_inner_with_filter_no_on() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join_on(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Inner,
                vec![col("t1.b").gt(col("t2.b"))],
            )?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=3
          Inner Join:  Filter: t1.b > t2.b
            TableScan: t1
            TableScan: t2
        "
        )
    }

    /// LEFT MARK join: one record per left row → pushdown to left.
    #[test]
    fn topk_pushed_to_left_of_left_mark_join() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::LeftMark,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=3
          LeftMark Join: t1.a = t2.a
            Sort: t1.b ASC NULLS LAST, fetch=3
              TableScan: t1
            TableScan: t2
        "
        )
    }

    /// RIGHT MARK join: symmetric to LeftMark.
    #[test]
    fn topk_pushed_to_right_of_right_mark_join() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::RightMark,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t2.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t2.b ASC NULLS LAST, fetch=3
          RightMark Join: t1.a = t2.a
            TableScan: t1
            Sort: t2.b ASC NULLS LAST, fetch=3
              TableScan: t2
        "
        )
    }

    /// LEFT JOIN but sort on right-side columns → no pushdown.
    #[test]
    fn topk_not_pushed_for_wrong_side() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t2.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t2.b ASC NULLS LAST, fetch=3
          Left Join: t1.a = t2.a
            TableScan: t1
            TableScan: t2
        "
        )
    }

    /// Join with non-equijoin filter → pushdown still happens.
    #[test]
    fn topk_pushed_with_join_filter() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join_on(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                vec![col("t1.a").eq(col("t2.a"))],
            )?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=3
          Left Join:  Filter: t1.a = t2.a
            Sort: t1.b ASC NULLS LAST, fetch=3
              TableScan: t1
            TableScan: t2
        "
        )
    }

    /// Sort without fetch → no pushdown.
    #[test]
    fn topk_not_pushed_without_fetch() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort(vec![col("t1.b").sort(true, false)])?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST
          Left Join: t1.a = t2.a
            TableScan: t1
            TableScan: t2
        "
        )
    }

    /// LEFT SEMI JOIN: not all left rows appear in output → no pushdown.
    #[test]
    fn topk_not_pushed_for_left_semi_join() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::LeftSemi,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=3
          LeftSemi Join: t1.a = t2.a
            TableScan: t1
            TableScan: t2
        "
        )
    }

    /// LEFT ANTI JOIN: not all left rows appear in output → no pushdown.
    #[test]
    fn topk_not_pushed_for_left_anti_join() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::LeftAnti,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=3
          LeftAnti Join: t1.a = t2.a
            TableScan: t1
            TableScan: t2
        "
        )
    }

    /// RIGHT SEMI JOIN: not all right rows appear in output → no pushdown.
    #[test]
    fn topk_not_pushed_for_right_semi_join() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::RightSemi,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t2.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t2.b ASC NULLS LAST, fetch=3
          RightSemi Join: t1.a = t2.a
            TableScan: t1
            TableScan: t2
        "
        )
    }

    /// RIGHT ANTI JOIN: not all right rows appear in output → no pushdown.
    #[test]
    fn topk_not_pushed_for_right_anti_join() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::RightAnti,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t2.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t2.b ASC NULLS LAST, fetch=3
          RightAnti Join: t1.a = t2.a
            TableScan: t1
            TableScan: t2
        "
        )
    }

    /// Multi-column sort with columns from both sides → no pushdown.
    #[test]
    fn topk_not_pushed_for_mixed_side_sort() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(
                vec![col("t1.b").sort(true, false), col("t2.b").sort(true, false)],
                Some(3),
            )?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, t2.b ASC NULLS LAST, fetch=3
          Left Join: t1.a = t2.a
            TableScan: t1
            TableScan: t2
        "
        )
    }

    /// Preserved child has a larger fetch → push our tighter limit.
    #[test]
    fn topk_pushed_when_child_has_larger_fetch() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let t1_with_sort = LogicalPlanBuilder::from(t1)
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(10))?
            .build()?;

        let plan = LogicalPlanBuilder::from(t1_with_sort)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=3
          Left Join: t1.a = t2.a
            Sort: t1.b ASC NULLS LAST, fetch=3
              TableScan: t1
            TableScan: t2
        "
        )
    }

    /// Preserved child already has a tighter fetch → skip pushdown.
    #[test]
    fn topk_not_pushed_when_child_has_smaller_fetch() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let t1_with_sort = LogicalPlanBuilder::from(t1)
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(2))?
            .build()?;

        let plan = LogicalPlanBuilder::from(t1_with_sort)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(5))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=5
          Left Join: t1.a = t2.a
            Sort: t1.b ASC NULLS LAST, fetch=2
              TableScan: t1
            TableScan: t2
        "
        )
    }

    /// Projection passthrough: sort expr matches a projected column directly.
    #[test]
    fn resolve_through_projection_passthrough() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let plan = LogicalPlanBuilder::from(t1)
            .project(vec![col("t1.a"), col("t1.b")])?
            .build()?;
        let LogicalPlan::Projection(proj) = &plan else {
            panic!("expected Projection");
        };
        let sort_exprs = vec![col("t1.b").sort(true, false)];
        let resolved = resolve_sort_exprs_through_projection(&sort_exprs, proj)?;
        assert_eq!(resolved.len(), 1);
        assert_eq!(resolved[0].expr.to_string(), "t1.b");
        assert!(resolved[0].asc);
        Ok(())
    }

    /// Projection alias: sort expr references an alias mapping to a negation.
    #[test]
    fn resolve_through_projection_alias() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let plan = LogicalPlanBuilder::from(t1)
            .project(vec![
                col("t1.a"),
                (Expr::Negative(Box::new(col("t1.b")))).alias("neg_b"),
            ])?
            .build()?;
        let LogicalPlan::Projection(proj) = &plan else {
            panic!("expected Projection");
        };
        let sort_exprs = vec![col("neg_b").sort(true, false)];
        let resolved = resolve_sort_exprs_through_projection(&sort_exprs, proj)?;
        assert_eq!(resolved.len(), 1);
        assert_eq!(resolved[0].expr.to_string(), "(- t1.b)");
        Ok(())
    }

    /// Regression test for a `flat_name()` collision: an unqualified alias
    /// literally named "t1.b" (as produced by a quoted identifier like
    /// `t2.y AS "t1.b"`) must not be confused with the qualified column
    /// `t1.b` that is also present in the same projection. Both used to
    /// stringify to the same `flat_name()` ("t1.b"), so the old
    /// `HashMap<String, Expr>` would silently resolve the alias to the
    /// wrong underlying expression.
    ///
    /// Built via `Projection::try_new` directly rather than
    /// `LogicalPlanBuilder::project`: the builder's `project()` rejects two
    /// expressions with the same `schema_name()` up front (so this exact
    /// shape can't come from a `SELECT` list), but nothing stops a
    /// `Projection` built by other means — e.g. a different optimizer rule,
    /// or a plan constructed directly via the expr API — from having two
    /// structurally distinct output columns whose *string* names collide.
    /// `resolve_sort_exprs_through_projection` must handle that shape
    /// correctly regardless of how the `Projection` was constructed.
    #[test]
    fn resolve_through_projection_quoted_alias_no_collision() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;
        let join = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .build()?;
        let proj = Projection::try_new(
            vec![col("t1.a"), col("t2.b").alias("t1.b"), col("t1.b")],
            Arc::new(join),
        )?;

        // `"t1.b"` here is the unqualified alias output column, not the
        // qualified `t1.b` column also present in the projection.
        let quoted_alias = Expr::Column(Column::new_unqualified("t1.b"));
        let sort_exprs = vec![quoted_alias.sort(true, false)];
        let resolved = resolve_sort_exprs_through_projection(&sort_exprs, &proj)?;
        assert_eq!(resolved.len(), 1);
        assert_eq!(resolved[0].expr.to_string(), "t2.b");
        Ok(())
    }

    /// Multi-column resolution preserves direction and nulls_first per column.
    #[test]
    fn resolve_through_projection_multi_column() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let plan = LogicalPlanBuilder::from(t1)
            .project(vec![col("t1.a"), col("t1.b"), col("t1.c")])?
            .build()?;
        let LogicalPlan::Projection(proj) = &plan else {
            panic!("expected Projection");
        };
        let sort_exprs =
            vec![col("t1.a").sort(true, false), col("t1.b").sort(false, true)];
        let resolved = resolve_sort_exprs_through_projection(&sort_exprs, proj)?;
        assert_eq!(resolved.len(), 2);
        assert_eq!(resolved[0].expr.to_string(), "t1.a");
        assert!(resolved[0].asc);
        assert_eq!(resolved[1].expr.to_string(), "t1.b");
        assert!(!resolved[1].asc);
        assert!(resolved[1].nulls_first);
        Ok(())
    }

    /// Stacked Projection + SubqueryAlias: resolve through both layers.
    #[test]
    fn resolve_through_projection_and_subquery_alias() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let plan = LogicalPlanBuilder::from(t1)
            .alias("sub")?
            .project(vec![
                col("sub.a"),
                (Expr::Negative(Box::new(col("sub.b")))).alias("neg_b"),
            ])?
            .build()?;
        let LogicalPlan::Projection(proj) = &plan else {
            panic!("expected Projection");
        };
        let LogicalPlan::SubqueryAlias(sq) = proj.input.as_ref() else {
            panic!("expected SubqueryAlias");
        };
        let sort_exprs = vec![col("neg_b").sort(true, false)];
        let after_proj = resolve_sort_exprs_through_projection(&sort_exprs, proj)?;
        assert_eq!(after_proj[0].expr.to_string(), "(- sub.b)");
        let after_sq = resolve_sort_exprs_through_subquery_alias(&after_proj, sq)?;
        assert_eq!(after_sq[0].expr.to_string(), "(- t1.b)");
        assert!(after_sq[0].asc);
        assert!(!after_sq[0].nulls_first);
        Ok(())
    }

    /// Simple SubqueryAlias resolution: sub.b → t1.b.
    #[test]
    fn resolve_through_subquery_alias_simple() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let plan = LogicalPlanBuilder::from(t1).alias("sub")?.build()?;
        let LogicalPlan::SubqueryAlias(sq) = &plan else {
            panic!("expected SubqueryAlias");
        };
        let sort_exprs = vec![col("sub.b").sort(true, false)];
        let resolved = resolve_sort_exprs_through_subquery_alias(&sort_exprs, sq)?;
        assert_eq!(resolved.len(), 1);
        assert_eq!(resolved[0].expr.to_string(), "t1.b");
        assert!(resolved[0].asc);
        assert!(!resolved[0].nulls_first);
        Ok(())
    }

    /// Multi-column SubqueryAlias resolution preserves direction per column.
    #[test]
    fn resolve_through_subquery_alias_multi_column() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let plan = LogicalPlanBuilder::from(t1).alias("sub")?.build()?;
        let LogicalPlan::SubqueryAlias(sq) = &plan else {
            panic!("expected SubqueryAlias");
        };
        let sort_exprs = vec![
            col("sub.a").sort(true, false),
            col("sub.b").sort(false, true),
        ];
        let resolved = resolve_sort_exprs_through_subquery_alias(&sort_exprs, sq)?;
        assert_eq!(resolved.len(), 2);
        assert_eq!(resolved[0].expr.to_string(), "t1.a");
        assert!(resolved[0].asc);
        assert_eq!(resolved[1].expr.to_string(), "t1.b");
        assert!(!resolved[1].asc);
        assert!(resolved[1].nulls_first);
        Ok(())
    }

    /// SubqueryAlias with a different alias name (foo ≠ t1).
    #[test]
    fn resolve_through_subquery_alias_different_name() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let plan = LogicalPlanBuilder::from(t1).alias("foo")?.build()?;
        let LogicalPlan::SubqueryAlias(sq) = &plan else {
            panic!("expected SubqueryAlias");
        };
        let sort_exprs = vec![col("foo.b").sort(true, false)];
        let resolved = resolve_sort_exprs_through_subquery_alias(&sort_exprs, sq)?;
        assert_eq!(resolved.len(), 1);
        assert_eq!(resolved[0].expr.to_string(), "t1.b");
        Ok(())
    }

    /// SubqueryAlias with nested expression: (- sub.b) → (- t1.b).
    #[test]
    fn resolve_through_subquery_alias_nested_expr() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let plan = LogicalPlanBuilder::from(t1).alias("sub")?.build()?;
        let LogicalPlan::SubqueryAlias(sq) = &plan else {
            panic!("expected SubqueryAlias");
        };
        let sort_exprs = vec![Expr::Negative(Box::new(col("sub.b"))).sort(true, false)];
        let resolved = resolve_sort_exprs_through_subquery_alias(&sort_exprs, sq)?;
        assert_eq!(resolved.len(), 1);
        assert_eq!(resolved[0].expr.to_string(), "(- t1.b)");
        assert!(resolved[0].asc);
        Ok(())
    }

    /// Inner Sort has different exprs WITH fetch → stacked sorts.
    #[test]
    fn topk_stacked_when_child_has_different_exprs_with_fetch() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let t1_with_sort = LogicalPlanBuilder::from(t1)
            .sort_with_limit(vec![col("t1.a").sort(true, false)], Some(5))?
            .build()?;

        let plan = LogicalPlanBuilder::from(t1_with_sort)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(2))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=2
          Left Join: t1.a = t2.a
            Sort: t1.b ASC NULLS LAST, fetch=2
              Sort: t1.a ASC NULLS LAST, fetch=5
                TableScan: t1
            TableScan: t2
        "
        )
    }

    /// Inner Sort has different exprs WITHOUT fetch → stacked sorts.
    #[test]
    fn topk_stacked_when_child_has_different_exprs_no_fetch() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let t1_with_sort = LogicalPlanBuilder::from(t1)
            .sort(vec![col("t1.a").sort(true, false)])?
            .build()?;

        let plan = LogicalPlanBuilder::from(t1_with_sort)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(2))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=2
          Left Join: t1.a = t2.a
            Sort: t1.b ASC NULLS LAST, fetch=2
              Sort: t1.a ASC NULLS LAST
                TableScan: t1
            TableScan: t2
        "
        )
    }

    /// End-to-end regression for the `flat_name()` collision: `t2.b AS
    /// "t1.b"` (an unqualified alias literally named "t1.b") sits next to
    /// the qualified column `t1.b` in the same projection. ORDER BY the
    /// quoted alias must push down sorted on `t2.b` (the right/preserved
    /// side of this RIGHT JOIN), not silently resolve to `t1.b` and push
    /// the wrong TopK onto the wrong side.
    #[test]
    fn topk_pushed_through_projection_quoted_alias_no_collision() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let join = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Right,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .build()?;
        // Built via `Projection::try_new` (see comment on
        // `resolve_through_projection_quoted_alias_no_collision`): the
        // builder's `project()` would reject this expression list outright.
        let proj = LogicalPlan::Projection(Projection::try_new(
            vec![col("t1.a"), col("t2.b").alias("t1.b"), col("t1.b")],
            Arc::new(join),
        )?);
        let plan = LogicalPlanBuilder::from(proj)
            .sort_with_limit(
                vec![Expr::Column(Column::new_unqualified("t1.b")).sort(true, false)],
                Some(3),
            )?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r#"
        Sort: t1.b ASC NULLS LAST, fetch=3
          Projection: t1.a, t2.b AS t1.b, t1.b
            Right Join: t1.a = t2.a
              TableScan: t1
              Sort: t2.b ASC NULLS LAST, fetch=3
                TableScan: t2
        "#
        )
    }

    /// Plan-level regression for the CROSS JOIN empty-side hazard: a
    /// division (fallible) sort key over a CROSS JOIN must never be pushed
    /// down, since doing so would evaluate it eagerly on rows that a
    /// correct, unoptimized execution would never reach if the other side
    /// turned out to be empty (see `topk_pushed_for_cross_join_with_empty_other_side_does_not_error`
    /// in the SLT suite for the corresponding end-to-end execution case).
    #[test]
    fn topk_not_pushed_for_cross_join_with_fallible_sort_expr() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .cross_join(LogicalPlanBuilder::from(t2).build()?)?
            .sort_with_limit(
                vec![(col("t1.a") / col("t1.b")).sort(true, false)],
                Some(3),
            )?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.a / t1.b ASC NULLS LAST, fetch=3
          Cross Join:
            TableScan: t1
            TableScan: t2
        "
        )
    }

    /// A sort key with no column references at all (e.g. a literal) can't
    /// distinguish any row from any other on either side. `has_all_column_refs`
    /// is vacuously true for it, so without an explicit guard this would
    /// still push a Sort onto the first candidate side — legal (any N rows
    /// satisfy an all-ties ORDER BY) but wasted work, since the pushed
    /// Sort's "top N" doesn't depend on that side's data at all.
    #[test]
    fn topk_not_pushed_for_sort_key_with_no_column_refs() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![lit(1i64).sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: Int64(1) ASC NULLS LAST, fetch=3
          Left Join: t1.a = t2.a
            TableScan: t1
            TableScan: t2
        "
        )
    }

    /// `Limit(skip=0) → Sort(no fetch) → Join`: `rewrite_limit` merges the
    /// Limit's fetch into the Sort and drops the redundant `Limit(skip=0)`
    /// in the same visit. Confirms that the TopK-through-join pushdown
    /// still applies to that Sort, so one pass reaches the fully collapsed
    /// form.
    #[test]
    fn topk_pushed_through_limit_then_sort_in_one_pass() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort(vec![col("t1.b").sort(true, false)])?
            .limit(0, Some(3))?
            .build()?;

        let optimizer_ctx = OptimizerContext::new().with_max_passes(1);
        let rules: Vec<Arc<dyn crate::OptimizerRule + Send + Sync>> =
            vec![Arc::new(PushDownLimit::new())];
        assert_optimized_plan_eq_snapshot!(
            optimizer_ctx,
            rules,
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=3
          Left Join: t1.a = t2.a
            Sort: t1.b ASC NULLS LAST, fetch=3
              TableScan: t1
            TableScan: t2
        "
        )
    }

    /// A `Limit` over a `Sort` that already has an equal or smaller fetch is
    /// dropped, and the `Sort` replaces it as the visited node. The TopK must
    /// still move through the join in the same pass.
    fn optimize_limit_over_sort_with_fetch(limit_fetch: usize) -> Result<LogicalPlan> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .limit(0, Some(limit_fetch))?
            .build()?;

        let optimizer_ctx = OptimizerContext::new().with_max_passes(1);
        crate::Optimizer::with_rules(vec![Arc::new(PushDownLimit::new())]).optimize(
            plan,
            &optimizer_ctx,
            |_, _| {},
        )
    }

    #[test]
    fn topk_pushed_through_limit_over_sort_with_same_fetch_in_one_pass() -> Result<()> {
        insta::assert_snapshot!(optimize_limit_over_sort_with_fetch(3)?, @r"
        Sort: t1.b ASC NULLS LAST, fetch=3
          Left Join: t1.a = t2.a
            Sort: t1.b ASC NULLS LAST, fetch=3
              TableScan: t1
            TableScan: t2
        ");
        Ok(())
    }

    #[test]
    fn topk_pushed_through_limit_over_sort_with_smaller_fetch_in_one_pass() -> Result<()>
    {
        insta::assert_snapshot!(optimize_limit_over_sort_with_fetch(5)?, @r"
        Sort: t1.b ASC NULLS LAST, fetch=3
          Left Join: t1.a = t2.a
            Sort: t1.b ASC NULLS LAST, fetch=3
              TableScan: t1
            TableScan: t2
        ");
        Ok(())
    }

    /// Running the optimizer a second time on its own output must be a
    /// no-op: a rule that keeps rewriting an already-optimized plan (e.g.
    /// oscillating between two equivalent forms) would never let the
    /// overall optimizer converge.
    #[test]
    fn topk_pushed_through_join_is_idempotent() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .build()?;

        let rules: Vec<Arc<dyn crate::OptimizerRule + Send + Sync>> =
            vec![Arc::new(PushDownLimit::new())];
        let optimizer = crate::Optimizer::with_rules(rules);
        let ctx = OptimizerContext::new().with_max_passes(1);

        let once = optimizer.optimize(plan, &ctx, |_, _| {})?;
        let twice = optimizer.optimize(once.clone(), &ctx, |_, _| {})?;
        assert_eq!(once, twice);
        Ok(())
    }

    /// `fetch = 0` is a degenerate but valid TopK: the pushed Sort still
    /// carries `fetch=0` rather than being special-cased away.
    #[test]
    fn topk_pushed_with_fetch_zero() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(0))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=0
          Left Join: t1.a = t2.a
            Sort: t1.b ASC NULLS LAST, fetch=0
              TableScan: t1
            TableScan: t2
        "
        )
    }

    /// Preserved child is a bare `Limit` with no `Sort` beneath it at all
    /// (`LEFT JOIN (SELECT * FROM t1 LIMIT 5)`) — there is nothing for the
    /// Limit to ever merge with, so this is a permanent missed
    /// optimization, not a transient one that resolves on a later pass (see
    /// the comment at the `matches!(inner_child, LogicalPlan::Limit(_))`
    /// check). It must still skip cleanly here rather than push past the
    /// Limit and silently ignore the existing row cap.
    #[test]
    fn topk_not_pushed_when_child_is_bare_limit() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let t1_with_limit = LogicalPlanBuilder::from(t1).limit(0, Some(5))?.build()?;

        let plan = LogicalPlanBuilder::from(t1_with_limit)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r"
        Sort: t1.b ASC NULLS LAST, fetch=3
          Left Join: t1.a = t2.a
            Limit: skip=0, fetch=5
              TableScan: t1, fetch=5
            TableScan: t2
        "
        )
    }

    /// A `Filter` between the `Sort` and the `Join` blocks pushdown: `Filter`
    /// is not one of the transparent nodes this rewrite peels through (a
    /// `Filter` can drop preserved-side rows, which would change which rows
    /// survive to be sorted). Running `PushDownLimit` alone can never push
    /// through it, however many passes are allowed.
    #[test]
    fn topk_not_pushed_through_filter_with_push_down_limit_alone() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .filter(col("t1.c").eq(lit("foo")))?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .build()?;

        assert_optimized_plan_equal!(
            plan,
            @r#"
        Sort: t1.b ASC NULLS LAST, fetch=3
          Filter: t1.c = Utf8("foo")
            Left Join: t1.a = t2.a
              TableScan: t1
              TableScan: t2
        "#
        )
    }

    /// Same plan as above, but with `PushDownFilter` also in the rule set
    /// and two passes allowed: pass 1 runs `push_down_limit` first (declines,
    /// same as the single-rule case above) and then `push_down_filter` sinks
    /// the Filter onto `t1` (the only side it references) below the Join;
    /// pass 2 revisits the Sort, which now sits directly above the Join and
    /// pushes through. One pass alone is not enough — see
    /// `topk_not_pushed_through_filter_with_push_down_limit_alone` and the
    /// module doc comment's note on this ordering dependency.
    #[test]
    fn topk_pushed_through_filter_after_push_down_filter_with_two_passes() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .filter(col("t1.c").eq(lit("foo")))?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .build()?;

        let optimizer_ctx = OptimizerContext::new().with_max_passes(2);
        let rules: Vec<Arc<dyn crate::OptimizerRule + Send + Sync>> = vec![
            Arc::new(PushDownLimit::new()),
            Arc::new(crate::push_down_filter::PushDownFilter::new()),
        ];
        assert_optimized_plan_eq_snapshot!(
            optimizer_ctx,
            rules,
            plan,
            @r#"
        Sort: t1.b ASC NULLS LAST, fetch=3
          Left Join: t1.a = t2.a
            Sort: t1.b ASC NULLS LAST, fetch=3
              TableScan: t1, full_filters=[t1.c = Utf8("foo")]
            TableScan: t2
        "#
        )
    }

    /// Regression test using the *real* default rule set and order
    /// (`Optimizer::new()`), not the 2-rule subset above: with
    /// `max_passes(1)` — e.g. `datafusion.optimizer.max_passes = 1` — the
    /// TopK is never pushed down for `Sort(fetch) -> Filter -> Left Join`,
    /// even though `PushDownFilter` does sink the filter into the scan
    /// within that same single pass. This is because `push_down_limit`
    /// (which currently owns the TopK-through-join rewrite) runs *before*
    /// `push_down_filter` in `Optimizer::new()`'s rule list, so by the time
    /// this rewrite's tree walk reaches the Filter, the Filter hasn't been
    /// sunk below the Join yet — and `push_down_limit` is never revisited
    /// again within the same pass. A later fix should let the specialized
    /// TopK-through-join transformation run after filter pushdown without
    /// moving the ordinary Limit transformation past filters (see the
    /// module doc comment).
    #[test]
    fn topk_not_pushed_with_default_optimizer_and_one_pass() -> Result<()> {
        let t1 = test_table_scan_with_name("t1")?;
        let t2 = test_table_scan_with_name("t2")?;

        let plan = LogicalPlanBuilder::from(t1)
            .join(
                LogicalPlanBuilder::from(t2).build()?,
                JoinType::Left,
                (vec!["a"], vec!["a"]),
                None,
            )?
            .filter(col("t1.c").eq(lit("foo")))?
            .sort_with_limit(vec![col("t1.b").sort(true, false)], Some(3))?
            .build()?;

        let optimizer_ctx = OptimizerContext::new().with_max_passes(1);
        let optimizer = crate::Optimizer::new();
        let optimized = optimizer.optimize(plan, &optimizer_ctx, |_, _| {})?;

        insta::assert_snapshot!(optimized, @r#"
        Sort: t1.b ASC NULLS LAST, fetch=3
          Left Join: t1.a = t2.a
            TableScan: t1 projection=[a, b, c], full_filters=[t1.c = Utf8("foo")]
            TableScan: t2 projection=[a, b, c]
        "#);
        Ok(())
    }
}