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.
§Errors
Returns [ArrowError::InvalidArgumentError] if no child type can be selected for
the target type, or conversion from the selected child type is unsupported.
[ArrowError::CastError] indicates that the conversion is supported, but a
value in the selected child array cannot be converted, for example because
of an invalid string format or numeric overflow. When CastOptions::safe
is true, failed values are replaced with nulls where supported.
§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