clickhouse-datafusion 0.2.0

High-performance ClickHouse integration for Apache DataFusion with federation support
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
//! Various UDFs providing `DataFusion`'s sql parsing with some `ClickHouse` specific functionality.
//!
//! [`self::eval::ClickHouseEval`] is a sort of 'escape-hatch' to allow passing syntax directly
//! to `ClickHouse` as SQL.
pub mod apply;
pub mod clickhouse;
pub mod eval;
pub mod placeholder;

use std::str::FromStr;

use datafusion::arrow::datatypes::{DataType, Field, FieldRef};
use datafusion::common::{plan_datafusion_err, plan_err};
use datafusion::error::Result;
use datafusion::logical_expr::ReturnFieldArgs;
use datafusion::prelude::SessionContext;
use datafusion::scalar::ScalarValue;

// TODO: Docs - explain how this registers the best-effort UDF that can be used when the full
// `ClickHouseQueryPlanner` is not available.
//
/// Registers `ClickHouse`-specific UDFs with the provided [`SessionContext`].
pub fn register_clickhouse_functions(ctx: &SessionContext) {
    ctx.register_udf(eval::clickhouse_eval_udf());
    ctx.register_udf(clickhouse::clickhouse_udf());
    ctx.register_udf(apply::clickhouse_apply_udf());
}

/// Helper function to extract return [`DataType`] from second UDF arg
fn extract_return_field_from_args(name: &str, args: &ReturnFieldArgs<'_>) -> Result<FieldRef> {
    if let Some(Some(
        ScalarValue::Utf8(Some(return_type_str))
        | ScalarValue::Utf8View(Some(return_type_str))
        | ScalarValue::LargeUtf8(Some(return_type_str)),
    )) = &args.scalar_arguments.last()
    {
        let dt = DataType::from_str(return_type_str.as_str())
            .map_err(|e| plan_datafusion_err!("Invalid return type for {name}: {e}"))?;
        Ok(udf_field_from_fields(name, dt, args.arg_fields))
    } else {
        plan_err!("Expected return type literal in scalar arguments for {name}")
    }
}

fn udf_field_from_fields(name: &str, dt: DataType, fields: &[FieldRef]) -> FieldRef {
    // Apply/lambda will be indicated by placeholder fields. These are flagged as nullable by
    // DataFusion during expr creation. But, the return field should only be nullable if any of the
    // columns the placeholders refer to are nullable.
    //
    // Apply functions have at least 3 args.
    let mut placeholder_nullable = false;
    if fields.len() >= 3 && fields.first().is_some_and(|f| f.name().starts_with('$')) {
        let rev_fields = fields.iter().rev();
        for (pl, col) in fields.iter().zip(rev_fields) {
            if pl.name().starts_with('$') {
                placeholder_nullable |= col.is_nullable();
            } else {
                break;
            }
        }
        return Field::new(name, dt, placeholder_nullable).into();
    }

    // Otherwise determine from the provided args

    // Treat array returns differently. ClickHouse doesn't support nullable arrays.
    let nullable = fields.iter().any(|a| {
        !matches!(
            a.data_type(),
            &DataType::List(_) | &DataType::ListView(_) | &DataType::LargeList(_)
        ) && a.is_nullable()
    });
    Field::new(name, dt, nullable).into()
}

pub mod functions {
    //! List of functions to create the various `ClickHouse`-specific UDFs.
    use datafusion::common::Column;
    use datafusion::logical_expr::expr::Placeholder;
    use datafusion::prelude::Expr;
    use datafusion::scalar::ScalarValue;

    /// Create a `ClickHouse` UDF to be 'evaluated' on the `ClickHouse` server.
    pub fn clickhouse_eval(expr: impl Into<String>, return_type: &str) -> Expr {
        super::eval::clickhouse_eval_udf().call(vec![
            Expr::Literal(ScalarValue::Utf8(Some(expr.into())), None),
            Expr::Literal(ScalarValue::Utf8(Some(return_type.to_string())), None),
        ])
    }

    /// Create a `ClickHouse` UDF that will be executed on the `ClickHouse` server.
    pub fn clickhouse(expr: Expr, return_type: &str) -> Expr {
        super::clickhouse::clickhouse_udf()
            .call(vec![expr, Expr::Literal(ScalarValue::Utf8(Some(return_type.to_string())), None)])
    }

    /// Create a `ClickHouse` Higher Order Function UDF that will be executed on the `ClickHouse`
    /// server.
    pub fn apply<C: IntoIterator<Item = Column>>(
        expr: Expr,
        columns: C,
        return_type: &str,
    ) -> Expr {
        let (mut args, columns): (Vec<_>, Vec<_>) = columns
            .into_iter()
            .enumerate()
            .map(|(i, c)| {
                (
                    Expr::Placeholder(Placeholder { id: format!("x{i}"), field: None }),
                    Expr::Column(c),
                )
            })
            .unzip();
        args.push(expr);
        args.extend(columns);
        let apply_udf = super::apply::clickhouse_apply_udf().call(args);
        clickhouse(apply_udf, return_type)
    }

    /// Alias for [`self::apply`]
    pub fn lambda<C: IntoIterator<Item = Column>>(
        expr: Expr,
        columns: C,
        return_type: &str,
    ) -> Expr {
        apply(expr, columns, return_type)
    }

    /// Alias for [`self::apply`]
    pub fn clickhouse_apply<C: IntoIterator<Item = Column>>(
        expr: Expr,
        columns: C,
        return_type: &str,
    ) -> Expr {
        apply(expr, columns, return_type)
    }

    /// Alias for [`self::apply`]
    pub fn clickhouse_lambda<C: IntoIterator<Item = Column>>(
        expr: Expr,
        columns: C,
        return_type: &str,
    ) -> Expr {
        apply(expr, columns, return_type)
    }

    /// Alias for [`self::apply`]
    pub fn clickhouse_map<C: IntoIterator<Item = Column>>(
        expr: Expr,
        columns: C,
        return_type: &str,
    ) -> Expr {
        apply(expr, columns, return_type)
    }

    #[cfg(test)]
    mod tests {
        use std::sync::Arc;

        use datafusion::common::ScalarValue;
        use datafusion::logical_expr::expr::ScalarFunction;
        use datafusion::prelude::{Expr, lit};

        use super::*;
        use crate::prelude::clickhouse_eval_udf;
        use crate::udfs::apply::clickhouse_apply_udf;
        use crate::udfs::clickhouse::clickhouse_udf;
        use crate::udfs::functions::clickhouse_eval;

        #[test]
        fn test_create_simple_udf() {
            assert_eq!(
                clickhouse_eval("count(*)", "UInt64"),
                Expr::ScalarFunction(ScalarFunction {
                    func: Arc::new(clickhouse_eval_udf()),
                    args: vec![
                        Expr::Literal(ScalarValue::Utf8(Some("count(*)".to_string())), None),
                        Expr::Literal(ScalarValue::Utf8(Some("UInt64".to_string())), None),
                    ],
                })
            );
        }

        #[test]
        fn test_clickhouse_udf() {
            assert_eq!(
                clickhouse(
                    Expr::Literal(ScalarValue::Utf8(Some("count(*)".to_string())), None),
                    "UInt64"
                ),
                Expr::ScalarFunction(ScalarFunction {
                    func: Arc::new(clickhouse_udf()),
                    args: vec![
                        Expr::Literal(ScalarValue::Utf8(Some("count(*)".to_string())), None),
                        Expr::Literal(ScalarValue::Utf8(Some("UInt64".to_string())), None),
                    ],
                })
            );
        }

        #[test]
        fn test_clickhouse_apply_udf() {
            let expr = Expr::Column(Column::from_name("id")) + lit(5);
            let columns = vec![Column::from_name("id")];
            let return_type = "UInt64";
            let apply_expr = apply(expr.clone(), columns.clone(), return_type);
            assert_eq!(
                apply_expr,
                Expr::ScalarFunction(ScalarFunction {
                    func: Arc::new(clickhouse_udf()),
                    args: vec![
                        // apply is a HOF
                        Expr::ScalarFunction(ScalarFunction {
                            func: Arc::new(clickhouse_apply_udf()),
                            args: vec![
                                Expr::Placeholder(Placeholder {
                                    id:    "x0".to_string(),
                                    field: None,
                                }),
                                Expr::Column(Column::from_name("id")) + lit(5),
                                Expr::Column(Column::from_name("id")),
                            ],
                        }),
                        Expr::Literal(ScalarValue::Utf8(Some("UInt64".to_string())), None),
                    ],
                })
            );

            let lambda_expr = lambda(expr.clone(), columns.clone(), return_type);
            let ch_apply_expr = clickhouse_apply(expr.clone(), columns.clone(), return_type);
            let ch_lambda_expr = clickhouse_lambda(expr.clone(), columns.clone(), return_type);
            let ch_map_expr = clickhouse_map(expr.clone(), columns.clone(), return_type);
            assert_eq!(apply_expr, lambda_expr);
            assert_eq!(apply_expr, ch_apply_expr);
            assert_eq!(apply_expr, ch_lambda_expr);
            assert_eq!(apply_expr, ch_map_expr);
        }
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use datafusion::arrow::datatypes::{DataType, Field};
    use datafusion::common::ScalarValue;
    use datafusion::logical_expr::ReturnFieldArgs;
    use datafusion::prelude::SessionContext;

    use super::*;

    #[test]
    fn test_register_clickhouse_functions() {
        let ctx = SessionContext::new();
        register_clickhouse_functions(&ctx);

        // Check that the clickhouse function was registered
        let state = ctx.state();
        let functions = state.scalar_functions();
        assert!(functions.contains_key("clickhouse_eval"));
        assert!(functions.contains_key("clickhouse"));
        assert!(functions.contains_key("apply"));
    }

    #[test]
    fn test_extract_return_field_from_args_utf8() {
        let field1 = Arc::new(Field::new("syntax", DataType::Utf8, false));
        let field2 = Arc::new(Field::new("type", DataType::Utf8, false));
        let scalar = [
            Some(ScalarValue::Utf8(Some("count()".to_string()))),
            Some(ScalarValue::Utf8(Some("Int64".to_string()))),
        ];
        let args = ReturnFieldArgs {
            arg_fields:       &[field1, field2],
            scalar_arguments: &[scalar[0].as_ref(), scalar[1].as_ref()],
        };
        let result = extract_return_field_from_args("test_func", &args);
        assert!(result.is_ok());
        let field = result.unwrap();
        assert_eq!(field.name(), "test_func");
        assert_eq!(field.data_type(), &DataType::Int64);
        assert!(!field.is_nullable());
    }

    #[test]
    fn test_extract_return_field_from_args_utf8_view() {
        let field1 = Arc::new(Field::new("syntax", DataType::Utf8View, false));
        let field2 = Arc::new(Field::new("type", DataType::Utf8View, false));
        let scalar = [
            Some(ScalarValue::Utf8View(Some("sum(x)".to_string()))),
            Some(ScalarValue::Utf8View(Some("Float64".to_string()))),
        ];
        let args = ReturnFieldArgs {
            arg_fields:       &[field1, field2],
            scalar_arguments: &[scalar[0].as_ref(), scalar[1].as_ref()],
        };

        let result = extract_return_field_from_args("test_func", &args);
        assert!(result.is_ok());
        let field = result.unwrap();
        assert_eq!(field.data_type(), &DataType::Float64);
    }

    #[test]
    fn test_extract_return_field_from_args_large_utf8() {
        let field1 = Arc::new(Field::new("syntax", DataType::LargeUtf8, false));
        let field2 = Arc::new(Field::new("type", DataType::LargeUtf8, false));
        let scalar = [
            Some(ScalarValue::LargeUtf8(Some("avg(y)".to_string()))),
            Some(ScalarValue::LargeUtf8(Some("Boolean".to_string()))),
        ];
        let args = ReturnFieldArgs {
            arg_fields:       &[field1, field2],
            scalar_arguments: &[scalar[0].as_ref(), scalar[1].as_ref()],
        };

        let result = extract_return_field_from_args("test_func", &args);
        assert!(result.is_ok());
        let field = result.unwrap();
        assert_eq!(field.data_type(), &DataType::Boolean);
    }

    #[test]
    fn test_extract_return_field_from_args_invalid_type() {
        let field1 = Arc::new(Field::new("syntax", DataType::Utf8, false));
        let field2 = Arc::new(Field::new("type", DataType::Utf8, false));
        let scalar = [
            Some(ScalarValue::Utf8(Some("count()".to_string()))),
            Some(ScalarValue::Utf8(Some("InvalidDataType".to_string()))),
        ];
        let args = ReturnFieldArgs {
            arg_fields:       &[field1, field2],
            scalar_arguments: &[scalar[0].as_ref(), scalar[1].as_ref()],
        };

        let result = extract_return_field_from_args("test_func", &args);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Invalid return type"));
    }

    #[test]
    fn test_extract_return_field_from_args_no_last_arg() {
        let field1 = Arc::new(Field::new("syntax", DataType::Utf8, false));
        let args = ReturnFieldArgs { arg_fields: &[field1], scalar_arguments: &[] };

        let result = extract_return_field_from_args("test_func", &args);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Expected return type"));
    }

    #[test]
    fn test_extract_return_field_from_args_null_last_arg() {
        let field1 = Arc::new(Field::new("syntax", DataType::Utf8, false));
        let field2 = Arc::new(Field::new("type", DataType::Utf8, false));
        let scalar = [Some(ScalarValue::Utf8(Some("count()".to_string()))), None];
        let args = ReturnFieldArgs {
            arg_fields:       &[field1, field2],
            scalar_arguments: &[scalar[0].as_ref(), scalar[1].as_ref()],
        };

        let result = extract_return_field_from_args("test_func", &args);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Expected return type"));
    }

    #[test]
    fn test_extract_return_field_from_args_non_string_last_arg() {
        let field1 = Arc::new(Field::new("syntax", DataType::Utf8, false));
        let field2 = Arc::new(Field::new("type", DataType::Int32, false));
        let scalar = [
            Some(ScalarValue::Utf8(Some("count()".to_string()))),
            Some(ScalarValue::Int32(Some(42))),
        ];
        let args = ReturnFieldArgs {
            arg_fields:       &[field1, field2],
            scalar_arguments: &[scalar[0].as_ref(), scalar[1].as_ref()],
        };

        let result = extract_return_field_from_args("test_func", &args);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Expected return type"));
    }

    #[test]
    fn test_extract_return_field_from_args_empty_string_last_arg() {
        let field1 = Arc::new(Field::new("syntax", DataType::Utf8, false));
        let field2 = Arc::new(Field::new("type", DataType::Utf8, false));
        let scalar = [
            Some(ScalarValue::Utf8(Some("count()".to_string()))),
            Some(ScalarValue::Utf8(Some(String::new()))),
        ];
        let args = ReturnFieldArgs {
            arg_fields:       &[field1, field2],
            scalar_arguments: &[scalar[0].as_ref(), scalar[1].as_ref()],
        };

        let result = extract_return_field_from_args("test_func", &args);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Invalid return type"));
    }

    #[test]
    fn test_extract_return_field_from_args_null_string_last_arg() {
        let field1 = Arc::new(Field::new("syntax", DataType::Utf8, false));
        let field2 = Arc::new(Field::new("type", DataType::Utf8, false));
        let scalar =
            [Some(ScalarValue::Utf8(Some("count()".to_string()))), Some(ScalarValue::Utf8(None))];
        let args = ReturnFieldArgs {
            arg_fields:       &[field1, field2],
            scalar_arguments: &[scalar[0].as_ref(), scalar[1].as_ref()],
        };

        let result = extract_return_field_from_args("test_func", &args);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Expected return type"));
    }
}