lightstream 0.6.0

Composable, zero-copy Arrow IPC and native data streaming for Rust with SIMD-aligned I/O, async support, and memory-mapping.
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
// Copyright Peter G. Bower 2025-2026.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Compression roundtrip tests
//!
//! These tests verify that compressed data can be written and read back
//! correctly with full data integrity verification.

use std::sync::Arc;
use tempfile::NamedTempFile;
use tokio::fs::File;

use lightstream::compression::Compression;
use lightstream::enums::IPCMessageProtocol;
use lightstream::models::readers::ipc::file_table::FileTableReader;
use lightstream::models::writers::ipc::table::TableWriter;

use minarrow::ffi::arrow_dtype::ArrowType;
use minarrow::{
    Array, BooleanArray, Buffer, Field, FieldArray, FloatArray, IntegerArray, MaskedArray,
    NumericArray, StringArray, Table, TextArray, Vec64,
};

/// Create a comprehensive test table with multiple data types
fn create_test_table() -> (Table, Vec<Field>) {
    let n_rows = 1000;

    // Integer column
    let int_data: Vec64<i64> = (0..n_rows).map(|i| i as i64 * 2).collect();
    let int_array = Array::NumericArray(NumericArray::Int64(Arc::new(IntegerArray {
        data: Buffer::from(int_data),
        null_mask: None,
    })));
    let int_field = Field {
        name: "id".into(),
        dtype: ArrowType::Int64,
        nullable: false,
        metadata: Default::default(),
    };

    // Float column
    let float_data: Vec64<f64> = (0..n_rows).map(|i| (i as f64) * 3.14159).collect();
    let float_array = Array::NumericArray(NumericArray::Float64(Arc::new(FloatArray {
        data: Buffer::from(float_data),
        null_mask: None,
    })));
    let float_field = Field {
        name: "value".into(),
        dtype: ArrowType::Float64,
        nullable: false,
        metadata: Default::default(),
    };

    // Boolean column
    let bool_data: Vec<bool> = (0..n_rows).map(|i| i % 2 == 0).collect();
    let bool_array = Array::BooleanArray(Arc::new(BooleanArray::from_slice(&bool_data)));
    let bool_field = Field {
        name: "flag".into(),
        dtype: ArrowType::Boolean,
        nullable: false,
        metadata: Default::default(),
    };

    // String column
    let string_data: Vec<String> = (0..n_rows).map(|i| format!("test_string_{}", i)).collect();
    let string_refs: Vec<&str> = string_data.iter().map(|s| s.as_str()).collect();
    let string_array = Array::TextArray(TextArray::String32(Arc::new(StringArray::from_vec(
        string_refs,
        None,
    ))));
    let string_field = Field {
        name: "text".into(),
        dtype: ArrowType::String,
        nullable: false,
        metadata: Default::default(),
    };

    let schema = vec![
        int_field.clone(),
        float_field.clone(),
        bool_field.clone(),
        string_field.clone(),
    ];

    let table = Table {
        name: "compression_roundtrip_test".to_string(),
        n_rows,
        cols: vec![
            FieldArray::new(int_field, int_array),
            FieldArray::new(float_field, float_array),
            FieldArray::new(bool_field, bool_array),
            FieldArray::new(string_field, string_array),
        ],
        ..Default::default()
    };

    (table, schema)
}

/// Read all tables from a FileTableReader
fn read_all_tables(reader: &FileTableReader) -> std::io::Result<Vec<Table>> {
    let num_batches = reader.num_batches();
    let mut tables = Vec::new();
    for i in 0..num_batches {
        tables.push(reader.read_batch(i)?);
    }
    Ok(tables)
}

/// Verify that two tables have identical data
fn verify_tables_equal(original: &Table, roundtrip: &Table) {
    // Note: Table names are not preserved in Arrow IPC format, so we don't check names
    assert_eq!(original.n_rows, roundtrip.n_rows, "Row counts should match");
    assert_eq!(
        original.cols.len(),
        roundtrip.cols.len(),
        "Column counts should match"
    );

    for (i, (orig_col, rt_col)) in original.cols.iter().zip(roundtrip.cols.iter()).enumerate() {
        assert_eq!(
            orig_col.field.name, rt_col.field.name,
            "Column {} name should match",
            i
        );
        assert_eq!(
            orig_col.field.dtype, rt_col.field.dtype,
            "Column {} type should match",
            i
        );
        assert_eq!(
            orig_col.field.nullable, rt_col.field.nullable,
            "Column {} nullable should match",
            i
        );

        // For detailed data verification, we'd need to implement array comparison
        // For now, we verify structural equality which catches most compression issues
        match (&orig_col.array, &rt_col.array) {
            (Array::NumericArray(orig), Array::NumericArray(rt)) => match (orig, rt) {
                (NumericArray::Int64(orig_arr), NumericArray::Int64(rt_arr)) => {
                    assert_eq!(
                        orig_arr.data.len(),
                        rt_arr.data.len(),
                        "Int64 column {} data length should match",
                        i
                    );
                }
                (NumericArray::Float64(orig_arr), NumericArray::Float64(rt_arr)) => {
                    assert_eq!(
                        orig_arr.data.len(),
                        rt_arr.data.len(),
                        "Float64 column {} data length should match",
                        i
                    );
                }
                _ => panic!("Numeric array types should match for column {}", i),
            },
            (Array::BooleanArray(orig), Array::BooleanArray(rt)) => {
                assert_eq!(
                    orig.len(),
                    rt.len(),
                    "Boolean column {} length should match",
                    i
                );
            }
            (Array::TextArray(orig), Array::TextArray(rt)) => match (orig, rt) {
                (TextArray::String32(orig_arr), TextArray::String32(rt_arr)) => {
                    assert_eq!(
                        orig_arr.len(),
                        rt_arr.len(),
                        "String column {} length should match",
                        i
                    );
                }
                _ => panic!("Text array types should match for column {}", i),
            },
            _ => panic!("Array types should match for column {}", i),
        }
    }
}

async fn write_and_read_roundtrip(compression: Option<Compression>) -> (Table, Table) {
    let temp_file = NamedTempFile::new().unwrap();
    let file_path = temp_file.path();

    let (original_table, schema) = create_test_table();

    // Write with compression
    {
        let file = File::create(file_path).await.unwrap();
        let mut writer = TableWriter::new(
            file,
            schema.clone(),
            IPCMessageProtocol::File,
            compression,
        )
        .unwrap();
        writer
            .write_all_tables(vec![original_table.clone()])
            .await
            .unwrap();
    }

    // Read back
    let reader = FileTableReader::open(file_path).unwrap();
    let tables = read_all_tables(&reader).unwrap();

    assert_eq!(tables.len(), 1, "Should read back exactly one table");
    let roundtrip_table = tables.into_iter().next().unwrap();

    (original_table, roundtrip_table)
}

#[tokio::test]
async fn test_compression_none_roundtrip() {
    let (original, roundtrip) = write_and_read_roundtrip(None).await;
    verify_tables_equal(&original, &roundtrip);
    println!("✓ Uncompressed roundtrip test passed");
}

#[cfg(feature = "snappy")]
#[tokio::test]
async fn test_snappy_ipc_write_rejected() {
    // Arrow IPC BodyCompression permits only LZ4_FRAME and ZSTD, so a
    // Snappy IPC write reports Unsupported rather than producing a file
    // other Arrow readers cannot open. Snappy remains available for the
    // formats that support it.
    let temp_file = NamedTempFile::new().unwrap();
    let file = File::create(temp_file.path()).await.unwrap();
    let (table, schema) = create_test_table();
    let mut writer = TableWriter::new(
        file,
        schema,
        IPCMessageProtocol::File,
        Some(Compression::Snappy),
    )
    .unwrap();
    let result = writer.write_all_tables(vec![table]).await;
    let err = result.expect_err("Snappy IPC write should be rejected");
    assert_eq!(err.kind(), std::io::ErrorKind::Unsupported);
}

#[cfg(feature = "zstd")]
#[tokio::test]
async fn test_zstd_compression_roundtrip() {
    let (original, roundtrip) = write_and_read_roundtrip(Some(Compression::Zstd)).await;
    verify_tables_equal(&original, &roundtrip);
    println!("✓ Zstd compression roundtrip test passed");
}

#[tokio::test]
async fn test_compression_multiple_tables_roundtrip() {
    let temp_file = NamedTempFile::new().unwrap();
    let file_path = temp_file.path();

    let (table1, schema) = create_test_table();
    let (mut table2, _) = create_test_table();
    table2.name = "second_table".to_string();

    let original_tables = vec![table1, table2];

    // Write with compression
    {
        let file = File::create(file_path).await.unwrap();
        let mut writer = TableWriter::new(
            file,
            schema.clone(),
            IPCMessageProtocol::File,
            None,
        )
        .unwrap();
        writer
            .write_all_tables(original_tables.clone())
            .await
            .unwrap();
    }

    // Read back
    let reader = FileTableReader::open(file_path).unwrap();
    let roundtrip_tables = read_all_tables(&reader).unwrap();

    assert_eq!(
        roundtrip_tables.len(),
        2,
        "Should read back exactly two tables"
    );

    for (i, (orig, rt)) in original_tables
        .iter()
        .zip(roundtrip_tables.iter())
        .enumerate()
    {
        verify_tables_equal(orig, rt);
        println!("✓ Table {} roundtrip verified", i + 1);
    }

    println!("✓ Multiple tables compression roundtrip test passed");
}

#[tokio::test]
async fn test_compression_large_table_roundtrip() {
    let temp_file = NamedTempFile::new().unwrap();
    let file_path = temp_file.path();

    // Create a larger table to better test compression effectiveness
    let n_rows = 10000;
    let int_data: Vec64<i64> = (0..n_rows).map(|i| (i % 100) as i64).collect(); // Repetitive data for better compression

    let int_array = Array::NumericArray(NumericArray::Int64(Arc::new(IntegerArray {
        data: Buffer::from(int_data),
        null_mask: None,
    })));
    let int_field = Field {
        name: "repeated_values".into(),
        dtype: ArrowType::Int64,
        nullable: false,
        metadata: Default::default(),
    };

    let schema = vec![int_field.clone()];
    let original_table = Table {
        name: "large_compression_test".to_string(),
        n_rows,
        cols: vec![FieldArray::new(int_field, int_array)],
        ..Default::default()
    };

    // Test with compression that should be effective on repetitive data
    {
        let file = File::create(file_path).await.unwrap();
        let mut writer = TableWriter::new(
            file,
            schema.clone(),
            IPCMessageProtocol::File,
            None,
        )
        .unwrap();
        writer
            .write_all_tables(vec![original_table.clone()])
            .await
            .unwrap();
    }

    // Read back
    let reader = FileTableReader::open(file_path).unwrap();
    let tables = read_all_tables(&reader).unwrap();

    assert_eq!(tables.len(), 1, "Should read back exactly one table");
    let roundtrip_table = tables.into_iter().next().unwrap();

    verify_tables_equal(&original_table, &roundtrip_table);
    println!("✓ Large table compression roundtrip test passed");
}

#[tokio::test]
async fn test_stream_protocol_compression_roundtrip() {
    let temp_file = NamedTempFile::new().unwrap();
    let file_path = temp_file.path();

    let (original_table, schema) = create_test_table();

    // Write with Stream protocol and compression
    {
        let file = File::create(file_path).await.unwrap();
        let mut writer = TableWriter::new(
            file,
            schema.clone(),
            IPCMessageProtocol::Stream,
            None,
        )
        .unwrap();
        writer
            .write_all_tables(vec![original_table.clone()])
            .await
            .unwrap();
    }

    // Note: Stream protocol files may need a different reader approach
    // For now, let's verify the file structure is correct
    let mut file = tokio::fs::File::open(file_path).await.unwrap();
    let mut buf = Vec::new();
    use tokio::io::AsyncReadExt;
    file.read_to_end(&mut buf).await.unwrap();

    assert!(!buf.is_empty(), "File should not be empty");
    // Stream protocol starts with 0xFFFF_FFFF
    assert_eq!(
        &buf[..4],
        &[0xFF, 0xFF, 0xFF, 0xFF],
        "Stream protocol magic should be present"
    );

    println!("✓ Stream protocol compression roundtrip structure test passed");
}

#[tokio::test]
async fn test_compression_data_integrity() {
    let temp_file = NamedTempFile::new().unwrap();
    let file_path = temp_file.path();

    // Create a simple integer table where we can verify exact values
    let n_rows = 100;
    let expected_values: Vec<i64> = (0..n_rows).map(|i| i as i64 * 7).collect(); // Multiply by 7 for uniqueness
    let int_data: Vec64<i64> = Vec64::from_slice(&expected_values);

    let int_array = Array::NumericArray(NumericArray::Int64(Arc::new(IntegerArray {
        data: Buffer::from(int_data),
        null_mask: None,
    })));
    let int_field = Field {
        name: "test_values".into(),
        dtype: ArrowType::Int64,
        nullable: false,
        metadata: Default::default(),
    };

    let schema = vec![int_field.clone()];
    let original_table = Table {
        name: "data_integrity_test".to_string(),
        n_rows,
        cols: vec![FieldArray::new(int_field, int_array)],
        ..Default::default()
    };

    // Write with compression
    {
        let file = File::create(file_path).await.unwrap();
        let mut writer = TableWriter::new(
            file,
            schema.clone(),
            IPCMessageProtocol::File,
            None,
        )
        .unwrap();
        writer
            .write_all_tables(vec![original_table.clone()])
            .await
            .unwrap();
    }

    // Read back
    let reader = FileTableReader::open(file_path).unwrap();
    let tables = read_all_tables(&reader).unwrap();

    assert_eq!(tables.len(), 1, "Should read back exactly one table");
    let roundtrip_table = tables.into_iter().next().unwrap();

    // Verify structure
    verify_tables_equal(&original_table, &roundtrip_table);

    // Verify data integrity: extract the actual values and compare
    if let Array::NumericArray(NumericArray::Int64(rt_arr)) = &roundtrip_table.cols[0].array {
        // Compare first few values to verify data integrity
        for i in 0..std::cmp::min(10, expected_values.len()) {
            let expected = expected_values[i];
            let actual = rt_arr.data[i];
            assert_eq!(actual, expected, "Value at index {} should match", i);
        }

        // Verify total length matches
        assert_eq!(
            rt_arr.data.len(),
            expected_values.len(),
            "Data length should match"
        );

        println!(
            "✓ First 10 values verified: {:?}",
            &rt_arr.data[..10].to_vec()
        );
        println!("✓ Expected first 10 values: {:?}", &expected_values[..10]);
    } else {
        panic!("Expected Int64 array in roundtrip table");
    }

    println!("✓ Compression data integrity test passed");
}

/// Roundtrip test for dictionary/categorical columns with zstd compression.
/// Verifies that compressed dictionary batches and record batches with
/// dictionary-encoded columns decompress correctly.
#[cfg(feature = "zstd")]
#[tokio::test]
async fn test_zstd_dictionary_roundtrip() {
    use minarrow::CategoricalArray;
    use minarrow::ffi::arrow_dtype::CategoricalIndexType;

    let temp_file = NamedTempFile::new().unwrap();
    let file_path = temp_file.path();
    let n_rows = 500;

    // Integer column for reference
    let int_data: Vec64<i64> = (0..n_rows).map(|i| i as i64).collect();
    let int_array = Array::NumericArray(NumericArray::Int64(Arc::new(IntegerArray {
        data: Buffer::from(int_data.clone()),
        null_mask: None,
    })));
    let int_field = Field {
        name: "id".into(),
        dtype: ArrowType::Int64,
        nullable: false,
        metadata: Default::default(),
    };

    // Categorical column with dictionary encoding
    let dict_values = vec!["alpha".to_string(), "beta".to_string(), "gamma".to_string()];

    #[cfg(not(feature = "default_categorical_8"))]
    let (dict_field, dict_array) = {
        let indices: Vec64<u32> = (0..n_rows).map(|i| (i % 3) as u32).collect();
        let field = Field {
            name: "category".into(),
            dtype: ArrowType::Dictionary(CategoricalIndexType::UInt32),
            nullable: false,
            metadata: Default::default(),
        };
        let array = Array::TextArray(TextArray::Categorical32(Arc::new(CategoricalArray {
            data: Buffer::from(indices),
            unique_values: Vec64::from(dict_values.clone()),
            null_mask: None,
        })));
        (field, array)
    };

    #[cfg(feature = "default_categorical_8")]
    let (dict_field, dict_array) = {
        let indices: Vec64<u8> = (0..n_rows).map(|i| (i % 3) as u8).collect();
        let field = Field {
            name: "category".into(),
            dtype: ArrowType::Dictionary(CategoricalIndexType::UInt8),
            nullable: false,
            metadata: Default::default(),
        };
        let array = Array::TextArray(TextArray::Categorical8(Arc::new(CategoricalArray {
            data: Buffer::from(indices),
            unique_values: Vec64::from(dict_values.clone()),
            null_mask: None,
        })));
        (field, array)
    };

    let schema = vec![int_field.clone(), dict_field.clone()];
    let original_table = Table::new(
        "dict_compression_test".to_string(),
        Some(vec![
            FieldArray::new(int_field, int_array),
            FieldArray::new(dict_field, dict_array),
        ]),
    );

    // Write with zstd compression
    {
        let file = File::create(file_path).await.unwrap();
        let mut writer = TableWriter::new(
            file,
            schema.clone(),
            IPCMessageProtocol::File,
            Some(Compression::Zstd),
        )
        .unwrap();
        writer.register_dictionary(1, dict_values.clone());
        writer
            .write_all_tables(vec![original_table.clone()])
            .await
            .unwrap();
    }

    // Read back
    let reader = FileTableReader::open(file_path).unwrap();
    let tables = read_all_tables(&reader).unwrap();
    assert_eq!(tables.len(), 1);
    let rt = &tables[0];

    // Verify structure
    assert_eq!(rt.n_rows, n_rows);
    assert_eq!(rt.cols.len(), 2);
    assert_eq!(rt.cols[0].field.name, "id");
    assert_eq!(rt.cols[1].field.name, "category");

    // Verify integer data survived compression roundtrip
    if let Array::NumericArray(NumericArray::Int64(arr)) = &rt.cols[0].array {
        for i in 0..n_rows {
            assert_eq!(arr.data[i], i as i64, "int value mismatch at row {}", i);
        }
    } else {
        panic!("Expected Int64 array for id column");
    }

    // Verify dictionary data survived compression roundtrip
    #[cfg(not(feature = "default_categorical_8"))]
    if let Array::TextArray(TextArray::Categorical32(arr)) = &rt.cols[1].array {
        assert_eq!(
            arr.unique_values.as_slice(),
            &["alpha", "beta", "gamma"],
            "Dictionary values should match"
        );
        for i in 0..n_rows {
            assert_eq!(
                arr.data[i],
                (i % 3) as u32,
                "category index mismatch at row {}",
                i
            );
        }
    } else {
        panic!("Expected Categorical32 array for category column");
    }

    #[cfg(feature = "default_categorical_8")]
    if let Array::TextArray(TextArray::Categorical8(arr)) = &rt.cols[1].array {
        assert_eq!(
            arr.unique_values.as_slice(),
            &["alpha", "beta", "gamma"],
            "Dictionary values should match"
        );
        for i in 0..n_rows {
            assert_eq!(
                arr.data[i],
                (i % 3) as u8,
                "category index mismatch at row {}",
                i
            );
        }
    } else {
        panic!("Expected Categorical8 array for category column");
    }
}