arrow_select::union_extract

Function union_extract

Source
pub fn union_extract(
    union_array: &UnionArray,
    target: &str,
) -> Result<ArrayRef, ArrowError>
Expand description

Returns the value of the target field when selected, or NULL otherwise.

┌─────────────────┐                                   ┌─────────────────┐
│       A=1       │                                   │        1        │
├─────────────────┤                                   ├─────────────────┤
│      A=NULL     │                                   │       NULL      │
├─────────────────┤    union_extract(values, 'A')     ├─────────────────┤
│      B='t'      │  ────────────────────────────▶    │       NULL      │
├─────────────────┤                                   ├─────────────────┤
│       A=3       │                                   │        3        │
├─────────────────┤                                   ├─────────────────┤
│      B=NULL     │                                   │       NULL      │
└─────────────────┘                                   └─────────────────┘
   union array                                              result

§Errors

Returns error if target field is not found

§Examples

let fields = UnionFields::new(
    [1, 3],
    [
        Field::new("A", DataType::Int32, true),
        Field::new("B", DataType::Utf8, true)
    ]
);

let union = UnionArray::try_new(
    fields,
    vec![1, 1, 3, 1, 3].into(),
    None,
    vec![
        Arc::new(Int32Array::from(vec![Some(1), None, None, Some(3), Some(0)])),
        Arc::new(StringArray::from(vec![None, None, Some("t"), Some("."), None]))
    ]
).unwrap();

// Extract field A
let extracted = union_extract(&union, "A").unwrap();

assert_eq!(*extracted, Int32Array::from(vec![Some(1), None, None, Some(3), None]));