spark_connect_core/
window.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
//! Utility structs for defining a window over a DataFrame

use crate::expressions::{ToExpr, ToLiteralExpr, ToVecExpr};
use crate::plan::sort_order;

use crate::spark;
use crate::spark::expression::window;

/// A window specification that defines the partitioning, ordering, and frame boundaries.
///
/// **Recommended to create a WindowSpec using [Window] and not directly**
#[derive(Debug, Default, Clone)]
pub struct WindowSpec {
    pub partition_spec: Vec<spark::Expression>,
    pub order_spec: Vec<spark::expression::SortOrder>,
    pub frame_spec: Option<Box<window::WindowFrame>>,
}

impl WindowSpec {
    pub fn new(
        partition_spec: Vec<spark::Expression>,
        order_spec: Vec<spark::expression::SortOrder>,
        frame_spec: Option<Box<window::WindowFrame>>,
    ) -> WindowSpec {
        WindowSpec {
            partition_spec,
            order_spec,
            frame_spec,
        }
    }

    pub fn partition_by<I: ToVecExpr>(self, cols: I) -> WindowSpec {
        WindowSpec::new(cols.to_vec_expr(), self.order_spec, self.frame_spec)
    }

    pub fn order_by<I, T>(self, cols: I) -> WindowSpec
    where
        T: ToExpr,
        I: IntoIterator<Item = T>,
    {
        let order_spec = sort_order(cols);

        WindowSpec::new(self.partition_spec, order_spec, self.frame_spec)
    }

    pub fn rows_between(self, start: i64, end: i64) -> WindowSpec {
        let frame_spec = WindowSpec::window_frame(true, start, end);

        WindowSpec::new(self.partition_spec, self.order_spec, frame_spec)
    }

    pub fn range_between(self, start: i64, end: i64) -> WindowSpec {
        let frame_spec = WindowSpec::window_frame(false, start, end);

        WindowSpec::new(self.partition_spec, self.order_spec, frame_spec)
    }

    fn frame_boundary(value: i64) -> Option<Box<window::window_frame::FrameBoundary>> {
        match value {
            0 => {
                let boundary = Some(window::window_frame::frame_boundary::Boundary::CurrentRow(
                    true,
                ));

                Some(Box::new(window::window_frame::FrameBoundary { boundary }))
            }
            i64::MIN => {
                let boundary = Some(window::window_frame::frame_boundary::Boundary::Unbounded(
                    true,
                ));

                Some(Box::new(window::window_frame::FrameBoundary { boundary }))
            }
            _ => {
                // !TODO - I don't like casting this to i32
                // however, the window boundary is expecting an INT and not a BIGINT
                // i64 is a BIGINT (i.e. Long)
                let expr = (value as i32).to_literal_expr();

                let boundary = Some(window::window_frame::frame_boundary::Boundary::Value(
                    Box::new(expr),
                ));

                Some(Box::new(window::window_frame::FrameBoundary { boundary }))
            }
        }
    }

    fn window_frame(row_frame: bool, start: i64, end: i64) -> Option<Box<window::WindowFrame>> {
        let frame_type = match row_frame {
            true => 1,
            false => 2,
        };

        let lower = WindowSpec::frame_boundary(start);
        let upper = WindowSpec::frame_boundary(end);

        Some(Box::new(window::WindowFrame {
            frame_type,
            lower,
            upper,
        }))
    }
}

/// Primary utility struct for defining window in DataFrames
#[derive(Debug, Default, Clone)]
pub struct Window {
    spec: WindowSpec,
}

impl Window {
    /// Creates a new empty [WindowSpec]
    pub fn new() -> Self {
        Window {
            spec: WindowSpec::default(),
        }
    }

    /// Returns 0
    pub fn current_row() -> i64 {
        0
    }

    /// Returns [i64::MAX]
    pub fn unbounded_following() -> i64 {
        i64::MAX
    }

    /// Returns [i64::MIN]
    pub fn unbounded_preceding() -> i64 {
        i64::MIN
    }

    /// Creates a [WindowSpec] with the partitioning defined
    pub fn partition_by<I: ToVecExpr>(mut self, cols: I) -> WindowSpec {
        self.spec = self.spec.partition_by(cols);

        self.spec
    }

    /// Creates a [WindowSpec] with the ordering defined
    pub fn order_by<I, T>(mut self, cols: I) -> WindowSpec
    where
        T: ToExpr,
        I: IntoIterator<Item = T>,
    {
        self.spec = self.spec.order_by(cols);

        self.spec
    }

    /// Creates a [WindowSpec] with the frame boundaries defined, from start (inclusive) to end (inclusive).
    ///
    /// Both start and end are relative from the current row. For example, “0” means “current row”,
    /// while “-1” means one off before the current row, and “5” means the five off after the current row.
    ///
    /// Recommended to use [Window::unboundedPreceding], [Window::unboundedFollowing], and [Window::currentRow]
    /// to specify special boundary values, rather than using integral values directly.
    ///
    /// # Example
    ///
    /// ```
    /// let window = Window::new()
    ///     .partition_by(col("name"))
    ///     .order_by([col("age")])
    ///     .range_between(Window::unboundedPreceding(), Window::currentRow());
    ///
    /// let df = df.with_column("rank", rank().over(window.clone()))
    ///     .with_column("min", min("age").over(window));
    /// ```
    pub fn range_between(mut self, start: i64, end: i64) -> WindowSpec {
        self.spec = self.spec.range_between(start, end);

        self.spec
    }

    /// Creates a [WindowSpec] with the frame boundaries defined, from start (inclusive) to end (inclusive).
    ///
    /// Both start and end are relative from the current row. For example, “0” means “current row”,
    /// while “-1” means one off before the current row, and “5” means the five off after the current row.
    ///
    /// Recommended to use [Window::unboundedPreceding], [Window::unboundedFollowing], and [Window::currentRow]
    /// to specify special boundary values, rather than using integral values directly.
    ///
    /// # Example
    ///
    /// ```
    /// let window = Window::new()
    ///     .partition_by(col("name"))
    ///     .order_by([col("age")])
    ///     .rows_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 rows_between(mut self, start: i64, end: i64) -> WindowSpec {
        self.spec = self.spec.rows_between(start, end);

        self.spec
    }
}

#[cfg(test)]
mod tests {

    use arrow::{
        array::{ArrayRef, Int32Array, Int64Array, StringArray},
        datatypes::{DataType, Field, Schema},
        record_batch::RecordBatch,
    };

    use std::sync::Arc;

    use super::*;

    use crate::errors::SparkError;
    use crate::functions::*;
    use crate::SparkSession;
    use crate::SparkSessionBuilder;

    async fn setup() -> SparkSession {
        println!("SparkSession Setup");

        let connection = "sc://127.0.0.1:15002/;user_id=rust_window";

        SparkSessionBuilder::remote(connection)
            .build()
            .await
            .unwrap()
    }

    fn mock_data() -> RecordBatch {
        let id: ArrayRef = Arc::new(Int64Array::from(vec![1, 1, 2, 1, 2, 3]));
        let category: ArrayRef = Arc::new(StringArray::from(vec!["a", "a", "a", "b", "b", "b"]));

        RecordBatch::try_from_iter(vec![("id", id), ("category", category)]).unwrap()
    }

    #[tokio::test]
    async fn test_window_over() -> Result<(), SparkError> {
        let spark = setup().await;

        let name: ArrayRef = Arc::new(StringArray::from(vec!["Alice", "Bob"]));
        let age: ArrayRef = Arc::new(Int64Array::from(vec![2, 5]));

        let data = RecordBatch::try_from_iter(vec![("name", name), ("age", age)])?;

        let df = spark.create_dataframe(&data)?;

        let window = Window::new()
            .partition_by(col("name"))
            .order_by([col("age")])
            .rows_between(Window::unbounded_preceding(), Window::current_row());

        let res = df
            .with_column("rank", rank().over(window.clone()))
            .with_column("min", min("age").over(window))
            .collect()
            .await?;

        let name: ArrayRef = Arc::new(StringArray::from(vec!["Alice", "Bob"]));
        let age: ArrayRef = Arc::new(Int64Array::from(vec![2, 5]));
        let rank: ArrayRef = Arc::new(Int32Array::from(vec![1, 1]));
        let min = age.clone();

        let schema = Schema::new(vec![
            Field::new("name", DataType::Utf8, false),
            Field::new("age", DataType::Int64, false),
            Field::new("rank", DataType::Int32, false),
            Field::new("min", DataType::Int64, true),
        ]);

        let expected = RecordBatch::try_new(Arc::new(schema), vec![name, age, rank, min])?;

        assert_eq!(expected, res);

        Ok(())
    }

    #[tokio::test]
    async fn test_window_orderby() -> Result<(), SparkError> {
        let spark = setup().await;

        let data = mock_data();

        let df = spark.create_dataframe(&data)?;

        let window = Window::new()
            .partition_by(col("id"))
            .order_by([col("category")]);

        let res = df
            .with_column("row_number", row_number().over(window))
            .collect()
            .await?;

        let id: ArrayRef = Arc::new(Int64Array::from(vec![1, 1, 1, 2, 2, 3]));
        let category: ArrayRef = Arc::new(StringArray::from(vec!["a", "a", "b", "a", "b", "b"]));
        let row_number: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3, 1, 2, 1]));

        let expected = RecordBatch::try_from_iter(vec![
            ("id", id),
            ("category", category),
            ("row_number", row_number),
        ])?;

        assert_eq!(expected, res);

        Ok(())
    }

    #[tokio::test]
    async fn test_window_partitionby() -> Result<(), SparkError> {
        let spark = setup().await;

        let data = mock_data();

        let df = spark.create_dataframe(&data)?;

        let window = Window::new()
            .partition_by(col("category"))
            .order_by([col("id")]);

        let res = df
            .with_column("row_number", row_number().over(window))
            .collect()
            .await?;

        let id: ArrayRef = Arc::new(Int64Array::from(vec![1, 1, 2, 1, 2, 3]));
        let category: ArrayRef = Arc::new(StringArray::from(vec!["a", "a", "a", "b", "b", "b"]));
        let row_number: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3, 1, 2, 3]));

        let expected = RecordBatch::try_from_iter(vec![
            ("id", id),
            ("category", category),
            ("row_number", row_number),
        ])?;

        assert_eq!(expected, res);

        Ok(())
    }

    #[tokio::test]
    async fn test_window_rangebetween() -> Result<(), SparkError> {
        let spark = setup().await;

        let data = mock_data();

        let df = spark.create_dataframe(&data)?;

        let window = Window::new()
            .partition_by(col("category"))
            .order_by([col("id")])
            .range_between(Window::current_row(), 1);

        let res = df
            .with_column("sum", sum("id").over(window))
            .sort([col("id"), col("category")])
            .collect()
            .await?;

        let id: ArrayRef = Arc::new(Int64Array::from(vec![1, 1, 1, 2, 2, 3]));
        let category: ArrayRef = Arc::new(StringArray::from(vec!["a", "a", "b", "a", "b", "b"]));
        let sum: ArrayRef = Arc::new(Int64Array::from(vec![4, 4, 3, 2, 5, 3]));

        let expected = RecordBatch::try_from_iter_with_nullable(vec![
            ("id", id, false),
            ("category", category, false),
            ("sum", sum, true),
        ])?;

        assert_eq!(expected, res);

        Ok(())
    }

    #[tokio::test]
    async fn test_window_rowsbetween() -> Result<(), SparkError> {
        let spark = setup().await;

        let data = mock_data();

        let df = spark.create_dataframe(&data)?;

        let window = Window::new()
            .partition_by(col("category"))
            .order_by([col("id")])
            .rows_between(Window::current_row(), 1);

        let res = df
            .with_column("sum", sum("id").over(window))
            .sort([col("id"), col("category"), col("sum")])
            .collect()
            .await?;

        let id: ArrayRef = Arc::new(Int64Array::from(vec![1, 1, 1, 2, 2, 3]));
        let category: ArrayRef = Arc::new(StringArray::from(vec!["a", "a", "b", "a", "b", "b"]));
        let sum: ArrayRef = Arc::new(Int64Array::from(vec![2, 3, 3, 2, 5, 3]));

        let expected = RecordBatch::try_from_iter_with_nullable(vec![
            ("id", id, false),
            ("category", category, false),
            ("sum", sum, true),
        ])?;

        assert_eq!(expected, res);

        Ok(())
    }
}