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

Function make_scalar_function

datafusion/functions-nested/src/utils.rs:54–84  ·  view source on GitHub ↗

array function wrapper that differentiates between scalar (length 1) and array.

(
    inner: F,
)

Source from the content-addressed store, hash-verified

52
53/// array function wrapper that differentiates between scalar (length 1) and array.
54pub(crate) fn make_scalar_function<F>(
55 inner: F,
56) -> impl Fn(&[ColumnarValue]) -> Result<ColumnarValue>
57where
58 F: Fn(&[ArrayRef]) -> Result<ArrayRef>,
59{
60 move |args: &[ColumnarValue]| {
61 // first, identify if any of the arguments is an Array. If yes, store its `len`,
62 // as any scalar will need to be converted to an array of len `len`.
63 let len = args
64 .iter()
65 .fold(Option::<usize>::None, |acc, arg| match arg {
66 ColumnarValue::Scalar(_) => acc,
67 ColumnarValue::Array(a) => Some(a.len()),
68 });
69
70 let is_scalar = len.is_none();
71
72 let args = ColumnarValue::values_to_arrays(args)?;
73
74 let result = (inner)(&args);
75
76 if is_scalar {
77 // If all inputs are scalar, keeps output as scalar
78 let result = result.and_then(|arr| ScalarValue::try_from_array(&arr, 0));
79 result.map(ColumnarValue::Scalar)
80 } else {
81 result.map(ColumnarValue::Array)
82 }
83 }
84}
85
86pub(crate) fn align_array_dimensions<O: OffsetSizeTrait>(
87 args: Vec<ArrayRef>,

Callers 15

invoke_with_argsMethod · 0.70
invoke_with_argsMethod · 0.70
invoke_with_argsMethod · 0.70
invoke_with_argsMethod · 0.70
invoke_with_argsMethod · 0.70
invoke_with_argsMethod · 0.70
invoke_with_argsMethod · 0.70
invoke_with_argsMethod · 0.70
invoke_with_argsMethod · 0.70
invoke_with_argsMethod · 0.70
invoke_with_argsMethod · 0.70
invoke_with_argsMethod · 0.70

Calls 5

values_to_arraysFunction · 0.85
is_noneMethod · 0.80
iterMethod · 0.45
lenMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…