You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| Scalar | A function that takes a row of data and returns a single value. |[simple_udf.rs][1]|
29
+
| Window | A function that takes a row of data and returns a single value, but also has access to the rows around it. |[simple_udwf.rs][2]|
30
+
| Aggregate | A function that takes a group of rows and returns a single value. |[simple_udaf.rs][3]|
31
+
| Table | A function that takes parameters and returns a `TableProvider` to be used in an query plan. |[simple_udtf.rs][4]|
32
+
| Async Scalar | A scalar function that natively supports asynchronous execution, allowing you to perform async operations (such as network or I/O calls) within the UDF. |[async_udf.rs][5]|
32
33
33
34
First we'll talk about adding an Scalar UDF end-to-end, then we'll talk about the differences between the different
34
35
types of UDFs.
@@ -344,6 +345,122 @@ async fn main() {
344
345
}
345
346
```
346
347
348
+
## Adding a Scalar Async UDF
349
+
350
+
A Scalar Async UDF allows you to implement user-defined functions that support asynchronous execution, such as performing network or I/O operations within the UDF.
351
+
352
+
To add a Scalar Async UDF, you need to:
353
+
354
+
1. Implement the `AsyncScalarUDFImpl` trait to define your async function logic, signature, and types.
355
+
2. Wrap your implementation with `AsyncScalarUDF::new` and register it with the `SessionContext`.
trace!("Invoking async_upper with args: {:?}", args);
430
+
letvalue=&args.args[0];
431
+
letresult=matchvalue {
432
+
ColumnarValue::Array(array) => {
433
+
letstring_array=array.as_string::<i32>();
434
+
letiter=ArrayIter::new(string_array);
435
+
letresult=iter
436
+
.map(|string|string.map(|s|s.to_uppercase()))
437
+
.collect::<StringArray>();
438
+
Arc::new(result) asArrayRef
439
+
}
440
+
_=>returninternal_err!("Expected a string argument, got {:?}", value),
441
+
};
442
+
Ok(result)
443
+
}
444
+
}
445
+
```
446
+
447
+
We can now transfer the async UDF into the normal scalar using `into_scalar_udf` to register the function with DataFusion so that it can be used in the context of a query.
0 commit comments