You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/11/15 03:56:06 UTC

[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3117: Add downcast_array (#2901)

tustvold commented on code in PR #3117:
URL: https://github.com/apache/arrow-rs/pull/3117#discussion_r1022291987


##########
arrow-array/src/cast.rs:
##########
@@ -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())

Review Comment:
   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



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org