nc2parquet 0.2.0

High-performance NetCDF to Parquet converter with cloud storage support
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
//! Post-processing pipeline for transforming DataFrames after NetCDF extraction.
//!
//! # Example
//! ```rust
//! use nc2parquet::postprocess::{PostProcessor, ProcessingPipeline, ColumnRenamer};
//! use polars::prelude::*;
//! use std::collections::HashMap;
//!
//! // Create a pipeline
//! let mut pipeline = ProcessingPipeline::new();
//!
//! // Add processors
//! let mut mappings = HashMap::new();
//! mappings.insert("t2".to_string(), "temperature_2m".to_string());
//! pipeline.add_processor(Box::new(ColumnRenamer::new(mappings)));
//!
//! // Create sample DataFrame
//! let sample_df = df! {
//!     "t2" => [20.5, 21.0, 19.8],
//!     "humidity" => [65, 70, 60]
//! }.unwrap();
//!
//! // Execute pipeline
//! let processed_df = pipeline.execute(sample_df).unwrap();
//! ```

use chrono::{DateTime, Utc};
use log::{debug, warn};
use polars::lazy::dsl::{max_horizontal, min_horizontal};
use polars::prelude::*;
use polars_ops::prelude::RoundMode;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

pub type PostProcessResult<T> = Result<T, PostProcessError>;

/// Errors that can occur during post-processing.
#[derive(thiserror::Error, Debug)]
pub enum PostProcessError {
    #[error("Column '{0}' not found in DataFrame")]
    ColumnNotFound(String),
    #[error("Conversion error: {0}")]
    ConversionError(String),
    #[error("Configuration error: {0}")]
    ConfigurationError(String),
    #[error("Polars error: {0}")]
    PolarsError(#[from] PolarsError),
    #[error("Processing error: {0}")]
    ProcessingError(String),
}

/// Core trait for post-processing operations on DataFrames.
///
/// Implement this trait to create a custom transformation step that can be added
/// to a [`ProcessingPipeline`].  All implementations must be `Send + Sync` so
/// that pipelines can be used in multi-threaded contexts.
///
/// # Examples
///
/// ```rust
/// use nc2parquet::postprocess::{PostProcessor, PostProcessResult};
/// use polars::prelude::*;
///
/// struct DoubleValues {
///     column: String,
/// }
///
/// impl PostProcessor for DoubleValues {
///     fn process(&self, df: DataFrame) -> PostProcessResult<DataFrame> {
///         Ok(df
///             .lazy()
///             .with_columns([(col(&self.column) * lit(2.0)).alias(&self.column)])
///             .collect()?)
///     }
///     fn name(&self) -> &str { "DoubleValues" }
///     fn description(&self) -> &str { "Multiplies a column's values by 2" }
/// }
/// ```
pub trait PostProcessor: Send + Sync {
    fn process(&self, df: DataFrame) -> PostProcessResult<DataFrame>;
    fn name(&self) -> &str;
    fn description(&self) -> &str;

    /// Validate that this processor can operate on the given DataFrame schema.
    /// The default implementation performs no validation.
    fn validate_schema(&self, schema: &Schema) -> PostProcessResult<()> {
        let _ = schema;
        Ok(())
    }

    /// Return the expected output schema after this processor runs.
    /// Override when the processor adds, removes, or renames columns.
    fn output_schema(&self, input_schema: &Schema) -> PostProcessResult<Schema> {
        Ok(input_schema.clone())
    }

    /// Return column names this processor reads from or writes to.
    ///
    /// Used by [`ProcessingPipeline`] to detect independent processors that can
    /// be batched into a single `.collect()` call. An empty `Vec` disables batching.
    fn target_columns(&self) -> Vec<String> {
        vec![]
    }

    /// Express this processor as Polars lazy [`Expr`]s for pipeline batching.
    ///
    /// When `Some` is returned, consecutive independent processors are fused into
    /// a single `.with_columns(…).collect()` call. Return `None` for schema-level
    /// renames, aggregations, or any transform that cannot be a column expression.
    fn to_lazy_expr(&self, schema: &Schema) -> Option<Vec<Expr>> {
        let _ = schema;
        None
    }
}

/// Configuration for the entire post-processing pipeline.
///
/// Use [`ProcessingPipeline::from_config`] to instantiate a runnable pipeline.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessingPipelineConfig {
    /// Human-readable name used in log output. Defaults to `"Configured Pipeline"`.
    pub name: Option<String>,
    /// Ordered list of processors; each step receives the output of the previous.
    pub processors: Vec<ProcessorConfig>,
}

/// Configuration for a single post-processing step.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ProcessorConfig {
    /// Rename one or more columns using an `old_name -> new_name` mapping.
    RenameColumns { mappings: HashMap<String, String> },
    /// Convert a numeric time-offset column to a [`DataType::Datetime`] column.
    DatetimeConvert {
        column: String,
        /// ISO 8601 base datetime (e.g. `"1900-01-01T00:00:00Z"`).
        base: String,
        unit: TimeUnit,
    },
    /// Convert column values from one physical unit to another.
    UnitConvert {
        column: String,
        from_unit: String,
        to_unit: String,
    },
    /// Aggregate rows via group-by and statistical operations.
    Aggregate {
        /// Columns to group by; use an empty list for a global aggregation.
        group_by: Vec<String>,
        aggregations: HashMap<String, AggregationOp>,
    },
    /// Create or overwrite a column by evaluating a mathematical formula.
    ApplyFormula {
        target_column: String,
        /// Formula string (e.g. `"a + b * 2.0"`, `"sqrt(a)"`, `"a < 5.0"`).
        formula: String,
        source_columns: Vec<String>,
    },
}

/// Time units for [`ProcessorConfig::DatetimeConvert`].
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TimeUnit {
    Seconds,
    Minutes,
    Hours,
    Days,
    Milliseconds,
    Microseconds,
    Nanoseconds,
}

/// Statistical aggregation operations for [`ProcessorConfig::Aggregate`].
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AggregationOp {
    Mean,
    Sum,
    Min,
    Max,
    Count,
    /// Sample standard deviation (ddof = 1).
    Std,
    /// Sample variance (ddof = 1).
    Var,
    First,
    Last,
}

impl TimeUnit {
    pub(crate) fn to_seconds_multiplier(&self) -> f64 {
        match self {
            TimeUnit::Nanoseconds => 1e-9,
            TimeUnit::Microseconds => 1e-6,
            TimeUnit::Milliseconds => 1e-3,
            TimeUnit::Seconds => 1.0,
            TimeUnit::Minutes => 60.0,
            TimeUnit::Hours => 3600.0,
            TimeUnit::Days => 86400.0,
        }
    }
}

/// Pipeline that chains multiple post-processors together.
///
/// Processors are executed in the order they were added.  The output DataFrame
/// of each step becomes the input of the next step.
///
/// # Examples
///
/// ```rust
/// use nc2parquet::postprocess::{ProcessingPipeline, ColumnRenamer, PostProcessor};
/// use polars::prelude::*;
/// use std::collections::HashMap;
///
/// let mut pipeline = ProcessingPipeline::new();
///
/// let mut mappings = HashMap::new();
/// mappings.insert("t2".to_string(), "temperature_2m".to_string());
/// pipeline.add_processor(Box::new(ColumnRenamer::new(mappings)));
///
/// let df = df! {
///     "t2" => [20.5f64, 21.0, 19.8],
/// }.unwrap();
///
/// let result = pipeline.execute(df).unwrap();
/// assert!(result.column("temperature_2m").is_ok());
/// ```
pub struct ProcessingPipeline {
    processors: Vec<Box<dyn PostProcessor>>,
    name: String,
}

impl ProcessingPipeline {
    pub fn new() -> Self {
        Self {
            name: "Unnamed Pipeline".to_string(),
            processors: Vec::new(),
        }
    }

    pub fn with_name(name: String) -> Self {
        Self {
            name,
            processors: Vec::new(),
        }
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    /// Creates a pipeline from a [`ProcessingPipelineConfig`].
    ///
    /// Returns [`PostProcessError::ConfigurationError`] if any processor config
    /// contains invalid parameters (e.g. a malformed base datetime string).
    pub fn from_config(config: &ProcessingPipelineConfig) -> PostProcessResult<Self> {
        let mut pipeline = Self {
            name: config
                .name
                .clone()
                .unwrap_or_else(|| "Configured Pipeline".to_string()),
            processors: Vec::new(),
        };

        for processor_config in &config.processors {
            let processor = create_processor(processor_config)?;
            pipeline.add_processor(processor);
        }

        Ok(pipeline)
    }

    pub fn add_processor(&mut self, processor: Box<dyn PostProcessor>) {
        self.processors.push(processor);
    }

    /// Executes all processors in order, returning the transformed DataFrame.
    ///
    /// Consecutive processors with disjoint [`target_columns`](PostProcessor::target_columns)
    /// that implement [`to_lazy_expr`](PostProcessor::to_lazy_expr) are batched into a
    /// single `.collect()` call to eliminate redundant materialisation.
    pub fn execute(&mut self, mut df: DataFrame) -> PostProcessResult<DataFrame> {
        debug!(
            "Executing pipeline '{}' with {} processors",
            self.name,
            self.processors.len()
        );

        if self.processors.is_empty() {
            return Ok(df);
        }

        debug!("Initial DataFrame shape: {:?}", df.shape());

        let mut i = 0;
        while i < self.processors.len() {
            let schema = df.schema();

            if let Some(exprs) = self.processors[i].to_lazy_expr(schema) {
                let mut batch_exprs = exprs;
                let mut batch_columns: std::collections::HashSet<String> =
                    self.processors[i].target_columns().into_iter().collect();
                let mut batch_end = i + 1;

                while batch_end < self.processors.len() {
                    let next_cols: std::collections::HashSet<String> = self.processors[batch_end]
                        .target_columns()
                        .into_iter()
                        .collect();

                    if batch_columns.is_disjoint(&next_cols)
                        && let Some(next_exprs) = self.processors[batch_end].to_lazy_expr(schema)
                    {
                        batch_exprs.extend(next_exprs);
                        batch_columns.extend(next_cols);
                        batch_end += 1;
                        continue;
                    }
                    break;
                }

                if batch_end > i + 1 {
                    debug!(
                        "Batching {} processors ({} to {}) into a single collect()",
                        batch_end - i,
                        i + 1,
                        batch_end
                    );
                    df = df
                        .lazy()
                        .with_columns(batch_exprs)
                        .collect()
                        .map_err(PostProcessError::PolarsError)?;
                } else {
                    let processor_name = self.processors[i].name();
                    debug!(
                        "Executing processor {} '{}' - input shape: {:?}",
                        i + 1,
                        processor_name,
                        df.shape()
                    );
                    df = self.processors[i].process(df)?;
                    debug!(
                        "Processor '{}' completed - output shape: {:?}",
                        processor_name,
                        df.shape()
                    );
                }

                i = batch_end;
            } else {
                let processor_name = self.processors[i].name();
                debug!(
                    "Executing processor {} '{}' - input shape: {:?}",
                    i + 1,
                    processor_name,
                    df.shape()
                );
                df = self.processors[i].process(df)?;
                debug!(
                    "Processor '{}' completed - output shape: {:?}",
                    processor_name,
                    df.shape()
                );
                i += 1;
            }
        }

        debug!("Pipeline '{}' completed successfully", self.name);
        Ok(df)
    }
}

impl Default for ProcessingPipeline {
    fn default() -> Self {
        Self::new()
    }
}

pub(crate) fn create_processor(
    config: &ProcessorConfig,
) -> PostProcessResult<Box<dyn PostProcessor>> {
    match config {
        ProcessorConfig::RenameColumns { mappings } => {
            Ok(Box::new(ColumnRenamer::new(mappings.clone())))
        }
        ProcessorConfig::DatetimeConvert { column, base, unit } => {
            let base_dt = DateTime::parse_from_rfc3339(base)
                .map_err(|e| {
                    PostProcessError::ConfigurationError(format!(
                        "Invalid base datetime '{}': {}",
                        base, e
                    ))
                })?
                .with_timezone(&Utc);
            Ok(Box::new(DateTimeConverter::new(
                column.clone(),
                base_dt,
                unit.clone(),
            )))
        }
        ProcessorConfig::UnitConvert {
            column,
            from_unit,
            to_unit,
        } => Ok(Box::new(UnitConverter::new(
            column.clone(),
            from_unit.clone(),
            to_unit.clone(),
        ))),
        ProcessorConfig::Aggregate {
            group_by,
            aggregations,
        } => Ok(Box::new(Aggregator::new(
            group_by.clone(),
            aggregations.clone(),
        ))),
        ProcessorConfig::ApplyFormula {
            target_column,
            formula,
            source_columns,
        } => Ok(Box::new(FormulaApplier::new(
            target_column.clone(),
            formula.clone(),
            source_columns.clone(),
        ))),
    }
}

/// Renames DataFrame columns according to an `old_name -> new_name` mapping.
/// Keys absent from the DataFrame are logged as warnings and skipped.
pub struct ColumnRenamer {
    mappings: HashMap<String, String>,
}

/// Converts a numeric time-offset column into a [`DataType::Datetime`] column.
/// Output values are `base_datetime + value * unit_multiplier`.
pub struct DateTimeConverter {
    column: String,
    base_datetime: DateTime<Utc>,
    unit: TimeUnit,
}

/// Converts column values from one physical unit to another.
///
/// Built-in conversions: Kelvin ↔ Celsius, Celsius ↔ Fahrenheit.
/// All other pairs apply a multiplicative `conversion_factor`.
pub struct UnitConverter {
    column: String,
    from_unit: String,
    to_unit: String,
    conversion_factor: f64,
}

/// Aggregates rows via group-by and statistical operations.
/// An empty `group_by` produces a global aggregation.
/// Output column names are `{original_column}_{operation}`.
pub struct Aggregator {
    group_by: Vec<String>,
    aggregations: HashMap<String, AggregationOp>,
}

/// Creates or overwrites a column by evaluating a formula string.
///
/// Supported syntax:
/// - Arithmetic: `+`, `-`, `*`, `/` — left-associative, `*`/`/` bind tighter than `+`/`-`.
/// - Parenthesised sub-expressions: `(a + b) * c`.
/// - Comparisons: `<`, `>`, `==`, `!=`, `<=`, `>=`.
/// - Numeric literals and column names.
/// - **Unary functions** (1 argument): `abs`, `sqrt`, `exp`, `ln`, `log10`,
///   `sin`, `cos`, `tan`, `ceil`, `floor`, `round`.
/// - **Binary functions** (2 arguments): `pow(base, exp)`, `min(a, b)`,
///   `max(a, b)`, `log(value, base)`.
///
/// Function names are case-insensitive. Functions may be nested arbitrarily
/// and combined with arithmetic, e.g. `abs(a - b) + pow(c, 2.0)`.
pub struct FormulaApplier {
    target_column: String,
    formula: String,
    source_columns: Vec<String>,
}

impl ColumnRenamer {
    pub fn new(mappings: HashMap<String, String>) -> Self {
        Self { mappings }
    }
}

impl DateTimeConverter {
    pub fn new(column: String, base_datetime: DateTime<Utc>, unit: TimeUnit) -> Self {
        Self {
            column,
            base_datetime,
            unit,
        }
    }
}

/// Physical unit families used for automatic conversion factor calculation.
///
/// Each family has a designated base unit.  Conversion factors between any two
/// members of the same family are derived from their individual base-unit factors,
/// so adding a new unit requires only a single entry in [`unit_to_base_factor`].
///
/// Temperature is handled separately because it requires an offset (not just a
/// scale), so its conversions remain as explicit match arms in
/// [`UnitConverter::build_conversion_expr`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum UnitFamily {
    /// Temperature (base: K).  Offset conversions handled separately.
    Temperature,
    /// Pressure (base: Pa).
    Pressure,
    /// Speed (base: m/s).
    Speed,
    /// Length (base: m).
    Length,
}

/// Maps a lowercased unit alias to its [`UnitFamily`] and the multiplicative
/// factor that converts **one unit of `alias`** into the family's base unit.
///
/// Examples
/// - `"hpa"` → `(Pressure, 100.0)`  because 1 hPa = 100 Pa
/// - `"km"` → `(Length, 1000.0)`    because 1 km = 1 000 m
/// - `"kt"` → `(Speed, 1.0/1.943844)` because 1 kt ≈ 0.5144 m/s
fn unit_to_base_factor(unit: &str) -> Option<(UnitFamily, f64)> {
    match unit {
        // ── Temperature (base: K) ──────────────────────────────────────────
        "kelvin" | "k" => Some((UnitFamily::Temperature, 1.0)),
        "celsius" | "c" => Some((UnitFamily::Temperature, 1.0)),
        "fahrenheit" | "f" => Some((UnitFamily::Temperature, 1.0)),

        // ── Pressure (base: Pa) ────────────────────────────────────────────
        "pa" | "pascal" => Some((UnitFamily::Pressure, 1.0)),
        "hpa" | "hectopascal" => Some((UnitFamily::Pressure, 100.0)),
        "mbar" | "millibar" => Some((UnitFamily::Pressure, 100.0)),
        "kpa" | "kilopascal" => Some((UnitFamily::Pressure, 1_000.0)),
        "atm" | "atmosphere" => Some((UnitFamily::Pressure, 101_325.0)),
        "inhg" => Some((UnitFamily::Pressure, 3_386.389)),
        "mmhg" => Some((UnitFamily::Pressure, 133.322)),

        // ── Speed (base: m/s) ──────────────────────────────────────────────
        "m/s" | "ms" => Some((UnitFamily::Speed, 1.0)),
        "km/h" | "kmh" => Some((UnitFamily::Speed, 1.0 / 3.6)),
        "kt" | "knot" | "knots" => Some((UnitFamily::Speed, 1.0 / 1.943_844)),
        "mph" => Some((UnitFamily::Speed, 1.0 / 2.236_936)),
        "ft/s" | "fts" => Some((UnitFamily::Speed, 1.0 / 3.280_84)),

        // ── Length (base: m) ───────────────────────────────────────────────
        "m" | "meter" | "metre" => Some((UnitFamily::Length, 1.0)),
        "km" | "kilometer" => Some((UnitFamily::Length, 1_000.0)),
        "ft" | "foot" | "feet" => Some((UnitFamily::Length, 0.304_8)),
        "mi" | "mile" => Some((UnitFamily::Length, 1_609.344)),
        "nm" | "nautical_mile" => Some((UnitFamily::Length, 1_852.0)),
        "cm" | "centimeter" => Some((UnitFamily::Length, 0.01)),
        "mm" | "millimeter" => Some((UnitFamily::Length, 0.001)),

        _ => None,
    }
}

impl UnitConverter {
    pub fn new(column: String, from_unit: String, to_unit: String) -> Self {
        let conversion_factor = Self::calculate_conversion_factor(&from_unit, &to_unit);
        Self {
            column,
            from_unit,
            to_unit,
            conversion_factor,
        }
    }

    /// Creates a unit converter with an explicit multiplicative scaling factor.
    pub fn with_conversion_factor(
        column: String,
        from_unit: String,
        to_unit: String,
        factor: f64,
    ) -> Self {
        Self {
            column,
            from_unit,
            to_unit,
            conversion_factor: factor,
        }
    }

    /// Computes the multiplicative factor needed to convert `from_unit` values
    /// into `to_unit` values.
    ///
    /// For non-temperature families the factor is derived from the base-unit
    /// lookup: `factor = from_to_base / to_to_base`.
    ///
    /// Temperature conversions involve an additive offset and therefore cannot
    /// be expressed as a pure scale factor; this function returns `1.0` for
    /// temperature pairs, and [`build_conversion_expr`] handles them via
    /// explicit match arms.
    ///
    /// Unknown or cross-family pairs also return `1.0` for backward compatibility.
    fn calculate_conversion_factor(from_unit: &str, to_unit: &str) -> f64 {
        let from_lower = from_unit.to_lowercase();
        let to_lower = to_unit.to_lowercase();

        match (
            unit_to_base_factor(from_lower.as_str()),
            unit_to_base_factor(to_lower.as_str()),
        ) {
            (Some((from_family, from_base)), Some((to_family, to_base)))
                if from_family == to_family =>
            {
                if from_family == UnitFamily::Temperature {
                    // Offset conversions: scale is handled in build_conversion_expr.
                    1.0
                } else {
                    from_base / to_base
                }
            }
            _ => 1.0,
        }
    }

    /// Returns the Polars lazy [`Expr`] for this conversion, shared by `process` and `to_lazy_expr`.
    fn build_conversion_expr(&self) -> Expr {
        let from = self.from_unit.to_lowercase();
        let to = self.to_unit.to_lowercase();

        match (from.as_str(), to.as_str()) {
            ("kelvin", "celsius") | ("k", "c") => col(&self.column) - lit(273.15),
            ("celsius", "kelvin") | ("c", "k") => col(&self.column) + lit(273.15),
            ("celsius", "fahrenheit") | ("c", "f") => {
                col(&self.column) * lit(9.0_f64 / 5.0) + lit(32.0_f64)
            }
            ("fahrenheit", "celsius") | ("f", "c") => {
                (col(&self.column) - lit(32.0_f64)) * lit(5.0_f64 / 9.0)
            }
            _ => col(&self.column) * lit(self.conversion_factor),
        }
    }
}

impl Aggregator {
    pub fn new(group_by: Vec<String>, aggregations: HashMap<String, AggregationOp>) -> Self {
        Self {
            group_by,
            aggregations,
        }
    }
}

impl PostProcessor for ColumnRenamer {
    fn process(&self, mut df: DataFrame) -> PostProcessResult<DataFrame> {
        debug!("Renaming columns with {} mappings", self.mappings.len());

        for (old_name, new_name) in &self.mappings {
            let column_names: Vec<&str> =
                df.get_column_names().iter().map(|s| s.as_str()).collect();
            if !column_names.contains(&old_name.as_str()) {
                warn!(
                    "Column '{}' not found in DataFrame, skipping rename",
                    old_name
                );
                continue;
            }

            debug!("Renaming column '{}' to '{}'", old_name, new_name);
            df.rename(old_name, new_name.into())?;
        }

        Ok(df)
    }

    fn name(&self) -> &str {
        "ColumnRenamer"
    }

    fn description(&self) -> &str {
        "Renames columns based on provided mappings"
    }

    fn target_columns(&self) -> Vec<String> {
        let mut cols: Vec<String> = self.mappings.keys().cloned().collect();
        cols.extend(self.mappings.values().cloned());
        cols
    }

    fn output_schema(&self, input_schema: &Schema) -> PostProcessResult<Schema> {
        let mut new_fields = Vec::new();
        for (name, dtype) in input_schema.iter() {
            let name_str = name.as_str();
            let new_name = if let Some(mapped) = self.mappings.get(name_str) {
                mapped.clone()
            } else {
                name_str.to_string()
            };
            new_fields.push(Field::new(new_name.into(), dtype.clone()));
        }

        Ok(Schema::from_iter(new_fields))
    }
}

impl PostProcessor for DateTimeConverter {
    fn process(&self, df: DataFrame) -> PostProcessResult<DataFrame> {
        debug!(
            "Converting column '{}' to datetime using base {} and unit {:?}",
            self.column,
            self.base_datetime.to_rfc3339(),
            self.unit
        );

        let column_names: Vec<&str> = df.get_column_names().iter().map(|s| s.as_str()).collect();
        if !column_names.contains(&self.column.as_str()) {
            return Err(PostProcessError::ColumnNotFound(self.column.clone()));
        }

        let base_timestamp_ms = self.base_datetime.timestamp_millis();
        let unit_multiplier_ms = self.unit.to_seconds_multiplier() * 1000.0;

        let result =
            df.lazy()
                .with_columns([(col(&self.column) * lit(unit_multiplier_ms)
                    + lit(base_timestamp_ms))
                .cast(DataType::Datetime(
                    polars::prelude::TimeUnit::Milliseconds,
                    None,
                ))
                .alias(&self.column)])
                .collect()?;

        Ok(result)
    }

    fn name(&self) -> &str {
        "DateTimeConverter"
    }

    fn description(&self) -> &str {
        "Converts numeric column values to datetime based on a base datetime and time unit"
    }

    fn target_columns(&self) -> Vec<String> {
        vec![self.column.clone()]
    }

    fn output_schema(&self, input_schema: &Schema) -> PostProcessResult<Schema> {
        let mut new_schema = input_schema.clone();
        new_schema.with_column(
            self.column.clone().into(),
            DataType::Datetime(polars::prelude::TimeUnit::Milliseconds, None),
        );

        Ok(new_schema)
    }
}

impl PostProcessor for UnitConverter {
    fn process(&self, df: DataFrame) -> PostProcessResult<DataFrame> {
        debug!(
            "Converting column '{}' from {} to {} (factor: {})",
            self.column, self.from_unit, self.to_unit, self.conversion_factor
        );

        let column_names: Vec<&str> = df.get_column_names().iter().map(|s| s.as_str()).collect();
        if !column_names.contains(&self.column.as_str()) {
            return Err(PostProcessError::ColumnNotFound(self.column.clone()));
        }

        let expr = self.build_conversion_expr();
        let result = df
            .lazy()
            .with_columns([expr.alias(&self.column)])
            .collect()?;

        Ok(result)
    }

    fn name(&self) -> &str {
        "UnitConverter"
    }

    fn description(&self) -> &str {
        "Converts values in a column from one unit to another"
    }

    fn target_columns(&self) -> Vec<String> {
        vec![self.column.clone()]
    }

    fn to_lazy_expr(&self, schema: &Schema) -> Option<Vec<Expr>> {
        if !schema.contains(&self.column) {
            return None;
        }

        let expr = self.build_conversion_expr();
        Some(vec![expr.alias(&self.column)])
    }
}

impl PostProcessor for Aggregator {
    fn process(&self, df: DataFrame) -> PostProcessResult<DataFrame> {
        debug!(
            "Aggregating data with group_by: {:?}, aggregations: {:?}",
            self.group_by, self.aggregations
        );

        let column_names: Vec<&str> = df.get_column_names().iter().map(|s| s.as_str()).collect();
        for col_name in &self.group_by {
            if !column_names.contains(&col_name.as_str()) {
                return Err(PostProcessError::ColumnNotFound(col_name.clone()));
            }
        }

        for col_name in self.aggregations.keys() {
            if !column_names.contains(&col_name.as_str()) {
                return Err(PostProcessError::ColumnNotFound(col_name.clone()));
            }
        }

        let mut agg_exprs = Vec::new();

        for (col_name, agg_op) in &self.aggregations {
            let (expr, suffix) = match agg_op {
                AggregationOp::Mean => (col(col_name).mean(), "mean"),
                AggregationOp::Sum => (col(col_name).sum(), "sum"),
                AggregationOp::Min => (col(col_name).min(), "min"),
                AggregationOp::Max => (col(col_name).max(), "max"),
                AggregationOp::Count => (col(col_name).count(), "count"),
                AggregationOp::Std => (col(col_name).std(1), "std"),
                AggregationOp::Var => (col(col_name).var(1), "var"),
                AggregationOp::First => (col(col_name).first(), "first"),
                AggregationOp::Last => (col(col_name).last(), "last"),
            };
            agg_exprs.push(expr.alias(format!("{}_{}", col_name, suffix)));
        }

        let result = if !self.group_by.is_empty() {
            df.lazy()
                .group_by(self.group_by.iter().map(col).collect::<Vec<_>>())
                .agg(agg_exprs)
                .collect()?
        } else {
            df.lazy().select(agg_exprs).collect()?
        };

        Ok(result)
    }

    fn name(&self) -> &str {
        "Aggregator"
    }

    fn description(&self) -> &str {
        "Aggregates data using group by operations and statistical functions"
    }

    fn target_columns(&self) -> Vec<String> {
        let mut cols = self.group_by.clone();
        cols.extend(self.aggregations.keys().cloned());
        cols
    }
}

impl PostProcessor for FormulaApplier {
    fn process(&self, df: DataFrame) -> PostProcessResult<DataFrame> {
        debug!(
            "Applying formula '{}' to create column '{}'",
            self.formula, self.target_column
        );

        let column_names: Vec<&str> = df.get_column_names().iter().map(|s| s.as_str()).collect();
        for col_name in &self.source_columns {
            if !column_names.contains(&col_name.as_str()) {
                return Err(PostProcessError::ColumnNotFound(col_name.clone()));
            }
        }

        let result = self.apply_formula(df)?;

        Ok(result)
    }

    fn name(&self) -> &str {
        "FormulaApplier"
    }

    fn description(&self) -> &str {
        "Applies mathematical formulas to create new columns or transform existing ones"
    }

    fn target_columns(&self) -> Vec<String> {
        let mut cols = self.source_columns.clone();
        cols.push(self.target_column.clone());
        cols
    }

    fn output_schema(&self, input_schema: &Schema) -> PostProcessResult<Schema> {
        let mut new_schema = input_schema.clone();
        if !new_schema.contains(&self.target_column) {
            new_schema.with_column(self.target_column.as_str().into(), DataType::Float64);
        }

        Ok(new_schema)
    }
}

impl FormulaApplier {
    pub fn new(target_column: String, formula: String, source_columns: Vec<String>) -> Self {
        Self {
            target_column,
            formula,
            source_columns,
        }
    }

    fn apply_formula(&self, df: DataFrame) -> PostProcessResult<DataFrame> {
        let formula = self.formula.trim();

        let result = if formula.contains('<')
            || formula.contains('>')
            || formula.contains("==")
            || formula.contains("!=")
        {
            self.parse_comparison_formula(df, formula)?
        } else {
            let expr = self.parse_expression(&df, formula)?;
            df.lazy()
                .with_columns([expr.alias(&self.target_column)])
                .collect()?
        };

        Ok(result)
    }

    fn parse_comparison_formula(
        &self,
        df: DataFrame,
        formula: &str,
    ) -> PostProcessResult<DataFrame> {
        let comparison_ops = ["==", "!=", "<=", ">=", "<", ">"];

        for op in comparison_ops {
            if formula.contains(op) {
                let parts: Vec<&str> = formula.split(op).collect();
                if parts.len() == 2 {
                    let left = parts[0].trim();
                    let right = parts[1].trim();

                    let left_expr = self.parse_operand_with_validation(&df, left)?;
                    let right_expr = self.parse_operand_with_validation(&df, right)?;

                    let result_expr = match op {
                        "==" => left_expr.eq(right_expr),
                        "!=" => left_expr.neq(right_expr),
                        "<" => left_expr.lt(right_expr),
                        "<=" => left_expr.lt_eq(right_expr),
                        ">" => left_expr.gt(right_expr),
                        ">=" => left_expr.gt_eq(right_expr),
                        _ => unreachable!(),
                    };

                    return Ok(df
                        .lazy()
                        .with_columns([result_expr.alias(&self.target_column)])
                        .collect()?);
                }
            }
        }

        Err(PostProcessError::ProcessingError(format!(
            "Unable to parse comparison formula: {}",
            formula
        )))
    }

    fn parse_expression(&self, df: &DataFrame, expr: &str) -> PostProcessResult<Expr> {
        let expr = expr.trim();
        // Scan right-to-left so that we split on the LAST `+`/`-` at depth 0,
        // giving left-associative evaluation: `a - b - c` → `(a - b) - c`.
        // When scanning right-to-left, `)` increases depth and `(` decreases it.
        let mut depth: i32 = 0;
        let chars: Vec<(usize, char)> = expr.char_indices().collect();
        for &(i, c) in chars.iter().rev() {
            match c {
                ')' => depth += 1,
                '(' => depth -= 1,
                '+' | '-' if depth == 0 => {
                    let left = &expr[..i];
                    let right = &expr[i + 1..];
                    let left_expr = self.parse_expression(df, left)?;
                    let right_expr = self.parse_term(df, right)?;

                    return Ok(match c {
                        '+' => left_expr + right_expr,
                        '-' => left_expr - right_expr,
                        _ => unreachable!(),
                    });
                }
                _ => {}
            }
        }

        self.parse_term(df, expr)
    }

    fn parse_term(&self, df: &DataFrame, expr: &str) -> PostProcessResult<Expr> {
        let expr = expr.trim();
        // Scan right-to-left so that we split on the LAST `*`/`/` at depth 0,
        // giving left-associative evaluation: `a / b / c` → `(a / b) / c`.
        // When scanning right-to-left, `)` increases depth and `(` decreases it.
        let mut depth: i32 = 0;
        let chars: Vec<(usize, char)> = expr.char_indices().collect();
        for &(i, c) in chars.iter().rev() {
            match c {
                ')' => depth += 1,
                '(' => depth -= 1,
                '*' | '/' if depth == 0 => {
                    let left = &expr[..i];
                    let right = &expr[i + 1..];
                    let left_expr = self.parse_term(df, left)?;
                    let right_expr = self.parse_factor(df, right)?;

                    return Ok(match c {
                        '*' => left_expr * right_expr,
                        '/' => left_expr / right_expr,
                        _ => unreachable!(),
                    });
                }
                _ => {}
            }
        }

        self.parse_factor(df, expr)
    }

    /// Parse a factor: a function call, a parenthesised sub-expression, or a
    /// bare operand (column name or numeric literal).
    ///
    /// Function calls are detected by `identifier(`. The matching closing
    /// parenthesis is found with depth-counting to handle nested parens.
    fn parse_factor(&self, df: &DataFrame, expr: &str) -> PostProcessResult<Expr> {
        let expr = expr.trim();

        // An identifier immediately followed by `(` is treated as a function call.
        // If there is any operator or whitespace before the `(`, it is a
        // parenthesised sub-expression, not a function call.
        if let Some(paren_pos) = expr.find('(') {
            let name = expr[..paren_pos].trim();
            let is_identifier =
                !name.is_empty() && name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_');
            if is_identifier {
                let mut depth: i32 = 0;
                let mut close_pos = None;
                for (i, c) in expr[paren_pos..].char_indices() {
                    match c {
                        '(' => depth += 1,
                        ')' => {
                            depth -= 1;
                            if depth == 0 {
                                close_pos = Some(paren_pos + i);
                                break;
                            }
                        }
                        _ => {}
                    }
                }
                let close = close_pos.ok_or_else(|| {
                    PostProcessError::ProcessingError(format!(
                        "Unmatched '(' in function call '{}'",
                        expr
                    ))
                })?;
                let args_str = &expr[paren_pos + 1..close];
                return self.parse_function_call(df, name, args_str);
            }
        }

        // Parenthesised sub-expression: verify the parens match each other
        // (guard against e.g. `(a) + (b)` reaching parse_factor).
        if expr.starts_with('(') && expr.ends_with(')') {
            let mut depth: i32 = 0;
            let mut matched_at_end = false;
            let last = expr.len() - 1;
            for (i, c) in expr.char_indices() {
                match c {
                    '(' => depth += 1,
                    ')' => {
                        depth -= 1;
                        if depth == 0 && i == last {
                            matched_at_end = true;
                        }
                    }
                    _ => {}
                }
            }
            if matched_at_end {
                return self.parse_expression(df, &expr[1..expr.len() - 1]);
            }
        }

        self.parse_operand_with_validation(df, expr)
    }

    fn parse_function_call(
        &self,
        df: &DataFrame,
        name: &str,
        args_str: &str,
    ) -> PostProcessResult<Expr> {
        let func_name = name.to_lowercase();
        let args = Self::split_function_args(args_str);

        match func_name.as_str() {
            // ── Unary functions ──────────────────────────────────────────────
            "abs" => {
                check_arity("abs", &args, 1)?;
                Ok(self.parse_expression(df, &args[0])?.abs())
            }
            "sqrt" => {
                check_arity("sqrt", &args, 1)?;
                Ok(self.parse_expression(df, &args[0])?.sqrt())
            }
            "exp" => {
                check_arity("exp", &args, 1)?;
                Ok(self.parse_expression(df, &args[0])?.exp())
            }
            "ln" => {
                // ln(x) = log base e.  Polars .log(base: Expr) so we pass
                // lit(std::f64::consts::E).
                check_arity("ln", &args, 1)?;
                Ok(self
                    .parse_expression(df, &args[0])?
                    .log(lit(std::f64::consts::E)))
            }
            "log10" => {
                check_arity("log10", &args, 1)?;
                Ok(self.parse_expression(df, &args[0])?.log(lit(10.0_f64)))
            }
            "sin" => {
                check_arity("sin", &args, 1)?;
                Ok(self.parse_expression(df, &args[0])?.sin())
            }
            "cos" => {
                check_arity("cos", &args, 1)?;
                Ok(self.parse_expression(df, &args[0])?.cos())
            }
            "tan" => {
                check_arity("tan", &args, 1)?;
                Ok(self.parse_expression(df, &args[0])?.tan())
            }
            "ceil" => {
                check_arity("ceil", &args, 1)?;
                Ok(self.parse_expression(df, &args[0])?.ceil())
            }
            "floor" => {
                check_arity("floor", &args, 1)?;
                Ok(self.parse_expression(df, &args[0])?.floor())
            }
            "round" => {
                check_arity("round", &args, 1)?;
                // round(x) rounds to 0 decimal places (nearest integer).
                Ok(self
                    .parse_expression(df, &args[0])?
                    .round(0, RoundMode::HalfAwayFromZero))
            }

            // ── Binary functions ─────────────────────────────────────────────
            "pow" => {
                check_arity("pow", &args, 2)?;
                let base = self.parse_expression(df, &args[0])?;
                let exp = self.parse_expression(df, &args[1])?;
                Ok(base.pow(exp))
            }
            "min" => {
                // Element-wise minimum of two expressions.
                check_arity("min", &args, 2)?;
                let a = self.parse_expression(df, &args[0])?;
                let b = self.parse_expression(df, &args[1])?;
                min_horizontal([a, b]).map_err(PostProcessError::PolarsError)
            }
            "max" => {
                // Element-wise maximum of two expressions.
                check_arity("max", &args, 2)?;
                let a = self.parse_expression(df, &args[0])?;
                let b = self.parse_expression(df, &args[1])?;
                max_horizontal([a, b]).map_err(PostProcessError::PolarsError)
            }
            "log" => {
                // log(value, base) — change-of-base via Polars .log(base: Expr).
                check_arity("log", &args, 2)?;
                let value = self.parse_expression(df, &args[0])?;
                let base = self.parse_expression(df, &args[1])?;
                Ok(value.log(base))
            }

            _ => Err(PostProcessError::ProcessingError(format!(
                "Unknown function: {}",
                name
            ))),
        }
    }

    /// Split comma-separated args respecting parenthesis nesting, so that
    /// `f(a, g(b, c))` splits into `["a", "g(b, c)"]`.
    fn split_function_args(args_str: &str) -> Vec<String> {
        let mut args = Vec::new();
        let mut depth: i32 = 0;
        let mut current = String::new();

        for c in args_str.chars() {
            match c {
                '(' => {
                    depth += 1;
                    current.push(c);
                }
                ')' => {
                    depth -= 1;
                    current.push(c);
                }
                ',' if depth == 0 => {
                    args.push(current.trim().to_string());
                    current.clear();
                }
                _ => current.push(c),
            }
        }

        let tail = current.trim().to_string();
        if !tail.is_empty() {
            args.push(tail);
        }

        args
    }

    fn parse_operand_with_validation(
        &self,
        df: &DataFrame,
        operand: &str,
    ) -> PostProcessResult<Expr> {
        let operand = operand.trim();

        if let Ok(constant) = operand.parse::<f64>() {
            return Ok(lit(constant));
        }

        let column_names: Vec<String> = df
            .get_column_names()
            .iter()
            .map(|s| s.to_string())
            .collect();
        if column_names.contains(&operand.to_string()) {
            Ok(col(operand))
        } else {
            Err(PostProcessError::ProcessingError(format!(
                "Invalid operand '{}': not a valid number or existing column. Available columns: [{}]",
                operand,
                column_names.join(", ")
            )))
        }
    }
}

fn check_arity(name: &str, args: &[String], expected: usize) -> PostProcessResult<()> {
    if args.len() == expected {
        Ok(())
    } else {
        Err(PostProcessError::ProcessingError(format!(
            "Function '{}' expects {} argument(s), got {}",
            name,
            expected,
            args.len()
        )))
    }
}