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
//! Cost Model for Query Optimization
//!
//! Provides cost estimation for physical operators, enabling the planner
//! to choose the most efficient execution strategy.
//!
//! Cost values are calibrated from AletheiaDB benchmarks:
//! - Node lookup: ~0.5µs
//! - Single-hop traversal: ~1µs
//! - HNSW search per k: ~0.3µs (log scale with index size)
//! - Temporal delta reconstruction: ~10µs per delta
//! - Filter evaluation: ~0.1µs per row

use super::physical::PhysicalOp;
use super::stats::Statistics;

// ── Cardinality / selectivity defaults ───────────────────────────────────────

/// Default fraction of rows surviving a filter when statistics are unavailable.
const DEFAULT_FILTER_SELECTIVITY: f64 = 0.1;

/// Default fraction of rows remaining after a DISTINCT operation.
const DEFAULT_DISTINCT_RATIO: f64 = 0.5;

/// Default fraction of the cross-product surviving a join predicate.
const DEFAULT_JOIN_SELECTIVITY: f64 = 0.1;

// ── Memory estimation factors ────────────────────────────────────────────────

/// Estimated number of property fields stored per current-state node.
const ESTIMATED_FIELDS_PER_NODE: usize = 10;

/// Estimated number of property fields stored per temporal (versioned) node.
const ESTIMATED_FIELDS_PER_TEMPORAL_NODE: usize = 20;

// ── I/O and overhead factors ─────────────────────────────────────────────────

/// Number of rows assumed per sequential I/O batch during a node scan.
const BATCH_IO_ROWS: f64 = 1000.0;

/// Multiplicative overhead applied to HNSW search when a label filter is active.
const FILTERED_SEARCH_OVERHEAD: f64 = 2.0;

/// Multiplicative overhead for temporal vector search snapshot lookups.
const TEMPORAL_SNAPSHOT_OVERHEAD: f64 = 1.5;

/// Estimated cost of executing a query plan.
#[derive(Debug, Clone, Copy, Default)]
pub struct Cost {
    /// CPU cost in abstract units (roughly microseconds)
    pub cpu: f64,
    /// I/O cost (storage accesses)
    pub io: f64,
    /// Memory cost in bytes
    pub memory: usize,
    /// Network cost (for future distributed support)
    pub network: f64,
}

impl Cost {
    /// Create a new cost with all zero values
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use aletheiadb::query::planner::cost::Cost;
    ///
    /// let cost = Cost::zero();
    /// assert_eq!(cost.cpu, 0.0);
    /// assert_eq!(cost.io, 0.0);
    /// ```
    #[must_use]
    pub fn zero() -> Self {
        Cost::default()
    }

    /// Calculate total weighted cost for comparison
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use aletheiadb::query::planner::cost::{Cost, CostWeights};
    ///
    /// let cost = Cost { cpu: 10.0, io: 5.0, memory: 100, network: 0.0 };
    /// let weights = CostWeights { cpu_weight: 1.0, io_weight: 10.0, memory_weight: 0.1, network_weight: 1.0 };
    ///
    /// // 10.0 * 1.0 + 5.0 * 10.0 + 100.0 * 0.1 = 10.0 + 50.0 + 10.0 = 70.0
    /// assert_eq!(cost.total(&weights), 70.0);
    /// ```
    #[must_use]
    pub fn total(&self, weights: &CostWeights) -> f64 {
        self.cpu * weights.cpu_weight
            + self.io * weights.io_weight
            + (self.memory as f64) * weights.memory_weight
            + self.network * weights.network_weight
    }

    /// Add two costs together
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use aletheiadb::query::planner::cost::Cost;
    ///
    /// let c1 = Cost { cpu: 1.0, io: 2.0, memory: 100, network: 0.0 };
    /// let c2 = Cost { cpu: 2.0, io: 3.0, memory: 50, network: 0.0 };
    ///
    /// let sum = c1.add(&c2);
    /// assert_eq!(sum.cpu, 3.0);
    /// assert_eq!(sum.io, 5.0);
    /// assert_eq!(sum.memory, 150);
    /// ```
    #[must_use]
    pub fn add(&self, other: &Cost) -> Cost {
        Cost {
            cpu: self.cpu + other.cpu,
            io: self.io + other.io,
            memory: self.memory.saturating_add(other.memory),
            network: self.network + other.network,
        }
    }

    /// Scale cost by a factor
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use aletheiadb::query::planner::cost::Cost;
    ///
    /// let cost = Cost { cpu: 2.0, io: 4.0, memory: 100, network: 0.0 };
    /// let scaled = cost.scale(1.5);
    ///
    /// assert_eq!(scaled.cpu, 3.0);
    /// assert_eq!(scaled.io, 6.0);
    /// assert_eq!(scaled.memory, 150);
    /// ```
    #[must_use]
    pub fn scale(&self, factor: f64) -> Cost {
        Cost {
            cpu: self.cpu * factor,
            io: self.io * factor,
            memory: (self.memory as f64 * factor) as usize,
            network: self.network * factor,
        }
    }
}

impl std::ops::Add for Cost {
    type Output = Cost;

    fn add(self, rhs: Self) -> Self::Output {
        Cost::add(&self, &rhs)
    }
}

/// Weights for cost components.
///
/// These weights determine the relative importance of different
/// cost factors when comparing plans.
#[derive(Debug, Clone)]
pub struct CostWeights {
    /// Weight for CPU cost
    pub cpu_weight: f64,
    /// Weight for I/O cost (typically higher than CPU)
    pub io_weight: f64,
    /// Weight for memory usage
    pub memory_weight: f64,
    /// Weight for network cost (for distributed queries)
    pub network_weight: f64,
}

impl Default for CostWeights {
    fn default() -> Self {
        CostWeights {
            cpu_weight: 1.0,
            io_weight: 10.0,       // I/O is typically 10x more expensive than CPU
            memory_weight: 0.001,  // Memory is cheap but limited
            network_weight: 100.0, // Network is very expensive
        }
    }
}

/// Base costs for operations, calibrated from benchmarks.
#[derive(Debug, Clone)]
pub struct OperationCosts {
    /// O(1) node lookup from DashMap (~0.5µs)
    pub node_lookup: f64,
    /// Single-hop traversal via CSR adjacency (~1µs)
    pub single_hop_traversal: f64,
    /// HNSW search base cost per result (~0.3µs)
    pub hnsw_search_per_k: f64,
    /// HNSW search logarithmic factor
    pub hnsw_log_factor: f64,
    /// Temporal delta reconstruction (~10µs per delta)
    pub temporal_delta: f64,
    /// Filter evaluation per row (~0.1µs)
    pub filter_eval: f64,
    /// Vector similarity computation per pair (~0.5µs for 384-dim)
    pub vector_similarity: f64,
    /// Sort cost per element (n log n factor)
    pub sort_per_element: f64,
    /// Hash join build side per element
    pub hash_build: f64,
    /// Hash join probe per element
    pub hash_probe: f64,
    /// Lock acquisition overhead (~2µs for RwLock)
    pub lock_acquisition: f64,
    /// Threshold for using batch iterator (nodes)
    pub batch_threshold: usize,
}

impl Default for OperationCosts {
    fn default() -> Self {
        OperationCosts {
            node_lookup: 0.5,
            single_hop_traversal: 1.0,
            hnsw_search_per_k: 0.3,
            hnsw_log_factor: 1.0,
            temporal_delta: 10.0,
            filter_eval: 0.1,
            vector_similarity: 0.5,
            sort_per_element: 0.01,
            hash_build: 0.1,
            hash_probe: 0.05,
            lock_acquisition: 2.0,
            batch_threshold: 100,
        }
    }
}

/// Cost model for estimating query execution costs.
///
/// Contains weights and base costs for CPU, I/O, memory, and network operations.
/// Different models can be used to optimize for different environments (e.g., low-latency vs high-throughput).
///
/// ## Examples
///
/// ```rust
/// use aletheiadb::query::planner::cost::CostModel;
///
/// let model = CostModel::new();
/// assert_eq!(model.weights.cpu_weight, 1.0);
/// ```
#[derive(Debug, Clone, Default)]
pub struct CostModel {
    /// Cost weights for combining different factors
    pub weights: CostWeights,
    /// Base operation costs
    pub operation_costs: OperationCosts,
}

impl CostModel {
    /// Create a new cost model with default values
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use aletheiadb::query::planner::cost::CostModel;
    ///
    /// let model = CostModel::new();
    /// assert_eq!(model.weights.cpu_weight, 1.0);
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a cost model optimized for low-latency queries
    ///
    /// This model penalizes I/O and network operations more heavily to favor
    /// fast, in-memory execution strategies.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use aletheiadb::query::planner::cost::CostModel;
    ///
    /// let model = CostModel::low_latency();
    /// assert!(model.weights.io_weight > 10.0); // Heavily penalizes IO
    /// ```
    #[must_use]
    pub fn low_latency() -> Self {
        CostModel {
            weights: CostWeights {
                cpu_weight: 1.0,
                io_weight: 20.0, // Penalize I/O more
                memory_weight: 0.0001,
                network_weight: 1000.0,
            },
            operation_costs: OperationCosts::default(),
        }
    }

    /// Create a cost model optimized for throughput
    ///
    /// This model is more tolerant of high memory usage and I/O if it means
    /// processing more data efficiently in parallel.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use aletheiadb::query::planner::cost::CostModel;
    ///
    /// let model = CostModel::high_throughput();
    /// assert!(model.weights.cpu_weight < 1.0); // CPU is weighted less heavily
    /// ```
    #[must_use]
    pub fn high_throughput() -> Self {
        CostModel {
            weights: CostWeights {
                cpu_weight: 0.5,
                io_weight: 5.0,
                memory_weight: 0.01, // Allow more memory usage
                network_weight: 50.0,
            },
            operation_costs: OperationCosts::default(),
        }
    }

    /// Determine whether to use batch iterator for temporal lookup.
    ///
    /// Batch iterator is more efficient when the lock acquisition overhead
    /// of per-node iteration exceeds the cost of holding the lock longer.
    ///
    /// Returns true if batch mode should be used.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use aletheiadb::query::planner::cost::CostModel;
    ///
    /// let model = CostModel::new();
    /// // Single lookup is cheaper with per-node lock
    /// assert!(!model.should_use_batch_temporal_lookup(1));
    /// // Large batches are cheaper holding the lock
    /// assert!(model.should_use_batch_temporal_lookup(1000));
    /// ```
    #[must_use]
    pub fn should_use_batch_temporal_lookup(&self, node_count: usize) -> bool {
        node_count >= self.operation_costs.batch_threshold
    }

    /// Estimate the cost of executing a physical operator
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use aletheiadb::query::planner::cost::CostModel;
    /// use aletheiadb::query::planner::stats::Statistics;
    /// use aletheiadb::query::planner::physical::PhysicalOp;
    /// use aletheiadb::core::NodeId;
    ///
    /// let model = CostModel::new();
    /// let stats = Statistics::new();
    /// let op = PhysicalOp::NodeLookup {
    ///     node_ids: vec![NodeId::new(1).unwrap(), NodeId::new(2).unwrap()]
    /// };
    ///
    /// let cost = model.estimate(&op, &stats);
    /// assert!(cost.cpu > 0.0);
    /// assert_eq!(cost.io, 0.0); // Simple lookups are assumed in-memory
    /// ```
    #[must_use]
    pub fn estimate(&self, op: &PhysicalOp, stats: &Statistics) -> Cost {
        match op {
            PhysicalOp::NodeLookup { node_ids } => self.estimate_node_lookup(node_ids.len()),

            PhysicalOp::NodeScan { estimated_rows, .. } => self.estimate_node_scan(*estimated_rows),

            PhysicalOp::EdgeScan { estimated_rows, .. } => self.estimate_edge_scan(*estimated_rows),

            // PropertyScan is cheaper than NodeScan+Filter because it skips predicate
            // evaluation on non-matching nodes at the storage level
            PhysicalOp::PropertyScan { estimated_rows, .. } => {
                self.estimate_node_lookup(*estimated_rows)
            }

            PhysicalOp::HnswSearch {
                k, label_filter, ..
            } => self.estimate_hnsw_search(*k, label_filter.is_some(), stats),

            PhysicalOp::TemporalNodeLookup {
                node_ids,
                use_batch,
                ..
            } => self.estimate_temporal_lookup(node_ids.len(), *use_batch, stats),

            PhysicalOp::TemporalVectorSearch { k, .. } => {
                self.estimate_temporal_vector_search(*k, stats)
            }

            PhysicalOp::IndexedTraversal { input, depth, .. } => {
                let input_cost = self.estimate(input, stats);
                let input_card = self.estimate_cardinality(input, stats);
                self.estimate_traversal(input_cost, input_card, *depth, stats)
            }

            PhysicalOp::Filter { input, .. } => {
                let input_cost = self.estimate(input, stats);
                let input_card = self.estimate_cardinality(input, stats);
                self.estimate_filter(input_cost, input_card)
            }

            PhysicalOp::VectorRerank { input, k, .. } => {
                let input_cost = self.estimate(input, stats);
                let input_card = self.estimate_cardinality(input, stats);
                self.estimate_vector_rerank(input_cost, input_card, *k)
            }

            PhysicalOp::Sort { input, .. } => {
                let input_cost = self.estimate(input, stats);
                let input_card = self.estimate_cardinality(input, stats);
                self.estimate_sort(input_cost, input_card)
            }

            PhysicalOp::Limit { input, .. } => {
                // Limit doesn't add much cost, just passes through
                self.estimate(input, stats)
            }

            PhysicalOp::HashJoin { left, right, .. } => {
                let left_cost = self.estimate(left, stats);
                let right_cost = self.estimate(right, stats);
                let left_card = self.estimate_cardinality(left, stats);
                let right_card = self.estimate_cardinality(right, stats);
                self.estimate_hash_join(left_cost, right_cost, left_card, right_card)
            }

            PhysicalOp::Union { left, right }
            | PhysicalOp::Intersect { left, right }
            | PhysicalOp::Except { left, right } => {
                let left_cost = self.estimate(left, stats);
                let right_cost = self.estimate(right, stats);
                left_cost + right_cost
            }

            PhysicalOp::Project { input, .. }
            | PhysicalOp::Distinct { input }
            | PhysicalOp::Count { input }
            | PhysicalOp::Materialize { input }
            | PhysicalOp::TemporalTrack { input, .. } => self.estimate(input, stats),

            PhysicalOp::SimilarToNode {
                k, label_filter, ..
            } => {
                // SimilarToNode = node lookup + HNSW search
                let lookup_cost = self.estimate_node_lookup(1);
                let search_cost = self.estimate_hnsw_search(*k, label_filter.is_some(), stats);
                lookup_cost + search_cost
            }

            PhysicalOp::Empty => Cost::zero(),
        }
    }

    /// Estimate cardinality of a physical operator's output
    ///
    /// ## Examples
    ///
    /// ```rust
    /// use aletheiadb::query::planner::cost::CostModel;
    /// use aletheiadb::query::planner::stats::Statistics;
    /// use aletheiadb::query::planner::physical::PhysicalOp;
    /// use aletheiadb::core::NodeId;
    ///
    /// let model = CostModel::new();
    /// let stats = Statistics::new();
    /// let op = PhysicalOp::NodeLookup {
    ///     node_ids: vec![NodeId::new(1).unwrap(), NodeId::new(2).unwrap()]
    /// };
    ///
    /// assert_eq!(model.estimate_cardinality(&op, &stats), 2);
    /// ```
    #[must_use]
    pub fn estimate_cardinality(&self, op: &PhysicalOp, stats: &Statistics) -> usize {
        match op {
            PhysicalOp::NodeLookup { node_ids } => node_ids.len(),
            PhysicalOp::NodeScan { estimated_rows, .. } => *estimated_rows,
            PhysicalOp::EdgeScan { estimated_rows, .. } => *estimated_rows,
            PhysicalOp::PropertyScan { estimated_rows, .. } => *estimated_rows,
            PhysicalOp::HnswSearch { k, .. } => *k,
            PhysicalOp::TemporalNodeLookup { node_ids, .. } => node_ids.len(),
            PhysicalOp::TemporalVectorSearch { k, .. } => *k,
            PhysicalOp::IndexedTraversal { input, depth, .. } => {
                let input_card = self.estimate_cardinality(input, stats);
                let avg_degree = stats.average_out_degree();
                (input_card as f64 * avg_degree.powi(*depth as i32)) as usize
            }
            PhysicalOp::Filter { input, .. } => {
                // Assume default selectivity when statistics are unavailable
                (self.estimate_cardinality(input, stats) as f64 * DEFAULT_FILTER_SELECTIVITY)
                    as usize
            }
            PhysicalOp::VectorRerank { k, .. } => *k,
            PhysicalOp::Limit { count, input, .. } => {
                (*count).min(self.estimate_cardinality(input, stats))
            }
            PhysicalOp::Sort { input, .. } | PhysicalOp::Project { input, .. } => {
                self.estimate_cardinality(input, stats)
            }
            PhysicalOp::Distinct { input } => {
                // Assume default distinct ratio
                (self.estimate_cardinality(input, stats) as f64 * DEFAULT_DISTINCT_RATIO) as usize
            }
            PhysicalOp::Count { .. } => 1,
            PhysicalOp::HashJoin { left, right, .. } => {
                // Assume default join selectivity of cross product
                let left_card = self.estimate_cardinality(left, stats);
                let right_card = self.estimate_cardinality(right, stats);
                (left_card as f64 * right_card as f64 * DEFAULT_JOIN_SELECTIVITY) as usize
            }
            PhysicalOp::Union { left, right } => {
                self.estimate_cardinality(left, stats) + self.estimate_cardinality(right, stats)
            }
            PhysicalOp::Intersect { left, right } => self
                .estimate_cardinality(left, stats)
                .min(self.estimate_cardinality(right, stats)),
            PhysicalOp::Except { left, .. } => self.estimate_cardinality(left, stats),
            PhysicalOp::Materialize { input } | PhysicalOp::TemporalTrack { input, .. } => {
                self.estimate_cardinality(input, stats)
            }
            PhysicalOp::SimilarToNode { k, .. } => *k,
            PhysicalOp::Empty => 0,
        }
    }

    // Private estimation methods

    fn estimate_node_lookup(&self, count: usize) -> Cost {
        Cost {
            cpu: self.operation_costs.node_lookup * count as f64,
            io: 0.0, // In-memory
            memory: count * std::mem::size_of::<u64>() * ESTIMATED_FIELDS_PER_NODE, // Rough estimate per node
            network: 0.0,
        }
    }

    fn estimate_node_scan(&self, estimated_rows: usize) -> Cost {
        Cost {
            cpu: self.operation_costs.node_lookup * estimated_rows as f64,
            io: (estimated_rows as f64 / BATCH_IO_ROWS).ceil(), // Batch I/O
            memory: estimated_rows * std::mem::size_of::<u64>() * ESTIMATED_FIELDS_PER_NODE,
            network: 0.0,
        }
    }

    /// Estimate cost for a full edge scan.
    ///
    /// Currently mirrors `estimate_node_scan` since edges and nodes have similar
    /// storage characteristics, but is kept as a separate method so the cost model
    /// can diverge later (e.g. edges may have fewer properties or different I/O
    /// patterns).
    fn estimate_edge_scan(&self, estimated_rows: usize) -> Cost {
        Cost {
            cpu: self.operation_costs.node_lookup * estimated_rows as f64,
            io: (estimated_rows as f64 / BATCH_IO_ROWS).ceil(),
            memory: estimated_rows * std::mem::size_of::<u64>() * ESTIMATED_FIELDS_PER_NODE,
            network: 0.0,
        }
    }

    fn estimate_hnsw_search(&self, k: usize, has_filter: bool, stats: &Statistics) -> Cost {
        let vector_count = stats.vector_count().max(1) as f64;
        let log_factor = vector_count.log2().max(1.0);
        let filter_overhead = if has_filter {
            FILTERED_SEARCH_OVERHEAD
        } else {
            1.0
        };

        Cost {
            cpu: self.operation_costs.hnsw_search_per_k
                * k as f64
                * log_factor
                * self.operation_costs.hnsw_log_factor
                * filter_overhead,
            io: 0.0,
            memory: k * std::mem::size_of::<(u64, f32)>(),
            network: 0.0,
        }
    }

    fn estimate_temporal_lookup(&self, count: usize, use_batch: bool, stats: &Statistics) -> Cost {
        let avg_delta_chain = stats.average_delta_chain_length().max(1.0);

        // Calculate lock overhead based on iterator strategy:
        // - Per-node iterator: acquires lock for each node lookup
        // - Batch iterator: acquires lock once but holds it longer
        let lock_overhead = if use_batch {
            // Batch: single lock acquisition
            self.operation_costs.lock_acquisition
        } else {
            // Per-node: lock acquisition per node
            self.operation_costs.lock_acquisition * count as f64
        };

        Cost {
            cpu: lock_overhead
                + self.operation_costs.temporal_delta * avg_delta_chain * count as f64,
            io: avg_delta_chain * count as f64,
            memory: count * std::mem::size_of::<u64>() * ESTIMATED_FIELDS_PER_TEMPORAL_NODE, // Larger due to versioning
            network: 0.0,
        }
    }

    fn estimate_temporal_vector_search(&self, k: usize, stats: &Statistics) -> Cost {
        // Temporal vector search uses snapshots, so similar to regular HNSW
        // but may need to check multiple snapshots
        let base = self.estimate_hnsw_search(k, false, stats);
        Cost {
            cpu: base.cpu * TEMPORAL_SNAPSHOT_OVERHEAD, // Snapshot lookup overhead
            io: base.io + 1.0,                          // Snapshot access
            memory: base.memory,
            network: 0.0,
        }
    }

    fn estimate_traversal(
        &self,
        input_cost: Cost,
        input_card: usize,
        depth: usize,
        stats: &Statistics,
    ) -> Cost {
        let avg_degree = stats.average_out_degree();
        let traversal_factor = avg_degree.powi(depth as i32);

        Cost {
            cpu: input_cost.cpu
                + self.operation_costs.single_hop_traversal * input_card as f64 * traversal_factor,
            io: input_cost.io,
            memory: input_cost.memory
                + (input_card as f64 * traversal_factor) as usize * std::mem::size_of::<u64>(),
            network: 0.0,
        }
    }

    fn estimate_filter(&self, input_cost: Cost, input_card: usize) -> Cost {
        Cost {
            cpu: input_cost.cpu + self.operation_costs.filter_eval * input_card as f64,
            io: input_cost.io,
            memory: input_cost.memory,
            network: 0.0,
        }
    }

    fn estimate_vector_rerank(&self, input_cost: Cost, input_card: usize, k: usize) -> Cost {
        Cost {
            cpu: input_cost.cpu + self.operation_costs.vector_similarity * input_card as f64,
            io: input_cost.io,
            memory: input_cost.memory + k * std::mem::size_of::<(u64, f32)>(),
            network: 0.0,
        }
    }

    fn estimate_sort(&self, input_cost: Cost, input_card: usize) -> Cost {
        let n = input_card.max(1) as f64;
        let nlogn = n * n.log2();

        Cost {
            cpu: input_cost.cpu + self.operation_costs.sort_per_element * nlogn,
            io: input_cost.io,
            memory: input_cost.memory + input_card * std::mem::size_of::<u64>(),
            network: 0.0,
        }
    }

    fn estimate_hash_join(
        &self,
        left_cost: Cost,
        right_cost: Cost,
        left_card: usize,
        right_card: usize,
    ) -> Cost {
        // Build phase on smaller side, probe on larger
        let (build_card, probe_card) = if left_card < right_card {
            (left_card, right_card)
        } else {
            (right_card, left_card)
        };

        Cost {
            cpu: left_cost.cpu
                + right_cost.cpu
                + self.operation_costs.hash_build * build_card as f64
                + self.operation_costs.hash_probe * probe_card as f64,
            io: left_cost.io + right_cost.io,
            memory: left_cost.memory
                + right_cost.memory
                + build_card * std::mem::size_of::<u64>() * 2,
            network: 0.0,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::NodeId;
    use std::sync::Arc;

    fn test_stats() -> Statistics {
        Statistics::default()
    }

    #[test]
    fn test_cost_arithmetic() {
        let a = Cost {
            cpu: 1.0,
            io: 2.0,
            memory: 100,
            network: 0.0,
        };
        let b = Cost {
            cpu: 0.5,
            io: 1.0,
            memory: 50,
            network: 0.0,
        };

        let sum = a.add(&b);
        assert_eq!(sum.cpu, 1.5);
        assert_eq!(sum.io, 3.0);
        assert_eq!(sum.memory, 150);

        let scaled = a.scale(2.0);
        assert_eq!(scaled.cpu, 2.0);
        assert_eq!(scaled.io, 4.0);
        assert_eq!(scaled.memory, 200);
    }

    #[test]
    fn test_cost_total() {
        let cost = Cost {
            cpu: 10.0,
            io: 5.0,
            memory: 1000,
            network: 0.0,
        };
        let weights = CostWeights::default();

        let total = cost.total(&weights);
        // cpu * 1.0 + io * 10.0 + memory * 0.001 + network * 100.0
        // = 10.0 + 50.0 + 1.0 + 0.0 = 61.0
        assert!((total - 61.0).abs() < 0.01);
    }

    #[test]
    fn test_node_lookup_cost() {
        let model = CostModel::default();
        let stats = test_stats();

        let op = PhysicalOp::NodeLookup {
            node_ids: vec![NodeId::new(1).unwrap(), NodeId::new(2).unwrap()],
        };

        let cost = model.estimate(&op, &stats);
        assert!(cost.cpu > 0.0);
        assert_eq!(cost.io, 0.0); // In-memory
    }

    #[test]
    fn test_hnsw_search_cost() {
        let model = CostModel::default();
        let stats = test_stats();

        let op = PhysicalOp::HnswSearch {
            embedding: Arc::from([0.1f32; 4].as_slice()),
            k: 10,
            label_filter: None,
            property_key: None,
        };

        let cost = model.estimate(&op, &stats);
        assert!(cost.cpu > 0.0);
    }

    #[test]
    fn test_traversal_cost() {
        let model = CostModel::default();
        let stats = test_stats();

        let op = PhysicalOp::IndexedTraversal {
            input: Box::new(PhysicalOp::NodeLookup {
                node_ids: vec![NodeId::new(1).unwrap()],
            }),
            direction: crate::query::ir::Direction::Outgoing,
            label: None,
            depth: 2,
            temporal_context: None,
        };

        let cost = model.estimate(&op, &stats);
        assert!(cost.cpu > 0.0);
    }

    #[test]
    fn test_cardinality_estimation() {
        let model = CostModel::default();
        let stats = test_stats();

        let lookup = PhysicalOp::NodeLookup {
            node_ids: vec![NodeId::new(1).unwrap(), NodeId::new(2).unwrap()],
        };
        assert_eq!(model.estimate_cardinality(&lookup, &stats), 2);

        let search = PhysicalOp::HnswSearch {
            embedding: Arc::from([0.1f32; 4].as_slice()),
            k: 10,
            label_filter: None,
            property_key: None,
        };
        assert_eq!(model.estimate_cardinality(&search, &stats), 10);

        let limit = PhysicalOp::Limit {
            input: Box::new(search),
            count: 5,
            offset: 0,
        };
        assert_eq!(model.estimate_cardinality(&limit, &stats), 5);
    }

    #[test]
    fn test_edge_scan_cost() {
        let model = CostModel::default();
        let stats = test_stats();

        let op = PhysicalOp::EdgeScan {
            edge_type: Some("KNOWS".to_string()),
            estimated_rows: 500,
        };

        let cost = model.estimate(&op, &stats);
        assert!(cost.cpu > 0.0, "EdgeScan should have positive CPU cost");
        assert!(cost.io > 0.0, "EdgeScan should have positive I/O cost");
        assert!(cost.memory > 0, "EdgeScan should have positive memory cost");
    }

    #[test]
    fn test_edge_scan_cardinality() {
        let model = CostModel::default();
        let stats = test_stats();

        let op = PhysicalOp::EdgeScan {
            edge_type: None,
            estimated_rows: 750,
        };
        assert_eq!(model.estimate_cardinality(&op, &stats), 750);
    }

    #[test]
    fn test_edge_scan_cost_matches_node_scan_for_now() {
        // The edge scan cost model currently mirrors node scan.
        // This test documents that contract so we notice when they diverge.
        let model = CostModel::default();
        let stats = test_stats();
        let rows = 200;

        let node_cost = model.estimate(
            &PhysicalOp::NodeScan {
                label: None,
                estimated_rows: rows,
            },
            &stats,
        );
        let edge_cost = model.estimate(
            &PhysicalOp::EdgeScan {
                edge_type: None,
                estimated_rows: rows,
            },
            &stats,
        );

        assert_eq!(node_cost.cpu, edge_cost.cpu);
        assert_eq!(node_cost.io, edge_cost.io);
        assert_eq!(node_cost.memory, edge_cost.memory);
    }
}