vibesql-executor 0.2.0

Query execution engine for vibesql SQL database
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
//! Parallel execution heuristics and configuration
//!
//! This module provides intelligent, hardware-aware parallelism that automatically
//! determines when to use parallel execution based on:
//! - Available CPU cores
//! - Row count
//! - Operation type
//! - User overrides via PARALLEL_THRESHOLD environment variable
//!
//! Note: This module is only compiled when the `parallel` feature is enabled.
//! In WASM builds, this feature is disabled as parallelism provides no benefit.

use std::sync::OnceLock;

/// Configuration for parallel execution decisions
#[derive(Debug, Clone, Copy)]
pub struct ParallelConfig {
    /// Number of threads available (from rayon)
    #[allow(dead_code)]
    pub num_threads: usize,
    /// Thresholds for different operations based on hardware tier
    pub thresholds: ParallelThresholds,
}

/// Operation-specific row count thresholds for parallel execution
#[derive(Debug, Clone, Copy)]
pub struct ParallelThresholds {
    /// Threshold for scan/filter operations
    pub scan_filter: usize,
    /// Threshold for aggregation operations
    pub aggregate: usize,
    /// Threshold for join operations
    pub join: usize,
    /// Threshold for sort operations
    pub sort: usize,
}

impl ParallelConfig {
    /// Get or initialize the global parallel configuration
    ///
    /// Note: This uses OnceLock for the user-specified threshold override,
    /// but re-checks rayon::current_num_threads() each time to handle
    /// custom thread pools created after initial configuration.
    pub fn global() -> Self {
        // Cache the user threshold override (or None) - this is checked once
        static THRESHOLD_OVERRIDE: OnceLock<Option<ParallelThresholds>> = OnceLock::new();
        let override_thresholds = THRESHOLD_OVERRIDE.get_or_init(|| {
            std::env::var("PARALLEL_THRESHOLD").ok().map(|s| Self::parse_threshold_override(&s))
        });

        // Always check current thread count - this allows custom pools to work
        let num_threads = rayon::current_num_threads();

        let thresholds = match override_thresholds {
            Some(t) => *t,
            None => Self::thresholds_for_hardware(num_threads),
        };

        ParallelConfig { num_threads, thresholds }
    }

    /// Detect hardware capabilities and create appropriate configuration (legacy, unused)
    #[allow(dead_code)]
    fn detect() -> Self {
        let num_threads = rayon::current_num_threads();

        // Debug: Log when ParallelConfig is initialized
        if std::env::var("MORSEL_DEBUG").is_ok() {
            eprintln!("[MORSEL] ParallelConfig initialized: detected {} threads", num_threads);
        }

        // Check for user override of threshold
        let thresholds = if let Ok(threshold_str) = std::env::var("PARALLEL_THRESHOLD") {
            Self::parse_threshold_override(&threshold_str)
        } else {
            // Auto-detect based on hardware
            Self::thresholds_for_hardware(num_threads)
        };

        ParallelConfig { num_threads, thresholds }
    }

    /// Parse PARALLEL_THRESHOLD environment variable
    /// Supports:
    /// - Numbers: "5000" -> custom threshold
    /// - "max" or "disabled" -> effectively disable parallelism
    fn parse_threshold_override(threshold_str: &str) -> ParallelThresholds {
        let threshold_str = threshold_str.trim().to_lowercase();

        if threshold_str == "max" || threshold_str == "disabled" {
            // Effectively disable by setting impossibly high threshold
            ParallelThresholds {
                scan_filter: usize::MAX,
                aggregate: usize::MAX,
                join: usize::MAX,
                sort: usize::MAX,
            }
        } else if let Ok(threshold) = threshold_str.parse::<usize>() {
            // Use custom threshold for all operations
            ParallelThresholds {
                scan_filter: threshold,
                aggregate: threshold,
                join: threshold,
                sort: threshold,
            }
        } else {
            // Invalid value, fall back to auto-detection
            Self::thresholds_for_hardware(rayon::current_num_threads())
        }
    }

    /// Determine appropriate thresholds based on hardware tier
    fn thresholds_for_hardware(num_threads: usize) -> ParallelThresholds {
        match num_threads {
            // Single core: never parallelize
            1 => ParallelThresholds {
                scan_filter: usize::MAX,
                aggregate: usize::MAX,
                join: usize::MAX,
                sort: usize::MAX,
            },
            // 2-3 cores: very conservative (most overhead from parallel coordination)
            2..=3 => ParallelThresholds {
                scan_filter: 10_000,
                aggregate: 12_500,
                join: 15_000,
                sort: 15_000,
            },
            // 4-7 cores: moderate thresholds - lowered for better 100-1000 row performance
            4..=7 => ParallelThresholds {
                scan_filter: 2_500,
                aggregate: 3_750,
                join: 5_000,
                sort: 5_000,
            },
            // 8+ cores: aggressive thresholds - lowered significantly for 100-1000 row datasets
            // Modern multi-core hardware benefits from earlier parallelization
            _ => ParallelThresholds {
                scan_filter: 1_000,
                aggregate: 1_500,
                join: 2_500,
                sort: 2_000,
            },
        }
    }

    /// Check if parallel execution should be used for a scan/filter operation
    pub fn should_parallelize_scan(&self, row_count: usize) -> bool {
        row_count >= self.thresholds.scan_filter
    }

    /// Check if parallel execution should be used for an aggregation operation
    #[allow(dead_code)]
    pub fn should_parallelize_aggregate(&self, row_count: usize) -> bool {
        row_count >= self.thresholds.aggregate
    }

    /// Check if parallel execution should be used for a join operation
    #[allow(dead_code)]
    pub fn should_parallelize_join(&self, row_count: usize) -> bool {
        row_count >= self.thresholds.join
    }

    /// Check if parallel execution should be used for a sort operation
    #[allow(dead_code)]
    pub fn should_parallelize_sort(&self, row_count: usize) -> bool {
        row_count >= self.thresholds.sort
    }
}

//
// Parallel Scan Operations
//

use rayon::prelude::*;
use vibesql_storage::Row;

use super::morsel::{
    global_config, morsel_parallel_filter, morsel_parallel_filter_map, morsel_parallel_map,
    morsel_parallel_sort,
};

/// Parallel scan with filtering predicate.
///
/// Applies a predicate function to each row and returns only those rows
/// where the predicate returns `true`. Automatically uses parallel execution
/// when beneficial based on hardware capabilities and row count.
///
/// # Arguments
/// * `rows` - Input rows to scan
/// * `predicate` - Function to test each row (returns true to keep, false to filter out)
///
/// # Returns
/// Vector of rows that satisfy the predicate
///
/// # Example
/// ```text
/// // Filter rows where column 0 > 100
/// let filtered = parallel_scan_filter(rows, |row| {
///     matches!(row.values[0], SqlValue::Integer(x) if x > 100)
/// });
/// ```
#[allow(dead_code)]
pub fn parallel_scan_filter<F>(rows: &[Row], predicate: F) -> Vec<Row>
where
    F: Fn(&Row) -> bool + Sync + Send,
{
    let config = ParallelConfig::global();

    if config.should_parallelize_scan(rows.len()) {
        // Parallel path: use rayon for efficient parallel filtering
        rows.par_iter().filter(|row| predicate(row)).cloned().collect()
    } else {
        // Sequential fallback for small datasets
        rows.iter().filter(|row| predicate(row)).cloned().collect()
    }
}

/// Parallel scan with transformation function.
///
/// Applies a transformation function to each row. Can be used for projection
/// (selecting specific columns) or other row transformations. Automatically
/// uses parallel execution when beneficial.
///
/// # Arguments
/// * `rows` - Input rows to scan
/// * `transform` - Function to transform each row (must not panic or return Err)
///
/// # Returns
/// Vector of transformed rows
///
/// # Example
/// ```text
/// // Project first two columns only
/// let projected = parallel_scan_map(rows, |row| {
///     Row {
///         values: row.values[..2].to_vec()
///     }
/// });
/// ```
#[allow(dead_code)]
pub fn parallel_scan_map<F>(rows: &[Row], transform: F) -> Vec<Row>
where
    F: Fn(&Row) -> Row + Sync + Send,
{
    let config = ParallelConfig::global();

    if config.should_parallelize_scan(rows.len()) {
        // Parallel path: use rayon for efficient parallel mapping
        rows.par_iter().map(&transform).collect()
    } else {
        // Sequential fallback for small datasets
        rows.iter().map(transform).collect()
    }
}

/// Parallel scan with filter-map for combined filtering and transformation.
///
/// Applies a function that can both filter (return None) and transform (return Some).
/// This is more efficient than separate filter + map operations.
///
/// # Arguments
/// * `rows` - Input rows to scan
/// * `filter_map` - Function that returns Some(row) to keep/transform, None to filter out
///
/// # Returns
/// Vector of transformed rows (where function returned Some)
///
/// # Example
/// ```text
/// // Filter AND project in one pass
/// let result = parallel_scan_filter_map(rows, |row| {
///     if matches!(row.values[0], SqlValue::Integer(x) if x > 100) {
///         Some(Row { values: row.values[..2].to_vec() })
///     } else {
///         None
///     }
/// });
/// ```
#[allow(dead_code)]
pub fn parallel_scan_filter_map<F>(rows: &[Row], filter_map: F) -> Vec<Row>
where
    F: Fn(&Row) -> Option<Row> + Sync + Send,
{
    let config = ParallelConfig::global();

    if config.should_parallelize_scan(rows.len()) {
        // Parallel path: use rayon for efficient parallel filter-map
        rows.par_iter().filter_map(&filter_map).collect()
    } else {
        // Sequential fallback for small datasets
        rows.iter().filter_map(filter_map).collect()
    }
}

/// Parallel scan that simply materializes a slice into a Vec.
///
/// When row count exceeds threshold, uses parallel copying for better
/// memory bandwidth utilization on large datasets.
///
/// # Arguments
/// * `rows` - Input row slice to materialize
///
/// # Returns
/// Vector containing clones of all input rows
pub fn parallel_scan_materialize(rows: &[Row]) -> Vec<Row> {
    let config = ParallelConfig::global();

    if config.should_parallelize_scan(rows.len()) {
        // Parallel path: use rayon for efficient parallel cloning
        rows.par_iter().cloned().collect()
    } else {
        // Sequential fallback for small datasets
        rows.to_vec()
    }
}

//
// Morsel-Driven Parallel Operations
//
// These functions use the morsel-driven dispatcher for dynamic load balancing.
// Use these for large datasets where work distribution may be uneven.
//

/// Morsel-driven parallel scan with filtering predicate.
///
/// Uses work-stealing for dynamic load balancing when processing rows
/// with varying predicate evaluation costs.
///
/// # When to Use
///
/// - Large datasets (>100K rows) with complex predicates
/// - Predicates with varying evaluation cost per row
/// - When you observe load imbalance with static partitioning
///
/// For simpler cases, `parallel_scan_filter` with static partitioning may suffice.
#[allow(dead_code)]
pub fn morsel_scan_filter<F>(rows: &[Row], predicate: F) -> Vec<Row>
where
    F: Fn(&Row) -> bool + Sync + Send,
{
    let config = ParallelConfig::global();

    if config.should_parallelize_scan(rows.len()) {
        // Use morsel-driven execution for dynamic load balancing
        morsel_parallel_filter(rows, global_config(), predicate)
    } else {
        // Sequential fallback for small datasets
        rows.iter().filter(|row| predicate(row)).cloned().collect()
    }
}

/// Morsel-driven parallel scan with transformation function.
///
/// Uses work-stealing for dynamic load balancing when processing rows
/// with varying transformation costs.
#[allow(dead_code)]
pub fn morsel_scan_map<F>(rows: &[Row], transform: F) -> Vec<Row>
where
    F: Fn(&Row) -> Row + Sync + Send,
{
    let config = ParallelConfig::global();

    if config.should_parallelize_scan(rows.len()) {
        // Use morsel-driven execution for dynamic load balancing
        morsel_parallel_map(rows, global_config(), transform)
    } else {
        // Sequential fallback for small datasets
        rows.iter().map(transform).collect()
    }
}

/// Morsel-driven parallel scan with filter-map for combined filtering and transformation.
///
/// Uses work-stealing for dynamic load balancing.
#[allow(dead_code)]
pub fn morsel_scan_filter_map<F>(rows: &[Row], filter_map: F) -> Vec<Row>
where
    F: Fn(&Row) -> Option<Row> + Sync + Send,
{
    let config = ParallelConfig::global();

    if config.should_parallelize_scan(rows.len()) {
        // Use morsel-driven execution
        morsel_parallel_filter_map(rows, global_config(), filter_map)
    } else {
        // Sequential fallback for small datasets
        rows.iter().filter_map(filter_map).collect()
    }
}

/// Morsel-driven parallel sort with work-stealing.
///
/// Sorts rows using a two-phase approach:
/// 1. **Phase 1 (Parallel)**: Workers steal morsels and sort them locally
/// 2. **Phase 2 (Sequential)**: K-way merge of sorted morsels using a min-heap
///
/// This provides dynamic load balancing during the sort phase, which is beneficial
/// when comparison costs vary across rows (e.g., variable-length strings, complex keys).
///
/// # When to Use
///
/// - Large datasets (>100K rows) with varying comparison costs
/// - When you observe load imbalance with static partitioning (`par_sort_by`)
/// - Queries with ORDER BY on multiple columns or complex expressions
///
/// For simpler cases with uniform comparison costs, rayon's `par_sort_by` may suffice.
///
/// # Arguments
/// * `rows` - Input rows to sort
/// * `compare` - Comparison function for ordering rows
///
/// # Returns
/// New vector containing sorted rows
#[allow(dead_code)]
pub fn morsel_scan_sort<F>(rows: &[Row], compare: F) -> Vec<Row>
where
    F: Fn(&Row, &Row) -> std::cmp::Ordering + Sync + Send,
{
    let config = ParallelConfig::global();

    if config.should_parallelize_sort(rows.len()) {
        // Use morsel-driven execution for dynamic load balancing
        morsel_parallel_sort(rows, global_config(), compare)
    } else {
        // Sequential fallback for small datasets
        let mut result = rows.to_vec();
        result.sort_by(compare);
        result
    }
}

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

    #[test]
    fn test_hardware_detection() {
        let config = ParallelConfig::detect();
        // Should detect at least 1 thread
        assert!(config.num_threads >= 1);
    }

    #[test]
    fn test_threshold_override_custom_value() {
        // Test the parse function directly to avoid global state issues
        let thresholds = ParallelConfig::parse_threshold_override("5000");
        assert_eq!(thresholds.scan_filter, 5000);
        assert_eq!(thresholds.aggregate, 5000);
    }

    #[test]
    fn test_threshold_override_max() {
        // Test the parse function directly to avoid global state issues
        let thresholds = ParallelConfig::parse_threshold_override("max");
        assert_eq!(thresholds.scan_filter, usize::MAX);
        assert_eq!(thresholds.aggregate, usize::MAX);
    }

    #[test]
    fn test_threshold_override_disabled() {
        // Test the parse function directly to avoid global state issues
        let thresholds = ParallelConfig::parse_threshold_override("disabled");
        assert_eq!(thresholds.scan_filter, usize::MAX);
    }

    #[test]
    fn test_single_core_never_parallelizes() {
        let thresholds = ParallelConfig::thresholds_for_hardware(1);
        assert_eq!(thresholds.scan_filter, usize::MAX);
    }

    #[test]
    fn test_conservative_thresholds_2_3_cores() {
        let thresholds = ParallelConfig::thresholds_for_hardware(2);
        assert_eq!(thresholds.scan_filter, 10_000);

        let thresholds = ParallelConfig::thresholds_for_hardware(3);
        assert_eq!(thresholds.scan_filter, 10_000);
    }

    #[test]
    fn test_moderate_thresholds_4_7_cores() {
        let thresholds = ParallelConfig::thresholds_for_hardware(4);
        assert_eq!(thresholds.scan_filter, 2_500);

        let thresholds = ParallelConfig::thresholds_for_hardware(7);
        assert_eq!(thresholds.scan_filter, 2_500);
    }

    #[test]
    fn test_aggressive_thresholds_8_plus_cores() {
        let thresholds = ParallelConfig::thresholds_for_hardware(8);
        assert_eq!(thresholds.scan_filter, 1_000);

        let thresholds = ParallelConfig::thresholds_for_hardware(16);
        assert_eq!(thresholds.scan_filter, 1_000);
    }

    #[test]
    fn test_should_parallelize_scan() {
        // Simulate 8+ core system
        let config = ParallelConfig {
            num_threads: 8,
            thresholds: ParallelConfig::thresholds_for_hardware(8),
        };

        // Below threshold
        assert!(!config.should_parallelize_scan(500));

        // At threshold
        assert!(config.should_parallelize_scan(1_000));

        // Above threshold
        assert!(config.should_parallelize_scan(10_000));
    }

    #[test]
    fn test_invalid_threshold_override_falls_back_to_auto() {
        // Test the parse function directly to avoid global state issues
        let thresholds = ParallelConfig::parse_threshold_override("invalid");

        // Should fall back to auto-detection based on current hardware
        let auto_thresholds = ParallelConfig::thresholds_for_hardware(rayon::current_num_threads());
        assert_eq!(thresholds.scan_filter, auto_thresholds.scan_filter);
    }

    // Tests for parallel scan functions

    fn create_test_rows(count: usize) -> Vec<Row> {
        (0..count)
            .map(|i| {
                Row::from_vec(vec![
                    vibesql_types::SqlValue::Integer(i as i64),
                    vibesql_types::SqlValue::Varchar(arcstr::ArcStr::from(format!("row{}", i))),
                ])
            })
            .collect()
    }

    #[test]
    fn test_parallel_scan_filter() {
        let rows = create_test_rows(100);

        // Filter for even numbers
        let filtered = parallel_scan_filter(
            &rows,
            |row| matches!(row.values[0], vibesql_types::SqlValue::Integer(x) if x % 2 == 0),
        );

        // Should have 50 even numbers (0, 2, 4, ..., 98)
        assert_eq!(filtered.len(), 50);

        // Verify first few results
        assert!(matches!(filtered[0].values[0], vibesql_types::SqlValue::Integer(0)));
        assert!(matches!(filtered[1].values[0], vibesql_types::SqlValue::Integer(2)));
        assert!(matches!(filtered[2].values[0], vibesql_types::SqlValue::Integer(4)));
    }

    #[test]
    fn test_parallel_scan_filter_empty() {
        let rows = create_test_rows(100);

        // Filter that matches nothing
        let filtered = parallel_scan_filter(&rows, |_| false);

        assert_eq!(filtered.len(), 0);
    }

    #[test]
    fn test_parallel_scan_filter_all() {
        let rows = create_test_rows(100);

        // Filter that matches everything
        let filtered = parallel_scan_filter(&rows, |_| true);

        assert_eq!(filtered.len(), 100);
    }

    #[test]
    fn test_parallel_scan_map() {
        let rows = create_test_rows(10);

        // Double all integers
        let transformed = parallel_scan_map(&rows, |row| {
            let mut new_row = row.clone();
            if let vibesql_types::SqlValue::Integer(x) = row.values[0] {
                new_row.values[0] = vibesql_types::SqlValue::Integer(x * 2);
            }
            new_row
        });

        assert_eq!(transformed.len(), 10);
        assert!(matches!(transformed[0].values[0], vibesql_types::SqlValue::Integer(0)));
        assert!(matches!(transformed[1].values[0], vibesql_types::SqlValue::Integer(2)));
        assert!(matches!(transformed[5].values[0], vibesql_types::SqlValue::Integer(10)));
    }

    #[test]
    fn test_parallel_scan_filter_map() {
        let rows = create_test_rows(100);

        // Filter for even numbers AND double them
        let result = parallel_scan_filter_map(&rows, |row| {
            if let vibesql_types::SqlValue::Integer(x) = row.values[0] {
                if x % 2 == 0 {
                    let mut new_row = row.clone();
                    new_row.values[0] = vibesql_types::SqlValue::Integer(x * 2);
                    return Some(new_row);
                }
            }
            None
        });

        // Should have 50 even numbers, doubled
        assert_eq!(result.len(), 50);
        assert!(matches!(result[0].values[0], vibesql_types::SqlValue::Integer(0)));
        assert!(matches!(result[1].values[0], vibesql_types::SqlValue::Integer(4))); // 2*2
        assert!(matches!(result[2].values[0], vibesql_types::SqlValue::Integer(8)));
        // 4*2
    }

    #[test]
    fn test_parallel_scan_materialize() {
        let rows = create_test_rows(50);

        let materialized = parallel_scan_materialize(&rows);

        assert_eq!(materialized.len(), 50);
        // Verify content is identical
        for (i, row) in materialized.iter().enumerate() {
            assert!(matches!(row.values[0], vibesql_types::SqlValue::Integer(x) if x == i as i64));
        }
    }

    #[test]
    fn test_parallel_scan_small_dataset_uses_sequential() {
        // With small dataset, should use sequential path
        // (This is implicit in the implementation, but we test correctness)
        let rows = create_test_rows(10); // Well below any threshold

        let filtered = parallel_scan_filter(
            &rows,
            |row| matches!(row.values[0], vibesql_types::SqlValue::Integer(x) if x < 5),
        );

        assert_eq!(filtered.len(), 5);
    }

    #[test]
    fn test_parallel_scan_preserves_row_structure() {
        let rows = vec![
            Row::from_vec(vec![
                vibesql_types::SqlValue::Integer(1),
                vibesql_types::SqlValue::Varchar(arcstr::ArcStr::from("test")),
                vibesql_types::SqlValue::Null,
            ]),
            Row::from_vec(vec![
                vibesql_types::SqlValue::Integer(2),
                vibesql_types::SqlValue::Varchar(arcstr::ArcStr::from("test2")),
                vibesql_types::SqlValue::Double(3.5),
            ]),
        ];

        let filtered = parallel_scan_filter(&rows, |_| true);

        assert_eq!(filtered.len(), 2);
        assert_eq!(filtered[0].values.len(), 3);
        assert_eq!(filtered[1].values.len(), 3);
        assert!(matches!(filtered[0].values[2], vibesql_types::SqlValue::Null));
        assert!(matches!(filtered[1].values[2], vibesql_types::SqlValue::Double(x) if x == 3.5));
    }
}