oxirs-core 0.2.2

Core RDF and SPARQL functionality for OxiRS - native Rust implementation with zero dependencies
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
//! WebAssembly compilation for client-side SPARQL execution
//!
//! This module provides WASM compilation of SPARQL queries for efficient
//! client-side execution in web browsers and edge environments.

use crate::model::Variable;
use crate::query::plan::ExecutionPlan;
use crate::OxirsError;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

/// WASM query compiler
pub struct WasmQueryCompiler {
    /// Compilation cache
    cache: Arc<RwLock<CompilationCache>>,
    /// Target configuration
    target: WasmTarget,
    /// Optimization level
    optimization: OptimizationLevel,
}

/// WASM compilation target
#[derive(Debug, Clone)]
pub enum WasmTarget {
    /// Standard WASM 1.0
    Wasm1_0,
    /// WASM with SIMD extensions
    WasmSimd,
    /// WASM with threads (SharedArrayBuffer)
    WasmThreads,
    /// WASM with both SIMD and threads
    WasmSimdThreads,
    /// WASM with GC proposal
    WasmGC,
}

/// Optimization level for WASM compilation
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub enum OptimizationLevel {
    /// No optimizations
    None,
    /// Basic optimizations
    Basic,
    /// Standard optimizations
    Standard,
    /// Aggressive optimizations
    Aggressive,
    /// Size optimizations
    Size,
}

/// Compilation cache
struct CompilationCache {
    /// Cached modules
    modules: HashMap<QueryHash, CachedModule>,
    /// Total cache size in bytes
    total_size: usize,
    /// Maximum cache size
    max_size: usize,
}

/// Query hash for caching
type QueryHash = u64;

/// Cached WASM module
struct CachedModule {
    /// Compiled WASM bytes
    wasm_bytes: Vec<u8>,
    /// Module metadata
    #[allow(dead_code)]
    metadata: ModuleMetadata,
    /// Compilation time
    #[allow(dead_code)]
    compile_time: std::time::Duration,
    /// Usage count
    usage_count: usize,
}

/// WASM module metadata
#[derive(Debug, Clone)]
pub struct ModuleMetadata {
    /// Module size in bytes
    pub size: usize,
    /// Memory requirements
    pub memory_pages: u32,
    /// Exported functions
    pub exports: Vec<ExportedFunction>,
    /// Required imports
    pub imports: Vec<RequiredImport>,
    /// Optimization statistics
    pub optimization_stats: OptimizationStats,
}

/// Exported WASM function
#[derive(Debug, Clone)]
pub struct ExportedFunction {
    /// Function name
    pub name: String,
    /// Parameter types
    pub params: Vec<WasmType>,
    /// Return types
    pub returns: Vec<WasmType>,
    /// Function kind
    pub kind: FunctionKind,
}

/// WASM value types
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum WasmType {
    I32,
    I64,
    F32,
    F64,
    V128,      // SIMD vector
    FuncRef,   // Function reference
    ExternRef, // External reference
}

/// Function kinds
#[derive(Debug, Clone, PartialEq)]
pub enum FunctionKind {
    /// Main query execution
    QueryExec,
    /// Triple matching
    TripleMatch,
    /// Join operation
    Join,
    /// Filter evaluation
    Filter,
    /// Aggregation
    Aggregate,
    /// Helper function
    Helper(String),
}

/// Required import for WASM module
#[derive(Debug, Clone)]
pub struct RequiredImport {
    /// Module name
    pub module: String,
    /// Import name
    pub name: String,
    /// Import type
    pub import_type: ImportType,
}

/// Import types
#[derive(Debug, Clone)]
pub enum ImportType {
    /// Function import
    Function {
        params: Vec<WasmType>,
        returns: Vec<WasmType>,
    },
    /// Memory import
    Memory {
        min_pages: u32,
        max_pages: Option<u32>,
    },
    /// Table import
    Table {
        element_type: WasmType,
        min_size: u32,
        max_size: Option<u32>,
    },
    /// Global import
    Global { value_type: WasmType, mutable: bool },
}

/// Optimization statistics
#[derive(Debug, Clone, Default)]
pub struct OptimizationStats {
    /// Instructions eliminated
    pub instructions_eliminated: usize,
    /// Functions inlined
    pub functions_inlined: usize,
    /// Dead code removed
    pub dead_code_removed: usize,
    /// Loop optimizations
    pub loops_optimized: usize,
    /// Memory accesses optimized
    pub memory_optimizations: usize,
}

/// WASM runtime interface
pub trait WasmRuntime: Send + Sync {
    /// Instantiate WASM module
    fn instantiate(&self, wasm_bytes: &[u8]) -> Result<Box<dyn WasmInstance>, OxirsError>;

    /// Validate WASM module
    fn validate(&self, wasm_bytes: &[u8]) -> Result<(), OxirsError>;

    /// Get runtime capabilities
    fn capabilities(&self) -> RuntimeCapabilities;
}

/// WASM module instance
pub trait WasmInstance: Send + Sync {
    /// Execute query
    fn execute_query(&mut self, input: &QueryInput) -> Result<QueryOutput, OxirsError>;

    /// Get memory usage
    fn memory_usage(&self) -> usize;

    /// Reset instance state
    fn reset(&mut self);
}

/// Runtime capabilities
#[derive(Debug, Clone)]
pub struct RuntimeCapabilities {
    /// Supports SIMD
    pub simd: bool,
    /// Supports threads
    pub threads: bool,
    /// Supports bulk memory operations
    pub bulk_memory: bool,
    /// Supports reference types
    pub reference_types: bool,
    /// Maximum memory pages
    pub max_memory_pages: u32,
}

/// Query input for WASM execution
pub struct QueryInput {
    /// Serialized RDF data
    pub data: Vec<u8>,
    /// Variable bindings
    pub bindings: HashMap<String, Vec<u8>>,
    /// Execution limits
    pub limits: ExecutionLimits,
}

/// Query output from WASM execution
pub struct QueryOutput {
    /// Result bindings
    pub results: Vec<u8>,
    /// Execution statistics
    pub stats: ExecutionStats,
}

/// Execution limits
#[derive(Debug, Clone)]
pub struct ExecutionLimits {
    /// Maximum execution time in milliseconds
    pub timeout_ms: u32,
    /// Maximum memory usage in bytes
    pub max_memory: usize,
    /// Maximum result count
    pub max_results: usize,
}

/// Execution statistics
#[derive(Debug, Clone)]
pub struct ExecutionStats {
    /// Execution time in microseconds
    pub exec_time_us: u64,
    /// Memory used in bytes
    pub memory_used: usize,
    /// Triples scanned
    pub triples_scanned: usize,
    /// Results produced
    pub results_count: usize,
}

impl WasmQueryCompiler {
    /// Create new WASM compiler
    pub fn new(target: WasmTarget, optimization: OptimizationLevel) -> Self {
        Self {
            cache: Arc::new(RwLock::new(CompilationCache::new())),
            target,
            optimization,
        }
    }

    /// Compile query to WASM
    pub fn compile(&self, plan: &ExecutionPlan) -> Result<Vec<u8>, OxirsError> {
        let hash = self.hash_plan(plan);

        // Check cache
        if let Some(module) = self.get_cached(hash) {
            return Ok(module);
        }

        // Generate WASM
        let wasm_bytes = self.generate_wasm(plan)?;

        // Optimize
        let optimized = self.optimize_wasm(wasm_bytes)?;

        // Cache result
        self.cache_module(hash, optimized.clone())?;

        Ok(optimized)
    }

    /// Generate WASM module from execution plan
    fn generate_wasm(&self, plan: &ExecutionPlan) -> Result<Vec<u8>, OxirsError> {
        let mut builder = WasmModuleBuilder::new(self.target.clone());

        // Add memory
        builder.add_memory(1, Some(1024))?; // 1-1024 pages

        // Add imports
        self.add_imports(&mut builder)?;

        // Add data structures
        self.add_data_structures(&mut builder)?;

        // Generate query execution function
        self.generate_query_function(&mut builder, plan)?;

        // Generate helper functions
        self.generate_helpers(&mut builder)?;

        // Build module
        builder.build()
    }

    /// Add required imports
    fn add_imports(&self, builder: &mut WasmModuleBuilder) -> Result<(), OxirsError> {
        // Import host functions
        builder.add_import(
            "host",
            "alloc",
            ImportType::Function {
                params: vec![WasmType::I32],
                returns: vec![WasmType::I32],
            },
        )?;

        builder.add_import(
            "host",
            "free",
            ImportType::Function {
                params: vec![WasmType::I32],
                returns: vec![],
            },
        )?;

        builder.add_import(
            "host",
            "log",
            ImportType::Function {
                params: vec![WasmType::I32, WasmType::I32],
                returns: vec![],
            },
        )?;

        Ok(())
    }

    /// Add data structures
    fn add_data_structures(&self, builder: &mut WasmModuleBuilder) -> Result<(), OxirsError> {
        // Define term structure
        builder.add_struct(
            "Term",
            vec![
                ("kind", WasmType::I32),
                ("value_ptr", WasmType::I32),
                ("value_len", WasmType::I32),
                ("datatype_ptr", WasmType::I32),
                ("datatype_len", WasmType::I32),
                ("lang_ptr", WasmType::I32),
                ("lang_len", WasmType::I32),
            ],
        )?;

        // Define triple structure
        builder.add_struct(
            "Triple",
            vec![
                ("subject", WasmType::I32),
                ("predicate", WasmType::I32),
                ("object", WasmType::I32),
            ],
        )?;

        // Define binding structure
        builder.add_struct(
            "Binding",
            vec![
                ("var_ptr", WasmType::I32),
                ("var_len", WasmType::I32),
                ("term", WasmType::I32),
            ],
        )?;

        Ok(())
    }

    /// Generate main query function
    fn generate_query_function(
        &self,
        builder: &mut WasmModuleBuilder,
        plan: &ExecutionPlan,
    ) -> Result<(), OxirsError> {
        match plan {
            ExecutionPlan::TripleScan { pattern } => {
                self.generate_triple_scan(builder, pattern)?;
            }
            ExecutionPlan::HashJoin {
                left,
                right,
                join_vars,
            } => {
                self.generate_hash_join(builder, left, right, join_vars)?;
            }
            _ => {
                return Err(OxirsError::Query(
                    "Unsupported plan type for WASM".to_string(),
                ))
            }
        }

        Ok(())
    }

    /// Generate triple scan function
    fn generate_triple_scan(
        &self,
        builder: &mut WasmModuleBuilder,
        _pattern: &crate::model::pattern::TriplePattern,
    ) -> Result<(), OxirsError> {
        builder.add_function(
            "query_exec",
            vec![
                WasmType::I32, // input data pointer
                WasmType::I32, // input data length
                WasmType::I32, // output buffer pointer
                WasmType::I32, // output buffer length
            ],
            vec![WasmType::I32],
            |fb| {
                // Function body would be generated here
                // This is a simplified placeholder
                fb.local_get(0);
                fb.local_get(1);
                fb.call("scan_triples");
                fb.i32_const(0); // Return success
            },
        )?;

        Ok(())
    }

    /// Generate hash join function
    fn generate_hash_join(
        &self,
        builder: &mut WasmModuleBuilder,
        left: &ExecutionPlan,
        right: &ExecutionPlan,
        _join_vars: &[Variable],
    ) -> Result<(), OxirsError> {
        // Generate left and right sub-plans
        self.generate_query_function(builder, left)?;
        self.generate_query_function(builder, right)?;

        // Generate join function
        builder.add_function(
            "hash_join",
            vec![
                WasmType::I32, // left results
                WasmType::I32, // right results
                WasmType::I32, // join variables
                WasmType::I32, // output buffer
            ],
            vec![WasmType::I32],
            |fb| {
                // Hash join implementation
                fb.i32_const(0);
            },
        )?;

        Ok(())
    }

    /// Generate helper functions
    fn generate_helpers(&self, builder: &mut WasmModuleBuilder) -> Result<(), OxirsError> {
        // String comparison
        builder.add_function(
            "str_eq",
            vec![
                WasmType::I32,
                WasmType::I32, // str1 ptr, len
                WasmType::I32,
                WasmType::I32, // str2 ptr, len
            ],
            vec![WasmType::I32],
            |fb| {
                // Compare lengths first
                fb.local_get(1);
                fb.local_get(3);
                fb.i32_ne();
                fb.if_else(
                    WasmType::I32,
                    |fb| {
                        fb.i32_const(0); // Different lengths
                    },
                    |fb| {
                        // Compare bytes
                        fb.i32_const(1); // Placeholder
                    },
                );
            },
        )?;

        // Term matching
        builder.add_function(
            "match_term",
            vec![
                WasmType::I32, // term1
                WasmType::I32, // term2
            ],
            vec![WasmType::I32],
            |fb| {
                fb.i32_const(1); // Placeholder
            },
        )?;

        Ok(())
    }

    /// Optimize WASM module
    fn optimize_wasm(&self, wasm_bytes: Vec<u8>) -> Result<Vec<u8>, OxirsError> {
        match self.optimization {
            OptimizationLevel::None => Ok(wasm_bytes),
            _ => {
                // Would use wasm-opt or similar
                Ok(wasm_bytes)
            }
        }
    }

    /// Hash execution plan
    fn hash_plan(&self, plan: &ExecutionPlan) -> QueryHash {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        format!("{plan:?}").hash(&mut hasher);
        hasher.finish()
    }

    /// Get cached module
    fn get_cached(&self, hash: QueryHash) -> Option<Vec<u8>> {
        let cache = self.cache.read().ok()?;
        cache.modules.get(&hash).map(|m| m.wasm_bytes.clone())
    }

    /// Cache compiled module
    fn cache_module(&self, hash: QueryHash, wasm_bytes: Vec<u8>) -> Result<(), OxirsError> {
        let mut cache = self
            .cache
            .write()
            .map_err(|_| OxirsError::Query("Failed to acquire cache lock".to_string()))?;

        let metadata = ModuleMetadata {
            size: wasm_bytes.len(),
            memory_pages: 1,
            exports: vec![],
            imports: vec![],
            optimization_stats: OptimizationStats::default(),
        };

        cache.add(
            hash,
            CachedModule {
                wasm_bytes,
                metadata,
                compile_time: std::time::Duration::from_millis(10),
                usage_count: 0,
            },
        );

        Ok(())
    }
}

/// WASM module builder
struct WasmModuleBuilder {
    #[allow(dead_code)]
    target: WasmTarget,
    imports: Vec<RequiredImport>,
    functions: Vec<FunctionDef>,
    memory: Option<MemoryDef>,
    structs: Vec<StructDef>,
}

/// Function definition
struct FunctionDef {
    #[allow(dead_code)]
    name: String,
    #[allow(dead_code)]
    params: Vec<WasmType>,
    #[allow(dead_code)]
    returns: Vec<WasmType>,
    #[allow(dead_code)]
    body: Vec<WasmInstruction>,
}

/// Memory definition
struct MemoryDef {
    #[allow(dead_code)]
    min_pages: u32,
    #[allow(dead_code)]
    max_pages: Option<u32>,
}

/// Structure definition
struct StructDef {
    #[allow(dead_code)]
    name: String,
    #[allow(dead_code)]
    fields: Vec<(String, WasmType)>,
}

/// WASM instructions (simplified)
#[allow(dead_code)]
enum WasmInstruction {
    LocalGet(u32),
    LocalSet(u32),
    I32Const(i32),
    I32Add,
    I32Sub,
    I32Eq,
    I32Ne,
    Call(String),
    If(Vec<WasmInstruction>),
    IfElse(WasmType, Vec<WasmInstruction>, Vec<WasmInstruction>),
    Return,
}

/// Function builder for WASM generation
struct FunctionBuilder {
    instructions: Vec<WasmInstruction>,
}

impl FunctionBuilder {
    fn new() -> Self {
        Self {
            instructions: Vec::new(),
        }
    }

    fn local_get(&mut self, idx: u32) {
        self.instructions.push(WasmInstruction::LocalGet(idx));
    }

    #[allow(dead_code)]
    fn local_set(&mut self, idx: u32) {
        self.instructions.push(WasmInstruction::LocalSet(idx));
    }

    fn i32_const(&mut self, val: i32) {
        self.instructions.push(WasmInstruction::I32Const(val));
    }

    #[allow(dead_code)]
    fn i32_add(&mut self) {
        self.instructions.push(WasmInstruction::I32Add);
    }

    fn i32_ne(&mut self) {
        self.instructions.push(WasmInstruction::I32Ne);
    }

    fn call(&mut self, func: &str) {
        self.instructions
            .push(WasmInstruction::Call(func.to_string()));
    }

    fn if_else<F1, F2>(&mut self, result_type: WasmType, then_fn: F1, else_fn: F2)
    where
        F1: FnOnce(&mut Self),
        F2: FnOnce(&mut Self),
    {
        let mut then_builder = FunctionBuilder::new();
        then_fn(&mut then_builder);

        let mut else_builder = FunctionBuilder::new();
        else_fn(&mut else_builder);

        self.instructions.push(WasmInstruction::IfElse(
            result_type,
            then_builder.instructions,
            else_builder.instructions,
        ));
    }
}

impl WasmModuleBuilder {
    fn new(target: WasmTarget) -> Self {
        Self {
            target,
            imports: Vec::new(),
            functions: Vec::new(),
            memory: None,
            structs: Vec::new(),
        }
    }

    fn add_import(
        &mut self,
        module: &str,
        name: &str,
        import_type: ImportType,
    ) -> Result<(), OxirsError> {
        self.imports.push(RequiredImport {
            module: module.to_string(),
            name: name.to_string(),
            import_type,
        });
        Ok(())
    }

    fn add_memory(&mut self, min: u32, max: Option<u32>) -> Result<(), OxirsError> {
        self.memory = Some(MemoryDef {
            min_pages: min,
            max_pages: max,
        });
        Ok(())
    }

    fn add_struct(&mut self, name: &str, fields: Vec<(&str, WasmType)>) -> Result<(), OxirsError> {
        self.structs.push(StructDef {
            name: name.to_string(),
            fields: fields
                .into_iter()
                .map(|(n, t)| (n.to_string(), t))
                .collect(),
        });
        Ok(())
    }

    fn add_function<F>(
        &mut self,
        name: &str,
        params: Vec<WasmType>,
        returns: Vec<WasmType>,
        body_fn: F,
    ) -> Result<(), OxirsError>
    where
        F: FnOnce(&mut FunctionBuilder),
    {
        let mut builder = FunctionBuilder::new();
        body_fn(&mut builder);

        self.functions.push(FunctionDef {
            name: name.to_string(),
            params,
            returns,
            body: builder.instructions,
        });

        Ok(())
    }

    fn build(self) -> Result<Vec<u8>, OxirsError> {
        // Would generate actual WASM bytes
        // This is a placeholder
        Ok(vec![0x00, 0x61, 0x73, 0x6d]) // WASM magic number
    }
}

impl CompilationCache {
    fn new() -> Self {
        Self {
            modules: HashMap::new(),
            total_size: 0,
            max_size: 100 * 1024 * 1024, // 100MB
        }
    }

    fn add(&mut self, hash: QueryHash, module: CachedModule) {
        self.total_size += module.wasm_bytes.len();
        self.modules.insert(hash, module);

        // Evict if needed
        while self.total_size > self.max_size && !self.modules.is_empty() {
            // Remove least used
            if let Some((&hash, _)) = self.modules.iter().min_by_key(|(_, m)| m.usage_count) {
                if let Some(removed) = self.modules.remove(&hash) {
                    self.total_size -= removed.wasm_bytes.len();
                }
            }
        }
    }
}

/// Streaming WASM compiler for large queries
pub struct StreamingWasmCompiler {
    base_compiler: WasmQueryCompiler,
    chunk_size: usize,
}

impl StreamingWasmCompiler {
    /// Create new streaming compiler
    pub fn new(target: WasmTarget, optimization: OptimizationLevel) -> Self {
        Self {
            base_compiler: WasmQueryCompiler::new(target, optimization),
            chunk_size: 1024 * 1024, // 1MB chunks
        }
    }

    /// Compile query in streaming fashion
    pub async fn compile_streaming(
        &self,
        plan: &ExecutionPlan,
    ) -> Result<impl futures::Stream<Item = Result<Vec<u8>, OxirsError>> + use<>, OxirsError> {
        use futures::stream;

        // Generate full module
        let wasm_bytes = self.base_compiler.compile(plan)?;

        // Split into chunks
        let chunks: Vec<Vec<u8>> = wasm_bytes
            .chunks(self.chunk_size)
            .map(|c| c.to_vec())
            .collect();

        Ok(stream::iter(chunks.into_iter().map(Ok)))
    }
}

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

    #[test]
    fn test_wasm_compiler_creation() {
        let compiler = WasmQueryCompiler::new(WasmTarget::Wasm1_0, OptimizationLevel::Standard);

        let cache = compiler.cache.read().expect("lock should not be poisoned");
        assert_eq!(cache.modules.len(), 0);
    }

    #[test]
    fn test_module_builder() {
        let mut builder = WasmModuleBuilder::new(WasmTarget::Wasm1_0);

        builder
            .add_memory(1, Some(16))
            .expect("add memory should succeed");
        builder
            .add_import(
                "host",
                "log",
                ImportType::Function {
                    params: vec![WasmType::I32],
                    returns: vec![],
                },
            )
            .expect("operation should succeed");

        builder
            .add_function("test", vec![], vec![WasmType::I32], |fb| {
                fb.i32_const(42);
            })
            .expect("operation should succeed");

        let wasm = builder.build().expect("operation should succeed");
        assert!(!wasm.is_empty());
    }

    #[test]
    fn test_optimization_levels() {
        assert_eq!(OptimizationLevel::None as u8, 0);
        assert!(OptimizationLevel::Aggressive > OptimizationLevel::Standard);
    }
}