MCPcopy Create free account
hub / github.com/apache/datafusion / create_example_udf

Function create_example_udf

datafusion/sqllogictest/src/test_context.rs:516–546  ·  view source on GitHub ↗

Create a UDF function named "example". See the `sample_udf.rs` example file for an explanation of the API.

()

Source from the content-addressed store, hash-verified

514/// Create a UDF function named "example". See the `sample_udf.rs` example
515/// file for an explanation of the API.
516fn create_example_udf() -> ScalarUDF {
517 let adder = Arc::new(|args: &[ColumnarValue]| {
518 let ColumnarValue::Array(lhs) = &args[0] else {
519 panic!("should be array")
520 };
521 let ColumnarValue::Array(rhs) = &args[1] else {
522 panic!("should be array")
523 };
524
525 let lhs = as_float64_array(lhs).expect("cast failed");
526 let rhs = as_float64_array(rhs).expect("cast failed");
527 let array = lhs
528 .iter()
529 .zip(rhs.iter())
530 .map(|(lhs, rhs)| match (lhs, rhs) {
531 (Some(lhs), Some(rhs)) => Some(lhs + rhs),
532 _ => None,
533 })
534 .collect::<Float64Array>();
535 Ok(ColumnarValue::from(Arc::new(array) as ArrayRef))
536 });
537 create_udf(
538 "example",
539 // Expects two f64 values:
540 vec![DataType::Float64, DataType::Float64],
541 // Returns an f64 value:
542 DataType::Float64,
543 Volatility::Immutable,
544 adder,
545 )
546}
547
548fn register_union_table(ctx: &SessionContext) {
549 let union = UnionArray::try_new(

Callers 1

try_new_for_test_fileMethod · 0.85

Calls 5

newFunction · 0.85
as_float64_arrayFunction · 0.85
create_udfFunction · 0.85
mapMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…