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
//! Specialized GROUP BY key types for efficient hashing
//!
//! This module provides specialized key types that avoid the overhead of
//! `Vec<SqlValue>` for common GROUP BY patterns:
//!
//! - Single primitive keys (i64, String)
//! - Two-column keys ((char, char), (i64, i64), etc.)
//! - Fallback to Vec<SqlValue> for complex cases
//!
//! Key benefits:
//! - No heap allocation for primitive keys
//! - Direct hashing without enum matching overhead
//! - Cache-friendly memory layout

use std::hash::{Hash, Hasher};

#[cfg(test)]
use vibesql_storage::Row;
use vibesql_types::{DataType, SqlValue};

use crate::select::columnar::batch::{ColumnArray, ColumnarBatch};

/// Specialized GROUP BY key types for efficient hashing
///
/// The key insight is that most GROUP BY queries use a small number of
/// primitive columns. By specializing for these cases, we can:
/// 1. Avoid Vec allocation per row
/// 2. Hash primitives directly without enum matching
/// 3. Use more cache-efficient memory layouts
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GroupKey {
    /// Single i64 key (common for integer GROUP BY)
    SingleI64(i64),

    /// Single String key (common for VARCHAR GROUP BY)
    SingleString(String),

    /// Two single-byte characters (TPC-H Q1: l_returnflag, l_linestatus)
    /// Packed into u16 for efficient hashing
    TwoChars(u8, u8),

    /// Two i64 keys
    TwoI64(i64, i64),

    /// (i64, String) - common mixed pattern
    I64String(i64, String),

    /// (i64, i64, i64) - TPC-H Q3 pattern (l_orderkey, o_orderdate, o_shippriority)
    ThreeI64(i64, i64, i64),

    /// Fallback for complex keys
    Generic(Vec<SqlValue>),
}

impl Hash for GroupKey {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        // Use discriminant for type distinction
        std::mem::discriminant(self).hash(state);

        match self {
            GroupKey::SingleI64(v) => v.hash(state),
            GroupKey::SingleString(v) => v.hash(state),
            GroupKey::TwoChars(a, b) => {
                // Pack into u16 and hash once
                let packed = (*a as u16) | ((*b as u16) << 8);
                packed.hash(state);
            }
            GroupKey::TwoI64(a, b) => {
                a.hash(state);
                b.hash(state);
            }
            GroupKey::I64String(i, s) => {
                i.hash(state);
                s.hash(state);
            }
            GroupKey::ThreeI64(a, b, c) => {
                a.hash(state);
                b.hash(state);
                c.hash(state);
            }
            GroupKey::Generic(v) => v.hash(state),
        }
    }
}

/// Specification for a GROUP BY key extraction strategy
#[derive(Debug, Clone)]
pub enum GroupKeySpec {
    /// Single i64 column
    SingleI64 { col_idx: usize },

    /// Single String column
    SingleString { col_idx: usize },

    /// Two single-character columns (e.g., l_returnflag, l_linestatus)
    TwoChars { col1_idx: usize, col2_idx: usize },

    /// Two i64 columns
    TwoI64 { col1_idx: usize, col2_idx: usize },

    /// (i64, String) pattern
    I64String { i64_col: usize, string_col: usize },

    /// Three integer columns (i64, i64, i64) - for TPC-H Q3 pattern
    ThreeI64 { col1_idx: usize, col2_idx: usize, col3_idx: usize },

    /// Generic fallback
    Generic { col_indices: Vec<(usize, DataType)> },
}

impl GroupKeySpec {
    /// Analyze GROUP BY columns and determine the best key strategy
    #[cfg(test)]
    pub fn from_columns(columns: &[(usize, DataType)]) -> Self {
        match columns.len() {
            1 => {
                let (idx, dtype) = &columns[0];
                match dtype {
                    DataType::Integer | DataType::Bigint => {
                        GroupKeySpec::SingleI64 { col_idx: *idx }
                    }
                    DataType::Varchar { .. } | DataType::Character { .. } => {
                        GroupKeySpec::SingleString { col_idx: *idx }
                    }
                    _ => GroupKeySpec::Generic { col_indices: columns.to_vec() },
                }
            }
            2 => {
                let (idx1, dtype1) = &columns[0];
                let (idx2, dtype2) = &columns[1];

                // Check for two single-char columns (TPC-H Q1 pattern)
                if matches!(
                    (dtype1, dtype2),
                    (
                        DataType::Varchar { max_length: Some(1) }
                            | DataType::Character { length: 1 },
                        DataType::Varchar { max_length: Some(1) }
                            | DataType::Character { length: 1 }
                    )
                ) {
                    return GroupKeySpec::TwoChars { col1_idx: *idx1, col2_idx: *idx2 };
                }

                // Check for two VARCHAR columns that might be single chars
                // (TPC-H lineitem uses VARCHAR without max_length but values are single chars)
                if matches!(
                    (dtype1, dtype2),
                    (
                        DataType::Varchar { .. } | DataType::Character { .. },
                        DataType::Varchar { .. } | DataType::Character { .. }
                    )
                ) {
                    return GroupKeySpec::TwoChars { col1_idx: *idx1, col2_idx: *idx2 };
                }

                // Check for two i64 columns
                if matches!(dtype1, DataType::Integer | DataType::Bigint)
                    && matches!(dtype2, DataType::Integer | DataType::Bigint)
                {
                    return GroupKeySpec::TwoI64 { col1_idx: *idx1, col2_idx: *idx2 };
                }

                // Check for (i64, String) pattern
                if matches!(dtype1, DataType::Integer | DataType::Bigint)
                    && matches!(dtype2, DataType::Varchar { .. } | DataType::Character { .. })
                {
                    return GroupKeySpec::I64String { i64_col: *idx1, string_col: *idx2 };
                }

                GroupKeySpec::Generic { col_indices: columns.to_vec() }
            }
            3 => {
                let (idx1, dtype1) = &columns[0];
                let (idx2, dtype2) = &columns[1];
                let (idx3, dtype3) = &columns[2];

                // Check for (i64, i64/Date, i64) - TPC-H Q3 pattern
                // Note: Date is stored as days-since-epoch and extracted as i64
                if matches!(dtype1, DataType::Integer | DataType::Bigint)
                    && matches!(dtype2, DataType::Integer | DataType::Bigint | DataType::Date)
                    && matches!(dtype3, DataType::Integer | DataType::Bigint)
                {
                    return GroupKeySpec::ThreeI64 {
                        col1_idx: *idx1,
                        col2_idx: *idx2,
                        col3_idx: *idx3,
                    };
                }

                GroupKeySpec::Generic { col_indices: columns.to_vec() }
            }
            _ => GroupKeySpec::Generic { col_indices: columns.to_vec() },
        }
    }

    /// Extract a group key from a row
    ///
    /// # Safety
    ///
    /// Uses unchecked accessors for performance. Caller must ensure column
    /// indices are valid.
    #[cfg(test)]
    #[allow(dead_code)]
    #[inline]
    pub unsafe fn extract_key(&self, row: &Row) -> GroupKey {
        match self {
            GroupKeySpec::SingleI64 { col_idx } => {
                GroupKey::SingleI64(row.get_i64_unchecked(*col_idx))
            }
            GroupKeySpec::SingleString { col_idx } => {
                GroupKey::SingleString(row.get_string_unchecked(*col_idx).to_string())
            }
            GroupKeySpec::TwoChars { col1_idx, col2_idx } => {
                // Get first char of each string, or 0 if empty
                let s1 = row.get_string_unchecked(*col1_idx);
                let s2 = row.get_string_unchecked(*col2_idx);
                let c1 = s1.as_bytes().first().copied().unwrap_or(0);
                let c2 = s2.as_bytes().first().copied().unwrap_or(0);
                GroupKey::TwoChars(c1, c2)
            }
            GroupKeySpec::TwoI64 { col1_idx, col2_idx } => {
                GroupKey::TwoI64(row.get_i64_unchecked(*col1_idx), row.get_i64_unchecked(*col2_idx))
            }
            GroupKeySpec::I64String { i64_col, string_col } => GroupKey::I64String(
                row.get_i64_unchecked(*i64_col),
                row.get_string_unchecked(*string_col).to_string(),
            ),
            GroupKeySpec::ThreeI64 { col1_idx, col2_idx, col3_idx } => GroupKey::ThreeI64(
                row.get_i64_unchecked(*col1_idx),
                row.get_i64_unchecked(*col2_idx),
                row.get_i64_unchecked(*col3_idx),
            ),
            GroupKeySpec::Generic { col_indices } => {
                let mut key = Vec::with_capacity(col_indices.len());
                for (idx, dtype) in col_indices {
                    let value = match dtype {
                        DataType::Integer | DataType::Bigint => {
                            SqlValue::Integer(row.get_i64_unchecked(*idx))
                        }
                        DataType::Varchar { .. } | DataType::Character { .. } => {
                            SqlValue::Varchar(arcstr::ArcStr::from(row.get_string_unchecked(*idx)))
                        }
                        DataType::DoublePrecision | DataType::Real | DataType::Decimal { .. } => {
                            SqlValue::Double(row.get_f64_unchecked(*idx))
                        }
                        DataType::Date => SqlValue::Date(row.get_date_unchecked(*idx)),
                        DataType::Boolean => SqlValue::Boolean(row.get_bool_unchecked(*idx)),
                        _ => row.get(*idx).cloned().unwrap_or(SqlValue::Null),
                    };
                    key.push(value);
                }
                GroupKey::Generic(key)
            }
        }
    }

    /// Convert a GroupKey back to Vec<SqlValue> for result output
    pub fn key_to_values(&self, key: &GroupKey) -> Vec<SqlValue> {
        match key {
            GroupKey::SingleI64(v) => vec![SqlValue::Integer(*v)],
            GroupKey::SingleString(v) => vec![SqlValue::Varchar(arcstr::ArcStr::from(v.clone()))],
            GroupKey::TwoChars(a, b) => vec![
                SqlValue::Varchar(arcstr::ArcStr::from(
                    String::from_utf8_lossy(&[*a]).into_owned(),
                )),
                SqlValue::Varchar(arcstr::ArcStr::from(
                    String::from_utf8_lossy(&[*b]).into_owned(),
                )),
            ],
            GroupKey::TwoI64(a, b) => vec![SqlValue::Integer(*a), SqlValue::Integer(*b)],
            GroupKey::I64String(i, s) => {
                vec![SqlValue::Integer(*i), SqlValue::Varchar(arcstr::ArcStr::from(s.clone()))]
            }
            GroupKey::ThreeI64(a, b, c) => {
                vec![SqlValue::Integer(*a), SqlValue::Integer(*b), SqlValue::Integer(*c)]
            }
            GroupKey::Generic(v) => v.clone(),
        }
    }

    /// Analyze ColumnarBatch columns and determine the best key strategy
    ///
    /// This method examines the column types in a ColumnarBatch to select
    /// the most efficient key representation for GROUP BY operations.
    pub fn from_columnar_batch(batch: &ColumnarBatch, group_cols: &[usize]) -> Self {
        match group_cols.len() {
            1 => {
                let col_idx = group_cols[0];
                if let Some(column) = batch.column(col_idx) {
                    match column {
                        ColumnArray::Int64(_, _) | ColumnArray::Int32(_, _) => {
                            return GroupKeySpec::SingleI64 { col_idx };
                        }
                        ColumnArray::String(_, _) | ColumnArray::FixedString(_, _) => {
                            return GroupKeySpec::SingleString { col_idx };
                        }
                        _ => {}
                    }
                }
                GroupKeySpec::Generic {
                    col_indices: group_cols
                        .iter()
                        .map(|&i| (i, DataType::Varchar { max_length: None }))
                        .collect(),
                }
            }
            2 => {
                let col1_idx = group_cols[0];
                let col2_idx = group_cols[1];
                if let (Some(col1), Some(col2)) = (batch.column(col1_idx), batch.column(col2_idx)) {
                    // Check for two string columns (TPC-H Q1 pattern: l_returnflag, l_linestatus)
                    if matches!(col1, ColumnArray::String(_, _) | ColumnArray::FixedString(_, _))
                        && matches!(
                            col2,
                            ColumnArray::String(_, _) | ColumnArray::FixedString(_, _)
                        )
                    {
                        return GroupKeySpec::TwoChars { col1_idx, col2_idx };
                    }
                    // Check for two integer columns
                    if matches!(col1, ColumnArray::Int64(_, _) | ColumnArray::Int32(_, _))
                        && matches!(col2, ColumnArray::Int64(_, _) | ColumnArray::Int32(_, _))
                    {
                        return GroupKeySpec::TwoI64 { col1_idx, col2_idx };
                    }
                    // Check for (int, string) pattern
                    if matches!(col1, ColumnArray::Int64(_, _) | ColumnArray::Int32(_, _))
                        && matches!(
                            col2,
                            ColumnArray::String(_, _) | ColumnArray::FixedString(_, _)
                        )
                    {
                        return GroupKeySpec::I64String { i64_col: col1_idx, string_col: col2_idx };
                    }
                }
                GroupKeySpec::Generic {
                    col_indices: group_cols
                        .iter()
                        .map(|&i| (i, DataType::Varchar { max_length: None }))
                        .collect(),
                }
            }
            3 => {
                let col1_idx = group_cols[0];
                let col2_idx = group_cols[1];
                let col3_idx = group_cols[2];
                if let (Some(col1), Some(col2), Some(col3)) =
                    (batch.column(col1_idx), batch.column(col2_idx), batch.column(col3_idx))
                {
                    // Check for three integer columns (TPC-H Q3 pattern)
                    if matches!(
                        col1,
                        ColumnArray::Int64(_, _)
                            | ColumnArray::Int32(_, _)
                            | ColumnArray::Date(_, _)
                    ) && matches!(
                        col2,
                        ColumnArray::Int64(_, _)
                            | ColumnArray::Int32(_, _)
                            | ColumnArray::Date(_, _)
                    ) && matches!(
                        col3,
                        ColumnArray::Int64(_, _)
                            | ColumnArray::Int32(_, _)
                            | ColumnArray::Date(_, _)
                    ) {
                        return GroupKeySpec::ThreeI64 { col1_idx, col2_idx, col3_idx };
                    }
                }
                GroupKeySpec::Generic {
                    col_indices: group_cols
                        .iter()
                        .map(|&i| (i, DataType::Varchar { max_length: None }))
                        .collect(),
                }
            }
            _ => GroupKeySpec::Generic {
                col_indices: group_cols
                    .iter()
                    .map(|&i| (i, DataType::Varchar { max_length: None }))
                    .collect(),
            },
        }
    }

    /// Extract a group key from a ColumnarBatch at the specified row index
    ///
    /// This provides direct columnar extraction without going through Row.
    #[inline]
    pub fn extract_key_from_batch(&self, batch: &ColumnarBatch, row_idx: usize) -> GroupKey {
        match self {
            GroupKeySpec::SingleI64 { col_idx } => {
                if let Some(ColumnArray::Int64(values, nulls)) = batch.column(*col_idx) {
                    if let Some(null_mask) = nulls {
                        if null_mask.get(row_idx).copied().unwrap_or(false) {
                            return GroupKey::Generic(vec![SqlValue::Null]);
                        }
                    }
                    return GroupKey::SingleI64(values[row_idx]);
                }
                if let Some(ColumnArray::Int32(values, nulls)) = batch.column(*col_idx) {
                    if let Some(null_mask) = nulls {
                        if null_mask.get(row_idx).copied().unwrap_or(false) {
                            return GroupKey::Generic(vec![SqlValue::Null]);
                        }
                    }
                    return GroupKey::SingleI64(values[row_idx] as i64);
                }
                // Fallback
                batch
                    .get_value(row_idx, *col_idx)
                    .map(|v| GroupKey::Generic(vec![v]))
                    .unwrap_or(GroupKey::Generic(vec![SqlValue::Null]))
            }
            GroupKeySpec::SingleString { col_idx } => {
                if let Some(ColumnArray::String(values, nulls)) = batch.column(*col_idx) {
                    if let Some(null_mask) = nulls {
                        if null_mask.get(row_idx).copied().unwrap_or(false) {
                            return GroupKey::Generic(vec![SqlValue::Null]);
                        }
                    }
                    return GroupKey::SingleString(values[row_idx].to_string());
                }
                if let Some(ColumnArray::FixedString(values, nulls)) = batch.column(*col_idx) {
                    if let Some(null_mask) = nulls {
                        if null_mask.get(row_idx).copied().unwrap_or(false) {
                            return GroupKey::Generic(vec![SqlValue::Null]);
                        }
                    }
                    return GroupKey::SingleString(values[row_idx].to_string());
                }
                // Fallback
                batch
                    .get_value(row_idx, *col_idx)
                    .map(|v| GroupKey::Generic(vec![v]))
                    .unwrap_or(GroupKey::Generic(vec![SqlValue::Null]))
            }
            GroupKeySpec::TwoChars { col1_idx, col2_idx } => {
                // Fast path: extract first char from each string column
                let c1 = Self::extract_first_char(batch, *col1_idx, row_idx);
                let c2 = Self::extract_first_char(batch, *col2_idx, row_idx);
                GroupKey::TwoChars(c1, c2)
            }
            GroupKeySpec::TwoI64 { col1_idx, col2_idx } => {
                let v1 = Self::extract_i64(batch, *col1_idx, row_idx);
                let v2 = Self::extract_i64(batch, *col2_idx, row_idx);
                GroupKey::TwoI64(v1, v2)
            }
            GroupKeySpec::I64String { i64_col, string_col } => {
                let i = Self::extract_i64(batch, *i64_col, row_idx);
                let s = Self::extract_string(batch, *string_col, row_idx);
                GroupKey::I64String(i, s)
            }
            GroupKeySpec::ThreeI64 { col1_idx, col2_idx, col3_idx } => {
                let v1 = Self::extract_i64(batch, *col1_idx, row_idx);
                let v2 = Self::extract_i64(batch, *col2_idx, row_idx);
                let v3 = Self::extract_i64(batch, *col3_idx, row_idx);
                GroupKey::ThreeI64(v1, v2, v3)
            }
            GroupKeySpec::Generic { col_indices } => {
                let mut key = Vec::with_capacity(col_indices.len());
                for (idx, _) in col_indices {
                    let value = batch.get_value(row_idx, *idx).unwrap_or(SqlValue::Null);
                    key.push(value);
                }
                GroupKey::Generic(key)
            }
        }
    }

    /// Extract first character from a string column (for TwoChars key)
    #[inline]
    fn extract_first_char(batch: &ColumnarBatch, col_idx: usize, row_idx: usize) -> u8 {
        if let Some(ColumnArray::String(values, _)) = batch.column(col_idx) {
            return values[row_idx].as_bytes().first().copied().unwrap_or(0);
        }
        if let Some(ColumnArray::FixedString(values, _)) = batch.column(col_idx) {
            return values[row_idx].as_bytes().first().copied().unwrap_or(0);
        }
        0
    }

    /// Extract i64 value from an integer column
    #[inline]
    fn extract_i64(batch: &ColumnarBatch, col_idx: usize, row_idx: usize) -> i64 {
        if let Some(ColumnArray::Int64(values, _)) = batch.column(col_idx) {
            return values[row_idx];
        }
        if let Some(ColumnArray::Int32(values, _)) = batch.column(col_idx) {
            return values[row_idx] as i64;
        }
        if let Some(ColumnArray::Date(values, _)) = batch.column(col_idx) {
            return values[row_idx] as i64;
        }
        0
    }

    /// Extract String value from a string column
    #[inline]
    fn extract_string(batch: &ColumnarBatch, col_idx: usize, row_idx: usize) -> String {
        if let Some(ColumnArray::String(values, _)) = batch.column(col_idx) {
            return values[row_idx].to_string();
        }
        if let Some(ColumnArray::FixedString(values, _)) = batch.column(col_idx) {
            return values[row_idx].to_string();
        }
        String::new()
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::*;

    #[test]
    fn test_single_i64_key() {
        let spec = GroupKeySpec::from_columns(&[(0, DataType::Integer)]);
        assert!(matches!(spec, GroupKeySpec::SingleI64 { .. }));
    }

    #[test]
    fn test_two_chars_key() {
        let spec = GroupKeySpec::from_columns(&[
            (0, DataType::Varchar { max_length: Some(1) }),
            (1, DataType::Varchar { max_length: Some(1) }),
        ]);
        assert!(matches!(spec, GroupKeySpec::TwoChars { .. }));
    }

    #[test]
    fn test_two_varchar_detected_as_two_chars() {
        // TPC-H lineitem has VARCHAR without max_length but values are single chars
        let spec = GroupKeySpec::from_columns(&[
            (0, DataType::Varchar { max_length: None }),
            (1, DataType::Varchar { max_length: None }),
        ]);
        assert!(matches!(spec, GroupKeySpec::TwoChars { .. }));
    }

    #[test]
    fn test_group_key_hashing() {
        let mut map: HashMap<GroupKey, i32> = HashMap::new();

        // Test TwoChars hashing (TPC-H Q1 pattern)
        map.insert(GroupKey::TwoChars(b'A', b'F'), 1);
        map.insert(GroupKey::TwoChars(b'N', b'O'), 2);
        map.insert(GroupKey::TwoChars(b'R', b'F'), 3);

        assert_eq!(map.get(&GroupKey::TwoChars(b'A', b'F')), Some(&1));
        assert_eq!(map.get(&GroupKey::TwoChars(b'N', b'O')), Some(&2));
        assert_eq!(map.get(&GroupKey::TwoChars(b'R', b'F')), Some(&3));
    }

    #[test]
    fn test_key_to_values() {
        let spec = GroupKeySpec::TwoChars { col1_idx: 0, col2_idx: 1 };

        let key = GroupKey::TwoChars(b'A', b'F');
        let values = spec.key_to_values(&key);

        assert_eq!(values.len(), 2);
        assert_eq!(values[0], SqlValue::Varchar(arcstr::ArcStr::from("A")));
        assert_eq!(values[1], SqlValue::Varchar(arcstr::ArcStr::from("F")));
    }

    #[test]
    fn test_columnar_batch_key_extraction() {
        use vibesql_storage::Row;

        // Create a batch with string columns (like TPC-H Q1: l_returnflag, l_linestatus)
        let rows = vec![
            Row::new(vec![
                SqlValue::Varchar(arcstr::ArcStr::from("A")),
                SqlValue::Varchar(arcstr::ArcStr::from("F")),
            ]),
            Row::new(vec![
                SqlValue::Varchar(arcstr::ArcStr::from("N")),
                SqlValue::Varchar(arcstr::ArcStr::from("O")),
            ]),
            Row::new(vec![
                SqlValue::Varchar(arcstr::ArcStr::from("R")),
                SqlValue::Varchar(arcstr::ArcStr::from("F")),
            ]),
        ];
        let batch = ColumnarBatch::from_rows(&rows).unwrap();

        // Should detect TwoChars pattern for two string columns
        let spec = GroupKeySpec::from_columnar_batch(&batch, &[0, 1]);
        assert!(matches!(spec, GroupKeySpec::TwoChars { col1_idx: 0, col2_idx: 1 }));

        // Extract keys and verify
        let key0 = spec.extract_key_from_batch(&batch, 0);
        let key1 = spec.extract_key_from_batch(&batch, 1);
        let key2 = spec.extract_key_from_batch(&batch, 2);

        assert_eq!(key0, GroupKey::TwoChars(b'A', b'F'));
        assert_eq!(key1, GroupKey::TwoChars(b'N', b'O'));
        assert_eq!(key2, GroupKey::TwoChars(b'R', b'F'));
    }

    #[test]
    fn test_columnar_batch_single_i64_extraction() {
        use vibesql_storage::Row;

        // Create a batch with integer column
        let rows = vec![
            Row::new(vec![SqlValue::Integer(100)]),
            Row::new(vec![SqlValue::Integer(200)]),
            Row::new(vec![SqlValue::Integer(300)]),
        ];
        let batch = ColumnarBatch::from_rows(&rows).unwrap();

        // Should detect SingleI64 pattern
        let spec = GroupKeySpec::from_columnar_batch(&batch, &[0]);
        assert!(matches!(spec, GroupKeySpec::SingleI64 { col_idx: 0 }));

        // Extract keys and verify
        let key0 = spec.extract_key_from_batch(&batch, 0);
        let key1 = spec.extract_key_from_batch(&batch, 1);
        let key2 = spec.extract_key_from_batch(&batch, 2);

        assert_eq!(key0, GroupKey::SingleI64(100));
        assert_eq!(key1, GroupKey::SingleI64(200));
        assert_eq!(key2, GroupKey::SingleI64(300));
    }

    #[test]
    fn test_columnar_batch_two_i64_extraction() {
        use vibesql_storage::Row;

        // Create a batch with two integer columns
        let rows = vec![
            Row::new(vec![SqlValue::Integer(1), SqlValue::Integer(10)]),
            Row::new(vec![SqlValue::Integer(2), SqlValue::Integer(20)]),
        ];
        let batch = ColumnarBatch::from_rows(&rows).unwrap();

        // Should detect TwoI64 pattern
        let spec = GroupKeySpec::from_columnar_batch(&batch, &[0, 1]);
        assert!(matches!(spec, GroupKeySpec::TwoI64 { col1_idx: 0, col2_idx: 1 }));

        // Extract keys and verify
        let key0 = spec.extract_key_from_batch(&batch, 0);
        let key1 = spec.extract_key_from_batch(&batch, 1);

        assert_eq!(key0, GroupKey::TwoI64(1, 10));
        assert_eq!(key1, GroupKey::TwoI64(2, 20));
    }
}