Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
viirya committed Nov 15, 2022
1 parent a3ad2c5 commit 4a028e6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 34 deletions.
29 changes: 4 additions & 25 deletions arrow-array/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,28 +372,6 @@ impl<'a, T: Array> Array for &'a T {
}
}

/// A kind of `Array` which can be downcasted by `downcast_array`.
pub(crate) trait SizedArray: AsDynAny + Array {}

/// A trait used to help conversion from `Arc<Self>` to `Arc<Any>`.
pub(crate) trait AsDynAny: Any {
fn as_dyn_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync + 'static>;
}

impl<T: Sized + Send + Sync + 'static> AsDynAny for T {
fn as_dyn_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync + 'static> {
self
}
}

/// Downcasts an Arc-ed sized `Array` to Arc-ed underlying arrow type.
#[allow(dead_code)]
pub(crate) fn downcast_array<T: Send + Sync + 'static>(
array: Arc<dyn SizedArray>,
) -> Option<Arc<T>> {
AsDynAny::as_dyn_any(array).downcast().ok()
}

/// A generic trait for accessing the values of an [`Array`]
///
/// # Validity
Expand Down Expand Up @@ -884,6 +862,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::cast::downcast_array;
use arrow_schema::Field;

#[test]
Expand Down Expand Up @@ -1140,10 +1119,10 @@ mod tests {
fn test_downcast_array() {
let array: Int32Array = vec![1, 2, 3].into_iter().map(Some).collect();

let boxed: Arc<dyn SizedArray> = Arc::new(array);
let array: Arc<Int32Array> = downcast_array(boxed).unwrap();
let boxed: ArrayRef = Arc::new(array);
let array: Int32Array = downcast_array(&boxed);

let expected: Int32Array = vec![1, 2, 3].into_iter().map(Some).collect();
assert_eq!(&array, &Arc::new(expected));
assert_eq!(array, expected);
}
}
15 changes: 6 additions & 9 deletions arrow-array/src/array/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// specific language governing permissions and limitations
// under the License.

use crate::array::SizedArray;
use crate::builder::{BooleanBufferBuilder, BufferBuilder, PrimitiveBuilder};
use crate::iterator::PrimitiveIter;
use crate::raw_pointer::RawPtrBox;
Expand Down Expand Up @@ -565,9 +564,6 @@ impl<T: ArrowPrimitiveType> Array for PrimitiveArray<T> {
}
}

/// Makes `PrimitiveArray<T>` can be "downcast_array".
impl<T: ArrowPrimitiveType> SizedArray for PrimitiveArray<T> {}

impl<'a, T: ArrowPrimitiveType> ArrayAccessor for &'a PrimitiveArray<T> {
type Item = T::Native;

Expand Down Expand Up @@ -1075,7 +1071,8 @@ impl<T: DecimalType + ArrowPrimitiveType> PrimitiveArray<T> {
mod tests {
use super::*;
use crate::builder::{Decimal128Builder, Decimal256Builder};
use crate::{downcast_array, BooleanArray};
use crate::cast::downcast_array;
use crate::{ArrayRef, BooleanArray};
use std::sync::Arc;

#[test]
Expand Down Expand Up @@ -1984,10 +1981,10 @@ mod tests {
fn test_into_builder() {
let array: Int32Array = vec![1, 2, 3].into_iter().map(Some).collect();

let boxed: Arc<dyn SizedArray> = Arc::new(array);
let array: Arc<Int32Array> = downcast_array(boxed).unwrap();
let boxed: ArrayRef = Arc::new(array);
let col: Int32Array = downcast_array(&boxed);
drop(boxed);

let col: Int32Array = Arc::try_unwrap(array).unwrap();
let mut builder = col.into_builder().unwrap();

builder.append_value(4);
Expand All @@ -2004,7 +2001,7 @@ mod tests {
fn test_into_builder_cloned_array() {
let array: Int32Array = vec![1, 2, 3].into_iter().map(Some).collect();

let boxed: Arc<dyn SizedArray> = Arc::new(array);
let boxed: ArrayRef = Arc::new(array);

let col: Int32Array = PrimitiveArray::<Int32Type>::from(boxed.data().clone());
let err = col.into_builder();
Expand Down

0 comments on commit 4a028e6

Please sign in to comment.