vortex_functions/zip/
struct_.rs1use std::ops::BitAnd;
5use std::ops::BitOr;
6use std::ops::Not;
7
8use vortex_array::ArrayRef;
9use vortex_array::ArrayView;
10use vortex_array::ExecutionCtx;
11use vortex_array::IntoArray;
12use vortex_array::arrays::Struct;
13use vortex_array::arrays::StructArray;
14use vortex_array::arrays::struct_::StructArrayExt;
15use vortex_array::builtins::ArrayBuiltins;
16use vortex_array::validity::Validity;
17use vortex_error::VortexResult;
18use vortex_mask::Mask;
19
20use crate::builtins::ArrayFunctions;
21use crate::zip::ZipKernel;
22
23impl ZipKernel for Struct {
24 fn zip(
25 if_true: ArrayView<'_, Struct>,
26 if_false: &ArrayRef,
27 mask: &ArrayRef,
28 ctx: &mut ExecutionCtx,
29 ) -> VortexResult<Option<ArrayRef>> {
30 let Some(if_false) = if_false.as_opt::<Struct>() else {
31 return Ok(None);
32 };
33 assert_eq!(
34 if_true.names(),
35 if_false.names(),
36 "input arrays to zip must have the same field names",
37 );
38
39 let fields = if_true
40 .iter_unmasked_fields()
41 .zip(if_false.iter_unmasked_fields())
42 .map(|(t, f)| mask.zip(t.clone(), f.clone()))
43 .collect::<VortexResult<Vec<_>>>()?;
44
45 let v1 = if_true.validity()?;
46 let v2 = if_false.validity()?;
47 let validity = match (&v1, &v2) {
48 (Validity::NonNullable, Validity::NonNullable) => Validity::NonNullable,
49 (Validity::AllValid, Validity::AllValid) => Validity::AllValid,
50 (Validity::AllInvalid, Validity::AllInvalid) => Validity::AllInvalid,
51
52 (v1, v2) => {
53 let mask_mask = mask.clone().fill_null(false)?.execute::<Mask>(ctx)?;
54 let v1m = v1.execute_mask(if_true.len(), ctx)?;
55 let v2m = v2.execute_mask(if_false.len(), ctx)?;
56
57 let combined = v1m.bitand(&mask_mask).bitor(&v2m.bitand(&mask_mask.not()));
58 Validity::from_mask(
59 combined,
60 if_true.dtype().nullability() | if_false.dtype().nullability(),
61 )
62 }
63 };
64
65 Ok(Some(
66 StructArray::try_new(if_true.names().clone(), fields, if_true.len(), validity)?
67 .into_array(),
68 ))
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use std::sync::LazyLock;
75
76 use vortex_array::IntoArray;
77 use vortex_array::VortexSessionExecute;
78 use vortex_array::arrays::PrimitiveArray;
79 use vortex_array::arrays::StructArray;
80 use vortex_array::dtype::FieldNames;
81 use vortex_array::validity::Validity;
82 use vortex_mask::Mask;
83 use vortex_session::VortexSession;
84
85 use crate::builtins::ArrayFunctions;
86
87 static SESSION: LazyLock<VortexSession> = LazyLock::new(|| {
88 let session = VortexSession::empty();
89
90 vortex_array::initialize(&session);
91
92 crate::initialize(&session);
93
94 session
95 });
96
97 #[test]
98 fn test_validity_zip_both_validity_array() {
99 let ctx = SESSION.create_execution_ctx();
100 let if_true = StructArray::new(
102 FieldNames::from_iter(["field"]),
103 vec![PrimitiveArray::from_iter([1, 2, 3, 4]).into_array()],
104 4,
105 Validity::from_iter([true, false, true, false]),
106 )
107 .into_array();
108
109 let if_false = StructArray::new(
110 FieldNames::from_iter(["field"]),
111 vec![PrimitiveArray::from_iter([10, 20, 30, 40]).into_array()],
112 4,
113 Validity::from_iter([false, true, false, true]),
114 )
115 .into_array();
116
117 let mask = Mask::from_iter([false, false, true, false]);
118
119 let result = mask.into_array().zip(if_true, if_false).unwrap();
120
121 insta::assert_snapshot!(result.display_table(&ctx), @r"
122 ┌───────┐
123 │ field │
124 ├───────┤
125 │ null │
126 ├───────┤
127 │ 20i32 │
128 ├───────┤
129 │ 3i32 │
130 ├───────┤
131 │ 40i32 │
132 └───────┘
133 ");
134 }
135
136 #[test]
137 fn test_validity_zip_allvalid_and_array() {
138 let ctx = SESSION.create_execution_ctx();
139 let if_true = StructArray::new(
140 FieldNames::from_iter(["a"]),
141 vec![PrimitiveArray::from_iter([1, 2, 3, 4]).into_array()],
142 4,
143 Validity::AllValid,
144 )
145 .into_array();
146
147 let if_false = StructArray::new(
148 FieldNames::from_iter(["a"]),
149 vec![PrimitiveArray::from_iter([10, 20, 30, 40]).into_array()],
150 4,
151 Validity::from_iter([false, false, true, true]),
152 )
153 .into_array();
154
155 let mask = Mask::from_iter([true, false, false, false]);
156
157 let result = mask.into_array().zip(if_true, if_false).unwrap();
158
159 insta::assert_snapshot!(result.display_table(&ctx), @r"
160 ┌───────┐
161 │ a │
162 ├───────┤
163 │ 1i32 │
164 ├───────┤
165 │ null │
166 ├───────┤
167 │ 30i32 │
168 ├───────┤
169 │ 40i32 │
170 └───────┘
171 ");
172 }
173}