tustvold commented on code in PR #3117:
URL: https://github.com/apache/arrow-rs/pull/3117#discussion_r1022295908
##########
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:
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
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]