heliosdb-nano 3.30.0

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
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
//! Query executor trait and implementations
//!
//! Standard Volcano model (iterator-based execution).

use crate::{Result, Tuple, Value, Error};
use super::aggregation::{AggregateState, CountState, create_aggregate};
use std::collections::HashMap;
use std::cmp::Ordering;

/// Executor trait (Volcano model)
///
/// Standard iterator-based execution model from database textbooks.
pub trait Executor: Send {
    /// Open the executor (initialize state)
    fn open(&mut self) -> Result<()>;

    /// Get next tuple
    fn next(&mut self) -> Result<Option<Tuple>>;

    /// Close the executor (cleanup)
    fn close(&mut self) -> Result<()>;
}

// =============================================================================
// ScanExecutor: Table scan
// =============================================================================

/// Table scan executor
///
/// Iterates over all tuples in a table.
pub struct ScanExecutor {
    /// Tuples to scan (materialized from storage)
    tuples: Vec<Tuple>,
    /// Current position in the scan
    position: usize,
    /// Whether the executor is open
    is_open: bool,
}

impl ScanExecutor {
    /// Create a new scan executor with pre-loaded tuples
    pub fn new(tuples: Vec<Tuple>) -> Self {
        Self {
            tuples,
            position: 0,
            is_open: false,
        }
    }
}

// SAFETY: self.position is bounds-checked (< self.tuples.len()) before indexing.
#[allow(clippy::indexing_slicing)]
impl Executor for ScanExecutor {
    fn open(&mut self) -> Result<()> {
        self.position = 0;
        self.is_open = true;
        Ok(())
    }

    fn next(&mut self) -> Result<Option<Tuple>> {
        if !self.is_open {
            return Err(Error::Generic("Executor not open".to_string()));
        }

        if self.position < self.tuples.len() {
            let tuple = self.tuples[self.position].clone();
            self.position += 1;
            Ok(Some(tuple))
        } else {
            Ok(None)
        }
    }

    fn close(&mut self) -> Result<()> {
        self.is_open = false;
        Ok(())
    }
}

// =============================================================================
// FilterExecutor: WHERE clause
// =============================================================================

/// Predicate function type
pub type PredicateFn = Box<dyn Fn(&Tuple) -> bool + Send>;

/// Filter executor for WHERE clause
///
/// Filters tuples based on a predicate.
pub struct FilterExecutor {
    /// Child executor providing input tuples
    child: Box<dyn Executor>,
    /// Predicate function to evaluate
    predicate: PredicateFn,
}

impl FilterExecutor {
    /// Create a new filter executor
    pub fn new(child: Box<dyn Executor>, predicate: PredicateFn) -> Self {
        Self { child, predicate }
    }
}

impl Executor for FilterExecutor {
    fn open(&mut self) -> Result<()> {
        self.child.open()
    }

    fn next(&mut self) -> Result<Option<Tuple>> {
        // Keep pulling from child until we find a matching tuple or exhaust input
        loop {
            match self.child.next()? {
                Some(tuple) => {
                    if (self.predicate)(&tuple) {
                        return Ok(Some(tuple));
                    }
                    // Otherwise continue to next tuple
                }
                None => return Ok(None),
            }
        }
    }

    fn close(&mut self) -> Result<()> {
        self.child.close()
    }
}

// =============================================================================
// ProjectExecutor: SELECT columns
// =============================================================================

/// Projection function type
pub type ProjectFn = Box<dyn Fn(&Tuple) -> Tuple + Send>;

/// Project executor for SELECT columns
///
/// Projects specific columns from input tuples.
pub struct ProjectExecutor {
    /// Child executor providing input tuples
    child: Box<dyn Executor>,
    /// Projection function
    project: ProjectFn,
}

impl ProjectExecutor {
    /// Create a new project executor
    pub fn new(child: Box<dyn Executor>, project: ProjectFn) -> Self {
        Self { child, project }
    }

    /// Create a project executor that selects specific column indices
    pub fn by_indices(child: Box<dyn Executor>, indices: Vec<usize>) -> Self {
        let project: ProjectFn = Box::new(move |tuple| {
            let values: Vec<Value> = indices
                .iter()
                .map(|&i| tuple.values.get(i).cloned().unwrap_or(Value::Null))
                .collect();
            Tuple::new(values)
        });
        Self { child, project }
    }
}

impl Executor for ProjectExecutor {
    fn open(&mut self) -> Result<()> {
        self.child.open()
    }

    fn next(&mut self) -> Result<Option<Tuple>> {
        match self.child.next()? {
            Some(tuple) => Ok(Some((self.project)(&tuple))),
            None => Ok(None),
        }
    }

    fn close(&mut self) -> Result<()> {
        self.child.close()
    }
}

// =============================================================================
// NestedLoopJoinExecutor: JOIN (nested loop)
// =============================================================================

/// Join condition function type
pub type JoinConditionFn = Box<dyn Fn(&Tuple, &Tuple) -> bool + Send>;

/// Nested loop join executor
///
/// Simple O(n*m) join implementation.
pub struct NestedLoopJoinExecutor {
    /// Left (outer) child executor
    left: Box<dyn Executor>,
    /// Right (inner) child executor
    right: Box<dyn Executor>,
    /// Join condition
    condition: JoinConditionFn,
    /// Current left tuple
    current_left: Option<Tuple>,
    /// Materialized right tuples (reloaded for each left tuple)
    right_tuples: Vec<Tuple>,
    /// Current position in right tuples
    right_position: usize,
    /// Whether right side has been materialized
    right_materialized: bool,
}

impl NestedLoopJoinExecutor {
    /// Create a new nested loop join executor
    pub fn new(
        left: Box<dyn Executor>,
        right: Box<dyn Executor>,
        condition: JoinConditionFn,
    ) -> Self {
        Self {
            left,
            right,
            condition,
            current_left: None,
            right_tuples: Vec::new(),
            right_position: 0,
            right_materialized: false,
        }
    }
}

// SAFETY: right_position is bounds-checked (< right_tuples.len()) in while loop condition.
#[allow(clippy::indexing_slicing)]
impl Executor for NestedLoopJoinExecutor {
    fn open(&mut self) -> Result<()> {
        self.left.open()?;
        self.right.open()?;
        self.current_left = None;
        self.right_tuples.clear();
        self.right_position = 0;
        self.right_materialized = false;
        Ok(())
    }

    fn next(&mut self) -> Result<Option<Tuple>> {
        // Materialize right side on first call
        if !self.right_materialized {
            while let Some(tuple) = self.right.next()? {
                self.right_tuples.push(tuple);
            }
            self.right_materialized = true;
        }

        loop {
            // Get current left tuple or advance to next
            if self.current_left.is_none() {
                self.current_left = self.left.next()?;
                self.right_position = 0;
            }

            // Process current left tuple if available
            let Some(left_tuple) = self.current_left.as_ref() else {
                return Ok(None); // Left side exhausted
            };

            // Scan through right tuples
            while self.right_position < self.right_tuples.len() {
                let right_tuple = &self.right_tuples[self.right_position];
                self.right_position += 1;

                if (self.condition)(left_tuple, right_tuple) {
                    // Combine tuples (concatenate values)
                    let mut combined_values = left_tuple.values.clone();
                    combined_values.extend(right_tuple.values.clone());
                    return Ok(Some(Tuple::new(combined_values)));
                }
            }

            // Right side exhausted for current left tuple, move to next left
            self.current_left = None;
        }
    }

    fn close(&mut self) -> Result<()> {
        self.left.close()?;
        self.right.close()?;
        self.right_tuples.clear();
        Ok(())
    }
}

// =============================================================================
// HashJoinExecutor: JOIN (hash-based)
// =============================================================================

/// Key extractor function type
pub type KeyExtractorFn = Box<dyn Fn(&Tuple) -> Value + Send>;

/// Hash join executor
///
/// Hash-based equi-join implementation.
pub struct HashJoinExecutor {
    /// Left (build) child executor
    left: Box<dyn Executor>,
    /// Right (probe) child executor
    right: Box<dyn Executor>,
    /// Left key extractor
    left_key: KeyExtractorFn,
    /// Right key extractor
    right_key: KeyExtractorFn,
    /// Hash table: key -> list of matching tuples
    hash_table: HashMap<String, Vec<Tuple>>,
    /// Current right tuple
    current_right: Option<Tuple>,
    /// Matching left tuples for current right
    current_matches: Vec<Tuple>,
    /// Position in current matches
    match_position: usize,
    /// Whether hash table has been built
    built: bool,
}

impl HashJoinExecutor {
    /// Create a new hash join executor
    pub fn new(
        left: Box<dyn Executor>,
        right: Box<dyn Executor>,
        left_key: KeyExtractorFn,
        right_key: KeyExtractorFn,
    ) -> Self {
        Self {
            left,
            right,
            left_key,
            right_key,
            hash_table: HashMap::new(),
            current_right: None,
            current_matches: Vec::new(),
            match_position: 0,
            built: false,
        }
    }

    /// Convert a value to a hashable string key
    fn value_to_key(value: &Value) -> String {
        format!("{:?}", value)
    }
}

// SAFETY: match_position is bounds-checked (< current_matches.len()) before indexing.
#[allow(clippy::indexing_slicing)]
impl Executor for HashJoinExecutor {
    fn open(&mut self) -> Result<()> {
        self.left.open()?;
        self.right.open()?;
        self.hash_table.clear();
        self.current_right = None;
        self.current_matches.clear();
        self.match_position = 0;
        self.built = false;
        Ok(())
    }

    fn next(&mut self) -> Result<Option<Tuple>> {
        // Build phase: create hash table from left side
        if !self.built {
            while let Some(tuple) = self.left.next()? {
                let key = Self::value_to_key(&(self.left_key)(&tuple));
                self.hash_table
                    .entry(key)
                    .or_insert_with(Vec::new)
                    .push(tuple);
            }
            self.built = true;
        }

        // Probe phase
        loop {
            // Return remaining matches for current right tuple
            if self.match_position < self.current_matches.len() {
                let left_tuple = &self.current_matches[self.match_position];
                self.match_position += 1;

                // current_right is guaranteed to be Some when we have matches
                let Some(right_tuple) = self.current_right.as_ref() else {
                    // This shouldn't happen, but handle gracefully by fetching next
                    self.current_matches.clear();
                    continue;
                };
                let mut combined_values = left_tuple.values.clone();
                combined_values.extend(right_tuple.values.clone());
                return Ok(Some(Tuple::new(combined_values)));
            }

            // Get next right tuple and find matches
            match self.right.next()? {
                Some(right_tuple) => {
                    let key = Self::value_to_key(&(self.right_key)(&right_tuple));

                    self.current_matches = self
                        .hash_table
                        .get(&key)
                        .cloned()
                        .unwrap_or_default();
                    self.match_position = 0;
                    self.current_right = Some(right_tuple);
                }
                None => return Ok(None),
            }
        }
    }

    fn close(&mut self) -> Result<()> {
        self.left.close()?;
        self.right.close()?;
        self.hash_table.clear();
        Ok(())
    }
}

// =============================================================================
// AggregateExecutor: GROUP BY aggregations
// =============================================================================

/// Group key extractor function type
pub type GroupKeyFn = Box<dyn Fn(&Tuple) -> Vec<Value> + Send>;

/// Aggregate specification
pub struct AggregateSpec {
    /// Aggregate function name (COUNT, SUM, etc.)
    pub function_name: String,
    /// Column index to aggregate (None for COUNT(*))
    pub column_index: Option<usize>,
}

/// Aggregate executor for GROUP BY
///
/// Groups tuples and computes aggregates.
pub struct AggregateExecutor {
    /// Child executor providing input tuples
    child: Box<dyn Executor>,
    /// Group key extractor (empty for no GROUP BY)
    group_key: GroupKeyFn,
    /// Aggregate specifications
    aggregates: Vec<AggregateSpec>,
    /// Computed results (materialized)
    results: Vec<Tuple>,
    /// Current position in results
    position: usize,
    /// Whether aggregation has been computed
    computed: bool,
}

// SAFETY: Accumulator array (entry.1) is sized to match self.aggregates via zip enumeration.
// self.position is bounds-checked (< self.results.len()) before indexing.
#[allow(clippy::indexing_slicing)]
impl AggregateExecutor {
    /// Create a new aggregate executor
    pub fn new(
        child: Box<dyn Executor>,
        group_key: GroupKeyFn,
        aggregates: Vec<AggregateSpec>,
    ) -> Self {
        Self {
            child,
            group_key,
            aggregates,
            results: Vec::new(),
            position: 0,
            computed: false,
        }
    }

    fn compute_aggregates(&mut self) -> Result<()> {
        // Materialize all input tuples grouped by key
        let mut groups: HashMap<String, (Vec<Value>, Vec<Box<dyn AggregateState>>)> = HashMap::new();

        while let Some(tuple) = self.child.next()? {
            let key_values = (self.group_key)(&tuple);
            let key_string = format!("{:?}", key_values);

            let entry = groups.entry(key_string).or_insert_with(|| {
                let states: Vec<Box<dyn AggregateState>> = self
                    .aggregates
                    .iter()
                    .map(|spec| {
                        create_aggregate(&spec.function_name)
                            .map(|f| f.init_state())
                            .unwrap_or_else(|| {
                                // Default to COUNT if unknown aggregate function
                                Box::new(CountState::default())
                            })
                    })
                    .collect();
                (key_values.clone(), states)
            });

            // Accumulate values into each aggregate
            for (i, spec) in self.aggregates.iter().enumerate() {
                let value = match spec.column_index {
                    Some(col_idx) => tuple.values.get(col_idx).cloned().unwrap_or(Value::Null),
                    None => Value::Int4(1), // COUNT(*) counts all rows
                };
                entry.1[i].accumulate(&value)?;
            }
        }

        // Finalize aggregates and build result tuples
        for (_, (key_values, states)) in groups {
            let mut result_values = key_values;
            for state in states {
                result_values.push(state.finalize()?);
            }
            self.results.push(Tuple::new(result_values));
        }

        self.computed = true;
        Ok(())
    }
}

// SAFETY: self.position is bounds-checked (< self.results.len()) before indexing.
#[allow(clippy::indexing_slicing)]
impl Executor for AggregateExecutor {
    fn open(&mut self) -> Result<()> {
        self.child.open()?;
        self.results.clear();
        self.position = 0;
        self.computed = false;
        Ok(())
    }

    fn next(&mut self) -> Result<Option<Tuple>> {
        if !self.computed {
            self.compute_aggregates()?;
        }

        if self.position < self.results.len() {
            let tuple = self.results[self.position].clone();
            self.position += 1;
            Ok(Some(tuple))
        } else {
            Ok(None)
        }
    }

    fn close(&mut self) -> Result<()> {
        self.child.close()?;
        self.results.clear();
        Ok(())
    }
}

// =============================================================================
// SortExecutor: ORDER BY
// =============================================================================

/// Sort key specification
#[derive(Clone)]
pub struct SortKey {
    /// Column index to sort by
    pub column_index: usize,
    /// Sort direction (true = ascending, false = descending)
    pub ascending: bool,
    /// Nulls first or last
    pub nulls_first: bool,
}

/// Sort executor for ORDER BY
///
/// Sorts tuples by specified keys.
pub struct SortExecutor {
    /// Child executor providing input tuples
    child: Box<dyn Executor>,
    /// Sort keys
    sort_keys: Vec<SortKey>,
    /// Sorted results (materialized)
    results: Vec<Tuple>,
    /// Current position in results
    position: usize,
    /// Whether sorting has been done
    sorted: bool,
}

impl SortExecutor {
    /// Create a new sort executor
    pub fn new(child: Box<dyn Executor>, sort_keys: Vec<SortKey>) -> Self {
        Self {
            child,
            sort_keys,
            results: Vec::new(),
            position: 0,
            sorted: false,
        }
    }

    fn compare_values(a: &Value, b: &Value) -> Ordering {
        match (a, b) {
            (Value::Null, Value::Null) => Ordering::Equal,
            (Value::Null, _) => Ordering::Less,
            (_, Value::Null) => Ordering::Greater,
            (Value::Int2(x), Value::Int2(y)) => x.cmp(y),
            (Value::Int4(x), Value::Int4(y)) => x.cmp(y),
            (Value::Int8(x), Value::Int8(y)) => x.cmp(y),
            (Value::Float4(x), Value::Float4(y)) => x.partial_cmp(y).unwrap_or(Ordering::Equal),
            (Value::Float8(x), Value::Float8(y)) => x.partial_cmp(y).unwrap_or(Ordering::Equal),
            (Value::String(x), Value::String(y)) => x.cmp(y),
            (Value::Boolean(x), Value::Boolean(y)) => x.cmp(y),
            _ => Ordering::Equal, // Can't compare different types
        }
    }

    fn compare_tuples(&self, a: &Tuple, b: &Tuple) -> Ordering {
        for key in &self.sort_keys {
            let a_val = a.values.get(key.column_index).unwrap_or(&Value::Null);
            let b_val = b.values.get(key.column_index).unwrap_or(&Value::Null);

            // Handle nulls
            let (first_null, second_null) = if key.nulls_first {
                (Ordering::Less, Ordering::Greater)
            } else {
                (Ordering::Greater, Ordering::Less)
            };

            let ordering = match (a_val, b_val) {
                (Value::Null, Value::Null) => Ordering::Equal,
                (Value::Null, _) => first_null,
                (_, Value::Null) => second_null,
                _ => Self::compare_values(a_val, b_val),
            };

            // Apply direction
            let ordering = if key.ascending {
                ordering
            } else {
                ordering.reverse()
            };

            if ordering != Ordering::Equal {
                return ordering;
            }
        }
        Ordering::Equal
    }

    fn sort_results(&mut self) -> Result<()> {
        // Materialize all input tuples
        while let Some(tuple) = self.child.next()? {
            self.results.push(tuple);
        }

        // Sort using comparison function
        let sort_keys = self.sort_keys.clone();
        self.results.sort_by(|a, b| {
            for key in &sort_keys {
                let a_val = a.values.get(key.column_index).unwrap_or(&Value::Null);
                let b_val = b.values.get(key.column_index).unwrap_or(&Value::Null);

                let (first_null, second_null) = if key.nulls_first {
                    (Ordering::Less, Ordering::Greater)
                } else {
                    (Ordering::Greater, Ordering::Less)
                };

                let ordering = match (a_val, b_val) {
                    (Value::Null, Value::Null) => Ordering::Equal,
                    (Value::Null, _) => first_null,
                    (_, Value::Null) => second_null,
                    _ => Self::compare_values(a_val, b_val),
                };

                let ordering = if key.ascending {
                    ordering
                } else {
                    ordering.reverse()
                };

                if ordering != Ordering::Equal {
                    return ordering;
                }
            }
            Ordering::Equal
        });

        self.sorted = true;
        Ok(())
    }
}

// SAFETY: self.position is bounds-checked (< self.results.len()) before indexing.
#[allow(clippy::indexing_slicing)]
impl Executor for SortExecutor {
    fn open(&mut self) -> Result<()> {
        self.child.open()?;
        self.results.clear();
        self.position = 0;
        self.sorted = false;
        Ok(())
    }

    fn next(&mut self) -> Result<Option<Tuple>> {
        if !self.sorted {
            self.sort_results()?;
        }

        if self.position < self.results.len() {
            let tuple = self.results[self.position].clone();
            self.position += 1;
            Ok(Some(tuple))
        } else {
            Ok(None)
        }
    }

    fn close(&mut self) -> Result<()> {
        self.child.close()?;
        self.results.clear();
        Ok(())
    }
}

// =============================================================================
// LimitExecutor: LIMIT/OFFSET
// =============================================================================

/// Limit executor for LIMIT/OFFSET
pub struct LimitExecutor {
    /// Child executor providing input tuples
    child: Box<dyn Executor>,
    /// Maximum number of tuples to return
    limit: Option<usize>,
    /// Number of tuples to skip
    offset: usize,
    /// Current count of returned tuples
    count: usize,
    /// Current count of skipped tuples
    skipped: usize,
}

impl LimitExecutor {
    /// Create a new limit executor
    pub fn new(child: Box<dyn Executor>, limit: Option<usize>, offset: usize) -> Self {
        Self {
            child,
            limit,
            offset,
            count: 0,
            skipped: 0,
        }
    }
}

impl Executor for LimitExecutor {
    fn open(&mut self) -> Result<()> {
        self.child.open()?;
        self.count = 0;
        self.skipped = 0;
        Ok(())
    }

    fn next(&mut self) -> Result<Option<Tuple>> {
        // Check if we've hit the limit
        if let Some(limit) = self.limit {
            if self.count >= limit {
                return Ok(None);
            }
        }

        // Skip offset tuples
        while self.skipped < self.offset {
            match self.child.next()? {
                Some(_) => self.skipped += 1,
                None => return Ok(None),
            }
        }

        // Return next tuple
        match self.child.next()? {
            Some(tuple) => {
                self.count += 1;
                Ok(Some(tuple))
            }
            None => Ok(None),
        }
    }

    fn close(&mut self) -> Result<()> {
        self.child.close()
    }
}

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

    fn test_tuples() -> Vec<Tuple> {
        vec![
            Tuple::new(vec![Value::Int4(1), Value::String("Alice".to_string()), Value::Int4(30)]),
            Tuple::new(vec![Value::Int4(2), Value::String("Bob".to_string()), Value::Int4(25)]),
            Tuple::new(vec![Value::Int4(3), Value::String("Charlie".to_string()), Value::Int4(35)]),
        ]
    }

    #[test]
    fn test_scan_executor() {
        let mut scan = ScanExecutor::new(test_tuples());
        scan.open().unwrap();

        let mut count = 0;
        while scan.next().unwrap().is_some() {
            count += 1;
        }
        assert_eq!(count, 3);

        scan.close().unwrap();
    }

    #[test]
    fn test_filter_executor() {
        let scan = Box::new(ScanExecutor::new(test_tuples()));
        let predicate: PredicateFn = Box::new(|tuple| {
            matches!(&tuple.values[2], Value::Int4(age) if *age > 28)
        });
        let mut filter = FilterExecutor::new(scan, predicate);
        filter.open().unwrap();

        let mut count = 0;
        while filter.next().unwrap().is_some() {
            count += 1;
        }
        assert_eq!(count, 2); // Alice (30) and Charlie (35)

        filter.close().unwrap();
    }

    #[test]
    fn test_project_executor() {
        let scan = Box::new(ScanExecutor::new(test_tuples()));
        let mut project = ProjectExecutor::by_indices(scan, vec![1]); // Just names
        project.open().unwrap();

        let tuple = project.next().unwrap().unwrap();
        assert_eq!(tuple.values.len(), 1);
        assert_eq!(tuple.values[0], Value::String("Alice".to_string()));

        project.close().unwrap();
    }

    #[test]
    fn test_sort_executor() {
        let scan = Box::new(ScanExecutor::new(test_tuples()));
        let sort_keys = vec![SortKey {
            column_index: 2, // Sort by age
            ascending: true,
            nulls_first: true,
        }];
        let mut sort = SortExecutor::new(scan, sort_keys);
        sort.open().unwrap();

        // First should be Bob (25)
        let first = sort.next().unwrap().unwrap();
        assert_eq!(first.values[1], Value::String("Bob".to_string()));

        // Then Alice (30)
        let second = sort.next().unwrap().unwrap();
        assert_eq!(second.values[1], Value::String("Alice".to_string()));

        sort.close().unwrap();
    }

    #[test]
    fn test_limit_executor() {
        let scan = Box::new(ScanExecutor::new(test_tuples()));
        let mut limit = LimitExecutor::new(scan, Some(2), 0);
        limit.open().unwrap();

        let mut count = 0;
        while limit.next().unwrap().is_some() {
            count += 1;
        }
        assert_eq!(count, 2);

        limit.close().unwrap();
    }
}