Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Feature dyn Array::as_any_mut + Default implementation of arrays. #951

Open
ritchie46 opened this issue Apr 17, 2022 · 2 comments
Open

Feature dyn Array::as_any_mut + Default implementation of arrays. #951

ritchie46 opened this issue Apr 17, 2022 · 2 comments

Comments

@ritchie46
Copy link
Collaborator

The new into_mut works like a charm. <3

But its quite hacky to make work with dyn Array.

We then need to use as_any() -> clone and drop the owned dyn Array to drop the ref count again.

Here an excerpt of what we do in polars:

        // Depending on the state of the underlying arrow array we
        // might be able to get a `MutablePrimitiveArray`
        //
        // This is only possible if the reference count of the array and its buffers are 1
        // So the logic below is needed to keep the reference count 1 if it is

        // First we must obtain an owned version of the array
        let arr = self.downcast_iter().next().unwrap();

        // increments 1
        let arr = arr.clone();

        // now we drop our owned ArrayRefs so that
        // decrements 1
        {
            self.chunks.clear();
        }

What I'd ideally like as an api would be:

let owned = std::mem::take(array.as_any_mut().downcast_mut::<PrimitiveArray<T>>())?;
owned.into_mut()

Then a Default implementation could be an empty array from that same type. This is similar to standard data collections in std.

Is this something we could add to the Array trait? I am willing to make a PR.

@declark1
Copy link

This would be great and was the exact issue I ran into and mentioned here. Thanks for sharing your solution, I will give this a try.

@jorgecarleitao
Copy link
Owner

I think that we have some challenges with the mutable versions since they are statically typed, while the arrays are dynamically typed.

Maybe it is time to introduce dynamically-typed mutable arrays that mimic 1-to-1 the current arrays?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants