vortex_array/arrays/dict/vtable/
plugin.rs1use prost::Message;
7use smallvec::smallvec;
8use vortex_error::VortexResult;
9use vortex_error::vortex_bail;
10use vortex_error::vortex_ensure;
11use vortex_error::vortex_ensure_eq;
12use vortex_error::vortex_err;
13use vortex_session::VortexSession;
14
15use crate::Array;
16use crate::ArrayDeserialization;
17use crate::ArrayId;
18use crate::ArrayParts;
19use crate::ArrayPlugin;
20use crate::ArrayRef;
21use crate::ArraySerialization;
22use crate::IntoArray;
23use crate::VTable;
24use crate::arrays::Dict;
25use crate::arrays::dict::DictArrayExt;
26use crate::arrays::dict::DictArraySlotsExt;
27use crate::arrays::dict::DictData;
28use crate::arrays::dict::DictMetadata;
29use crate::dtype::DType;
30use crate::dtype::Nullability;
31use crate::dtype::PType;
32
33impl ArrayPlugin for Dict {
34 fn id(&self) -> ArrayId {
35 VTable::id(self)
36 }
37
38 fn serialize(
39 &self,
40 array: &ArrayRef,
41 _session: &VortexSession,
42 ) -> VortexResult<Option<ArraySerialization>> {
43 vortex_ensure_eq!(
44 array.encoding_id(),
45 ArrayPlugin::id(self),
46 "Dict plugin cannot serialize an array of another encoding",
47 );
48 let array = array.as_::<Dict>();
49 let metadata = DictMetadata {
50 codes_ptype: PType::try_from(array.codes().dtype())? as i32,
51 values_len: u32::try_from(array.values().len()).map_err(|_| {
52 vortex_err!(
53 "Dictionary values size {} overflowed u32",
54 array.values().len()
55 )
56 })?,
57 is_nullable_codes: Some(array.codes().dtype().is_nullable()),
58 all_values_referenced: Some(array.has_all_values_referenced()),
59 }
60 .encode_to_vec();
61 Ok(Some(ArraySerialization::from_array(
62 ArrayPlugin::id(self),
63 array.array(),
64 metadata,
65 )))
66 }
67
68 fn deserialize(
69 &self,
70 parts: ArrayDeserialization<'_>,
71 _session: &VortexSession,
72 ) -> VortexResult<ArrayRef> {
73 vortex_ensure_eq!(
74 parts.serialized_id,
75 ArrayPlugin::id(self),
76 "Dict plugin does not recognize the serialized ID",
77 );
78 let ArrayDeserialization {
79 dtype,
80 len,
81 metadata,
82 children,
83 ..
84 } = parts;
85 let metadata = DictMetadata::decode(metadata)?;
86 let codes_ptype = metadata.get_codes_ptype()?;
87 vortex_ensure!(
88 codes_ptype.is_int(),
89 "DictArray codes must be integers, got {codes_ptype}"
90 );
91
92 if children.len() != 2 {
93 vortex_bail!(
94 "Expected 2 children for dict encoding, found {}",
95 children.len()
96 )
97 }
98 let codes_nullable = metadata
99 .is_nullable_codes
100 .map(Nullability::from)
101 .unwrap_or_else(|| dtype.nullability());
104 let codes_dtype = DType::Primitive(codes_ptype, codes_nullable);
105 let codes = children.get(0, &codes_dtype, len)?;
106 let values = children.get(1, dtype, metadata.values_len as usize)?;
107 let all_values_referenced = metadata.all_values_referenced.unwrap_or(false);
108
109 Ok(Array::try_from_parts(ArrayParts::new(
110 self.clone(),
111 dtype.clone(),
112 len,
113 unsafe { DictData::new_unchecked().set_all_values_referenced(all_values_referenced) },
117 smallvec![Some(codes), Some(values)],
118 ))?
119 .into_array())
120 }
121}