Create a UDF function named "example". See the `sample_udf.rs` example file for an explanation of the API.
()
| 514 | /// Create a UDF function named "example". See the `sample_udf.rs` example |
| 515 | /// file for an explanation of the API. |
| 516 | fn 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 | |
| 548 | fn register_union_table(ctx: &SessionContext) { |
| 549 | let union = UnionArray::try_new( |
no test coverage detected
searching dependent graphs…