Skip to content

Commit

Permalink
Derive clone for arrays (#3184)
Browse files Browse the repository at this point in the history
* Derive clone for arrays

* Also derive DictionaryArray

* Sidestep derive trait constraints

* Clippy
  • Loading branch information
tustvold committed Nov 25, 2022
1 parent 2460c7b commit 2c86895
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions arrow-array/src/array/boolean_array.rs
Expand Up @@ -63,6 +63,7 @@ use std::any::Any;
/// assert!(arr.is_valid(3));
/// assert_eq!(true, arr.value(3));
/// ```
#[derive(Clone)]
pub struct BooleanArray {
data: ArrayData,
/// Pointer to the value array. The lifetime of this must be <= to the value buffer
Expand Down
10 changes: 10 additions & 0 deletions arrow-array/src/array/byte_array.rs
Expand Up @@ -42,6 +42,16 @@ pub struct GenericByteArray<T: ByteArrayType> {
value_data: RawPtrBox<u8>,
}

impl<T: ByteArrayType> Clone for GenericByteArray<T> {
fn clone(&self) -> Self {
Self {
data: self.data.clone(),
value_offsets: self.value_offsets,
value_data: self.value_data,
}
}
}

impl<T: ByteArrayType> GenericByteArray<T> {
/// Data type of the array.
pub const DATA_TYPE: DataType = T::DATA_TYPE;
Expand Down
11 changes: 11 additions & 0 deletions arrow-array/src/array/dictionary_array.rs
Expand Up @@ -222,6 +222,17 @@ pub struct DictionaryArray<K: ArrowPrimitiveType> {
is_ordered: bool,
}

impl<K: ArrowPrimitiveType> Clone for DictionaryArray<K> {
fn clone(&self) -> Self {
Self {
data: self.data.clone(),
keys: self.keys.clone(),
values: self.values.clone(),
is_ordered: self.is_ordered,
}
}
}

impl<K: ArrowPrimitiveType> DictionaryArray<K> {
/// Attempt to create a new DictionaryArray with a specified keys
/// (indexes into the dictionary) and values (dictionary)
Expand Down
1 change: 1 addition & 0 deletions arrow-array/src/array/fixed_size_binary_array.rs
Expand Up @@ -47,6 +47,7 @@ use std::any::Any;
///
/// ```
///
#[derive(Clone)]
pub struct FixedSizeBinaryArray {
data: ArrayData,
value_data: RawPtrBox<u8>,
Expand Down
1 change: 1 addition & 0 deletions arrow-array/src/array/fixed_size_list_array.rs
Expand Up @@ -60,6 +60,7 @@ use std::any::Any;
///
/// For non generic lists, you may wish to consider using
/// [crate::array::FixedSizeBinaryArray]
#[derive(Clone)]
pub struct FixedSizeListArray {
data: ArrayData,
values: ArrayRef,
Expand Down
10 changes: 10 additions & 0 deletions arrow-array/src/array/list_array.rs
Expand Up @@ -68,6 +68,16 @@ pub struct GenericListArray<OffsetSize> {
value_offsets: RawPtrBox<OffsetSize>,
}

impl<OffsetSize> Clone for GenericListArray<OffsetSize> {
fn clone(&self) -> Self {
Self {
data: self.data.clone(),
values: self.values.clone(),
value_offsets: self.value_offsets,
}
}
}

impl<OffsetSize: OffsetSizeTrait> GenericListArray<OffsetSize> {
/// The data type constructor of list array.
/// The input is the schema of the child array and
Expand Down
1 change: 1 addition & 0 deletions arrow-array/src/array/map_array.rs
Expand Up @@ -28,6 +28,7 @@ use std::sync::Arc;
///
/// [MapArray] is physically a [crate::array::ListArray] that has a
/// [crate::array::StructArray] with 2 child fields.
#[derive(Clone)]
pub struct MapArray {
data: ArrayData,
values: ArrayRef,
Expand Down
1 change: 1 addition & 0 deletions arrow-array/src/array/null_array.rs
Expand Up @@ -36,6 +36,7 @@ use std::any::Any;
/// assert_eq!(array.len(), 10);
/// assert_eq!(array.null_count(), 10);
/// ```
#[derive(Clone)]
pub struct NullArray {
data: ArrayData,
}
Expand Down
9 changes: 9 additions & 0 deletions arrow-array/src/array/primitive_array.rs
Expand Up @@ -257,6 +257,15 @@ pub struct PrimitiveArray<T: ArrowPrimitiveType> {
raw_values: RawPtrBox<T::Native>,
}

impl<T: ArrowPrimitiveType> Clone for PrimitiveArray<T> {
fn clone(&self) -> Self {
Self {
data: self.data.clone(),
raw_values: self.raw_values,
}
}
}

impl<T: ArrowPrimitiveType> PrimitiveArray<T> {
/// Returns the length of this array.
#[inline]
Expand Down
1 change: 1 addition & 0 deletions arrow-array/src/array/struct_array.rs
Expand Up @@ -50,6 +50,7 @@ use std::any::Any;
/// assert_eq!(0, struct_array.null_count());
/// assert_eq!(0, struct_array.offset());
/// ```
#[derive(Clone)]
pub struct StructArray {
data: ArrayData,
pub(crate) boxed_fields: Vec<ArrayRef>,
Expand Down
1 change: 1 addition & 0 deletions arrow-array/src/array/union_array.rs
Expand Up @@ -104,6 +104,7 @@ use std::any::Any;
/// let value = array.value(2).as_any().downcast_ref::<Int32Array>().unwrap().value(0);
/// assert_eq!(34, value);
/// ```
#[derive(Clone)]
pub struct UnionArray {
data: ArrayData,
boxed_fields: Vec<ArrayRef>,
Expand Down
8 changes: 8 additions & 0 deletions arrow-array/src/raw_pointer.rs
Expand Up @@ -25,6 +25,14 @@ pub(super) struct RawPtrBox<T> {
ptr: NonNull<T>,
}

impl<T> Clone for RawPtrBox<T> {
fn clone(&self) -> Self {
Self { ptr: self.ptr }
}
}

impl<T> Copy for RawPtrBox<T> {}

impl<T> RawPtrBox<T> {
/// # Safety
/// The user must guarantee that:
Expand Down

0 comments on commit 2c86895

Please sign in to comment.