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
//! Storage layer conversion
//!
//! This module contains methods for converting between the storage layer's
//! ColumnarTable and the executor's ColumnarBatch.
use std::sync::Arc;
use vibesql_types::SqlValue;
use super::types::{ColumnArray, ColumnarBatch};
use crate::errors::ExecutorError;
impl ColumnarBatch {
/// Convert from storage layer ColumnarTable to executor ColumnarBatch
///
/// This method provides **true zero-copy** conversion from the storage layer's
/// columnar format to the executor's columnar format. This is the key integration
/// point for native columnar table scans.
///
/// # Performance
///
/// - **O(1) for numeric/string columns**: Arc::clone is just a reference count bump
/// - **< 1 microsecond** for millions of rows (vs O(n) with data copy)
/// - Directly shares storage ColumnData with executor ColumnArray
/// - Critical path for TPC-H Q6 and other analytical queries
///
/// # Zero-Copy Design
///
/// Both `vibesql_storage::ColumnData` and executor `ColumnArray` use `Arc<Vec<T>>`
/// for column data. Calling `Arc::clone()` only increments a reference count,
/// avoiding any data copying:
///
/// ```text
/// Storage: Arc<Vec<i64>> ─┬─> [1, 2, 3, 4, ...] (shared memory)
/// │
/// Executor: Arc<Vec<i64>> ┘
/// ```
///
/// # Arguments
///
/// * `storage_columnar` - ColumnarTable from storage layer (vibesql-storage)
///
/// # Returns
///
/// * `Ok(ColumnarBatch)` - Executor-ready columnar batch with shared Arc references
/// * `Err(ExecutorError)` - If type conversion fails
pub fn from_storage_columnar(
storage_columnar: &vibesql_storage::ColumnarTable,
) -> Result<Self, ExecutorError> {
use vibesql_storage::ColumnData;
let column_names = storage_columnar.column_names().to_vec();
let row_count = storage_columnar.row_count();
// Handle empty tables: return an empty batch with column names but no data
// This happens when ColumnarTable::from_rows is called with empty rows -
// the column_names are preserved but the columns HashMap is empty
if row_count == 0 {
return Ok(Self {
row_count: 0,
columns: Vec::new(),
column_names: Some(column_names),
});
}
let mut columns = Vec::with_capacity(column_names.len());
for col_name in column_names.iter() {
let storage_col = storage_columnar.get_column(col_name).ok_or_else(|| {
ExecutorError::ColumnarColumnNotFoundByName { column_name: col_name.to_string() }
})?;
let column_array =
match storage_col {
ColumnData::Int64 { values, nulls } => {
// Zero-copy: Arc::clone is O(1) - just bumps reference count
let null_bitmap =
if nulls.iter().any(|&n| n) { Some(Arc::clone(nulls)) } else { None };
ColumnArray::Int64(Arc::clone(values), null_bitmap)
}
ColumnData::Float64 { values, nulls } => {
// Zero-copy: Arc::clone is O(1)
let null_bitmap =
if nulls.iter().any(|&n| n) { Some(Arc::clone(nulls)) } else { None };
ColumnArray::Float64(Arc::clone(values), null_bitmap)
}
ColumnData::String { values, nulls } => {
// Zero-copy: Arc::clone is O(1)
let null_bitmap =
if nulls.iter().any(|&n| n) { Some(Arc::clone(nulls)) } else { None };
ColumnArray::String(Arc::clone(values), null_bitmap)
}
ColumnData::Bool { values, nulls } => {
// Convert bool to u8 for SIMD compatibility (requires iteration)
let u8_values: Vec<u8> =
values.iter().map(|&b| if b { 1 } else { 0 }).collect();
let null_bitmap =
if nulls.iter().any(|&n| n) { Some(Arc::clone(nulls)) } else { None };
ColumnArray::Boolean(Arc::new(u8_values), null_bitmap)
}
ColumnData::Date { values, nulls } => {
// Convert Date to i32 (days since Unix epoch 1970-01-01)
// Must use the same formula as simd_filter.rs:date_to_days_since_epoch
// for predicate evaluation to work correctly
let i32_values: Vec<i32> =
values.iter().map(date_to_days_since_epoch).collect();
let null_bitmap =
if nulls.iter().any(|&n| n) { Some(Arc::clone(nulls)) } else { None };
ColumnArray::Date(Arc::new(i32_values), null_bitmap)
}
ColumnData::Timestamp { values, nulls } => {
// Convert Timestamp to Mixed (fallback - no direct i64 conversion)
let sql_values: Vec<SqlValue> = values
.iter()
.zip(nulls.iter())
.map(
|(t, &is_null)| {
if is_null {
SqlValue::Null
} else {
SqlValue::Timestamp(*t)
}
},
)
.collect();
ColumnArray::Mixed(Arc::new(sql_values))
}
ColumnData::Time { values, nulls } => {
// Convert Time to Mixed (fallback - Time doesn't have direct i64 conversion)
let sql_values: Vec<SqlValue> = values
.iter()
.zip(nulls.iter())
.map(
|(t, &is_null)| {
if is_null {
SqlValue::Null
} else {
SqlValue::Time(*t)
}
},
)
.collect();
ColumnArray::Mixed(Arc::new(sql_values))
}
ColumnData::Interval { values, nulls } => {
// Convert Interval to Mixed (fallback)
let sql_values: Vec<SqlValue> = values
.iter()
.zip(nulls.iter())
.map(|(i, &is_null)| {
if is_null {
SqlValue::Null
} else {
SqlValue::Interval(i.clone())
}
})
.collect();
ColumnArray::Mixed(Arc::new(sql_values))
}
ColumnData::Vector { values, nulls } => {
// Convert Vector to Mixed (fallback)
let sql_values: Vec<SqlValue> =
values
.iter()
.zip(nulls.iter())
.map(|(v, &is_null)| {
if is_null {
SqlValue::Null
} else {
SqlValue::Vector(v.clone())
}
})
.collect();
ColumnArray::Mixed(Arc::new(sql_values))
}
ColumnData::Blob { values, nulls } => {
// Convert Blob to Mixed (fallback)
let sql_values: Vec<SqlValue> =
values
.iter()
.zip(nulls.iter())
.map(|(b, &is_null)| {
if is_null {
SqlValue::Null
} else {
SqlValue::Blob(b.clone())
}
})
.collect();
ColumnArray::Mixed(Arc::new(sql_values))
}
};
columns.push(column_array);
}
Ok(Self { row_count, columns, column_names: Some(column_names) })
}
}
/// Convert Date to days since Unix epoch (1970-01-01)
///
/// This function MUST be kept in sync with simd_filter.rs::date_to_days_since_epoch()
/// to ensure predicates compare dates correctly.
fn date_to_days_since_epoch(date: &vibesql_types::Date) -> i32 {
// Accurate days since Unix epoch calculation with leap year handling
let year_days = (date.year - 1970) * 365;
let leap_years =
((date.year - 1969) / 4) - ((date.year - 1901) / 100) + ((date.year - 1601) / 400);
let month_days: i32 = match date.month {
1 => 0,
2 => 31,
3 => 59,
4 => 90,
5 => 120,
6 => 151,
7 => 181,
8 => 212,
9 => 243,
10 => 273,
11 => 304,
12 => 334,
_ => 0,
};
// Add leap day if after February in a leap year
let is_leap = date.year % 4 == 0 && (date.year % 100 != 0 || date.year % 400 == 0);
let leap_adjustment = if is_leap && date.month > 2 { 1 } else { 0 };
year_days + leap_years + month_days + date.day as i32 - 1 + leap_adjustment
}
#[cfg(test)]
mod tests {
use vibesql_storage::Row;
use super::*;
#[test]
fn test_from_storage_columnar() {
// Create storage-layer columnar table
let rows = vec![
Row::new(vec![
SqlValue::Integer(1),
SqlValue::Double(10.5),
SqlValue::Varchar(arcstr::ArcStr::from("Alice")),
]),
Row::new(vec![
SqlValue::Integer(2),
SqlValue::Double(20.5),
SqlValue::Varchar(arcstr::ArcStr::from("Bob")),
]),
Row::new(vec![
SqlValue::Integer(3),
SqlValue::Double(30.5),
SqlValue::Varchar(arcstr::ArcStr::from("Charlie")),
]),
];
let column_names = vec!["id".to_string(), "value".to_string(), "name".to_string()];
let storage_columnar =
vibesql_storage::ColumnarTable::from_rows(&rows, &column_names).unwrap();
// Convert to executor ColumnarBatch
let batch = ColumnarBatch::from_storage_columnar(&storage_columnar).unwrap();
// Verify structure
assert_eq!(batch.row_count(), 3);
assert_eq!(batch.column_count(), 3);
// Verify column names
let names = batch.column_names().unwrap();
assert_eq!(names, &["id", "value", "name"]);
// Verify Int64 column
let col0 = batch.column(0).unwrap();
if let Some((values, nulls)) = col0.as_i64() {
assert_eq!(values, &[1, 2, 3]);
assert!(nulls.is_none());
} else {
panic!("Expected i64 column");
}
// Verify Float64 column
let col1 = batch.column(1).unwrap();
if let Some((values, nulls)) = col1.as_f64() {
assert_eq!(values, &[10.5, 20.5, 30.5]);
assert!(nulls.is_none());
} else {
panic!("Expected f64 column");
}
// Verify String column
let col2 = batch.column(2).unwrap();
if let ColumnArray::String(values, nulls) = col2 {
let str_refs: Vec<&str> = values.iter().map(|s| s.as_ref()).collect();
assert_eq!(str_refs, vec!["Alice", "Bob", "Charlie"]);
assert!(nulls.is_none());
} else {
panic!("Expected String column");
}
}
#[test]
fn test_from_storage_columnar_with_nulls() {
// Create storage-layer columnar table with NULLs
let rows = vec![
Row::new(vec![SqlValue::Integer(1), SqlValue::Double(10.0)]),
Row::new(vec![SqlValue::Null, SqlValue::Double(20.0)]),
Row::new(vec![SqlValue::Integer(3), SqlValue::Null]),
];
let column_names = vec!["id".to_string(), "value".to_string()];
let storage_columnar =
vibesql_storage::ColumnarTable::from_rows(&rows, &column_names).unwrap();
// Convert to executor ColumnarBatch
let batch = ColumnarBatch::from_storage_columnar(&storage_columnar).unwrap();
// Verify Int64 column with NULL
let col0 = batch.column(0).unwrap();
if let Some((values, Some(nulls))) = col0.as_i64() {
assert_eq!(values.len(), 3);
assert_eq!(nulls, &[false, true, false]);
} else {
panic!("Expected i64 column with nulls");
}
// Verify Float64 column with NULL
let col1 = batch.column(1).unwrap();
if let Some((values, Some(nulls))) = col1.as_f64() {
assert_eq!(values.len(), 3);
assert_eq!(nulls, &[false, false, true]);
} else {
panic!("Expected f64 column with nulls");
}
}
}