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
// ============================================================================
// Database Columnar Cache Integration
// ============================================================================
use std::sync::Arc;
use super::core::Database;
use crate::{columnar_cache::ColumnarCache, StorageError};
impl Database {
// ============================================================================
// Columnar Cache Methods
// ============================================================================
/// Get columnar representation of a table, using cache if available
///
/// This method provides an Arc-wrapped columnar representation of the table,
/// enabling zero-copy sharing between queries. The cache automatically manages
/// memory via LRU eviction.
///
/// # Arguments
/// * `table_name` - Name of the table to get columnar representation for
///
/// # Returns
/// * `Ok(Some(Arc<ColumnarTable>))` - Cached or newly converted columnar data
/// * `Ok(None)` - Table not found
/// * `Err(StorageError)` - Conversion failed
///
/// # Example
/// ```text
/// if let Some(columnar) = db.get_columnar("lineitem")? {
/// // Use columnar data for SIMD operations
/// }
/// ```
pub fn get_columnar(
&self,
table_name: &str,
) -> Result<Option<Arc<crate::ColumnarTable>>, StorageError> {
// For native columnar tables, return data directly from the table
// (no cache needed -- data is always hot and maintained incrementally)
if let Some(table) = self.get_table(table_name) {
if table.is_native_columnar() {
let columnar = table.scan_columnar()?;
return Ok(Some(Arc::new(columnar)));
}
}
// Check cache first (for row-oriented tables)
if let Some(cached) = self.columnar_cache.get(table_name) {
return Ok(Some(cached));
}
// Table not in cache - need to get table and convert
let table = match self.get_table(table_name) {
Some(t) => t,
None => return Ok(None),
};
// Convert to columnar format
let columnar = table.scan_columnar()?;
// Insert into cache and return
let cached = self.columnar_cache.insert(table_name, columnar);
Ok(Some(cached))
}
/// Invalidate columnar cache entry for a table
///
/// Called automatically when a table is modified (INSERT/UPDATE/DELETE)
/// to ensure the cache doesn't serve stale data.
///
/// For native columnar tables, this is a no-op since the columnar data
/// is maintained incrementally by the Table itself during DML operations.
pub fn invalidate_columnar_cache(&self, table_name: &str) {
// For native columnar tables, skip invalidation -- they maintain
// their own columnar data incrementally during INSERT/UPDATE/DELETE
if let Some(table) = self.get_table(table_name) {
if table.is_native_columnar() {
return;
}
}
self.columnar_cache.invalidate(table_name);
}
/// Clear all columnar cache entries
pub fn clear_columnar_cache(&self) {
self.columnar_cache.clear();
}
/// Get columnar cache statistics
///
/// Returns statistics about cache hits, misses, evictions, and conversions.
/// Useful for monitoring cache effectiveness and tuning the cache budget.
pub fn columnar_cache_stats(&self) -> crate::columnar_cache::CacheStats {
self.columnar_cache.stats()
}
/// Get current columnar cache memory usage in bytes
pub fn columnar_cache_memory_usage(&self) -> usize {
self.columnar_cache.memory_usage()
}
/// Get columnar cache memory budget in bytes
pub fn columnar_cache_budget(&self) -> usize {
self.columnar_cache.max_memory()
}
/// Set the columnar cache memory budget
///
/// Note: This creates a new cache, discarding all cached data.
/// Call this before loading data for best results.
pub fn set_columnar_cache_budget(&mut self, max_bytes: usize) {
self.columnar_cache = Arc::new(ColumnarCache::new(max_bytes));
}
/// Pre-warm the columnar cache for specific tables
///
/// This method eagerly converts row data to columnar format and populates
/// the cache. Call this after data loading to avoid conversion overhead
/// during query execution.
///
/// # Arguments
/// * `table_names` - Names of tables to pre-warm
///
/// # Returns
/// * `Ok(count)` - Number of tables successfully pre-warmed
/// * `Err(StorageError)` - Conversion failed for a table
///
/// # Example
/// ```text
/// // After loading TPC-H data
/// let warmed = db.pre_warm_columnar_cache(&["lineitem", "orders"])?;
/// eprintln!("Pre-warmed {} tables", warmed);
/// ```
///
/// # Performance
///
/// This method performs the row-to-columnar conversion once, eliminating
/// the ~31% overhead that would otherwise occur on the first query.
/// For a 600K row LINEITEM table, this saves ~40ms per query session.
pub fn pre_warm_columnar_cache(&self, table_names: &[&str]) -> Result<usize, StorageError> {
let mut count = 0;
for table_name in table_names {
// get_columnar will convert and cache if not already cached
if self.get_columnar(table_name)?.is_some() {
count += 1;
}
}
Ok(count)
}
/// Pre-warm the columnar cache for all tables in the database
///
/// This method eagerly converts all tables to columnar format.
/// Useful for benchmark scenarios where all tables will be queried.
///
/// # Returns
/// * `Ok(count)` - Number of tables successfully pre-warmed
/// * `Err(StorageError)` - Conversion failed for a table
///
/// # Example
/// ```text
/// // After loading all benchmark data
/// let warmed = db.pre_warm_all_columnar()?;
/// eprintln!("Pre-warmed {} tables", warmed);
/// ```
pub fn pre_warm_all_columnar(&self) -> Result<usize, StorageError> {
let table_names: Vec<String> = self.list_tables();
let refs: Vec<&str> = table_names.iter().map(|s| s.as_str()).collect();
self.pre_warm_columnar_cache(&refs)
}
}
#[cfg(test)]
mod tests {
use vibesql_catalog::{ColumnSchema, TableSchema};
use vibesql_types::{DataType, SqlValue};
use super::*;
use crate::Row;
fn create_test_table_schema(name: &str) -> TableSchema {
TableSchema::new(
name.to_string(),
vec![
ColumnSchema::new("id".to_string(), DataType::Integer, false),
ColumnSchema::new(
"name".to_string(),
DataType::Varchar { max_length: Some(255) },
true,
),
],
)
}
fn create_test_rows(count: usize) -> Vec<Row> {
(0..count)
.map(|i| {
Row::new(vec![
SqlValue::Integer(i as i64),
SqlValue::Varchar(arcstr::ArcStr::from(format!("name_{}", i))),
])
})
.collect()
}
#[test]
fn test_pre_warm_columnar_cache_with_valid_tables() {
let mut db = Database::new();
// Create test tables
db.create_table(create_test_table_schema("table1")).unwrap();
db.create_table(create_test_table_schema("table2")).unwrap();
// Insert some rows
for row in create_test_rows(10) {
db.insert_row("table1", row).unwrap();
}
for row in create_test_rows(5) {
db.insert_row("table2", row).unwrap();
}
// Pre-warm specific tables
let count = db.pre_warm_columnar_cache(&["table1", "table2"]).unwrap();
assert_eq!(count, 2, "Should have pre-warmed 2 tables");
// Verify stats show conversions occurred
let stats = db.columnar_cache_stats();
assert_eq!(stats.conversions, 2, "Should have converted 2 tables");
}
#[test]
fn test_pre_warm_columnar_cache_nonexistent_table() {
let db = Database::new();
// Pre-warm with nonexistent tables
let count = db.pre_warm_columnar_cache(&["nonexistent1", "nonexistent2"]).unwrap();
assert_eq!(count, 0, "Should return 0 for nonexistent tables");
// Verify no conversions occurred
let stats = db.columnar_cache_stats();
assert_eq!(stats.conversions, 0, "Should have 0 conversions for nonexistent tables");
}
#[test]
fn test_pre_warm_columnar_cache_mixed_tables() {
let mut db = Database::new();
// Create only one table
db.create_table(create_test_table_schema("exists")).unwrap();
for row in create_test_rows(5) {
db.insert_row("exists", row).unwrap();
}
// Pre-warm with mix of existing and nonexistent tables
let count = db.pre_warm_columnar_cache(&["exists", "nonexistent"]).unwrap();
assert_eq!(count, 1, "Should have pre-warmed only 1 existing table");
}
#[test]
fn test_pre_warm_all_columnar() {
let mut db = Database::new();
// Create multiple test tables
db.create_table(create_test_table_schema("table_a")).unwrap();
db.create_table(create_test_table_schema("table_b")).unwrap();
db.create_table(create_test_table_schema("table_c")).unwrap();
// Insert some rows
for row in create_test_rows(5) {
db.insert_row("table_a", row).unwrap();
}
for row in create_test_rows(3) {
db.insert_row("table_b", row).unwrap();
}
for row in create_test_rows(7) {
db.insert_row("table_c", row).unwrap();
}
// Pre-warm all tables
let count = db.pre_warm_all_columnar().unwrap();
assert_eq!(count, 3, "Should have pre-warmed all 3 tables");
// Verify stats
let stats = db.columnar_cache_stats();
assert_eq!(stats.conversions, 3, "Should have converted all 3 tables");
}
#[test]
fn test_pre_warm_results_in_cache_hits() {
let mut db = Database::new();
// Create and populate a table
db.create_table(create_test_table_schema("cached_table")).unwrap();
for row in create_test_rows(10) {
db.insert_row("cached_table", row).unwrap();
}
// Pre-warm the cache
let count = db.pre_warm_columnar_cache(&["cached_table"]).unwrap();
assert_eq!(count, 1);
// Record stats after pre-warming
let stats_before = db.columnar_cache_stats();
let hits_before = stats_before.hits;
// Access the columnar data again - should be a cache hit
let _ = db.get_columnar("cached_table").unwrap();
// Verify cache hit occurred
let stats_after = db.columnar_cache_stats();
assert_eq!(
stats_after.hits,
hits_before + 1,
"Should have one more cache hit after accessing pre-warmed table"
);
assert_eq!(
stats_after.conversions, stats_before.conversions,
"Should not have additional conversions"
);
}
#[test]
fn test_pre_warm_empty_table_list() {
let db = Database::new();
// Pre-warm with empty list
let count = db.pre_warm_columnar_cache(&[]).unwrap();
assert_eq!(count, 0, "Should return 0 for empty table list");
}
#[test]
fn test_pre_warm_all_empty_database() {
let db = Database::new();
// Pre-warm all on empty database
let count = db.pre_warm_all_columnar().unwrap();
assert_eq!(count, 0, "Should return 0 for empty database");
}
#[test]
fn test_pre_warm_idempotent() {
let mut db = Database::new();
// Create and populate a table
db.create_table(create_test_table_schema("test_table")).unwrap();
for row in create_test_rows(5) {
db.insert_row("test_table", row).unwrap();
}
// Pre-warm twice
let count1 = db.pre_warm_columnar_cache(&["test_table"]).unwrap();
let stats1 = db.columnar_cache_stats();
let count2 = db.pre_warm_columnar_cache(&["test_table"]).unwrap();
let stats2 = db.columnar_cache_stats();
// Both should report success
assert_eq!(count1, 1);
assert_eq!(count2, 1);
// But only one conversion should have occurred (second should be cache hit)
assert_eq!(stats1.conversions, 1);
assert_eq!(stats2.conversions, 1, "Second pre-warm should not cause additional conversion");
assert_eq!(stats2.hits, stats1.hits + 1, "Second pre-warm should result in cache hit");
}
}