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
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
//! Columnar execution for high-performance aggregation queries
//!
//! This module implements column-oriented query execution that avoids
//! materializing full Row objects during table scans, providing 8-10x speedup
//! for aggregation-heavy workloads.
//!
//! ## Architecture
//!
//! Instead of:
//! ```text
//! TableScan → Row{Vec<SqlValue>} → Filter(Row) → Aggregate(Row) → Vec<Row>
//! ```
//!
//! We use:
//! ```text
//! TableScan → ColumnRefs → Filter(native types) → Aggregate → Row
//! ```
//!
//! ## Benefits
//!
//! - **Zero-copy**: Work with `&SqlValue` references instead of cloning
//! - **Cache-friendly**: Access contiguous column data instead of scattered row data
//! - **Type-specialized**: Skip SqlValue enum matching overhead for filters/aggregates
//! - **Minimal allocations**: Only allocate result rows, not intermediate data
//!
//! ## Usage
//!
//! This path is automatically selected for simple aggregate queries that:
//! - Have a single table scan (no JOINs)
//! - Use simple WHERE predicates
//! - Compute aggregates (SUM, COUNT, AVG, MIN, MAX)
//! - Don't use window functions or complex subqueries

mod aggregate;
pub mod batch;
mod executor;
pub mod filter;
mod scan;
mod string_ops;

// Auto-vectorized SIMD operations - replaces the `wide` crate dependency
// See simd_ops.rs for documentation on why these patterns are structured this way
pub mod simd_ops;

mod simd_aggregate;
pub mod simd_filter;
mod simd_join;

pub use aggregate::{
    columnar_group_by, columnar_group_by_batch, compute_aggregates_from_batch,
    compute_multiple_aggregates, evaluate_expression_to_column,
    evaluate_expression_with_cached_column, extract_aggregates, AggregateOp, AggregateSource,
    AggregateSpec,
};
pub use batch::{ColumnArray, ColumnarBatch};
pub use executor::execute_columnar_batch;
pub use filter::{
    apply_columnar_filter, apply_columnar_filter_simd_streaming, create_filter_bitmap,
    create_filter_bitmap_tree, evaluate_predicate_tree, extract_column_predicates,
    extract_predicate_tree, ColumnPredicate, PredicateTree,
};
use log;
pub use scan::ColumnarScan;
pub use simd_aggregate::{can_use_simd_for_column, simd_aggregate_f64, simd_aggregate_i64};
pub use simd_filter::{
    simd_create_filter_mask, simd_create_filter_mask_auto, simd_create_filter_mask_packed,
    simd_filter_batch, simd_filter_to_indices,
};
#[cfg(feature = "parallel")]
pub use simd_filter::{simd_create_filter_mask_parallel, simd_filter_batch_parallel};
pub use simd_join::columnar_hash_join_inner;
pub use simd_ops::PackedMask;
use vibesql_storage::Row;
use vibesql_types::SqlValue;

use crate::{errors::ExecutorError, schema::CombinedSchema};

/// Execute a columnar aggregate query with filtering
///
/// This is a simplified entry point for columnar execution that demonstrates
/// the full pipeline: scan → filter → aggregate.
///
/// # Arguments
///
/// * `rows` - Input rows to process
/// * `predicates` - Column predicates for filtering (optional)
/// * `aggregates` - List of (column_index, aggregate_op) pairs to compute
///
/// # Returns
///
/// A single Row containing the computed aggregate values
///
/// # Example
///
/// ```text
/// // Compute SUM(col0), AVG(col1) WHERE col2 < 100
/// let predicates = vec![
///     ColumnPredicate::LessThan {
///         column_idx: 2,
///         value: SqlValue::Integer(100),
///     },
/// ];
/// let aggregates = vec![
///     (0, AggregateOp::Sum),
///     (1, AggregateOp::Avg),
/// ];
///
/// let result = execute_columnar_aggregate(&rows, &predicates, &aggregates)?;
/// ```
///
/// Note: This function provides SIMD-accelerated filtering and aggregation through
/// LLVM auto-vectorization of batch-native operations.
pub fn execute_columnar_aggregate(
    rows: &[Row],
    predicates: &[ColumnPredicate],
    aggregates: &[aggregate::AggregateSpec],
    schema: Option<&CombinedSchema>,
) -> Result<Vec<Row>, ExecutorError> {
    // Early return for empty input
    // SQL standard: COUNT returns 0 for empty input, other aggregates return NULL
    if rows.is_empty() {
        let values: Vec<SqlValue> = aggregates
            .iter()
            .map(|spec| match spec.op {
                aggregate::AggregateOp::Count => SqlValue::Integer(0),
                _ => SqlValue::Null,
            })
            .collect();
        return Ok(vec![Row::new(values)]);
    }

    // Phase 1: Convert to columnar batch for SIMD acceleration
    #[cfg(feature = "profile-q6")]
    let batch_start = std::time::Instant::now();

    let batch = ColumnarBatch::from_rows(rows)?;

    #[cfg(feature = "profile-q6")]
    {
        let batch_time = batch_start.elapsed();
        eprintln!("[PROFILE-Q6]   Phase 1 - Convert to batch: {:?}", batch_time);
    }

    // Phase 2: Apply SIMD-accelerated filtering
    #[cfg(feature = "profile-q6")]
    let filter_start = std::time::Instant::now();

    let filtered_batch =
        if predicates.is_empty() { batch.clone() } else { simd_filter_batch(&batch, predicates)? };

    #[cfg(feature = "profile-q6")]
    {
        let filter_time = filter_start.elapsed();
        eprintln!(
            "[PROFILE-Q6]   Phase 2 - SIMD filter: {:?} ({}/{} rows passed)",
            filter_time,
            filtered_batch.row_count(),
            rows.len()
        );
    }

    // Phase 3: Compute aggregates directly on batch (no row conversion!)
    #[cfg(feature = "profile-q6")]
    let agg_start = std::time::Instant::now();

    // Use batch-native aggregation to avoid to_rows() conversion overhead
    let results = compute_aggregates_from_batch(&filtered_batch, aggregates, schema)?;

    #[cfg(feature = "profile-q6")]
    {
        let agg_time = agg_start.elapsed();
        eprintln!(
            "[PROFILE-Q6]   Phase 3 - Batch-native aggregate: {:?} ({} aggregates)",
            agg_time,
            aggregates.len()
        );
    }

    // Return as single row
    Ok(vec![Row::new(results)])
}

/// Fast single-pass aggregate on rows - avoids batch conversion overhead
///
/// This function performs filtering and aggregation in a single pass over the input rows,
/// without converting to columnar format. It's 3-5x faster than `execute_columnar_aggregate`
/// for queries that come from row-based storage.
///
/// # Use Cases
///
/// Best suited for:
/// - Simple aggregate queries without GROUP BY
/// - When data arrives as Vec<Row> (not native columnar)
/// - TPC-H style queries: SUM(price * discount) WHERE ...
///
/// # Arguments
///
/// * `rows` - Input rows to process
/// * `predicates` - Column predicates for filtering
/// * `aggregates` - Aggregate specifications
///
/// # Returns
///
/// A single Row containing the computed aggregate values
pub fn fast_aggregate_on_rows(
    rows: &[Row],
    predicates: &[ColumnPredicate],
    aggregates: &[aggregate::AggregateSpec],
) -> Result<Vec<Row>, ExecutorError> {
    use aggregate::{AggregateOp, AggregateSource};

    // Early return for empty input
    if rows.is_empty() {
        let values: Vec<SqlValue> = aggregates
            .iter()
            .map(|spec| match spec.op {
                AggregateOp::Count => SqlValue::Integer(0),
                _ => SqlValue::Null,
            })
            .collect();
        return Ok(vec![Row::new(values)]);
    }

    // Initialize accumulators for each aggregate
    struct Accumulator {
        sum_f64: f64,
        sum_i64: i64,
        count: i64,
        min_f64: Option<f64>,
        max_f64: Option<f64>,
        min_i64: Option<i64>,
        max_i64: Option<i64>,
        is_integer: bool,
    }

    let mut accumulators: Vec<Accumulator> = aggregates
        .iter()
        .map(|_| Accumulator {
            sum_f64: 0.0,
            sum_i64: 0,
            count: 0,
            min_f64: None,
            max_f64: None,
            min_i64: None,
            max_i64: None,
            is_integer: true,
        })
        .collect();

    // Single pass: filter and accumulate
    for row in rows {
        // Check all predicates
        let passes_filter = predicates.iter().all(|pred| evaluate_predicate(row, pred));

        if !passes_filter {
            continue;
        }

        // Accumulate values for each aggregate
        for (i, spec) in aggregates.iter().enumerate() {
            let acc = &mut accumulators[i];

            match &spec.source {
                AggregateSource::CountStar => {
                    acc.count += 1;
                }
                AggregateSource::Column(col_idx) => {
                    if let Some(value) = row.get(*col_idx) {
                        if !matches!(value, SqlValue::Null) {
                            acc.count += 1;
                            match value {
                                SqlValue::Integer(v) => {
                                    acc.sum_i64 += v;
                                    acc.sum_f64 += *v as f64;
                                    acc.min_i64 = Some(acc.min_i64.map_or(*v, |m| m.min(*v)));
                                    acc.max_i64 = Some(acc.max_i64.map_or(*v, |m| m.max(*v)));
                                    acc.min_f64 =
                                        Some(acc.min_f64.map_or(*v as f64, |m| m.min(*v as f64)));
                                    acc.max_f64 =
                                        Some(acc.max_f64.map_or(*v as f64, |m| m.max(*v as f64)));
                                }
                                SqlValue::Double(v) => {
                                    acc.is_integer = false;
                                    acc.sum_f64 += v;
                                    acc.min_f64 = Some(acc.min_f64.map_or(*v, |m| m.min(*v)));
                                    acc.max_f64 = Some(acc.max_f64.map_or(*v, |m| m.max(*v)));
                                }
                                SqlValue::Float(v) => {
                                    acc.is_integer = false;
                                    acc.sum_f64 += *v as f64;
                                    acc.min_f64 =
                                        Some(acc.min_f64.map_or(*v as f64, |m| m.min(*v as f64)));
                                    acc.max_f64 =
                                        Some(acc.max_f64.map_or(*v as f64, |m| m.max(*v as f64)));
                                }
                                SqlValue::Bigint(v) => {
                                    acc.sum_i64 += v;
                                    acc.sum_f64 += *v as f64;
                                    acc.min_i64 = Some(acc.min_i64.map_or(*v, |m| m.min(*v)));
                                    acc.max_i64 = Some(acc.max_i64.map_or(*v, |m| m.max(*v)));
                                    acc.min_f64 =
                                        Some(acc.min_f64.map_or(*v as f64, |m| m.min(*v as f64)));
                                    acc.max_f64 =
                                        Some(acc.max_f64.map_or(*v as f64, |m| m.max(*v as f64)));
                                }
                                SqlValue::Numeric(v) => {
                                    acc.is_integer = false;
                                    acc.sum_f64 += v;
                                    acc.min_f64 = Some(acc.min_f64.map_or(*v, |m| m.min(*v)));
                                    acc.max_f64 = Some(acc.max_f64.map_or(*v, |m| m.max(*v)));
                                }
                                _ => {}
                            }
                        }
                    }
                }
                AggregateSource::Expression(expr) => {
                    // For expression aggregates like SUM(a * b), evaluate the expression
                    // This is a simplified evaluator for common binary operations
                    if let Some(value) = eval_simple_expression(row, expr) {
                        acc.count += 1;
                        acc.is_integer = false;
                        acc.sum_f64 += value;
                        acc.min_f64 = Some(acc.min_f64.map_or(value, |m| m.min(value)));
                        acc.max_f64 = Some(acc.max_f64.map_or(value, |m| m.max(value)));
                    }
                }
            }
        }
    }

    // Build result row from accumulators
    let values: Vec<SqlValue> = aggregates
        .iter()
        .zip(accumulators.iter())
        .map(|(spec, acc)| match spec.op {
            AggregateOp::Count => SqlValue::Integer(acc.count),
            AggregateOp::Sum => {
                // SQLite's SUM() preserves integer type for integer inputs
                if acc.count == 0 {
                    SqlValue::Null
                } else if acc.is_integer {
                    SqlValue::Integer(acc.sum_i64)
                } else {
                    SqlValue::Double(acc.sum_f64)
                }
            }
            AggregateOp::Avg => {
                if acc.count == 0 {
                    SqlValue::Null
                } else {
                    SqlValue::Double(acc.sum_f64 / acc.count as f64)
                }
            }
            AggregateOp::Min => {
                if acc.is_integer {
                    acc.min_i64.map(SqlValue::Integer).unwrap_or(SqlValue::Null)
                } else {
                    acc.min_f64.map(SqlValue::Double).unwrap_or(SqlValue::Null)
                }
            }
            AggregateOp::Max => {
                if acc.is_integer {
                    acc.max_i64.map(SqlValue::Integer).unwrap_or(SqlValue::Null)
                } else {
                    acc.max_f64.map(SqlValue::Double).unwrap_or(SqlValue::Null)
                }
            }
        })
        .collect();

    Ok(vec![Row::new(values)])
}

/// Evaluate a column predicate against a row
///
/// Delegates to the canonical comparator in `filter::evaluation`. Issue
/// #5335: this module previously carried a near-duplicate `compare_values`
/// whose incomparable-types catch-all returned `Ordering::Equal`, turning
/// equality/range predicates on temporal columns into tautologies or
/// contradictions. Routing through the filter module keeps a single source
/// of truth for predicate comparison semantics.
fn evaluate_predicate(row: &Row, predicate: &ColumnPredicate) -> bool {
    // Column-to-column comparison needs two values
    if let ColumnPredicate::ColumnCompare { left_column_idx, op, right_column_idx } = predicate {
        return filter::evaluate_column_compare(
            *op,
            row.get(*left_column_idx),
            row.get(*right_column_idx),
        );
    }

    let column_idx = match predicate {
        ColumnPredicate::LessThan { column_idx, .. }
        | ColumnPredicate::GreaterThan { column_idx, .. }
        | ColumnPredicate::GreaterThanOrEqual { column_idx, .. }
        | ColumnPredicate::LessThanOrEqual { column_idx, .. }
        | ColumnPredicate::Equal { column_idx, .. }
        | ColumnPredicate::NotEqual { column_idx, .. }
        | ColumnPredicate::Between { column_idx, .. }
        | ColumnPredicate::Like { column_idx, .. }
        | ColumnPredicate::InList { column_idx, .. } => *column_idx,
        ColumnPredicate::ColumnCompare { .. } => unreachable!(), // Handled above
    };

    match row.get(column_idx) {
        Some(value) => filter::evaluate_predicate(predicate, value),
        None => false,
    }
}

/// Evaluate a simple expression (for expression aggregates)
#[allow(clippy::only_used_in_recursion)]
fn eval_simple_expression(row: &Row, expr: &vibesql_ast::Expression) -> Option<f64> {
    use vibesql_ast::{BinaryOperator, Expression};

    match expr {
        Expression::BinaryOp { left, op, right } => {
            let left_val = eval_simple_expression(row, left)?;
            let right_val = eval_simple_expression(row, right)?;
            match op {
                BinaryOperator::Multiply => Some(left_val * right_val),
                BinaryOperator::Divide => Some(left_val / right_val),
                BinaryOperator::Plus => Some(left_val + right_val),
                BinaryOperator::Minus => Some(left_val - right_val),
                _ => None,
            }
        }
        Expression::ColumnRef(col_id) => {
            // Cannot resolve column names without a schema - return None to skip
            // the fast path and fall back to the columnar execution path which
            // properly handles expression aggregates with schema resolution.
            log::debug!(
                "fast_aggregate_on_rows: ColumnRef '{}' requires schema resolution, skipping fast path",
                col_id.column_canonical()
            );
            None
        }
        Expression::Literal(val) => match val {
            SqlValue::Integer(v) => Some(*v as f64),
            SqlValue::Double(v) => Some(*v),
            SqlValue::Float(v) => Some(*v as f64),
            SqlValue::Bigint(v) => Some(*v as f64),
            SqlValue::Numeric(v) => Some(*v),
            _ => None,
        },
        _ => None,
    }
}

/// Execute a query using columnar processing (AST-based interface)
///
/// This is the entry point for columnar execution that accepts AST expressions
/// and converts them to the columnar execution pipeline.
///
/// # Arguments
///
/// * `rows` - The rows to process
/// * `filter` - Optional WHERE clause expression
/// * `aggregates` - SELECT list aggregate expressions
/// * `schema` - Schema for resolving column names to indices
///
/// # Returns
///
/// Some(Result) if the query can be optimized using columnar execution,
/// None if the expressions are too complex for columnar optimization.
///
/// Note: This function uses LLVM auto-vectorization for vectorized execution.
pub fn execute_columnar(
    rows: &[Row],
    filter: Option<&vibesql_ast::Expression>,
    aggregates: &[vibesql_ast::Expression],
    schema: &CombinedSchema,
    case_sensitive_like: bool,
) -> Option<Result<Vec<Row>, ExecutorError>> {
    log::debug!("  Executing columnar query with {} rows", rows.len());

    // Extract column predicates from filter expression
    let predicates = if let Some(filter_expr) = filter {
        match extract_column_predicates(filter_expr, schema, case_sensitive_like) {
            Some(preds) => {
                log::debug!("    ✓ Extracted {} column predicates for SIMD filtering", preds.len());
                preds
            }
            None => {
                log::debug!("    ✗ Filter too complex for columnar optimization");
                return None; // Too complex for columnar optimization
            }
        }
    } else {
        log::debug!("    No filter predicates");
        vec![] // No filter
    };

    // Extract aggregates from SELECT list
    let agg_specs = match extract_aggregates(aggregates, schema) {
        Some(specs) => {
            log::debug!("    ✓ Extracted {} aggregate operations", specs.len());
            for (i, spec) in specs.iter().enumerate() {
                log::debug!("      Aggregate {}: {:?}", i + 1, spec.op);
            }
            specs
        }
        None => {
            log::debug!("    ✗ Aggregates too complex for columnar optimization");
            return None; // Too complex for columnar optimization
        }
    };

    // Call the simplified interface, passing schema if any aggregates use expressions
    let needs_schema = agg_specs
        .iter()
        .any(|spec| matches!(spec.source, aggregate::AggregateSource::Expression(_)));
    let schema_ref = if needs_schema { Some(schema) } else { None };

    log::debug!("    Executing SIMD-accelerated columnar aggregation");
    Some(execute_columnar_aggregate(rows, &predicates, &agg_specs, schema_ref))
}

#[cfg(test)]
mod tests {
    use vibesql_types::Date;

    use super::*;

    /// Test the full columnar pipeline: filter + aggregation
    #[test]
    fn test_columnar_pipeline_filtered_sum() {
        // Create test data: TPC-H Q6 style query
        // SELECT SUM(l_extendedprice * l_discount)
        // WHERE l_shipdate >= '1994-01-01'
        //   AND l_shipdate < '1995-01-01'
        //   AND l_discount BETWEEN 0.05 AND 0.07
        //   AND l_quantity < 24

        let rows = vec![
            Row::new(vec![
                SqlValue::Integer(10),   // quantity
                SqlValue::Double(100.0), // extendedprice
                SqlValue::Double(0.06),  // discount
                SqlValue::Date(Date::new(1994, 6, 1).unwrap()),
            ]),
            Row::new(vec![
                SqlValue::Integer(25), // quantity (filtered out: > 24)
                SqlValue::Double(200.0),
                SqlValue::Double(0.06),
                SqlValue::Date(Date::new(1994, 7, 1).unwrap()),
            ]),
            Row::new(vec![
                SqlValue::Integer(15), // quantity
                SqlValue::Double(150.0),
                SqlValue::Double(0.05), // discount
                SqlValue::Date(Date::new(1994, 8, 1).unwrap()),
            ]),
            Row::new(vec![
                SqlValue::Integer(20), // quantity
                SqlValue::Double(180.0),
                SqlValue::Double(0.08), // discount (filtered out: > 0.07)
                SqlValue::Date(Date::new(1994, 9, 1).unwrap()),
            ]),
        ];

        // Predicates: quantity < 24 AND discount BETWEEN 0.05 AND 0.07
        let predicates = vec![
            ColumnPredicate::LessThan { column_idx: 0, value: SqlValue::Integer(24) },
            ColumnPredicate::Between {
                column_idx: 2,
                low: SqlValue::Double(0.05),
                high: SqlValue::Double(0.07),
            },
        ];

        // Aggregates: SUM(extendedprice), COUNT(*)
        let aggregates = vec![
            AggregateSpec { op: AggregateOp::Sum, source: AggregateSource::Column(1) }, /* SUM(extendedprice) */
            AggregateSpec { op: AggregateOp::Count, source: AggregateSource::Column(0) }, /* COUNT(*) */
        ];

        let result = execute_columnar_aggregate(&rows, &predicates, &aggregates, None).unwrap();

        assert_eq!(result.len(), 1);
        let result_row = &result[0];

        // Only rows 0 and 2 pass the filter (quantity < 24 AND discount in range)
        // SUM(extendedprice) = 100.0 + 150.0 = 250.0
        assert!(
            matches!(result_row.get(0), Some(&SqlValue::Double(sum)) if (sum - 250.0).abs() < 0.001)
        );
        // COUNT(*) = 2
        assert_eq!(result_row.get(1), Some(&SqlValue::Integer(2)));
    }

    /// Test columnar execution with no filtering
    #[test]
    fn test_columnar_pipeline_no_filter() {
        let rows = vec![
            Row::new(vec![SqlValue::Integer(10), SqlValue::Double(1.5)]),
            Row::new(vec![SqlValue::Integer(20), SqlValue::Double(2.5)]),
            Row::new(vec![SqlValue::Integer(30), SqlValue::Double(3.5)]),
        ];

        let predicates = vec![];
        let aggregates = vec![
            AggregateSpec { op: AggregateOp::Sum, source: AggregateSource::Column(0) },
            AggregateSpec { op: AggregateOp::Avg, source: AggregateSource::Column(1) },
            AggregateSpec { op: AggregateOp::Max, source: AggregateSource::Column(0) },
        ];

        let result = execute_columnar_aggregate(&rows, &predicates, &aggregates, None).unwrap();

        assert_eq!(result.len(), 1);
        let result_row = &result[0];

        // SUM(col0) = 60 (SQLite's SUM() returns REAL)
        assert_eq!(result_row.get(0), Some(&SqlValue::Integer(60)));
        // AVG(col1) = 2.5
        assert!(
            matches!(result_row.get(1), Some(&SqlValue::Double(avg)) if (avg - 2.5).abs() < 0.001)
        );
        // MAX(col0) = 30
        assert_eq!(result_row.get(2), Some(&SqlValue::Integer(30)));
    }

    /// Test columnar execution with empty result set
    #[test]
    fn test_columnar_pipeline_empty_result() {
        let rows =
            vec![Row::new(vec![SqlValue::Integer(100)]), Row::new(vec![SqlValue::Integer(200)])];

        // Filter that matches nothing
        let predicates =
            vec![ColumnPredicate::LessThan { column_idx: 0, value: SqlValue::Integer(50) }];

        let aggregates = vec![
            AggregateSpec { op: AggregateOp::Sum, source: AggregateSource::Column(0) },
            AggregateSpec { op: AggregateOp::Count, source: AggregateSource::Column(0) },
        ];

        let result = execute_columnar_aggregate(&rows, &predicates, &aggregates, None).unwrap();

        assert_eq!(result.len(), 1);
        let result_row = &result[0];

        // SUM of empty set = NULL
        assert_eq!(result_row.get(0), Some(&SqlValue::Null));
        // COUNT of empty set = 0
        assert_eq!(result_row.get(1), Some(&SqlValue::Integer(0)));
    }

    // AST Integration Tests

    use vibesql_ast::{BinaryOperator, Expression};
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_types::DataType;

    use crate::schema::CombinedSchema;

    fn make_test_schema() -> CombinedSchema {
        let schema = TableSchema::new(
            "test".to_string(),
            vec![
                ColumnSchema::new("quantity".to_string(), DataType::Integer, false),
                ColumnSchema::new("price".to_string(), DataType::DoublePrecision, false),
            ],
        );
        CombinedSchema::from_table("test".to_string(), schema)
    }

    fn make_test_rows_for_ast() -> Vec<Row> {
        vec![
            Row::new(vec![SqlValue::Integer(10), SqlValue::Double(1.5)]),
            Row::new(vec![SqlValue::Integer(20), SqlValue::Double(2.5)]),
            Row::new(vec![SqlValue::Integer(30), SqlValue::Double(3.5)]),
            Row::new(vec![SqlValue::Integer(40), SqlValue::Double(4.5)]),
        ]
    }

    #[test]
    fn test_execute_columnar_simple_aggregate() {
        let rows = make_test_rows_for_ast();
        let schema = make_test_schema();

        // SELECT SUM(price) FROM test
        let aggregates = vec![Expression::AggregateFunction {
            name: vibesql_ast::FunctionIdentifier::new("SUM"),
            distinct: false,
            args: vec![Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(
                "price", false,
            ))],
            order_by: None,
            filter: None,
        }];

        let result = execute_columnar(&rows, None, &aggregates, &schema, false);
        assert!(result.is_some());

        let rows_result = result.unwrap();
        assert!(rows_result.is_ok());

        let result_rows = rows_result.unwrap();
        assert_eq!(result_rows.len(), 1);
        assert_eq!(result_rows[0].len(), 1);

        // Sum should be 1.5 + 2.5 + 3.5 + 4.5 = 12.0
        if let Some(SqlValue::Double(sum)) = result_rows[0].get(0) {
            assert!((sum - 12.0).abs() < 0.001);
        } else {
            panic!("Expected Numeric value for SUM");
        }
    }

    #[test]
    fn test_execute_columnar_with_filter() {
        let rows = make_test_rows_for_ast();
        let schema = make_test_schema();

        // SELECT SUM(price) FROM test WHERE quantity < 25
        let filter = Expression::BinaryOp {
            left: Box::new(Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(
                "quantity", false,
            ))),
            op: BinaryOperator::LessThan,
            right: Box::new(Expression::Literal(SqlValue::Integer(25))),
        };

        let aggregates = vec![Expression::AggregateFunction {
            name: vibesql_ast::FunctionIdentifier::new("SUM"),
            distinct: false,
            args: vec![Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(
                "price", false,
            ))],
            order_by: None,
            filter: None,
        }];

        let result = execute_columnar(&rows, Some(&filter), &aggregates, &schema, false);
        assert!(result.is_some());

        let rows_result = result.unwrap();
        assert!(rows_result.is_ok());

        let result_rows = rows_result.unwrap();
        assert_eq!(result_rows.len(), 1);
        assert_eq!(result_rows[0].len(), 1);

        // Sum of rows where quantity < 25: 1.5 + 2.5 = 4.0
        if let Some(SqlValue::Double(sum)) = result_rows[0].get(0) {
            assert!((sum - 4.0).abs() < 0.001);
        } else {
            panic!("Expected Numeric value for SUM");
        }
    }

    #[test]
    fn test_execute_columnar_multiple_aggregates() {
        let rows = make_test_rows_for_ast();
        let schema = make_test_schema();

        // SELECT SUM(price), COUNT(*), AVG(quantity) FROM test
        let aggregates = vec![
            Expression::AggregateFunction {
                name: vibesql_ast::FunctionIdentifier::new("SUM"),
                distinct: false,
                args: vec![Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(
                    "price", false,
                ))],
                order_by: None,
                filter: None,
            },
            Expression::AggregateFunction {
                name: vibesql_ast::FunctionIdentifier::new("COUNT"),
                distinct: false,
                args: vec![Expression::Wildcard],
                order_by: None,
                filter: None,
            },
            Expression::AggregateFunction {
                name: vibesql_ast::FunctionIdentifier::new("AVG"),
                distinct: false,
                args: vec![Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(
                    "quantity", false,
                ))],
                order_by: None,
                filter: None,
            },
        ];

        let result = execute_columnar(&rows, None, &aggregates, &schema, false);
        assert!(result.is_some());

        let rows_result = result.unwrap();
        assert!(rows_result.is_ok());

        let result_rows = rows_result.unwrap();
        assert_eq!(result_rows.len(), 1);
        assert_eq!(result_rows[0].len(), 3);

        // Check SUM(price) = 12.0
        if let Some(SqlValue::Double(sum)) = result_rows[0].get(0) {
            assert!((sum - 12.0).abs() < 0.001);
        } else {
            panic!("Expected Numeric value for SUM");
        }

        // Check COUNT(*) = 4
        assert_eq!(result_rows[0].get(1), Some(&SqlValue::Integer(4)));

        // Check AVG(quantity) = (10+20+30+40)/4 = 25.0
        if let Some(SqlValue::Double(avg)) = result_rows[0].get(2) {
            assert!((avg - 25.0).abs() < 0.001);
        } else {
            panic!("Expected Double value for AVG");
        }
    }

    #[test]
    fn test_execute_columnar_unsupported_distinct() {
        let rows = make_test_rows_for_ast();
        let schema = make_test_schema();

        // SELECT SUM(DISTINCT price) FROM test - should return None
        let aggregates = vec![Expression::AggregateFunction {
            name: vibesql_ast::FunctionIdentifier::new("SUM"),
            distinct: true,
            args: vec![Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(
                "price", false,
            ))],
            order_by: None,
            filter: None,
        }];

        let result = execute_columnar(&rows, None, &aggregates, &schema, false);
        assert!(result.is_none());
    }

    #[test]
    fn test_execute_columnar_unsupported_complex_filter() {
        let rows = make_test_rows_for_ast();
        let schema = make_test_schema();

        // SELECT SUM(price) FROM test WHERE quantity IN (SELECT ...) - unsupported
        let filter = Expression::ScalarSubquery(Box::new(vibesql_ast::SelectStmt {
            with_clause: None,
            distinct: false,
            select_list: vec![],
            into_table: None,
            into_variables: None,
            from: None,
            where_clause: None,
            group_by: None,
            having: None,
            window_definitions: None,
            order_by: None,
            limit: None,
            offset: None,
            set_operation: None,
            values: None,
        }));

        let aggregates = vec![Expression::AggregateFunction {
            name: vibesql_ast::FunctionIdentifier::new("SUM"),
            distinct: false,
            args: vec![Expression::ColumnRef(vibesql_ast::ColumnIdentifier::simple(
                "price", false,
            ))],
            order_by: None,
            filter: None,
        }];

        let result = execute_columnar(&rows, Some(&filter), &aggregates, &schema, false);
        assert!(result.is_none());
    }
}