datafusion-extra-functions 0.5.3

Extra Functions for DataFusion
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
use datafusion::logical_expr::AggregateUDFImpl;
use datafusion::{arrow, common, error, functions_aggregate, logical_expr};
use std::fmt;
use std::ops::Deref;

make_udaf_expr_and_func!(
    MaxByFunction,
    max_by,
    x y,
    "Returns the value of the first column corresponding to the maximum value in the second column.",
    max_by_udaf
);

#[derive(Eq, Hash, PartialEq)]
pub struct MaxByFunction {
    null_first: bool,
    signature: logical_expr::Signature,
}

impl fmt::Debug for MaxByFunction {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("MaxBy")
            .field("name", &self.name())
            .field("signature", &self.signature)
            .field("accumulator", &"<FUNC>")
            .finish()
    }
}
impl Default for MaxByFunction {
    fn default() -> Self {
        Self::new(true)
    }
}

impl MaxByFunction {
    pub fn new(null_first: bool) -> Self {
        Self {
            null_first,
            signature: logical_expr::Signature::user_defined(logical_expr::Volatility::Immutable),
        }
    }
}

fn get_min_max_by_result_type(
    input_types: &[arrow::datatypes::DataType],
) -> error::Result<Vec<arrow::datatypes::DataType>> {
    match &input_types[0] {
        arrow::datatypes::DataType::Dictionary(_, dict_value_type) => {
            // x add checker, if the value type is complex data type
            let mut result = vec![dict_value_type.deref().clone()];
            // Preserve all other argument types
            result.extend_from_slice(&input_types[1..]);
            Ok(result)
        }
        _ => Ok(input_types.to_vec()),
    }
}

impl logical_expr::AggregateUDFImpl for MaxByFunction {
    fn name(&self) -> &str {
        "max_by"
    }

    fn signature(&self) -> &logical_expr::Signature {
        &self.signature
    }

    fn return_type(
        &self,
        arg_types: &[arrow::datatypes::DataType],
    ) -> error::Result<arrow::datatypes::DataType> {
        Ok(arg_types[0].to_owned())
    }

    fn accumulator(
        &self,
        _acc_args: logical_expr::function::AccumulatorArgs,
    ) -> error::Result<Box<dyn logical_expr::Accumulator>> {
        common::exec_err!("should not reach here")
    }

    fn coerce_types(
        &self,
        arg_types: &[arrow::datatypes::DataType],
    ) -> error::Result<Vec<arrow::datatypes::DataType>> {
        get_min_max_by_result_type(arg_types)
    }

    fn simplify(&self) -> Option<logical_expr::function::AggregateFunctionSimplification> {
        let null_first = self.null_first;
        let simplify = move |mut aggr_func: logical_expr::expr::AggregateFunction,
                             _: &logical_expr::simplify::SimplifyContext| {
            let mut order_by = aggr_func.params.order_by;
            let (second_arg, first_arg) = (
                aggr_func.params.args.remove(1),
                aggr_func.params.args.remove(0),
            );
            let sort = logical_expr::expr::Sort::new(second_arg, true, null_first);
            order_by.push(sort);
            let func = logical_expr::expr::AggregateFunction::new_udf(
                functions_aggregate::first_last::last_value_udaf(),
                vec![first_arg],
                aggr_func.params.distinct,
                aggr_func.params.filter,
                order_by,
                aggr_func.params.null_treatment,
            );
            let func = logical_expr::expr::Expr::AggregateFunction(func);
            Ok(func)
        };
        Some(Box::new(simplify))
    }
}

make_udaf_expr_and_func!(
    MinByFunction,
    min_by,
    x y,
    "Returns the value of the first column corresponding to the minimum value in the second column.",
    min_by_udaf
);

#[derive(Eq, Hash, PartialEq)]
pub struct MinByFunction {
    null_first: bool,
    signature: logical_expr::Signature,
}

impl fmt::Debug for MinByFunction {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("MinBy")
            .field("name", &self.name())
            .field("signature", &self.signature)
            .field("accumulator", &"<FUNC>")
            .finish()
    }
}

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

impl MinByFunction {
    pub fn new(null_first: bool) -> Self {
        Self {
            null_first,
            signature: logical_expr::Signature::user_defined(logical_expr::Volatility::Immutable),
        }
    }
}

impl logical_expr::AggregateUDFImpl for MinByFunction {
    fn name(&self) -> &str {
        "min_by"
    }

    fn signature(&self) -> &logical_expr::Signature {
        &self.signature
    }

    fn return_type(
        &self,
        arg_types: &[arrow::datatypes::DataType],
    ) -> error::Result<arrow::datatypes::DataType> {
        Ok(arg_types[0].to_owned())
    }

    fn accumulator(
        &self,
        _acc_args: logical_expr::function::AccumulatorArgs,
    ) -> error::Result<Box<dyn logical_expr::Accumulator>> {
        common::exec_err!("should not reach here")
    }

    fn coerce_types(
        &self,
        arg_types: &[arrow::datatypes::DataType],
    ) -> error::Result<Vec<arrow::datatypes::DataType>> {
        get_min_max_by_result_type(arg_types)
    }

    fn simplify(&self) -> Option<logical_expr::function::AggregateFunctionSimplification> {
        let null_first = self.null_first;
        let simplify = move |mut aggr_func: logical_expr::expr::AggregateFunction,
                             _: &logical_expr::simplify::SimplifyContext| {
            let mut order_by = aggr_func.params.order_by;
            let (second_arg, first_arg) = (
                aggr_func.params.args.remove(1),
                aggr_func.params.args.remove(0),
            );

            let sort = logical_expr::expr::Sort::new(second_arg, false, null_first);
            order_by.push(sort); // false for ascending sort
            let func = logical_expr::expr::AggregateFunction::new_udf(
                functions_aggregate::first_last::last_value_udaf(),
                vec![first_arg],
                aggr_func.params.distinct,
                aggr_func.params.filter,
                order_by,
                aggr_func.params.null_treatment,
            );
            let func = logical_expr::expr::Expr::AggregateFunction(func);
            Ok(func)
        };
        Some(Box::new(simplify))
    }
}

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

    use datafusion::arrow::array::ArrayAccessor;
    use datafusion::{arrow, datasource, error, prelude};
    use std::sync;

    const TEST_TABLE_NAME: &str = "types";
    const STRING_COLUMN_NAME: &str = "string";
    const DICTIONARY_COLUMN_NAME: &str = "dict_string";
    const INT64_COLUMN_NAME: &str = "int64";
    const FLOAT64_COLUMN_NAME: &str = "float64";

    const MIN_STRING_VALUE: &str = "a";
    const MID_STRING_VALUE: &str = "b";
    const MAX_STRING_VALUE: &str = "c";
    const MIN_FLOAT_VALUE: f64 = 0.25;
    const MID_FLOAT_VALUE: f64 = 0.5;
    const MAX_FLOAT_VALUE: f64 = 0.75;
    const MIN_INT_VALUE: i64 = -1;
    const MID_INT_VALUE: i64 = 0;
    const MAX_INT_VALUE: i64 = 1;
    const MIN_DICTIONARY_VALUE: &str = "a";
    const MID_DICTIONARY_VALUE: &str = "b";
    const MAX_DICTIONARY_VALUE: &str = "c";

    fn test_schema() -> sync::Arc<arrow::datatypes::Schema> {
        sync::Arc::new(arrow::datatypes::Schema::new(vec![
            arrow::datatypes::Field::new(
                STRING_COLUMN_NAME,
                arrow::datatypes::DataType::Utf8,
                false,
            ),
            arrow::datatypes::Field::new_dictionary(
                DICTIONARY_COLUMN_NAME,
                arrow::datatypes::DataType::Int32,
                arrow::datatypes::DataType::Utf8,
                false,
            ),
            arrow::datatypes::Field::new(
                INT64_COLUMN_NAME,
                arrow::datatypes::DataType::Int64,
                false,
            ),
            arrow::datatypes::Field::new(
                FLOAT64_COLUMN_NAME,
                arrow::datatypes::DataType::Float64,
                false,
            ),
        ]))
    }

    fn test_data(
        schema: sync::Arc<arrow::datatypes::Schema>,
    ) -> Vec<arrow::record_batch::RecordBatch> {
        vec![
            arrow::record_batch::RecordBatch::try_new(
                schema,
                vec![
                    sync::Arc::new(arrow::array::StringArray::from(vec![
                        MID_STRING_VALUE,
                        MIN_STRING_VALUE,
                        MAX_STRING_VALUE,
                    ])),
                    sync::Arc::new(
                        vec![
                            Some(MID_DICTIONARY_VALUE),
                            Some(MIN_DICTIONARY_VALUE),
                            Some(MAX_DICTIONARY_VALUE),
                        ]
                        .into_iter()
                        .collect::<arrow::array::DictionaryArray<arrow::datatypes::Int32Type>>(),
                    ),
                    sync::Arc::new(arrow::array::Int64Array::from(vec![
                        MID_INT_VALUE,
                        MIN_INT_VALUE,
                        MAX_INT_VALUE,
                    ])),
                    sync::Arc::new(arrow::array::Float64Array::from(vec![
                        MID_FLOAT_VALUE,
                        MIN_FLOAT_VALUE,
                        MAX_FLOAT_VALUE,
                    ])),
                ],
            )
            .unwrap(),
        ]
    }

    fn test_ctx() -> datafusion::common::Result<prelude::SessionContext> {
        let schema = test_schema();
        let data = test_data(schema.clone());
        let table = datasource::MemTable::try_new(schema, vec![data])?;
        let ctx = prelude::SessionContext::new();
        ctx.register_table(TEST_TABLE_NAME, sync::Arc::new(table))?;
        Ok(ctx)
    }

    async fn extract_single_value<T, A>(df: prelude::DataFrame) -> error::Result<T>
    where
        A: arrow::array::Array + 'static,
        for<'a> &'a A: arrow::array::ArrayAccessor,
        for<'a> <&'a A as arrow::array::ArrayAccessor>::Item: Into<T>,
    {
        let results = df.collect().await?;
        let col = results[0].column(0);
        let v1 = col.as_any().downcast_ref::<A>().unwrap();
        let value = v1.value(0).into();
        Ok(value)
    }

    #[cfg(test)]
    mod max_by {

        use super::*;

        #[tokio::test]
        async fn test_max_by_string_int() -> error::Result<()> {
            let query = format!(
                "SELECT max_by({}, {}) FROM {}",
                STRING_COLUMN_NAME, INT64_COLUMN_NAME, TEST_TABLE_NAME
            );
            let df = ctx()?.sql(&query).await?;
            let result = extract_single_value::<String, arrow::array::StringArray>(df).await?;
            assert_eq!(result, MAX_STRING_VALUE);
            Ok(())
        }

        #[tokio::test]
        async fn test_max_by_string_float() -> error::Result<()> {
            let query = format!(
                "SELECT max_by({}, {}) FROM {}",
                STRING_COLUMN_NAME, FLOAT64_COLUMN_NAME, TEST_TABLE_NAME
            );
            let df = ctx()?.sql(&query).await?;
            let result = extract_single_value::<String, arrow::array::StringArray>(df).await?;
            assert_eq!(result, MAX_STRING_VALUE);
            Ok(())
        }

        #[tokio::test]
        async fn test_max_by_float_string() -> error::Result<()> {
            let query = format!(
                "SELECT max_by({}, {}) FROM {}",
                FLOAT64_COLUMN_NAME, STRING_COLUMN_NAME, TEST_TABLE_NAME
            );
            let df = ctx()?.sql(&query).await?;
            let result = extract_single_value::<f64, arrow::array::Float64Array>(df).await?;
            assert_eq!(result, MAX_FLOAT_VALUE);
            Ok(())
        }

        #[tokio::test]
        async fn test_max_by_int_string() -> error::Result<()> {
            let query = format!(
                "SELECT max_by({}, {}) FROM {}",
                INT64_COLUMN_NAME, STRING_COLUMN_NAME, TEST_TABLE_NAME
            );
            let df = ctx()?.sql(&query).await?;
            let result = extract_single_value::<i64, arrow::array::Int64Array>(df).await?;
            assert_eq!(result, MAX_INT_VALUE);
            Ok(())
        }

        #[tokio::test]
        async fn test_max_by_dictionary_int() -> error::Result<()> {
            let query = format!(
                "SELECT max_by({}, {}) FROM {}",
                DICTIONARY_COLUMN_NAME, INT64_COLUMN_NAME, TEST_TABLE_NAME
            );
            let df = ctx()?.sql(&query).await?;
            let result = extract_single_value::<String, arrow::array::StringArray>(df).await?;
            assert_eq!(result, MAX_DICTIONARY_VALUE);
            Ok(())
        }

        #[tokio::test]
        async fn test_max_by_ignores_nulls() -> error::Result<()> {
            let query = r#"
                SELECT max_by(v, k)
                FROM (
                    VALUES
                        ('a', 1),
                        ('b', CAST(NULL AS INT)),
                        ('c', 2)
                ) AS t(v, k)
            "#;
            let df = ctx()?.sql(query).await?;
            let result = extract_single_value::<String, arrow::array::StringArray>(df).await?;
            assert_eq!(result, "c", "max_by should ignore NULLs");
            Ok(())
        }

        fn ctx() -> error::Result<prelude::SessionContext> {
            let ctx = test_ctx()?;
            let max_by_udaf = MaxByFunction::default();
            ctx.register_udaf(max_by_udaf.into());
            Ok(ctx)
        }
    }

    #[cfg(test)]
    mod min_by {

        use super::*;

        #[tokio::test]
        async fn test_min_by_string_int() -> error::Result<()> {
            let query = format!(
                "SELECT min_by({}, {}) FROM {}",
                STRING_COLUMN_NAME, INT64_COLUMN_NAME, TEST_TABLE_NAME
            );
            let df = ctx()?.sql(&query).await?;
            let result = extract_single_value::<String, arrow::array::StringArray>(df).await?;
            assert_eq!(result, MIN_STRING_VALUE);
            Ok(())
        }

        #[tokio::test]
        async fn test_min_by_string_float() -> error::Result<()> {
            let query = format!(
                "SELECT min_by({}, {}) FROM {}",
                STRING_COLUMN_NAME, FLOAT64_COLUMN_NAME, TEST_TABLE_NAME
            );
            let df = ctx()?.sql(&query).await?;
            let result = extract_single_value::<String, arrow::array::StringArray>(df).await?;
            assert_eq!(result, MIN_STRING_VALUE);
            Ok(())
        }

        #[tokio::test]
        async fn test_min_by_float_string() -> error::Result<()> {
            let query = format!(
                "SELECT min_by({}, {}) FROM {}",
                FLOAT64_COLUMN_NAME, STRING_COLUMN_NAME, TEST_TABLE_NAME
            );
            let df = ctx()?.sql(&query).await?;
            let result = extract_single_value::<f64, arrow::array::Float64Array>(df).await?;
            assert_eq!(result, MIN_FLOAT_VALUE);
            Ok(())
        }

        #[tokio::test]
        async fn test_min_by_int_string() -> error::Result<()> {
            let query = format!(
                "SELECT min_by({}, {}) FROM {}",
                INT64_COLUMN_NAME, STRING_COLUMN_NAME, TEST_TABLE_NAME
            );
            let df = ctx()?.sql(&query).await?;
            let result = extract_single_value::<i64, arrow::array::Int64Array>(df).await?;
            assert_eq!(result, MIN_INT_VALUE);
            Ok(())
        }

        #[tokio::test]
        async fn test_min_by_dictionary_int() -> error::Result<()> {
            let query = format!(
                "SELECT min_by({}, {}) FROM {}",
                DICTIONARY_COLUMN_NAME, INT64_COLUMN_NAME, TEST_TABLE_NAME
            );
            let df = ctx()?.sql(&query).await?;
            let result = extract_single_value::<String, arrow::array::StringArray>(df).await?;
            assert_eq!(result, MIN_DICTIONARY_VALUE);
            Ok(())
        }

        #[tokio::test]
        async fn test_min_by_ignores_nulls() -> error::Result<()> {
            let query = r#"
                SELECT min_by(v, k)
                FROM (
                    VALUES
                        ('a', 1),
                        ('b', CAST(NULL AS INT)),
                        ('c', 2)
                ) AS t(v, k)
            "#;
            let df = ctx()?.sql(query).await?;
            let result = extract_single_value::<String, arrow::array::StringArray>(df).await?;
            assert_eq!(result, "a", "min_by should ignore NULLs");
            Ok(())
        }

        fn ctx() -> error::Result<prelude::SessionContext> {
            let ctx = test_ctx()?;
            let min_by_udaf = MinByFunction::default();
            ctx.register_udaf(min_by_udaf.into());
            Ok(ctx)
        }
    }
}