Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Added UnionArray::new_null
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Aug 14, 2021
1 parent d144d06 commit d806ae8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ pub fn new_null_array(data_type: DataType, length: usize) -> Box<dyn Array> {
DataType::LargeList(_) => Box::new(ListArray::<i64>::new_null(data_type, length)),
DataType::FixedSizeList(_, _) => Box::new(FixedSizeListArray::new_null(data_type, length)),
DataType::Struct(fields) => Box::new(StructArray::new_null(&fields, length)),
DataType::Union(_, _, _) => todo!(),
DataType::Union(_, _, _) => Box::new(UnionArray::new_null(data_type, length)),
DataType::Dictionary(key_type, value_type) => match key_type.as_ref() {
DataType::Int8 => Box::new(DictionaryArray::<i8>::new_null(*value_type, length)),
DataType::Int16 => Box::new(DictionaryArray::<i16>::new_null(*value_type, length)),
Expand Down
24 changes: 23 additions & 1 deletion src/array/union/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::HashMap, sync::Arc};

use crate::{
array::{display::get_value_display, display_fmt, new_empty_array, Array},
array::{display::get_value_display, display_fmt, new_empty_array, new_null_array, Array},
bitmap::Bitmap,
buffer::Buffer,
datatypes::{DataType, Field},
Expand Down Expand Up @@ -35,6 +35,28 @@ pub struct UnionArray {
}

impl UnionArray {
pub fn new_null(data_type: DataType, length: usize) -> Self {
if let DataType::Union(f, _, is_sparse) = &data_type {
let fields = f
.iter()
.map(|x| new_null_array(x.data_type().clone(), length).into())
.collect();

let offsets = if *is_sparse {
None
} else {
Some((0..length as i32).collect::<Buffer<i32>>())
};

// all from the same field
let types = Buffer::new_zeroed(length);

Self::from_data(data_type, types, fields, offsets)
} else {
panic!("Union struct must be created with the corresponding Union DataType")
}
}

pub fn new_empty(data_type: DataType) -> Self {
if let DataType::Union(f, _, is_sparse) = &data_type {
let fields = f
Expand Down

0 comments on commit d806ae8

Please sign in to comment.