vibesql-storage 0.2.0

Storage 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
//! Columnar table storage.
//!
//! This module provides the `ColumnarTable` struct for storing data in
//! column-oriented format for analytical query performance.

use std::collections::HashMap;

use super::{builder::ColumnBuilder, data::ColumnData, types::ColumnTypeClass};
use crate::Row;

/// Columnar table storage
///
/// Stores data in column-oriented format for analytical query performance.
/// Each column is stored as a typed vector with a separate NULL bitmap.
///
/// # Example
///
/// ```text
/// use vibesql_storage::{Row, ColumnarTable};
/// use vibesql_types::SqlValue;
///
/// // Create rows
/// let rows = vec![
///     Row::new(vec![SqlValue::Integer(1), SqlValue::Double(3.14)]),
///     Row::new(vec![SqlValue::Integer(2), SqlValue::Double(2.71)]),
/// ];
///
/// // Convert to columnar
/// let column_names = vec!["id".to_string(), "value".to_string()];
/// let columnar = ColumnarTable::from_rows(&rows, &column_names).unwrap();
///
/// // Access column data
/// assert_eq!(columnar.row_count(), 2);
/// assert_eq!(columnar.column_count(), 2);
/// ```
#[derive(Debug, Clone)]
pub struct ColumnarTable {
    /// Column data indexed by column name
    columns: HashMap<String, ColumnData>,
    /// Column names in order (for iteration)
    column_names: Vec<String>,
    /// Number of rows
    row_count: usize,
}

impl ColumnarTable {
    /// Create a new empty columnar table
    pub fn new() -> Self {
        ColumnarTable { columns: HashMap::new(), column_names: Vec::new(), row_count: 0 }
    }

    /// Convert row-oriented data to columnar format (optimized single-pass)
    ///
    /// # Arguments
    /// * `rows` - Vector of rows to convert
    /// * `column_names` - Names of columns in order
    ///
    /// # Returns
    /// * `Ok(ColumnarTable)` on success
    /// * `Err(String)` if column count mismatch or incompatible types
    ///
    /// # Performance
    /// O(n * m) single pass through all data
    pub fn from_rows(rows: &[Row], column_names: &[String]) -> Result<Self, String> {
        if rows.is_empty() {
            return Ok(ColumnarTable {
                columns: HashMap::new(),
                column_names: column_names.to_vec(),
                row_count: 0,
            });
        }

        let row_count = rows.len();
        let col_count = column_names.len();

        // Validate first row column count
        if rows[0].len() != col_count {
            return Err(format!("Row 0 has {} columns, expected {}", rows[0].len(), col_count));
        }

        // Infer column types from first non-null value in each column
        let col_types: Vec<_> = (0..col_count)
            .map(|col_idx| {
                rows.iter()
                    .filter_map(|row| row.get(col_idx))
                    .find(|v| !v.is_null())
                    .map(ColumnTypeClass::from_sql_value)
                    .unwrap_or(ColumnTypeClass::Null)
            })
            .collect();

        // Pre-allocate column storage based on inferred types
        let mut column_builders: Vec<ColumnBuilder> =
            col_types.iter().map(|t| ColumnBuilder::new(*t, row_count)).collect();

        // Single pass through rows - distribute values to columns
        for (row_idx, row) in rows.iter().enumerate() {
            if row.len() != col_count {
                return Err(format!(
                    "Row {} has {} columns, expected {}",
                    row_idx,
                    row.len(),
                    col_count
                ));
            }

            for (col_idx, value) in row.values.iter().enumerate() {
                column_builders[col_idx].push(value)?;
            }
        }

        // Build final columns HashMap - consume builders
        let mut columns = HashMap::with_capacity(col_count);
        for (col_name, builder) in column_names.iter().zip(column_builders.into_iter()) {
            columns.insert(col_name.clone(), builder.build());
        }

        Ok(ColumnarTable { columns, column_names: column_names.to_vec(), row_count })
    }

    /// Create a ColumnarTable from a slice of row references
    ///
    /// This is useful when you have filtered rows (e.g., skipping deleted rows)
    /// and want to avoid cloning the entire row just to pass to from_rows.
    ///
    /// # Arguments
    /// * `rows` - Slice of row references to convert
    /// * `column_names` - Column names for the table schema
    ///
    /// # Returns
    /// * `Ok(ColumnarTable)` on success
    /// * `Err(String)` if column count mismatch or incompatible types
    pub fn from_row_refs(rows: &[&Row], column_names: &[String]) -> Result<Self, String> {
        if rows.is_empty() {
            return Ok(ColumnarTable {
                columns: HashMap::new(),
                column_names: column_names.to_vec(),
                row_count: 0,
            });
        }

        let row_count = rows.len();
        let col_count = column_names.len();

        // Validate first row column count
        if rows[0].len() != col_count {
            return Err(format!("Row 0 has {} columns, expected {}", rows[0].len(), col_count));
        }

        // Infer column types from first non-null value in each column
        let col_types: Vec<_> = (0..col_count)
            .map(|col_idx| {
                rows.iter()
                    .filter_map(|row| row.get(col_idx))
                    .find(|v| !v.is_null())
                    .map(ColumnTypeClass::from_sql_value)
                    .unwrap_or(ColumnTypeClass::Null)
            })
            .collect();

        // Pre-allocate column storage based on inferred types
        let mut column_builders: Vec<ColumnBuilder> =
            col_types.iter().map(|t| ColumnBuilder::new(*t, row_count)).collect();

        // Single pass through rows - distribute values to columns
        for (row_idx, row) in rows.iter().enumerate() {
            if row.len() != col_count {
                return Err(format!(
                    "Row {} has {} columns, expected {}",
                    row_idx,
                    row.len(),
                    col_count
                ));
            }

            for (col_idx, value) in row.values.iter().enumerate() {
                column_builders[col_idx].push(value)?;
            }
        }

        // Build final columns HashMap - consume builders
        let mut columns = HashMap::with_capacity(col_count);
        for (col_name, builder) in column_names.iter().zip(column_builders.into_iter()) {
            columns.insert(col_name.clone(), builder.build());
        }

        Ok(ColumnarTable { columns, column_names: column_names.to_vec(), row_count })
    }

    /// Convert columnar data back to row-oriented format
    ///
    /// # Returns
    /// Vector of rows reconstructed from columnar data
    ///
    /// # Performance
    /// O(n * m) where n = rows, m = columns
    pub fn to_rows(&self) -> Vec<Row> {
        let mut rows = Vec::with_capacity(self.row_count);

        for row_idx in 0..self.row_count {
            let mut values = Vec::with_capacity(self.column_names.len());

            for col_name in &self.column_names {
                if let Some(column) = self.columns.get(col_name) {
                    values.push(column.get(row_idx));
                } else {
                    values.push(vibesql_types::SqlValue::Null);
                }
            }

            rows.push(Row::new(values));
        }

        rows
    }

    /// Get the number of rows
    pub fn row_count(&self) -> usize {
        self.row_count
    }

    /// Get the number of columns
    pub fn column_count(&self) -> usize {
        self.column_names.len()
    }

    /// Get column data by name
    pub fn get_column(&self, name: &str) -> Option<&ColumnData> {
        self.columns.get(name)
    }

    /// Get mutable column data by name
    pub fn get_column_mut(&mut self, name: &str) -> Option<&mut ColumnData> {
        self.columns.get_mut(name)
    }

    /// Get all column names
    pub fn column_names(&self) -> &[String] {
        &self.column_names
    }

    /// Append a single row to the columnar table incrementally.
    ///
    /// This is O(m) where m = number of columns, compared to O(n * m) for a full rebuild.
    /// Uses `Arc::make_mut` on each column for copy-on-write semantics, so outstanding
    /// read snapshots (via `Arc<ColumnarTable>`) remain valid and unmodified.
    ///
    /// # Arguments
    /// * `row` - The row to append
    /// * `column_names` - Column names corresponding to the row values
    ///
    /// # Returns
    /// * `Ok(())` on success
    /// * `Err(String)` if column count mismatch or type mismatch
    pub fn append_row(&mut self, row: &crate::Row) -> Result<(), String> {
        if row.len() != self.column_names.len() {
            return Err(format!(
                "Row has {} columns, expected {}",
                row.len(),
                self.column_names.len()
            ));
        }

        // Handle empty table: need to initialize columns from the row
        if self.columns.is_empty() && !self.column_names.is_empty() {
            // Infer column types from row values
            let col_types: Vec<_> = row
                .values
                .iter()
                .map(|v| ColumnTypeClass::from_sql_value(v))
                .collect();

            // Create columns using ColumnBuilder for initial row
            for (col_idx, col_name) in self.column_names.iter().enumerate() {
                let mut builder = ColumnBuilder::new(col_types[col_idx], 1);
                builder.push(&row.values[col_idx])?;
                self.columns.insert(col_name.clone(), builder.build());
            }

            self.row_count = 1;
            return Ok(());
        }

        // Append to existing columns
        for (col_idx, col_name) in self.column_names.iter().enumerate() {
            let column = self.columns.get_mut(col_name).ok_or_else(|| {
                format!("Column '{}' not found in columnar table", col_name)
            })?;
            column.push_value(&row.values[col_idx])?;
        }

        self.row_count += 1;
        Ok(())
    }

    /// Append multiple rows to the columnar table incrementally.
    ///
    /// This is O(batch_size * m) where m = number of columns, compared to
    /// O(n * m) for a full rebuild from all rows.
    ///
    /// # Arguments
    /// * `rows` - Slice of rows to append
    ///
    /// # Returns
    /// * `Ok(())` on success
    /// * `Err(String)` if column count mismatch or type mismatch
    pub fn append_rows(&mut self, rows: &[crate::Row]) -> Result<(), String> {
        for row in rows {
            self.append_row(row)?;
        }
        Ok(())
    }

    /// Update a row at the given index in the columnar table.
    ///
    /// This is O(m) where m = number of columns.
    ///
    /// # Arguments
    /// * `index` - Row index to update
    /// * `row` - New row values
    ///
    /// # Returns
    /// * `Ok(())` on success
    /// * `Err(String)` if index out of bounds or type mismatch
    pub fn update_row_at(&mut self, index: usize, row: &crate::Row) -> Result<(), String> {
        if index >= self.row_count {
            return Err(format!(
                "Row index {} out of bounds (row_count: {})",
                index, self.row_count
            ));
        }
        if row.len() != self.column_names.len() {
            return Err(format!(
                "Row has {} columns, expected {}",
                row.len(),
                self.column_names.len()
            ));
        }

        for (col_idx, col_name) in self.column_names.iter().enumerate() {
            let column = self.columns.get_mut(col_name).ok_or_else(|| {
                format!("Column '{}' not found in columnar table", col_name)
            })?;
            column.set_value(index, &row.values[col_idx])?;
        }

        Ok(())
    }

    /// Remove a row at the given index from the columnar table.
    ///
    /// This shifts all subsequent rows down by one. For bulk deletions,
    /// consider using `remove_rows_sorted` instead.
    ///
    /// # Arguments
    /// * `index` - Row index to remove
    ///
    /// # Returns
    /// * `Ok(())` on success
    /// * `Err(String)` if index out of bounds
    pub fn remove_row_at(&mut self, index: usize) -> Result<(), String> {
        if index >= self.row_count {
            return Err(format!(
                "Row index {} out of bounds (row_count: {})",
                index, self.row_count
            ));
        }

        for col_name in self.column_names.clone() {
            if let Some(column) = self.columns.get_mut(&col_name) {
                column.remove_at(index);
            }
        }

        self.row_count -= 1;
        Ok(())
    }

    /// Remove multiple rows by sorted indices (must be sorted in ascending order).
    ///
    /// Removes from the end first to avoid index shifting issues.
    ///
    /// # Arguments
    /// * `indices` - Row indices to remove, sorted in ascending order
    ///
    /// # Returns
    /// * `Ok(())` on success
    /// * `Err(String)` if any index is out of bounds
    pub fn remove_rows_sorted(&mut self, indices: &[usize]) -> Result<(), String> {
        // Remove from the end first to avoid index invalidation
        for &index in indices.iter().rev() {
            self.remove_row_at(index)?;
        }
        Ok(())
    }

    /// Clear all data from the columnar table, keeping column names.
    pub fn clear(&mut self) {
        self.columns.clear();
        self.row_count = 0;
    }

    /// Estimate the memory size of this columnar table in bytes
    ///
    /// This is used for memory budgeting in the columnar cache.
    /// The estimate includes:
    /// - All column data (via ColumnData::size_in_bytes)
    /// - HashMap overhead for columns
    /// - Vec overhead for column_names
    /// - String storage for column names
    pub fn size_in_bytes(&self) -> usize {
        const VEC_OVERHEAD: usize = 3 * std::mem::size_of::<usize>();
        // HashMap has ~48 bytes base overhead plus per-bucket overhead
        const HASHMAP_BASE_OVERHEAD: usize = 48;
        const HASHMAP_ENTRY_OVERHEAD: usize = 8; // approximate per-entry overhead

        let columns_size: usize = self.columns.values().map(|c| c.size_in_bytes()).sum();

        let column_names_size: usize =
            self.column_names.iter().map(|s| std::mem::size_of::<String>() + s.capacity()).sum();

        // HashMap keys (column names stored again)
        let hashmap_keys_size: usize =
            self.columns.keys().map(|s| std::mem::size_of::<String>() + s.capacity()).sum();

        std::mem::size_of::<Self>() // Base struct size
            + columns_size
            + HASHMAP_BASE_OVERHEAD
            + self.columns.len() * HASHMAP_ENTRY_OVERHEAD
            + hashmap_keys_size
            + VEC_OVERHEAD
            + column_names_size
    }
}

impl Default for ColumnarTable {
    fn default() -> Self {
        Self::new()
    }
}