-
Notifications
You must be signed in to change notification settings - Fork 915
Experiment with Extension as a DataType #7398
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
paleolimbot
wants to merge
16
commits into
apache:main
Choose a base branch
from
paleolimbot:type-extension-maybe
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
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0ec13ae
so close to compiling
paleolimbot a7e3ec6
let arrow csv compile
paleolimbot e01cc11
don't serde Extension
paleolimbot 7ddf616
format
paleolimbot 396442a
demo scoping + opt-in import
paleolimbot 1573540
format
paleolimbot 9b3620f
plausible array
paleolimbot 3d05ba8
one kernel
paleolimbot f63828e
rat
paleolimbot 19b5976
add interleave
paleolimbot 0e212b3
nullif
paleolimbot b4a22b8
fix typo in test extension name
paleolimbot 6026ba4
take
paleolimbot d3f1a4e
new_null
paleolimbot 0259655
concat
paleolimbot 783a303
zip
paleolimbot 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,143 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use std::{any::Any, sync::Arc}; | ||
|
||
use arrow_data::ArrayData; | ||
use arrow_schema::{extension::DynExtensionType, ArrowError, DataType}; | ||
|
||
use super::{make_array, Array, ArrayRef}; | ||
|
||
/// Array type for DataType::Extension | ||
#[derive(Debug)] | ||
pub struct ExtensionArray { | ||
data_type: DataType, | ||
storage: ArrayRef, | ||
} | ||
|
||
impl ExtensionArray { | ||
/// Try to create a new ExtensionArray | ||
pub fn try_new( | ||
extension: Arc<dyn DynExtensionType + Send + Sync>, | ||
storage: ArrayRef, | ||
) -> Result<Self, ArrowError> { | ||
Ok(Self { | ||
data_type: DataType::Extension(extension), | ||
storage, | ||
}) | ||
} | ||
|
||
/// Create a new ExtensionArray | ||
pub fn new(extension: Arc<dyn DynExtensionType + Send + Sync>, storage: ArrayRef) -> Self { | ||
Self::try_new(extension, storage).unwrap() | ||
} | ||
|
||
/// Return the underlying storage array | ||
pub fn storage(&self) -> &ArrayRef { | ||
&self.storage | ||
} | ||
|
||
/// Return a new array with new storage of the same type | ||
pub fn with_storage(&self, new_storage: ArrayRef) -> Self { | ||
assert_eq!(new_storage.data_type(), new_storage.data_type()); | ||
Self { | ||
data_type: self.data_type.clone(), | ||
storage: new_storage, | ||
} | ||
} | ||
} | ||
|
||
impl From<ArrayData> for ExtensionArray { | ||
fn from(data: ArrayData) -> Self { | ||
if let DataType::Extension(extension) = data.data_type() { | ||
let storage_data = ArrayData::try_new( | ||
extension.storage_type().clone(), | ||
data.len(), | ||
data.nulls().map(|b| b.buffer()).cloned(), | ||
data.offset(), | ||
data.buffers().to_vec(), | ||
data.child_data().to_vec(), | ||
) | ||
.unwrap(); | ||
|
||
Self { | ||
data_type: data.data_type().clone(), | ||
storage: Arc::new(make_array(storage_data)) as ArrayRef, | ||
} | ||
} else { | ||
panic!("{} is not Extension", data.data_type()) | ||
} | ||
} | ||
} | ||
|
||
impl Array for ExtensionArray { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn to_data(&self) -> ArrayData { | ||
let storage_data = self.storage.to_data(); | ||
ArrayData::try_new( | ||
self.data_type.clone(), | ||
storage_data.len(), | ||
storage_data.nulls().map(|b| b.buffer()).cloned(), | ||
storage_data.offset(), | ||
storage_data.buffers().to_vec(), | ||
storage_data.child_data().to_vec(), | ||
) | ||
.unwrap() | ||
} | ||
|
||
fn into_data(self) -> ArrayData { | ||
self.to_data() | ||
} | ||
|
||
fn data_type(&self) -> &DataType { | ||
&self.data_type | ||
} | ||
|
||
fn slice(&self, offset: usize, length: usize) -> ArrayRef { | ||
Arc::new(Self { | ||
data_type: self.data_type.clone(), | ||
storage: self.storage.slice(offset, length), | ||
}) | ||
} | ||
|
||
fn len(&self) -> usize { | ||
self.storage.len() | ||
} | ||
|
||
fn is_empty(&self) -> bool { | ||
self.storage.is_empty() | ||
} | ||
|
||
fn offset(&self) -> usize { | ||
self.storage.offset() | ||
} | ||
|
||
fn nulls(&self) -> Option<&arrow_buffer::NullBuffer> { | ||
self.storage.nulls() | ||
} | ||
|
||
fn get_buffer_memory_size(&self) -> usize { | ||
self.storage.get_buffer_memory_size() | ||
} | ||
|
||
fn get_array_memory_size(&self) -> usize { | ||
self.storage.get_array_memory_size() | ||
} | ||
} |
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.
This highlights the challenge with this approach, every kernel/function must now be updated to accommodate this new extension type. Not only does this stand a very high chance of regressing functionality, but also is IMO not in the spirit of extension types.
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.
Yes, the tradeoff is taking on the burden of extensibility (so that other projects don't have to implement it separately, for example, by redefining wrapper DataTypes or Arrays). This extra burden hasn't caused issues in Arrow C++/pyarrow that I'm aware of but absolutely fair that it might here.