spark_connect_core/
column.rs

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
//! [Column] represents a column in a DataFrame that holds a [spark::Expression]
use std::convert::From;
use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Not, Rem, Sub};

use crate::spark;

use crate::expressions::{ToExpr, ToLiteralExpr};
use crate::functions::invoke_func;
use crate::types::DataType;
use crate::window::WindowSpec;

/// # Column
///
/// A column holds a specific [spark::Expression] which will be resolved once an action is called.
/// The columns are resolved by the Spark Connect server of the remote session.
///
/// A column instance can be created by in a similar way as to the Spark API. A column with created
/// with `col("*")` or `col("name.*")` is created as an unresolved star attribute which will select
/// all columns or references in the specified column.
///
/// ```rust
/// use spark_connect_rs::{SparkSession, SparkSessionBuilder};
///
/// let spark: SparkSession = SparkSessionBuilder::remote("sc://127.0.0.1:15002/;user_id=example_rs".to_string())
///         .build()
///         .await?;
///
/// // As a &str representing an unresolved column in the dataframe
/// spark.range(None, 1, 1, Some(1)).select("id");
///
/// // By using the `col` function
/// spark.range(None, 1, 1, Some(1)).select(col("id"));
///
/// // By using the `lit` function to return a literal value
/// spark.range(None, 1, 1, Some(1)).select(lit(4.0).alias("num_col"));
///
/// ```
#[derive(Clone, Debug)]
pub struct Column {
    /// a [spark::Expression] containing any unresolved value to be leveraged in a [spark::Plan]
    pub expression: spark::Expression,
}

/// Trait used to cast columns to a specific [DataType]
///
/// Either with a String or a [DataType]
pub trait CastToDataType {
    fn cast_to_data_type(&self) -> spark::expression::cast::CastToType;
}

impl CastToDataType for DataType {
    fn cast_to_data_type(&self) -> spark::expression::cast::CastToType {
        spark::expression::cast::CastToType::Type(self.clone().into())
    }
}

impl CastToDataType for String {
    fn cast_to_data_type(&self) -> spark::expression::cast::CastToType {
        spark::expression::cast::CastToType::TypeStr(self.to_string())
    }
}

impl CastToDataType for &str {
    fn cast_to_data_type(&self) -> spark::expression::cast::CastToType {
        spark::expression::cast::CastToType::TypeStr(self.to_string())
    }
}

impl Column {
    /// Returns the column with a new name
    ///
    /// # Example:
    /// ```rust
    /// let cols = [
    ///     col("name").alias("new_name"),
    ///     col("age").alias("new_age")
    /// ];
    ///
    /// df.select(cols);
    /// ```
    pub fn alias(self, value: &str) -> Column {
        let alias = spark::expression::Alias {
            expr: Some(Box::new(self.expression)),
            name: vec![value.to_string()],
            metadata: None,
        };

        let expression = spark::Expression {
            expr_type: Some(spark::expression::ExprType::Alias(Box::new(alias))),
        };

        Column::from(expression)
    }

    /// An alias for the function `alias`
    pub fn name(self, value: &str) -> Column {
        self.alias(value)
    }

    /// Returns a sorted expression based on the ascending order of the column
    ///
    /// # Example:
    /// ```rust
    /// let df: DataFrame = df.sort(col("id").asc());
    ///
    /// let df: DataFrame = df.sort(asc(col("id")));
    /// ```
    pub fn asc(self) -> Column {
        self.asc_nulls_first()
    }

    pub fn asc_nulls_first(self) -> Column {
        let asc = spark::expression::SortOrder {
            child: Some(Box::new(self.expression)),
            direction: 1,
            null_ordering: 1,
        };

        let expression = spark::Expression {
            expr_type: Some(spark::expression::ExprType::SortOrder(Box::new(asc))),
        };

        Column::from(expression)
    }

    pub fn asc_nulls_last(self) -> Column {
        let asc = spark::expression::SortOrder {
            child: Some(Box::new(self.expression)),
            direction: 1,
            null_ordering: 2,
        };

        let expression = spark::Expression {
            expr_type: Some(spark::expression::ExprType::SortOrder(Box::new(asc))),
        };

        Column::from(expression)
    }

    /// Returns a sorted expression based on the ascending order of the column
    ///
    /// # Example:
    /// ```rust
    /// let df: DataFrame = df.sort(col("id").desc());
    ///
    /// let df: DataFrame = df.sort(desc(col("id")));
    /// ```
    pub fn desc(self) -> Column {
        self.desc_nulls_first()
    }

    pub fn desc_nulls_first(self) -> Column {
        let asc = spark::expression::SortOrder {
            child: Some(Box::new(self.expression)),
            direction: 2,
            null_ordering: 1,
        };

        let expression = spark::Expression {
            expr_type: Some(spark::expression::ExprType::SortOrder(Box::new(asc))),
        };

        Column::from(expression)
    }

    pub fn desc_nulls_last(self) -> Column {
        let asc = spark::expression::SortOrder {
            child: Some(Box::new(self.expression)),
            direction: 2,
            null_ordering: 2,
        };

        let expression = spark::Expression {
            expr_type: Some(spark::expression::ExprType::SortOrder(Box::new(asc))),
        };

        Column::from(expression)
    }

    pub fn drop_fields<'a, I>(self, field_names: I) -> Column
    where
        I: IntoIterator<Item = &'a str>,
    {
        let mut parent_col = self.expression;

        for field in field_names {
            parent_col = spark::Expression {
                expr_type: Some(spark::expression::ExprType::UpdateFields(Box::new(
                    spark::expression::UpdateFields {
                        struct_expression: Some(Box::new(parent_col)),
                        field_name: field.to_string(),
                        value_expression: None,
                    },
                ))),
            };
        }

        Column::from(parent_col)
    }

    pub fn with_field(self, field_name: &str, col: Column) -> Column {
        let update_field = spark::Expression {
            expr_type: Some(spark::expression::ExprType::UpdateFields(Box::new(
                spark::expression::UpdateFields {
                    struct_expression: Some(Box::new(self.expression)),
                    field_name: field_name.to_string(),
                    value_expression: Some(Box::new(col.to_literal_expr())),
                },
            ))),
        };

        Column::from(update_field)
    }

    pub fn substr<T: ToExpr>(self, start_pos: T, length: T) -> Column {
        invoke_func(
            "substr",
            vec![self.to_expr(), start_pos.to_expr(), length.to_expr()],
        )
    }

    /// Casts the column into the Spark DataType
    ///
    /// # Arguments:
    ///
    /// * `to_type` is a string or [DataType] of the target type
    ///
    /// # Example:
    /// ```rust
    /// use crate::types::DataType;
    ///
    /// let df = df.select([
    ///       col("age").cast("int"),
    ///       col("name").cast("string")
    ///     ])
    ///
    /// // Using DataTypes
    /// let df = df.select([
    ///       col("age").cast(DataType::Integer),
    ///       col("name").cast(DataType::String)
    ///     ])
    /// ```
    pub fn cast<T: CastToDataType>(self, to_type: T) -> Column {
        let cast = spark::expression::Cast {
            expr: Some(Box::new(self.expression)),
            cast_to_type: Some(to_type.cast_to_data_type()),
        };

        let expression = spark::Expression {
            expr_type: Some(spark::expression::ExprType::Cast(Box::new(cast))),
        };

        Column::from(expression)
    }

    /// A boolean expression that is evaluated to `true` if the value of the expression is
    /// contained by the evaluated values of the arguments
    ///
    /// # Arguments:
    ///
    /// * `cols` a value that implements the [ToLiteralExpr] trait
    ///
    /// # Example:
    /// ```rust
    /// df.filter(col("name").isin(["Jorge", "Bob"]));
    /// ```
    pub fn isin<T: ToLiteralExpr>(self, cols: Vec<T>) -> Column {
        let mut values = cols
            .iter()
            .map(|col| Column::from(col.to_literal_expr()))
            .collect::<Vec<Column>>();

        values.insert(0, self);

        invoke_func("in", values)
    }

    /// A boolean expression that is evaluated to `true` if the value is in the Column
    ///
    /// # Arguments:
    ///
    /// * `cols`: a col reference that is translated into an [spark::Expression]
    ///
    /// # Example:
    /// ```rust
    /// df.filter(col("name").contains("ge"));
    /// ```
    pub fn contains<T: ToLiteralExpr>(self, other: T) -> Column {
        invoke_func("contains", vec![self.to_expr(), other.to_literal_expr()])
    }

    /// A filter expression that evaluates if the column startswith a string literal
    pub fn startswith<T: ToLiteralExpr>(self, other: T) -> Column {
        invoke_func("startswith", vec![self.to_expr(), other.to_literal_expr()])
    }

    /// A filter expression that evaluates if the column endswith a string literal
    pub fn endswith<T: ToLiteralExpr>(self, other: T) -> Column {
        invoke_func("endswith", vec![self.to_expr(), other.to_literal_expr()])
    }

    /// A SQL LIKE filter expression that evaluates the column based on a case sensitive match
    pub fn like<T: ToLiteralExpr>(self, other: T) -> Column {
        invoke_func("like", vec![self.to_expr(), other.to_literal_expr()])
    }

    /// A SQL ILIKE filter expression that evaluates the column based on a case insensitive match
    pub fn ilike<T: ToLiteralExpr>(self, other: T) -> Column {
        invoke_func("ilike", vec![self.to_expr(), other.to_literal_expr()])
    }

    /// A SQL RLIKE filter expression that evaluates the column based on a regex match
    pub fn rlike<T: ToLiteralExpr>(self, other: T) -> Column {
        invoke_func("rlike", vec![self.to_expr(), other.to_literal_expr()])
    }

    /// Equality comparion. Cannot overload the '==' and return something other
    /// than a bool
    pub fn eq<T: ToExpr>(self, other: T) -> Column {
        invoke_func("==", vec![self.to_expr(), other.to_expr()])
    }

    /// Logical AND comparion. Cannot overload the '&&' and return something other
    /// than a bool
    pub fn and<T: ToExpr>(self, other: T) -> Column {
        invoke_func("and", vec![self.to_expr(), other.to_expr()])
    }

    /// Logical OR comparion.
    pub fn or<T: ToExpr>(self, other: T) -> Column {
        invoke_func("or", vec![self.to_expr(), other.to_expr()])
    }

    /// A filter expression that evaluates to true is the expression is null
    pub fn is_null(self) -> Column {
        invoke_func("isnull", self)
    }

    /// A filter expression that evaluates to true is the expression is NOT null
    pub fn is_not_null(self) -> Column {
        invoke_func("isnotnull", self)
    }

    pub fn is_nan(self) -> Column {
        invoke_func("isNaN", self)
    }

    /// Defines a windowing column
    /// # Arguments:
    ///
    /// * `window`: a [WindowSpec]
    ///
    /// # Example
    ///
    /// ```
    /// let window = Window::new()
    ///     .partition_by(col("name"))
    ///     .order_by([col("age")])
    ///     .range_between(Window::unbounded_preceding(), Window::current_row());
    ///
    /// let df = df.with_column("rank", rank().over(window.clone()))
    ///     .with_column("min", min("age").over(window));
    /// ```
    pub fn over(self, window: WindowSpec) -> Column {
        let window_expr = spark::expression::Window {
            window_function: Some(Box::new(self.expression)),
            partition_spec: window.partition_spec,
            order_spec: window.order_spec,
            frame_spec: window.frame_spec,
        };

        let expression = spark::Expression {
            expr_type: Some(spark::expression::ExprType::Window(Box::new(window_expr))),
        };

        Column::from(expression)
    }
}

impl From<spark::Expression> for Column {
    /// Used for creating columns from a [spark::Expression]
    fn from(expression: spark::Expression) -> Self {
        Self { expression }
    }
}

impl From<&str> for Column {
    /// `&str` values containing a `*` will be created as an unresolved star expression
    /// Otherwise, the value is created as an unresolved attribute
    fn from(value: &str) -> Self {
        let expression = match value {
            "*" => spark::Expression {
                expr_type: Some(spark::expression::ExprType::UnresolvedStar(
                    spark::expression::UnresolvedStar {
                        unparsed_target: None,
                    },
                )),
            },
            value if value.ends_with(".*") => spark::Expression {
                expr_type: Some(spark::expression::ExprType::UnresolvedStar(
                    spark::expression::UnresolvedStar {
                        unparsed_target: Some(value.to_string()),
                    },
                )),
            },
            _ => spark::Expression {
                expr_type: Some(spark::expression::ExprType::UnresolvedAttribute(
                    spark::expression::UnresolvedAttribute {
                        unparsed_identifier: value.to_string(),
                        plan_id: None,
                    },
                )),
            },
        };

        Column::from(expression)
    }
}

impl Add for Column {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        invoke_func("+", vec![self, other])
    }
}

impl Neg for Column {
    type Output = Self;

    fn neg(self) -> Self {
        invoke_func("negative", self)
    }
}

impl Sub for Column {
    type Output = Self;

    fn sub(self, other: Self) -> Self {
        invoke_func("-", vec![self, other])
    }
}

impl Mul for Column {
    type Output = Self;

    fn mul(self, other: Self) -> Self {
        invoke_func("*", vec![self, other])
    }
}

impl Div for Column {
    type Output = Self;

    fn div(self, other: Self) -> Self {
        invoke_func("/", vec![self, other])
    }
}

impl Rem for Column {
    type Output = Self;

    fn rem(self, other: Self) -> Self {
        invoke_func("%", vec![self, other])
    }
}

impl BitOr for Column {
    type Output = Self;

    fn bitor(self, other: Self) -> Self {
        invoke_func("|", vec![self, other])
    }
}

impl BitAnd for Column {
    type Output = Self;

    fn bitand(self, other: Self) -> Self {
        invoke_func("&", vec![self, other])
    }
}

impl BitXor for Column {
    type Output = Self;

    fn bitxor(self, other: Self) -> Self {
        invoke_func("^", vec![self, other])
    }
}

impl Not for Column {
    type Output = Self;

    fn not(self) -> Self::Output {
        invoke_func("not", vec![self])
    }
}