vortex_functions/zip/
map.rs1use vortex_array::ArrayRef;
5use vortex_array::ArrayView;
6use vortex_array::ExecutionCtx;
7use vortex_array::IntoArray;
8use vortex_array::arrays::ListView;
9use vortex_array::arrays::MapArray;
10use vortex_array::arrays::map::Map;
11use vortex_array::arrays::map::MapArrayExt;
12use vortex_array::arrays::map::MapArraySlotsExt;
13use vortex_error::VortexResult;
14use vortex_error::vortex_err;
15
16use crate::zip::ZipKernel;
17
18impl ZipKernel for Map {
20 fn zip(
21 if_true: ArrayView<'_, Map>,
22 if_false: &ArrayRef,
23 mask: &ArrayRef,
24 ctx: &mut ExecutionCtx,
25 ) -> VortexResult<Option<ArrayRef>> {
26 let Some(if_false) = if_false.as_opt::<Map>() else {
27 return Ok(None);
28 };
29
30 if if_true.map_dtype() != if_false.map_dtype() {
33 return Ok(None);
34 }
35
36 let Some(entries) = <ListView as ZipKernel>::zip(
37 if_true.entries().as_::<ListView>(),
38 if_false.entries(),
39 mask,
40 ctx,
41 )?
42 else {
43 return Ok(None);
44 };
45
46 let map_entries = entries.try_downcast::<ListView>().map_err(|arr| {
47 vortex_err!(
48 "Map entries operation expected vortex.listview/ListView, got {}",
49 arr.encoding_id()
50 )
51 })?;
52 MapArray::try_new(if_true.map_dtype().clone(), map_entries)
53 .map(IntoArray::into_array)
54 .map(Some)
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use std::sync::LazyLock;
61
62 use rstest::rstest;
63 use vortex_array::ArrayRef;
64 use vortex_array::IntoArray;
65 use vortex_array::VortexSessionExecute;
66 use vortex_array::arrays::Map;
67 use vortex_array::assert_arrays_eq;
68 use vortex_array::builders::builder_with_capacity_in;
69 use vortex_array::dtype::DType;
70 use vortex_array::dtype::MapDType;
71 use vortex_array::dtype::Nullability;
72 use vortex_array::dtype::PType;
73 use vortex_array::memory::BufferAllocatorRef;
74 use vortex_array::scalar::Scalar;
75 use vortex_error::VortexResult;
76 use vortex_mask::Mask;
77 use vortex_session::VortexSession;
78
79 use crate::builtins::ArrayFunctions;
80
81 static SESSION: LazyLock<VortexSession> = LazyLock::new(|| {
82 let session = VortexSession::empty();
83 vortex_array::initialize(&session);
84 crate::initialize(&session);
85 session
86 });
87
88 fn map_dtype() -> VortexResult<MapDType> {
89 MapDType::try_new(
90 DType::Primitive(PType::I32, Nullability::NonNullable),
91 DType::Utf8(Nullability::Nullable),
92 false,
93 )
94 }
95
96 fn map(offset: i32, i: i32, nullability: Nullability) -> VortexResult<Scalar> {
99 let dtype = DType::Map(map_dtype()?, nullability);
100 if nullability.is_nullable() && i % 4 == 0 {
101 return Ok(Scalar::null(dtype));
102 }
103
104 Scalar::try_map(
105 dtype,
106 (0..i % 3).map(|j| {
107 let key = offset + j;
108 (
109 Scalar::from(key),
110 Scalar::utf8(format!("v{key}"), Nullability::Nullable),
111 )
112 }),
113 )
114 }
115
116 fn maps(
117 nullability: Nullability,
118 scalars: impl IntoIterator<Item = VortexResult<Scalar>>,
119 ) -> VortexResult<ArrayRef> {
120 let mut builder = builder_with_capacity_in(
121 &DType::Map(map_dtype()?, nullability),
122 0,
123 BufferAllocatorRef::static_ref(),
124 );
125 for scalar in scalars {
126 builder.append_scalar(&scalar?)?;
127 }
128 Ok(builder.finish())
129 }
130
131 #[rstest]
133 #[case::non_nullable(Nullability::NonNullable, Nullability::NonNullable)]
134 #[case::nullable_if_true(Nullability::Nullable, Nullability::NonNullable)]
135 #[case::nullable_if_false(Nullability::NonNullable, Nullability::Nullable)]
136 fn zip_maps(
137 #[case] true_nullability: Nullability,
138 #[case] false_nullability: Nullability,
139 ) -> VortexResult<()> {
140 let len = 20;
141 let selected = |i: i32| i % 2 == 1 || i == 4;
142 let nullability = true_nullability | false_nullability;
143
144 let if_true = maps(
145 true_nullability,
146 (0..len).map(|i| map(0, i, true_nullability)),
147 )?;
148 let if_false = maps(
149 false_nullability,
150 (0..len).map(|i| map(100, i, false_nullability)),
151 )?;
152 let mask = Mask::from_iter((0..len).map(selected));
153
154 let mut ctx = SESSION.create_execution_ctx();
155 let result = mask
156 .into_array()
157 .zip(if_true, if_false)?
158 .execute::<ArrayRef>(&mut ctx)?;
159 assert!(result.is::<Map>());
160
161 let expected = maps(
162 nullability,
163 (0..len).map(|i| {
164 let selected_map = if selected(i) {
165 map(0, i, true_nullability)?
166 } else {
167 map(100, i, false_nullability)?
168 };
169 selected_map.cast(&DType::Map(map_dtype()?, nullability))
170 }),
171 )?;
172 assert_arrays_eq!(result, expected, &mut ctx);
173 Ok(())
174 }
175}