Skip to content

Commit 98cf8e2

Browse files
committed
fix doc test
1 parent 68debd5 commit 98cf8e2

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

docs/source/library-user-guide/functions/adding-udfs.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,97 @@ impl AsyncScalarUDFImpl for AsyncUpper {
447447
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.
448448

449449
```rust
450+
# use arrow::array::{ArrayIter, ArrayRef, AsArray, StringArray};
451+
# use arrow_schema::DataType;
452+
# use async_trait::async_trait;
453+
# use datafusion::common::error::Result;
454+
# use datafusion::common::internal_err;
455+
# use datafusion::common::types::logical_string;
456+
# use datafusion::config::ConfigOptions;
457+
# use datafusion::logical_expr::async_udf::{
458+
# AsyncScalarFunctionArgs, AsyncScalarUDFImpl,
459+
# };
460+
# use datafusion::logical_expr::{
461+
# ColumnarValue, Signature, TypeSignature, TypeSignatureClass, Volatility,
462+
# };
463+
# use datafusion::logical_expr_common::signature::Coercion;
464+
# use log::trace;
465+
# use std::any::Any;
466+
# use std::sync::Arc;
467+
#
468+
# #[derive(Debug)]
469+
# pub struct AsyncUpper {
470+
# signature: Signature,
471+
# }
472+
#
473+
# impl Default for AsyncUpper {
474+
# fn default() -> Self {
475+
# Self::new()
476+
# }
477+
# }
478+
#
479+
# impl AsyncUpper {
480+
# pub fn new() -> Self {
481+
# Self {
482+
# signature: Signature::new(
483+
# TypeSignature::Coercible(vec![Coercion::Exact {
484+
# desired_type: TypeSignatureClass::Native(logical_string()),
485+
# }]),
486+
# Volatility::Volatile,
487+
# ),
488+
# }
489+
# }
490+
# }
491+
#
492+
# #[async_trait]
493+
# impl AsyncScalarUDFImpl for AsyncUpper {
494+
# fn as_any(&self) -> &dyn Any {
495+
# self
496+
# }
497+
#
498+
# fn name(&self) -> &str {
499+
# "async_upper"
500+
# }
501+
#
502+
# fn signature(&self) -> &Signature {
503+
# &self.signature
504+
# }
505+
#
506+
# fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
507+
# Ok(DataType::Utf8)
508+
# }
509+
#
510+
# fn ideal_batch_size(&self) -> Option<usize> {
511+
# Some(10)
512+
# }
513+
#
514+
# async fn invoke_async_with_args(
515+
# &self,
516+
# args: AsyncScalarFunctionArgs,
517+
# _option: &ConfigOptions,
518+
# ) -> Result<ArrayRef> {
519+
# trace!("Invoking async_upper with args: {:?}", args);
520+
# let value = &args.args[0];
521+
# let result = match value {
522+
# ColumnarValue::Array(array) => {
523+
# let string_array = array.as_string::<i32>();
524+
# let iter = ArrayIter::new(string_array);
525+
# let result = iter
526+
# .map(|string| string.map(|s| s.to_uppercase()))
527+
# .collect::<StringArray>();
528+
# Arc::new(result) as ArrayRef
529+
# }
530+
# _ => return internal_err!("Expected a string argument, got {:?}", value),
531+
# };
532+
# Ok(result)
533+
# }
534+
# }
535+
use datafusion::execution::context::SessionContext;
536+
use datafusion::logical_expr::async_udf::AsyncScalarUDF;
537+
450538
let async_upper = AsyncUpper::new();
451539
let udf = AsyncScalarUDF::new(Arc::new(async_upper));
540+
let mut ctx = SessionContext::new();
452541
ctx.register_udf(udf.into_scalar_udf());
453542
```
454543

0 commit comments

Comments
 (0)