-
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.
- Loading branch information
Showing
8 changed files
with
138 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,85 @@ | ||
use arrow_buffer::Buffer; | ||
use vortex::serde::data::ArrayData; | ||
use vortex_error::VortexResult; | ||
use vortex_schema::DType; | ||
|
||
use crate::visitor::ArrayVisitor; | ||
use crate::Array; | ||
use crate::{Array, ArrayData, ArrayTrait, ToArrayData, WithArray}; | ||
|
||
/// A column batch contains the flattened list of columns. | ||
#[derive(Debug)] | ||
pub struct ColumnBatch { | ||
dtype: DType, | ||
columns: Vec<Array<'static>>, | ||
columns: Vec<ArrayData>, | ||
length: usize, | ||
} | ||
|
||
pub struct ColumnBatchBuilder { | ||
columns: Vec<ArrayData>, | ||
impl ColumnBatch { | ||
pub fn from_array(array: &dyn ArrayTrait) -> Self { | ||
// We want to walk the struct array extracting all nested columns | ||
let mut batch = ColumnBatch { | ||
columns: vec![], | ||
length: array.len(), | ||
}; | ||
array.accept(&mut batch).unwrap(); | ||
batch | ||
} | ||
|
||
pub fn columns(&self) -> &[ArrayData] { | ||
self.columns.as_slice() | ||
} | ||
} | ||
|
||
impl ArrayVisitor for ColumnBatchBuilder { | ||
fn visit_column(&mut self, _name: &str, _array: &Array) -> VortexResult<()> { | ||
todo!() | ||
impl From<&Array<'_>> for ColumnBatch { | ||
fn from(value: &Array) -> Self { | ||
value.with_array(|a| ColumnBatch::from_array(a)) | ||
} | ||
} | ||
|
||
fn visit_child(&mut self, _name: &str, _array: &Array) -> VortexResult<()> { | ||
// If the array is a struct, then pull out each column. | ||
// But we can't do this in case some non-column child is a struct. | ||
// Can we ask an array for column(idx)? Seems like a lot of work. | ||
todo!() | ||
/// Collect all the nested column leaves from an array. | ||
impl ArrayVisitor for ColumnBatch { | ||
fn visit_column(&mut self, _name: &str, array: &Array) -> VortexResult<()> { | ||
let ncols = self.columns.len(); | ||
array.with_array(|a| a.accept(self))?; | ||
if ncols == self.columns.len() { | ||
assert_eq!(self.length, array.len()); | ||
self.columns.push(array.to_array_data()) | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn visit_buffer(&mut self, _buffer: &Buffer) -> VortexResult<()> { | ||
todo!() | ||
fn visit_child(&mut self, _name: &str, array: &Array) -> VortexResult<()> { | ||
// Stop traversing when we hit the first non-column array. | ||
assert_eq!(self.length, array.len()); | ||
self.columns.push(array.to_array_data()); | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use std::sync::Arc; | ||
|
||
use crate::array::primitive::PrimitiveData; | ||
use crate::array::r#struct::StructData; | ||
use crate::batch::ColumnBatch; | ||
use crate::IntoArray; | ||
|
||
#[test] | ||
fn batch_visitor() { | ||
let col = PrimitiveData::from_vec(vec![0, 1, 2]).into_data(); | ||
let nested_struct = StructData::try_new( | ||
vec![Arc::new("x".into()), Arc::new("y".into())], | ||
vec![col.clone(), col.clone()], | ||
3, | ||
) | ||
.unwrap() | ||
.into_data(); | ||
|
||
let arr = StructData::try_new( | ||
vec![Arc::new("a".into()), Arc::new("b".into())], | ||
vec![col.clone(), nested_struct], | ||
3, | ||
) | ||
.unwrap() | ||
.into_array(); | ||
|
||
let batch = ColumnBatch::from(&arr); | ||
assert_eq!(batch.columns().len(), 3); | ||
} | ||
} |
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