Skip to content

Commit de38d3f

Browse files
authored
fix: Correctly canonicalize nullable list arrays (#2808)
1 parent 7d863d7 commit de38d3f

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

encodings/fsst/src/canonical.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ mod tests {
164164
let (chunked_arr, data) = make_data_chunked();
165165

166166
let mut builder =
167-
VarBinViewBuilder::with_capacity(DType::Utf8(Nullability::Nullable), chunked_arr.len());
167+
VarBinViewBuilder::with_capacity(chunked_arr.dtype().clone(), chunked_arr.len());
168168
chunked_arr.append_to_builder(&mut builder).unwrap();
169169

170170
{

vortex-array/src/array/implementation.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,21 @@ impl<A: ArrayImpl + 'static> Array for A {
186186
///
187187
/// The [`DType`] of the builder must match that of the array.
188188
fn append_to_builder(&self, builder: &mut dyn ArrayBuilder) -> VortexResult<()> {
189-
// TODO(ngates): add dtype function to ArrayBuilder
190-
// if builder.dtype() != self.dtype() {
191-
// vortex_bail!(
192-
// "Builder dtype mismatch: expected {:?}, got {:?}",
193-
// self.dtype(),
194-
// builder.dtype()
195-
// );
196-
// }
189+
if builder.dtype() != self.dtype() {
190+
vortex_bail!(
191+
"Builder dtype mismatch: expected {}, got {}",
192+
self.dtype(),
193+
builder.dtype(),
194+
);
195+
}
197196
let len = builder.len();
198197

199198
ArrayCanonicalImpl::_append_to_builder(self, builder)?;
200199
assert_eq!(
201200
len + self.len(),
202201
builder.len(),
203-
"Builder length mismatch after writing array"
202+
"Builder length mismatch after writing array for encoding {}",
203+
self.encoding(),
204204
);
205205
Ok(())
206206
}

vortex-array/src/arrays/constant/canonical.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ fn canonical_list_array(
145145
) -> VortexResult<ListArray> {
146146
match values {
147147
None => ListArray::try_new(
148-
ConstantArray::new(Scalar::null(element_dtype.clone()), 1).into_array(),
148+
Canonical::empty(element_dtype).into_array(),
149149
ConstantArray::new(
150150
Scalar::new(
151151
DType::Primitive(PType::U64, Nullability::NonNullable),
152152
ScalarValue::from(0),
153153
),
154-
len,
154+
len + 1,
155155
)
156156
.into_array(),
157157
Validity::AllInvalid,
@@ -305,4 +305,29 @@ mod tests {
305305
[0u64, 0, 0]
306306
);
307307
}
308+
309+
#[test]
310+
fn test_canonicalize_null_list() {
311+
let list_scalar = Scalar::null(DType::List(
312+
Arc::new(DType::Primitive(PType::U64, Nullability::NonNullable)),
313+
Nullability::Nullable,
314+
));
315+
let const_array = ConstantArray::new(list_scalar, 2).into_array();
316+
let canonical_const = const_array.to_list().unwrap();
317+
assert!(
318+
canonical_const
319+
.elements()
320+
.to_primitive()
321+
.unwrap()
322+
.is_empty()
323+
);
324+
assert_eq!(
325+
canonical_const
326+
.offsets()
327+
.to_primitive()
328+
.unwrap()
329+
.as_slice::<u64>(),
330+
[0u64, 0, 0]
331+
);
332+
}
308333
}

0 commit comments

Comments
 (0)