Converts [`ColumnarValue`]s to [`ArrayRef`]s with the same length. # Performance Note This function expands any [`ScalarValue`] to an array. This expansion permits using a single function in terms of arrays, but it can be inefficient compared to handling the scalar value directly. Thus, it is recommended to provide specialized implementations for scalar values if performance is a concern. # E
(args: &[ColumnarValue])
| 241 | /// |
| 242 | /// If there are multiple array arguments that have different lengths |
| 243 | pub fn values_to_arrays(args: &[ColumnarValue]) -> Result<Vec<ArrayRef>> { |
| 244 | if args.is_empty() { |
| 245 | return Ok(vec![]); |
| 246 | } |
| 247 | |
| 248 | let mut array_len = None; |
| 249 | for arg in args { |
| 250 | array_len = match (arg, array_len) { |
| 251 | (ColumnarValue::Array(a), None) => Some(a.len()), |
| 252 | (ColumnarValue::Array(a), Some(array_len)) => { |
| 253 | if array_len == a.len() { |
| 254 | Some(array_len) |
| 255 | } else { |
| 256 | return internal_err!( |
| 257 | "Arguments has mixed length. Expected length: {array_len}, found length: {}", |
| 258 | a.len() |
| 259 | ); |
| 260 | } |
| 261 | } |
| 262 | (ColumnarValue::Scalar(_), array_len) => array_len, |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // If array_len is none, it means there are only scalars, so make a 1 element array |
| 267 | let inferred_length = array_len.unwrap_or(1); |
| 268 | |
| 269 | let args = args |
| 270 | .iter() |
| 271 | .map(|arg| arg.to_array(inferred_length)) |
| 272 | .collect::<Result<Vec<_>>>()?; |
| 273 | |
| 274 | Ok(args) |
| 275 | } |
| 276 | |
| 277 | /// Cast this [ColumnarValue] to the specified `DataType` |
| 278 | /// |