Skip to content

Support as_mut_any in Array trait  #3655

Closed
@Ted-Jiang

Description

@Ted-Jiang

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

Activity

added
enhancementAny new improvement worthy of a entry in the changelog
on Feb 3, 2023
Ted-Jiang

Ted-Jiang commented on Feb 3, 2023

@Ted-Jiang
MemberAuthor

@tustvold @alamb Plz help take a look 😄

tustvold

tustvold commented on Feb 3, 2023

@tustvold
Contributor

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

alamb

alamb commented on Feb 3, 2023

@alamb
Contributor

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

Ted-Jiang commented on Feb 3, 2023

@Ted-Jiang
MemberAuthor

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

tustvold

tustvold commented on Feb 3, 2023

@tustvold
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrowChanges to the arrow crateenhancementAny new improvement worthy of a entry in the changelog

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @alamb@tustvold@Ted-Jiang

        Issue actions

          Support `as_mut_any` in Array trait · Issue #3655 · apache/arrow-rs