aletheiadb 0.1.0

A high-performance bi-temporal graph database for LLM integration
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
//! Operation Reordering Optimization
//!
//! Reorders operations based on estimated costs and selectivity to minimize
//! overall query execution cost.
//!
//! # Optimization Strategy
//!
//! This rule applies three main optimizations:
//!
//! 1. **Filter Reordering**: Push more selective filters deeper into the plan (closer to the data source).
//!    This reduces the number of rows that subsequent operations need to process.
//! 2. **Join Reordering**: Ensure the smaller relation is on the left side of a hash join (build side).
//!    This minimizes the memory footprint of the hash table.
//! 3. **Cost-Based Ordering**: Use cardinality estimates to choose the cheapest operation sequence.
//!
//! # Example: Join Optimization
//!
//! ```text
//! BEFORE: Large Table (Left) JOIN Small Table (Right)
//!         (Requires building hash table on Large Table)
//!
//!         Join
//!        /    \
//!   Large      Small
//!
//! AFTER:  Small Table (Left) JOIN Large Table (Right)
//!         (Builds hash table on Small Table - faster & less memory)
//!
//!         Join
//!        /    \
//!   Small      Large
//! ```

use crate::core::error::Result;
use crate::query::ir::Predicate;
use crate::query::plan::{BinaryOp, LogicalOp, LogicalPlan, ScanOp, UnaryOp};

use super::{OptimizationRule, Statistics};

/// Default cardinality estimate for node scans without statistics.
const DEFAULT_NODE_SCAN_CARDINALITY: usize = 1000;

/// Default cardinality estimate for edge scans without statistics.
const DEFAULT_EDGE_SCAN_CARDINALITY: usize = 1000;

/// Default cardinality estimate for property-indexed scans (~10% of full scan).
const DEFAULT_PROPERTY_SCAN_CARDINALITY: usize = 100;

/// Default selectivity estimate for filter predicates (10%).
const DEFAULT_FILTER_SELECTIVITY: f64 = 0.1;

/// Default selectivity for join operations (10% of cross product).
const DEFAULT_JOIN_SELECTIVITY: f64 = 0.1;

// Selectivity estimates for different predicate types
// (0.0 = filters everything, 1.0 = filters nothing)
/// Selectivity for `IS NULL` checks (0.1). Assumes nulls are relatively rare.
const NULL_CHECK_SELECTIVITY: f64 = 0.1;
/// Selectivity for existence checks (0.1). Assumes checking for a specific property filters well.
const EXISTENCE_CHECK_SELECTIVITY: f64 = 0.1;
/// Selectivity for `IN` predicates (0.15).
const IN_PREDICATE_SELECTIVITY: f64 = 0.15;
/// Selectivity for string matching (Contains/StartsWith/EndsWith) (0.2).
const STRING_PREDICATE_SELECTIVITY: f64 = 0.2;
/// Selectivity for range predicates (Gt/Lt/Gte/Lte) (0.3).
const RANGE_PREDICATE_SELECTIVITY: f64 = 0.3;
/// Selectivity for not-equals (!=) (0.9). These typically filter very little.
const NOT_EQUALS_SELECTIVITY: f64 = 0.9;
/// Selectivity for `TRUE` (1.0). Filters nothing.
const TRUE_SELECTIVITY: f64 = 1.0;
/// Selectivity for `FALSE` (0.0). Filters everything.
const FALSE_SELECTIVITY: f64 = 0.0;
// Note: AND/OR/NOT selectivity is computed dynamically based on child predicates

/// Operation reordering optimization rule.
///
/// This rule reorders operations based on cost estimates to minimize
/// total execution time.
///
/// # Examples
///
/// ```text
/// Input Plan:
/// Filter(B) -> Filter(A) -> Scan
/// (Where Filter A is very selective, and Filter B is not)
///
/// Optimized Plan:
/// Filter(A) -> Filter(B) -> Scan
/// (Filter A is applied first, reducing rows for Filter B)
/// ```
///
/// ## Examples
///
/// ```rust
/// use aletheiadb::query::planner::rules::{OptimizationRule, OperationReordering};
/// use aletheiadb::query::planner::stats::Statistics;
/// use aletheiadb::query::plan::{LogicalPlan, LogicalOp, UnaryOp, ScanOp};
/// use aletheiadb::query::ir::{Predicate, PredicateValue};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // 1. Construct a sub-optimal plan: A very selective filter (Eq) is ABOVE a less selective one (NotEq)
/// let scan = LogicalOp::Scan(ScanOp::NodeScan {
///     label: Some("Person".into()),
///     estimated_rows: Some(100),
/// });
/// let filter_bad = LogicalOp::unary(
///     UnaryOp::Filter(Predicate::Ne {
///         key: "status".into(),
///         value: PredicateValue::String("deleted".into())
///     }),
///     scan
/// );
/// let filter_good = LogicalOp::unary(
///     UnaryOp::Filter(Predicate::Eq {
///         key: "id".into(),
///         value: PredicateValue::Int(42)
///     }),
///     filter_bad
/// );
///
/// let plan = LogicalPlan { root: filter_good, temporal_context: None, hints: Default::default() };
///
/// // 2. Apply the rule
/// let rule = OperationReordering;
/// let stats = Statistics::new();
/// let optimized_plan = rule.apply(&plan, &stats)?.unwrap(); // unwraps if `changed == true`
///
/// // 3. The rule reorders them so the more selective Filter(Eq) is applied first (deeper in the tree)
/// if let LogicalOp::Unary { op: UnaryOp::Filter(Predicate::Ne { .. }), input } = optimized_plan.root {
///     assert!(matches!(
///         *input,
///         LogicalOp::Unary { op: UnaryOp::Filter(Predicate::Eq { .. }), .. }
///     ));
/// } else {
///     panic!("Expected Ne filter at root after reordering");
/// }
/// # Ok(())
/// # }
/// ```
pub struct OperationReordering;

impl OptimizationRule for OperationReordering {
    fn name(&self) -> &str {
        "operation-reordering"
    }

    fn apply(&self, plan: &LogicalPlan, stats: &Statistics) -> Result<Option<LogicalPlan>> {
        let (new_root, changed) = self.reorder(&plan.root, stats)?;

        if changed {
            Ok(Some(LogicalPlan {
                root: new_root,
                temporal_context: plan.temporal_context.clone(),
                hints: plan.hints.clone(),
            }))
        } else {
            Ok(None)
        }
    }
}

impl OperationReordering {
    /// Recursively reorder operations in the plan tree.
    fn reorder(&self, op: &LogicalOp, stats: &Statistics) -> Result<(LogicalOp, bool)> {
        match op {
            // Check if this is a sequence of filters that can be reordered
            LogicalOp::Unary {
                op: UnaryOp::Filter(_),
                ..
            } => {
                // Collect all consecutive filters
                let (filters, base) = self.collect_filters(op);

                if filters.len() > 1 {
                    // Reorder filters by selectivity (most selective first = deepest)
                    let reordered = self.reorder_filters(filters, base, stats)?;
                    // Check if order actually changed
                    let changed = !self.filters_equal(op, &reordered);
                    Ok((reordered, changed))
                } else {
                    // Single filter or no filter - just recurse
                    if let LogicalOp::Unary {
                        op: filter_op,
                        input,
                    } = op
                    {
                        let (new_input, changed) = self.reorder(input, stats)?;
                        Ok((LogicalOp::unary(filter_op.clone(), new_input), changed))
                    } else {
                        Ok((op.clone(), false))
                    }
                }
            }

            // Join reordering: put smaller table on left (build side)
            LogicalOp::Binary {
                op:
                    BinaryOp::Join {
                        left_key,
                        right_key,
                    },
                left,
                right,
            } => {
                // First, optimize children
                let (opt_left, left_changed) = self.reorder(left, stats)?;
                let (opt_right, right_changed) = self.reorder(right, stats)?;

                // Estimate cardinalities
                let left_card = self.estimate_cardinality(&opt_left);
                let right_card = self.estimate_cardinality(&opt_right);

                // If right side is smaller, swap them
                let (final_left, final_right, final_left_key, final_right_key, swapped) =
                    if right_card < left_card {
                        (
                            opt_right,
                            opt_left,
                            right_key.clone(),
                            left_key.clone(),
                            true,
                        )
                    } else {
                        (
                            opt_left,
                            opt_right,
                            left_key.clone(),
                            right_key.clone(),
                            false,
                        )
                    };

                Ok((
                    LogicalOp::binary(
                        BinaryOp::Join {
                            left_key: final_left_key,
                            right_key: final_right_key,
                        },
                        final_left,
                        final_right,
                    ),
                    left_changed || right_changed || swapped,
                ))
            }

            // Other unary operations: recursively optimize
            LogicalOp::Unary { op, input } => {
                let (new_input, changed) = self.reorder(input, stats)?;
                Ok((LogicalOp::unary(op.clone(), new_input), changed))
            }

            // Binary operations (non-join): recursively optimize both sides
            LogicalOp::Binary { op, left, right } => {
                let (opt_left, left_changed) = self.reorder(left, stats)?;
                let (opt_right, right_changed) = self.reorder(right, stats)?;
                Ok((
                    LogicalOp::binary(op.clone(), opt_left, opt_right),
                    left_changed || right_changed,
                ))
            }

            // Leaf nodes: no change
            LogicalOp::Scan(_) | LogicalOp::Empty => Ok((op.clone(), false)),
        }
    }

    /// Collect all consecutive filters from a filter chain.
    /// Returns (filters, base) where filters are in top-to-bottom order.
    fn collect_filters(&self, op: &LogicalOp) -> (Vec<Predicate>, LogicalOp) {
        let mut filters = Vec::with_capacity(4); // âš¡ Bolt Optimization: Pre-allocate capacity for query filter lists to prevent multiple reallocations during logical plan extraction.
        let mut current = op;

        while let LogicalOp::Unary {
            op: UnaryOp::Filter(predicate),
            input,
        } = current
        {
            filters.push(predicate.clone());
            current = input;
        }

        (filters, current.clone())
    }

    /// Reorder filters by selectivity (most selective applied first = deepest in tree).
    fn reorder_filters(
        &self,
        mut filters: Vec<Predicate>,
        base: LogicalOp,
        stats: &Statistics,
    ) -> Result<LogicalOp> {
        // Sort filters by selectivity (ascending = most selective first)
        filters.sort_by(|a, b| {
            let sel_a = self.estimate_filter_selectivity(a, stats);
            let sel_b = self.estimate_filter_selectivity(b, stats);
            sel_a
                .partial_cmp(&sel_b)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Build filter chain with most selective at bottom (applied first)
        let mut result = base;
        for filter in filters {
            result = LogicalOp::unary(UnaryOp::Filter(filter), result);
        }

        Ok(result)
    }

    /// Estimate filter selectivity (0.0 = filters everything, 1.0 = filters nothing).
    ///
    /// # Heuristics
    ///
    /// - **Equality**: Uses statistical histograms if available.
    /// - **Range**: Assumes 30% selectivity (`RANGE_PREDICATE_SELECTIVITY`).
    /// - **String**: Assumes 20% selectivity (`STRING_PREDICATE_SELECTIVITY`).
    /// - **Logic**:
    ///   - `AND`: Product of probabilities (intersection).
    ///   - `OR`: 1 - Product of complement probabilities (union).
    ///   - `NOT`: 1 - Probability (complement).
    fn estimate_filter_selectivity(&self, predicate: &Predicate, stats: &Statistics) -> f64 {
        match predicate {
            Predicate::Eq { key, value } => {
                // Special case: Null checks are typically selective
                if matches!(value, crate::query::ir::PredicateValue::Null) {
                    return NULL_CHECK_SELECTIVITY;
                }

                // Convert value to string for selectivity estimation
                let value_str = match value {
                    crate::query::ir::PredicateValue::String(s) => s.clone(),
                    crate::query::ir::PredicateValue::Int(i) => i.to_string(),
                    crate::query::ir::PredicateValue::Float(f) => f.to_string(),
                    crate::query::ir::PredicateValue::Bool(b) => b.to_string(),
                    crate::query::ir::PredicateValue::Null => unreachable!(), // Already handled above
                };
                stats.estimate_selectivity(key, &value_str)
            }
            Predicate::Gt { .. }
            | Predicate::Lt { .. }
            | Predicate::Gte { .. }
            | Predicate::Lte { .. } => RANGE_PREDICATE_SELECTIVITY,
            Predicate::Contains { .. }
            | Predicate::StartsWith { .. }
            | Predicate::EndsWith { .. } => STRING_PREDICATE_SELECTIVITY,
            Predicate::And(predicates) => {
                // For AND: multiply selectivities (intersection rule)
                // Empty AND defaults to high selectivity (filters nothing)
                if predicates.is_empty() {
                    return TRUE_SELECTIVITY;
                }
                predicates
                    .iter()
                    .map(|p| self.estimate_filter_selectivity(p, stats))
                    .product()
            }
            Predicate::Or(predicates) => {
                // For OR: use union rule: 1 - (1-sel1) * (1-sel2) * ...
                // Empty OR defaults to low selectivity (filters everything)
                if predicates.is_empty() {
                    return FALSE_SELECTIVITY;
                }
                let complement_product: f64 = predicates
                    .iter()
                    .map(|p| 1.0 - self.estimate_filter_selectivity(p, stats))
                    .product();
                1.0 - complement_product
            }
            Predicate::Not(inner) => {
                // For NOT: complement of inner selectivity
                1.0 - self.estimate_filter_selectivity(inner, stats)
            }
            Predicate::Ne { .. } => NOT_EQUALS_SELECTIVITY,
            Predicate::In { .. } => IN_PREDICATE_SELECTIVITY,
            Predicate::Exists(_) | Predicate::NotExists(_) => EXISTENCE_CHECK_SELECTIVITY,
            Predicate::True => TRUE_SELECTIVITY,
            Predicate::False => FALSE_SELECTIVITY,
        }
    }

    /// Estimate cardinality of a logical operation's output.
    fn estimate_cardinality(&self, op: &LogicalOp) -> usize {
        match op {
            LogicalOp::Scan(scan) => match scan {
                ScanOp::NodeLookup(ids) => ids.len(),
                ScanOp::NodeScan { estimated_rows, .. } => {
                    estimated_rows.unwrap_or(DEFAULT_NODE_SCAN_CARDINALITY)
                }
                ScanOp::VectorSearch { k, .. } => *k,
                ScanOp::TemporalNodeLookup { node_ids, .. } => node_ids.len(),
                ScanOp::TemporalVectorSearch { k, .. } => *k,
                ScanOp::SimilarToNode { k, .. } => *k,
                ScanOp::PropertyScan { .. } => DEFAULT_PROPERTY_SCAN_CARDINALITY,
                ScanOp::EdgeScan { estimated_rows, .. } => {
                    estimated_rows.unwrap_or(DEFAULT_EDGE_SCAN_CARDINALITY)
                }
            },
            LogicalOp::Unary {
                op: UnaryOp::Filter(_),
                input,
            } => (self.estimate_cardinality(input) as f64 * DEFAULT_FILTER_SELECTIVITY) as usize,
            LogicalOp::Unary {
                op: UnaryOp::Limit(n),
                ..
            } => *n,
            LogicalOp::Unary { input, .. } => self.estimate_cardinality(input),
            LogicalOp::Binary { left, right, .. } => {
                let left_card = self.estimate_cardinality(left);
                let right_card = self.estimate_cardinality(right);
                (left_card as f64 * right_card as f64 * DEFAULT_JOIN_SELECTIVITY) as usize
            }
            LogicalOp::Empty => 0,
        }
    }

    /// Check if two filter chains are equal (same filters in same order).
    ///
    /// `LogicalOp` and `Predicate` both derive `PartialEq`, so a direct `==`
    /// comparison is sufficient.
    fn filters_equal(&self, a: &LogicalOp, b: &LogicalOp) -> bool {
        a == b
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::NodeId;
    use crate::query::ir::Predicate;
    use crate::query::plan::{BinaryOp, ScanOp, UnaryOp};

    fn test_stats() -> Statistics {
        let stats = Statistics::default();
        // Set up some statistics for testing
        stats.refresh(1000, 5000, 100, vec![], 5.0);

        // Set up property statistics for selectivity estimation
        stats.update_property_stats("rare_property", 10); // Very selective (1/10)
        stats.update_property_stats("common_property", 500); // Less selective (1/500)

        stats
    }

    // ==================== Filter Reordering Tests ====================

    #[test]
    fn test_reorder_filters_by_selectivity() {
        let rule = OperationReordering;
        let stats = test_stats();

        // Original: Filter(common) -> Filter(rare) -> Scan
        // common_property: 500 distinct values → 0.2% selectivity (MORE selective)
        // rare_property: 10 distinct values → 10% selectivity (LESS selective)
        // Should be reordered to: Filter(rare) -> Filter(common) -> Scan
        // (most selective at bottom/applied first)
        let plan = LogicalPlan::new(LogicalOp::unary(
            UnaryOp::Filter(Predicate::eq("common_property", "value")),
            LogicalOp::unary(
                UnaryOp::Filter(Predicate::eq("rare_property", "value")),
                LogicalOp::Scan(ScanOp::NodeScan {
                    label: None,
                    estimated_rows: Some(1000),
                }),
            ),
        ));

        let result = rule.apply(&plan, &stats).unwrap();
        assert!(result.is_some(), "Should reorder filters by selectivity");

        let optimized = result.unwrap();
        // The outermost filter should be the LESS selective one (rare_property)
        // The innermost/deepest filter should be the MORE selective one (common_property)
        match &optimized.root {
            LogicalOp::Unary {
                op: UnaryOp::Filter(predicate),
                input,
            } => {
                // Root filter should be rare (less selective, applied last)
                assert!(matches!(
                    predicate,
                    Predicate::Eq { key, .. } if key == "rare_property"
                ));

                // Inner filter should be common (more selective, applied first)
                match input.as_ref() {
                    LogicalOp::Unary {
                        op: UnaryOp::Filter(inner_pred),
                        ..
                    } => {
                        assert!(matches!(
                            inner_pred,
                            Predicate::Eq { key, .. } if key == "common_property"
                        ));
                    }
                    _ => panic!("Expected inner filter"),
                }
            }
            _ => panic!("Expected Filter at root"),
        }
    }

    #[test]
    fn test_no_reorder_when_filters_already_optimal() {
        let rule = OperationReordering;
        let stats = test_stats();

        // Filter(rare - less selective) -> Filter(common - more selective) -> Scan
        // Already optimal: most selective at bottom
        let plan = LogicalPlan::new(LogicalOp::unary(
            UnaryOp::Filter(Predicate::eq("rare_property", "value")),
            LogicalOp::unary(
                UnaryOp::Filter(Predicate::eq("common_property", "value")),
                LogicalOp::Scan(ScanOp::NodeScan {
                    label: None,
                    estimated_rows: Some(1000),
                }),
            ),
        ));

        let result = rule.apply(&plan, &stats).unwrap();
        // Should return None since already optimal
        assert!(
            result.is_none(),
            "Should not reorder already optimal filters"
        );
    }

    #[test]
    fn test_reorder_three_filters() {
        let rule = OperationReordering;
        let stats = test_stats();

        // Add a third property with medium selectivity
        stats.update_property_stats("medium_property", 100); // Medium (1/100)

        // Filter(common) -> Filter(medium) -> Filter(rare) -> Scan
        // Should become: Filter(rare) -> Filter(medium) -> Filter(common) -> Scan
        let plan = LogicalPlan::new(LogicalOp::unary(
            UnaryOp::Filter(Predicate::eq("common_property", "value")),
            LogicalOp::unary(
                UnaryOp::Filter(Predicate::eq("medium_property", "value")),
                LogicalOp::unary(
                    UnaryOp::Filter(Predicate::eq("rare_property", "value")),
                    LogicalOp::Scan(ScanOp::NodeScan {
                        label: None,
                        estimated_rows: Some(1000),
                    }),
                ),
            ),
        ));

        let result = rule.apply(&plan, &stats).unwrap();
        assert!(result.is_some(), "Should reorder three filters");
    }

    // ==================== Join Reordering Tests ====================

    #[test]
    fn test_reorder_join_operands_by_size() {
        let rule = OperationReordering;
        let stats = test_stats();

        // Join with large left side and small right side
        // Should be reordered to put small side first (for hash join build)
        let large_scan = LogicalOp::Scan(ScanOp::NodeScan {
            label: Some("LargeTable".to_string()),
            estimated_rows: Some(10000),
        });

        let small_scan = LogicalOp::Scan(ScanOp::NodeScan {
            label: Some("SmallTable".to_string()),
            estimated_rows: Some(100),
        });

        let plan = LogicalPlan::new(LogicalOp::binary(
            BinaryOp::Join {
                left_key: "id".to_string(),
                right_key: "ref_id".to_string(),
            },
            large_scan,
            small_scan,
        ));

        let result = rule.apply(&plan, &stats).unwrap();
        assert!(result.is_some(), "Should reorder join operands");

        let optimized = result.unwrap();
        // Small table should be on the left (build side)
        match &optimized.root {
            LogicalOp::Binary {
                op: BinaryOp::Join { .. },
                left,
                ..
            } => {
                if let LogicalOp::Scan(ScanOp::NodeScan {
                    estimated_rows: Some(rows),
                    ..
                }) = left.as_ref()
                {
                    assert_eq!(*rows, 100, "Smaller table should be build side");
                } else {
                    panic!("Expected NodeScan on left");
                }
            }
            _ => panic!("Expected Join at root"),
        }
    }

    #[test]
    fn test_no_reorder_when_join_already_optimal() {
        let rule = OperationReordering;
        let stats = test_stats();

        let small_scan = LogicalOp::Scan(ScanOp::NodeScan {
            label: Some("SmallTable".to_string()),
            estimated_rows: Some(100),
        });

        let large_scan = LogicalOp::Scan(ScanOp::NodeScan {
            label: Some("LargeTable".to_string()),
            estimated_rows: Some(10000),
        });

        // Small already on left - optimal
        let plan = LogicalPlan::new(LogicalOp::binary(
            BinaryOp::Join {
                left_key: "id".to_string(),
                right_key: "ref_id".to_string(),
            },
            small_scan,
            large_scan,
        ));

        let result = rule.apply(&plan, &stats).unwrap();
        assert!(result.is_none(), "Should not reorder optimal join");
    }

    // ==================== Complex Query Reordering Tests ====================

    #[test]
    fn test_reorder_complex_query() {
        let rule = OperationReordering;
        let stats = test_stats();

        // Complex query with multiple opportunities for reordering:
        // Join(large, small) with filters in non-optimal order
        let plan = LogicalPlan::new(LogicalOp::binary(
            BinaryOp::Join {
                left_key: "id".to_string(),
                right_key: "ref_id".to_string(),
            },
            // Left side: large scan with non-optimal filter order
            LogicalOp::unary(
                UnaryOp::Filter(Predicate::eq("common_property", "value")),
                LogicalOp::Scan(ScanOp::NodeScan {
                    label: Some("LargeTable".to_string()),
                    estimated_rows: Some(10000),
                }),
            ),
            // Right side: small scan
            LogicalOp::Scan(ScanOp::NodeScan {
                label: Some("SmallTable".to_string()),
                estimated_rows: Some(100),
            }),
        ));

        let result = rule.apply(&plan, &stats).unwrap();
        assert!(result.is_some(), "Should optimize complex query");
    }

    // ==================== Edge Cases ====================

    #[test]
    fn test_no_change_for_simple_scan() {
        let rule = OperationReordering;
        let stats = test_stats();

        let plan = LogicalPlan::new(LogicalOp::Scan(ScanOp::NodeLookup(vec![
            NodeId::new(1).unwrap(),
        ])));

        let result = rule.apply(&plan, &stats).unwrap();
        assert!(result.is_none(), "Should not change simple scan");
    }

    #[test]
    fn test_single_filter_no_reorder() {
        let rule = OperationReordering;
        let stats = test_stats();

        let plan = LogicalPlan::new(LogicalOp::unary(
            UnaryOp::Filter(Predicate::eq("name", "Alice")),
            LogicalOp::Scan(ScanOp::NodeScan {
                label: None,
                estimated_rows: Some(1000),
            }),
        ));

        let result = rule.apply(&plan, &stats).unwrap();
        assert!(result.is_none(), "Single filter cannot be reordered");
    }

    #[test]
    fn test_selectivity_and_predicate() {
        let rule = OperationReordering;
        let stats = test_stats();

        // AND of two predicates: selectivity should be product
        // Use predicates with fixed selectivity (not dependent on property stats)
        let pred = Predicate::And(vec![
            Predicate::gt("score", 50i64), // RANGE_PREDICATE_SELECTIVITY = 0.3
            Predicate::contains("name", "test"), // STRING_PREDICATE_SELECTIVITY = 0.2
        ]);

        let sel = rule.estimate_filter_selectivity(&pred, &stats);
        // Expected: 0.3 * 0.2 = 0.06
        assert!(
            (sel - 0.06).abs() < 0.001,
            "AND selectivity should be product, got {}",
            sel
        );
    }

    #[test]
    fn test_selectivity_or_predicate() {
        let rule = OperationReordering;
        let stats = test_stats();

        // OR of two predicates: selectivity = 1 - (1-sel1) * (1-sel2)
        // Use predicates with fixed selectivity
        let pred = Predicate::Or(vec![
            Predicate::gt("score", 50i64), // RANGE_PREDICATE_SELECTIVITY = 0.3
            Predicate::contains("name", "test"), // STRING_PREDICATE_SELECTIVITY = 0.2
        ]);

        let sel = rule.estimate_filter_selectivity(&pred, &stats);
        // Expected: 1 - (1-0.3) * (1-0.2) = 1 - 0.7 * 0.8 = 1 - 0.56 = 0.44
        assert!(
            (sel - 0.44).abs() < 0.001,
            "OR selectivity should use union rule, got {}",
            sel
        );
    }

    #[test]
    fn test_selectivity_not_predicate() {
        let rule = OperationReordering;
        let stats = test_stats();

        // NOT of a predicate: selectivity = 1 - inner_sel
        // Use predicate with fixed selectivity
        let pred = Predicate::Not(Box::new(Predicate::gt("score", 50i64))); // RANGE_PREDICATE_SELECTIVITY = 0.3

        let sel = rule.estimate_filter_selectivity(&pred, &stats);
        // Expected: 1 - 0.3 = 0.7
        assert!(
            (sel - 0.7).abs() < 0.001,
            "NOT selectivity should be complement, got {}",
            sel
        );
    }

    #[test]
    fn test_selectivity_empty_and() {
        let rule = OperationReordering;
        let stats = test_stats();

        // Empty AND should have high selectivity (filters nothing)
        let pred = Predicate::And(vec![]);
        let sel = rule.estimate_filter_selectivity(&pred, &stats);
        assert_eq!(sel, TRUE_SELECTIVITY);
    }

    #[test]
    fn test_selectivity_empty_or() {
        let rule = OperationReordering;
        let stats = test_stats();

        // Empty OR should have low selectivity (filters everything)
        let pred = Predicate::Or(vec![]);
        let sel = rule.estimate_filter_selectivity(&pred, &stats);
        assert_eq!(sel, FALSE_SELECTIVITY);
    }

    #[test]
    fn test_selectivity_nested_predicates() {
        let rule = OperationReordering;
        let stats = test_stats();

        // Nested: AND(OR(a, b), c)
        // Use predicates with fixed selectivity
        let pred = Predicate::And(vec![
            Predicate::Or(vec![
                Predicate::gt("score", 50i64),       // RANGE = 0.3
                Predicate::contains("name", "test"), // STRING = 0.2
            ]),
            Predicate::lt("age", 100i64), // RANGE_PREDICATE_SELECTIVITY = 0.3
        ]);

        let sel = rule.estimate_filter_selectivity(&pred, &stats);
        // OR: 1 - (1-0.3) * (1-0.2) = 1 - 0.7 * 0.8 = 0.44
        // AND: 0.44 * 0.3 = 0.132
        assert!(
            (sel - 0.132).abs() < 0.001,
            "Nested predicates should compute correctly, got {}",
            sel
        );
    }

    #[test]
    fn test_selectivity_all_types() {
        let rule = OperationReordering;
        let stats = test_stats();

        // Test all predicate types have defined selectivity
        assert_eq!(
            rule.estimate_filter_selectivity(&Predicate::True, &stats),
            TRUE_SELECTIVITY
        );
        assert_eq!(
            rule.estimate_filter_selectivity(&Predicate::False, &stats),
            FALSE_SELECTIVITY
        );
        assert_eq!(
            rule.estimate_filter_selectivity(&Predicate::gt("x", 1i64), &stats),
            RANGE_PREDICATE_SELECTIVITY
        );
        assert_eq!(
            rule.estimate_filter_selectivity(&Predicate::lt("x", 1i64), &stats),
            RANGE_PREDICATE_SELECTIVITY
        );
        assert_eq!(
            rule.estimate_filter_selectivity(&Predicate::contains("x", "y"), &stats),
            STRING_PREDICATE_SELECTIVITY
        );
        assert_eq!(
            rule.estimate_filter_selectivity(
                &Predicate::StartsWith {
                    key: "x".to_string(),
                    prefix: "y".to_string()
                },
                &stats
            ),
            STRING_PREDICATE_SELECTIVITY
        );
        assert_eq!(
            rule.estimate_filter_selectivity(
                &Predicate::EndsWith {
                    key: "x".to_string(),
                    suffix: "y".to_string()
                },
                &stats
            ),
            STRING_PREDICATE_SELECTIVITY
        );
        assert_eq!(
            rule.estimate_filter_selectivity(&Predicate::ne("x", 1i64), &stats),
            NOT_EQUALS_SELECTIVITY
        );
        assert_eq!(
            rule.estimate_filter_selectivity(
                &Predicate::In {
                    key: "x".to_string(),
                    values: vec![
                        crate::query::ir::PredicateValue::Int(1),
                        crate::query::ir::PredicateValue::Int(2)
                    ]
                },
                &stats
            ),
            IN_PREDICATE_SELECTIVITY
        );
        assert_eq!(
            rule.estimate_filter_selectivity(&Predicate::exists("x"), &stats),
            EXISTENCE_CHECK_SELECTIVITY
        );
        assert_eq!(
            rule.estimate_filter_selectivity(&Predicate::NotExists("x".to_string()), &stats),
            EXISTENCE_CHECK_SELECTIVITY
        );
    }

    #[test]
    fn test_predicates_equal_basic() {
        // Same predicates should be equal (Predicate derives PartialEq)
        let p1 = Predicate::eq("name", "Alice");
        let p2 = Predicate::eq("name", "Alice");
        assert_eq!(p1, p2);

        // Different values should not be equal
        let p3 = Predicate::eq("name", "Bob");
        assert_ne!(p1, p3);

        // Different keys should not be equal
        let p4 = Predicate::eq("age", "Alice");
        assert_ne!(p1, p4);
    }

    #[test]
    fn test_predicates_equal_different_types() {
        // Different predicate types should not be equal
        let p1 = Predicate::eq("name", "Alice");
        let p2 = Predicate::ne("name", "Alice");
        assert_ne!(p1, p2);

        let p3 = Predicate::gt("age", 30i64);
        let p4 = Predicate::lt("age", 30i64);
        assert_ne!(p3, p4);
    }

    #[test]
    fn test_predicates_equal_and_or() {
        // Same AND predicates
        let p1 = Predicate::And(vec![
            Predicate::eq("name", "Alice"),
            Predicate::gt("age", 30i64),
        ]);
        let p2 = Predicate::And(vec![
            Predicate::eq("name", "Alice"),
            Predicate::gt("age", 30i64),
        ]);
        assert_eq!(p1, p2);

        // Different order in AND should not be equal (structural comparison)
        let p3 = Predicate::And(vec![
            Predicate::gt("age", 30i64),
            Predicate::eq("name", "Alice"),
        ]);
        assert_ne!(p1, p3);

        // Different length AND
        let p4 = Predicate::And(vec![Predicate::eq("name", "Alice")]);
        assert_ne!(p1, p4);

        // AND vs OR
        let p5 = Predicate::Or(vec![
            Predicate::eq("name", "Alice"),
            Predicate::gt("age", 30i64),
        ]);
        assert_ne!(p1, p5);
    }

    #[test]
    fn test_predicates_equal_not() {
        // Same NOT predicates
        let p1 = Predicate::Not(Box::new(Predicate::eq("active", true)));
        let p2 = Predicate::Not(Box::new(Predicate::eq("active", true)));
        assert_eq!(p1, p2);

        // Different inner predicates
        let p3 = Predicate::Not(Box::new(Predicate::eq("active", false)));
        assert_ne!(p1, p3);
    }

    #[test]
    fn test_predicates_equal_all_variants() {
        // Test all variants for coverage (Predicate derives PartialEq)
        assert_eq!(Predicate::True, Predicate::True);
        assert_eq!(Predicate::False, Predicate::False);
        assert_ne!(Predicate::True, Predicate::False);

        // String predicates
        let c1 = Predicate::contains("text", "hello");
        let c2 = Predicate::contains("text", "hello");
        assert_eq!(c1, c2);

        let s1 = Predicate::StartsWith {
            key: "text".to_string(),
            prefix: "hello".to_string(),
        };
        let s2 = Predicate::StartsWith {
            key: "text".to_string(),
            prefix: "hello".to_string(),
        };
        assert_eq!(s1, s2);

        let e1 = Predicate::EndsWith {
            key: "text".to_string(),
            suffix: "world".to_string(),
        };
        let e2 = Predicate::EndsWith {
            key: "text".to_string(),
            suffix: "world".to_string(),
        };
        assert_eq!(e1, e2);

        // In predicate
        let i1 = Predicate::In {
            key: "id".to_string(),
            values: vec![
                crate::query::ir::PredicateValue::Int(1),
                crate::query::ir::PredicateValue::Int(2),
            ],
        };
        let i2 = Predicate::In {
            key: "id".to_string(),
            values: vec![
                crate::query::ir::PredicateValue::Int(1),
                crate::query::ir::PredicateValue::Int(2),
            ],
        };
        assert_eq!(i1, i2);

        // Exists predicates
        let ex1 = Predicate::exists("prop");
        let ex2 = Predicate::exists("prop");
        assert_eq!(ex1, ex2);

        let nex1 = Predicate::NotExists("prop".to_string());
        let nex2 = Predicate::NotExists("prop".to_string());
        assert_eq!(nex1, nex2);
    }

    #[test]
    fn test_selectivity_null_check() {
        let rule = OperationReordering;
        let stats = test_stats();

        // Null checks should use NULL_CHECK_SELECTIVITY
        let pred = Predicate::Eq {
            key: "value".to_string(),
            value: crate::query::ir::PredicateValue::Null,
        };

        let sel = rule.estimate_filter_selectivity(&pred, &stats);
        assert_eq!(sel, NULL_CHECK_SELECTIVITY);
    }

    #[test]
    fn test_predicates_equal_exhaustive_mismatches() {
        // Predicate derives PartialEq, so we use assert_ne! directly.

        // Eq mismatches
        assert_ne!(Predicate::eq("a", 1), Predicate::eq("b", 1));
        assert_ne!(Predicate::eq("a", 1), Predicate::eq("a", 2));

        // Ne mismatches
        assert_ne!(Predicate::ne("a", 1), Predicate::ne("b", 1));
        assert_ne!(Predicate::ne("a", 1), Predicate::ne("a", 2));

        // Gt mismatches
        assert_ne!(Predicate::gt("a", 1), Predicate::gt("b", 1));
        assert_ne!(Predicate::gt("a", 1), Predicate::gt("a", 2));

        // Gte mismatches
        assert_ne!(
            Predicate::Gte {
                key: "a".to_string(),
                value: crate::query::ir::PredicateValue::Int(1)
            },
            Predicate::Gte {
                key: "b".to_string(),
                value: crate::query::ir::PredicateValue::Int(1)
            }
        );
        assert_ne!(
            Predicate::Gte {
                key: "a".to_string(),
                value: crate::query::ir::PredicateValue::Int(1)
            },
            Predicate::Gte {
                key: "a".to_string(),
                value: crate::query::ir::PredicateValue::Int(2)
            }
        );

        // Lt mismatches
        assert_ne!(Predicate::lt("a", 1), Predicate::lt("b", 1));
        assert_ne!(Predicate::lt("a", 1), Predicate::lt("a", 2));

        // Lte mismatches
        assert_ne!(
            Predicate::Lte {
                key: "a".to_string(),
                value: crate::query::ir::PredicateValue::Int(1)
            },
            Predicate::Lte {
                key: "b".to_string(),
                value: crate::query::ir::PredicateValue::Int(1)
            }
        );
        assert_ne!(
            Predicate::Lte {
                key: "a".to_string(),
                value: crate::query::ir::PredicateValue::Int(1)
            },
            Predicate::Lte {
                key: "a".to_string(),
                value: crate::query::ir::PredicateValue::Int(2)
            }
        );

        // In mismatches
        assert_ne!(
            Predicate::In {
                key: "a".to_string(),
                values: vec![crate::query::ir::PredicateValue::Int(1)]
            },
            Predicate::In {
                key: "b".to_string(),
                values: vec![crate::query::ir::PredicateValue::Int(1)]
            }
        );
        assert_ne!(
            Predicate::In {
                key: "a".to_string(),
                values: vec![crate::query::ir::PredicateValue::Int(1)]
            },
            Predicate::In {
                key: "a".to_string(),
                values: vec![crate::query::ir::PredicateValue::Int(2)]
            }
        );

        // Contains mismatches
        assert_ne!(
            Predicate::contains("a", "abc"),
            Predicate::contains("b", "abc")
        );
        assert_ne!(
            Predicate::contains("a", "abc"),
            Predicate::contains("a", "xyz")
        );

        // StartsWith mismatches
        assert_ne!(
            Predicate::StartsWith {
                key: "a".to_string(),
                prefix: "abc".to_string()
            },
            Predicate::StartsWith {
                key: "b".to_string(),
                prefix: "abc".to_string()
            }
        );
        assert_ne!(
            Predicate::StartsWith {
                key: "a".to_string(),
                prefix: "abc".to_string()
            },
            Predicate::StartsWith {
                key: "a".to_string(),
                prefix: "xyz".to_string()
            }
        );

        // EndsWith mismatches
        assert_ne!(
            Predicate::EndsWith {
                key: "a".to_string(),
                suffix: "abc".to_string()
            },
            Predicate::EndsWith {
                key: "b".to_string(),
                suffix: "abc".to_string()
            }
        );
        assert_ne!(
            Predicate::EndsWith {
                key: "a".to_string(),
                suffix: "abc".to_string()
            },
            Predicate::EndsWith {
                key: "a".to_string(),
                suffix: "xyz".to_string()
            }
        );

        // Exists mismatches
        assert_ne!(Predicate::exists("a"), Predicate::exists("b"));

        // NotExists mismatches
        assert_ne!(
            Predicate::NotExists("a".to_string()),
            Predicate::NotExists("b".to_string())
        );

        // And / Or structural mismatches (different sizes)
        assert_ne!(
            Predicate::And(vec![Predicate::eq("a", 1)]),
            Predicate::And(vec![Predicate::eq("a", 1), Predicate::eq("b", 2)])
        );
        assert_ne!(
            Predicate::Or(vec![Predicate::eq("a", 1)]),
            Predicate::Or(vec![Predicate::eq("a", 1), Predicate::eq("b", 2)])
        );
    }
}