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
//! Distributed query executor with scatter-gather.
//!
//! This module implements the execution engine for distributed queries
//! across multiple shards. It handles:
//!
//! - Query distribution (scatter)
//! - Result aggregation (gather)
//! - Timeout handling
//! - Partial failure handling
//!
//! # Architecture
//!
//! ```text
//! ┌──────────────────────────────────────────────────────────────────┐
//! │                     Query Executor                                │
//! │  ┌────────────────────────────────────────────────────────────┐  │
//! │  │  1. Route query to shards (from ShardRouter)              │  │
//! │  │  2. Scatter: Send query to all relevant shards            │  │
//! │  │  3. Wait for responses (with timeout)                      │  │
//! │  │  4. Gather: Aggregate results from all shards             │  │
//! │  │  5. Return combined result                                 │  │
//! │  └────────────────────────────────────────────────────────────┘  │
//! └──────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # ⚠️ Performance Warning: Sequential Execution
//!
//! Currently, the "Scatter" phase is implemented as a **Sequential Send-and-Wait** loop.
//!
//! - **Latency**: Scales linearly with the number of target shards ($O(N)$), not constant ($O(1)$).
//!   Total latency ≈ $\sum \text{latency}(\text{shard}_i)$.
//! - **Throughput**: Limited by the single-threaded dispatch loop.
//!
//! This implementation is suitable for small clusters or low-latency local shards but
//! may become a bottleneck in large distributed deployments. Future versions will
//! implement true parallel scatter-gather using async/await or thread pools.

use super::network::{NetworkError, NetworkResult, ShardClient};
use super::router::{ShardRouter, TraversalPlan};
use super::types::ShardId;
use crate::core::id::NodeId;
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};

/// Error types for query execution.
#[derive(Debug, Clone)]
#[allow(missing_docs)]
pub enum ExecutorError {
    /// All target shards failed.
    AllShardsFailed {
        query_id: u64,
        failures: Vec<(ShardId, NetworkError)>,
    },
    /// Query timed out.
    Timeout {
        query_id: u64,
        timeout: Duration,
        responded: Vec<ShardId>,
        pending: Vec<ShardId>,
    },
    /// Partial failure (some shards succeeded, some failed).
    PartialFailure {
        query_id: u64,
        successes: Vec<ShardId>,
        failures: Vec<(ShardId, NetworkError)>,
    },
    /// No shards available for query.
    NoShardsAvailable,
    /// Invalid query.
    InvalidQuery(String),
    /// Aggregation error.
    AggregationError(String),
}

impl fmt::Display for ExecutorError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ExecutorError::AllShardsFailed { query_id, failures } => {
                write!(
                    f,
                    "Query {} failed on all {} shards",
                    query_id,
                    failures.len()
                )
            }
            ExecutorError::Timeout {
                query_id,
                timeout,
                responded,
                pending,
            } => {
                write!(
                    f,
                    "Query {} timed out after {:?} ({} responded, {} pending)",
                    query_id,
                    timeout,
                    responded.len(),
                    pending.len()
                )
            }
            ExecutorError::PartialFailure {
                query_id,
                successes,
                failures,
            } => {
                write!(
                    f,
                    "Query {} partially failed ({} succeeded, {} failed)",
                    query_id,
                    successes.len(),
                    failures.len()
                )
            }
            ExecutorError::NoShardsAvailable => {
                write!(f, "No shards available for query")
            }
            ExecutorError::InvalidQuery(msg) => {
                write!(f, "Invalid query: {}", msg)
            }
            ExecutorError::AggregationError(msg) => {
                write!(f, "Aggregation error: {}", msg)
            }
        }
    }
}

impl std::error::Error for ExecutorError {}

/// Result type for query execution.
pub type ExecutorResult<T> = Result<T, ExecutorError>;

/// Configuration for the query executor.
#[derive(Debug, Clone)]
pub struct ExecutorConfig {
    /// Default timeout for queries.
    pub default_timeout: Duration,
    /// Maximum concurrent queries per shard.
    pub max_concurrent_per_shard: usize,
    /// Whether to allow partial results on timeout.
    pub allow_partial_results: bool,
    /// Retry failed shards.
    pub retry_failed_shards: bool,
    /// Maximum retries per shard.
    pub max_retries: usize,
}

impl Default for ExecutorConfig {
    fn default() -> Self {
        Self {
            default_timeout: Duration::from_secs(30),
            max_concurrent_per_shard: 100,
            allow_partial_results: false,
            retry_failed_shards: true,
            max_retries: 2,
        }
    }
}

/// A query to be executed across shards.
#[derive(Debug, Clone)]
pub struct DistributedQuery {
    /// Unique query ID.
    pub id: u64,
    /// Serialized query data.
    pub data: Vec<u8>,
    /// Target shards (None = all shards from router).
    pub target_shards: Option<Vec<ShardId>>,
    /// Query timeout (overrides default).
    pub timeout: Option<Duration>,
    /// Aggregation strategy.
    pub aggregation: AggregationStrategy,
}

impl DistributedQuery {
    /// Create a new distributed query.
    pub fn new(id: u64, data: Vec<u8>) -> Self {
        Self {
            id,
            data,
            target_shards: None,
            timeout: None,
            aggregation: AggregationStrategy::Concat,
        }
    }

    /// Set target shards.
    pub fn with_shards(mut self, shards: Vec<ShardId>) -> Self {
        self.target_shards = Some(shards);
        self
    }

    /// Set timeout.
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Set aggregation strategy.
    pub fn with_aggregation(mut self, strategy: AggregationStrategy) -> Self {
        self.aggregation = strategy;
        self
    }
}

/// Strategy for aggregating results from multiple shards.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AggregationStrategy {
    /// Concatenate all results.
    Concat,
    /// Return first non-empty result.
    First,
    /// Merge and deduplicate by node ID.
    MergeNodes,
    /// Sum numeric results.
    Sum,
    /// Count total results.
    Count,
    /// Return all results separately by shard.
    ByShard,
}

/// Result from a single shard.
#[derive(Debug, Clone)]
pub struct ShardResult {
    /// Shard ID.
    pub shard_id: ShardId,
    /// Result data.
    pub data: Vec<u8>,
    /// Execution time on this shard.
    pub execution_time: Duration,
    /// Number of results from this shard.
    pub result_count: usize,
}

/// Aggregated result from all shards.
#[derive(Debug, Clone)]
pub struct QueryResult {
    /// Query ID.
    pub query_id: u64,
    /// Aggregated data.
    pub data: Vec<u8>,
    /// Individual shard results.
    pub shard_results: Vec<ShardResult>,
    /// Total execution time.
    pub total_time: Duration,
    /// Number of shards queried.
    pub shards_queried: usize,
    /// Number of shards that succeeded.
    pub shards_succeeded: usize,
    /// Total result count.
    pub total_results: usize,
}

impl QueryResult {
    /// Check if all shards succeeded.
    pub fn is_complete(&self) -> bool {
        self.shards_queried == self.shards_succeeded
    }

    /// Get the success rate.
    pub fn success_rate(&self) -> f64 {
        if self.shards_queried == 0 {
            1.0
        } else {
            self.shards_succeeded as f64 / self.shards_queried as f64
        }
    }
}

/// Distributed query executor.
#[derive(Debug)]
pub struct QueryExecutor<C: ShardClient> {
    /// Configuration.
    config: ExecutorConfig,
    /// Shard clients indexed by shard ID.
    clients: HashMap<ShardId, Arc<C>>,
    /// Router for query routing.
    router: ShardRouter,
    /// Query ID generator.
    next_query_id: AtomicU64,
    /// Total queries executed.
    queries_executed: AtomicU64,
    /// Total queries failed.
    queries_failed: AtomicU64,
}

impl<C: ShardClient> QueryExecutor<C> {
    /// Create a new query executor.
    pub fn new(config: ExecutorConfig, router: ShardRouter) -> Self {
        Self {
            config,
            clients: HashMap::new(),
            router,
            next_query_id: AtomicU64::new(1),
            queries_executed: AtomicU64::new(0),
            queries_failed: AtomicU64::new(0),
        }
    }

    /// Register a shard client.
    pub fn register_client(&mut self, shard_id: ShardId, client: Arc<C>) {
        self.clients.insert(shard_id, client);
    }

    /// Remove a shard client.
    pub fn unregister_client(&mut self, shard_id: ShardId) {
        self.clients.remove(&shard_id);
    }

    /// Generate a new query ID.
    pub fn next_query_id(&self) -> u64 {
        self.next_query_id.fetch_add(1, Ordering::SeqCst)
    }

    /// Execute a query across shards.
    ///
    /// This method iterates through the target shards **sequentially** and collects
    /// results.
    ///
    /// # Partial Results and Timeouts
    ///
    /// If `allow_partial_results` is enabled and the query times out:
    /// - Results collected *so far* are returned.
    /// - Shards that have not yet been contacted (due to being later in the iteration order)
    ///   are marked as `pending` in the timeout error (or silently omitted if results are returned).
    ///
    /// This means the order of `target_shards` matters: earlier shards are prioritized.
    pub fn execute(&self, query: DistributedQuery) -> ExecutorResult<QueryResult> {
        let start = Instant::now();
        let timeout = query.timeout.unwrap_or(self.config.default_timeout);

        let target_shards: Cow<'_, [ShardId]> = match &query.target_shards {
            Some(shards) => Cow::Borrowed(shards),
            None => Cow::Owned(self.clients.keys().copied().collect()),
        };

        if target_shards.is_empty() {
            return Err(ExecutorError::NoShardsAvailable);
        }

        // Scatter: Execute query on all target shards
        let mut results: Vec<ShardResult> = Vec::with_capacity(target_shards.len());
        let mut failures: Vec<(ShardId, NetworkError)> = Vec::new();

        for shard_id in target_shards.as_ref() {
            if start.elapsed() >= timeout {
                // Timed out before sending to all shards
                let pending: Vec<_> = target_shards
                    .iter()
                    .filter(|s| !results.iter().any(|r| r.shard_id == **s))
                    .copied()
                    .collect();

                if self.config.allow_partial_results && !results.is_empty() {
                    break;
                }

                return Err(ExecutorError::Timeout {
                    query_id: query.id,
                    timeout,
                    responded: results.iter().map(|r| r.shard_id).collect(),
                    pending,
                });
            }

            let result = self.execute_on_shard(*shard_id, &query);

            match result {
                Ok(shard_result) => {
                    results.push(shard_result);
                }
                Err(err) => {
                    failures.push((*shard_id, err));
                }
            }
        }

        // Check for complete failure
        if results.is_empty() {
            self.queries_failed.fetch_add(1, Ordering::Relaxed);
            return Err(ExecutorError::AllShardsFailed {
                query_id: query.id,
                failures,
            });
        }

        // Check for partial failure
        if !failures.is_empty() && !self.config.allow_partial_results {
            self.queries_failed.fetch_add(1, Ordering::Relaxed);
            return Err(ExecutorError::PartialFailure {
                query_id: query.id,
                successes: results.iter().map(|r| r.shard_id).collect(),
                failures,
            });
        }

        // Gather: Aggregate results
        let aggregated = self.aggregate_results(&query, &results)?;

        let total_results: usize = results.iter().map(|r| r.result_count).sum();
        let total_time = start.elapsed();

        self.queries_executed.fetch_add(1, Ordering::Relaxed);

        let shards_succeeded = results.len(); // Optimization note

        Ok(QueryResult {
            query_id: query.id,
            data: aggregated,
            shard_results: results,
            total_time,
            shards_queried: target_shards.len(),
            shards_succeeded,
            total_results,
        })
    }

    /// Execute a graph traversal across shards.
    pub fn execute_traversal(
        &self,
        _start_node: NodeId,
        start_label: &str,
        target_labels: &[&str],
    ) -> ExecutorResult<QueryResult> {
        let plan = self.router.route_traversal(start_label, target_labels);

        // Build query from traversal plan
        let query_id = self.next_query_id();
        let query_data = self.serialize_traversal_plan(&plan);

        let target_shards: Vec<_> = plan.involved_shards.iter().copied().collect();

        let query = DistributedQuery::new(query_id, query_data)
            .with_shards(target_shards)
            .with_aggregation(AggregationStrategy::MergeNodes);

        self.execute(query)
    }

    /// Get executor statistics.
    pub fn stats(&self) -> ExecutorStats {
        ExecutorStats {
            queries_executed: self.queries_executed.load(Ordering::Relaxed),
            queries_failed: self.queries_failed.load(Ordering::Relaxed),
            registered_clients: self.clients.len(),
        }
    }

    // ==================== Private Methods ====================

    fn execute_on_shard(
        &self,
        shard_id: ShardId,
        query: &DistributedQuery,
    ) -> NetworkResult<ShardResult> {
        let client = self
            .clients
            .get(&shard_id)
            .ok_or(NetworkError::ShardUnavailable(shard_id))?;

        let start = Instant::now();
        let data = client.query(query.id, &query.data)?;
        let execution_time = start.elapsed();

        // Parse result count from data (assume first 4 bytes = count)
        let result_count = if data.len() >= 4 {
            u32::from_le_bytes([data[0], data[1], data[2], data[3]]) as usize
        } else {
            0
        };

        Ok(ShardResult {
            shard_id,
            data,
            execution_time,
            result_count,
        })
    }

    fn aggregate_results(
        &self,
        query: &DistributedQuery,
        results: &[ShardResult],
    ) -> ExecutorResult<Vec<u8>> {
        match query.aggregation {
            AggregationStrategy::Concat => {
                // Simple concatenation
                let mut aggregated = Vec::with_capacity(results.iter().map(|r| r.data.len()).sum());
                for result in results {
                    aggregated.extend(&result.data);
                }
                Ok(aggregated)
            }
            AggregationStrategy::First => {
                // Return first non-empty result
                for result in results {
                    if !result.data.is_empty() {
                        return Ok(result.data.clone());
                    }
                }
                Ok(Vec::new())
            }
            AggregationStrategy::MergeNodes => {
                // Merge and deduplicate (simplified - real impl would parse nodes)
                // Avoids O(N) heap allocations by only cloning the largest result once.
                let best_result = results.iter().max_by_key(|r| r.data.len());
                match best_result {
                    Some(res) if !res.data.is_empty() => Ok(res.data.clone()),
                    _ => Ok(Vec::new()),
                }
            }
            AggregationStrategy::Sum => {
                // Sum numeric results
                let mut total: u64 = 0;
                for result in results {
                    if result.data.len() >= 8 {
                        let value = u64::from_le_bytes([
                            result.data[0],
                            result.data[1],
                            result.data[2],
                            result.data[3],
                            result.data[4],
                            result.data[5],
                            result.data[6],
                            result.data[7],
                        ]);
                        total += value;
                    }
                }
                Ok(total.to_le_bytes().to_vec())
            }
            AggregationStrategy::Count => {
                // Count total results
                let total: usize = results.iter().map(|r| r.result_count).sum();
                Ok((total as u64).to_le_bytes().to_vec())
            }
            AggregationStrategy::ByShard => {
                // Return results separately (serialize shard -> data mapping)
                let capacity: usize = results.iter().map(|r| r.data.len()).sum();
                let mut aggregated = Vec::with_capacity(capacity + 4 + (results.len() * 6));
                aggregated.extend_from_slice(&(results.len() as u32).to_le_bytes());
                for result in results {
                    aggregated.extend_from_slice(&result.shard_id.as_u16().to_le_bytes());
                    aggregated.extend_from_slice(&(result.data.len() as u32).to_le_bytes());
                    aggregated.extend(&result.data);
                }
                Ok(aggregated)
            }
        }
    }

    fn serialize_traversal_plan(&self, plan: &TraversalPlan) -> Vec<u8> {
        const STEP_COUNT_SIZE: usize = size_of::<u32>();
        const SHARD_ID_SIZE: usize = size_of::<u16>();
        const LABEL_COUNT_SIZE: usize = size_of::<u32>();
        const LABEL_LEN_SIZE: usize = size_of::<u32>();
        const CROSS_SHARD_FLAG_SIZE: usize = size_of::<u8>();

        let capacity = STEP_COUNT_SIZE
            + plan
                .steps
                .iter()
                .map(|step| {
                    SHARD_ID_SIZE
                        + LABEL_COUNT_SIZE
                        + step
                            .edge_labels
                            .iter()
                            .map(|label| LABEL_LEN_SIZE + label.len())
                            .sum::<usize>()
                        + CROSS_SHARD_FLAG_SIZE
                })
                .sum::<usize>();

        let mut data = Vec::with_capacity(capacity);
        data.extend_from_slice(&(plan.steps.len() as u32).to_le_bytes());
        for step in &plan.steps {
            data.extend_from_slice(&step.shard_id.as_u16().to_le_bytes());
            data.extend_from_slice(&(step.edge_labels.len() as u32).to_le_bytes());
            for label in &step.edge_labels {
                data.extend_from_slice(&(label.len() as u32).to_le_bytes());
                data.extend_from_slice(label.as_bytes());
            }
            data.push(if step.may_cross_shard { 1 } else { 0 });
        }
        data
    }
}

/// Statistics for the query executor.
#[derive(Debug, Clone)]
pub struct ExecutorStats {
    /// Total queries executed.
    pub queries_executed: u64,
    /// Total queries failed.
    pub queries_failed: u64,
    /// Number of registered clients.
    pub registered_clients: usize,
}

impl ExecutorStats {
    /// Calculate success rate.
    pub fn success_rate(&self) -> f64 {
        let total = self.queries_executed + self.queries_failed;
        if total == 0 {
            1.0
        } else {
            self.queries_executed as f64 / total as f64
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::sharding::config::{ShardConfig, ShardDefinition};
    use crate::storage::sharding::network::MockShardClient;

    fn make_shard_id(id: u16) -> ShardId {
        ShardId::new(id).unwrap()
    }

    fn test_config() -> ShardConfig {
        ShardConfig::new(vec![
            ShardDefinition::new(0, "shard0:9000", vec!["Person", "User", "Account"]),
            ShardDefinition::new(1, "shard1:9000", vec!["Place", "Location", "Address"]),
            ShardDefinition::new(2, "shard2:9000", vec!["Event", "Transaction", "Activity"]),
        ])
    }

    fn test_router() -> ShardRouter {
        ShardRouter::new(test_config())
    }

    // ==================== ExecutorError Tests ====================

    #[test]
    fn test_executor_error_display() {
        let err = ExecutorError::NoShardsAvailable;
        assert!(format!("{}", err).contains("No shards"));

        let err = ExecutorError::InvalidQuery("bad".into());
        assert!(format!("{}", err).contains("Invalid"));

        let err = ExecutorError::AllShardsFailed {
            query_id: 1,
            failures: vec![],
        };
        assert!(format!("{}", err).contains("failed on all"));
    }

    // ==================== DistributedQuery Tests ====================

    #[test]
    fn test_distributed_query_creation() {
        let query = DistributedQuery::new(1, vec![1, 2, 3]);
        assert_eq!(query.id, 1);
        assert_eq!(query.data, vec![1, 2, 3]);
        assert!(query.target_shards.is_none());
    }

    #[test]
    fn test_distributed_query_builders() {
        let query = DistributedQuery::new(1, vec![])
            .with_shards(vec![make_shard_id(0), make_shard_id(1)])
            .with_timeout(Duration::from_secs(10))
            .with_aggregation(AggregationStrategy::Sum);

        assert_eq!(query.target_shards.unwrap().len(), 2);
        assert_eq!(query.timeout.unwrap(), Duration::from_secs(10));
        assert_eq!(query.aggregation, AggregationStrategy::Sum);
    }

    // ==================== QueryResult Tests ====================

    #[test]
    fn test_query_result_complete() {
        let result = QueryResult {
            query_id: 1,
            data: vec![],
            shard_results: vec![],
            total_time: Duration::from_millis(100),
            shards_queried: 3,
            shards_succeeded: 3,
            total_results: 10,
        };

        assert!(result.is_complete());
        assert!((result.success_rate() - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_query_result_partial() {
        let result = QueryResult {
            query_id: 1,
            data: vec![],
            shard_results: vec![],
            total_time: Duration::from_millis(100),
            shards_queried: 3,
            shards_succeeded: 2,
            total_results: 5,
        };

        assert!(!result.is_complete());
        assert!((result.success_rate() - 0.666).abs() < 0.01);
    }

    // ==================== QueryExecutor Tests ====================

    #[test]
    fn test_executor_creation() {
        let executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        assert_eq!(executor.stats().registered_clients, 0);
        assert_eq!(executor.stats().queries_executed, 0);
    }

    #[test]
    fn test_executor_register_client() {
        let mut executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        let client = Arc::new(MockShardClient::new(make_shard_id(0)));
        executor.register_client(make_shard_id(0), client);

        assert_eq!(executor.stats().registered_clients, 1);
    }

    #[test]
    fn test_executor_unregister_client() {
        let mut executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        let client = Arc::new(MockShardClient::new(make_shard_id(0)));
        executor.register_client(make_shard_id(0), client);
        executor.unregister_client(make_shard_id(0));

        assert_eq!(executor.stats().registered_clients, 0);
    }

    #[test]
    fn test_executor_no_shards() {
        let executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        let query = DistributedQuery::new(1, vec![]);
        let result = executor.execute(query);

        assert!(matches!(result, Err(ExecutorError::NoShardsAvailable)));
    }

    #[test]
    fn test_executor_single_shard_success() {
        let mut executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        let client = Arc::new(MockShardClient::new(make_shard_id(0)));
        executor.register_client(make_shard_id(0), client);

        let query = DistributedQuery::new(1, vec![1, 2, 3]).with_shards(vec![make_shard_id(0)]);

        let result = executor.execute(query).unwrap();
        assert_eq!(result.shards_queried, 1);
        assert_eq!(result.shards_succeeded, 1);
        assert!(result.is_complete());
    }

    #[test]
    fn test_executor_multiple_shards_success() {
        let mut executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        for i in 0..3 {
            let client = Arc::new(MockShardClient::new(make_shard_id(i)));
            executor.register_client(make_shard_id(i), client);
        }

        let query = DistributedQuery::new(1, vec![]).with_shards(vec![
            make_shard_id(0),
            make_shard_id(1),
            make_shard_id(2),
        ]);

        let result = executor.execute(query).unwrap();
        assert_eq!(result.shards_queried, 3);
        assert_eq!(result.shards_succeeded, 3);
    }

    #[test]
    fn test_executor_partial_failure() {
        let mut executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        let client0 = Arc::new(MockShardClient::new(make_shard_id(0)));
        let client1 = Arc::new(MockShardClient::new(make_shard_id(1)));
        client1.set_healthy(false);

        executor.register_client(make_shard_id(0), client0);
        executor.register_client(make_shard_id(1), client1);

        let query =
            DistributedQuery::new(1, vec![]).with_shards(vec![make_shard_id(0), make_shard_id(1)]);

        let result = executor.execute(query);
        assert!(matches!(result, Err(ExecutorError::PartialFailure { .. })));
    }

    #[test]
    fn test_executor_partial_allowed() {
        let config = ExecutorConfig {
            allow_partial_results: true,
            ..Default::default()
        };
        let mut executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(config, test_router());

        let client0 = Arc::new(MockShardClient::new(make_shard_id(0)));
        let client1 = Arc::new(MockShardClient::new(make_shard_id(1)));
        client1.set_healthy(false);

        executor.register_client(make_shard_id(0), client0);
        executor.register_client(make_shard_id(1), client1);

        let query =
            DistributedQuery::new(1, vec![]).with_shards(vec![make_shard_id(0), make_shard_id(1)]);

        let result = executor.execute(query).unwrap();
        assert_eq!(result.shards_queried, 2);
        assert_eq!(result.shards_succeeded, 1);
    }

    #[test]
    fn test_executor_all_failed() {
        let mut executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        let client0 = Arc::new(MockShardClient::new(make_shard_id(0)));
        client0.set_healthy(false);

        executor.register_client(make_shard_id(0), client0);

        let query = DistributedQuery::new(1, vec![]).with_shards(vec![make_shard_id(0)]);

        let result = executor.execute(query);
        assert!(matches!(result, Err(ExecutorError::AllShardsFailed { .. })));
    }

    // ==================== Aggregation Tests ====================

    #[test]
    fn test_aggregation_concat() {
        let mut executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        let client0 = Arc::new(MockShardClient::new(make_shard_id(0)));
        let client1 = Arc::new(MockShardClient::new(make_shard_id(1)));

        executor.register_client(make_shard_id(0), client0);
        executor.register_client(make_shard_id(1), client1);

        let query = DistributedQuery::new(1, vec![])
            .with_shards(vec![make_shard_id(0), make_shard_id(1)])
            .with_aggregation(AggregationStrategy::Concat);

        let result = executor.execute(query).unwrap();
        assert!(result.is_complete());
    }

    #[test]
    fn test_aggregation_merge_nodes() {
        let mut executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        let client0 = Arc::new(MockShardClient::new(make_shard_id(0)));
        // Simulate client0 returning some data
        client0.set_query_response(vec![1, 2, 3]);

        let client1 = Arc::new(MockShardClient::new(make_shard_id(1)));
        // Simulate client1 returning a larger vector, so the merge node logic actually matches it
        client1.set_query_response(vec![1, 2, 3, 4, 5]);

        executor.register_client(make_shard_id(0), client0);
        executor.register_client(make_shard_id(1), client1);

        let query = DistributedQuery::new(1, vec![])
            .with_shards(vec![make_shard_id(0), make_shard_id(1)])
            .with_aggregation(AggregationStrategy::MergeNodes);

        let result = executor.execute(query).unwrap();
        assert!(result.is_complete());
        // Aggregation should select the largest result
        assert_eq!(result.data, vec![1, 2, 3, 4, 5]);
    }

    #[test]
    fn test_aggregation_count() {
        let mut executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        let client = Arc::new(MockShardClient::new(make_shard_id(0)));
        executor.register_client(make_shard_id(0), client);

        let query = DistributedQuery::new(1, vec![])
            .with_shards(vec![make_shard_id(0)])
            .with_aggregation(AggregationStrategy::Count);

        let result = executor.execute(query).unwrap();
        assert_eq!(result.data.len(), 8); // u64
    }

    #[test]
    fn test_aggregation_by_shard() {
        let mut executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        let client0 = Arc::new(MockShardClient::new(make_shard_id(0)));
        let client1 = Arc::new(MockShardClient::new(make_shard_id(1)));

        executor.register_client(make_shard_id(0), client0);
        executor.register_client(make_shard_id(1), client1);

        let query = DistributedQuery::new(1, vec![])
            .with_shards(vec![make_shard_id(0), make_shard_id(1)])
            .with_aggregation(AggregationStrategy::ByShard);

        let result = executor.execute(query).unwrap();

        // Verify structure: [count: u32] ([shard_id: u16] [len: u32] [data...])*
        let mut offset = 0;

        // Count
        let shard_count = u32::from_le_bytes([
            result.data[offset],
            result.data[offset + 1],
            result.data[offset + 2],
            result.data[offset + 3],
        ]);
        offset += 4;
        assert_eq!(shard_count, 2);

        // Result 1
        let shard_id_1 = u16::from_le_bytes([result.data[offset], result.data[offset + 1]]);
        offset += 2;
        // MockClient returns empty vec by default for query
        let len_1 = u32::from_le_bytes([
            result.data[offset],
            result.data[offset + 1],
            result.data[offset + 2],
            result.data[offset + 3],
        ]);
        offset += 4;
        assert_eq!(len_1, 0);

        // Result 2
        let shard_id_2 = u16::from_le_bytes([result.data[offset], result.data[offset + 1]]);
        offset += 2;
        let len_2 = u32::from_le_bytes([
            result.data[offset],
            result.data[offset + 1],
            result.data[offset + 2],
            result.data[offset + 3],
        ]);
        // offset += 4; // Not needed for further checks
        assert_eq!(len_2, 0);

        // Check IDs are valid (0 and 1, order depends on iteration)
        assert!(shard_id_1 == 0 || shard_id_1 == 1);
        assert!(shard_id_2 == 0 || shard_id_2 == 1);
        assert_ne!(shard_id_1, shard_id_2);
    }

    // ==================== Serialization Tests ====================

    #[test]
    fn test_serialize_traversal_plan() {
        use crate::storage::sharding::router::{TraversalPlan, TraversalStep};
        use std::collections::HashSet;

        let executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        let mut plan = TraversalPlan {
            start_shard: make_shard_id(0),
            involved_shards: HashSet::new(),
            steps: vec![],
            is_distributed: false,
            estimated_cost: 0.0,
        };

        plan.involved_shards.insert(make_shard_id(0));
        plan.involved_shards.insert(make_shard_id(1));

        plan.steps.push(TraversalStep {
            shard_id: make_shard_id(0),
            edge_labels: vec!["KNOWS".to_string(), "FRIEND".to_string()],
            may_cross_shard: true,
        });

        plan.steps.push(TraversalStep {
            shard_id: make_shard_id(1),
            edge_labels: vec!["WORKS_AT".to_string()],
            may_cross_shard: false,
        });

        let serialized = executor.serialize_traversal_plan(&plan);

        // Expected length calculation
        // 4 bytes for number of steps
        // Step 1: 2 (shard_id) + 4 (num labels) + (4 + 5) (KNOWS) + (4 + 6) (FRIEND) + 1 (cross shard) = 26 bytes
        // Step 2: 2 (shard_id) + 4 (num labels) + (4 + 8) (WORKS_AT) + 1 (cross shard) = 19 bytes
        // Total = 4 + 26 + 19 = 49 bytes
        assert_eq!(serialized.len(), 49);

        let mut offset = 0;

        // Num steps
        let num_steps = u32::from_le_bytes([
            serialized[offset],
            serialized[offset + 1],
            serialized[offset + 2],
            serialized[offset + 3],
        ]);
        assert_eq!(num_steps, 2);
        offset += 4;

        // Step 1
        let shard_1 = u16::from_le_bytes([serialized[offset], serialized[offset + 1]]);
        assert_eq!(shard_1, 0);
        offset += 2;

        let num_labels_1 = u32::from_le_bytes([
            serialized[offset],
            serialized[offset + 1],
            serialized[offset + 2],
            serialized[offset + 3],
        ]);
        assert_eq!(num_labels_1, 2);
        offset += 4;

        // Label 1
        let label_1_len = u32::from_le_bytes([
            serialized[offset],
            serialized[offset + 1],
            serialized[offset + 2],
            serialized[offset + 3],
        ]);
        assert_eq!(label_1_len, 5);
        offset += 4;

        let label_1 = std::str::from_utf8(&serialized[offset..offset + 5]).unwrap();
        assert_eq!(label_1, "KNOWS");

        // Skip rest of checks, ensuring the capacity matches actual output is the main goal
        assert_eq!(serialized.capacity(), serialized.len());
    }

    // ==================== ExecutorStats Tests ====================

    #[test]
    fn test_executor_stats() {
        let mut executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        let client = Arc::new(MockShardClient::new(make_shard_id(0)));
        executor.register_client(make_shard_id(0), client);

        // Execute a successful query
        let query = DistributedQuery::new(1, vec![]).with_shards(vec![make_shard_id(0)]);
        let _ = executor.execute(query);

        let stats = executor.stats();
        assert_eq!(stats.queries_executed, 1);
        assert_eq!(stats.queries_failed, 0);
        assert!((stats.success_rate() - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_executor_stats_with_failures() {
        let mut executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        let client = Arc::new(MockShardClient::new(make_shard_id(0)));
        client.set_healthy(false);
        executor.register_client(make_shard_id(0), client);

        // Execute a failing query
        let query = DistributedQuery::new(1, vec![]).with_shards(vec![make_shard_id(0)]);
        let _ = executor.execute(query);

        let stats = executor.stats();
        assert_eq!(stats.queries_failed, 1);
    }

    #[test]
    fn test_query_id_generation() {
        let executor: QueryExecutor<MockShardClient> =
            QueryExecutor::new(ExecutorConfig::default(), test_router());

        let id1 = executor.next_query_id();
        let id2 = executor.next_query_id();
        let id3 = executor.next_query_id();

        assert_eq!(id1, 1);
        assert_eq!(id2, 2);
        assert_eq!(id3, 3);
    }

    #[test]
    fn test_execute_traversal() {
        // 🛡️ Sentry test: This tests that `execute_traversal` correctly populates `target_shards`
        // from the `TraversalPlan::involved_shards`. Previously, this would crash with `NoShardsAvailable`
        // because it erroneously read from `plan.steps` which is intentionally empty for multi-shard plans.
        let router = test_router();
        let config = ExecutorConfig::default();
        let mut executor = QueryExecutor::new(config, router);

        let shard0 = make_shard_id(0);
        let shard1 = make_shard_id(1);

        executor.register_client(shard0, Arc::new(MockShardClient::new(shard0)));
        executor.register_client(shard1, Arc::new(MockShardClient::new(shard1)));

        let start_node = NodeId::new(42).unwrap();
        // Person maps to shard0. Place maps to shard1. Company is not in test_config, so it won't map to anything and route_node probably hashes it or falls back?
        // Let's use "Place" to ensure we hit shard1 as well.
        let result = executor
            .execute_traversal(start_node, "Person", &["Place"])
            .unwrap();

        assert_eq!(result.shards_queried, 2);
    }
}