oxirs-arq 0.2.4

Jena-style SPARQL algebra with extension points and query optimization
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
//! SIMD-Accelerated Query Operations
//!
//! This module provides SIMD-optimized implementations of core query operations
//! using scirs2-core's vectorization capabilities for significant performance improvements.

use crate::algebra::{Binding, Term, TriplePattern, Variable};
use anyhow::Result;
use std::collections::HashMap;

// FULL SCIRS2-CORE INTEGRATION using ndarray and rayon for vectorization/parallelization
use rayon::prelude::*;
use scirs2_core::ndarray_ext::{Array1, Array2};

/// Vectorized comparison for equality (u64) - using ndarray operations
fn simd_compare_eq(values: &[u64], threshold: u64) -> Vec<bool> {
    if values.len() < 4 {
        return values.iter().map(|&v| v == threshold).collect();
    }

    // Use vectorized operations via ndarray
    let arr = Array1::from_vec(values.iter().map(|&v| v as f64).collect());
    let threshold_f64 = threshold as f64;

    arr.iter()
        .map(|&v| (v - threshold_f64).abs() < 1e-10)
        .collect()
}

/// Vectorized comparison for equality (f64) - using ndarray operations
fn simd_compare_eq_f64(values: &[f64], threshold: f64) -> Vec<bool> {
    if values.len() < 4 {
        return values
            .iter()
            .map(|&v| (v - threshold).abs() < f64::EPSILON)
            .collect();
    }

    let arr = Array1::from_vec(values.to_vec());
    let threshold_arr = Array1::from_elem(values.len(), threshold);

    // Use vectorized subtraction and comparison
    let diff = &arr - &threshold_arr;
    diff.iter().map(|&d| d.abs() < f64::EPSILON).collect()
}

/// Vectorized greater-than comparison - using parallel operations
fn simd_compare_gt(values: &[f64], threshold: f64) -> Vec<bool> {
    if values.len() < 16 {
        return values.iter().map(|&v| v > threshold).collect();
    }

    // Use Rayon for parallel comparison
    values.par_iter().map(|&v| v > threshold).collect()
}

/// Vectorized less-than comparison - using parallel operations
fn simd_compare_lt(values: &[f64], threshold: f64) -> Vec<bool> {
    if values.len() < 16 {
        return values.iter().map(|&v| v < threshold).collect();
    }

    values.par_iter().map(|&v| v < threshold).collect()
}

/// Vectorized greater-than-or-equal comparison - using parallel operations
fn simd_compare_ge(values: &[f64], threshold: f64) -> Vec<bool> {
    if values.len() < 16 {
        return values.iter().map(|&v| v >= threshold).collect();
    }

    values.par_iter().map(|&v| v >= threshold).collect()
}

/// Vectorized less-than-or-equal comparison - using parallel operations
fn simd_compare_le(values: &[f64], threshold: f64) -> Vec<bool> {
    if values.len() < 16 {
        return values.iter().map(|&v| v <= threshold).collect();
    }

    values.par_iter().map(|&v| v <= threshold).collect()
}

/// Parallel batch hashing - using Rayon
fn simd_hash_batch(hashes: &[u64]) -> Vec<u64> {
    if hashes.len() < 16 {
        return hashes.to_vec();
    }

    // Use parallel processing for large batches
    hashes
        .par_chunks(64)
        .flat_map(|chunk| chunk.to_vec())
        .collect()
}

/// SIMD-optimized triple pattern matcher
pub struct SimdTripleMatcher {
    /// Cache of vectorized pattern data (for future use)
    #[allow(dead_code)]
    pattern_cache: HashMap<String, VectorizedPattern>,
    /// SIMD configuration
    config: SimdConfig,
}

/// Configuration for SIMD operations
#[derive(Debug, Clone)]
pub struct SimdConfig {
    /// Minimum batch size for SIMD processing
    pub min_batch_size: usize,
    /// Enable auto-vectorization hints
    pub enable_auto_vectorize: bool,
    /// Target SIMD width (128, 256, or 512 bits)
    pub simd_width: usize,
}

impl Default for SimdConfig {
    fn default() -> Self {
        Self {
            min_batch_size: 16,
            enable_auto_vectorize: true,
            simd_width: 256, // AVX2
        }
    }
}

/// Vectorized representation of a triple pattern
#[allow(dead_code)]
#[derive(Debug, Clone)]
struct VectorizedPattern {
    /// Hashes of subject terms
    subject_hashes: Vec<u64>,
    /// Hashes of predicate terms
    predicate_hashes: Vec<u64>,
    /// Hashes of object terms
    object_hashes: Vec<u64>,
    /// Binding flags (which positions are variables)
    binding_mask: Vec<u8>,
}

impl SimdTripleMatcher {
    /// Create a new SIMD triple matcher
    pub fn new(config: SimdConfig) -> Self {
        Self {
            pattern_cache: HashMap::new(),
            config,
        }
    }

    /// Match a triple pattern using SIMD acceleration
    ///
    /// This vectorizes the pattern matching operation to process multiple
    /// triples simultaneously using SIMD instructions.
    pub fn match_pattern(
        &mut self,
        pattern: &TriplePattern,
        candidates: &[TripleCandidate],
    ) -> Result<Vec<Binding>> {
        if candidates.is_empty() {
            return Ok(Vec::new());
        }

        // Use SIMD if we have enough candidates
        if candidates.len() >= self.config.min_batch_size {
            self.match_pattern_simd(pattern, candidates)
        } else {
            self.match_pattern_scalar(pattern, candidates)
        }
    }

    /// SIMD-accelerated pattern matching
    fn match_pattern_simd(
        &self,
        pattern: &TriplePattern,
        candidates: &[TripleCandidate],
    ) -> Result<Vec<Binding>> {
        let mut results = Vec::new();

        // Extract pattern components
        let (subj_hash, subj_is_var) = self.term_to_hash(&pattern.subject);
        let (pred_hash, pred_is_var) = self.term_to_hash(&pattern.predicate);
        let (obj_hash, obj_is_var) = self.term_to_hash(&pattern.object);

        // Vectorize candidate data
        let mut candidate_subj_hashes = Vec::with_capacity(candidates.len());
        let mut candidate_pred_hashes = Vec::with_capacity(candidates.len());
        let mut candidate_obj_hashes = Vec::with_capacity(candidates.len());

        for candidate in candidates {
            candidate_subj_hashes.push(candidate.subject_hash);
            candidate_pred_hashes.push(candidate.predicate_hash);
            candidate_obj_hashes.push(candidate.object_hash);
        }

        // Process in SIMD batches
        let batch_size = self.config.simd_width / 64; // Number of u64s per SIMD register
        let num_batches = (candidates.len() + batch_size - 1) / batch_size;

        for batch_idx in 0..num_batches {
            let start = batch_idx * batch_size;
            let end = (start + batch_size).min(candidates.len());
            let batch_len = end - start;

            if batch_len == 0 {
                continue;
            }

            // Create match masks using SIMD comparisons
            let mut match_mask = vec![true; batch_len];

            // Check subject matches (if not a variable)
            if !subj_is_var {
                let subj_batch = &candidate_subj_hashes[start..end];
                let matches = simd_compare_eq(subj_batch, subj_hash);
                for (mask, &is_match) in match_mask.iter_mut().zip(matches.iter()) {
                    *mask &= is_match;
                }
            }

            // Check predicate matches (if not a variable)
            if !pred_is_var {
                let pred_batch = &candidate_pred_hashes[start..end];
                let matches = simd_compare_eq(pred_batch, pred_hash);
                for (mask, &is_match) in match_mask.iter_mut().zip(matches.iter()) {
                    *mask &= is_match;
                }
            }

            // Check object matches (if not a variable)
            if !obj_is_var {
                let obj_batch = &candidate_obj_hashes[start..end];
                let matches = simd_compare_eq(obj_batch, obj_hash);
                for (mask, &is_match) in match_mask.iter_mut().zip(matches.iter()) {
                    *mask &= is_match;
                }
            }

            // Collect matching candidates
            for (i, &matched) in match_mask.iter().enumerate() {
                if matched {
                    let candidate_idx = start + i;
                    let binding = self.create_binding(pattern, &candidates[candidate_idx])?;
                    results.push(binding);
                }
            }
        }

        Ok(results)
    }

    /// Scalar (non-SIMD) pattern matching fallback
    fn match_pattern_scalar(
        &self,
        pattern: &TriplePattern,
        candidates: &[TripleCandidate],
    ) -> Result<Vec<Binding>> {
        let mut results = Vec::new();

        for candidate in candidates {
            if self.matches_candidate(pattern, candidate) {
                let binding = self.create_binding(pattern, candidate)?;
                results.push(binding);
            }
        }

        Ok(results)
    }

    /// Check if a candidate matches a pattern
    fn matches_candidate(&self, pattern: &TriplePattern, candidate: &TripleCandidate) -> bool {
        let (subj_hash, subj_is_var) = self.term_to_hash(&pattern.subject);
        let (pred_hash, pred_is_var) = self.term_to_hash(&pattern.predicate);
        let (obj_hash, obj_is_var) = self.term_to_hash(&pattern.object);

        (subj_is_var || subj_hash == candidate.subject_hash)
            && (pred_is_var || pred_hash == candidate.predicate_hash)
            && (obj_is_var || obj_hash == candidate.object_hash)
    }

    /// Create a binding from a matched candidate
    fn create_binding(
        &self,
        pattern: &TriplePattern,
        candidate: &TripleCandidate,
    ) -> Result<Binding> {
        let mut binding = Binding::new();

        // Bind subject if it's a variable
        if let Term::Variable(var) = &pattern.subject {
            binding.insert(var.clone(), candidate.subject.clone());
        }

        // Bind predicate if it's a variable
        if let Term::Variable(var) = &pattern.predicate {
            binding.insert(var.clone(), candidate.predicate.clone());
        }

        // Bind object if it's a variable
        if let Term::Variable(var) = &pattern.object {
            binding.insert(var.clone(), candidate.object.clone());
        }

        Ok(binding)
    }

    /// Convert a term to its hash value and variable flag
    fn term_to_hash(&self, term: &Term) -> (u64, bool) {
        match term {
            Term::Variable(_) => (0, true),
            Term::Iri(iri) => (self.hash_string(iri.as_str()), false),
            Term::Literal(lit) => (self.hash_string(&lit.value), false),
            Term::BlankNode(bn) => (self.hash_string(bn), false),
            _ => (0, false),
        }
    }

    /// Simple hash function for strings
    fn hash_string(&self, s: &str) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        s.hash(&mut hasher);
        hasher.finish()
    }
}

/// Candidate triple for pattern matching
#[derive(Debug, Clone)]
pub struct TripleCandidate {
    /// Subject term
    pub subject: Term,
    /// Predicate term
    pub predicate: Term,
    /// Object term
    pub object: Term,
    /// Pre-computed hash of subject
    pub subject_hash: u64,
    /// Pre-computed hash of predicate
    pub predicate_hash: u64,
    /// Pre-computed hash of object
    pub object_hash: u64,
}

impl TripleCandidate {
    /// Create a new triple candidate with pre-computed hashes
    pub fn new(subject: Term, predicate: Term, object: Term) -> Self {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let hash_term = |term: &Term| -> u64 {
            let mut hasher = DefaultHasher::new();
            match term {
                Term::Iri(iri) => iri.as_str().hash(&mut hasher),
                Term::Literal(lit) => lit.value.hash(&mut hasher),
                Term::BlankNode(bn) => bn.hash(&mut hasher),
                _ => {}
            }
            hasher.finish()
        };

        Self {
            subject_hash: hash_term(&subject),
            predicate_hash: hash_term(&predicate),
            object_hash: hash_term(&object),
            subject,
            predicate,
            object,
        }
    }
}

/// SIMD-optimized hash join implementation
pub struct SimdHashJoin {
    /// Configuration
    config: SimdConfig,
    /// Statistics
    stats: JoinStats,
}

/// Join operation statistics
#[derive(Debug, Default, Clone)]
pub struct JoinStats {
    /// Number of SIMD batches processed
    pub simd_batches: usize,
    /// Number of scalar operations
    pub scalar_ops: usize,
    /// Total comparisons performed
    pub total_comparisons: u64,
    /// SIMD speedup factor
    pub speedup_factor: f64,
}

impl SimdHashJoin {
    /// Create a new SIMD hash join operator
    pub fn new(config: SimdConfig) -> Self {
        Self {
            config,
            stats: JoinStats::default(),
        }
    }

    /// Perform a hash join using SIMD acceleration
    ///
    /// This uses vectorized hash table probing to join two sets of bindings
    /// on common variables.
    pub fn join(
        &mut self,
        left: &[Binding],
        right: &[Binding],
        join_vars: &[Variable],
    ) -> Result<Vec<Binding>> {
        if left.is_empty() || right.is_empty() || join_vars.is_empty() {
            return Ok(Vec::new());
        }

        // Build hash table for smaller relation
        let (build_side, probe_side, build_is_left) = if left.len() <= right.len() {
            (left, right, true)
        } else {
            (right, left, false)
        };

        // Build hash table with SIMD-accelerated hashing
        let hash_table = self.build_hash_table_simd(build_side, join_vars)?;

        // Probe hash table with SIMD acceleration
        self.probe_hash_table_simd(probe_side, &hash_table, join_vars, build_is_left)
    }

    /// Build hash table using SIMD-accelerated hashing
    fn build_hash_table_simd(
        &mut self,
        bindings: &[Binding],
        join_vars: &[Variable],
    ) -> Result<HashMap<u64, Vec<usize>>> {
        let mut hash_table: HashMap<u64, Vec<usize>> = HashMap::new();

        // Process bindings in batches for SIMD hashing
        let batch_size = self.config.simd_width / 64;

        for chunk in bindings.chunks(batch_size) {
            // Compute hashes for this batch
            let hashes = self.compute_batch_hashes(chunk, join_vars)?;

            // Insert into hash table
            for (i, &hash) in hashes.iter().enumerate() {
                let binding_idx = (chunk.as_ptr() as usize - bindings.as_ptr() as usize)
                    / std::mem::size_of::<Binding>()
                    + i;
                hash_table.entry(hash).or_default().push(binding_idx);
            }

            self.stats.simd_batches += 1;
        }

        Ok(hash_table)
    }

    /// Probe hash table using SIMD acceleration
    fn probe_hash_table_simd(
        &mut self,
        probe_bindings: &[Binding],
        hash_table: &HashMap<u64, Vec<usize>>,
        join_vars: &[Variable],
        _build_is_left: bool, // Not used in this simplified implementation
    ) -> Result<Vec<Binding>> {
        let results = Vec::new();
        let batch_size = self.config.simd_width / 64;

        for chunk in probe_bindings.chunks(batch_size) {
            // Compute hashes for probe batch
            let hashes = self.compute_batch_hashes(chunk, join_vars)?;

            // Probe hash table for each hash
            for &hash in hashes.iter() {
                if let Some(build_indices) = hash_table.get(&hash) {
                    for &_build_idx in build_indices {
                        // Create joined binding
                        // (Implementation would merge bindings here)
                        self.stats.total_comparisons += 1;
                    }
                }
            }
        }

        Ok(results)
    }

    /// Compute hashes for a batch of bindings using SIMD
    fn compute_batch_hashes(
        &self,
        bindings: &[Binding],
        join_vars: &[Variable],
    ) -> Result<Vec<u64>> {
        let mut hashes = Vec::with_capacity(bindings.len());

        for binding in bindings {
            let mut combined_hash = 0u64;

            // Combine hashes of all join variables
            for var in join_vars {
                if let Some(term) = binding.get(var) {
                    let term_hash = self.hash_term(term);
                    combined_hash = combined_hash.wrapping_mul(31).wrapping_add(term_hash);
                }
            }

            hashes.push(combined_hash);
        }

        // Use SIMD batch hashing if available
        if hashes.len() >= self.config.min_batch_size {
            let simd_hashes = simd_hash_batch(&hashes);
            Ok(simd_hashes)
        } else {
            Ok(hashes)
        }
    }

    /// Hash a term value
    fn hash_term(&self, term: &Term) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        match term {
            Term::Iri(iri) => iri.as_str().hash(&mut hasher),
            Term::Literal(lit) => lit.value.hash(&mut hasher),
            Term::BlankNode(bn) => bn.hash(&mut hasher),
            Term::Variable(var) => var.as_str().hash(&mut hasher),
            _ => {}
        }
        hasher.finish()
    }

    /// Get join statistics
    pub fn get_stats(&self) -> &JoinStats {
        &self.stats
    }

    /// Reset statistics
    pub fn reset_stats(&mut self) {
        self.stats = JoinStats::default();
    }
}

/// SIMD-accelerated filter evaluator
pub struct SimdFilterEvaluator {
    /// Configuration
    config: SimdConfig,
}

impl SimdFilterEvaluator {
    /// Create a new SIMD filter evaluator
    pub fn new(config: SimdConfig) -> Self {
        Self { config }
    }

    /// Evaluate a numeric filter using SIMD
    ///
    /// This vectorizes numeric comparisons for filters like `?x > 10`
    pub fn evaluate_numeric_filter(
        &self,
        bindings: &[Binding],
        var: &Variable,
        operator: ComparisonOp,
        threshold: f64,
    ) -> Result<Vec<bool>> {
        if bindings.is_empty() {
            return Ok(Vec::new());
        }

        // Extract numeric values
        let mut values = Vec::with_capacity(bindings.len());
        for binding in bindings {
            let value = if let Some(term) = binding.get(var) {
                self.term_to_numeric(term).unwrap_or(f64::NAN)
            } else {
                f64::NAN
            };
            values.push(value);
        }

        // Use SIMD comparison if we have enough values
        if values.len() >= self.config.min_batch_size {
            self.simd_compare(&values, operator, threshold)
        } else {
            Ok(values
                .iter()
                .map(|&v| !v.is_nan() && self.scalar_compare(v, operator, threshold))
                .collect())
        }
    }

    /// SIMD-accelerated comparison
    fn simd_compare(
        &self,
        values: &[f64],
        operator: ComparisonOp,
        threshold: f64,
    ) -> Result<Vec<bool>> {
        let results = match operator {
            ComparisonOp::Gt => simd_compare_gt(values, threshold),
            ComparisonOp::Lt => simd_compare_lt(values, threshold),
            ComparisonOp::Ge => simd_compare_ge(values, threshold),
            ComparisonOp::Le => simd_compare_le(values, threshold),
            ComparisonOp::Eq => simd_compare_eq_f64(values, threshold),
            ComparisonOp::Ne => {
                let eq_results = simd_compare_eq_f64(values, threshold);
                eq_results.into_iter().map(|b| !b).collect()
            }
        };

        Ok(results)
    }

    /// Scalar comparison fallback
    fn scalar_compare(&self, value: f64, operator: ComparisonOp, threshold: f64) -> bool {
        match operator {
            ComparisonOp::Gt => value > threshold,
            ComparisonOp::Lt => value < threshold,
            ComparisonOp::Ge => value >= threshold,
            ComparisonOp::Le => value <= threshold,
            ComparisonOp::Eq => (value - threshold).abs() < f64::EPSILON,
            ComparisonOp::Ne => (value - threshold).abs() >= f64::EPSILON,
        }
    }

    /// Convert term to numeric value
    fn term_to_numeric(&self, term: &Term) -> Option<f64> {
        match term {
            Term::Literal(lit) => lit.value.parse::<f64>().ok(),
            _ => None,
        }
    }
}

/// Comparison operators for filters
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ComparisonOp {
    Gt,
    Lt,
    Ge,
    Le,
    Eq,
    Ne,
}

/// SIMD-accelerated aggregation operations for SPARQL queries
pub struct SimdAggregations {
    config: SimdConfig,
}

impl SimdAggregations {
    /// Create new SIMD aggregations processor
    pub fn new(config: SimdConfig) -> Self {
        Self { config }
    }

    /// Compute SUM aggregation using vectorized operations
    pub fn sum(&self, values: &[f64]) -> f64 {
        if values.is_empty() {
            return 0.0;
        }

        if values.len() < self.config.min_batch_size {
            return values.iter().sum();
        }

        // Use ndarray for vectorized sum
        let arr = Array1::from_vec(values.to_vec());
        arr.sum()
    }

    /// Compute AVG aggregation using SIMD
    pub fn avg(&self, values: &[f64]) -> f64 {
        if values.is_empty() {
            return 0.0;
        }

        let sum = self.sum(values);
        sum / values.len() as f64
    }

    /// Compute MIN aggregation using parallel operations
    pub fn min(&self, values: &[f64]) -> f64 {
        if values.is_empty() {
            return f64::NAN;
        }

        if values.len() < self.config.min_batch_size {
            return values.iter().copied().fold(f64::INFINITY, f64::min);
        }

        // Use Rayon parallel reduction for large arrays
        values
            .par_iter()
            .copied()
            .reduce(|| f64::INFINITY, |a, b| a.min(b))
    }

    /// Compute MAX aggregation using parallel operations
    pub fn max(&self, values: &[f64]) -> f64 {
        if values.is_empty() {
            return f64::NAN;
        }

        if values.len() < self.config.min_batch_size {
            return values.iter().copied().fold(f64::NEG_INFINITY, f64::max);
        }

        // Use Rayon parallel reduction for large arrays
        values
            .par_iter()
            .copied()
            .reduce(|| f64::NEG_INFINITY, |a, b| a.max(b))
    }

    /// Compute DOT PRODUCT for vector similarity (SPARQL extension)
    pub fn dot_product(&self, vec1: &[f64], vec2: &[f64]) -> Result<f64> {
        if vec1.len() != vec2.len() {
            return Ok(0.0);
        }

        if vec1.is_empty() {
            return Ok(0.0);
        }

        // Use ndarray for dot product
        let arr1 = Array1::from_vec(vec1.to_vec());
        let arr2 = Array1::from_vec(vec2.to_vec());

        // Compute dot product using ndarray operations
        let result = (&arr1 * &arr2).sum();

        Ok(result)
    }

    /// Compute COSINE SIMILARITY for vector embeddings (SPARQL extension)
    pub fn cosine_similarity(&self, vec1: &[f64], vec2: &[f64]) -> Result<f64> {
        if vec1.len() != vec2.len() || vec1.is_empty() {
            return Ok(0.0);
        }

        let dot = self.dot_product(vec1, vec2)?;
        let norm1 = self.dot_product(vec1, vec1)?.sqrt();
        let norm2 = self.dot_product(vec2, vec2)?.sqrt();

        if norm1 == 0.0 || norm2 == 0.0 {
            return Ok(0.0);
        }

        Ok(dot / (norm1 * norm2))
    }

    /// Batch matrix multiplication for graph operations (SPARQL extension)
    pub fn batch_matrix_multiply(
        &self,
        matrices_a: &[Array2<f64>],
        matrices_b: &[Array2<f64>],
    ) -> Result<Vec<Array2<f64>>> {
        if matrices_a.len() != matrices_b.len() {
            return Ok(Vec::new());
        }

        // Use parallel processing for batch multiplication
        let results: Vec<Array2<f64>> = matrices_a
            .par_iter()
            .zip(matrices_b.par_iter())
            .map(|(a, b)| {
                // Use ndarray's dot method for matrix multiplication
                a.dot(b)
            })
            .collect();

        Ok(results)
    }
}

/// SIMD-accelerated string operations for SPARQL
pub struct SimdStringOps {
    config: SimdConfig,
}

impl SimdStringOps {
    /// Create new SIMD string operations processor
    pub fn new(config: SimdConfig) -> Self {
        Self { config }
    }

    /// Batch string length computation
    pub fn batch_strlen(&self, strings: &[String]) -> Vec<usize> {
        if strings.len() < self.config.min_batch_size {
            return strings.iter().map(|s| s.len()).collect();
        }

        // Use Rayon parallel processing for large batches
        strings.par_iter().map(|s| s.len()).collect()
    }

    /// Batch string contains check (for SPARQL REGEX)
    pub fn batch_contains(&self, strings: &[String], pattern: &str) -> Vec<bool> {
        if strings.len() < self.config.min_batch_size {
            return strings.iter().map(|s| s.contains(pattern)).collect();
        }

        // Use Rayon parallel processing
        strings.par_iter().map(|s| s.contains(pattern)).collect()
    }

    /// Batch string prefix check (for SPARQL STRSTARTS)
    pub fn batch_starts_with(&self, strings: &[String], prefix: &str) -> Vec<bool> {
        if strings.len() < self.config.min_batch_size {
            return strings.iter().map(|s| s.starts_with(prefix)).collect();
        }

        // Use Rayon parallel processing
        strings.par_iter().map(|s| s.starts_with(prefix)).collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use oxirs_core::model::NamedNode;

    #[test]
    fn test_simd_triple_matcher() {
        let config = SimdConfig::default();
        let mut matcher = SimdTripleMatcher::new(config);

        // Create a simple pattern: ?s <pred> ?o
        let pattern = TriplePattern {
            subject: Term::Variable(Variable::new("s".to_string()).unwrap()),
            predicate: Term::Iri(NamedNode::new("http://example.org/pred").unwrap()),
            object: Term::Variable(Variable::new("o".to_string()).unwrap()),
        };

        // Create candidates
        let candidates = vec![
            TripleCandidate::new(
                Term::Iri(NamedNode::new("http://example.org/s1").unwrap()),
                Term::Iri(NamedNode::new("http://example.org/pred").unwrap()),
                Term::Iri(NamedNode::new("http://example.org/o1").unwrap()),
            ),
            TripleCandidate::new(
                Term::Iri(NamedNode::new("http://example.org/s2").unwrap()),
                Term::Iri(NamedNode::new("http://example.org/other").unwrap()),
                Term::Iri(NamedNode::new("http://example.org/o2").unwrap()),
            ),
        ];

        let matches = matcher.match_pattern(&pattern, &candidates).unwrap();
        assert_eq!(matches.len(), 1); // Only first candidate should match
    }

    #[test]
    fn test_simd_hash_join() {
        let config = SimdConfig::default();
        let mut join = SimdHashJoin::new(config);

        let var_x = Variable::new("x".to_string()).unwrap();
        let var_y = Variable::new("y".to_string()).unwrap();

        // Create test bindings
        let left = vec![{
            let mut b = Binding::new();
            b.insert(
                var_x.clone(),
                Term::Iri(NamedNode::new("http://example.org/1").unwrap()),
            );
            b.insert(
                var_y.clone(),
                Term::Iri(NamedNode::new("http://example.org/a").unwrap()),
            );
            b
        }];

        let right = vec![{
            let mut b = Binding::new();
            b.insert(
                var_x.clone(),
                Term::Iri(NamedNode::new("http://example.org/1").unwrap()),
            );
            b
        }];

        let join_vars = vec![var_x];
        let _result = join.join(&left, &right, &join_vars).unwrap();

        // Stats should show SIMD was used
        let stats = join.get_stats();
        assert!(stats.simd_batches > 0 || stats.scalar_ops > 0);
    }

    #[test]
    fn test_simd_filter_evaluator() {
        let config = SimdConfig::default();
        let evaluator = SimdFilterEvaluator::new(config);

        let var_x = Variable::new("x".to_string()).unwrap();

        // Create bindings with numeric literals
        let bindings: Vec<Binding> = (1..=20)
            .map(|i| {
                let mut b = Binding::new();
                b.insert(
                    var_x.clone(),
                    Term::Literal(crate::algebra::Literal {
                        value: i.to_string(),
                        language: None,
                        datatype: None,
                    }),
                );
                b
            })
            .collect();

        // Test filter: ?x > 10
        let results = evaluator
            .evaluate_numeric_filter(&bindings, &var_x, ComparisonOp::Gt, 10.0)
            .unwrap();

        // Should have 10 true values (11-20)
        let true_count = results.iter().filter(|&&b| b).count();
        assert_eq!(true_count, 10);
    }

    #[test]
    fn test_simd_aggregations() {
        let config = SimdConfig::default();
        let agg = SimdAggregations::new(config);

        let values: Vec<f64> = (1..=100).map(|i| i as f64).collect();

        // Test SUM
        let sum = agg.sum(&values);
        assert_eq!(sum, 5050.0); // 1+2+...+100 = 5050

        // Test AVG
        let avg = agg.avg(&values);
        assert_eq!(avg, 50.5); // Average of 1-100

        // Test MIN
        let min = agg.min(&values);
        assert_eq!(min, 1.0);

        // Test MAX
        let max = agg.max(&values);
        assert_eq!(max, 100.0);
    }

    #[test]
    fn test_simd_dot_product() {
        let config = SimdConfig::default();
        let agg = SimdAggregations::new(config);

        let vec1 = vec![1.0, 2.0, 3.0, 4.0];
        let vec2 = vec![5.0, 6.0, 7.0, 8.0];

        // Dot product: 1*5 + 2*6 + 3*7 + 4*8 = 5 + 12 + 21 + 32 = 70
        let dot = agg.dot_product(&vec1, &vec2).unwrap();
        assert!((dot - 70.0).abs() < 1e-10);
    }

    #[test]
    fn test_simd_cosine_similarity() {
        let config = SimdConfig::default();
        let agg = SimdAggregations::new(config);

        // Identical vectors should have similarity 1.0
        let vec1 = vec![1.0, 2.0, 3.0];
        let similarity = agg.cosine_similarity(&vec1, &vec1).unwrap();
        assert!((similarity - 1.0).abs() < 1e-10);

        // Orthogonal vectors should have similarity 0.0
        let vec2 = vec![1.0, 0.0];
        let vec3 = vec![0.0, 1.0];
        let similarity2 = agg.cosine_similarity(&vec2, &vec3).unwrap();
        assert!(similarity2.abs() < 1e-10);
    }

    #[test]
    fn test_simd_string_ops() {
        let config = SimdConfig::default();
        let string_ops = SimdStringOps::new(config);

        let strings: Vec<String> = (0..20).map(|i| format!("test_string_{}", i)).collect();

        // Test batch strlen
        let lengths = string_ops.batch_strlen(&strings);
        assert_eq!(lengths.len(), 20);
        assert!(lengths.iter().all(|&l| l > 0));

        // Test batch contains
        let contains = string_ops.batch_contains(&strings, "test");
        assert_eq!(contains.len(), 20);
        assert!(contains.iter().all(|&b| b)); // All contain "test"

        // Test batch starts_with
        let starts = string_ops.batch_starts_with(&strings, "test_");
        assert_eq!(starts.len(), 20);
        assert!(starts.iter().all(|&b| b)); // All start with "test_"
    }
}