alopex-dataframe 0.7.2

Polars-compatible DataFrame API for Alopex DB (v0.1)
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
/// Expression AST used by `DataFrame` and `LazyFrame`.
#[derive(Debug, Clone, PartialEq)]
pub enum Expr {
    /// Column reference.
    Column(String),
    /// Literal scalar value.
    Literal(Scalar),
    /// Binary operator expression.
    BinaryOp {
        left: Box<Expr>,
        op: Operator,
        right: Box<Expr>,
    },
    /// Unary operator expression.
    UnaryOp { op: UnaryOperator, expr: Box<Expr> },
    /// Aggregation expression (only valid under `group_by().agg()`).
    Agg { func: AggFunc, expr: Box<Expr> },
    /// Namespace function expression.
    Function {
        input: Box<Expr>,
        function: ExprFunction,
    },
    /// Expression alias (renames the resulting column).
    Alias { expr: Box<Expr>, name: String },
    /// Wildcard (`*`) that expands to all columns in projections.
    Wildcard,
}

/// Supported namespace expression functions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExprFunction {
    /// String namespace function.
    String(StringFunction),
    /// Datetime namespace function.
    Datetime(DatetimeFunction),
    /// List namespace function.
    List(ListFunction),
}

/// Supported `str.*` functions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StringFunction {
    /// Convert UTF-8 strings to lowercase.
    ToLowercase,
    /// Convert UTF-8 strings to uppercase.
    ToUppercase,
    /// Regex contains.
    Contains { pattern: String },
    /// Regex replacement.
    Replace {
        pattern: String,
        replacement: String,
    },
    /// Strip whitespace or the provided characters.
    StripChars { chars: Option<String> },
    /// Split by a literal separator.
    Split { separator: String },
    /// Count Unicode scalar values.
    LenChars,
    /// Extract a regex capture group.
    Extract {
        pattern: String,
        capture_group: usize,
    },
}

/// Supported `dt.*` functions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DatetimeFunction {
    /// Extract UTC year.
    Year,
    /// Extract UTC month.
    Month,
    /// Extract UTC day of month.
    Day,
    /// Extract ISO weekday.
    Weekday,
    /// Format as UTC text.
    ToString,
    /// Convert between fixed-offset time zones.
    ConvertTimeZone {
        from_offset: String,
        to_offset: String,
    },
}

/// Supported `list.*` functions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ListFunction {
    /// Join string list elements.
    Join {
        separator: String,
        null_value: Option<String>,
    },
    /// Return list length.
    Len,
    /// Test whether a list contains a non-null string value.
    Contains { value: String },
}

/// Supported binary operators.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Operator {
    /// Addition.
    Add,
    /// Subtraction.
    Sub,
    /// Multiplication.
    Mul,
    /// Division.
    Div,
    /// Equality.
    Eq,
    /// Inequality.
    Neq,
    /// Greater-than.
    Gt,
    /// Less-than.
    Lt,
    /// Greater-than-or-equal.
    Ge,
    /// Less-than-or-equal.
    Le,
    /// Boolean AND.
    And,
    /// Boolean OR.
    Or,
}

/// Supported unary operators.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum UnaryOperator {
    /// Boolean NOT.
    Not,
}

/// Supported aggregation functions.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum AggFunc {
    /// Sum of non-null values.
    Sum,
    /// Mean of non-null values.
    Mean,
    /// Count of non-null values.
    Count,
    /// Minimum of non-null values.
    Min,
    /// Maximum of non-null values.
    Max,
}

/// Scalar literal values.
#[derive(Debug, Clone, PartialEq)]
pub enum Scalar {
    /// Null literal.
    Null,
    /// Boolean literal.
    Boolean(bool),
    /// 64-bit integer literal.
    Int64(i64),
    /// 64-bit float literal.
    Float64(f64),
    /// UTF-8 string literal.
    Utf8(String),
}

impl From<()> for Scalar {
    fn from(_: ()) -> Self {
        Scalar::Null
    }
}

impl From<bool> for Scalar {
    fn from(v: bool) -> Self {
        Scalar::Boolean(v)
    }
}

impl From<i64> for Scalar {
    fn from(v: i64) -> Self {
        Scalar::Int64(v)
    }
}

impl From<f64> for Scalar {
    fn from(v: f64) -> Self {
        Scalar::Float64(v)
    }
}

impl From<String> for Scalar {
    fn from(v: String) -> Self {
        Scalar::Utf8(v)
    }
}

impl From<&str> for Scalar {
    fn from(v: &str) -> Self {
        Scalar::Utf8(v.to_string())
    }
}

impl Expr {
    /// Alias this expression (used to name output columns).
    pub fn alias(self, name: impl Into<String>) -> Expr {
        Expr::Alias {
            expr: Box::new(self),
            name: name.into(),
        }
    }

    /// Build an addition expression.
    #[allow(clippy::should_implement_trait)]
    pub fn add(self, rhs: Expr) -> Expr {
        Expr::BinaryOp {
            left: Box::new(self),
            op: Operator::Add,
            right: Box::new(rhs),
        }
    }

    /// Build a subtraction expression.
    #[allow(clippy::should_implement_trait)]
    pub fn sub(self, rhs: Expr) -> Expr {
        Expr::BinaryOp {
            left: Box::new(self),
            op: Operator::Sub,
            right: Box::new(rhs),
        }
    }

    /// Build a multiplication expression.
    #[allow(clippy::should_implement_trait)]
    pub fn mul(self, rhs: Expr) -> Expr {
        Expr::BinaryOp {
            left: Box::new(self),
            op: Operator::Mul,
            right: Box::new(rhs),
        }
    }

    /// Build a division expression.
    #[allow(clippy::should_implement_trait)]
    pub fn div(self, rhs: Expr) -> Expr {
        Expr::BinaryOp {
            left: Box::new(self),
            op: Operator::Div,
            right: Box::new(rhs),
        }
    }

    /// Build an equality predicate.
    pub fn eq(self, rhs: Expr) -> Expr {
        Expr::BinaryOp {
            left: Box::new(self),
            op: Operator::Eq,
            right: Box::new(rhs),
        }
    }

    /// Build an inequality predicate.
    pub fn neq(self, rhs: Expr) -> Expr {
        Expr::BinaryOp {
            left: Box::new(self),
            op: Operator::Neq,
            right: Box::new(rhs),
        }
    }

    /// Build a greater-than predicate.
    pub fn gt(self, rhs: Expr) -> Expr {
        Expr::BinaryOp {
            left: Box::new(self),
            op: Operator::Gt,
            right: Box::new(rhs),
        }
    }

    /// Build a less-than predicate.
    pub fn lt(self, rhs: Expr) -> Expr {
        Expr::BinaryOp {
            left: Box::new(self),
            op: Operator::Lt,
            right: Box::new(rhs),
        }
    }

    /// Build a greater-than-or-equal predicate.
    pub fn ge(self, rhs: Expr) -> Expr {
        Expr::BinaryOp {
            left: Box::new(self),
            op: Operator::Ge,
            right: Box::new(rhs),
        }
    }

    /// Build a less-than-or-equal predicate.
    pub fn le(self, rhs: Expr) -> Expr {
        Expr::BinaryOp {
            left: Box::new(self),
            op: Operator::Le,
            right: Box::new(rhs),
        }
    }

    /// Build a boolean AND predicate.
    pub fn and_(self, rhs: Expr) -> Expr {
        Expr::BinaryOp {
            left: Box::new(self),
            op: Operator::And,
            right: Box::new(rhs),
        }
    }

    /// Build a boolean OR predicate.
    pub fn or_(self, rhs: Expr) -> Expr {
        Expr::BinaryOp {
            left: Box::new(self),
            op: Operator::Or,
            right: Box::new(rhs),
        }
    }

    /// Build a boolean NOT predicate.
    pub fn not_(self) -> Expr {
        Expr::UnaryOp {
            op: UnaryOperator::Not,
            expr: Box::new(self),
        }
    }

    /// Build a `sum` aggregation.
    pub fn sum(self) -> Expr {
        Expr::Agg {
            func: AggFunc::Sum,
            expr: Box::new(self),
        }
    }

    /// Build a `mean` aggregation.
    pub fn mean(self) -> Expr {
        Expr::Agg {
            func: AggFunc::Mean,
            expr: Box::new(self),
        }
    }

    /// Build a `count` aggregation (nulls excluded).
    pub fn count(self) -> Expr {
        Expr::Agg {
            func: AggFunc::Count,
            expr: Box::new(self),
        }
    }

    /// Build a `min` aggregation.
    pub fn min(self) -> Expr {
        Expr::Agg {
            func: AggFunc::Min,
            expr: Box::new(self),
        }
    }

    /// Build a `max` aggregation.
    pub fn max(self) -> Expr {
        Expr::Agg {
            func: AggFunc::Max,
            expr: Box::new(self),
        }
    }

    /// Enter the `str.*` expression namespace.
    pub fn str(self) -> StringExpr {
        StringExpr { input: self }
    }

    /// Enter the `dt.*` expression namespace.
    pub fn dt(self) -> DatetimeExpr {
        DatetimeExpr { input: self }
    }

    /// Enter the `list.*` expression namespace.
    pub fn list(self) -> ListExpr {
        ListExpr { input: self }
    }
}

/// Builder for `str.*` expression functions.
#[derive(Debug, Clone, PartialEq)]
pub struct StringExpr {
    input: Expr,
}

impl StringExpr {
    fn function(self, function: StringFunction) -> Expr {
        Expr::Function {
            input: Box::new(self.input),
            function: ExprFunction::String(function),
        }
    }

    /// Convert UTF-8 strings to lowercase.
    pub fn to_lowercase(self) -> Expr {
        self.function(StringFunction::ToLowercase)
    }

    /// Convert UTF-8 strings to uppercase.
    pub fn to_uppercase(self) -> Expr {
        self.function(StringFunction::ToUppercase)
    }

    /// Return whether each string matches a regex pattern.
    pub fn contains(self, pattern: impl Into<String>) -> Expr {
        self.function(StringFunction::Contains {
            pattern: pattern.into(),
        })
    }

    /// Replace regex matches.
    pub fn replace(self, pattern: impl Into<String>, replacement: impl Into<String>) -> Expr {
        self.function(StringFunction::Replace {
            pattern: pattern.into(),
            replacement: replacement.into(),
        })
    }

    /// Strip whitespace from both ends.
    pub fn strip_chars(self, chars: Option<impl Into<String>>) -> Expr {
        self.function(StringFunction::StripChars {
            chars: chars.map(Into::into),
        })
    }

    /// Split by a literal separator.
    pub fn split(self, separator: impl Into<String>) -> Expr {
        self.function(StringFunction::Split {
            separator: separator.into(),
        })
    }

    /// Count Unicode scalar values.
    pub fn len_chars(self) -> Expr {
        self.function(StringFunction::LenChars)
    }

    /// Extract a regex capture group.
    pub fn extract(self, pattern: impl Into<String>, capture_group: usize) -> Expr {
        self.function(StringFunction::Extract {
            pattern: pattern.into(),
            capture_group,
        })
    }
}

/// Builder for `dt.*` expression functions.
#[derive(Debug, Clone, PartialEq)]
pub struct DatetimeExpr {
    input: Expr,
}

impl DatetimeExpr {
    fn function(self, function: DatetimeFunction) -> Expr {
        Expr::Function {
            input: Box::new(self.input),
            function: ExprFunction::Datetime(function),
        }
    }

    /// Extract UTC year.
    pub fn year(self) -> Expr {
        self.function(DatetimeFunction::Year)
    }

    /// Extract UTC month.
    pub fn month(self) -> Expr {
        self.function(DatetimeFunction::Month)
    }

    /// Extract UTC day of month.
    pub fn day(self) -> Expr {
        self.function(DatetimeFunction::Day)
    }

    /// Extract ISO weekday.
    pub fn weekday(self) -> Expr {
        self.function(DatetimeFunction::Weekday)
    }

    /// Format timestamps as UTC text.
    #[allow(clippy::inherent_to_string)]
    pub fn to_string(self) -> Expr {
        self.function(DatetimeFunction::ToString)
    }

    /// Convert between fixed-offset time zones.
    pub fn convert_time_zone(
        self,
        from_offset: impl Into<String>,
        to_offset: impl Into<String>,
    ) -> Expr {
        self.function(DatetimeFunction::ConvertTimeZone {
            from_offset: from_offset.into(),
            to_offset: to_offset.into(),
        })
    }
}

/// Builder for `list.*` expression functions.
#[derive(Debug, Clone, PartialEq)]
pub struct ListExpr {
    input: Expr,
}

impl ListExpr {
    fn function(self, function: ListFunction) -> Expr {
        Expr::Function {
            input: Box::new(self.input),
            function: ExprFunction::List(function),
        }
    }

    /// Join string list elements with a separator.
    pub fn join(self, separator: impl Into<String>, null_value: Option<impl Into<String>>) -> Expr {
        self.function(ListFunction::Join {
            separator: separator.into(),
            null_value: null_value.map(Into::into),
        })
    }

    /// Return list lengths.
    pub fn len(self) -> Expr {
        self.function(ListFunction::Len)
    }

    /// Return whether a string list contains `value`.
    pub fn contains(self, value: impl Into<String>) -> Expr {
        self.function(ListFunction::Contains {
            value: value.into(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::{AggFunc, Expr, ExprFunction, Operator, Scalar, StringFunction, UnaryOperator};
    use crate::expr::{col, lit};

    #[test]
    fn builder_and_chaining_works() {
        let expr = col("a").add(lit(1_i64)).alias("b");
        assert_eq!(
            expr,
            Expr::Alias {
                expr: Box::new(Expr::BinaryOp {
                    left: Box::new(Expr::Column("a".to_string())),
                    op: Operator::Add,
                    right: Box::new(Expr::Literal(Scalar::Int64(1))),
                }),
                name: "b".to_string(),
            }
        );
    }

    #[test]
    fn logical_and_agg_works() {
        let expr = col("x")
            .gt(lit(1_i64))
            .and_(col("y").lt(lit(10_i64)).not_())
            .alias("p");

        assert!(matches!(
            expr,
            Expr::Alias {
                expr: _,
                name
            } if name == "p"
        ));

        let agg = col("v").sum();
        assert_eq!(
            agg,
            Expr::Agg {
                func: AggFunc::Sum,
                expr: Box::new(Expr::Column("v".to_string()))
            }
        );

        let u = Expr::Column("a".to_string()).not_();
        assert_eq!(
            u,
            Expr::UnaryOp {
                op: UnaryOperator::Not,
                expr: Box::new(Expr::Column("a".to_string()))
            }
        );
    }

    #[test]
    fn namespace_builders_create_function_exprs() {
        let expr = col("name").str().to_lowercase().alias("name_lower");
        assert_eq!(
            expr,
            Expr::Alias {
                expr: Box::new(Expr::Function {
                    input: Box::new(Expr::Column("name".to_string())),
                    function: ExprFunction::String(StringFunction::ToLowercase),
                }),
                name: "name_lower".to_string(),
            }
        );
    }
}