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

Commit

Permalink
Have UnionScalar hold a non-nullable value
Browse files Browse the repository at this point in the history
  • Loading branch information
ncpenke committed Apr 3, 2022
1 parent 0529b67 commit 12bcdfc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
10 changes: 3 additions & 7 deletions src/scalar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,13 @@ pub fn new_scalar(array: &dyn Array, index: usize) -> Box<dyn Scalar> {
}
Union => {
let array = array.as_any().downcast_ref::<UnionArray>().unwrap();
let type_ = array.types()[index];
let type_id = array.types()[index];
let (field_index, index) = array.index(index);
let field_value = new_scalar(&*array.fields()[field_index], index);
Box::new(UnionScalar::new(
array.data_type().clone(),
type_,
if field_value.is_valid() {
Some(field_value.into())
} else {
None
},
type_id,
field_value.into(),
))
}
Map => todo!(),
Expand Down
8 changes: 4 additions & 4 deletions src/scalar/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use super::Scalar;
/// A single entry of a [`crate::array::UnionArray`].
#[derive(Debug, Clone, PartialEq)]
pub struct UnionScalar {
value: Option<Arc<dyn Scalar>>,
value: Arc<dyn Scalar>,
type_: i8,
data_type: DataType,
}

impl UnionScalar {
/// Returns a new [`UnionScalar`]
#[inline]
pub fn new(data_type: DataType, type_: i8, value: Option<Arc<dyn Scalar>>) -> Self {
pub fn new(data_type: DataType, type_: i8, value: Arc<dyn Scalar>) -> Self {
Self {
value,
type_,
Expand All @@ -25,7 +25,7 @@ impl UnionScalar {

/// Returns the values irrespectively of the validity.
#[inline]
pub fn value(&self) -> &Option<Arc<dyn Scalar>> {
pub fn value(&self) -> &Arc<dyn Scalar> {
&self.value
}
}
Expand All @@ -38,7 +38,7 @@ impl Scalar for UnionScalar {

#[inline]
fn is_valid(&self) -> bool {
self.value.is_some()
true
}

#[inline]
Expand Down
6 changes: 2 additions & 4 deletions tests/it/array/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ where
.downcast_ref::<UnionScalar>()
.unwrap()
.value()
.as_ref()
.unwrap()
.as_any()
.downcast_ref::<T>()
.unwrap()
Expand All @@ -31,14 +29,14 @@ fn assert_next_is_none<I>(iter: &mut I)
where
I: Iterator<Item = Box<dyn Scalar>>,
{
assert!(iter
assert!(!iter
.next()
.unwrap()
.as_any()
.downcast_ref::<UnionScalar>()
.unwrap()
.value()
.is_none())
.is_valid())
}

#[test]
Expand Down

0 comments on commit 12bcdfc

Please sign in to comment.