akar-storage 0.1.6

Storage engine for the Akar embedded graph 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
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
//! Spiller — disk spilling and stream-merge for memory-constrained batch ingestion.
//!
//! When a `ColumnChunk` or `NodeGroup` exceeds the configured memory threshold
//! during `COPY FROM` or bulk inserts, the spiller serializes the in-memory data
//! to a temporary file. Once all rows are ingested, a multi-way stream-merge
//! reads back all spill files plus the final in-memory buffer, deduplicates by
//! primary key, and writes the merged result to the persistent `Column` via
//! `BufferManager`.
//!
//! # Strategy
//!
//! This is a simple `Vec<Value>` → JSON-lines → disk approach. Each spill file
//! contains one JSON object per row. This is intentionally not Arrow-CSR format
//! — that can be a future optimization.

use crate::column_chunk::ColumnChunk;
use akar_common::error::StorageError;
use akar_common::types::Value;
use serde::{Deserialize, Serialize};
use serde_json;
use std::fs;
use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

/// Metadata for a single spill file on disk.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpillFile {
    /// Absolute path to the temporary spill file.
    pub path: PathBuf,
    /// Number of rows in this spill file.
    pub row_count: usize,
    /// Optional sort key column index (for PK-ordered merge).
    pub sort_key_column: Option<usize>,
}

/// The spiller manages temporary files for memory-constrained batch operations.
///
/// Each `spill()` call serializes a `ColumnChunk` to a new temp file and clears
/// the chunk's in-memory buffer. Spill files are named `spill_00001.jsonl` etc.
#[derive(Debug)]
pub struct Spiller {
    /// Directory where spill files are written.
    tmp_dir: PathBuf,
    /// Monotonically increasing counter for unique spill file names.
    spill_counter: AtomicU64,
    /// Maximum in-memory bytes per ColumnChunk before spilling triggers.
    /// When a chunk's estimated memory exceeds this, `spill()` is called.
    pub memory_threshold: u64,
}

impl Spiller {
    /// Create a new spiller that writes to `tmp_dir`.
    ///
    /// `memory_threshold` is the estimated byte limit for a `ColumnChunk`
    /// before triggering a spill. Use 0 to disable spilling entirely.
    pub fn new(tmp_dir: impl Into<PathBuf>, memory_threshold: u64) -> Self {
        let tmp_dir = tmp_dir.into();
        let _ = fs::create_dir_all(&tmp_dir);
        Self {
            tmp_dir,
            spill_counter: AtomicU64::new(1),
            memory_threshold,
        }
    }

    /// Return the next unique spill file path.
    fn next_spill_path(&self) -> PathBuf {
        let n = self.spill_counter.fetch_add(1, Ordering::Relaxed);
        self.tmp_dir.join(format!("spill_{n:05}.jsonl"))
    }

    /// Estimate the in-memory size of a `ColumnChunk`'s buffered values.
    ///
    /// This is a rough estimate: each `Value` variant has different sizes.
    /// We use a conservative estimate of 64 bytes per value on average.
    pub fn estimated_chunk_size(chunk: &ColumnChunk) -> u64 {
        chunk.num_values() as u64 * 64
    }

    /// Spill a `ColumnChunk` to disk: serialize its values to a JSON-lines file
    /// and clear the in-memory buffer.
    ///
    /// Returns the `SpillFile` metadata on success.
    ///
    /// If the chunk is empty, returns `None`.
    pub fn spill(&self, chunk: &mut ColumnChunk) -> Result<Option<SpillFile>, StorageError> {
        if chunk.is_empty() {
            return Ok(None);
        }

        let path = self.next_spill_path();
        let values: Vec<Value> = chunk.drain();
        let row_count = values.len();

        // Write as JSON-lines: one line per row, each line is a JSON array of column values
        // For a single-column chunk, each line is just one value.
        // Multi-column chunks are handled by the caller (NodeGroup) which spills
        // all columns together in a coordinated way.
        let mut file = fs::File::create(&path)
            .map_err(|e| StorageError::Spiller(format!("Failed to create spill file {:?}: {e}", path)))?;

        for value in &values {
            let line = serde_json::to_string(value)
                .map_err(|e| StorageError::Spiller(format!("Failed to serialize value: {e}")))?;
            writeln!(file, "{line}").map_err(|e| StorageError::Spiller(format!("Failed to write spill file: {e}")))?;
        }

        // Re-allocate the chunk's buffer
        let _ = chunk; // chunk is already drained by `drain()`

        Ok(Some(SpillFile {
            path,
            row_count,
            sort_key_column: None,
        }))
    }

    /// Spill all columns of a NodeGroup-style set of column chunks together.
    ///
    /// Each row is serialized as a JSON array `[col0, col1, ..., colN]`.
    /// This is used by `NodeGroup` to spill multi-column data.
    pub fn spill_columns(&self, chunks: &mut [ColumnChunk]) -> Result<Option<SpillFile>, StorageError> {
        if chunks.is_empty() || chunks[0].is_empty() {
            return Ok(None);
        }

        let path = self.next_spill_path();
        let num_rows = chunks[0].num_values();
        let num_cols = chunks.len();

        // Drain all columns
        let mut drained: Vec<Vec<Value>> = chunks.iter_mut().map(|c| c.drain()).collect();

        let mut file = fs::File::create(&path)
            .map_err(|e| StorageError::Spiller(format!("Failed to create spill file {:?}: {e}", path)))?;

        for row in 0..num_rows {
            let mut row_values = Vec::with_capacity(num_cols);
            for col in 0..num_cols {
                let val = if row < drained[col].len() {
                    std::mem::replace(&mut drained[col][row], Value::Null)
                } else {
                    Value::Null
                };
                row_values.push(val);
            }
            let line = serde_json::to_string(&row_values)
                .map_err(|e| StorageError::Spiller(format!("Failed to serialize row: {e}")))?;
            writeln!(file, "{line}").map_err(|e| StorageError::Spiller(format!("Failed to write spill file: {e}")))?;
        }

        Ok(Some(SpillFile {
            path,
            row_count: num_rows,
            sort_key_column: None,
        }))
    }

    /// Restore a single-column `ColumnChunk` from a spill file.
    ///
    /// Each line of the JSON-lines file is deserialized as a single `Value`.
    /// Returns a new `ColumnChunk` with the restored values.
    pub fn restore(&self, spill: &SpillFile) -> Result<ColumnChunk, StorageError> {
        let file = fs::File::open(&spill.path)
            .map_err(|e| StorageError::Spiller(format!("Failed to open spill file {:?}: {e}", spill.path)))?;
        let reader = BufReader::new(file);
        let mut values = Vec::with_capacity(spill.row_count);

        for line in reader.lines() {
            let line = line.map_err(|e| StorageError::Spiller(format!("Failed to read spill file: {e}")))?;
            let line = line.trim().to_string();
            if line.is_empty() {
                continue;
            }
            let value: Value = serde_json::from_str(&line)
                .map_err(|e| StorageError::Spiller(format!("Failed to deserialize value from spill file: {e}")))?;
            values.push(value);
        }

        let chunk = ColumnChunk::from(values);
        Ok(chunk)
    }

    /// Restore a multi-column result from a spill file containing JSON arrays.
    ///
    /// Returns one `ColumnChunk` per column.
    pub fn restore_columns(&self, spill: &SpillFile, num_cols: usize) -> Result<Vec<ColumnChunk>, StorageError> {
        let file = fs::File::open(&spill.path)
            .map_err(|e| StorageError::Spiller(format!("Failed to open spill file {:?}: {e}", spill.path)))?;
        let reader = BufReader::new(file);

        let mut columns: Vec<Vec<Value>> = (0..num_cols).map(|_| Vec::new()).collect();

        for line in reader.lines() {
            let line = line.map_err(|e| StorageError::Spiller(format!("Failed to read spill file: {e}")))?;
            let line = line.trim().to_string();
            if line.is_empty() {
                continue;
            }
            let row: Vec<Value> = serde_json::from_str(&line)
                .map_err(|e| StorageError::Spiller(format!("Failed to deserialize row from spill file: {e}")))?;
            for (col, value) in row.into_iter().enumerate() {
                if col < num_cols {
                    columns[col].push(value);
                }
            }
        }

        Ok(columns.into_iter().map(ColumnChunk::from).collect())
    }

    /// Remove a spill file from disk.
    pub fn cleanup(&self, spill: &SpillFile) -> Result<(), StorageError> {
        fs::remove_file(&spill.path)
            .map_err(|e| StorageError::Spiller(format!("Failed to remove spill file {:?}: {e}", spill.path)))
    }

    /// Remove all spill files in the temp directory.
    pub fn cleanup_all(&self) -> Result<(), StorageError> {
        if self.tmp_dir.exists() {
            fs::remove_dir_all(&self.tmp_dir)
                .map_err(|e| StorageError::Spiller(format!("Failed to remove spill dir {:?}: {e}", self.tmp_dir)))?;
        }
        Ok(())
    }

    /// Check whether a chunk's estimated size exceeds the memory threshold.
    pub fn should_spill(&self, chunk: &ColumnChunk) -> bool {
        if self.memory_threshold == 0 {
            return false;
        }
        Self::estimated_chunk_size(chunk) > self.memory_threshold
    }
}

impl Drop for Spiller {
    fn drop(&mut self) {
        // Best-effort cleanup of the temp directory
        let _ = fs::remove_dir_all(&self.tmp_dir);
    }
}

// ---------------------------------------------------------------------------
// Multi-way stream-merge
// ---------------------------------------------------------------------------

/// A file handle paired with the next buffered row, for streaming merge.
struct MergeCursor {
    /// Source spill file path (for logging).
    _source: PathBuf,
    /// Reader over the JSON-lines file.
    reader: BufReader<fs::File>,
    /// The next buffered row (None if exhausted).
    current: Option<Vec<Value>>,
    /// Column index to use as sort key (for ordering).
    sort_key_col: usize,
}

impl MergeCursor {
    fn new(path: &Path, sort_key_col: usize) -> Result<Self, StorageError> {
        let file = fs::File::open(path)
            .map_err(|e| StorageError::Spiller(format!("Failed to open merge source {:?}: {e}", path)))?;
        let mut reader = BufReader::new(file);
        let current = Self::read_next_row(&mut reader);
        Ok(Self {
            _source: path.to_path_buf(),
            reader,
            current,
            sort_key_col,
        })
    }

    fn read_next_row(reader: &mut BufReader<fs::File>) -> Option<Vec<Value>> {
        let mut line = String::new();
        loop {
            line.clear();
            match reader.read_line(&mut line) {
                Ok(0) => return None, // EOF
                Ok(_) => {
                    let trimmed = line.trim();
                    if trimmed.is_empty() {
                        continue;
                    }
                    match serde_json::from_str::<Vec<Value>>(trimmed) {
                        Ok(row) => return Some(row),
                        Err(_) => {
                            // Try as single value (single-column spill files)
                            match serde_json::from_str::<Value>(trimmed) {
                                Ok(val) => return Some(vec![val]),
                                Err(_) => continue,
                            }
                        }
                    }
                }
                Err(_) => return None,
            }
        }
    }

    fn advance(&mut self) {
        self.current = Self::read_next_row(&mut self.reader);
    }

    fn is_exhausted(&self) -> bool {
        self.current.is_none()
    }

    fn sort_key(&self) -> Option<i64> {
        self.current.as_ref().and_then(|row| {
            if self.sort_key_col < row.len() {
                match &row[self.sort_key_col] {
                    Value::Int64(v) => Some(*v),
                    Value::Int32(v) => Some(*v as i64),
                    Value::Int16(v) => Some(*v as i64),
                    Value::Int8(v) => Some(*v as i64),
                    Value::UInt64(v) => Some(*v as i64),
                    Value::UInt32(v) => Some(*v as i64),
                    Value::UInt16(v) => Some(*v as i64),
                    Value::UInt8(v) => Some(*v as i64),
                    _ => None,
                }
            } else {
                None
            }
        })
    }
}

/// Multi-way streaming merge of multiple spill files.
///
/// Reads N spill files + one optional in-memory buffer, merges them in
/// sort-key order (ascending), and optionally deduplicates by primary key.
pub struct MultiWayStreamMerge {
    cursors: Vec<MergeCursor>,
    /// The in-memory buffer (last "run" to merge).
    in_memory_rows: Vec<Vec<Value>>,
    in_memory_idx: usize,
    sort_key_col: usize,
    dedup: bool,
    /// Last emitted sort key (for dedup).
    last_key: Option<i64>,
}

impl MultiWayStreamMerge {
    /// Create a new multi-way stream merge.
    ///
    /// * `spill_files` — list of spill files to read from disk.
    /// * `in_memory` — optional final in-memory buffer (rows as `Vec<Vec<Value>>`).
    /// * `sort_key_col` — column index to use as the sort/merge key.
    /// * `dedup` — if true, consecutive duplicate sort keys are skipped.
    pub fn new(
        spill_files: &[SpillFile],
        in_memory: Option<Vec<Vec<Value>>>,
        sort_key_col: usize,
        dedup: bool,
    ) -> Result<Self, StorageError> {
        let mut cursors = Vec::new();
        for sf in spill_files {
            if sf.row_count > 0 {
                cursors.push(MergeCursor::new(&sf.path, sort_key_col)?);
            }
        }
        Ok(Self {
            cursors,
            in_memory_rows: in_memory.unwrap_or_default(),
            in_memory_idx: 0,
            sort_key_col,
            dedup,
            last_key: None,
        })
    }

    /// Get the next row from the merge.
    ///
    /// Returns `None` when all sources are exhausted.
    pub fn next_tuple(&mut self) -> Option<Vec<Value>> {
        loop {
            // Find the cursor with the smallest sort key
            let smallest = self.find_smallest();
            let row = match smallest {
                MergeSource::Cursor(idx) => {
                    let row = self.cursors[idx].current.take()?;
                    self.cursors[idx].advance();
                    row
                }
                MergeSource::InMemory => {
                    if self.in_memory_idx < self.in_memory_rows.len() {
                        let row = self.in_memory_rows[self.in_memory_idx].clone();
                        self.in_memory_idx += 1;
                        row
                    } else {
                        return None;
                    }
                }
                MergeSource::None => return None,
            };

            // Dedup: skip if same sort key as last emitted row
            if self.dedup {
                let key = if self.sort_key_col < row.len() {
                    match &row[self.sort_key_col] {
                        Value::Int64(v) => Some(*v),
                        Value::Int32(v) => Some(*v as i64),
                        Value::Int16(v) => Some(*v as i64),
                        Value::Int8(v) => Some(*v as i64),
                        Value::UInt64(v) => Some(*v as i64),
                        Value::UInt32(v) => Some(*v as i64),
                        Value::UInt16(v) => Some(*v as i64),
                        Value::UInt8(v) => Some(*v as i64),
                        _ => None,
                    }
                } else {
                    None
                };

                if let Some(k) = key {
                    if self.last_key == Some(k) {
                        // Duplicate — skip and continue
                        continue;
                    }
                    self.last_key = Some(k);
                }
            }

            return Some(row);
        }
    }

    /// Find the source with the smallest sort key among all cursors and the in-memory buffer.
    fn find_smallest(&self) -> MergeSource {
        let mut best: Option<(MergeSource, i64)> = None;

        for (idx, cursor) in self.cursors.iter().enumerate() {
            if cursor.is_exhausted() {
                continue;
            }
            if let Some(key) = cursor.sort_key() {
                match best {
                    Some((_, best_key)) if key < best_key => {
                        best = Some((MergeSource::Cursor(idx), key));
                    }
                    None => {
                        best = Some((MergeSource::Cursor(idx), key));
                    }
                    _ => {}
                }
            }
        }

        // Check in-memory buffer
        if self.in_memory_idx < self.in_memory_rows.len() {
            let row = &self.in_memory_rows[self.in_memory_idx];
            let key = if self.sort_key_col < row.len() {
                match &row[self.sort_key_col] {
                    Value::Int64(v) => Some(*v),
                    Value::Int32(v) => Some(*v as i64),
                    Value::Int16(v) => Some(*v as i64),
                    Value::Int8(v) => Some(*v as i64),
                    Value::UInt64(v) => Some(*v as i64),
                    Value::UInt32(v) => Some(*v as i64),
                    Value::UInt16(v) => Some(*v as i64),
                    Value::UInt8(v) => Some(*v as i64),
                    _ => None,
                }
            } else {
                None
            };

            if let Some(k) = key {
                match best {
                    Some((_, best_key)) if k < best_key => {
                        best = Some((MergeSource::InMemory, k));
                    }
                    None => {
                        best = Some((MergeSource::InMemory, k));
                    }
                    _ => {}
                }
            }
        }

        best.map_or(MergeSource::None, |(src, _)| src)
    }
}

enum MergeSource {
    Cursor(usize),
    InMemory,
    None,
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_spill_and_restore_single_column() {
        let tmp = tempfile::tempdir().unwrap();
        let spiller = Spiller::new(tmp.path(), 1024);

        let mut chunk = ColumnChunk::new();
        for i in 0..10 {
            chunk.append(Value::Int64(i));
        }
        assert_eq!(chunk.num_values(), 10);

        let spill = spiller.spill(&mut chunk).unwrap().unwrap();
        assert!(chunk.is_empty());
        assert_eq!(spill.row_count, 10);
        assert!(spill.path.exists());

        let restored = spiller.restore(&spill).unwrap();
        assert_eq!(restored.num_values(), 10);
        assert_eq!(restored.get(0), Some(&Value::Int64(0)));
        assert_eq!(restored.get(9), Some(&Value::Int64(9)));

        spiller.cleanup(&spill).unwrap();
    }

    #[test]
    fn test_spill_and_restore_columns() {
        let tmp = tempfile::tempdir().unwrap();
        let spiller = Spiller::new(tmp.path(), 1024);

        let mut chunk0 = ColumnChunk::new();
        let mut chunk1 = ColumnChunk::new();
        for i in 0..5 {
            chunk0.append(Value::Int64(i));
            chunk1.append(Value::String(format!("val_{i}")));
        }

        let spill = spiller.spill_columns(&mut [chunk0, chunk1]).unwrap().unwrap();
        assert_eq!(spill.row_count, 5);

        let restored = spiller.restore_columns(&spill, 2).unwrap();
        assert_eq!(restored.len(), 2);
        assert_eq!(restored[0].num_values(), 5);
        assert_eq!(restored[0].get(0), Some(&Value::Int64(0)));
        assert_eq!(restored[1].get(4), Some(&Value::String("val_4".into())));

        spiller.cleanup(&spill).unwrap();
    }

    #[test]
    fn test_should_spill() {
        let tmp = tempfile::tempdir().unwrap();
        let spiller = Spiller::new(tmp.path(), 128); // Very low threshold

        let mut chunk = ColumnChunk::new();
        // 100 values * 64 bytes = 6400 bytes → should spill
        for i in 0..100 {
            chunk.append(Value::Int64(i));
        }
        assert!(spiller.should_spill(&chunk));

        // Disabled spilling
        let spiller_disabled = Spiller::new(tmp.path(), 0);
        assert!(!spiller_disabled.should_spill(&chunk));
    }

    #[test]
    fn test_spill_empty_chunk() {
        let tmp = tempfile::tempdir().unwrap();
        let spiller = Spiller::new(tmp.path(), 1024);
        let mut chunk = ColumnChunk::new();
        let result = spiller.spill(&mut chunk).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_multi_way_merge() {
        let tmp = tempfile::tempdir().unwrap();
        let spiller = Spiller::new(tmp.path(), 1024);

        // Create 3 spill files with sorted data
        let mut files = Vec::new();

        // File 1: values [1, 4, 7]
        let mut c1 = ColumnChunk::new();
        for v in [1i64, 4, 7] {
            c1.append(Value::Int64(v));
        }
        files.push(spiller.spill(&mut c1).unwrap().unwrap());

        // File 2: values [2, 5, 8]
        let mut c2 = ColumnChunk::new();
        for v in [2i64, 5, 8] {
            c2.append(Value::Int64(v));
        }
        files.push(spiller.spill(&mut c2).unwrap().unwrap());

        // File 3: values [3, 6, 9]
        let mut c3 = ColumnChunk::new();
        for v in [3i64, 6, 9] {
            c3.append(Value::Int64(v));
        }
        files.push(spiller.spill(&mut c3).unwrap().unwrap());

        // Merge with no dedup
        let mut merger = MultiWayStreamMerge::new(&files, None, 0, false).unwrap();
        let mut merged = Vec::new();
        while let Some(row) = merger.next_tuple() {
            merged.push(row[0].clone());
        }

        assert_eq!(merged.len(), 9);
        // Verify sorted order
        for i in 0..9 {
            assert_eq!(merged[i], Value::Int64((i + 1) as i64), "Position {i}");
        }
    }

    #[test]
    fn test_multi_way_merge_with_dedup() {
        let tmp = tempfile::tempdir().unwrap();
        let spiller = Spiller::new(tmp.path(), 1024);

        let mut files = Vec::new();

        // File 1: [1, 2, 3]
        let mut c1 = ColumnChunk::new();
        for v in [1i64, 2, 3] {
            c1.append(Value::Int64(v));
        }
        files.push(spiller.spill(&mut c1).unwrap().unwrap());

        // File 2: [2, 3, 4] (overlaps with file 1)
        let mut c2 = ColumnChunk::new();
        for v in [2i64, 3, 4] {
            c2.append(Value::Int64(v));
        }
        files.push(spiller.spill(&mut c2).unwrap().unwrap());

        // Merge WITH dedup
        let mut merger = MultiWayStreamMerge::new(&files, None, 0, true).unwrap();
        let mut merged = Vec::new();
        while let Some(row) = merger.next_tuple() {
            merged.push(row[0].clone());
        }

        assert_eq!(merged.len(), 4);
        assert_eq!(merged[0], Value::Int64(1));
        assert_eq!(merged[1], Value::Int64(2));
        assert_eq!(merged[2], Value::Int64(3));
        assert_eq!(merged[3], Value::Int64(4));
    }

    #[test]
    fn test_merge_with_in_memory() {
        let tmp = tempfile::tempdir().unwrap();
        let spiller = Spiller::new(tmp.path(), 1024);

        // Spill file: [1, 3, 5]
        let mut c1 = ColumnChunk::new();
        for v in [1i64, 3, 5] {
            c1.append(Value::Int64(v));
        }
        let files = vec![spiller.spill(&mut c1).unwrap().unwrap()];

        // In-memory: [2, 4, 6]
        let in_memory: Vec<Vec<Value>> = vec![vec![Value::Int64(2)], vec![Value::Int64(4)], vec![Value::Int64(6)]];

        let mut merger = MultiWayStreamMerge::new(&files, Some(in_memory), 0, false).unwrap();
        let mut merged = Vec::new();
        while let Some(row) = merger.next_tuple() {
            merged.push(row[0].clone());
        }

        assert_eq!(merged.len(), 6);
        for i in 0..6 {
            assert_eq!(merged[i], Value::Int64((i + 1) as i64));
        }
    }

    #[test]
    fn test_cleanup_all() {
        let tmp = tempfile::tempdir().unwrap();
        let spiller = Spiller::new(tmp.path(), 1024);

        let mut c = ColumnChunk::new();
        c.append(Value::Int64(42));
        let spill = spiller.spill(&mut c).unwrap().unwrap();
        assert!(spill.path.exists());

        spiller.cleanup_all().unwrap();
        assert!(!spill.path.exists());
    }

    #[test]
    fn test_cleanup_on_drop() {
        let tmp = tempfile::tempdir().unwrap();
        let spill_dir = tmp.path().join("spill_test");
        {
            let spiller = Spiller::new(&spill_dir, 1024);
            let mut c = ColumnChunk::new();
            c.append(Value::Int64(42));
            spiller.spill(&mut c).unwrap();
            assert!(spill_dir.exists());
        }
        // After Spiller is dropped, the temp directory should be cleaned up
        assert!(!spill_dir.exists());
    }
}