-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
412 additions
and
44 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use vortex::scalar::Scalar; | ||
use vortex_error::VortexResult; | ||
|
||
use crate::array::chunked::ChunkedArray; | ||
use crate::compute::as_contiguous::{as_contiguous, AsContiguousFn}; | ||
use crate::compute::scalar_at::{scalar_at, ScalarAtFn}; | ||
use crate::compute::take::TakeFn; | ||
use crate::compute::ArrayCompute; | ||
use crate::{Array, OwnedArray, ToStatic}; | ||
|
||
mod take; | ||
|
||
impl ArrayCompute for ChunkedArray<'_> { | ||
fn as_contiguous(&self) -> Option<&dyn AsContiguousFn> { | ||
Some(self) | ||
} | ||
|
||
fn scalar_at(&self) -> Option<&dyn ScalarAtFn> { | ||
Some(self) | ||
} | ||
|
||
fn take(&self) -> Option<&dyn TakeFn> { | ||
Some(self) | ||
} | ||
} | ||
|
||
impl AsContiguousFn for ChunkedArray<'_> { | ||
fn as_contiguous(&self, arrays: &[Array]) -> VortexResult<OwnedArray> { | ||
// Combine all the chunks into one, then call as_contiguous again. | ||
let mut chunks = Vec::with_capacity(self.nchunks()); | ||
for array in arrays { | ||
for chunk in ChunkedArray::try_from(array).unwrap().chunks() { | ||
chunks.push(chunk.to_static()); | ||
} | ||
} | ||
as_contiguous(&chunks) | ||
} | ||
} | ||
|
||
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).unwrap(), chunk_offset) | ||
} | ||
} |
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,82 @@ | ||
use vortex::ptype::PType; | ||
use vortex_error::VortexResult; | ||
|
||
use crate::array::chunked::ChunkedArray; | ||
use crate::compute::cast::cast; | ||
use crate::compute::take::{take, TakeFn}; | ||
use crate::{Array, IntoArray, OwnedArray, ToArray, ToStatic}; | ||
|
||
impl TakeFn for ChunkedArray<'_> { | ||
fn take(&self, indices: &Array) -> VortexResult<OwnedArray> { | ||
if self.len() == indices.len() { | ||
return Ok(self.to_array().to_static()); | ||
} | ||
|
||
let indices = cast(indices, PType::U64.into())?.flatten_primitive()?; | ||
|
||
// While the chunk idx remains the same, accumulate a list of chunk indices. | ||
let mut chunks = Vec::new(); | ||
let mut indices_in_chunk = Vec::new(); | ||
let mut prev_chunk_idx = self | ||
.find_chunk_idx(indices.typed_data::<u64>()[0] as usize) | ||
.0; | ||
for idx in indices.typed_data::<u64>() { | ||
let (chunk_idx, idx_in_chunk) = self.find_chunk_idx(*idx as usize); | ||
|
||
if chunk_idx != prev_chunk_idx { | ||
// Start a new chunk | ||
let indices_in_chunk_array = indices_in_chunk.clone().into_array(); | ||
chunks.push(take( | ||
&self.chunk(prev_chunk_idx).unwrap(), | ||
&indices_in_chunk_array, | ||
)?); | ||
indices_in_chunk = Vec::new(); | ||
} | ||
|
||
indices_in_chunk.push(idx_in_chunk as u64); | ||
prev_chunk_idx = chunk_idx; | ||
} | ||
|
||
if !indices_in_chunk.is_empty() { | ||
let indices_in_chunk_array = indices_in_chunk.into_array(); | ||
chunks.push(take( | ||
&self.chunk(prev_chunk_idx).unwrap(), | ||
&indices_in_chunk_array, | ||
)?); | ||
} | ||
|
||
Ok(ChunkedArray::new(chunks, self.dtype().clone()).into_array()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use itertools::Itertools; | ||
|
||
use crate::array::chunked::ChunkedArray; | ||
use crate::array::primitive::PrimitiveArray; | ||
use crate::compute::as_contiguous::as_contiguous; | ||
use crate::compute::take::take; | ||
use crate::IntoArray; | ||
|
||
#[test] | ||
fn test_take() { | ||
let a = vec![1i32, 2, 3].into_array(); | ||
let arr = ChunkedArray::new(vec![a.clone(), a.clone(), a.clone()], a.dtype().clone()); | ||
assert_eq!(arr.nchunks(), 3); | ||
assert_eq!(arr.len(), 9); | ||
let indices = vec![0, 0, 6, 4].into_array(); | ||
|
||
let result = PrimitiveArray::try_from( | ||
as_contiguous( | ||
&ChunkedArray::try_from(take(arr.as_ref(), &indices).unwrap()) | ||
.unwrap() | ||
.chunks() | ||
.collect_vec(), | ||
) | ||
.unwrap(), | ||
) | ||
.unwrap(); | ||
assert_eq!(result.typed_data::<i32>(), &[1, 1, 1, 2]); | ||
} | ||
} |
Oops, something went wrong.