lightstream 0.4.4

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
//! Parquet I/O example demonstrating read and write functionality.
//!
//! This example demonstrates how to:
//! - Write Arrow Tables to Parquet format with various compression options
//! - Read Parquet files back into Arrow Tables
//! - Handle different data types (integers, floats, strings, booleans, dates)
//! - Use compression (None, Snappy, Zstd) for file size optimisation
//! - Verify data integrity through round-trip testing

#[cfg(feature = "parquet")]
use lightstream::{
    compression::Compression,
    models::{
        readers::parquet_reader::read_parquet_table, writers::parquet_writer::write_parquet_table,
    },
};

#[cfg(feature = "parquet")]
use minarrow::ffi::arrow_dtype::ArrowType;
#[cfg(feature = "parquet")]
use minarrow::{
    Array, Bitmask, BooleanArray, Buffer, Field, FieldArray, FloatArray, IntegerArray,
    NumericArray, StringArray, Table, TextArray, Vec64,
};
#[cfg(feature = "parquet")]
use std::fs::File;
#[cfg(feature = "parquet")]
use std::io::{Cursor, Seek, SeekFrom};
#[cfg(feature = "parquet")]
use std::path::Path;
#[cfg(feature = "parquet")]
use std::sync::Arc;
#[cfg(feature = "parquet")]
use tempfile::tempdir;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("Parquet I/O Example");
    println!("==================");

    #[cfg(feature = "parquet")]
    {
        // Create a temporary directory for our examples
        let temp_dir = tempdir()?;

        // Example 1: Basic Parquet write and read
        println!("\n1. Basic Parquet Write and Read");
        let basic_path = temp_dir.path().join("basic_data.parquet");
        basic_parquet_example(&basic_path).await?;

        // Example 2: Compression comparison
        println!("\n2. Compression Comparison");
        let compression_dir = temp_dir.path().join("compression");
        std::fs::create_dir_all(&compression_dir)?;
        compression_example(&compression_dir).await?;

        // Example 3: Complex data types
        println!("\n3. Complex Data Types");
        let complex_path = temp_dir.path().join("complex_data.parquet");
        complex_types_example(&complex_path).await?;

        // Example 4: Large dataset performance
        println!("\n4. Large Dataset Performance");
        let large_path = temp_dir.path().join("large_dataset.parquet");
        large_dataset_example(&large_path).await?;

        println!("\n✓ Parquet I/O example completed successfully!");
    }

    #[cfg(not(feature = "parquet"))]
    {
        println!("\nParquet feature not enabled. Enable with --features parquet");
        println!("Run: cargo run --example parquet_io --features parquet");
    }

    Ok(())
}

#[cfg(feature = "parquet")]
/// Basic Parquet write and read example
async fn basic_parquet_example(file_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
    // Create a simple table
    let table = create_simple_table();
    println!(
        "  Created table '{}' with {} rows",
        table.name, table.n_rows
    );

    // Write to Parquet file
    let start = std::time::Instant::now();
    {
        let mut file = File::create(file_path)?;
        write_parquet_table(&table, &mut file, Compression::None)?;
    }
    let write_time = start.elapsed();

    let file_size = std::fs::metadata(file_path)?.len();
    println!("  Wrote to Parquet in {:?}", write_time);
    println!(
        "  File size: {} bytes ({:.2} KB)",
        file_size,
        file_size as f64 / 1024.0
    );

    // Read back from Parquet file
    let start = std::time::Instant::now();
    let read_table = {
        let mut file = File::open(file_path)?;
        read_parquet_table(&mut file)?
    };
    let read_time = start.elapsed();

    println!("  Read from Parquet in {:?}", read_time);
    println!(
        "  Read table '{}' with {} rows",
        read_table.name, read_table.n_rows
    );

    // Verify data integrity
    verify_simple_table(&table, &read_table)?;
    println!("  ✓ Data integrity verified");

    Ok(())
}

#[cfg(feature = "parquet")]
/// Compression comparison example
async fn compression_example(compression_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
    let table = create_large_string_table();
    println!(
        "  Created table with {} rows for compression testing",
        table.n_rows
    );

    let compressions = vec![
        ("none", Compression::None),
        #[cfg(feature = "snappy")]
        ("snappy", Compression::Snappy),
        #[cfg(feature = "zstd")]
        ("zstd", Compression::Zstd),
    ];

    println!("  Testing different compression algorithms:");

    for (name, compression) in compressions {
        let file_path = compression_dir.join(format!("data_{}.parquet", name));

        // Write with compression
        let start = std::time::Instant::now();
        {
            let mut file = File::create(&file_path)?;
            write_parquet_table(&table, &mut file, compression)?;
        }
        let write_time = start.elapsed();

        let file_size = std::fs::metadata(&file_path)?.len();

        // Read back and verify
        let start = std::time::Instant::now();
        let read_table = {
            let mut file = File::open(&file_path)?;
            read_parquet_table(&mut file)?
        };
        let read_time = start.elapsed();

        println!(
            "    {}: {} bytes, write {:?}, read {:?}",
            name, file_size, write_time, read_time
        );

        // Verify data integrity
        assert_eq!(table.n_rows, read_table.n_rows);
        assert_eq!(table.cols.len(), read_table.cols.len());
    }

    println!("  ✓ All compression methods verified");
    Ok(())
}

#[cfg(feature = "parquet")]
/// Complex data types example
async fn complex_types_example(_file_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
    let table = create_complex_types_table();
    println!(
        "  Created table with complex data types ({} rows)",
        table.n_rows
    );

    // Print schema
    for (i, col) in table.cols.iter().enumerate() {
        println!(
            "    Column {}: {} ({:?})",
            i, col.field.name, col.field.dtype
        );
    }

    // Round-trip test
    let read_table = roundtrip_parquet(&table, Compression::None)?;

    println!("  ✓ Complex types round-trip successful");
    println!(
        "    Original: {} rows, {} columns",
        table.n_rows,
        table.cols.len()
    );
    println!(
        "    Read back: {} rows, {} columns",
        read_table.n_rows,
        read_table.cols.len()
    );

    // Verify some sample data
    if let Array::TextArray(TextArray::String32(str_arr)) = &read_table.cols[2].array {
        if read_table.n_rows > 0 {
            let first_offset = str_arr.offsets[0] as usize;
            let second_offset = str_arr.offsets[1] as usize;
            let first_str = std::str::from_utf8(&str_arr.data[first_offset..second_offset])?;
            println!("    Sample string: '{}'", first_str);
        }
    }

    Ok(())
}

#[cfg(feature = "parquet")]
/// Large dataset performance example
async fn large_dataset_example(file_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
    let table = create_large_performance_table();
    println!("  Created large table with {} rows", table.n_rows);

    // Write with best compression for large files
    let compression = {
        #[cfg(feature = "zstd")]
        {
            Compression::Zstd
        }
        #[cfg(all(feature = "snappy", not(feature = "zstd")))]
        {
            Compression::Snappy
        }
        #[cfg(all(not(feature = "zstd"), not(feature = "snappy")))]
        {
            Compression::None
        }
    };

    let start = std::time::Instant::now();
    {
        let mut file = File::create(file_path)?;
        write_parquet_table(&table, &mut file, compression)?;
    }
    let write_time = start.elapsed();

    let file_size = std::fs::metadata(file_path)?.len();
    println!(
        "  Write performance: {} rows in {:?}",
        table.n_rows, write_time
    );
    println!(
        "  File size: {:.2} MB",
        file_size as f64 / (1024.0 * 1024.0)
    );

    // Read back and measure performance
    let start = std::time::Instant::now();
    let read_table = {
        let mut file = File::open(file_path)?;
        read_parquet_table(&mut file)?
    };
    let read_time = start.elapsed();

    println!(
        "  Read performance: {} rows in {:?}",
        read_table.n_rows, read_time
    );
    println!(
        "  Throughput: {:.0} rows/sec (read)",
        read_table.n_rows as f64 / read_time.as_secs_f64()
    );

    // Verify row count
    assert_eq!(table.n_rows, read_table.n_rows);
    println!("  ✓ Large dataset processing completed");

    Ok(())
}

#[cfg(feature = "parquet")]
/// Create a simple table for basic testing
fn create_simple_table() -> Table {
    let n_rows = 1000;

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

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

    Table {
        name: "simple_test".to_string(),
        n_rows,
        cols: vec![int_field, float_field],
    }
}

#[cfg(feature = "parquet")]
/// Create a table with lots of string data for compression testing
fn create_large_string_table() -> Table {
    let n_rows = 5000;

    // Create repetitive string data that compresses well
    let individual_strings: Vec<String> = (0..n_rows)
        .map(|i| match i % 10 {
            0..=2 => "Common string pattern that appears frequently in the data".to_string(),
            3..=5 => format!("Variable content item number {}", i % 100),
            6..=8 => "Another repeated pattern for compression testing".to_string(),
            _ => format!("Unique entry {}", i),
        })
        .collect();

    let mut str_data = Vec::new();
    let mut offsets = Vec::with_capacity(n_rows + 1);
    offsets.push(0u32);
    for s in &individual_strings {
        str_data.extend_from_slice(s.as_bytes());
        offsets.push(str_data.len() as u32);
    }
    let str_array = Array::TextArray(TextArray::String32(Arc::new(StringArray::new(
        Buffer::from(Vec64::from_slice(&str_data)),
        None,
        Buffer::from(Vec64::from_slice(&offsets)),
    ))));
    let str_field = FieldArray::new(
        Field {
            name: "description".into(),
            dtype: ArrowType::String,
            nullable: false,
            metadata: Default::default(),
        },
        str_array,
    );

    Table {
        name: "compression_test".to_string(),
        n_rows,
        cols: vec![str_field],
    }
}

#[cfg(feature = "parquet")]
/// Create a table with various complex data types
fn create_complex_types_table() -> Table {
    let n_rows = 500;

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

    // Float column
    let float_data: Vec<f32> = (0..n_rows).map(|i| (i as f32) * 0.01 - 25.5).collect();
    let float_array = Array::NumericArray(NumericArray::Float32(Arc::new(FloatArray {
        data: Buffer::from(Vec64::from_slice(&float_data)),
        null_mask: None,
    })));
    let float_field = FieldArray::new(
        Field {
            name: "measurement".into(),
            dtype: ArrowType::Float32,
            nullable: false,
            metadata: Default::default(),
        },
        float_array,
    );

    // String column with varied lengths
    let individual_strings: Vec<String> = (0..n_rows)
        .map(|i| match i % 5 {
            0 => "A".to_string(),
            1 => "Short".to_string(),
            2 => "Medium length string".to_string(),
            3 => "This is a considerably longer string for testing variable-length encoding"
                .to_string(),
            _ => format!("Generated string number {} with some content", i),
        })
        .collect();

    let mut str_data = Vec::new();
    let mut offsets = Vec::with_capacity(n_rows + 1);
    offsets.push(0u32);
    for s in &individual_strings {
        str_data.extend_from_slice(s.as_bytes());
        offsets.push(str_data.len() as u32);
    }
    let str_array = Array::TextArray(TextArray::String32(Arc::new(StringArray::new(
        Buffer::from(Vec64::from_slice(&str_data)),
        None,
        Buffer::from(Vec64::from_slice(&offsets)),
    ))));
    let str_field = FieldArray::new(
        Field {
            name: "variable_text".into(),
            dtype: ArrowType::String,
            nullable: false,
            metadata: Default::default(),
        },
        str_array,
    );

    // Boolean column
    let bool_data: Vec<bool> = (0..n_rows).map(|i| (i * 3 + 1) % 7 < 3).collect();
    let bitmask_bytes = {
        let mut bytes = vec![0u8; (n_rows + 7) / 8];
        for (i, &value) in bool_data.iter().enumerate() {
            if value {
                bytes[i / 8] |= 1 << (i % 8);
            }
        }
        bytes
    };
    let bool_array = Array::BooleanArray(Arc::new(BooleanArray {
        data: Bitmask::from_bytes(&bitmask_bytes, n_rows),
        null_mask: None,
        len: n_rows,
        _phantom: std::marker::PhantomData,
    }));
    let bool_field = FieldArray::new(
        Field {
            name: "flag".into(),
            dtype: ArrowType::Boolean,
            nullable: false,
            metadata: Default::default(),
        },
        bool_array,
    );

    Table {
        name: "complex_types".to_string(),
        n_rows,
        cols: vec![int_field, float_field, str_field, bool_field],
    }
}

#[cfg(feature = "parquet")]
/// Create a large table for performance testing
fn create_large_performance_table() -> Table {
    let n_rows = 50_000;

    // Simple integer sequence
    let int_data: Vec<i32> = (0..n_rows).map(|i| i as i32).collect();
    let int_array = Array::NumericArray(NumericArray::Int32(Arc::new(IntegerArray {
        data: Buffer::from(Vec64::from_slice(&int_data)),
        null_mask: None,
    })));
    let int_field = FieldArray::new(
        Field {
            name: "sequence".into(),
            dtype: ArrowType::Int32,
            nullable: false,
            metadata: Default::default(),
        },
        int_array,
    );

    // Computed float values
    let float_data: Vec<f64> = (0..n_rows)
        .map(|i| (i as f64).sin() * 1000.0 + (i as f64) * 0.001)
        .collect();
    let float_array = Array::NumericArray(NumericArray::Float64(Arc::new(FloatArray {
        data: Buffer::from(Vec64::from_slice(&float_data)),
        null_mask: None,
    })));
    let float_field = FieldArray::new(
        Field {
            name: "computed_value".into(),
            dtype: ArrowType::Float64,
            nullable: false,
            metadata: Default::default(),
        },
        float_array,
    );

    Table {
        name: "large_performance".to_string(),
        n_rows,
        cols: vec![int_field, float_field],
    }
}

#[cfg(feature = "parquet")]
/// Verify that two simple tables have the same data
fn verify_simple_table(
    original: &Table,
    read_back: &Table,
) -> Result<(), Box<dyn std::error::Error>> {
    assert_eq!(original.n_rows, read_back.n_rows, "Row count mismatch");
    assert_eq!(
        original.cols.len(),
        read_back.cols.len(),
        "Column count mismatch"
    );

    // Check integer column
    if let (
        Array::NumericArray(NumericArray::Int32(orig_int)),
        Array::NumericArray(NumericArray::Int32(read_int)),
    ) = (&original.cols[0].array, &read_back.cols[0].array)
    {
        assert_eq!(
            orig_int.data.as_ref(),
            read_int.data.as_ref(),
            "Integer data mismatch"
        );
    }

    // Check float column
    if let (
        Array::NumericArray(NumericArray::Float64(orig_float)),
        Array::NumericArray(NumericArray::Float64(read_float)),
    ) = (&original.cols[1].array, &read_back.cols[1].array)
    {
        // Allow for small floating point differences
        for (orig, read) in orig_float
            .data
            .as_ref()
            .iter()
            .zip(read_float.data.as_ref().iter())
        {
            assert!(
                (orig - read).abs() < 1e-10,
                "Float data mismatch: {} vs {}",
                orig,
                read
            );
        }
    }

    Ok(())
}

#[cfg(feature = "parquet")]
/// Round-trip a table through Parquet format
fn roundtrip_parquet(
    table: &Table,
    compression: Compression,
) -> Result<Table, Box<dyn std::error::Error>> {
    let mut buf = Cursor::new(Vec::new());
    write_parquet_table(table, &mut buf, compression)?;
    buf.seek(SeekFrom::Start(0))?;
    let read_table = read_parquet_table(&mut buf)?;
    Ok(read_table)
}