pub fn union_extract_by_type(
union_array: &UnionArray,
target_type: &DataType,
cast_options: &CastOptions<'_>,
) -> Result<ArrayRef, ArrowError>Expand description
Extracts the best-matching child array from a [UnionArray] for a given target type,
and casts it to that type.
Rows where a different child array is active become NULL. If no child array matches, returns an error.
ยงExample
let fields = UnionFields::try_new(
[0, 1],
[
Field::new("int", DataType::Int32, true),
Field::new("str", DataType::Utf8, true),
],
).unwrap();
let union = UnionArray::try_new(
fields,
vec![0, 1, 0].into(),
None,
vec![
Arc::new(Int32Array::from(vec![Some(42), None, Some(99)])),
Arc::new(StringArray::from(vec![None, Some("hello"), None])),
],
)
.unwrap();
// extract the Utf8 child array and cast to Utf8View
let result = union_extract_by_type(&union, &DataType::Utf8View, &CastOptions::default()).unwrap();
assert_eq!(result.data_type(), &DataType::Utf8View);
assert!(result.is_null(0)); // Int32 row -> NULL
assert!(!result.is_null(1)); // Utf8 row -> "hello"
assert!(result.is_null(2)); // Int32 row -> NULL