Creates a scalar function implementation for the given function. `inner` - the function to be executed `hints` - hints to be used when expanding scalars to arrays
(
inner: F,
hints: Vec<Hint>,
)
| 78 | /// * `inner` - the function to be executed |
| 79 | /// * `hints` - hints to be used when expanding scalars to arrays |
| 80 | pub fn make_scalar_function<F>( |
| 81 | inner: F, |
| 82 | hints: Vec<Hint>, |
| 83 | ) -> impl Fn(&[ColumnarValue]) -> Result<ColumnarValue> |
| 84 | where |
| 85 | F: Fn(&[ArrayRef]) -> Result<ArrayRef>, |
| 86 | { |
| 87 | move |args: &[ColumnarValue]| { |
| 88 | // first, identify if any of the arguments is an Array. If yes, store its `len`, |
| 89 | // as any scalar will need to be converted to an array of len `len`. |
| 90 | let len = args |
| 91 | .iter() |
| 92 | .fold(Option::<usize>::None, |acc, arg| match arg { |
| 93 | ColumnarValue::Scalar(_) => acc, |
| 94 | ColumnarValue::Array(a) => Some(a.len()), |
| 95 | }); |
| 96 | |
| 97 | let is_scalar = len.is_none(); |
| 98 | |
| 99 | let inferred_length = len.unwrap_or(1); |
| 100 | let args = args |
| 101 | .iter() |
| 102 | .zip(hints.iter().chain(std::iter::repeat(&Hint::Pad))) |
| 103 | .map(|(arg, hint)| { |
| 104 | // Decide on the length to expand this scalar to depending |
| 105 | // on the given hints. |
| 106 | let expansion_len = match hint { |
| 107 | Hint::AcceptsSingular => 1, |
| 108 | Hint::Pad => inferred_length, |
| 109 | }; |
| 110 | arg.to_array(expansion_len) |
| 111 | }) |
| 112 | .collect::<Result<Vec<_>>>()?; |
| 113 | |
| 114 | let result = (inner)(&args); |
| 115 | if is_scalar { |
| 116 | // If all inputs are scalar, keeps output as scalar |
| 117 | let result = result.and_then(|arr| ScalarValue::try_from_array(&arr, 0)); |
| 118 | result.map(ColumnarValue::Scalar) |
| 119 | } else { |
| 120 | result.map(ColumnarValue::Array) |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /// Computes a binary math function for input arrays using a specified function. |
| 126 | /// Generic types: |
no test coverage detected
searching dependent graphs…