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

Add downcast_array (#2901) #3117

Merged
merged 1 commit into from Nov 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions arrow-array/src/cast.rs
Expand Up @@ -19,6 +19,7 @@

use crate::array::*;
use crate::types::*;
use arrow_data::ArrayData;

/// Repeats the provided pattern based on the number of comma separated identifiers
#[doc(hidden)]
Expand Down Expand Up @@ -550,6 +551,38 @@ array_downcast_fn!(as_union_array, UnionArray);
array_downcast_fn!(as_map_array, MapArray);
array_downcast_fn!(as_decimal_array, Decimal128Array);

/// Downcasts a `dyn Array` to a concrete type
///
/// ```
/// # use arrow_array::{BooleanArray, Int32Array, RecordBatch, StringArray};
/// # use arrow_array::cast::downcast_array;
/// struct ConcreteBatch {
/// col1: Int32Array,
/// col2: BooleanArray,
/// col3: StringArray,
/// }
///
/// impl ConcreteBatch {
/// fn new(batch: &RecordBatch) -> Self {
/// Self {
/// col1: downcast_array(batch.column(0).as_ref()),
/// col2: downcast_array(batch.column(1).as_ref()),
/// col3: downcast_array(batch.column(2).as_ref()),
/// }
/// }
/// }
/// ```
///
/// # Panics
///
/// Panics if array is not of the correct data type
pub fn downcast_array<T>(array: &dyn Array) -> T
where
T: From<ArrayData>,
{
T::from(array.data().clone())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be some mechanism to avoid this clone, if/when we work this out this can be optimised. In practice, this clone should be irrelevant, as it all the underlying buffers are reference counted

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this clone make us unable to Arc::try_unwrap later?

Copy link
Contributor Author

@tustvold tustvold Nov 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provided the source Array is dropped first, and there are no other references to the underlying Buffer, e.g. as a result of slicing the array, it will work.

Edit: There isn't really a way to avoid this, even approaches using AsDynAny, etc... will have the same complication

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. Let's merge this so I can play with it in #3115.

}

#[cfg(test)]
mod tests {
use arrow_buffer::i256;
Expand Down