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

Function try_array_positions_scalar

datafusion/functions-nested/src/position.rs:433–474  ·  view source on GitHub ↗

Attempts the scalar-needle fast path for `array_positions`.

(args: &[ColumnarValue])

Source from the content-addressed store, hash-verified

431
432/// Attempts the scalar-needle fast path for `array_positions`.
433fn try_array_positions_scalar(args: &[ColumnarValue]) -> Result<Option<ColumnarValue>> {
434 let [haystack_arg, needle_arg] = take_function_args("array_positions", args)?;
435
436 let scalar_needle = match needle_arg {
437 ColumnarValue::Scalar(s) => s,
438 ColumnarValue::Array(_) => return Ok(None),
439 };
440
441 // `not_distinct` doesn't support nested types (List, Struct, etc.),
442 // so fall back to the per-row path for those.
443 if scalar_needle.data_type().is_nested() {
444 return Ok(None);
445 }
446
447 let (num_rows, all_inputs_scalar) = match haystack_arg {
448 ColumnarValue::Array(a) => (a.len(), false),
449 ColumnarValue::Scalar(_) => (1, true),
450 };
451
452 let needle = scalar_needle.to_array_of_size(1)?;
453 let haystack = haystack_arg.to_array(num_rows)?;
454
455 let result = match haystack.data_type() {
456 List(_) => {
457 let list = as_list_array(&haystack)?;
458 array_positions_scalar::<i32>(list, &needle)
459 }
460 LargeList(_) => {
461 let list = as_large_list_array(&haystack)?;
462 array_positions_scalar::<i64>(list, &needle)
463 }
464 t => exec_err!("array_positions does not support type '{t}'"),
465 }?;
466
467 if all_inputs_scalar {
468 Ok(Some(ColumnarValue::Scalar(ScalarValue::try_from_array(
469 &result, 0,
470 )?)))
471 } else {
472 Ok(Some(ColumnarValue::Array(result)))
473 }
474}
475
476fn array_positions_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
477 let [haystack, needle] = take_function_args("array_positions", args)?;

Callers 1

invoke_with_argsMethod · 0.85

Calls 7

take_function_argsFunction · 0.85
as_list_arrayFunction · 0.85
as_large_list_arrayFunction · 0.85
data_typeMethod · 0.45
lenMethod · 0.45
to_array_of_sizeMethod · 0.45
to_arrayMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…