-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CHORE] Refactor arrays to share a FromArrow constructor trait (#1276)
This eliminates some code duplication by having each Array implementation implement a `FromArrow` trait. Will be useful when we add more arrays (e.g. StructArray, FixedSizeListArray, LogicalStructArray etc) Co-authored-by: Jay Chia <[email protected]@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
38 additions
and
11 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,28 @@ | ||
use common_error::DaftResult; | ||
|
||
use crate::{ | ||
array::DataArray, | ||
datatypes::{logical::LogicalArray, DaftLogicalType, DaftPhysicalType, Field}, | ||
}; | ||
|
||
/// Arrays that implement [`FromArrow`] can be instantiated from a Box<dyn arrow2::array::Array> | ||
pub trait FromArrow | ||
where | ||
Self: Sized, | ||
{ | ||
fn from_arrow(field: &Field, arrow_arr: Box<dyn arrow2::array::Array>) -> DaftResult<Self>; | ||
} | ||
|
||
impl<T: DaftPhysicalType> FromArrow for DataArray<T> { | ||
fn from_arrow(field: &Field, arrow_arr: Box<dyn arrow2::array::Array>) -> DaftResult<Self> { | ||
DataArray::<T>::try_from((field.clone(), arrow_arr)) | ||
} | ||
} | ||
|
||
impl<L: DaftLogicalType> FromArrow for LogicalArray<L> { | ||
fn from_arrow(field: &Field, arrow_arr: Box<dyn arrow2::array::Array>) -> DaftResult<Self> { | ||
let data_array_field = Field::new(field.name.clone(), field.dtype.to_physical()); | ||
let physical = DataArray::try_from((data_array_field, arrow_arr))?; | ||
Ok(LogicalArray::<L>::new(field.clone(), physical)) | ||
} | ||
} |
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