Skip to main content

vortex_array/arrays/variant/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4mod vtable;
5
6use vortex_error::VortexExpect;
7
8pub use self::vtable::Variant;
9pub use self::vtable::VariantArray;
10use crate::ArrayRef;
11use crate::array::Array;
12use crate::array::ArrayParts;
13use crate::array::EmptyArrayData;
14use crate::array::TypedArrayRef;
15use crate::dtype::DType;
16
17pub(super) const NUM_SLOTS: usize = 1;
18pub(super) const SLOT_NAMES: [&str; NUM_SLOTS] = ["child"];
19
20pub trait VariantArrayExt: TypedArrayRef<Variant> {
21    fn child(&self) -> &ArrayRef {
22        self.as_ref().slots()[0]
23            .as_ref()
24            .vortex_expect("validated variant child slot")
25    }
26}
27impl<T: TypedArrayRef<Variant>> VariantArrayExt for T {}
28
29impl Array<Variant> {
30    /// Creates a new `VariantArray`.
31    pub fn new(child: ArrayRef) -> Self {
32        let dtype = DType::Variant(child.dtype().nullability());
33        let len = child.len();
34        let stats = child.statistics().to_owned();
35        unsafe {
36            Array::from_parts_unchecked(
37                ArrayParts::new(Variant, dtype, len, EmptyArrayData).with_slots(vec![Some(child)]),
38            )
39        }
40        .with_stats_set(stats)
41    }
42}