-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ChunkedArray find_chunk_idx for empty chunks (#802)
- Loading branch information
1 parent
1a384b3
commit 750b9c8
Showing
8 changed files
with
171 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use vortex_error::{vortex_err, vortex_panic, VortexResult}; | ||
use vortex_scalar::Scalar; | ||
|
||
use crate::array::ChunkedArray; | ||
use crate::compute::unary::{scalar_at, scalar_at_unchecked, ScalarAtFn}; | ||
|
||
impl ScalarAtFn for ChunkedArray { | ||
fn scalar_at(&self, index: usize) -> VortexResult<Scalar> { | ||
let (chunk_index, chunk_offset) = self.find_chunk_idx(index); | ||
scalar_at( | ||
&self | ||
.chunk(chunk_index) | ||
.ok_or_else(|| vortex_err!(OutOfBounds: chunk_index, 0, self.nchunks()))?, | ||
chunk_offset, | ||
) | ||
} | ||
|
||
fn scalar_at_unchecked(&self, index: usize) -> Scalar { | ||
let (chunk_index, chunk_offset) = self.find_chunk_idx(index); | ||
scalar_at_unchecked( | ||
&self | ||
.chunk(chunk_index) | ||
.unwrap_or_else(|| vortex_panic!(OutOfBounds: chunk_index, 0, self.nchunks())), | ||
chunk_offset, | ||
) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use vortex_dtype::{DType, Nullability, PType}; | ||
|
||
use crate::array::{ChunkedArray, PrimitiveArray}; | ||
use crate::compute::unary::scalar_at; | ||
use crate::IntoArray; | ||
|
||
#[test] | ||
fn empty_children_both_sides() { | ||
let array = ChunkedArray::try_new( | ||
vec![ | ||
PrimitiveArray::from(Vec::<u64>::new()).into_array(), | ||
PrimitiveArray::from(Vec::<u64>::new()).into_array(), | ||
PrimitiveArray::from(vec![1u64, 2]).into_array(), | ||
PrimitiveArray::from(Vec::<u64>::new()).into_array(), | ||
PrimitiveArray::from(Vec::<u64>::new()).into_array(), | ||
], | ||
DType::Primitive(PType::U64, Nullability::NonNullable), | ||
) | ||
.unwrap(); | ||
assert_eq!(scalar_at(array.array(), 0).unwrap(), 1u64.into()); | ||
assert_eq!(scalar_at(array.array(), 1).unwrap(), 2u64.into()); | ||
} | ||
|
||
#[test] | ||
fn empty_children_trailing() { | ||
let array = ChunkedArray::try_new( | ||
vec![ | ||
PrimitiveArray::from(vec![1u64, 2]).into_array(), | ||
PrimitiveArray::from(Vec::<u64>::new()).into_array(), | ||
PrimitiveArray::from(Vec::<u64>::new()).into_array(), | ||
PrimitiveArray::from(vec![3u64, 4]).into_array(), | ||
], | ||
DType::Primitive(PType::U64, Nullability::NonNullable), | ||
) | ||
.unwrap(); | ||
assert_eq!(scalar_at(array.array(), 0).unwrap(), 1u64.into()); | ||
assert_eq!(scalar_at(array.array(), 1).unwrap(), 2u64.into()); | ||
assert_eq!(scalar_at(array.array(), 2).unwrap(), 3u64.into()); | ||
assert_eq!(scalar_at(array.array(), 3).unwrap(), 4u64.into()); | ||
} | ||
|
||
#[test] | ||
fn empty_children_leading() { | ||
let array = ChunkedArray::try_new( | ||
vec![ | ||
PrimitiveArray::from(Vec::<u64>::new()).into_array(), | ||
PrimitiveArray::from(Vec::<u64>::new()).into_array(), | ||
PrimitiveArray::from(vec![1u64, 2]).into_array(), | ||
PrimitiveArray::from(vec![3u64, 4]).into_array(), | ||
], | ||
DType::Primitive(PType::U64, Nullability::NonNullable), | ||
) | ||
.unwrap(); | ||
assert_eq!(scalar_at(array.array(), 0).unwrap(), 1u64.into()); | ||
assert_eq!(scalar_at(array.array(), 1).unwrap(), 2u64.into()); | ||
assert_eq!(scalar_at(array.array(), 2).unwrap(), 3u64.into()); | ||
assert_eq!(scalar_at(array.array(), 3).unwrap(), 4u64.into()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters