etl-unit 0.1.0

Semantic data model for ETL units — qualities and measurements over subjects and time. Built on Polars.
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
// etl-unit/src/column.rs

use std::{fmt, hash::Hash, ops::Deref, sync::Arc};

use polars::prelude::{Expr, PlSmallStr, Selector, col};
use serde::{Deserialize, Serialize};

// ============================================================================
// Column Name Newtypes
// ============================================================================

/// The actual column name as it appears in a source DataFrame.
///
/// This is distinct from [`CanonicalColumnName`] which represents the
/// standardized name used for cross-source matching and output.
///
/// # Example
/// ```ignore
/// // A source might have column "pump_station_id"
/// let source_col = SourceColumnName::new("pump_station_id");
///
/// // Which maps to canonical name "station"
/// let canonical = CanonicalColumnName::new("station");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct SourceColumnName(String);

impl SourceColumnName {
    pub fn new(name: impl Into<String>) -> Self {
        Self(name.into())
    }

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

    pub fn into_inner(self) -> String {
        self.0
    }
}

impl Deref for SourceColumnName {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<str> for SourceColumnName {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for SourceColumnName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Source: {}", self.0)
    }
}

impl From<&str> for SourceColumnName {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

impl From<String> for SourceColumnName {
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<PlSmallStr> for SourceColumnName {
    fn from(s: PlSmallStr) -> Self {
        Self(s.to_string())
    }
}

// Allow col(&name) to work by converting &CanonicalColumnName -> PlSmallStr
// impl From<&SourceColumnName> for PlSmallStr {
// 	fn from(c: &SourceColumnName) -> Self {
// 		PlSmallStr::from(c.as_str())
// }
// }

// Allow col(&name) to work by converting &CanonicalColumnName -> PlSmallStr
impl From<SourceColumnName> for PlSmallStr {
    fn from(c: SourceColumnName) -> Self {
        PlSmallStr::from(c.as_str())
    }
}

// Great for: df.select(my_col.into()) using the selectors API
impl From<SourceColumnName> for Selector {
    fn from(c: SourceColumnName) -> Self {
        Selector::ByName {
            names: Arc::from(vec![PlSmallStr::from_string(c.0)]),
            strict: false,
        }
    }
}

impl From<&SourceColumnName> for Selector {
    fn from(c: &SourceColumnName) -> Self {
        Selector::ByName {
            names: Arc::from(vec![PlSmallStr::from(c.as_str())]),
            strict: false,
        }
    }
}

/// The canonical column name used for cross-source matching and output.
///
/// Canonical names provide a standardized interface regardless of how
/// different sources name their columns. For example, one source might
/// have "pump_station_id" while another has "station_name", but both
/// map to canonical name "station".
///
/// # Example
/// ```ignore
/// // Different sources use different column names
/// let source_a_col = SourceColumnName::new("pump_station_id");
/// let source_b_col = SourceColumnName::new("station_name");
///
/// // But they both map to the same canonical name
/// let canonical = CanonicalColumnName::new("station");
/// ```
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct CanonicalColumnName(String);

impl CanonicalColumnName {
    pub fn new(name: impl Into<String>) -> Self {
        Self(name.into())
    }

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

    pub fn into_inner(self) -> String {
        self.0
    }
}

impl Deref for CanonicalColumnName {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<str> for CanonicalColumnName {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for CanonicalColumnName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Canonical: {}", self.0)
    }
}

impl From<&str> for CanonicalColumnName {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

impl From<String> for CanonicalColumnName {
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<CanonicalColumnName> for String {
    fn from(c: CanonicalColumnName) -> Self {
        c.0
    }
}

impl From<PlSmallStr> for CanonicalColumnName {
    fn from(s: PlSmallStr) -> Self {
        Self(s.to_string())
    }
}

// Allow col(&name) to work by converting &CanonicalColumnName -> PlSmallStr
impl From<&CanonicalColumnName> for PlSmallStr {
    fn from(c: &CanonicalColumnName) -> Self {
        PlSmallStr::from(c.as_str())
    }
}

// Allow col(&name) to work by converting &CanonicalColumnName -> PlSmallStr
impl From<CanonicalColumnName> for PlSmallStr {
    fn from(c: CanonicalColumnName) -> Self {
        PlSmallStr::from(c.as_str())
    }
}

// Great for: df.select(my_col.into()) using the selectors API
impl From<CanonicalColumnName> for Selector {
    fn from(c: CanonicalColumnName) -> Self {
        Selector::ByName {
            names: Arc::from(vec![PlSmallStr::from_string(c.0)]),
            strict: false,
        }
    }
}

impl From<&CanonicalColumnName> for Selector {
    fn from(c: &CanonicalColumnName) -> Self {
        Selector::ByName {
            names: Arc::from(vec![PlSmallStr::from(c.as_str())]),
            strict: false,
        }
    }
}

// Great for: df.filter(col.into().eq(lit(1)))
impl From<CanonicalColumnName> for Expr {
    fn from(c: CanonicalColumnName) -> Self {
        col(&c.0)
    }
}

// Great for: df.select(cols.into())
impl From<CanonicalColumnName> for Vec<String> {
    fn from(c: CanonicalColumnName) -> Self {
        vec![c.0]
    }
}

// Great for: df.unique(col.into(), ...)
impl From<CanonicalColumnName> for Option<Vec<String>> {
    fn from(c: CanonicalColumnName) -> Self {
        Some(vec![c.0])
    }
}

// ============================================================================
// Domain Signature (for determining stack vs join)
// ============================================================================

/// The domain signature of an EtlUnit, used to determine composition strategy.
/// Units with matching signatures can be stacked (unioned).
/// Units with different signatures are joined on Subject.
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct DomainSignature {
    /// Canonical subject column name
    pub subject: CanonicalColumnName,
    /// Canonical time column name (None for Quality units)
    pub time: Option<CanonicalColumnName>,
    /// Canonical component column names (sorted for comparison)
    pub components: Vec<CanonicalColumnName>,
}

impl DomainSignature {
    pub fn quality(subject: impl Into<String>) -> Self {
        Self {
            subject: CanonicalColumnName::new(subject),
            time: None,
            components: vec![],
        }
    }

    pub fn measurement(subject: impl Into<String>, time: impl Into<String>) -> Self {
        Self {
            subject: CanonicalColumnName::new(subject),
            time: Some(CanonicalColumnName::new(time)),
            components: vec![],
        }
    }

    pub fn with_components(mut self, mut components: Vec<String>) -> Self {
        components.sort();
        self.components = components
            .into_iter()
            .map(CanonicalColumnName::new)
            .collect();
        self
    }

    /// Check if two signatures can be stacked (must match exactly)
    pub fn can_stack_with(&self, other: &DomainSignature) -> bool {
        self == other
    }

    /// Check if two signatures can be joined (must share subject)
    pub fn can_join_with(&self, other: &DomainSignature) -> bool {
        self.subject == other.subject
    }

    /// Check if this signature has fewer components than another
    /// (would need to aggregate components when stacking)
    pub fn needs_component_reduction(&self, other: &DomainSignature) -> bool {
        self.subject == other.subject
            && self.time == other.time
            && self.components.len() < other.components.len()
    }
}

// ============================================================================
// Extension Trait for Explicit Column Name Construction
// ============================================================================

/// Extension trait for creating column names with explicit intent.
///
/// This makes it impossible to accidentally swap canonical and source columns:
/// ```rust ignore
/// # use etl_unit::column::ColumnNameExt;
/// // Clear and explicit - canonical on left, source on right
/// source.map("engine_status".canonical(), "status".source())
/// ```
pub trait ColumnNameExt {
    /// Create a canonical column name (the standardized schema name)
    fn canonical(self) -> CanonicalColumnName;

    /// Create a source column name (the actual DataFrame column name)
    fn source(self) -> SourceColumnName;
}

impl ColumnNameExt for &str {
    fn canonical(self) -> CanonicalColumnName {
        CanonicalColumnName::new(self)
    }

    fn source(self) -> SourceColumnName {
        SourceColumnName::new(self)
    }
}

impl ColumnNameExt for String {
    fn canonical(self) -> CanonicalColumnName {
        CanonicalColumnName::new(self)
    }

    fn source(self) -> SourceColumnName {
        SourceColumnName::new(self)
    }
}
// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_source_column_name() {
        let col = SourceColumnName::new("pump_station_id");
        assert_eq!(col.as_str(), "pump_station_id");
        assert_eq!(&*col, "pump_station_id"); // Deref
        assert_eq!(format!("{}", col), "Source: pump_station_id"); // Display
    }

    #[test]
    fn test_canonical_column_name() {
        let col = CanonicalColumnName::new("station");
        assert_eq!(col.as_str(), "station");
        assert_eq!(&*col, "station"); // Deref
        assert_eq!(format!("{}", col), "Canonical: station"); // Display
    }

    #[test]
    fn test_column_name_equality() {
        let a = SourceColumnName::new("col_a");
        let b = SourceColumnName::new("col_a");
        let c = SourceColumnName::new("col_c");

        assert_eq!(a, b);
        assert_ne!(a, c);
    }

    #[test]
    fn test_domain_signature_stack() {
        let sig1 =
            DomainSignature::measurement("station", "time").with_components(vec!["color".into()]);
        let sig2 =
            DomainSignature::measurement("station", "time").with_components(vec!["color".into()]);

        assert!(sig1.can_stack_with(&sig2));
    }

    #[test]
    fn test_domain_signature_join() {
        let sig1 = DomainSignature::measurement("station", "time");
        let sig2 = DomainSignature::measurement("station", "time")
            .with_components(vec!["sensor_type".into()]);

        assert!(!sig1.can_stack_with(&sig2)); // Different components
        assert!(sig1.can_join_with(&sig2)); // Same subject
        assert!(sig1.needs_component_reduction(&sig2));
    }

    #[test]
    fn test_domain_signature_quality_vs_measurement() {
        let quality_sig = DomainSignature::quality("station");
        let measurement_sig = DomainSignature::measurement("station", "time");

        assert!(!quality_sig.can_stack_with(&measurement_sig));
        assert!(quality_sig.can_join_with(&measurement_sig));
    }
}