-
Notifications
You must be signed in to change notification settings - Fork 416
refactor: logical instead of physical extension array #6409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
universalmind303
wants to merge
1
commit into
main
Choose a base branch
from
extension-logical-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,154 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use arrow::{array::ArrayRef, buffer::NullBuffer}; | ||
| use common_error::DaftResult; | ||
| use daft_schema::{dtype::DataType, field::Field}; | ||
|
|
||
| use crate::{datatypes::DaftArrayType, series::Series}; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct ExtensionArray { | ||
| field: Arc<Field>, | ||
| /// Extension type name (e.g. "geoarrow.point") | ||
| extension_name: Arc<str>, | ||
| /// Extension metadata (e.g. '{"crs": "WGS84"}') | ||
| metadata: Option<Arc<str>>, | ||
| /// The underlying storage data | ||
| pub physical: Series, | ||
| } | ||
|
|
||
| impl ExtensionArray { | ||
| pub fn new(field: Arc<Field>, physical: Series) -> Self { | ||
| let DataType::Extension(ext_name, _, ext_metadata) = &field.dtype else { | ||
| panic!( | ||
| "ExtensionArray field must have Extension dtype, got {}", | ||
| field.dtype | ||
| ); | ||
| }; | ||
| Self { | ||
| extension_name: Arc::from(ext_name.as_str()), | ||
| metadata: ext_metadata.as_deref().map(Arc::from), | ||
| field, | ||
| physical, | ||
| } | ||
| } | ||
|
|
||
| pub fn name(&self) -> &str { | ||
| self.field.name.as_ref() | ||
| } | ||
|
|
||
| pub fn data_type(&self) -> &DataType { | ||
| &self.field.dtype | ||
| } | ||
|
|
||
| pub fn extension_name(&self) -> &str { | ||
| &self.extension_name | ||
| } | ||
|
|
||
| pub fn extension_metadata(&self) -> Option<&str> { | ||
| self.metadata.as_deref() | ||
| } | ||
|
|
||
| pub fn field(&self) -> &Field { | ||
| &self.field | ||
| } | ||
|
|
||
| pub fn len(&self) -> usize { | ||
| self.physical.len() | ||
| } | ||
|
|
||
| pub fn is_empty(&self) -> bool { | ||
| self.len() == 0 | ||
| } | ||
|
|
||
| pub fn rename(&self, name: &str) -> Self { | ||
| Self { | ||
| field: Arc::new(Field::new(name, self.field.dtype.clone())), | ||
| extension_name: self.extension_name.clone(), | ||
| metadata: self.metadata.clone(), | ||
| physical: self.physical.rename(name), | ||
| } | ||
| } | ||
|
|
||
| /// Replace the underlying physical `Series` of this `ExtensionArray`. | ||
| pub fn with_physical(&self, physical: Series) -> Self { | ||
| Self { | ||
| field: self.field.clone(), | ||
| extension_name: self.extension_name.clone(), | ||
| metadata: self.metadata.clone(), | ||
| physical, | ||
| } | ||
| } | ||
|
|
||
| pub fn nulls(&self) -> Option<&NullBuffer> { | ||
| self.physical.inner.nulls() | ||
| } | ||
|
|
||
| pub fn to_arrow(&self) -> DaftResult<ArrayRef> { | ||
| let arr = self.physical.to_arrow()?; | ||
| let target_field = self.field.to_arrow()?; | ||
| if arr.data_type() != target_field.data_type() { | ||
| Ok(arrow::compute::cast(&arr, target_field.data_type())?) | ||
| } else { | ||
| Ok(arr) | ||
| } | ||
| } | ||
|
|
||
| pub fn slice(&self, start: usize, end: usize) -> DaftResult<Self> { | ||
| Ok(self.with_physical(self.physical.slice(start, end)?)) | ||
| } | ||
|
|
||
| pub fn concat(arrays: &[&Self]) -> DaftResult<Self> { | ||
| if arrays.is_empty() { | ||
| return Err(common_error::DaftError::ValueError( | ||
| "Cannot concat empty list of ExtensionArrays".to_string(), | ||
| )); | ||
| } | ||
| let first = arrays[0]; | ||
| let physical_arrays: Vec<&Series> = arrays.iter().map(|a| &a.physical).collect(); | ||
| let physical = Series::concat(&physical_arrays)?; | ||
| Ok(first.with_physical(physical)) | ||
| } | ||
| } | ||
|
|
||
| impl DaftArrayType for ExtensionArray { | ||
| fn data_type(&self) -> &DataType { | ||
| &self.field.dtype | ||
| } | ||
| } | ||
|
|
||
| impl crate::array::ops::from_arrow::FromArrow for ExtensionArray { | ||
| fn from_arrow<F: Into<daft_schema::field::FieldRef>>( | ||
| field: F, | ||
| arrow_arr: ArrayRef, | ||
| ) -> DaftResult<Self> { | ||
| let field: daft_schema::field::FieldRef = field.into(); | ||
| let DataType::Extension(_, storage_type, _) = &field.dtype else { | ||
| return Err(common_error::DaftError::TypeError(format!( | ||
| "Expected Extension dtype for ExtensionArray, got {}", | ||
| field.dtype | ||
| ))); | ||
| }; | ||
| let storage_field = Arc::new(Field::new(field.name.as_ref(), *storage_type.clone())); | ||
| let physical = Series::from_arrow(storage_field, arrow_arr)?; | ||
| Ok(Self::new(field, physical)) | ||
| } | ||
| } | ||
|
|
||
| impl crate::array::ops::full::FullNull for ExtensionArray { | ||
| fn full_null(name: &str, dtype: &DataType, length: usize) -> Self { | ||
| let DataType::Extension(_, storage_type, _) = dtype else { | ||
| panic!("Expected Extension dtype for ExtensionArray::full_null, got {dtype}"); | ||
| }; | ||
| let physical = Series::full_null(name, storage_type, length); | ||
| Self::new(Arc::new(Field::new(name, dtype.clone())), physical) | ||
| } | ||
|
|
||
| fn empty(name: &str, dtype: &DataType) -> Self { | ||
| let DataType::Extension(_, storage_type, _) = dtype else { | ||
| panic!("Expected Extension dtype for ExtensionArray::empty, got {dtype}"); | ||
| }; | ||
| let physical = Series::empty(name, storage_type); | ||
| Self::new(Arc::new(Field::new(name, dtype.clone())), physical) | ||
| } | ||
| } |
This file contains hidden or 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,54 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use common_error::DaftResult; | ||
| use daft_schema::{dtype::DataType, field::Field}; | ||
|
|
||
| use super::Growable; | ||
| use crate::{ | ||
| array::extension_array::ExtensionArray, | ||
| series::{IntoSeries, Series}, | ||
| }; | ||
|
|
||
| pub struct ExtensionGrowable<'a> { | ||
| name: String, | ||
| dtype: DataType, | ||
| physical_growable: Box<dyn Growable + 'a>, | ||
| } | ||
|
|
||
| impl<'a> ExtensionGrowable<'a> { | ||
| pub fn new( | ||
| name: &str, | ||
| dtype: &DataType, | ||
| arrays: Vec<&'a ExtensionArray>, | ||
| use_validity: bool, | ||
| capacity: usize, | ||
| ) -> Self { | ||
| let DataType::Extension(_, storage_type, _) = dtype else { | ||
| panic!("Expected Extension dtype for ExtensionGrowable, got {dtype}"); | ||
| }; | ||
| let physical_series: Vec<&Series> = arrays.iter().map(|a| &a.physical).collect(); | ||
| let physical_growable = | ||
| super::make_growable(name, storage_type, physical_series, use_validity, capacity); | ||
| Self { | ||
| name: name.to_string(), | ||
| dtype: dtype.clone(), | ||
| physical_growable, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Growable for ExtensionGrowable<'_> { | ||
| fn extend(&mut self, index: usize, start: usize, len: usize) { | ||
| self.physical_growable.extend(index, start, len); | ||
| } | ||
|
|
||
| fn add_nulls(&mut self, additional: usize) { | ||
| self.physical_growable.add_nulls(additional); | ||
| } | ||
|
|
||
| fn build(&mut self) -> DaftResult<Series> { | ||
| let physical = self.physical_growable.build()?; | ||
| let field = Arc::new(Field::new(self.name.as_str(), self.dtype.clone())); | ||
| Ok(ExtensionArray::new(field, physical).into_series()) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,3 +1,4 @@ | ||
| pub mod extension_array; | ||
| pub mod file_array; | ||
| mod fixed_size_list_array; | ||
| pub mod from; | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential panic on
to_arrow()failureto_arrow()onExtensionArrayis now fallible — it performs a field lookup (which can fail if the extension type has no Arrow representation) and potentially acast. The previous implementation was infallible (it returned the inner Arrow array directly). Usingunwrap()here will panic at runtime if the cast fails, rather than returningNonegracefully.Consider propagating the error or returning
None: