1use vortex_array::ArrayRef;
5use vortex_array::ArrayView;
6use vortex_array::ExecutionCtx;
7use vortex_array::arrays::Widen;
8use vortex_array::arrays::widen::WidenArraySlotsExt;
9use vortex_array::arrays::widen::compute::cast_keeping_nullability;
10use vortex_array::arrays::widen::compute::ptype_holding_both_values;
11use vortex_array::arrays::widen::compute::ptype_holding_values_and_scalar;
12use vortex_array::arrays::widen::compute::wrap_values;
13use vortex_error::VortexResult;
14
15use crate::builtins::ArrayFunctions;
16use crate::zip::ZipKernel;
17
18impl ZipKernel for Widen {
21 fn zip(
22 array: ArrayView<'_, Self>,
23 if_false: &ArrayRef,
24 mask: &ArrayRef,
25 _ctx: &mut ExecutionCtx,
26 ) -> VortexResult<Option<ArrayRef>> {
27 let (if_false_values, ptype) = if let Some(other) = if_false.as_opt::<Widen>() {
28 let Some(ptype) = ptype_holding_both_values(array, other) else {
29 return Ok(None);
30 };
31 (other.values().clone(), ptype)
32 } else if let Some(constant) = if_false.as_constant() {
33 let Some(ptype) = ptype_holding_values_and_scalar(array, constant) else {
34 return Ok(None);
35 };
36 (if_false.clone(), ptype)
37 } else {
38 return Ok(None);
39 };
40
41 let if_true_values = cast_keeping_nullability(array.values(), ptype)?;
42 let if_false_values = cast_keeping_nullability(&if_false_values, ptype)?;
43
44 let zipped = mask.zip(if_true_values, if_false_values)?;
45 let dtype = array.dtype().with_nullability(zipped.dtype().nullability());
46
47 wrap_values(zipped, dtype).map(Some)
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use std::sync::LazyLock;
54
55 use rstest::rstest;
56 use vortex_array::ArrayRef;
57 use vortex_array::Canonical;
58 use vortex_array::ExecutionCtx;
59 use vortex_array::IntoArray;
60 use vortex_array::VortexSessionExecute;
61 use vortex_array::arrays::BoolArray;
62 use vortex_array::arrays::ConstantArray;
63 use vortex_array::arrays::DecimalArray;
64 use vortex_array::arrays::PrimitiveArray;
65 use vortex_array::arrays::Widen;
66 use vortex_array::arrays::WidenArray;
67 use vortex_array::arrays::widen::WidenArraySlotsExt;
68 use vortex_array::assert_arrays_eq;
69 use vortex_array::dtype::DType;
70 use vortex_array::dtype::DecimalDType;
71 use vortex_array::dtype::Nullability;
72 use vortex_array::dtype::PType;
73 use vortex_array::validity::Validity;
74 use vortex_buffer::buffer;
75 use vortex_error::VortexExpect;
76 use vortex_error::VortexResult;
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 canonical(array: &ArrayRef, ctx: &mut ExecutionCtx) -> VortexResult<ArrayRef> {
90 Ok(array.clone().execute::<Canonical>(ctx)?.into_array())
91 }
92
93 fn widen(values: impl IntoArray, logical: PType) -> WidenArray {
95 let values = values.into_array();
96 let dtype = DType::Primitive(logical, values.dtype().nullability());
97 WidenArray::try_new(values, dtype).vortex_expect("fixture values are narrower integers")
98 }
99
100 fn stored_ptype(array: &ArrayRef) -> PType {
102 match array.as_opt::<Widen>() {
103 Some(widen) => widen.values().dtype().as_ptype(),
104 None => array.dtype().as_ptype(),
105 }
106 }
107
108 fn signed() -> WidenArray {
110 let values = [Some(i8::MIN), Some(-1), Some(0), Some(1), Some(i8::MAX)];
111 widen(PrimitiveArray::from_option_iter(values), PType::I64)
112 }
113
114 fn partially_valid() -> WidenArray {
116 let values = [Some(-300i16), None, Some(0), None, Some(300)];
117 widen(PrimitiveArray::from_option_iter(values), PType::I64)
118 }
119
120 fn decimal_widen(validity: Validity) -> VortexResult<ArrayRef> {
122 let values = DecimalArray::new(
123 buffer![-125i16, 50, 50, 250, 900],
124 DecimalDType::new(4, 2),
125 validity,
126 &mut SESSION.create_execution_ctx(),
127 );
128 let dtype = DType::Decimal(DecimalDType::new(18, 2), values.dtype().nullability());
129 Ok(WidenArray::try_new(values.into_array(), dtype)?.into_array())
130 }
131
132 #[rstest]
135 #[case::constant_if_false(
136 signed().into_array(),
137 ConstantArray::new(1_000i64, 5).into_array(),
138 )]
139 #[case::constant_if_true(
140 ConstantArray::new(1_000i64, 5).into_array(),
141 signed().into_array(),
142 )]
143 #[case::nullable_if_false(
144 ConstantArray::new(1_000i64, 5).into_array(),
145 partially_valid().into_array(),
146 )]
147 #[case::widen_both(
148 signed().into_array(),
149 widen(buffer![1_000i16, 2, 300, -4, 5], PType::I64).into_array(),
150 )]
151 fn test_zip(#[case] if_true: ArrayRef, #[case] if_false: ArrayRef) -> VortexResult<()> {
152 let mut ctx = SESSION.create_execution_ctx();
153 let mask = BoolArray::from_iter([Some(true), None, Some(false), Some(true), Some(false)]);
154 let mask = mask.into_array();
155
156 let zipped = mask
157 .zip(if_true.clone(), if_false.clone())?
158 .execute::<ArrayRef>(&mut ctx)?;
159
160 assert_eq!(stored_ptype(&zipped), PType::I16);
161 let expected = mask.zip(
162 canonical(&if_true, &mut ctx)?,
163 canonical(&if_false, &mut ctx)?,
164 )?;
165 assert_arrays_eq!(zipped, expected, &mut ctx);
166
167 Ok(())
168 }
169
170 #[rstest]
171 #[case::true_branch(true, false)]
172 #[case::false_branch(false, true)]
173 #[case::both_branches(true, true)]
174 fn test_decimal_zip_falls_back_to_canonical(
175 #[case] widen_if_true: bool,
176 #[case] widen_if_false: bool,
177 ) -> VortexResult<()> {
178 let mut ctx = SESSION.create_execution_ctx();
179 let if_true = decimal_widen(Validity::from_iter([true, false, true, true, false]))?;
180 let if_false = WidenArray::try_new(
181 DecimalArray::from_iter([500i16, -600, 700, -800, 900], DecimalDType::new(4, 2))
182 .into_array(),
183 DType::Decimal(DecimalDType::new(18, 2), Nullability::NonNullable),
184 )?
185 .into_array();
186 let canonical_true = canonical(&if_true, &mut ctx)?;
187 let canonical_false = canonical(&if_false, &mut ctx)?;
188 let mask = BoolArray::from_iter([Some(true), Some(true), None, Some(false), Some(true)])
189 .into_array();
190 let expected = mask
191 .zip(canonical_true.clone(), canonical_false.clone())?
192 .execute::<Canonical>(&mut ctx)?;
193 let if_true = if widen_if_true {
194 if_true
195 } else {
196 canonical_true
197 };
198 let if_false = if widen_if_false {
199 if_false
200 } else {
201 canonical_false
202 };
203
204 let actual = mask
205 .zip(if_true, if_false)?
206 .execute::<Canonical>(&mut ctx)?;
207
208 assert_arrays_eq!(actual, expected, &mut ctx);
209
210 Ok(())
211 }
212}