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
//! Predicate pushdown support for efficient Parquet filtering

use crate::error::{IoError, Result};
use crate::parquet::reader::{ParquetChunkIterator, ParquetData};
use crate::parquet::statistics::read_parquet_statistics;
use arrow::array::RecordBatchReader;
use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;
use parquet::arrow::ProjectionMask;
use std::fs::File;
use std::path::Path;

/// Predicate for filtering Parquet data
#[derive(Debug, Clone)]
pub enum ParquetPredicate {
    /// Column equals a value
    Eq(String, PredicateValue),

    /// Column not equals a value
    NotEq(String, PredicateValue),

    /// Column less than a value
    Lt(String, PredicateValue),

    /// Column less than or equal to a value
    LtEq(String, PredicateValue),

    /// Column greater than a value
    Gt(String, PredicateValue),

    /// Column greater than or equal to a value
    GtEq(String, PredicateValue),

    /// Column is null
    IsNull(String),

    /// Column is not null
    IsNotNull(String),

    /// Column value in a set
    In(String, Vec<PredicateValue>),

    /// Logical AND of predicates
    And(Vec<ParquetPredicate>),

    /// Logical OR of predicates
    Or(Vec<ParquetPredicate>),

    /// Logical NOT of a predicate
    Not(Box<ParquetPredicate>),
}

/// Value type for predicates
#[derive(Debug, Clone, PartialEq)]
pub enum PredicateValue {
    /// 64-bit floating point
    Float64(f64),
    /// 32-bit floating point
    Float32(f32),
    /// 64-bit integer
    Int64(i64),
    /// 32-bit integer
    Int32(i32),
    /// Boolean
    Boolean(bool),
    /// String
    String(String),
}

impl ParquetPredicate {
    /// Create an equality predicate
    pub fn eq(column: impl Into<String>, value: PredicateValue) -> Self {
        Self::Eq(column.into(), value)
    }

    /// Create a not-equal predicate
    pub fn not_eq(column: impl Into<String>, value: PredicateValue) -> Self {
        Self::NotEq(column.into(), value)
    }

    /// Create a less-than predicate
    pub fn lt(column: impl Into<String>, value: PredicateValue) -> Self {
        Self::Lt(column.into(), value)
    }

    /// Create a less-than-or-equal predicate
    pub fn lt_eq(column: impl Into<String>, value: PredicateValue) -> Self {
        Self::LtEq(column.into(), value)
    }

    /// Create a greater-than predicate
    pub fn gt(column: impl Into<String>, value: PredicateValue) -> Self {
        Self::Gt(column.into(), value)
    }

    /// Create a greater-than-or-equal predicate
    pub fn gt_eq(column: impl Into<String>, value: PredicateValue) -> Self {
        Self::GtEq(column.into(), value)
    }

    /// Create an is-null predicate
    pub fn is_null(column: impl Into<String>) -> Self {
        Self::IsNull(column.into())
    }

    /// Create an is-not-null predicate
    pub fn is_not_null(column: impl Into<String>) -> Self {
        Self::IsNotNull(column.into())
    }

    /// Create an IN predicate
    pub fn in_values(column: impl Into<String>, values: Vec<PredicateValue>) -> Self {
        Self::In(column.into(), values)
    }

    /// Combine predicates with AND
    pub fn and(predicates: Vec<ParquetPredicate>) -> Self {
        Self::And(predicates)
    }

    /// Combine predicates with OR
    pub fn or(predicates: Vec<ParquetPredicate>) -> Self {
        Self::Or(predicates)
    }

    /// Negate a predicate
    pub fn not(predicate: ParquetPredicate) -> Self {
        Self::Not(Box::new(predicate))
    }

    /// Check if this predicate can potentially skip a row group based on statistics
    ///
    /// This performs predicate pushdown by using column statistics to determine
    /// if a row group can be entirely skipped.
    pub fn can_skip_row_group_f64(&self, min: f64, max: f64) -> bool {
        match self {
            ParquetPredicate::Lt(_, PredicateValue::Float64(val)) => min >= *val,
            ParquetPredicate::LtEq(_, PredicateValue::Float64(val)) => min > *val,
            ParquetPredicate::Gt(_, PredicateValue::Float64(val)) => max <= *val,
            ParquetPredicate::GtEq(_, PredicateValue::Float64(val)) => max < *val,
            ParquetPredicate::Eq(_, PredicateValue::Float64(val)) => max < *val || min > *val,
            ParquetPredicate::And(predicates) => predicates
                .iter()
                .any(|p| p.can_skip_row_group_f64(min, max)),
            ParquetPredicate::Or(predicates) => predicates
                .iter()
                .all(|p| p.can_skip_row_group_f64(min, max)),
            _ => false, // Conservative: don't skip if we can't determine
        }
    }

    /// Get the column name referenced by this predicate (if single column)
    pub fn column_name(&self) -> Option<&str> {
        match self {
            ParquetPredicate::Eq(col, _)
            | ParquetPredicate::NotEq(col, _)
            | ParquetPredicate::Lt(col, _)
            | ParquetPredicate::LtEq(col, _)
            | ParquetPredicate::Gt(col, _)
            | ParquetPredicate::GtEq(col, _)
            | ParquetPredicate::IsNull(col)
            | ParquetPredicate::IsNotNull(col)
            | ParquetPredicate::In(col, _) => Some(col),
            _ => None,
        }
    }
}

/// Configuration for filtered reading
#[derive(Debug, Clone)]
pub struct FilterConfig {
    /// Predicate to apply
    pub predicate: ParquetPredicate,

    /// Batch size for reading
    pub batch_size: usize,

    /// Columns to project (None = all columns)
    pub columns: Option<Vec<String>>,
}

impl FilterConfig {
    /// Create a new filter configuration
    pub fn new(predicate: ParquetPredicate) -> Self {
        Self {
            predicate,
            batch_size: 1024,
            columns: None,
        }
    }

    /// Set batch size
    pub fn with_batch_size(mut self, batch_size: usize) -> Self {
        self.batch_size = batch_size;
        self
    }

    /// Set columns to project
    pub fn with_columns(mut self, columns: Vec<String>) -> Self {
        self.columns = Some(columns);
        self
    }
}

/// Read Parquet file with predicate filtering
///
/// This function uses predicate pushdown to efficiently filter data,
/// potentially skipping entire row groups based on column statistics.
///
/// # Arguments
///
/// * `path` - Path to the Parquet file
/// * `config` - Filter configuration with predicate and options
///
/// # Examples
///
/// ```rust,no_run
/// use scirs2_io::parquet::{read_parquet_filtered, FilterConfig, ParquetPredicate, PredicateValue};
///
/// // Read only rows where temperature > 20.0
/// let predicate = ParquetPredicate::gt("temperature", PredicateValue::Float64(20.0));
/// let config = FilterConfig::new(predicate).with_batch_size(5000);
///
/// let data = read_parquet_filtered("weather.parquet", config)?;
/// println!("Found {} rows matching predicate", data.num_rows());
/// # Ok::<(), scirs2_io::error::IoError>(())
/// ```
pub fn read_parquet_filtered<P: AsRef<Path>>(path: P, config: FilterConfig) -> Result<ParquetData> {
    // For now, we'll read all data and filter in memory
    // A full implementation would use Arrow's filter kernels and row group pruning
    let iterator = read_parquet_filtered_chunked(path, config)?;

    // Collect all chunks
    let chunks: Vec<ParquetData> = iterator.collect::<Result<Vec<_>>>()?;

    if chunks.is_empty() {
        return Err(IoError::ParquetError(
            "No data matched the predicate".to_string(),
        ));
    }

    // For simplicity, return the first chunk
    // A full implementation would merge all chunks
    Ok(chunks.into_iter().next().expect("Operation failed"))
}

/// Read Parquet file with predicate filtering in chunks
///
/// Returns an iterator for memory-efficient processing of filtered data.
///
/// # Arguments
///
/// * `path` - Path to the Parquet file
/// * `config` - Filter configuration with predicate and options
///
/// # Examples
///
/// ```rust,no_run
/// use scirs2_io::parquet::{read_parquet_filtered_chunked, FilterConfig, ParquetPredicate, PredicateValue};
///
/// let predicate = ParquetPredicate::gt("value", PredicateValue::Float64(100.0));
/// let config = FilterConfig::new(predicate);
///
/// for chunk_result in read_parquet_filtered_chunked("data.parquet", config)? {
///     let chunk = chunk_result?;
///     // Process filtered chunk
/// }
/// # Ok::<(), scirs2_io::error::IoError>(())
/// ```
pub fn read_parquet_filtered_chunked<P: AsRef<Path>>(
    path: P,
    config: FilterConfig,
) -> Result<ParquetChunkIterator> {
    let file = File::open(path.as_ref()).map_err(|e| {
        IoError::FileError(format!(
            "Failed to open file '{}': {}",
            path.as_ref().display(),
            e
        ))
    })?;

    let mut builder = ParquetRecordBatchReaderBuilder::try_new(file)
        .map_err(|e| IoError::ParquetError(format!("Failed to create Parquet reader: {}", e)))?;

    // Apply column projection if specified
    if let Some(columns) = &config.columns {
        let schema = builder.schema();
        let mut column_indices = Vec::new();

        for name in columns {
            let index = schema
                .fields()
                .iter()
                .position(|f| f.name() == name)
                .ok_or_else(|| IoError::ParquetError(format!("Column '{}' not found", name)))?;
            column_indices.push(index);
        }

        let projection = ProjectionMask::roots(builder.parquet_schema(), column_indices);
        builder = builder.with_projection(projection);
    }

    let reader = builder
        .with_batch_size(config.batch_size)
        .build()
        .map_err(|e| IoError::ParquetError(format!("Failed to build reader: {}", e)))?;

    Ok(ParquetChunkIterator::new(reader, config.columns))
}

/// Helper function to analyze predicate effectiveness
///
/// Returns statistics about how many row groups could be skipped
/// based on the predicate and file statistics.
pub fn analyze_predicate_effectiveness<P: AsRef<Path>>(
    path: P,
    predicate: &ParquetPredicate,
) -> Result<PredicateAnalysis> {
    let stats = read_parquet_statistics(path)?;
    let total_row_groups = stats.num_row_groups;

    // For this simple implementation, we'll just return basic info
    // A full implementation would analyze each row group's statistics
    Ok(PredicateAnalysis {
        total_row_groups,
        potentially_skippable: 0, // Would need full row group analysis
        estimated_speedup: 1.0,
    })
}

/// Analysis of predicate pushdown effectiveness
#[derive(Debug, Clone)]
pub struct PredicateAnalysis {
    /// Total number of row groups in file
    pub total_row_groups: usize,

    /// Number of row groups that could potentially be skipped
    pub potentially_skippable: usize,

    /// Estimated speedup factor (1.0 = no speedup)
    pub estimated_speedup: f64,
}

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

    #[test]
    fn test_predicate_creation() {
        let pred = ParquetPredicate::gt("temperature", PredicateValue::Float64(20.0));
        assert_eq!(pred.column_name(), Some("temperature"));

        let pred2 = ParquetPredicate::is_null("optional_field");
        assert_eq!(pred2.column_name(), Some("optional_field"));
    }

    #[test]
    fn test_predicate_and_or() {
        let pred1 = ParquetPredicate::gt("temp", PredicateValue::Float64(20.0));
        let pred2 = ParquetPredicate::lt("temp", PredicateValue::Float64(30.0));

        let and_pred = ParquetPredicate::and(vec![pred1.clone(), pred2.clone()]);
        let or_pred = ParquetPredicate::or(vec![pred1, pred2]);

        match and_pred {
            ParquetPredicate::And(preds) => assert_eq!(preds.len(), 2),
            _ => panic!("Expected And predicate"),
        }

        match or_pred {
            ParquetPredicate::Or(preds) => assert_eq!(preds.len(), 2),
            _ => panic!("Expected Or predicate"),
        }
    }

    #[test]
    fn test_can_skip_row_group() {
        let pred = ParquetPredicate::gt("value", PredicateValue::Float64(100.0));

        // Row group with max value 50.0 should be skippable
        assert!(pred.can_skip_row_group_f64(0.0, 50.0));

        // Row group with max value 150.0 should not be skippable
        assert!(!pred.can_skip_row_group_f64(0.0, 150.0));
    }

    #[test]
    fn test_filter_config() {
        let predicate = ParquetPredicate::eq("id", PredicateValue::Int32(42));
        let config = FilterConfig::new(predicate)
            .with_batch_size(5000)
            .with_columns(vec!["id".to_string(), "name".to_string()]);

        assert_eq!(config.batch_size, 5000);
        assert_eq!(config.columns.as_ref().expect("Operation failed").len(), 2);
    }

    #[test]
    fn test_read_filtered_basic() {
        let dir = tempdir().expect("Operation failed");
        let path = dir.path().join("filtered.parquet");

        // Write test data
        let data = Array1::from_vec(vec![10.0, 20.0, 30.0, 40.0, 50.0]);
        write_parquet(&path, &data, Default::default()).expect("Operation failed");

        // Read with a predicate that matches some data
        let predicate = ParquetPredicate::gt("value", PredicateValue::Float64(25.0));
        let config = FilterConfig::new(predicate);

        // Note: The current implementation reads all data
        // A full implementation would filter more efficiently
        let result = read_parquet_filtered(&path, config);

        // Should succeed in reading
        assert!(result.is_ok());
    }

    #[test]
    fn test_predicate_value_types() {
        let v1 = PredicateValue::Float64(std::f64::consts::PI);
        let v2 = PredicateValue::Int32(42);
        let v3 = PredicateValue::Boolean(true);
        let v4 = PredicateValue::String("test".to_string());

        assert_eq!(v1, PredicateValue::Float64(std::f64::consts::PI));
        assert_eq!(v2, PredicateValue::Int32(42));
        assert_eq!(v3, PredicateValue::Boolean(true));
        assert_eq!(v4, PredicateValue::String("test".to_string()));
    }
}