rust-data-processing 0.1.6

Schema-first ingestion (CSV, JSON, Parquet, Excel) into an in-memory DataSet, plus Polars-backed pipelines, SQL, profiling, validation, and map/reduce-style processing.
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
use std::path::Path;

use calamine::{Data, Reader, open_workbook_auto};

use crate::error::{IngestionError, IngestionResult};
use crate::types::{DataSet, DataType, Schema, Value};

/// Ingest an Excel document (`.xlsx`, `.xls`, `.ods`, etc.) into an in-memory `DataSet`.
///
/// Behavior:
/// - Picks `sheet_name` if provided; otherwise uses the first sheet in the workbook
/// - Detects the first non-empty row as the header row
/// - Validates that all schema fields exist as headers
/// - Reads remaining rows and converts cells into typed `Value`s
pub fn ingest_excel_from_path(
    path: impl AsRef<Path>,
    sheet_name: Option<&str>,
    schema: &Schema,
) -> IngestionResult<DataSet> {
    let sheets: Option<Vec<&str>> = sheet_name.map(|s| vec![s]);
    ingest_excel_workbook_from_path(path, sheets.as_deref(), schema)
}

/// Ingest multiple sheets from an Excel workbook and concatenate all rows into one `DataSet`.
///
/// - If `sheet_names` is `None`, ingests **all sheets** in workbook order.
/// - If `sheet_names` is `Some(&[...])`, ingests only those sheets (in the provided order).
///
/// Assumption for 1.1.3.1: all tabs share the same header schema.
pub fn ingest_excel_workbook_from_path(
    path: impl AsRef<Path>,
    sheet_names: Option<&[&str]>,
    schema: &Schema,
) -> IngestionResult<DataSet> {
    let mut workbook = open_workbook_auto(path)?;

    let sheets: Vec<String> = match sheet_names {
        Some(names) => names.iter().map(|s| s.to_string()).collect(),
        None => workbook.sheet_names().to_vec(),
    };
    if sheets.is_empty() {
        return Err(IngestionError::SchemaMismatch {
            message: "workbook has no sheets".to_string(),
        });
    }

    let mut all_rows: Vec<Vec<Value>> = Vec::new();
    for sheet in sheets {
        let range = workbook.worksheet_range(&sheet)?;
        let mut sheet_rows = ingest_sheet_range(&sheet, &range, schema)?;
        all_rows.append(&mut sheet_rows);
    }

    Ok(DataSet::new(schema.clone(), all_rows))
}

/// Infer a schema from an Excel workbook by reading the header row and scanning cell types.
///
/// Heuristic:
/// - Column names come from the first non-empty row (same as ingestion).
/// - Types are inferred as one of: Bool / Int64 / Float64 / Utf8
/// - Mixed-type columns fall back to Utf8.
pub fn infer_excel_schema_from_path(
    path: impl AsRef<Path>,
    sheet_name: Option<&str>,
) -> IngestionResult<Schema> {
    let sheets: Option<Vec<&str>> = sheet_name.map(|s| vec![s]);
    infer_excel_schema_workbook_from_path(path, sheets.as_deref())
}

/// Infer a schema from multiple sheets of a workbook.
///
/// Assumption: all selected sheets share the same header schema.
pub fn infer_excel_schema_workbook_from_path(
    path: impl AsRef<Path>,
    sheet_names: Option<&[&str]>,
) -> IngestionResult<Schema> {
    let mut workbook = open_workbook_auto(path)?;

    let sheets: Vec<String> = match sheet_names {
        Some(names) => names.iter().map(|s| s.to_string()).collect(),
        None => workbook.sheet_names().to_vec(),
    };
    if sheets.is_empty() {
        return Err(IngestionError::SchemaMismatch {
            message: "workbook has no sheets".to_string(),
        });
    }

    // Infer from the first selected sheet only (header schema assumed consistent).
    let sheet = &sheets[0];
    let range = workbook.worksheet_range(sheet)?;
    infer_schema_from_sheet_range(sheet, &range).map_err(|e| wrap_schema_err_with_sheet(sheet, e))
}

fn ingest_sheet_range(
    sheet: &str,
    range: &calamine::Range<Data>,
    schema: &Schema,
) -> IngestionResult<Vec<Vec<Value>>> {
    let (header_row_idx, col_idxs, header_cells) =
        build_header_projection(range, schema).map_err(|e| wrap_schema_err_with_sheet(sheet, e))?;

    let mut rows: Vec<Vec<Value>> = Vec::new();
    for (idx0, row) in range.rows().enumerate() {
        if idx0 <= header_row_idx {
            continue;
        }

        // Report 1-based row number (Excel-like).
        let user_row = idx0 + 1;

        let mut out_row: Vec<Value> = Vec::with_capacity(schema.fields.len());
        for (field, &col_idx) in schema.fields.iter().zip(col_idxs.iter()) {
            let cell = row.get(col_idx).unwrap_or(&Data::Empty);
            let col_label = format!("{sheet}:{name}", name = field.name);
            out_row.push(convert_cell(user_row, &col_label, &field.data_type, cell)?);
        }
        rows.push(out_row);
    }

    // Use header_cells only to avoid unused warning in some feature builds.
    let _ = header_cells;
    Ok(rows)
}

fn infer_schema_from_sheet_range(
    sheet: &str,
    range: &calamine::Range<Data>,
) -> IngestionResult<Schema> {
    let (header_row_idx, header_cells) = find_header_row(range)?;

    // Initialize per-column inference state.
    let mut states: Vec<InferState> = vec![InferState::Unknown; header_cells.len()];

    for (idx0, row) in range.rows().enumerate() {
        if idx0 <= header_row_idx {
            continue;
        }
        for (col_idx, cell) in row.iter().enumerate() {
            if col_idx >= states.len() {
                break;
            }
            states[col_idx].observe(cell);
        }
    }

    let mut fields = Vec::with_capacity(header_cells.len());
    for (name, st) in header_cells.into_iter().zip(states.into_iter()) {
        let name = name.trim().to_string();
        if name.is_empty() {
            continue;
        }
        fields.push(crate::types::Field::new(name, st.finish_type()));
    }

    if fields.is_empty() {
        return Err(IngestionError::SchemaMismatch {
            message: format!("sheet '{sheet}': no header columns found"),
        });
    }

    Ok(Schema::new(fields))
}

fn find_header_row(range: &calamine::Range<Data>) -> IngestionResult<(usize, Vec<String>)> {
    for (idx0, row) in range.rows().enumerate() {
        let non_empty = row.iter().any(|c| !matches!(c, Data::Empty));
        if non_empty {
            let header_cells = row.iter().map(cell_to_header_string).collect::<Vec<_>>();
            return Ok((idx0, header_cells));
        }
    }
    Err(IngestionError::SchemaMismatch {
        message: "sheet has no non-empty rows (no header row found)".to_string(),
    })
}

#[derive(Debug, Clone, Copy)]
enum InferState {
    Unknown,
    Bool,
    Int,
    Float,
    Utf8,
}

impl InferState {
    fn observe(&mut self, c: &Data) {
        if matches!(c, Data::Empty) {
            return;
        }

        let next = match c {
            Data::Bool(_) => InferState::Bool,
            Data::Int(_) => InferState::Int,
            Data::Float(f) => {
                if f.fract() == 0.0 {
                    InferState::Int
                } else {
                    InferState::Float
                }
            }
            // Dates/durations/errors/strings: treat as strings.
            _ => InferState::Utf8,
        };

        *self = match (*self, next) {
            (InferState::Unknown, x) => x,
            (InferState::Utf8, _) => InferState::Utf8,
            (_, InferState::Utf8) => InferState::Utf8,
            (InferState::Bool, InferState::Bool) => InferState::Bool,
            (InferState::Int, InferState::Int) => InferState::Int,
            (InferState::Float, InferState::Float) => InferState::Float,
            (InferState::Int, InferState::Float) | (InferState::Float, InferState::Int) => {
                InferState::Float
            }
            // Any other mixing falls back to Utf8.
            _ => InferState::Utf8,
        };
    }

    fn finish_type(self) -> DataType {
        match self {
            InferState::Bool => DataType::Bool,
            InferState::Int => DataType::Int64,
            InferState::Float => DataType::Float64,
            InferState::Utf8 | InferState::Unknown => DataType::Utf8,
        }
    }
}

fn wrap_schema_err_with_sheet(sheet: &str, err: IngestionError) -> IngestionError {
    match err {
        IngestionError::SchemaMismatch { message } => IngestionError::SchemaMismatch {
            message: format!("sheet '{sheet}': {message}"),
        },
        other => other,
    }
}

fn build_header_projection(
    range: &calamine::Range<Data>,
    schema: &Schema,
) -> IngestionResult<(usize, Vec<usize>, Vec<String>)> {
    let mut header_row_idx: Option<usize> = None;
    let mut header_cells: Option<Vec<String>> = None;

    for (idx0, row) in range.rows().enumerate() {
        let non_empty = row.iter().any(|c| !matches!(c, Data::Empty));
        if non_empty {
            header_row_idx = Some(idx0);
            header_cells = Some(row.iter().map(cell_to_header_string).collect());
            break;
        }
    }

    let header_row_idx = header_row_idx.ok_or_else(|| IngestionError::SchemaMismatch {
        message: "sheet has no non-empty rows (no header row found)".to_string(),
    })?;
    let header_cells = header_cells.unwrap_or_default();

    // Build a projection of schema field -> column index by searching header_cells.
    let mut col_idxs: Vec<usize> = Vec::with_capacity(schema.fields.len());
    for f in &schema.fields {
        match header_cells.iter().position(|h| h.trim() == f.name) {
            Some(idx) => col_idxs.push(idx),
            None => {
                return Err(IngestionError::SchemaMismatch {
                    message: format!(
                        "missing required column '{}'. headers={:?}",
                        f.name, header_cells
                    ),
                });
            }
        }
    }

    Ok((header_row_idx, col_idxs, header_cells))
}

fn cell_to_header_string(c: &Data) -> String {
    match c {
        Data::String(s) => s.clone(),
        Data::Int(i) => i.to_string(),
        Data::Float(f) => {
            if f.fract() == 0.0 {
                (*f as i64).to_string()
            } else {
                f.to_string()
            }
        }
        Data::Bool(b) => b.to_string(),
        Data::DateTime(f) => f.to_string(),
        Data::DateTimeIso(s) => s.clone(),
        Data::DurationIso(s) => s.clone(),
        Data::Error(e) => format!("{e:?}"),
        Data::Empty => "".to_string(),
    }
}

fn convert_cell(
    row: usize,
    column: &str,
    data_type: &DataType,
    c: &Data,
) -> IngestionResult<Value> {
    if matches!(c, Data::Empty) {
        return Ok(Value::Null);
    }

    match data_type {
        DataType::Utf8 => Ok(Value::Utf8(cell_to_string(c))),
        DataType::Bool => parse_bool_cell(row, column, c).map(Value::Bool),
        DataType::Int64 => parse_i64_cell(row, column, c).map(Value::Int64),
        DataType::Float64 => parse_f64_cell(row, column, c).map(Value::Float64),
    }
}

fn cell_to_string(c: &Data) -> String {
    match c {
        Data::String(s) => s.clone(),
        _ => c.to_string(),
    }
}

fn parse_bool_cell(row: usize, column: &str, c: &Data) -> IngestionResult<bool> {
    match c {
        Data::Bool(b) => Ok(*b),
        Data::Int(i) => Ok(*i != 0),
        Data::Float(f) => Ok(*f != 0.0),
        Data::String(s) => parse_bool_str(s).map_err(|message| IngestionError::ParseError {
            row,
            column: column.to_string(),
            raw: s.clone(),
            message,
        }),
        _ => Err(IngestionError::ParseError {
            row,
            column: column.to_string(),
            raw: c.to_string(),
            message: "expected bool".to_string(),
        }),
    }
}

fn parse_bool_str(s: &str) -> Result<bool, String> {
    match s.trim().to_ascii_lowercase().as_str() {
        "true" | "t" | "1" | "yes" | "y" => Ok(true),
        "false" | "f" | "0" | "no" | "n" => Ok(false),
        _ => Err("expected bool (true/false/1/0/yes/no)".to_string()),
    }
}

fn parse_i64_cell(row: usize, column: &str, c: &Data) -> IngestionResult<i64> {
    match c {
        Data::Int(i) => Ok(*i),
        Data::Float(f) => {
            if f.fract() == 0.0 {
                Ok(*f as i64)
            } else {
                Err(IngestionError::ParseError {
                    row,
                    column: column.to_string(),
                    raw: c.to_string(),
                    message: "expected integer (got non-integer float)".to_string(),
                })
            }
        }
        Data::String(s) => s
            .trim()
            .parse::<i64>()
            .map_err(|e| IngestionError::ParseError {
                row,
                column: column.to_string(),
                raw: s.clone(),
                message: e.to_string(),
            }),
        _ => Err(IngestionError::ParseError {
            row,
            column: column.to_string(),
            raw: c.to_string(),
            message: "expected integer".to_string(),
        }),
    }
}

fn parse_f64_cell(row: usize, column: &str, c: &Data) -> IngestionResult<f64> {
    match c {
        Data::Float(f) => Ok(*f),
        Data::Int(i) => Ok(*i as f64),
        Data::String(s) => s
            .trim()
            .parse::<f64>()
            .map_err(|e| IngestionError::ParseError {
                row,
                column: column.to_string(),
                raw: s.clone(),
                message: e.to_string(),
            }),
        _ => Err(IngestionError::ParseError {
            row,
            column: column.to_string(),
            raw: c.to_string(),
            message: "expected number".to_string(),
        }),
    }
}