scirs2-io 0.4.2

Input/Output utilities module for SciRS2 (scirs2-io)
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
//! Apache Parquet file format support
//!
//! This module provides reading and writing of Apache Parquet files,
//! leveraging the Apache Arrow ecosystem for efficient columnar data storage.
//!
//! # Features
//!
//! - **Columnar storage**: Efficient storage and retrieval of large datasets
//! - **Compression**: Multiple compression codecs (Snappy, Gzip, LZ4, ZSTD, Brotli)
//! - **Schema handling**: Automatic schema inference and validation
//! - **Python interoperability**: Compatible with Pandas, Polars, PyArrow
//! - **Column selection**: Read specific columns for reduced I/O
//! - **Chunked reading**: Memory-efficient streaming for large files
//! - **Column statistics**: Fast metadata access without reading data
//! - **Predicate pushdown**: Efficient filtering with row group pruning
//!
//! # Examples
//!
//! ## Basic Reading
//!
//! ```rust,no_run
//! use scirs2_io::parquet::read_parquet;
//!
//! let data = read_parquet("data.parquet")?;
//! println!("Columns: {:?}", data.column_names());
//! # Ok::<(), scirs2_io::error::IoError>(())
//! ```
//!
//! ## Column Selection
//!
//! ```rust,no_run
//! use scirs2_io::parquet::read_parquet_columns;
//!
//! let data = read_parquet_columns("data.parquet", &["revenue", "date"])?;
//! # Ok::<(), scirs2_io::error::IoError>(())
//! ```
//!
//! ## Writing with Compression
//!
//! ```rust,no_run
//! use scirs2_io::parquet::{write_parquet, ParquetWriteOptions, CompressionCodec};
//! use scirs2_core::ndarray::Array1;
//!
//! let data = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0]);
//! let options = ParquetWriteOptions {
//!     compression: CompressionCodec::Brotli,
//!     ..Default::default()
//! };
//! write_parquet("output.parquet", &data, options)?;
//! # Ok::<(), scirs2_io::error::IoError>(())
//! ```
//!
//! ## Chunked Reading for Large Files
//!
//! ```rust,no_run
//! use scirs2_io::parquet::read_parquet_chunked;
//!
//! // Process large file in memory-efficient chunks
//! for chunk_result in read_parquet_chunked("large_data.parquet", 10000)? {
//!     let chunk = chunk_result?;
//!     println!("Processing {} rows", chunk.num_rows());
//!     // Process chunk without loading entire file into memory
//! }
//! # Ok::<(), scirs2_io::error::IoError>(())
//! ```
//!
//! ## Column Statistics
//!
//! ```rust,no_run
//! use scirs2_io::parquet::read_parquet_statistics;
//!
//! // Read metadata without loading data
//! let stats = read_parquet_statistics("data.parquet")?;
//! println!("File has {} rows in {} row groups", stats.num_rows, stats.num_row_groups);
//!
//! if let Some(col_stats) = stats.column_stats("temperature") {
//!     println!("Temperature column has {} values", col_stats.num_values);
//!     if let (Some(min), Some(max)) = (col_stats.min_f64(), col_stats.max_f64()) {
//!         println!("Range: {} to {}", min, max);
//!     }
//! }
//! # Ok::<(), scirs2_io::error::IoError>(())
//! ```
//!
//! ## Predicate Pushdown
//!
//! ```rust,no_run
//! use scirs2_io::parquet::{read_parquet_filtered, FilterConfig, ParquetPredicate, PredicateValue};
//!
//! // Filter data efficiently using predicates
//! let predicate = ParquetPredicate::and(vec![
//!     ParquetPredicate::gt("temperature", PredicateValue::Float64(20.0)),
//!     ParquetPredicate::lt("temperature", PredicateValue::Float64(30.0)),
//! ]);
//!
//! let config = FilterConfig::new(predicate)
//!     .with_batch_size(5000)
//!     .with_columns(vec!["temperature".to_string(), "humidity".to_string()]);
//!
//! let data = read_parquet_filtered("weather.parquet", config)?;
//! println!("Found {} rows matching predicate", data.num_rows());
//! # Ok::<(), scirs2_io::error::IoError>(())
//! ```

use crate::error::{IoError, Result};

pub mod conversion;
pub mod options;
pub mod predicates;
pub mod reader;
pub mod schema;
pub mod statistics;
pub mod writer;

pub use conversion::{arrow_to_ndarray, ndarray_to_arrow};
pub use options::{CompressionCodec, ParquetVersion, ParquetWriteOptions};
pub use predicates::{
    analyze_predicate_effectiveness, read_parquet_filtered, read_parquet_filtered_chunked,
    FilterConfig, ParquetPredicate, PredicateAnalysis, PredicateValue,
};
pub use reader::{
    read_parquet, read_parquet_chunked, read_parquet_chunked_columns, read_parquet_columns,
    ParquetChunkIterator, ParquetData, ParquetReader,
};
pub use schema::{infer_arrow_schema, ParquetSchema};
pub use statistics::{read_parquet_statistics, ColumnStatistics, ParquetFileStatistics};
pub use writer::{write_parquet, write_parquet_with_name, ParquetWriter};

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::Array1;
    use std::fs;
    use tempfile::tempdir;

    #[test]
    fn test_parquet_roundtrip_f64() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test.parquet");

        // Create test data
        let data = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0]);

        // Write
        write_parquet(&path, &data, Default::default()).expect("Test I/O operation failed");

        // Read
        let loaded = read_parquet(&path).expect("Test I/O operation failed");

        // Verify
        assert_eq!(loaded.column_names().len(), 1);
        let column = loaded
            .get_column_f64("value")
            .expect("Test I/O operation failed");
        assert_eq!(column.len(), 5);
        assert_eq!(column[0], 1.0);
        assert_eq!(column[4], 5.0);
    }

    #[test]
    fn test_compression_codecs() {
        let dir = tempdir().expect("Test I/O operation failed");
        let data = Array1::from_vec((0..100).map(|x| x as f64).collect::<Vec<_>>());

        for codec in [
            CompressionCodec::Uncompressed,
            CompressionCodec::Snappy,
            CompressionCodec::Gzip,
        ] {
            let path = dir.path().join(format!("test_{:?}.parquet", codec));
            let options = ParquetWriteOptions {
                compression: codec,
                ..Default::default()
            };

            write_parquet(&path, &data, options).expect("Test I/O operation failed");
            let loaded = read_parquet(&path).expect("Test I/O operation failed");

            assert_eq!(loaded.num_rows(), 100);
        }
    }

    #[test]
    fn test_parquet_roundtrip_i32() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_i32.parquet");

        let data = Array1::from_vec(vec![10i32, 20, 30, 40, 50]);

        write_parquet(&path, &data, Default::default()).expect("Test I/O operation failed");
        let loaded = read_parquet(&path).expect("Test I/O operation failed");

        assert_eq!(loaded.num_rows(), 5);
        let column = loaded
            .get_column_i32("value")
            .expect("Test I/O operation failed");
        assert_eq!(column[0], 10);
        assert_eq!(column[4], 50);
    }

    #[test]
    fn test_parquet_roundtrip_f32() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_f32.parquet");

        let data = Array1::from_vec(vec![1.5f32, 2.5, 3.5, 4.5]);

        write_parquet(&path, &data, Default::default()).expect("Test I/O operation failed");
        let loaded = read_parquet(&path).expect("Test I/O operation failed");

        let column = loaded
            .get_column_f32("value")
            .expect("Test I/O operation failed");
        assert_eq!(column.len(), 4);
        assert!((column[0] - 1.5).abs() < 1e-6);
    }

    #[test]
    fn test_custom_column_name() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_named.parquet");

        let data = Array1::from_vec(vec![1.0, 2.0, 3.0]);

        write_parquet_with_name(&path, &data, "temperature", Default::default())
            .expect("Test I/O operation failed");
        let loaded = read_parquet(&path).expect("Test I/O operation failed");

        assert_eq!(loaded.column_names(), vec!["temperature"]);
        let column = loaded
            .get_column_f64("temperature")
            .expect("Test I/O operation failed");
        assert_eq!(column.len(), 3);
    }

    #[test]
    fn test_large_dataset() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_large.parquet");

        let data: Vec<f64> = (0..10000).map(|x| x as f64 * 0.1).collect();
        let array = Array1::from_vec(data);

        write_parquet(&path, &array, Default::default()).expect("Test I/O operation failed");
        let loaded = read_parquet(&path).expect("Test I/O operation failed");

        assert_eq!(loaded.num_rows(), 10000);
        let column = loaded
            .get_column_f64("value")
            .expect("Test I/O operation failed");
        assert!((column[0] - 0.0).abs() < 1e-10);
        assert!((column[9999] - 999.9).abs() < 1e-6);
    }

    #[test]
    fn test_schema_introspection() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_schema.parquet");

        let data = Array1::from_vec(vec![1.0, 2.0, 3.0]);

        write_parquet_with_name(&path, &data, "measurements", Default::default())
            .expect("Test I/O operation failed");
        let loaded = read_parquet(&path).expect("Test I/O operation failed");

        let schema = loaded.schema();
        assert_eq!(schema.num_columns(), 1);
        assert_eq!(schema.column_names(), vec!["measurements"]);
        assert!(schema.field("measurements").is_some());
        assert!(schema.field("nonexistent").is_none());
    }

    #[test]
    fn test_options_builder_pattern() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_builder.parquet");

        let data = Array1::from_vec(vec![1.0, 2.0, 3.0]);
        let options = ParquetWriteOptions::with_compression(CompressionCodec::Brotli)
            .with_row_group_size(500)
            .with_dictionary(false);

        write_parquet(&path, &data, options).expect("Test I/O operation failed");
        assert!(path.exists());
    }

    #[test]
    fn test_error_invalid_column() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_error.parquet");

        let data = Array1::from_vec(vec![1.0, 2.0, 3.0]);
        write_parquet(&path, &data, Default::default()).expect("Test I/O operation failed");

        let loaded = read_parquet(&path).expect("Test I/O operation failed");

        // Attempt to access non-existent column
        let result = loaded.get_column_f64("nonexistent");
        assert!(result.is_err());

        // Attempt to access with wrong type
        let result = loaded.get_column_i32("value");
        assert!(result.is_err());
    }

    #[test]
    fn test_data_accuracy() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_accuracy.parquet");

        let original = Array1::from_vec(vec![
            1.23456789,
            2.98765432,
            std::f64::consts::PI,
            4.71238898,
            5.55555555,
        ]);

        write_parquet(&path, &original, Default::default()).expect("Test I/O operation failed");
        let loaded = read_parquet(&path).expect("Test I/O operation failed");
        let recovered = loaded
            .get_column_f64("value")
            .expect("Test I/O operation failed");

        assert_eq!(recovered.len(), original.len());
        for (a, b) in original.iter().zip(recovered.iter()) {
            assert!((a - b).abs() < 1e-10, "Value mismatch: {} vs {}", a, b);
        }
    }

    #[test]
    fn test_chunked_reading() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_chunked.parquet");

        // Create a dataset with 100 rows
        let data: Vec<f64> = (0..100).map(|x| x as f64).collect();
        let array = Array1::from_vec(data);

        write_parquet(&path, &array, Default::default()).expect("Test I/O operation failed");

        // Read in chunks of 10 rows
        let chunks: Vec<_> = read_parquet_chunked(&path, 10)
            .expect("Operation failed")
            .collect::<Result<Vec<_>>>()
            .expect("Test I/O operation failed");

        // Should have 10 chunks
        assert_eq!(chunks.len(), 10);

        // Verify total rows
        let total_rows: usize = chunks.iter().map(|c| c.num_rows()).sum();
        assert_eq!(total_rows, 100);

        // Verify first chunk
        let first_chunk = &chunks[0];
        let first_values = first_chunk
            .get_column_f64("value")
            .expect("Test I/O operation failed");
        assert_eq!(first_values[0], 0.0);
        assert_eq!(first_values[9], 9.0);
    }

    #[test]
    fn test_chunked_column_selection() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_chunked_cols.parquet");

        // Write data
        let data = Array1::from_vec((0..50).map(|x| x as f64).collect::<Vec<_>>());
        write_parquet_with_name(&path, &data, "column_a", Default::default())
            .expect("Test I/O operation failed");

        // Read in chunks with column selection
        let chunks: Vec<_> = read_parquet_chunked_columns(&path, &["column_a"], 10)
            .expect("Operation failed")
            .collect::<Result<Vec<_>>>()
            .expect("Test I/O operation failed");

        assert_eq!(chunks.len(), 5); // 50 rows / 10 per chunk = 5 chunks
        assert_eq!(chunks[0].num_columns(), 1);
        assert_eq!(chunks[0].column_names(), vec!["column_a"]);
    }

    #[test]
    fn test_chunked_reading_schema() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_schema_chunk.parquet");

        let data = Array1::from_vec(vec![1.0, 2.0, 3.0]);
        write_parquet_with_name(&path, &data, "test_col", Default::default())
            .expect("Test I/O operation failed");

        let mut iterator = read_parquet_chunked(&path, 2).expect("Test I/O operation failed");
        let schema = iterator.schema();

        assert_eq!(schema.num_columns(), 1);
        assert_eq!(schema.column_names(), vec!["test_col"]);

        // Consume the iterator to verify it works
        let chunks: Vec<_> = iterator
            .collect::<Result<Vec<_>>>()
            .expect("Test I/O operation failed");
        assert_eq!(chunks.len(), 2); // 3 rows with chunk size 2 = 2 chunks
    }

    #[test]
    fn test_chunked_memory_efficiency() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_memory.parquet");

        // Create a large dataset
        let data: Vec<f64> = (0..10000).map(|x| x as f64).collect();
        let array = Array1::from_vec(data);
        write_parquet(&path, &array, Default::default()).expect("Test I/O operation failed");

        // Read in small chunks to test memory efficiency
        let mut row_count = 0;
        let mut chunk_count = 0;

        for chunk_result in read_parquet_chunked(&path, 100).expect("Operation failed") {
            let chunk = chunk_result.expect("Test I/O operation failed");
            row_count += chunk.num_rows();
            chunk_count += 1;

            // Each chunk should have at most 100 rows
            assert!(chunk.num_rows() <= 100);
        }

        assert_eq!(row_count, 10000);
        assert_eq!(chunk_count, 100); // 10000 / 100 = 100 chunks
    }

    #[test]
    fn test_empty_chunked_reading() {
        // Test that non-existent file returns proper error
        let result = read_parquet_chunked("nonexistent.parquet", 10);
        assert!(result.is_err());
    }

    #[test]
    fn test_chunked_single_row() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_single_row.parquet");

        let data = Array1::from_vec(vec![42.0]);
        write_parquet(&path, &data, Default::default()).expect("Test I/O operation failed");

        let chunks: Vec<_> = read_parquet_chunked(&path, 10)
            .expect("Operation failed")
            .collect::<Result<Vec<_>>>()
            .expect("Test I/O operation failed");

        assert_eq!(chunks.len(), 1);
        assert_eq!(chunks[0].num_rows(), 1);

        let values = chunks[0]
            .get_column_f64("value")
            .expect("Test I/O operation failed");
        assert_eq!(values[0], 42.0);
    }

    #[test]
    fn test_statistics_api() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_stats_api.parquet");

        // Write data with known range
        let data = Array1::from_vec(vec![10.0, 20.0, 30.0, 40.0, 50.0]);
        write_parquet(&path, &data, Default::default()).expect("Test I/O operation failed");

        // Read statistics
        let stats = read_parquet_statistics(&path).expect("Test I/O operation failed");

        assert_eq!(stats.num_rows, 5);
        assert!(stats.has_statistics());
        assert!(stats.column_stats("value").is_some());

        let col_stats = stats
            .column_stats("value")
            .expect("Test I/O operation failed");
        assert_eq!(col_stats.num_values, 5);
        assert!(!col_stats.has_nulls());
    }

    #[test]
    fn test_predicate_filtering() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_predicate.parquet");

        // Write data
        let data = Array1::from_vec(vec![5.0, 15.0, 25.0, 35.0, 45.0]);
        write_parquet(&path, &data, Default::default()).expect("Test I/O operation failed");

        // Read with predicate
        let predicate = ParquetPredicate::gt("value", PredicateValue::Float64(20.0));
        let config = FilterConfig::new(predicate);

        let result = read_parquet_filtered(&path, config);
        assert!(result.is_ok());

        let data = result.expect("Test I/O operation failed");
        assert!(data.num_rows() > 0);
    }

    #[test]
    fn test_predicate_chunked_filtering() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_pred_chunked.parquet");

        // Write test data
        let data = Array1::from_vec((0..100).map(|x| x as f64).collect::<Vec<_>>());
        write_parquet(&path, &data, Default::default()).expect("Test I/O operation failed");

        // Read with predicate in chunks
        let predicate = ParquetPredicate::gt("value", PredicateValue::Float64(50.0));
        let config = FilterConfig::new(predicate).with_batch_size(10);

        let chunks: Vec<_> = read_parquet_filtered_chunked(&path, config)
            .expect("Operation failed")
            .collect::<Result<Vec<_>>>()
            .expect("Test I/O operation failed");

        // Should get multiple chunks
        assert!(!chunks.is_empty());
    }

    #[test]
    fn test_combined_statistics_and_predicates() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_combined.parquet");

        // Write data
        let data = Array1::from_vec(vec![1.0, 5.0, 10.0, 15.0, 20.0]);
        write_parquet(&path, &data, Default::default()).expect("Test I/O operation failed");

        // First, check statistics
        let stats = read_parquet_statistics(&path).expect("Test I/O operation failed");
        assert_eq!(stats.num_rows, 5);

        // Then filter based on statistics
        let predicate = ParquetPredicate::gt("value", PredicateValue::Float64(8.0));
        let config = FilterConfig::new(predicate);

        let filtered = read_parquet_filtered(&path, config).expect("Test I/O operation failed");
        assert!(filtered.num_rows() > 0);
    }

    #[test]
    fn test_statistics_with_compression() {
        let dir = tempdir().expect("Test I/O operation failed");
        let path = dir.path().join("test_stats_compressed.parquet");

        // Write compressed data
        let data = Array1::from_vec((0..50).map(|x| x as f64).collect::<Vec<_>>());
        let options = ParquetWriteOptions {
            compression: CompressionCodec::Brotli,
            enable_statistics: true,
            ..Default::default()
        };
        write_parquet(&path, &data, options).expect("Test I/O operation failed");

        // Statistics should work with compressed data
        let stats = read_parquet_statistics(&path).expect("Test I/O operation failed");
        assert_eq!(stats.num_rows, 50);
        assert!(stats.has_statistics());
    }
}