Skip to content

Commit

Permalink
Add downcast_array (#2901) (#3117)
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold committed Nov 15, 2022
1 parent b0b5d8b commit 5c2801d
Showing 1 changed file with 33 additions and 0 deletions.
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())
}

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

0 comments on commit 5c2801d

Please sign in to comment.