stoolap 0.4.0

High-performance embedded SQL database with MVCC, time-travel queries, and full ACID compliance
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
// Copyright 2025 Stoolap Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(clippy::only_used_in_recursion)]

//! Join ordering optimization for multi-way joins
//!
//! This module implements join ordering optimization using **Dynamic Programming**
//! for optimal plans and **greedy fallback** for large queries.
//!
//! ```sql
//! SELECT * FROM A JOIN B ON A.x = B.x JOIN C ON B.y = C.y JOIN D ON C.z = D.z
//! ```
//!
//! ## Algorithms
//!
//! ### 1. Dynamic Programming (for n ≤ 10 tables)
//!
//! Uses bitmask-based DP to explore ALL possible join orderings, finding the
//! globally optimal plan. This is O(3^n) but produces perfect results.
//!
//! The algorithm:
//! 1. Enumerate all 2^n subsets of tables
//! 2. For each subset, find the cheapest way to partition it into two subsets
//! 3. Memoize the optimal cost for each subset
//! 4. Build bushy trees (not just left-deep) for potentially better plans
//!
//! ### 2. Greedy Algorithm (fallback for n > 10)
//!
//! Uses O(n²) greedy approach for very large joins where DP would be too slow.
//!
//! ## Stoolap-Specific Optimizations
//!
//! - **Edge-aware memory limits**: Considers available memory for hash tables
//! - **Interesting orderings**: Tracks sort orders for potential merge joins
//! - **Cross-product avoidance**: Strongly penalizes Cartesian products

use rustc_hash::{FxHashMap, FxHashSet};

use crate::optimizer::cost::{CostEstimator, JoinAlgorithm, JoinStats, PlanCost};
use crate::storage::statistics::TableStats;

/// Represents a join condition between two tables
#[derive(Debug, Clone)]
pub struct JoinCondition {
    /// Left table name
    pub left_table: String,
    /// Left column name
    pub left_column: String,
    /// Right table name
    pub right_table: String,
    /// Right column name
    pub right_column: String,
    /// Whether this is an equality condition (col1 = col2)
    pub is_equality: bool,
}

impl JoinCondition {
    /// Create a new equality join condition
    pub fn new_equality(
        left_table: String,
        left_column: String,
        right_table: String,
        right_column: String,
    ) -> Self {
        Self {
            left_table,
            left_column,
            right_table,
            right_column,
            is_equality: true,
        }
    }

    /// Check if this condition connects the given tables
    pub fn connects(&self, tables1: &FxHashSet<String>, tables2: &FxHashSet<String>) -> bool {
        (tables1.contains(&self.left_table) && tables2.contains(&self.right_table))
            || (tables1.contains(&self.right_table) && tables2.contains(&self.left_table))
    }

    /// Check if this condition involves the given table
    pub fn involves(&self, table: &str) -> bool {
        self.left_table == table || self.right_table == table
    }
}

/// Represents a column sort order
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SortOrder {
    /// Column name (may be table.column for qualified names)
    pub column: String,
    /// True for ascending, false for descending
    pub ascending: bool,
}

impl SortOrder {
    /// Create ascending sort order
    pub fn asc(column: impl Into<String>) -> Self {
        Self {
            column: column.into(),
            ascending: true,
        }
    }

    /// Create descending sort order
    pub fn desc(column: impl Into<String>) -> Self {
        Self {
            column: column.into(),
            ascending: false,
        }
    }
}

/// Represents a node in the join tree
/// Can be a single table or a joined result of multiple tables
#[derive(Debug, Clone)]
pub struct JoinNode {
    /// Tables included in this node
    pub tables: FxHashSet<String>,
    /// Estimated row count after joining
    pub row_estimate: u64,
    /// Distinct count on the join columns (for further joins)
    pub distinct_estimate: u64,
    /// Cumulative cost to reach this node
    pub cumulative_cost: f64,
    /// The algorithm used to create this node (None for base tables)
    pub algorithm: Option<JoinAlgorithm>,
    /// Columns this node is sorted by (empty = unsorted)
    /// For index scans, this is the index key columns
    /// For merge joins, this is the join key columns
    pub sorted_by: Vec<SortOrder>,
}

impl JoinNode {
    /// Create a new leaf node for a single table
    pub fn leaf(table_name: String, row_count: u64, distinct_count: u64) -> Self {
        let mut tables = FxHashSet::default();
        tables.insert(table_name);
        Self {
            tables,
            row_estimate: row_count,
            distinct_estimate: distinct_count,
            cumulative_cost: 0.0,
            algorithm: None,
            sorted_by: Vec::new(),
        }
    }

    /// Create a new leaf node with sorted columns (e.g., from an index scan)
    pub fn leaf_sorted(
        table_name: String,
        row_count: u64,
        distinct_count: u64,
        sorted_by: Vec<SortOrder>,
    ) -> Self {
        let mut tables = FxHashSet::default();
        tables.insert(table_name);
        Self {
            tables,
            row_estimate: row_count,
            distinct_estimate: distinct_count,
            cumulative_cost: 0.0,
            algorithm: None,
            sorted_by,
        }
    }

    /// Create a joined node from two nodes
    pub fn joined(
        left: &JoinNode,
        right: &JoinNode,
        output_rows: u64,
        output_distinct: u64,
        join_cost: f64,
        algorithm: JoinAlgorithm,
    ) -> Self {
        let mut tables = left.tables.clone();
        tables.extend(right.tables.iter().cloned());

        // Determine output sort order based on join algorithm
        let sorted_by = match &algorithm {
            JoinAlgorithm::MergeJoin { .. } => {
                // Merge join preserves left sort order (since we merge on sorted inputs)
                left.sorted_by.clone()
            }
            JoinAlgorithm::NestedLoop { .. } => {
                // Nested loop preserves outer (left) sort order
                left.sorted_by.clone()
            }
            _ => {
                // Hash join does not preserve sort order
                Vec::new()
            }
        };

        Self {
            tables,
            row_estimate: output_rows,
            distinct_estimate: output_distinct,
            cumulative_cost: left.cumulative_cost + right.cumulative_cost + join_cost,
            algorithm: Some(algorithm),
            sorted_by,
        }
    }

    /// Check if this node contains the given table
    pub fn contains(&self, table: &str) -> bool {
        self.tables.contains(table)
    }

    /// Check if this node is sorted by the given column (any order)
    pub fn is_sorted_by(&self, column: &str) -> bool {
        self.sorted_by
            .first()
            .map(|s| {
                s.column == column
                    || s.column.ends_with(&format!(".{}", column))
                    || column.ends_with(&format!(".{}", s.column))
            })
            .unwrap_or(false)
    }

    /// Check if this node is sorted by a column from the given table
    pub fn is_sorted_by_table_column(&self, table: &str, column: &str) -> bool {
        let full_name = format!("{}.{}", table, column);
        self.sorted_by
            .first()
            .map(|s| s.column == full_name || s.column == column)
            .unwrap_or(false)
    }
}

/// A step in the join plan
#[derive(Debug, Clone)]
pub struct JoinStep {
    /// Tables being joined in this step
    pub left_tables: FxHashSet<String>,
    pub right_tables: FxHashSet<String>,
    /// The join algorithm to use
    pub algorithm: JoinAlgorithm,
    /// The join condition for this step
    pub condition: Option<JoinCondition>,
    /// Estimated cost of this step
    pub cost: PlanCost,
    /// Estimated output rows
    pub output_rows: u64,
}

/// The result of join ordering optimization
#[derive(Debug, Clone)]
pub struct JoinPlan {
    /// The steps to execute joins, in order
    pub steps: Vec<JoinStep>,
    /// Total estimated cost
    pub total_cost: f64,
    /// Final output row estimate
    pub output_rows: u64,
}

impl JoinPlan {
    /// Create an empty plan
    pub fn empty() -> Self {
        Self {
            steps: Vec::new(),
            total_cost: 0.0,
            output_rows: 0,
        }
    }

    /// Create a single-join plan
    pub fn single(step: JoinStep) -> Self {
        let total_cost = step.cost.total;
        let output_rows = step.output_rows;
        Self {
            steps: vec![step],
            total_cost,
            output_rows,
        }
    }
}

/// Maximum number of tables for DP optimization (2^n subsets)
const DP_TABLE_LIMIT: usize = 10;

/// Cost penalty for cross products (Cartesian joins)
const CROSS_PRODUCT_PENALTY: f64 = 1000.0;

/// Memory limit for hash join build side (in rows, for edge computing)
const HASH_JOIN_MEMORY_LIMIT: u64 = 100_000;

/// DP memoization entry
#[derive(Debug, Clone)]
struct DpEntry {
    /// Total cost to compute this subset
    cost: f64,
    /// Estimated output rows
    rows: u64,
    /// Estimated distinct count (for further joins)
    distinct: u64,
    /// Left subset mask (0 for base tables)
    left_mask: usize,
    /// Right subset mask (0 for base tables)
    right_mask: usize,
    /// Join algorithm used (None for base tables)
    algorithm: Option<JoinAlgorithm>,
    /// Columns this subset is sorted by (for merge join optimization)
    sorted_by: Vec<SortOrder>,
}

/// Result of join cost computation
#[derive(Debug)]
struct JoinCostResult {
    cost: f64,
    rows: u64,
    distinct: u64,
    algorithm: JoinAlgorithm,
    /// Output sort order after join
    sorted_by: Vec<SortOrder>,
}

/// Join optimizer using Dynamic Programming (optimal) or greedy (fallback)
pub struct JoinOptimizer {
    /// Cost estimator for join cost calculations
    cost_estimator: CostEstimator,
    /// Table statistics (table_name -> TableStats)
    table_stats: FxHashMap<String, TableStats>,
    /// Column distinct counts (table_name.column_name -> distinct_count)
    column_distinct: FxHashMap<String, u64>,
    /// Available memory for hash tables (rows, for edge-aware planning)
    memory_budget: u64,
    /// Sorted tables: table_name -> columns the input is sorted by
    /// This is set when the input comes from an index scan or pre-sorted source
    sorted_inputs: FxHashMap<String, Vec<SortOrder>>,
}

impl JoinOptimizer {
    /// Create a new join optimizer
    pub fn new(cost_estimator: CostEstimator) -> Self {
        Self {
            cost_estimator,
            table_stats: FxHashMap::default(),
            column_distinct: FxHashMap::default(),
            memory_budget: HASH_JOIN_MEMORY_LIMIT,
            sorted_inputs: FxHashMap::default(),
        }
    }

    /// Create optimizer with custom memory budget (for edge devices)
    pub fn with_memory_budget(cost_estimator: CostEstimator, memory_budget: u64) -> Self {
        Self {
            cost_estimator,
            table_stats: FxHashMap::default(),
            column_distinct: FxHashMap::default(),
            memory_budget,
            sorted_inputs: FxHashMap::default(),
        }
    }

    /// Add table statistics
    pub fn add_table_stats(&mut self, table_name: &str, stats: TableStats) {
        self.table_stats.insert(table_name.to_string(), stats);
    }

    /// Add column distinct count
    pub fn add_column_distinct(&mut self, table_name: &str, column_name: &str, distinct: u64) {
        let key = format!("{}.{}", table_name, column_name);
        self.column_distinct.insert(key, distinct);
    }

    /// Mark a table's input as sorted by specific columns
    /// This is called when the scan uses an index that provides sorted output
    pub fn add_sorted_input(&mut self, table_name: &str, sorted_by: Vec<SortOrder>) {
        self.sorted_inputs.insert(table_name.to_string(), sorted_by);
    }

    /// Check if a table's input is sorted
    pub fn is_input_sorted(&self, table_name: &str) -> bool {
        self.sorted_inputs.contains_key(table_name)
    }

    /// Get the sort order for a table's input
    pub fn get_input_sort_order(&self, table_name: &str) -> Option<&Vec<SortOrder>> {
        self.sorted_inputs.get(table_name)
    }

    /// Check if input is sorted by a specific column
    #[allow(dead_code)]
    fn is_sorted_by_column(&self, tables: &FxHashSet<String>, column: &str) -> bool {
        for table in tables {
            if let Some(sorted_cols) = self.sorted_inputs.get(table) {
                if sorted_cols
                    .first()
                    .map(|s| s.column == column || s.column.ends_with(&format!(".{}", column)))
                    .unwrap_or(false)
                {
                    return true;
                }
            }
        }
        false
    }

    /// Get table row count (with fallback)
    fn get_row_count(&self, table_name: &str) -> u64 {
        self.table_stats
            .get(table_name)
            .map(|s| s.row_count)
            .unwrap_or(1000) // Default estimate
    }

    /// Get table stats (with fallback)
    fn get_table_stats(&self, table_name: &str) -> TableStats {
        self.table_stats
            .get(table_name)
            .cloned()
            .unwrap_or_else(|| TableStats {
                table_name: table_name.to_string(),
                row_count: 1000,
                page_count: 10,
                avg_row_size: 100,
            })
    }

    /// Get column distinct count (with fallback)
    fn get_distinct_count(&self, table_name: &str, column_name: &str) -> u64 {
        let key = format!("{}.{}", table_name, column_name);
        self.column_distinct.get(&key).copied().unwrap_or_else(|| {
            // Default: assume 10% of rows are distinct
            (self.get_row_count(table_name) / 10).max(1)
        })
    }

    /// Optimize join order for a set of tables with given join conditions
    ///
    /// Uses **Dynamic Programming** for ≤10 tables (optimal)
    /// Falls back to **greedy** for larger joins (fast but suboptimal)
    pub fn optimize_join_order(&self, tables: &[&str], conditions: &[JoinCondition]) -> JoinPlan {
        if tables.is_empty() {
            return JoinPlan::empty();
        }

        if tables.len() == 1 {
            return JoinPlan::empty();
        }

        // Use DP for small joins (optimal), greedy for large (practical)
        if tables.len() <= DP_TABLE_LIMIT {
            self.optimize_dp(tables, conditions)
        } else {
            self.optimize_greedy(tables, conditions)
        }
    }

    // =========================================================================
    // DYNAMIC PROGRAMMING JOIN ORDERING (OPTIMAL)
    // =========================================================================

    /// Dynamic Programming join ordering - explores ALL possible orderings
    ///
    /// This is the "gold standard" algorithm used by PostgreSQL, Oracle, etc.
    /// Complexity: O(3^n) time, O(2^n) space where n = number of tables
    fn optimize_dp(&self, tables: &[&str], conditions: &[JoinCondition]) -> JoinPlan {
        let n = tables.len();

        // Map table names to indices for bitmask operations
        let _table_to_idx: FxHashMap<&str, usize> =
            tables.iter().enumerate().map(|(i, &t)| (t, i)).collect();
        let idx_to_table: Vec<&str> = tables.to_vec();

        // DP table: dp[mask] = (best_cost, best_plan, row_estimate, distinct_estimate)
        // mask is a bitmask of included tables
        let num_subsets = 1 << n;
        let mut dp: Vec<Option<DpEntry>> = vec![None; num_subsets];

        // Base case: single tables (mask with single bit set)
        for (i, &table) in tables.iter().enumerate() {
            let mask = 1 << i;
            let row_count = self.get_row_count(table);
            let distinct = conditions
                .iter()
                .find(|c| c.involves(table))
                .map(|c| {
                    if c.left_table == table {
                        self.get_distinct_count(table, &c.left_column)
                    } else {
                        self.get_distinct_count(table, &c.right_column)
                    }
                })
                .unwrap_or_else(|| (row_count / 10).max(1));

            // Get sorted columns for this table (from index scans)
            let sorted_by = self.sorted_inputs.get(table).cloned().unwrap_or_default();

            dp[mask] = Some(DpEntry {
                cost: 0.0,
                rows: row_count,
                distinct,
                left_mask: 0,
                right_mask: 0,
                algorithm: None,
                sorted_by,
            });
        }

        // Fill DP table in order of subset size (bottom-up)
        for size in 2..=n {
            for mask in 1..num_subsets {
                if (mask as u32).count_ones() != size as u32 {
                    continue;
                }

                // Try all ways to partition this subset into two non-empty parts
                let mut best: Option<DpEntry> = None;

                // Iterate over all proper subsets of mask
                let mut left = mask;
                while left > 0 {
                    left = (left - 1) & mask;
                    if left == 0 || left == mask {
                        continue;
                    }
                    let right = mask ^ left;

                    // Both subsets must be computable
                    let (left_entry, right_entry) = match (&dp[left], &dp[right]) {
                        (Some(l), Some(r)) => (l, r),
                        _ => continue,
                    };

                    // Check if there's a valid join condition connecting left and right
                    let left_tables: FxHashSet<String> = (0..n)
                        .filter(|i| left & (1 << i) != 0)
                        .map(|i| idx_to_table[i].to_string())
                        .collect();
                    let right_tables: FxHashSet<String> = (0..n)
                        .filter(|i| right & (1 << i) != 0)
                        .map(|i| idx_to_table[i].to_string())
                        .collect();

                    let connecting = conditions
                        .iter()
                        .find(|c| c.connects(&left_tables, &right_tables));

                    let has_equality = connecting.map(|c| c.is_equality).unwrap_or(false);

                    // Compute join cost (with sorted input detection)
                    let join_result = self.compute_join_cost(
                        left_entry,
                        right_entry,
                        has_equality,
                        connecting.is_none(), // cross product?
                        connecting,           // pass condition for sorted detection
                    );

                    let total_cost = left_entry.cost + right_entry.cost + join_result.cost;

                    // Update best if this is better
                    if best.is_none() || total_cost < best.as_ref().unwrap().cost {
                        best = Some(DpEntry {
                            cost: total_cost,
                            rows: join_result.rows,
                            distinct: join_result.distinct,
                            left_mask: left,
                            right_mask: right,
                            algorithm: Some(join_result.algorithm),
                            sorted_by: join_result.sorted_by,
                        });
                    }
                }

                dp[mask] = best;
            }
        }

        // Reconstruct plan from DP table
        let full_mask = (1 << n) - 1;
        match &dp[full_mask] {
            Some(entry) => self.reconstruct_plan(entry, &dp, &idx_to_table, conditions),
            None => JoinPlan::empty(),
        }
    }

    /// Compute join cost for DP
    fn compute_join_cost(
        &self,
        left: &DpEntry,
        right: &DpEntry,
        has_equality: bool,
        is_cross_product: bool,
        condition: Option<&JoinCondition>,
    ) -> JoinCostResult {
        let left_rows = left.rows;
        let right_rows = right.rows;

        // Create synthetic table stats for cost estimation
        let left_stats = TableStats {
            table_name: "left".to_string(),
            row_count: left_rows,
            page_count: (left_rows / 100).max(1),
            avg_row_size: 100,
        };
        let right_stats = TableStats {
            table_name: "right".to_string(),
            row_count: right_rows,
            page_count: (right_rows / 100).max(1),
            avg_row_size: 100,
        };

        let join_stats = JoinStats {
            left_stats,
            right_stats,
            left_distinct: left.distinct,
            right_distinct: right.distinct,
        };

        // Detect if inputs are sorted on the join column
        let (left_sorted, right_sorted) = if let Some(cond) = condition {
            // Check if left side is sorted by its join column
            let left_is_sorted = left.sorted_by.first().is_some_and(|s| {
                s.column == cond.left_column
                    || s.column.ends_with(&format!(".{}", cond.left_column))
            });
            // Check if right side is sorted by its join column
            let right_is_sorted = right.sorted_by.first().is_some_and(|s| {
                s.column == cond.right_column
                    || s.column.ends_with(&format!(".{}", cond.right_column))
            });
            (left_is_sorted, right_is_sorted)
        } else {
            (false, false)
        };

        // Use extended algorithm selection that considers sorted inputs
        let (algorithm, cost) = self.cost_estimator.choose_join_algorithm_extended(
            &join_stats,
            has_equality,
            left_sorted,
            right_sorted,
        );

        // Apply cross-product penalty
        let mut final_cost = cost.total;
        if is_cross_product {
            final_cost *= CROSS_PRODUCT_PENALTY;
        }

        // Edge-aware: penalize hash joins that exceed memory budget
        if let JoinAlgorithm::HashJoin { build_rows, .. } = &algorithm {
            if *build_rows > self.memory_budget {
                // Memory overflow - add spill cost
                let overflow_ratio = *build_rows as f64 / self.memory_budget as f64;
                final_cost *= 1.0 + overflow_ratio.ln().max(0.0);
            }
        }

        // Estimate output cardinality
        let max_distinct = left.distinct.max(right.distinct).max(1);
        let output_rows = if is_cross_product {
            // Cross product: multiply
            (left_rows as u128 * right_rows as u128).min(u64::MAX as u128) as u64
        } else {
            // Join: divide by max distinct
            (left_rows as u128 * right_rows as u128 / max_distinct as u128) as u64
        };

        let output_distinct = left.distinct.min(right.distinct).max(1);

        // Determine output sort order based on join algorithm
        let output_sorted_by = match &algorithm {
            JoinAlgorithm::MergeJoin { .. } => {
                // Merge join produces output sorted by the join key (left side)
                if let Some(cond) = condition {
                    vec![SortOrder::asc(cond.left_column.clone())]
                } else {
                    left.sorted_by.clone()
                }
            }
            JoinAlgorithm::NestedLoop { .. } => {
                // Nested loop preserves outer (left) sort order
                left.sorted_by.clone()
            }
            _ => {
                // Hash join does not preserve sort order
                Vec::new()
            }
        };

        JoinCostResult {
            cost: final_cost,
            rows: output_rows.max(1),
            distinct: output_distinct,
            algorithm,
            sorted_by: output_sorted_by,
        }
    }

    /// Reconstruct join plan from DP table
    fn reconstruct_plan(
        &self,
        entry: &DpEntry,
        dp: &[Option<DpEntry>],
        idx_to_table: &[&str],
        conditions: &[JoinCondition],
    ) -> JoinPlan {
        let mut steps = Vec::new();
        self.collect_steps(entry, dp, idx_to_table, conditions, &mut steps);

        let total_cost = entry.cost;
        let output_rows = entry.rows;

        JoinPlan {
            steps,
            total_cost,
            output_rows,
        }
    }

    /// Recursively collect join steps from DP
    fn collect_steps(
        &self,
        entry: &DpEntry,
        dp: &[Option<DpEntry>],
        idx_to_table: &[&str],
        conditions: &[JoinCondition],
        steps: &mut Vec<JoinStep>,
    ) {
        if entry.left_mask == 0 || entry.right_mask == 0 {
            // Base case: single table
            return;
        }

        // Recurse into children first (post-order)
        if let Some(left_entry) = &dp[entry.left_mask] {
            self.collect_steps(left_entry, dp, idx_to_table, conditions, steps);
        }
        if let Some(right_entry) = &dp[entry.right_mask] {
            self.collect_steps(right_entry, dp, idx_to_table, conditions, steps);
        }

        // Build table sets
        let n = idx_to_table.len();
        let left_tables: FxHashSet<String> = (0..n)
            .filter(|i| entry.left_mask & (1 << i) != 0)
            .map(|i| idx_to_table[i].to_string())
            .collect();
        let right_tables: FxHashSet<String> = (0..n)
            .filter(|i| entry.right_mask & (1 << i) != 0)
            .map(|i| idx_to_table[i].to_string())
            .collect();

        // Find connecting condition
        let condition = conditions
            .iter()
            .find(|c| c.connects(&left_tables, &right_tables))
            .cloned();

        // Get child info
        let left_entry = dp[entry.left_mask].as_ref().unwrap();
        let right_entry = dp[entry.right_mask].as_ref().unwrap();

        let step_cost = entry.cost - left_entry.cost - right_entry.cost;

        let step = JoinStep {
            left_tables,
            right_tables,
            algorithm: entry
                .algorithm
                .clone()
                .unwrap_or(JoinAlgorithm::NestedLoop {
                    outer_rows: left_entry.rows,
                    inner_rows: right_entry.rows,
                }),
            condition,
            cost: PlanCost::new(
                step_cost * 0.3,
                step_cost * 0.7,
                0,
                entry.rows,
                String::new(),
            ),
            output_rows: entry.rows,
        };

        steps.push(step);
    }

    // =========================================================================
    // GREEDY FALLBACK (for large joins)
    // =========================================================================

    /// Greedy join ordering - fast but suboptimal
    fn optimize_greedy(&self, tables: &[&str], conditions: &[JoinCondition]) -> JoinPlan {
        // Initialize nodes: each table is a leaf node
        let mut nodes: Vec<JoinNode> = tables
            .iter()
            .map(|&t| {
                let row_count = self.get_row_count(t);
                let distinct = conditions
                    .iter()
                    .find(|c| c.involves(t))
                    .map(|c| {
                        if c.left_table == t {
                            self.get_distinct_count(t, &c.left_column)
                        } else {
                            self.get_distinct_count(t, &c.right_column)
                        }
                    })
                    .unwrap_or_else(|| (row_count / 10).max(1));
                JoinNode::leaf(t.to_string(), row_count, distinct)
            })
            .collect();

        let mut steps = Vec::new();

        // Greedy: repeatedly join the two cheapest nodes
        while nodes.len() > 1 {
            let (best_i, best_j, best_step, best_node) =
                match self.find_cheapest_join_pair(&nodes, conditions) {
                    Some(result) => result,
                    None => {
                        // No valid join found - create a cross join as fallback
                        break;
                    }
                };

            steps.push(best_step);

            // Remove joined nodes and add the merged node
            // Remove in reverse order to maintain indices
            let (i, j) = if best_i > best_j {
                (best_i, best_j)
            } else {
                (best_j, best_i)
            };
            nodes.remove(i);
            nodes.remove(j);
            nodes.push(best_node);
        }

        if steps.is_empty() {
            return JoinPlan::empty();
        }

        let total_cost = steps.iter().map(|s| s.cost.total).sum();
        let output_rows = steps.last().map(|s| s.output_rows).unwrap_or(0);

        JoinPlan {
            steps,
            total_cost,
            output_rows,
        }
    }

    /// Find the cheapest pair of nodes to join
    fn find_cheapest_join_pair(
        &self,
        nodes: &[JoinNode],
        conditions: &[JoinCondition],
    ) -> Option<(usize, usize, JoinStep, JoinNode)> {
        let mut best: Option<(usize, usize, JoinStep, JoinNode, f64)> = None;

        for i in 0..nodes.len() {
            for j in (i + 1)..nodes.len() {
                // Find a condition that connects these two nodes
                let connecting_condition = conditions
                    .iter()
                    .find(|c| c.connects(&nodes[i].tables, &nodes[j].tables));

                // If no direct connection, skip this pair (don't create cross joins)
                let condition = match connecting_condition {
                    Some(c) => c,
                    None => continue,
                };

                // Calculate join cost
                let (step, joined_node, cost) =
                    self.estimate_join_pair(&nodes[i], &nodes[j], Some(condition.clone()));

                // Track the cheapest join
                let total_cost = nodes[i].cumulative_cost + nodes[j].cumulative_cost + cost;
                if best.is_none() || total_cost < best.as_ref().unwrap().4 {
                    best = Some((i, j, step, joined_node, total_cost));
                }
            }
        }

        best.map(|(i, j, step, node, _)| (i, j, step, node))
    }

    /// Estimate the cost of joining two nodes
    fn estimate_join_pair(
        &self,
        left: &JoinNode,
        right: &JoinNode,
        condition: Option<JoinCondition>,
    ) -> (JoinStep, JoinNode, f64) {
        // Create table stats for the nodes
        let left_stats = TableStats {
            table_name: left.tables.iter().next().cloned().unwrap_or_default(),
            row_count: left.row_estimate,
            page_count: (left.row_estimate / 100).max(1),
            avg_row_size: 100,
        };
        let right_stats = TableStats {
            table_name: right.tables.iter().next().cloned().unwrap_or_default(),
            row_count: right.row_estimate,
            page_count: (right.row_estimate / 100).max(1),
            avg_row_size: 100,
        };

        let join_stats = JoinStats {
            left_stats,
            right_stats,
            left_distinct: left.distinct_estimate,
            right_distinct: right.distinct_estimate,
        };

        let has_equality = condition.as_ref().map(|c| c.is_equality).unwrap_or(false);
        let (algorithm, cost) = self
            .cost_estimator
            .choose_join_algorithm(&join_stats, has_equality);

        // Estimate output rows using join cardinality formula
        let max_distinct = left.distinct_estimate.max(right.distinct_estimate).max(1);
        let output_rows =
            (left.row_estimate as u128 * right.row_estimate as u128 / max_distinct as u128) as u64;

        // Estimate output distinct (for further joins)
        // Conservative estimate: minimum of input distincts
        let output_distinct = left.distinct_estimate.min(right.distinct_estimate).max(1);

        let step = JoinStep {
            left_tables: left.tables.clone(),
            right_tables: right.tables.clone(),
            algorithm: algorithm.clone(),
            condition,
            cost: cost.clone(),
            output_rows,
        };

        let joined_node = JoinNode::joined(
            left,
            right,
            output_rows,
            output_distinct,
            cost.total,
            algorithm,
        );

        (step, joined_node, cost.total)
    }

    /// Get a simple recommendation for a two-table join
    /// This is useful when the full optimizer isn't needed
    pub fn recommend_join_algorithm(
        &self,
        left_table: &str,
        right_table: &str,
        has_equality_keys: bool,
    ) -> (JoinAlgorithm, PlanCost) {
        let left_stats = self.get_table_stats(left_table);
        let right_stats = self.get_table_stats(right_table);

        let left_distinct = (left_stats.row_count / 10).max(1);
        let right_distinct = (right_stats.row_count / 10).max(1);

        let join_stats = JoinStats {
            left_stats,
            right_stats,
            left_distinct,
            right_distinct,
        };

        self.cost_estimator
            .choose_join_algorithm(&join_stats, has_equality_keys)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::optimizer::cost::BuildSide;

    fn create_optimizer_with_stats() -> JoinOptimizer {
        let mut optimizer = JoinOptimizer::new(CostEstimator::new());

        // Add some test table stats
        optimizer.add_table_stats(
            "users",
            TableStats {
                table_name: "users".to_string(),
                row_count: 1000,
                page_count: 10,
                avg_row_size: 100,
            },
        );
        optimizer.add_table_stats(
            "orders",
            TableStats {
                table_name: "orders".to_string(),
                row_count: 10000,
                page_count: 100,
                avg_row_size: 200,
            },
        );
        optimizer.add_table_stats(
            "products",
            TableStats {
                table_name: "products".to_string(),
                row_count: 500,
                page_count: 5,
                avg_row_size: 150,
            },
        );
        optimizer.add_table_stats(
            "order_items",
            TableStats {
                table_name: "order_items".to_string(),
                row_count: 50000,
                page_count: 500,
                avg_row_size: 50,
            },
        );

        // Add column distinct counts
        optimizer.add_column_distinct("users", "id", 1000);
        optimizer.add_column_distinct("orders", "user_id", 1000);
        optimizer.add_column_distinct("orders", "id", 10000);
        optimizer.add_column_distinct("products", "id", 500);
        optimizer.add_column_distinct("order_items", "order_id", 10000);
        optimizer.add_column_distinct("order_items", "product_id", 500);

        optimizer
    }

    #[test]
    fn test_join_condition_connects() {
        let condition = JoinCondition::new_equality(
            "users".to_string(),
            "id".to_string(),
            "orders".to_string(),
            "user_id".to_string(),
        );

        let mut tables1 = FxHashSet::default();
        tables1.insert("users".to_string());

        let mut tables2 = FxHashSet::default();
        tables2.insert("orders".to_string());

        assert!(condition.connects(&tables1, &tables2));
        assert!(condition.connects(&tables2, &tables1));

        let mut tables3 = FxHashSet::default();
        tables3.insert("products".to_string());
        assert!(!condition.connects(&tables1, &tables3));
    }

    #[test]
    fn test_join_node_leaf() {
        let node = JoinNode::leaf("users".to_string(), 1000, 1000);
        assert!(node.contains("users"));
        assert!(!node.contains("orders"));
        assert_eq!(node.row_estimate, 1000);
        assert_eq!(node.cumulative_cost, 0.0);
    }

    #[test]
    fn test_two_table_join_optimization() {
        let optimizer = create_optimizer_with_stats();

        let conditions = vec![JoinCondition::new_equality(
            "users".to_string(),
            "id".to_string(),
            "orders".to_string(),
            "user_id".to_string(),
        )];

        let plan = optimizer.optimize_join_order(&["users", "orders"], &conditions);

        assert_eq!(plan.steps.len(), 1);
        assert!(plan.total_cost > 0.0);

        // With equality keys and large tables, should use hash join
        let step = &plan.steps[0];
        assert!(step.algorithm.is_hash_join());
    }

    #[test]
    fn test_three_table_join_optimization() {
        let optimizer = create_optimizer_with_stats();

        // users JOIN orders ON users.id = orders.user_id
        // JOIN products ON orders.product_id = products.id
        // (Note: orders doesn't have product_id directly, but for testing)
        let conditions = vec![
            JoinCondition::new_equality(
                "users".to_string(),
                "id".to_string(),
                "orders".to_string(),
                "user_id".to_string(),
            ),
            JoinCondition::new_equality(
                "orders".to_string(),
                "id".to_string(),
                "order_items".to_string(),
                "order_id".to_string(),
            ),
        ];

        let plan = optimizer.optimize_join_order(&["users", "orders", "order_items"], &conditions);

        // Should have 2 join steps
        assert_eq!(plan.steps.len(), 2);
        assert!(plan.total_cost > 0.0);
    }

    #[test]
    fn test_recommend_join_algorithm() {
        let optimizer = create_optimizer_with_stats();

        // Large tables with equality keys -> hash join
        let (algo, cost) = optimizer.recommend_join_algorithm("orders", "users", true);
        assert!(algo.is_hash_join());
        assert!(cost.total > 0.0);

        // Without equality keys -> nested loop
        let (algo2, _) = optimizer.recommend_join_algorithm("orders", "users", false);
        assert!(algo2.is_nested_loop());
    }

    #[test]
    fn test_build_side_optimization() {
        let optimizer = create_optimizer_with_stats();

        let (algo, _) = optimizer.recommend_join_algorithm("orders", "users", true);

        if let JoinAlgorithm::HashJoin {
            build_side,
            build_rows,
            probe_rows,
        } = algo
        {
            // Should build on smaller table (users: 1000 rows)
            // and probe with larger table (orders: 10000 rows)
            assert_eq!(build_side, BuildSide::Right);
            assert_eq!(build_rows, 1000);
            assert_eq!(probe_rows, 10000);
        } else {
            panic!("Expected HashJoin");
        }
    }

    #[test]
    fn test_empty_tables() {
        let optimizer = JoinOptimizer::new(CostEstimator::new());
        let plan = optimizer.optimize_join_order(&[], &[]);
        assert!(plan.steps.is_empty());
    }

    #[test]
    fn test_single_table() {
        let optimizer = create_optimizer_with_stats();
        let plan = optimizer.optimize_join_order(&["users"], &[]);
        assert!(plan.steps.is_empty());
    }

    #[test]
    fn test_join_step_output_rows() {
        let optimizer = create_optimizer_with_stats();

        let conditions = vec![JoinCondition::new_equality(
            "users".to_string(),
            "id".to_string(),
            "orders".to_string(),
            "user_id".to_string(),
        )];

        let plan = optimizer.optimize_join_order(&["users", "orders"], &conditions);
        let step = &plan.steps[0];

        // Output rows should be estimated using join cardinality formula
        // users.id has 1000 distinct, orders.user_id has 1000 distinct
        // |users| * |orders| / max(1000, 1000) = 1000 * 10000 / 1000 = 10000
        assert_eq!(step.output_rows, 10000);
    }

    // =========================================================================
    // DYNAMIC PROGRAMMING TESTS
    // =========================================================================

    #[test]
    fn test_dp_four_table_join() {
        let optimizer = create_optimizer_with_stats();

        // A star schema: users in center, orders, products, order_items around it
        let conditions = vec![
            JoinCondition::new_equality(
                "users".to_string(),
                "id".to_string(),
                "orders".to_string(),
                "user_id".to_string(),
            ),
            JoinCondition::new_equality(
                "orders".to_string(),
                "id".to_string(),
                "order_items".to_string(),
                "order_id".to_string(),
            ),
            JoinCondition::new_equality(
                "order_items".to_string(),
                "product_id".to_string(),
                "products".to_string(),
                "id".to_string(),
            ),
        ];

        let plan = optimizer
            .optimize_join_order(&["users", "orders", "products", "order_items"], &conditions);

        // Should have 3 join steps (4 tables = 3 joins)
        assert_eq!(plan.steps.len(), 3);
        assert!(plan.total_cost > 0.0);

        // DP should produce a reasonable plan
        // The optimal order typically starts with smallest tables
        println!("DP 4-table plan cost: {}", plan.total_cost);
    }

    #[test]
    fn test_dp_finds_optimal_order() {
        // Create an optimizer with very different table sizes to verify DP finds optimal
        let mut optimizer = JoinOptimizer::new(CostEstimator::new());

        // tiny (10 rows), small (100), medium (1000), large (10000)
        optimizer.add_table_stats(
            "tiny",
            TableStats {
                table_name: "tiny".to_string(),
                row_count: 10,
                page_count: 1,
                avg_row_size: 100,
            },
        );
        optimizer.add_table_stats(
            "small",
            TableStats {
                table_name: "small".to_string(),
                row_count: 100,
                page_count: 1,
                avg_row_size: 100,
            },
        );
        optimizer.add_table_stats(
            "medium",
            TableStats {
                table_name: "medium".to_string(),
                row_count: 1000,
                page_count: 10,
                avg_row_size: 100,
            },
        );
        optimizer.add_table_stats(
            "large",
            TableStats {
                table_name: "large".to_string(),
                row_count: 10000,
                page_count: 100,
                avg_row_size: 100,
            },
        );

        // Chain join: tiny - small - medium - large
        let conditions = vec![
            JoinCondition::new_equality(
                "tiny".to_string(),
                "id".to_string(),
                "small".to_string(),
                "tiny_id".to_string(),
            ),
            JoinCondition::new_equality(
                "small".to_string(),
                "id".to_string(),
                "medium".to_string(),
                "small_id".to_string(),
            ),
            JoinCondition::new_equality(
                "medium".to_string(),
                "id".to_string(),
                "large".to_string(),
                "medium_id".to_string(),
            ),
        ];

        let plan =
            optimizer.optimize_join_order(&["tiny", "small", "medium", "large"], &conditions);

        // DP should find the optimal plan
        assert_eq!(plan.steps.len(), 3);

        // The optimal strategy should join smaller tables first
        // to reduce intermediate result sizes
        println!("DP optimal plan total cost: {}", plan.total_cost);
    }

    #[test]
    fn test_dp_vs_greedy_consistency() {
        // With 2 tables, DP and greedy should produce same result
        let optimizer = create_optimizer_with_stats();

        let conditions = vec![JoinCondition::new_equality(
            "users".to_string(),
            "id".to_string(),
            "orders".to_string(),
            "user_id".to_string(),
        )];

        let dp_plan = optimizer.optimize_dp(&["users", "orders"], &conditions);
        let greedy_plan = optimizer.optimize_greedy(&["users", "orders"], &conditions);

        // Both should have exactly one join step
        assert_eq!(dp_plan.steps.len(), 1);
        assert_eq!(greedy_plan.steps.len(), 1);

        // Costs should be similar (not necessarily identical due to algorithm differences)
        let cost_ratio = dp_plan.total_cost / greedy_plan.total_cost.max(0.001);
        assert!(
            cost_ratio > 0.5 && cost_ratio < 2.0,
            "DP and greedy costs should be similar for 2 tables"
        );
    }

    #[test]
    fn test_memory_aware_planning() {
        // Test that memory-constrained optimizer penalizes large hash joins
        let mut low_memory_optimizer = JoinOptimizer::with_memory_budget(CostEstimator::new(), 100); // Very low memory

        low_memory_optimizer.add_table_stats(
            "big",
            TableStats {
                table_name: "big".to_string(),
                row_count: 100000,
                page_count: 1000,
                avg_row_size: 100,
            },
        );
        low_memory_optimizer.add_table_stats(
            "small",
            TableStats {
                table_name: "small".to_string(),
                row_count: 10,
                page_count: 1,
                avg_row_size: 100,
            },
        );

        let conditions = vec![JoinCondition::new_equality(
            "big".to_string(),
            "id".to_string(),
            "small".to_string(),
            "big_id".to_string(),
        )];

        let plan = low_memory_optimizer.optimize_join_order(&["big", "small"], &conditions);

        // Should still produce a valid plan
        assert_eq!(plan.steps.len(), 1);

        // The cost should be inflated due to memory overflow
        println!(
            "Memory-constrained plan cost: {} (includes spill penalty)",
            plan.total_cost
        );
    }

    #[test]
    fn test_cross_product_penalty() {
        let optimizer = create_optimizer_with_stats();

        // No join condition = cross product
        let plan = optimizer.optimize_join_order(&["users", "products"], &[]);

        // Should still produce a plan but with high cost
        // (may be empty if no conditions connect tables in DP)
        println!("Cross product plan: {:?}", plan);
    }

    // =========================================================================
    // SORTED INPUT DETECTION TESTS
    // =========================================================================

    #[test]
    fn test_sorted_input_detection() {
        let mut optimizer = create_optimizer_with_stats();

        // Mark users as sorted by 'id' (e.g., from index scan)
        optimizer.add_sorted_input("users", vec![SortOrder::asc("id")]);

        // Mark orders as sorted by 'user_id'
        optimizer.add_sorted_input("orders", vec![SortOrder::asc("user_id")]);

        // Join on users.id = orders.user_id
        let conditions = vec![JoinCondition::new_equality(
            "users".to_string(),
            "id".to_string(),
            "orders".to_string(),
            "user_id".to_string(),
        )];

        let plan = optimizer.optimize_join_order(&["users", "orders"], &conditions);
        assert_eq!(plan.steps.len(), 1);

        // With both inputs sorted on join columns, merge join should be chosen
        let step = &plan.steps[0];
        assert!(
            step.algorithm.is_merge_join(),
            "Expected merge join when both inputs are sorted, got {:?}",
            step.algorithm
        );

        // Verify the merge join knows inputs are sorted
        if let JoinAlgorithm::MergeJoin {
            left_sorted,
            right_sorted,
            ..
        } = &step.algorithm
        {
            assert!(*left_sorted, "Left side should be detected as sorted");
            assert!(*right_sorted, "Right side should be detected as sorted");
        }
    }

    #[test]
    fn test_unsorted_vs_sorted_cost() {
        let unsorted_optimizer = create_optimizer_with_stats();
        let mut sorted_optimizer = create_optimizer_with_stats();

        // Mark inputs as sorted in one optimizer
        sorted_optimizer.add_sorted_input("users", vec![SortOrder::asc("id")]);
        sorted_optimizer.add_sorted_input("orders", vec![SortOrder::asc("user_id")]);

        let conditions = vec![JoinCondition::new_equality(
            "users".to_string(),
            "id".to_string(),
            "orders".to_string(),
            "user_id".to_string(),
        )];

        let unsorted_plan =
            unsorted_optimizer.optimize_join_order(&["users", "orders"], &conditions);
        let sorted_plan = sorted_optimizer.optimize_join_order(&["users", "orders"], &conditions);

        // Both should produce a plan
        assert_eq!(unsorted_plan.steps.len(), 1);
        assert_eq!(sorted_plan.steps.len(), 1);

        // Sorted input should have lower or equal cost (no sort needed for merge)
        println!(
            "Unsorted cost: {}, Sorted cost: {}",
            unsorted_plan.total_cost, sorted_plan.total_cost
        );

        // When both inputs are sorted, merge join avoids sort cost
        if sorted_plan.steps[0].algorithm.is_merge_join() {
            // Merge join on sorted data should be cheaper than hash join
            // (The exact cost depends on table sizes, but we can verify it's reasonable)
            assert!(
                sorted_plan.total_cost <= unsorted_plan.total_cost * 1.5,
                "Sorted merge join should not be much more expensive than unsorted"
            );
        }
    }

    #[test]
    fn test_partial_sorted_input() {
        let mut optimizer = create_optimizer_with_stats();

        // Only mark one side as sorted
        optimizer.add_sorted_input("users", vec![SortOrder::asc("id")]);
        // orders is NOT sorted

        let conditions = vec![JoinCondition::new_equality(
            "users".to_string(),
            "id".to_string(),
            "orders".to_string(),
            "user_id".to_string(),
        )];

        let plan = optimizer.optimize_join_order(&["users", "orders"], &conditions);
        assert_eq!(plan.steps.len(), 1);

        // With only one side sorted, hash join is typically still preferred
        // (or merge join if the sort cost for one side is acceptable)
        let step = &plan.steps[0];
        println!(
            "Partial sorted plan chose: {:?} with cost {}",
            step.algorithm, plan.total_cost
        );
    }

    #[test]
    fn test_sort_order_preserved_through_merge_join() {
        let mut optimizer = create_optimizer_with_stats();

        // Add more tables for multi-way join
        optimizer.add_table_stats(
            "customers",
            TableStats {
                table_name: "customers".to_string(),
                row_count: 500,
                page_count: 5,
                avg_row_size: 100,
            },
        );
        optimizer.add_column_distinct("customers", "id", 500);
        optimizer.add_column_distinct("orders", "customer_id", 500);

        // Mark both sides as sorted
        optimizer.add_sorted_input("customers", vec![SortOrder::asc("id")]);
        optimizer.add_sorted_input("orders", vec![SortOrder::asc("customer_id")]);

        let conditions = vec![JoinCondition::new_equality(
            "customers".to_string(),
            "id".to_string(),
            "orders".to_string(),
            "customer_id".to_string(),
        )];

        let plan = optimizer.optimize_join_order(&["customers", "orders"], &conditions);
        assert_eq!(plan.steps.len(), 1);

        // Verify plan was created successfully
        assert!(plan.output_rows > 0);
    }

    #[test]
    fn test_node_sorted_by_detection() {
        // Test the JoinNode.is_sorted_by helper
        let node =
            JoinNode::leaf_sorted("users".to_string(), 1000, 1000, vec![SortOrder::asc("id")]);

        assert!(node.is_sorted_by("id"));
        assert!(node.is_sorted_by("users.id")); // Should match qualified names too
        assert!(!node.is_sorted_by("name"));
        assert!(!node.is_sorted_by("other_column"));
    }
}