Skip to content
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

Support as_mut_any in Array trait #3655

Closed
Ted-Jiang opened this issue Feb 3, 2023 · 5 comments
Closed

Support as_mut_any in Array trait #3655

Ted-Jiang opened this issue Feb 3, 2023 · 5 comments
Labels
arrow Changes to the arrow crate enhancement Any new improvement worthy of a entry in the changelog

Comments

@Ted-Jiang
Copy link
Member

Is your feature request related to a problem or challenge? Please describe what you are trying to do.
now Array trait only support as_any

pub trait Array: std::fmt::Debug + Send + Sync {
/// Returns the array as [`Any`](std::any::Any) so that it can be
/// downcasted to a specific implementation.
///
/// # Example:
///
/// ```
/// # use std::sync::Arc;
/// # use arrow_array::{Int32Array, RecordBatch};
/// # use arrow_schema::{Schema, Field, DataType, ArrowError};
///
/// let id = Int32Array::from(vec![1, 2, 3, 4, 5]);
/// let batch = RecordBatch::try_new(
/// Arc::new(Schema::new(vec![Field::new("id", DataType::Int32, false)])),
/// vec![Arc::new(id)]
/// ).unwrap();
///
/// let int32array = batch
/// .column(0)
/// .as_any()
/// .downcast_ref::<Int32Array>()
/// .expect("Failed to downcast");
/// ```
fn as_any(&self) -> &dyn Any;
/// Returns a reference to the underlying data of this array.
fn data(&self) -> &ArrayData;
/// Returns the underlying data of this array.
fn into_data(self) -> ArrayData;
/// Returns a reference-counted pointer to the underlying data of this array.
fn data_ref(&self) -> &ArrayData {
self.data()
}

When I implementing using copy on write to evaluate col_x+1 binary_expr,
I need cast ArrayRef to PrimitiveArray with ownership and with strong reference one😩 then pass to unary_mut.

pub fn unary_mut<I, F>(
array: PrimitiveArray<I>,
op: F,
) -> std::result::Result<PrimitiveArray<I>, PrimitiveArray<I>>
where
I: ArrowPrimitiveType,
F: Fn(I::Native) -> I::Native,

So i decide using downcast_mut then use mem::take() get the array: PrimitiveArray<I>,

But there no as_any_mut , so i try to add this func 🤔.
Seems there already one in ArrayBuilder

fn as_any_mut(&mut self) -> &mut dyn Any;

Describe the solution you'd like

Describe alternatives you've considered

Additional context

@Ted-Jiang Ted-Jiang added the enhancement Any new improvement worthy of a entry in the changelog label Feb 3, 2023
@Ted-Jiang
Copy link
Member Author

@tustvold @alamb Plz help take a look 😄

@tustvold
Copy link
Contributor

tustvold commented Feb 3, 2023

Why not use https://docs.rs/arrow-array/latest/arrow_array/cast/fn.downcast_array.html, this would avoid needing to use take

@alamb
Copy link
Contributor

alamb commented Feb 3, 2023

I think this is a similar ask to #2901

There is somewhat of a workaround listed #2901 (comment)

I wonder if that would work?

@Ted-Jiang
Copy link
Member Author

seems both solution use T::from(array.data().clone()) will +1 to array.data() strong reference.

@tustvold
Copy link
Contributor

tustvold commented Feb 3, 2023

seems both solution use T::from(array.data().clone()) will +1 to array.data() strong reference.

Provided you then proceed to drop the ArrayRef that is fine? FWIW you will have the same problem with downcast_mut

@tustvold tustvold added the arrow Changes to the arrow crate label Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate enhancement Any new improvement worthy of a entry in the changelog
Projects
None yet
Development

No branches or pull requests

3 participants