dbx-core 0.1.2-beta

High-performance file-based database engine with 5-Tier Hybrid Storage
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
//! Columnar Cache — Tier 2: OLAP-optimized in-memory cache.
//!
//! Provides Arrow RecordBatch-based columnar storage for fast analytical queries.
//! Automatically syncs from Row-based Delta Store and supports SIMD-accelerated operations.

use crate::error::{DbxError, DbxResult};

use arrow::array::{ArrayRef, RecordBatch};

use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
use arrow::ipc::reader::StreamReader;

use dashmap::DashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};

/// Default maximum memory usage: 1GB
const DEFAULT_MAX_MEMORY: usize = 1024 * 1024 * 1024;

/// Cached data type: Typed (SQL tables) vs Raw (CRUD data)
#[derive(Clone)]
pub enum CachedData {
    /// SQL tables: Schema-based type conversion
    Typed {
        schema: SchemaRef,
        batches: Vec<RecordBatch>,
    },

    /// CRUD data: Raw Binary format
    Raw {
        batches: Vec<RecordBatch>, // Schema: [Binary(key), Binary(value)]
    },
}

impl CachedData {
    /// Get batches from cached data
    pub fn batches(&self) -> &[RecordBatch] {
        match self {
            CachedData::Typed { batches, .. } => batches,
            CachedData::Raw { batches } => batches,
        }
    }

    /// Get schema from cached data
    pub fn schema(&self) -> SchemaRef {
        match self {
            CachedData::Typed { schema, .. } => schema.clone(),
            CachedData::Raw { batches } => {
                if batches.is_empty() {
                    // Default Binary schema
                    Arc::new(Schema::new(vec![
                        Field::new("key", DataType::Binary, false),
                        Field::new("value", DataType::Binary, true),
                    ]))
                } else {
                    batches[0].schema()
                }
            }
        }
    }

    /// Check if cached data is typed (SQL table)
    pub fn is_typed(&self) -> bool {
        matches!(self, CachedData::Typed { .. })
    }
}

/// Tier 2: Columnar cache for OLAP queries
pub struct ColumnarCache {
    /// Table name → Columnar data mapping
    tables: DashMap<String, Arc<TableCache>>,

    /// Maximum memory usage (bytes)
    max_memory: usize,

    /// Current memory usage (bytes)
    current_memory: AtomicUsize,

    /// Access counter for LRU tracking
    access_counter: AtomicU64,
}

/// Per-table columnar cache
struct TableCache {
    /// Cached data (Typed or Raw)
    data: parking_lot::RwLock<CachedData>,

    /// Last sync timestamp from Delta
    _last_sync_ts: AtomicU64,

    /// Last access timestamp (logical) for LRU
    last_access: AtomicU64,

    /// Estimated memory usage (bytes)
    memory_usage: AtomicUsize,
}

impl ColumnarCache {
    /// Create a new Columnar Cache with default memory limit (1GB).
    pub fn new() -> Self {
        Self::with_memory_limit(DEFAULT_MAX_MEMORY)
    }

    /// Create a new Columnar Cache with custom memory limit.
    pub fn with_memory_limit(max_memory: usize) -> Self {
        Self {
            tables: DashMap::new(),
            max_memory,
            current_memory: AtomicUsize::new(0),
            access_counter: AtomicU64::new(0),
        }
    }

    /// Get current memory usage in bytes.
    pub fn memory_usage(&self) -> usize {
        self.current_memory.load(Ordering::Relaxed)
    }

    /// Get memory limit in bytes.
    pub fn memory_limit(&self) -> usize {
        self.max_memory
    }

    /// Check if cache should evict entries.
    pub fn should_evict(&self) -> bool {
        self.memory_usage() > self.max_memory
    }

    /// Persist table cache to disk using Arrow IPC format
    ///
    /// # Performance
    /// - Eliminates JSON parsing on restart (50-70% faster)
    /// - Zero-copy deserialization
    pub fn persist_to_disk(&self, table: &str, cache_dir: &str) -> DbxResult<()> {
        use crate::storage::arrow_ipc::write_ipc_batch;
        use std::fs;
        use std::path::Path;

        let table_cache = self
            .tables
            .get(table)
            .ok_or_else(|| DbxError::Storage(format!("Table '{}' not in cache", table)))?;

        let data = table_cache.data.read();
        let batches = data.batches();

        if batches.is_empty() {
            return Ok(());
        }

        let cache_path = Path::new(cache_dir);
        fs::create_dir_all(cache_path)
            .map_err(|e| DbxError::Storage(format!("Failed to create cache dir: {}", e)))?;

        for (idx, batch) in batches.iter().enumerate() {
            let ipc_bytes = write_ipc_batch(batch)?;
            let file_path = cache_path.join(format!("{}_{}.arrow", table, idx));
            fs::write(&file_path, ipc_bytes)
                .map_err(|e| DbxError::Storage(format!("Failed to write cache file: {}", e)))?;
        }

        Ok(())
    }

    /// Load table cache from disk using Arrow IPC format
    ///
    /// # Performance
    /// - ~0.5µs per batch (vs JSON: ~10µs)
    /// - Zero-copy: direct memory mapping
    pub fn load_from_disk(&self, table: &str, cache_dir: &str) -> DbxResult<Vec<RecordBatch>> {
        use crate::storage::arrow_ipc::read_ipc_batch;
        use std::fs;
        use std::path::Path;

        let cache_path = Path::new(cache_dir);
        if !cache_path.exists() {
            return Ok(vec![]);
        }

        let mut batches = Vec::new();
        let mut idx = 0;

        loop {
            let file_path = cache_path.join(format!("{}_{}.arrow", table, idx));
            if !file_path.exists() {
                break;
            }

            let ipc_bytes = fs::read(&file_path)
                .map_err(|e| DbxError::Storage(format!("Failed to read cache file: {}", e)))?;
            let batch = read_ipc_batch(&ipc_bytes)?;
            batches.push(batch);
            idx += 1;
        }

        if !batches.is_empty() {
            for batch in &batches {
                self.insert_batch(table, batch.clone())?;
            }
        }

        Ok(batches)
    }

    /// Clear persisted cache files for a table
    pub fn clear_disk_cache(&self, table: &str, cache_dir: &str) -> DbxResult<()> {
        use std::fs;
        use std::path::Path;

        let cache_path = Path::new(cache_dir);
        if !cache_path.exists() {
            return Ok(());
        }

        let mut idx = 0;
        loop {
            let file_path = cache_path.join(format!("{}_{}.arrow", table, idx));
            if !file_path.exists() {
                break;
            }
            fs::remove_file(&file_path)
                .map_err(|e| DbxError::Storage(format!("Failed to remove cache file: {}", e)))?;
            idx += 1;
        }

        Ok(())
    }

    /// Insert a RecordBatch into the cache as Raw (Binary) data.
    pub fn insert_batch(&self, table: &str, batch: RecordBatch) -> DbxResult<()> {
        let memory_size = estimate_batch_memory(&batch);

        // Check memory limit and evict if necessary
        let mut attempts = 0;
        const MAX_EVICTION_ATTEMPTS: usize = 10;

        while self.current_memory.load(Ordering::Relaxed) + memory_size > self.max_memory {
            if attempts >= MAX_EVICTION_ATTEMPTS {
                return Err(DbxError::Storage(
                    "Columnar cache memory limit exceeded (eviction failed)".to_string(),
                ));
            }
            if !self.evict_lru() {
                // No more tables to evict or eviction failed
                return Err(DbxError::Storage(
                    "Columnar cache memory limit exceeded (nothing to evict)".to_string(),
                ));
            }
            attempts += 1;
        }

        // Get or create table cache with Raw data
        let table_cache = self.tables.entry(table.to_string()).or_insert_with(|| {
            Arc::new(TableCache {
                data: parking_lot::RwLock::new(CachedData::Raw {
                    batches: Vec::new(),
                }),
                _last_sync_ts: AtomicU64::new(0),
                last_access: AtomicU64::new(self.access_counter.fetch_add(1, Ordering::Relaxed)),
                memory_usage: AtomicUsize::new(0),
            })
        });

        // Update access time
        table_cache.last_access.store(
            self.access_counter.fetch_add(1, Ordering::Relaxed),
            Ordering::Relaxed,
        );

        // Insert batch into existing data
        let mut data = table_cache.data.write();
        match &mut *data {
            CachedData::Raw { batches } => batches.push(batch),
            CachedData::Typed { batches, .. } => batches.push(batch),
        }

        table_cache
            .memory_usage
            .fetch_add(memory_size, Ordering::Relaxed);
        self.current_memory
            .fetch_add(memory_size, Ordering::Relaxed);

        Ok(())
    }

    /// Sync data from storage tiers to Columnar Cache.
    ///
    /// If table_schema is provided, converts data to typed format (SQL table).
    /// Otherwise, stores as raw Binary format (CRUD data).
    pub fn sync_from_storage(
        &self,
        table: &str,
        rows: Vec<(Vec<u8>, Vec<u8>)>,
        table_schema: Option<SchemaRef>,
    ) -> DbxResult<usize> {
        if rows.is_empty() {
            self.clear_table(table)?;
            return Ok(0);
        }

        // Branch: Typed (SQL) vs Raw (CRUD)
        if let Some(schema) = table_schema {
            self.sync_typed(table, rows, schema)
        } else {
            self.sync_raw(table, rows)
        }
    }

    /// Deserialize Arrow IPC format to RecordBatch
    fn deserialize_arrow_ipc(value: &[u8]) -> DbxResult<RecordBatch> {
        let cursor = std::io::Cursor::new(value);
        let mut reader = StreamReader::try_new(cursor, None)
            .map_err(|e| DbxError::Serialization(format!("Arrow IPC read error: {}", e)))?;

        // Read the first (and only) batch
        reader
            .next()
            .ok_or_else(|| DbxError::Serialization("No batch in Arrow IPC stream".to_string()))?
            .map_err(|e| DbxError::Serialization(format!("Arrow IPC batch error: {}", e)))
    }

    /// Sync as typed data (SQL tables with schema)
    fn sync_typed(
        &self,
        table: &str,
        rows: Vec<(Vec<u8>, Vec<u8>)>,
        schema: SchemaRef,
    ) -> DbxResult<usize> {
        // Deserialize Arrow IPC and collect batches
        let mut batches = Vec::new();

        for (_key, value) in rows.iter() {
            let batch = Self::deserialize_arrow_ipc(value)?;
            batches.push(batch);
        }

        // Clear table and insert consolidated batch
        self.clear_table(table)?;
        if !batches.is_empty() {
            let consolidated = arrow::compute::concat_batches(&schema, &batches)
                .map_err(|e| DbxError::Storage(format!("Failed to consolidate batches: {}", e)))?;
            self.insert_typed_batch(table, schema, consolidated)?;
        }

        Ok(rows.len())
    }

    /// Sync as raw Binary data (CRUD tables without schema)
    fn sync_raw(&self, table: &str, rows: Vec<(Vec<u8>, Vec<u8>)>) -> DbxResult<usize> {
        // 2. Convert to RecordBatch (Schema: key[Binary], value[Binary])
        use arrow::array::builder::BinaryBuilder;
        let schema = Arc::new(Schema::new(vec![
            Field::new("key", DataType::Binary, false),
            Field::new("value", DataType::Binary, true),
        ]));

        let mut key_builder = BinaryBuilder::with_capacity(rows.len(), rows.len() * 32);
        let mut val_builder = BinaryBuilder::with_capacity(rows.len(), rows.len() * 128);

        for (k, v) in rows {
            // Decode versioned keys for columnar cache
            let user_key = if k.len() > 8 {
                if let Ok(vk) = crate::transaction::mvcc::version::VersionedKey::decode(&k) {
                    vk.user_key
                } else {
                    k
                }
            } else {
                k
            };
            key_builder.append_value(user_key);
            val_builder.append_value(v);
        }

        let batch = RecordBatch::try_new(
            schema,
            vec![
                Arc::new(key_builder.finish()),
                Arc::new(val_builder.finish()),
            ],
        )?;

        let row_count = batch.num_rows();

        // 3. Replace cache content
        self.clear_table(table)?;
        self.insert_batch(table, batch)?;

        Ok(row_count)
    }

    /// Insert a typed batch (SQL table)
    fn insert_typed_batch(
        &self,
        table: &str,
        schema: SchemaRef,
        batch: RecordBatch,
    ) -> DbxResult<()> {
        let memory_size = estimate_batch_memory(&batch);

        // Check memory limit (same as insert_batch)
        let mut attempts = 0;
        const MAX_EVICTION_ATTEMPTS: usize = 10;

        while self.current_memory.load(Ordering::Relaxed) + memory_size > self.max_memory {
            if attempts >= MAX_EVICTION_ATTEMPTS {
                return Err(DbxError::Storage(
                    "Columnar cache memory limit exceeded (eviction failed)".to_string(),
                ));
            }
            if !self.evict_lru() {
                return Err(DbxError::Storage(
                    "Columnar cache memory limit exceeded (nothing to evict)".to_string(),
                ));
            }
            attempts += 1;
        }

        // Get or create table cache with Typed data
        let table_cache = {
            self.tables
                .entry(table.to_string())
                .or_insert_with(|| {
                    Arc::new(TableCache {
                        data: parking_lot::RwLock::new(CachedData::Typed {
                            schema: schema.clone(),
                            batches: Vec::new(),
                        }),
                        _last_sync_ts: AtomicU64::new(0),
                        last_access: AtomicU64::new(
                            self.access_counter.fetch_add(1, Ordering::Relaxed),
                        ),
                        memory_usage: AtomicUsize::new(0),
                    })
                })
                .clone()
        };

        // Update access time
        table_cache.last_access.store(
            self.access_counter.fetch_add(1, Ordering::Relaxed),
            Ordering::Relaxed,
        );

        // Insert batch
        let mut data = table_cache.data.write();
        match &mut *data {
            CachedData::Typed { batches, .. } => {
                batches.push(batch);
            }
            CachedData::Raw { .. } => {
                *data = CachedData::Typed {
                    schema,
                    batches: vec![batch],
                };
            }
        }
        drop(data);

        table_cache
            .memory_usage
            .fetch_add(memory_size, Ordering::Relaxed);
        self.current_memory
            .fetch_add(memory_size, Ordering::Relaxed);

        Ok(())
    }

    /// Get batches with filter pushdown.
    ///
    /// The filter closure takes a full RecordBatch and returns a BooleanArray (mask).
    /// Rows where mask is true are kept.
    pub fn get_batches_with_filter<F>(
        &self,
        table: &str,
        projection: Option<&[usize]>,
        filter: F,
    ) -> DbxResult<Option<Vec<RecordBatch>>>
    where
        F: Fn(&RecordBatch) -> DbxResult<arrow::array::BooleanArray>,
    {
        let Some(table_cache) = self.tables.get(table) else {
            return Ok(None);
        };

        // Update LRU on access
        let current_access = self.access_counter.fetch_add(1, Ordering::Relaxed);
        table_cache
            .last_access
            .store(current_access, Ordering::Relaxed);

        let data = table_cache.data.read();
        let batches = data.batches();

        if batches.is_empty() {
            return Ok(None);
        }

        let mut result = Vec::with_capacity(batches.len());

        for batch in batches.iter() {
            // 1. Apply Filter
            let mask = filter(batch)?;

            // Use arrow::compute::filter_record_batch to filter rows
            let filtered_batch = arrow::compute::filter_record_batch(batch, &mask)
                .map_err(|e| DbxError::Storage(format!("Failed to filter batch: {}", e)))?;

            if filtered_batch.num_rows() == 0 {
                continue;
            }

            // 2. Apply Projection
            let final_batch = if let Some(indices) = projection {
                project_batch(&filtered_batch, indices)?
            } else {
                filtered_batch
            };

            result.push(final_batch);
        }

        Ok(Some(result))
    }

    /// Get all batches for a table with optional column projection.
    pub fn get_batches(
        &self,
        table: &str,
        projection: Option<&[usize]>,
    ) -> DbxResult<Option<Vec<RecordBatch>>> {
        // Try case-insensitive lookup
        let table_key = self
            .tables
            .iter()
            .find(|entry| entry.key().to_lowercase() == table.to_lowercase())
            .map(|entry| entry.key().clone());

        let lookup_key = table_key.as_deref().unwrap_or(table);

        let Some(table_cache) = self.tables.get(lookup_key) else {
            return Ok(None);
        };

        // Update LRU on access
        let current_access = self.access_counter.fetch_add(1, Ordering::Relaxed);
        table_cache
            .last_access
            .store(current_access, Ordering::Relaxed);

        let data = table_cache.data.read();
        let batches = data.batches();

        if batches.is_empty() {
            return Ok(None);
        }

        // Apply projection if specified — par_iter()로 병렬 적용
        let result = if let Some(indices) = projection {
            use rayon::prelude::*;
            batches
                .par_iter()
                .map(|batch| project_batch(batch, indices))
                .collect::<DbxResult<Vec<_>>>()?
        } else {
            batches.to_vec()
        };

        Ok(Some(result))
    }

    /// Clear all cached data for a table.
    pub fn clear_table(&self, table: &str) -> DbxResult<()> {
        if let Some((_, table_cache)) = self.tables.remove(table) {
            let memory = table_cache.memory_usage.load(Ordering::Relaxed);
            self.current_memory.fetch_sub(memory, Ordering::Relaxed);
        }
        Ok(())
    }

    /// Clear all cached data.
    pub fn clear_all(&self) -> DbxResult<()> {
        self.tables.clear();
        self.current_memory.store(0, Ordering::Relaxed);
        Ok(())
    }

    /// Get schema for a table.
    pub fn get_schema(&self, table: &str) -> Option<SchemaRef> {
        self.tables.get(table).map(|tc| {
            let data = tc.data.read();
            data.schema()
        })
    }

    /// Evict the least recently used table.
    /// Returns true if something was evicted, false otherwise.
    fn evict_lru(&self) -> bool {
        // Find the table with the smallest last_access value
        // Using a simple scan O(N) where N is number of tables.
        // For a cache with thousands of tables, this might need optimization (MinHeap),
        // but for typical DB workloads with limited active tables, it's fine.

        let candidate = self
            .tables
            .iter()
            .min_by_key(|entry| entry.value().last_access.load(Ordering::Relaxed))
            .map(|entry| entry.key().clone());

        if let Some(table_to_evict) = candidate {
            // Remove the table
            if let Some((_, table_cache)) = self.tables.remove(&table_to_evict) {
                let memory = table_cache.memory_usage.load(Ordering::Relaxed);
                self.current_memory.fetch_sub(memory, Ordering::Relaxed);
                return true;
            }
        }

        false
    }

    /// Get list of cached tables.
    pub fn table_names(&self) -> Vec<String> {
        self.tables.iter().map(|e| e.key().clone()).collect()
    }

    /// Check if a table exists in the cache.
    pub fn has_table(&self, table: &str) -> bool {
        self.tables.contains_key(table)
    }
}

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

/// Estimate memory usage of a RecordBatch.
fn estimate_batch_memory(batch: &RecordBatch) -> usize {
    batch
        .columns()
        .iter()
        .map(|array| array.get_array_memory_size())
        .sum()
}

/// Project a RecordBatch to selected columns.
fn project_batch(batch: &RecordBatch, indices: &[usize]) -> DbxResult<RecordBatch> {
    let schema = batch.schema();
    let columns: Vec<ArrayRef> = indices.iter().map(|&i| batch.column(i).clone()).collect();

    let projected_fields: Vec<_> = indices.iter().map(|&i| schema.field(i).clone()).collect();
    let projected_schema = Arc::new(arrow::datatypes::Schema::new(projected_fields));

    RecordBatch::try_new(projected_schema, columns)
        .map_err(|e| DbxError::Storage(format!("Failed to project batch: {}", e)))
}

#[cfg(test)]
mod tests {
    use super::*;
    use arrow::array::{Int32Array, StringArray};
    use arrow::datatypes::{DataType, Field, Schema};

    fn create_test_batch() -> RecordBatch {
        let schema = Arc::new(Schema::new(vec![
            Field::new("id", DataType::Int32, false),
            Field::new("name", DataType::Utf8, false),
        ]));

        let id_array = Int32Array::from(vec![1, 2, 3]);
        let name_array = StringArray::from(vec!["Alice", "Bob", "Charlie"]);

        RecordBatch::try_new(schema, vec![Arc::new(id_array), Arc::new(name_array)]).unwrap()
    }

    #[test]
    fn test_insert_and_get() {
        let cache = ColumnarCache::new();
        let batch = create_test_batch();

        cache.insert_batch("users", batch.clone()).unwrap();

        let result = cache.get_batches("users", None).unwrap();
        assert!(result.is_some());
        assert_eq!(result.unwrap().len(), 1);
    }

    #[test]
    fn test_projection() {
        let cache = ColumnarCache::new();
        let batch = create_test_batch();

        cache.insert_batch("users", batch).unwrap();

        // Project only column 0 (id)
        let result = cache.get_batches("users", Some(&[0])).unwrap().unwrap();
        assert_eq!(result[0].num_columns(), 1);
        assert_eq!(result[0].schema().field(0).name(), "id");
    }

    #[test]
    fn test_memory_tracking() {
        let cache = ColumnarCache::new();
        let batch = create_test_batch();

        let initial_memory = cache.memory_usage();
        cache.insert_batch("users", batch).unwrap();
        let after_insert = cache.memory_usage();

        assert!(after_insert > initial_memory);
    }

    #[test]
    fn test_clear_table() {
        let cache = ColumnarCache::new();
        let batch = create_test_batch();

        cache.insert_batch("users", batch).unwrap();
        assert!(cache.get_batches("users", None).unwrap().is_some());

        cache.clear_table("users").unwrap();
        assert!(cache.get_batches("users", None).unwrap().is_none());
        assert_eq!(cache.memory_usage(), 0);
    }

    #[test]
    fn test_memory_limit() {
        let cache = ColumnarCache::with_memory_limit(100); // Very small limit
        let batch = create_test_batch();

        let result = cache.insert_batch("users", batch);
        assert!(result.is_err()); // Should fail due to memory limit
    }

    #[test]
    fn test_table_names() {
        let cache = ColumnarCache::new();
        let batch = create_test_batch();

        cache.insert_batch("users", batch.clone()).unwrap();
        cache.insert_batch("orders", batch).unwrap();

        let mut names = cache.table_names();
        names.sort();
        assert_eq!(names, vec!["orders", "users"]);
    }

    #[test]
    fn test_lru_eviction() {
        let batch = create_test_batch();
        let batch_size = estimate_batch_memory(&batch);

        // Limit allows for 2 batches exactly
        let cache = ColumnarCache::with_memory_limit(batch_size * 2 + 100);

        // Insert A, B
        cache.insert_batch("A", batch.clone()).unwrap();
        cache.insert_batch("B", batch.clone()).unwrap();

        // Access A (makes B the LRU)
        cache.get_batches("A", None).unwrap();

        // Insert C (triggers eviction of B)
        // Note: insert_batch loops until enough space.
        // Needs 1 batch size. Currently used 2.
        // Eviction removes one table.
        // It should pick B.
        cache.insert_batch("C", batch.clone()).unwrap();

        // Check content
        let names = cache.table_names();
        assert!(names.contains(&"A".to_string()));
        assert!(names.contains(&"C".to_string()));
        assert!(!names.contains(&"B".to_string())); // B should be gone
    }

    #[test]
    fn test_filter_pushdown() {
        let cache = ColumnarCache::new();
        let batch = create_test_batch(); // id: 1, 2, 3

        cache.insert_batch("users", batch).unwrap();

        // Filter: id > 1
        let result = cache
            .get_batches_with_filter("users", None, |batch| {
                use arrow::array::Array; // Import Array trait for is_null
                let id_col = batch
                    .column(0)
                    .as_any()
                    .downcast_ref::<Int32Array>()
                    .unwrap();
                let mut builder = arrow::array::BooleanBuilder::with_capacity(id_col.len());

                for i in 0..id_col.len() {
                    if id_col.is_null(i) {
                        builder.append_null();
                    } else {
                        builder.append_value(id_col.value(i) > 1);
                    }
                }
                Ok(builder.finish())
            })
            .unwrap()
            .unwrap();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].num_rows(), 2); // 2 and 3

        let ids = result[0]
            .column(0)
            .as_any()
            .downcast_ref::<Int32Array>()
            .unwrap();
        assert_eq!(ids.value(0), 2);
        assert_eq!(ids.value(1), 3);
    }
}