Skip to content

Commit

Permalink
Allow Constructing Non-Empty StructArray with no Fields (#4842) (#4845)
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold authored Sep 25, 2023
1 parent 6d5d7e3 commit 72a2dab
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
17 changes: 17 additions & 0 deletions arrow-array/src/array/struct_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,23 @@ impl StructArray {
}
}

/// Create a new [`StructArray`] containing no fields
///
/// # Panics
///
/// If `len != nulls.len()`
pub fn new_empty_fields(len: usize, nulls: Option<NullBuffer>) -> Self {
if let Some(n) = &nulls {
assert_eq!(len, n.len())
}
Self {
len,
data_type: DataType::Struct(Fields::empty()),
fields: vec![],
nulls,
}
}

/// Deconstruct this array into its constituent parts
pub fn into_parts(self) -> (Fields, Vec<ArrayRef>, Option<NullBuffer>) {
let f = match self.data_type {
Expand Down
28 changes: 28 additions & 0 deletions arrow-array/src/builder/struct_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ impl StructBuilder {
/// Builds the `StructArray` and reset this builder.
pub fn finish(&mut self) -> StructArray {
self.validate_content();
if self.fields.is_empty() {
return StructArray::new_empty_fields(
self.len(),
self.null_buffer_builder.finish(),
);
}

let arrays = self.field_builders.iter_mut().map(|f| f.finish()).collect();
let nulls = self.null_buffer_builder.finish();
Expand All @@ -243,6 +249,13 @@ impl StructBuilder {
pub fn finish_cloned(&self) -> StructArray {
self.validate_content();

if self.fields.is_empty() {
return StructArray::new_empty_fields(
self.len(),
self.null_buffer_builder.finish_cloned(),
);
}

let arrays = self
.field_builders
.iter()
Expand Down Expand Up @@ -591,4 +604,19 @@ mod tests {
let mut sa = StructBuilder::new(fields, field_builders);
sa.finish();
}

#[test]
fn test_empty() {
let mut builder = StructBuilder::new(Fields::empty(), vec![]);
builder.append(true);
builder.append(false);

let a1 = builder.finish_cloned();
let a2 = builder.finish();
assert_eq!(a1, a2);
assert_eq!(a1.len(), 2);
assert_eq!(a1.null_count(), 1);
assert!(a1.is_valid(0));
assert!(a1.is_null(1));
}
}

0 comments on commit 72a2dab

Please sign in to comment.