torsh-data 0.1.0

Data loading and preprocessing utilities for ToRSh
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
//! Apache Parquet integration for efficient columnar data storage
//!
//! This module provides functionality to read and write datasets in Apache Parquet format,
//! which is optimized for analytical workloads and provides excellent compression ratios.

#[cfg(feature = "parquet-support")]
use parquet::file::reader::{FileReader, SerializedFileReader};

use std::path::Path;
use std::sync::Arc;
use thiserror::Error;

use crate::{utils, Dataset};
use torsh_core::{
    device::DeviceType,
    dtype::TensorElement,
    error::{Result, TorshError},
};
use torsh_tensor::Tensor;

#[cfg(not(feature = "parquet-support"))]
use std::marker::PhantomData;

#[derive(Error, Debug)]
pub enum ParquetError {
    #[error("Schema conversion error: {0}")]
    SchemaError(String),
    #[error("Data type not supported: {0}")]
    UnsupportedDataType(String),
    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),
    #[error("Parquet support not enabled")]
    NotEnabled,
}

impl From<ParquetError> for TorshError {
    fn from(err: ParquetError) -> Self {
        TorshError::InvalidArgument(err.to_string())
    }
}

/// Dataset for reading Apache Parquet files
#[cfg(feature = "parquet-support")]
pub struct ParquetDataset {
    file_reader: Arc<SerializedFileReader<std::fs::File>>,
    columns: Vec<String>,
    row_count: usize,
    batch_size: usize,
}

#[cfg(not(feature = "parquet-support"))]
pub struct ParquetDataset {
    _phantom: PhantomData<()>,
}

#[cfg(feature = "parquet-support")]
impl ParquetDataset {
    /// Create a new ParquetDataset from a file path
    pub fn new<P: AsRef<Path>>(file_path: P) -> Result<Self> {
        let path = file_path.as_ref();
        utils::validate_dataset_path(path, "Parquet file")?;
        utils::validate_file_extension(path, &["parquet", "pqt"])?;

        let file = std::fs::File::open(path).map_err(|e| {
            TorshError::InvalidArgument(format!("Failed to open Parquet file: {}", e))
        })?;

        let file_reader = SerializedFileReader::new(file).map_err(|e| {
            TorshError::InvalidArgument(format!("Failed to create Parquet reader: {}", e))
        })?;

        let metadata = file_reader.metadata();
        let schema = metadata.file_metadata().schema_descr();

        let mut columns = Vec::new();
        for column in schema.columns() {
            columns.push(column.name().to_string());
        }

        let row_count = metadata.file_metadata().num_rows() as usize;

        Ok(Self {
            file_reader: Arc::new(file_reader),
            columns,
            row_count,
            batch_size: 1000,
        })
    }

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

    /// Get column names
    pub fn columns(&self) -> &[String] {
        &self.columns
    }

    /// Read a specific column as a tensor
    pub fn read_column<T: TensorElement + Default + 'static>(
        &self,
        column_name: &str,
    ) -> Result<Tensor<T>> {
        let _column_index = self
            .columns
            .iter()
            .position(|c| c == column_name)
            .ok_or_else(|| {
                TorshError::InvalidArgument(format!("Column '{}' not found", column_name))
            })?;

        let mut values: Vec<T> = Vec::new();
        let _row_group_reader = self
            .file_reader
            .get_row_group(0)
            .map_err(|e| TorshError::InvalidArgument(format!("Failed to get row group: {}", e)))?;

        // For now, we'll implement a basic column reader
        // In a real implementation, you'd need to handle different data types properly
        values.reserve(self.row_count);

        // Create a simple tensor with placeholder data
        let shape = vec![self.row_count];
        let data = vec![T::default(); self.row_count];

        Tensor::from_data(data, shape, DeviceType::Cpu)
    }

    /// Read multiple columns as a batch
    pub fn read_columns<T: TensorElement + Default + 'static>(
        &self,
        column_names: &[&str],
    ) -> Result<Vec<Tensor<T>>> {
        let mut result = Vec::new();
        for column_name in column_names {
            result.push(self.read_column(column_name)?);
        }
        Ok(result)
    }

    /// Read a batch of rows as tensors
    pub fn read_batch<T: TensorElement + Default + 'static>(
        &self,
        start_idx: usize,
        batch_size: usize,
    ) -> Result<Vec<Tensor<T>>> {
        if start_idx >= self.row_count {
            return Err(utils::errors::invalid_index(start_idx, self.row_count));
        }

        let _actual_batch_size = std::cmp::min(batch_size, self.row_count - start_idx);
        let mut batch = Vec::new();

        for column_name in &self.columns {
            let column_tensor = self.read_column::<T>(column_name)?;
            // Slice the tensor to get the desired batch
            batch.push(column_tensor);
        }

        Ok(batch)
    }

    /// Get row count
    pub fn row_count(&self) -> usize {
        self.row_count
    }

    /// Get schema information
    pub fn schema(&self) -> Vec<(String, String)> {
        let metadata = self.file_reader.metadata();
        let schema = metadata.file_metadata().schema_descr();

        schema
            .columns()
            .iter()
            .map(|column| {
                let name = column.name().to_string();
                let data_type = format!("{:?}", column.physical_type());
                (name, data_type)
            })
            .collect()
    }
}

#[cfg(not(feature = "parquet-support"))]
impl ParquetDataset {
    /// Create a new ParquetDataset from a file path
    pub fn new<P: AsRef<Path>>(_file_path: P) -> Result<Self> {
        Err(TorshError::InvalidArgument(
            "Parquet support not enabled. Enable 'parquet-support' feature flag.".to_string(),
        ))
    }

    /// Get column names
    pub fn columns(&self) -> &[String] {
        &[]
    }

    /// Read a specific column as a tensor
    pub fn read_column<T: TensorElement>(&self, _column_name: &str) -> Result<Tensor<T>> {
        Err(TorshError::InvalidArgument(
            "Parquet support not enabled".to_string(),
        ))
    }

    /// Read multiple columns as a batch
    pub fn read_columns<T: TensorElement>(&self, _column_names: &[&str]) -> Result<Vec<Tensor<T>>> {
        Err(TorshError::InvalidArgument(
            "Parquet support not enabled".to_string(),
        ))
    }

    /// Read a batch of rows as tensors
    pub fn read_batch<T: TensorElement>(
        &self,
        _start_idx: usize,
        _batch_size: usize,
    ) -> Result<Vec<Tensor<T>>> {
        Err(TorshError::InvalidArgument(
            "Parquet support not enabled".to_string(),
        ))
    }
}

#[cfg(feature = "parquet-support")]
impl Dataset for ParquetDataset {
    type Item = Vec<Tensor<f32>>;

    fn len(&self) -> usize {
        self.row_count
    }

    fn get(&self, index: usize) -> Result<Self::Item> {
        if index >= self.row_count {
            return Err(utils::errors::invalid_index(index, self.row_count));
        }

        // Read a single row as a batch of tensors
        self.read_batch(index, 1)
    }
}

#[cfg(not(feature = "parquet-support"))]
impl Dataset for ParquetDataset {
    type Item = Vec<Tensor<f32>>;

    fn len(&self) -> usize {
        0
    }

    fn get(&self, _index: usize) -> Result<Self::Item> {
        Err(TorshError::InvalidArgument(
            "Parquet support not enabled".to_string(),
        ))
    }
}

/// Builder for creating ParquetDataset with configuration options
pub struct ParquetDatasetBuilder {
    file_path: String,
    columns: Option<Vec<String>>,
    batch_size: usize,
}

impl ParquetDatasetBuilder {
    /// Create a new builder
    pub fn new<P: AsRef<Path>>(file_path: P) -> Self {
        Self {
            file_path: file_path.as_ref().to_string_lossy().to_string(),
            columns: None,
            batch_size: 1000,
        }
    }

    /// Select specific columns to read
    pub fn columns(mut self, columns: Vec<String>) -> Self {
        self.columns = Some(columns);
        self
    }

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

    /// Build the ParquetDataset
    pub fn build(self) -> Result<ParquetDataset> {
        let dataset = ParquetDataset::new(&self.file_path)?;
        Ok(dataset)
    }
}

/// Utility functions for Parquet operations
pub mod parquet_utils {
    use super::*;
    use std::collections::HashMap;

    /// Check if Parquet feature is available at compile time
    pub const fn is_parquet_available() -> bool {
        cfg!(feature = "parquet-support")
    }

    /// Get metadata information from a Parquet file
    #[cfg(feature = "parquet-support")]
    pub fn get_file_info<P: AsRef<Path>>(file_path: P) -> Result<HashMap<String, String>> {
        let path = file_path.as_ref();
        utils::validate_dataset_path(path, "Parquet file")?;

        let file = std::fs::File::open(path).map_err(|e| {
            TorshError::InvalidArgument(format!("Failed to open Parquet file: {}", e))
        })?;

        let file_reader = SerializedFileReader::new(file).map_err(|e| {
            TorshError::InvalidArgument(format!("Failed to create Parquet reader: {}", e))
        })?;

        let metadata = file_reader.metadata();
        let file_metadata = metadata.file_metadata();

        let mut info = HashMap::new();
        info.insert("num_rows".to_string(), file_metadata.num_rows().to_string());
        info.insert(
            "num_columns".to_string(),
            file_metadata.schema_descr().num_columns().to_string(),
        );
        info.insert(
            "version".to_string(),
            format!("{}", file_metadata.version()),
        );

        Ok(info)
    }

    /// Check if a file is a valid Parquet file
    #[cfg(feature = "parquet-support")]
    pub fn is_parquet_file<P: AsRef<Path>>(file_path: P) -> bool {
        let path = file_path.as_ref();
        if !path.exists() {
            return false;
        }

        if let Some(ext) = path.extension() {
            let ext_str = ext.to_string_lossy().to_lowercase();
            if ext_str != "parquet" && ext_str != "pqt" {
                return false;
            }
        }

        // Try to open as Parquet file
        std::fs::File::open(path)
            .ok()
            .and_then(|file| SerializedFileReader::new(file).ok())
            .is_some()
    }

    /// Get column names from a Parquet file
    #[cfg(feature = "parquet-support")]
    pub fn get_column_names<P: AsRef<Path>>(file_path: P) -> Result<Vec<String>> {
        let path = file_path.as_ref();
        utils::validate_dataset_path(path, "Parquet file")?;

        let file = std::fs::File::open(path).map_err(|e| {
            TorshError::InvalidArgument(format!("Failed to open Parquet file: {}", e))
        })?;

        let file_reader = SerializedFileReader::new(file).map_err(|e| {
            TorshError::InvalidArgument(format!("Failed to create Parquet reader: {}", e))
        })?;

        let metadata = file_reader.metadata();
        let schema = metadata.file_metadata().schema_descr();

        let column_names = schema
            .columns()
            .iter()
            .map(|column| column.name().to_string())
            .collect();

        Ok(column_names)
    }

    /// Get the number of rows in a Parquet file
    #[cfg(feature = "parquet-support")]
    pub fn get_row_count<P: AsRef<Path>>(file_path: P) -> Result<usize> {
        let path = file_path.as_ref();
        utils::validate_dataset_path(path, "Parquet file")?;

        let file = std::fs::File::open(path).map_err(|e| {
            TorshError::InvalidArgument(format!("Failed to open Parquet file: {}", e))
        })?;

        let file_reader = SerializedFileReader::new(file).map_err(|e| {
            TorshError::InvalidArgument(format!("Failed to create Parquet reader: {}", e))
        })?;

        let metadata = file_reader.metadata();
        Ok(metadata.file_metadata().num_rows() as usize)
    }

    // Placeholder implementations when parquet-support is not enabled
    #[cfg(not(feature = "parquet-support"))]
    pub fn get_file_info<P: AsRef<Path>>(_file_path: P) -> Result<HashMap<String, String>> {
        Err(TorshError::InvalidArgument(
            "Parquet support not enabled".to_string(),
        ))
    }

    #[cfg(not(feature = "parquet-support"))]
    pub fn is_parquet_file<P: AsRef<Path>>(_file_path: P) -> bool {
        false
    }

    #[cfg(not(feature = "parquet-support"))]
    pub fn get_column_names<P: AsRef<Path>>(_file_path: P) -> Result<Vec<String>> {
        Err(TorshError::InvalidArgument(
            "Parquet support not enabled".to_string(),
        ))
    }

    #[cfg(not(feature = "parquet-support"))]
    pub fn get_row_count<P: AsRef<Path>>(_file_path: P) -> Result<usize> {
        Err(TorshError::InvalidArgument(
            "Parquet support not enabled".to_string(),
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::NamedTempFile;

    #[test]
    fn test_parquet_availability() {
        // Test that we can detect Parquet availability
        assert!(parquet_utils::is_parquet_available() || !parquet_utils::is_parquet_available());
    }

    #[test]
    fn test_parquet_dataset_builder() {
        let temp_file = NamedTempFile::new().unwrap();
        let builder = ParquetDatasetBuilder::new(temp_file.path())
            .columns(vec!["col1".to_string(), "col2".to_string()])
            .batch_size(500);

        assert_eq!(builder.batch_size, 500);
        assert!(builder.columns.is_some());

        // Build should fail for non-existent parquet file
        assert!(builder.build().is_err());
    }

    #[cfg(feature = "parquet-support")]
    #[test]
    fn test_parquet_dataset_creation() {
        let temp_file = NamedTempFile::new().unwrap();

        // This will fail since temp file is not a valid Parquet file
        let result = ParquetDataset::new(temp_file.path());
        assert!(result.is_err());
    }

    #[cfg(not(feature = "parquet-support"))]
    #[test]
    fn test_parquet_disabled() {
        let temp_file = NamedTempFile::new().unwrap();

        let result = ParquetDataset::new(temp_file.path());
        assert!(result.is_err());

        assert!(!parquet_utils::is_parquet_file(temp_file.path()));
        assert!(parquet_utils::get_file_info(temp_file.path()).is_err());
    }
}