Skip to content

Commit be23ca2

Browse files
chore[array]: fsl append null scalar (#5156)
Signed-off-by: Joe Isaacs <[email protected]>
1 parent cc4737e commit be23ca2

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

vortex-array/src/builders/fixed_size_list.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ impl FixedSizeListBuilder {
7171
///
7272
/// [`ListArray`]: crate::arrays::ListArray
7373
pub fn append_value(&mut self, value: ListScalar) -> VortexResult<()> {
74+
let Some(elements) = value.elements() else {
75+
// If `elements` is `None`, then the `value` is a null value.
76+
self.append_null();
77+
return Ok(());
78+
};
79+
7480
if value.len() != self.list_size() as usize {
7581
vortex_bail!(
7682
"Tried to append a `ListScalar` with length {} to a `FixedSizeListScalar` \
@@ -80,12 +86,6 @@ impl FixedSizeListBuilder {
8086
);
8187
}
8288

83-
let Some(elements) = value.elements() else {
84-
// If `elements` is `None`, then the `value` is a null value.
85-
self.append_null();
86-
return Ok(());
87-
};
88-
8989
for scalar in elements {
9090
// TODO(connor): This is slow, we should be able to append multiple values at once, or
9191
// the list scalar should hold an Array
@@ -492,6 +492,28 @@ mod tests {
492492
}
493493
}
494494

495+
#[test]
496+
fn test_append_scalar_nulls() {
497+
// Elements must be nullable if we're going to append null lists
498+
let dtype: Arc<DType> = Arc::new(DType::Primitive(I32, Nullable));
499+
let mut builder = FixedSizeListBuilder::with_capacity(dtype, 2, Nullable, 0);
500+
501+
assert_eq!(builder.dtype().nullability(), Nullable);
502+
builder
503+
.append_scalar(&Scalar::null(builder.dtype().clone()))
504+
.unwrap();
505+
assert_eq!(builder.len(), 1);
506+
507+
let fsl = builder.finish();
508+
assert_eq!(fsl.len(), 1);
509+
510+
let fsl_array = fsl.to_fixed_size_list();
511+
assert_eq!(fsl_array.list_size(), 2);
512+
513+
// Check that all lists are null.
514+
assert!(!fsl_array.validity().is_valid(0));
515+
}
516+
495517
#[test]
496518
fn test_append_zeros_degenerate() {
497519
let dtype: Arc<DType> = Arc::new(I32.into());

0 commit comments

Comments
 (0)