Skip to main content

vortex_array/arrays/masked/vtable/
plugin.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! [`ArrayPlugin`] implementation for [`Masked`].
5
6use smallvec::smallvec;
7use vortex_error::VortexResult;
8use vortex_error::vortex_bail;
9use vortex_error::vortex_ensure;
10use vortex_error::vortex_ensure_eq;
11use vortex_session::VortexSession;
12
13use crate::Array;
14use crate::ArrayDeserialization;
15use crate::ArrayId;
16use crate::ArrayParts;
17use crate::ArrayPlugin;
18use crate::ArrayRef;
19use crate::ArraySerialization;
20use crate::IntoArray;
21use crate::VTable;
22use crate::VortexSessionExecute;
23use crate::array::validity_to_child;
24use crate::arrays::Masked;
25use crate::arrays::masked::MaskedData;
26use crate::validity::Validity;
27
28impl ArrayPlugin for Masked {
29    fn id(&self) -> ArrayId {
30        VTable::id(self)
31    }
32
33    fn serialize(
34        &self,
35        array: &ArrayRef,
36        _session: &VortexSession,
37    ) -> VortexResult<Option<ArraySerialization>> {
38        vortex_ensure_eq!(
39            array.encoding_id(),
40            ArrayPlugin::id(self),
41            "Masked plugin cannot serialize an array of another encoding",
42        );
43        Ok(Some(ArraySerialization::from_array(
44            ArrayPlugin::id(self),
45            array,
46            vec![],
47        )))
48    }
49
50    fn deserialize(
51        &self,
52        parts: ArrayDeserialization<'_>,
53        session: &VortexSession,
54    ) -> VortexResult<ArrayRef> {
55        vortex_ensure_eq!(
56            parts.serialized_id,
57            ArrayPlugin::id(self),
58            "Masked plugin does not recognize the serialized ID",
59        );
60        let ArrayDeserialization {
61            dtype,
62            len,
63            metadata,
64            buffers,
65            children,
66            ..
67        } = parts;
68        if !metadata.is_empty() {
69            vortex_bail!(
70                "MaskedArray expects empty metadata, got {} bytes",
71                metadata.len()
72            );
73        }
74        if !buffers.is_empty() {
75            vortex_bail!("Expected 0 buffer, got {}", buffers.len());
76        }
77
78        vortex_ensure!(
79            children.len() == 1 || children.len() == 2,
80            "`MaskedArray::build` expects 1 or 2 children, got {}",
81            children.len()
82        );
83
84        let child = children.get(0, &dtype.as_nonnullable(), len)?;
85
86        let validity = if children.len() == 2 {
87            let validity = children.get(1, &Validity::DTYPE, len)?;
88            Validity::Array(validity)
89        } else {
90            Validity::from(dtype.nullability())
91        };
92
93        let validity_child = validity_to_child(&validity, len);
94        let data = MaskedData::try_new(
95            len,
96            child.all_valid(&mut session.create_execution_ctx())?,
97            validity,
98        )?;
99        Ok(Array::try_from_parts(ArrayParts::new(
100            self.clone(),
101            dtype.clone(),
102            len,
103            data,
104            smallvec![Some(child), validity_child],
105        ))?
106        .into_array())
107    }
108}