From 6137e161c6e8ca960c5c74378c2aa920af308d9d Mon Sep 17 00:00:00 2001 From: Jorge Leitao Date: Sun, 12 Feb 2023 19:14:02 +0100 Subject: [PATCH] Changed methods to slice arrays (#1396) --- benches/bitmap_ops.rs | 9 ++- guide/src/low_level.md | 2 +- src/array/binary/mod.rs | 43 +++++++------- src/array/boolean/mod.rs | 37 ++++++------ src/array/dictionary/mod.rs | 30 ++++------ src/array/equal/struct_.rs | 6 +- src/array/fixed_size_binary/mod.rs | 43 ++++++-------- src/array/fixed_size_list/mod.rs | 45 +++++++-------- src/array/list/mod.rs | 37 ++++++------ src/array/map/mod.rs | 34 +++++------ src/array/mod.rs | 66 ++++++++++++++++++++-- src/array/null.rs | 17 +++--- src/array/primitive/mod.rs | 53 +++++++---------- src/array/struct_/mod.rs | 41 ++++++-------- src/array/union/ffi.rs | 2 +- src/array/union/mod.rs | 31 +++++----- src/array/utf8/mod.rs | 50 ++++++++-------- src/bitmap/bitmap_ops.rs | 2 +- src/bitmap/immutable.rs | 28 +++++++-- src/buffer/immutable.rs | 33 +++++++++-- src/compute/comparison/primitive.rs | 18 +++--- src/compute/limit.rs | 2 +- src/compute/take/fixed_size_list.rs | 2 +- src/compute/take/list.rs | 2 +- src/compute/window.rs | 2 +- src/ffi/array.rs | 4 +- src/io/ipc/write/serialize.rs | 4 +- src/io/parquet/write/binary/nested.rs | 2 +- src/io/parquet/write/boolean/nested.rs | 2 +- src/io/parquet/write/dictionary.rs | 2 +- src/io/parquet/write/mod.rs | 2 +- src/io/parquet/write/primitive/nested.rs | 2 +- src/io/parquet/write/utf8/nested.rs | 2 +- src/offset.rs | 24 ++++++-- tests/it/array/binary/mod.rs | 2 +- tests/it/array/boolean/mod.rs | 2 +- tests/it/array/equal/boolean.rs | 8 +-- tests/it/array/equal/fixed_size_list.rs | 12 ++-- tests/it/array/equal/list.rs | 14 ++--- tests/it/array/equal/primitive.rs | 4 +- tests/it/array/fixed_size_binary/mod.rs | 2 +- tests/it/array/fixed_size_list/mod.rs | 2 +- tests/it/array/growable/binary.rs | 6 +- tests/it/array/growable/fixed_binary.rs | 6 +- tests/it/array/growable/fixed_size_list.rs | 2 +- tests/it/array/growable/list.rs | 4 +- tests/it/array/growable/primitive.rs | 6 +- tests/it/array/growable/struct_.rs | 10 ++-- tests/it/array/growable/union.rs | 4 +- tests/it/array/growable/utf8.rs | 6 +- tests/it/array/map/mod.rs | 2 +- tests/it/array/primitive/mod.rs | 2 +- tests/it/array/union.rs | 6 +- tests/it/array/utf8/mod.rs | 2 +- tests/it/bitmap/assign_ops.rs | 4 +- tests/it/bitmap/immutable.rs | 6 +- tests/it/bitmap/mod.rs | 22 ++++---- tests/it/bitmap/utils/slice_iterator.rs | 4 +- tests/it/bitmap/utils/zip_validity.rs | 4 +- tests/it/buffer/immutable.rs | 4 +- tests/it/compute/boolean.rs | 24 ++++---- tests/it/compute/cast.rs | 4 +- tests/it/compute/comparison.rs | 2 +- tests/it/compute/concatenate.rs | 4 +- tests/it/compute/filter.rs | 4 +- tests/it/compute/sort/lex_sort.rs | 4 +- tests/it/ffi/data.rs | 16 +++--- tests/it/io/ipc/mmap.rs | 26 ++++----- tests/it/io/ipc/write/file.rs | 4 +- tests/it/io/ndjson/read.rs | 2 +- tests/it/io/parquet/mod.rs | 2 +- 71 files changed, 480 insertions(+), 437 deletions(-) diff --git a/benches/bitmap_ops.rs b/benches/bitmap_ops.rs index f1fd06f195a..85a6db602b9 100644 --- a/benches/bitmap_ops.rs +++ b/benches/bitmap_ops.rs @@ -26,7 +26,8 @@ fn add_benchmark(c: &mut Criterion) { &format!("bitmap count zeros 85% slice 2^{log2_size}"), |b| { b.iter(|| { - let r = bitmap.clone().slice(offset, len); + let mut r = bitmap.clone(); + r.slice(offset, len); assert!(r.unset_bits() > 0); }) }, @@ -39,13 +40,15 @@ fn add_benchmark(c: &mut Criterion) { &format!("bitmap count zeros 51% slice 2^{log2_size}"), |b| { b.iter(|| { - let r = bitmap.clone().slice(offset, len); + let mut r = bitmap.clone(); + r.slice(offset, len); assert!(r.unset_bits() > 0); }) }, ); - let bitmap1 = bitmap.clone().slice(1, size - 1); + let mut bitmap1 = bitmap.clone(); + bitmap1.slice(1, size - 1); c.bench_function(&format!("bitmap not 2^{log2_size}"), |b| { b.iter(|| { let r = !&bitmap1; diff --git a/guide/src/low_level.md b/guide/src/low_level.md index db29f077d71..83fa0f08bf1 100644 --- a/guide/src/low_level.md +++ b/guide/src/low_level.md @@ -34,7 +34,7 @@ let x = vec![1u32, 2, 3]; let x: Buffer = x.into(); assert_eq!(x.as_slice(), &[1u32, 2, 3]); -let x = x.slice(1, 2); // O(1) +let x = x.sliced(1, 2); // O(1) assert_eq!(x.as_slice(), &[2, 3]); # } ``` diff --git a/src/array/binary/mod.rs b/src/array/binary/mod.rs index 0c26d6e4c5c..91b402b69d5 100644 --- a/src/array/binary/mod.rs +++ b/src/array/binary/mod.rs @@ -175,13 +175,12 @@ impl BinaryArray { self.validity.as_ref() } - /// Creates a new [`BinaryArray`] by slicing this [`BinaryArray`]. + /// Slices this [`BinaryArray`]. /// # Implementation - /// This function is `O(1)`: all data will be shared between both arrays. + /// This function is `O(1)`. /// # Panics /// iff `offset + length > self.len()`. - #[must_use] - pub fn slice(&self, offset: usize, length: usize) -> Self { + pub fn slice(&mut self, offset: usize, length: usize) { assert!( offset + length <= self.len(), "the offset of the new Buffer cannot exceed the existing length" @@ -189,27 +188,21 @@ impl BinaryArray { unsafe { self.slice_unchecked(offset, length) } } - /// Creates a new [`BinaryArray`] by slicing this [`BinaryArray`]. + /// Slices this [`BinaryArray`]. /// # Implementation - /// This function is `O(1)`: all data will be shared between both arrays. + /// This function is `O(1)`. /// # Safety /// The caller must ensure that `offset + length <= self.len()`. - #[must_use] - pub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self { - let validity = self - .validity - .clone() - .map(|bitmap| bitmap.slice_unchecked(offset, length)) - .and_then(|bitmap| (bitmap.unset_bits() > 0).then(|| bitmap)); - let offsets = self.offsets.clone().slice_unchecked(offset, length + 1); - Self { - data_type: self.data_type.clone(), - offsets, - values: self.values.clone(), - validity, - } + pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.validity.as_mut().and_then(|bitmap| { + bitmap.slice_unchecked(offset, length); + (bitmap.unset_bits() > 0).then(|| bitmap) + }); + self.offsets.slice_unchecked(offset, length + 1); } + impl_sliced!(); + /// Boxes self into a [`Box`]. pub fn boxed(self) -> Box { Box::new(self) @@ -440,12 +433,14 @@ impl Array for BinaryArray { self.validity.as_ref() } - fn slice(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice(offset, length)) + fn slice(&mut self, offset: usize, length: usize) { + self.slice(offset, length) } - unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice_unchecked(offset, length)) + + unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.slice_unchecked(offset, length) } + fn with_validity(&self, validity: Option) -> Box { Box::new(self.clone().with_validity(validity)) } diff --git a/src/array/boolean/mod.rs b/src/array/boolean/mod.rs index 01e12f83ca8..be6cfe358a0 100644 --- a/src/array/boolean/mod.rs +++ b/src/array/boolean/mod.rs @@ -143,14 +143,13 @@ impl BooleanArray { self.values.get_bit_unchecked(i) } - /// Returns a slice of this [`BooleanArray`]. + /// Slices this [`BooleanArray`]. /// # Implementation /// This operation is `O(1)` as it amounts to increase up to two ref counts. /// # Panic /// This function panics iff `offset + length > self.len()`. #[inline] - #[must_use] - pub fn slice(&self, offset: usize, length: usize) -> Self { + pub fn slice(&mut self, offset: usize, length: usize) { assert!( offset + length <= self.len(), "the offset of the new Buffer cannot exceed the existing length" @@ -158,26 +157,22 @@ impl BooleanArray { unsafe { self.slice_unchecked(offset, length) } } - /// Returns a slice of this [`BooleanArray`]. + /// Slices this [`BooleanArray`]. /// # Implementation /// This operation is `O(1)` as it amounts to increase two ref counts. /// # Safety /// The caller must ensure that `offset + length <= self.len()`. #[inline] - #[must_use] - pub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self { - let validity = self - .validity - .clone() - .map(|bitmap| bitmap.slice_unchecked(offset, length)) - .and_then(|bitmap| (bitmap.unset_bits() > 0).then(|| bitmap)); - Self { - data_type: self.data_type.clone(), - values: self.values.clone().slice_unchecked(offset, length), - validity, - } + pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.validity.as_mut().and_then(|bitmap| { + bitmap.slice_unchecked(offset, length); + (bitmap.unset_bits() > 0).then(|| bitmap) + }); + self.values.slice_unchecked(offset, length); } + impl_sliced!(); + /// Returns this [`BooleanArray`] with a new validity. /// # Panic /// This function panics iff `validity.len() != self.len()`. @@ -400,13 +395,15 @@ impl Array for BooleanArray { } #[inline] - fn slice(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice(offset, length)) + fn slice(&mut self, offset: usize, length: usize) { + self.slice(offset, length) } + #[inline] - unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice_unchecked(offset, length)) + unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.slice_unchecked(offset, length) } + fn with_validity(&self, validity: Option) -> Box { Box::new(self.clone().with_validity(validity)) } diff --git a/src/array/dictionary/mod.rs b/src/array/dictionary/mod.rs index 46023209565..1225edb1c3c 100644 --- a/src/array/dictionary/mod.rs +++ b/src/array/dictionary/mod.rs @@ -291,28 +291,22 @@ impl DictionaryArray { DataType::Dictionary(K::KEY_TYPE, Box::new(values_datatype), false) } - /// Creates a new [`DictionaryArray`] by slicing the existing [`DictionaryArray`]. + /// Slices this [`DictionaryArray`]. /// # Panics /// iff `offset + length > self.len()`. - pub fn slice(&self, offset: usize, length: usize) -> Self { - Self { - data_type: self.data_type.clone(), - keys: self.keys.clone().slice(offset, length), - values: self.values.clone(), - } + pub fn slice(&mut self, offset: usize, length: usize) { + self.keys.slice(offset, length); } - /// Creates a new [`DictionaryArray`] by slicing the existing [`DictionaryArray`]. + /// Slices this [`DictionaryArray`]. /// # Safety /// Safe iff `offset + length <= self.len()`. - pub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self { - Self { - data_type: self.data_type.clone(), - keys: self.keys.clone().slice_unchecked(offset, length), - values: self.values.clone(), - } + pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.keys.slice_unchecked(offset, length); } + impl_sliced!(); + /// Returns this [`DictionaryArray`] with a new validity. /// # Panic /// This function panics iff `validity.len() != self.len()`. @@ -437,12 +431,12 @@ impl Array for DictionaryArray { self.keys.validity() } - fn slice(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice(offset, length)) + fn slice(&mut self, offset: usize, length: usize) { + self.slice(offset, length) } - unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice_unchecked(offset, length)) + unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.slice_unchecked(offset, length) } fn with_validity(&self, validity: Option) -> Box { diff --git a/src/array/equal/struct_.rs b/src/array/equal/struct_.rs index c092d8d546f..147277ae614 100644 --- a/src/array/equal/struct_.rs +++ b/src/array/equal/struct_.rs @@ -13,7 +13,7 @@ pub(super) fn equal(lhs: &StructArray, rhs: &StructArray) -> bool { l_validity.iter().zip(r_validity.iter()).enumerate().all( |(i, (lhs_is_valid, rhs_is_valid))| { if lhs_is_valid && rhs_is_valid { - lhs.slice(i, 1) == rhs.slice(i, 1) + lhs.sliced(i, 1) == rhs.sliced(i, 1) } else { lhs_is_valid == rhs_is_valid } @@ -27,7 +27,7 @@ pub(super) fn equal(lhs: &StructArray, rhs: &StructArray) -> bool { .all(|(lhs, rhs)| { l_validity.iter().enumerate().all(|(i, lhs_is_valid)| { if lhs_is_valid { - lhs.slice(i, 1) == rhs.slice(i, 1) + lhs.sliced(i, 1) == rhs.sliced(i, 1) } else { // rhs is always valid => different false @@ -42,7 +42,7 @@ pub(super) fn equal(lhs: &StructArray, rhs: &StructArray) -> bool { .all(|(lhs, rhs)| { r_validity.iter().enumerate().all(|(i, rhs_is_valid)| { if rhs_is_valid { - lhs.slice(i, 1) == rhs.slice(i, 1) + lhs.sliced(i, 1) == rhs.sliced(i, 1) } else { // lhs is always valid => different false diff --git a/src/array/fixed_size_binary/mod.rs b/src/array/fixed_size_binary/mod.rs index 9806262db84..57fc4ddec34 100644 --- a/src/array/fixed_size_binary/mod.rs +++ b/src/array/fixed_size_binary/mod.rs @@ -97,13 +97,12 @@ impl FixedSizeBinaryArray { // must use impl FixedSizeBinaryArray { - /// Returns a slice of this [`FixedSizeBinaryArray`]. + /// Slices this [`FixedSizeBinaryArray`]. /// # Implementation - /// This operation is `O(1)` as it amounts to increase 3 ref counts. + /// This operation is `O(1)`. /// # Panics /// panics iff `offset + length > self.len()` - #[must_use] - pub fn slice(&self, offset: usize, length: usize) -> Self { + pub fn slice(&mut self, offset: usize, length: usize) { assert!( offset + length <= self.len(), "the offset of the new Buffer cannot exceed the existing length" @@ -111,30 +110,22 @@ impl FixedSizeBinaryArray { unsafe { self.slice_unchecked(offset, length) } } - /// Returns a slice of this [`FixedSizeBinaryArray`]. + /// Slices this [`FixedSizeBinaryArray`]. /// # Implementation - /// This operation is `O(1)` as it amounts to increase 3 ref counts. + /// This operation is `O(1)`. /// # Safety /// The caller must ensure that `offset + length <= self.len()`. - #[must_use] - pub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self { - let validity = self - .validity - .clone() - .map(|bitmap| bitmap.slice_unchecked(offset, length)) - .and_then(|bitmap| (bitmap.unset_bits() > 0).then(|| bitmap)); - let values = self - .values - .clone() + pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.validity.as_mut().and_then(|bitmap| { + bitmap.slice_unchecked(offset, length); + (bitmap.unset_bits() > 0).then(|| bitmap) + }); + self.values .slice_unchecked(offset * self.size, length * self.size); - Self { - data_type: self.data_type.clone(), - size: self.size, - values, - validity, - } } + impl_sliced!(); + /// Returns this [`FixedSizeBinaryArray`] with a new validity. /// # Panic /// This function panics iff `validity.len() != self.len()`. @@ -267,12 +258,12 @@ impl Array for FixedSizeBinaryArray { self.validity.as_ref() } - fn slice(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice(offset, length)) + fn slice(&mut self, offset: usize, length: usize) { + self.slice(offset, length) } - unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice_unchecked(offset, length)) + unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.slice_unchecked(offset, length) } fn with_validity(&self, validity: Option) -> Box { diff --git a/src/array/fixed_size_list/mod.rs b/src/array/fixed_size_list/mod.rs index 5b8df559ee7..dce8c65179a 100644 --- a/src/array/fixed_size_list/mod.rs +++ b/src/array/fixed_size_list/mod.rs @@ -111,13 +111,12 @@ impl FixedSizeListArray { // must use impl FixedSizeListArray { - /// Returns a slice of this [`FixedSizeListArray`]. + /// Slices this [`FixedSizeListArray`]. /// # Implementation /// This operation is `O(1)`. /// # Panics /// panics iff `offset + length > self.len()` - #[must_use] - pub fn slice(&self, offset: usize, length: usize) -> Self { + pub fn slice(&mut self, offset: usize, length: usize) { assert!( offset + length <= self.len(), "the offset of the new Buffer cannot exceed the existing length" @@ -125,30 +124,22 @@ impl FixedSizeListArray { unsafe { self.slice_unchecked(offset, length) } } - /// Returns a slice of this [`FixedSizeListArray`]. + /// Slices this [`FixedSizeListArray`]. /// # Implementation /// This operation is `O(1)`. /// # Safety /// The caller must ensure that `offset + length <= self.len()`. - #[must_use] - pub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self { - let validity = self - .validity - .clone() - .map(|bitmap| bitmap.slice_unchecked(offset, length)) - .and_then(|bitmap| (bitmap.unset_bits() > 0).then(|| bitmap)); - let values = self - .values - .clone() + pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.validity.as_mut().and_then(|bitmap| { + bitmap.slice_unchecked(offset, length); + (bitmap.unset_bits() > 0).then(|| bitmap) + }); + self.values .slice_unchecked(offset * self.size, length * self.size); - Self { - data_type: self.data_type.clone(), - size: self.size, - values, - validity, - } } + impl_sliced!(); + /// Returns this [`FixedSizeListArray`] with a new validity. /// # Panic /// This function panics iff `validity.len() != self.len()`. @@ -193,7 +184,7 @@ impl FixedSizeListArray { /// panics iff `i >= self.len()` #[inline] pub fn value(&self, i: usize) -> Box { - self.values.slice(i * self.size, self.size) + self.values.sliced(i * self.size, self.size) } /// Returns the `Vec` at position `i`. @@ -201,7 +192,7 @@ impl FixedSizeListArray { /// Caller must ensure that `i < self.len()` #[inline] pub unsafe fn value_unchecked(&self, i: usize) -> Box { - self.values.slice_unchecked(i * self.size, self.size) + self.values.sliced_unchecked(i * self.size, self.size) } } @@ -256,12 +247,14 @@ impl Array for FixedSizeListArray { self.validity.as_ref() } - fn slice(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice(offset, length)) + fn slice(&mut self, offset: usize, length: usize) { + self.slice(offset, length) } - unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice_unchecked(offset, length)) + + unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.slice_unchecked(offset, length) } + fn with_validity(&self, validity: Option) -> Box { Box::new(self.clone().with_validity(validity)) } diff --git a/src/array/list/mod.rs b/src/array/list/mod.rs index 9cb1157978e..98361ef26d8 100644 --- a/src/array/list/mod.rs +++ b/src/array/list/mod.rs @@ -117,10 +117,10 @@ impl ListArray { } impl ListArray { - /// Returns a slice of this [`ListArray`]. + /// Slices this [`ListArray`]. /// # Panics /// panics iff `offset + length >= self.len()` - pub fn slice(&self, offset: usize, length: usize) -> Self { + pub fn slice(&mut self, offset: usize, length: usize) { assert!( offset + length <= self.len(), "the offset of the new Buffer cannot exceed the existing length" @@ -128,24 +128,19 @@ impl ListArray { unsafe { self.slice_unchecked(offset, length) } } - /// Returns a slice of this [`ListArray`]. + /// Slices this [`ListArray`]. /// # Safety /// The caller must ensure that `offset + length < self.len()`. - pub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self { - let validity = self - .validity - .clone() - .map(|bitmap| bitmap.slice_unchecked(offset, length)) - .and_then(|bitmap| (bitmap.unset_bits() > 0).then(|| bitmap)); - let offsets = self.offsets.clone().slice_unchecked(offset, length + 1); - Self { - data_type: self.data_type.clone(), - offsets, - values: self.values.clone(), - validity, - } + pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.validity.as_mut().and_then(|bitmap| { + bitmap.slice_unchecked(offset, length); + (bitmap.unset_bits() > 0).then(|| bitmap) + }); + self.offsets.slice_unchecked(offset, length + 1); } + impl_sliced!(); + /// Returns this [`ListArray`] with a new validity. /// # Panic /// This function panics iff `validity.len() != self.len()`. @@ -194,7 +189,7 @@ impl ListArray { let length = end - start; // safety: the invariant of the struct - self.values.slice_unchecked(start, length) + self.values.sliced_unchecked(start, length) } /// The optional validity. @@ -285,12 +280,12 @@ impl Array for ListArray { self.validity.as_ref() } - fn slice(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice(offset, length)) + fn slice(&mut self, offset: usize, length: usize) { + self.slice(offset, length) } - unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice_unchecked(offset, length)) + unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.slice_unchecked(offset, length) } fn with_validity(&self, validity: Option) -> Box { diff --git a/src/array/map/mod.rs b/src/array/map/mod.rs index 92e4ef90315..1176e54e965 100644 --- a/src/array/map/mod.rs +++ b/src/array/map/mod.rs @@ -139,7 +139,7 @@ impl MapArray { /// Returns a slice of this [`MapArray`]. /// # Panics /// panics iff `offset + length >= self.len()` - pub fn slice(&self, offset: usize, length: usize) -> Self { + pub fn slice(&mut self, offset: usize, length: usize) { assert!( offset + length <= self.len(), "the offset of the new Buffer cannot exceed the existing length" @@ -150,21 +150,17 @@ impl MapArray { /// Returns a slice of this [`MapArray`]. /// # Safety /// The caller must ensure that `offset + length < self.len()`. - pub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self { - let offsets = self.offsets.clone().slice_unchecked(offset, length + 1); - let validity = self - .validity - .clone() - .map(|bitmap| bitmap.slice_unchecked(offset, length)) - .and_then(|bitmap| (bitmap.unset_bits() > 0).then(|| bitmap)); - Self { - data_type: self.data_type.clone(), - offsets, - field: self.field.clone(), - validity, - } + #[inline] + pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.validity.as_mut().and_then(|bitmap| { + bitmap.slice_unchecked(offset, length); + (bitmap.unset_bits() > 0).then(|| bitmap) + }); + self.offsets.slice_unchecked(offset, length + 1); } + impl_sliced!(); + pub(crate) fn try_get_field(data_type: &DataType) -> Result<&Field, Error> { if let DataType::Map(field, _) = data_type.to_logical_type() { Ok(field.as_ref()) @@ -217,7 +213,7 @@ impl MapArray { let length = end - start; // soundness: the invariant of the struct - self.field.slice_unchecked(start, length) + self.field.sliced_unchecked(start, length) } } @@ -247,12 +243,12 @@ impl Array for MapArray { self.validity.as_ref() } - fn slice(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice(offset, length)) + fn slice(&mut self, offset: usize, length: usize) { + self.slice(offset, length) } - unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice_unchecked(offset, length)) + unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.slice_unchecked(offset, length) } fn with_validity(&self, validity: Option) -> Box { diff --git a/src/array/mod.rs b/src/array/mod.rs index 5aa1dd1eb64..4ce3b56e57f 100644 --- a/src/array/mod.rs +++ b/src/array/mod.rs @@ -87,21 +87,44 @@ pub trait Array: Send + Sync + dyn_clone::DynClone + 'static { !self.is_null(i) } - /// Slices the [`Array`], returning a new `Box`. + /// Slices this [`Array`]. /// # Implementation - /// This operation is `O(1)` over `len`, as it amounts to increase two ref counts - /// and moving the struct to the heap. + /// This operation is `O(1)` over `len`. /// # Panic /// This function panics iff `offset + length > self.len()`. - fn slice(&self, offset: usize, length: usize) -> Box; + fn slice(&mut self, offset: usize, length: usize); - /// Slices the [`Array`], returning a new `Box`. + /// Slices the [`Array`]. + /// # Implementation + /// This operation is `O(1)`. + /// # Safety + /// The caller must ensure that `offset + length <= self.len()` + unsafe fn slice_unchecked(&mut self, offset: usize, length: usize); + + /// Returns a slice of this [`Array`]. + /// # Implementation + /// This operation is `O(1)` over `len`. + /// # Panic + /// This function panics iff `offset + length > self.len()`. + #[must_use] + fn sliced(&self, offset: usize, length: usize) -> Box { + let mut new = self.to_boxed(); + new.slice(offset, length); + new + } + + /// Returns a slice of this [`Array`]. /// # Implementation /// This operation is `O(1)` over `len`, as it amounts to increase two ref counts /// and moving the struct to the heap. /// # Safety /// The caller must ensure that `offset + length <= self.len()` - unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Box; + #[must_use] + unsafe fn sliced_unchecked(&self, offset: usize, length: usize) -> Box { + let mut new = self.to_boxed(); + new.slice_unchecked(offset, length); + new + } /// Clones this [`Array`] with a new new assigned bitmap. /// # Panic @@ -373,6 +396,37 @@ macro_rules! clone_dyn { }}; } +macro_rules! impl_sliced { + () => { + /// Returns this array sliced. + /// # Implementation + /// This function is `O(1)`. + /// # Panics + /// iff `offset + length > self.len()`. + #[inline] + #[must_use] + pub fn sliced(self, offset: usize, length: usize) -> Self { + assert!( + offset + length <= self.len(), + "the offset of the new Buffer cannot exceed the existing length" + ); + unsafe { self.sliced_unchecked(offset, length) } + } + + /// Returns this array sliced. + /// # Implementation + /// This function is `O(1)`. + /// # Safety + /// The caller must ensure that `offset + length <= self.len()`. + #[inline] + #[must_use] + pub fn sliced_unchecked(mut self, offset: usize, length: usize) -> Self { + unsafe { self.slice_unchecked(offset, length) }; + self + } + }; +} + /// Clones a dynamic [`Array`]. /// # Implementation /// This operation is `O(1)` over `len`, as it amounts to increase two ref counts diff --git a/src/array/null.rs b/src/array/null.rs index 75bd6b6dcbe..45d956073c5 100644 --- a/src/array/null.rs +++ b/src/array/null.rs @@ -60,11 +60,8 @@ impl NullArray { impl NullArray { /// Returns a slice of the [`NullArray`]. - pub fn slice(&self, _offset: usize, length: usize) -> Self { - Self { - data_type: self.data_type.clone(), - length, - } + pub fn slice(&mut self, _offset: usize, length: usize) { + self.length = length; } #[inline] @@ -91,19 +88,19 @@ impl Array for NullArray { #[inline] fn data_type(&self) -> &DataType { - &DataType::Null + &self.data_type } fn validity(&self) -> Option<&Bitmap> { None } - fn slice(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice(offset, length)) + fn slice(&mut self, offset: usize, length: usize) { + self.slice(offset, length) } - unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice(offset, length)) + unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.slice(offset, length) } fn with_validity(&self, _: Option) -> Box { diff --git a/src/array/primitive/mod.rs b/src/array/primitive/mod.rs index d5d96df6422..d855b5ef720 100644 --- a/src/array/primitive/mod.rs +++ b/src/array/primitive/mod.rs @@ -196,24 +196,11 @@ impl PrimitiveArray { *self.values.get_unchecked(i) } - /// Returns a clone of this [`PrimitiveArray`] sliced by an offset and length. + /// Slices this [`PrimitiveArray`] by an offset and length. /// # Implementation - /// This operation is `O(1)` as it amounts to increase two ref counts. - /// # Examples - /// ``` - /// use arrow2::array::PrimitiveArray; - /// - /// let array = PrimitiveArray::from_vec(vec![1, 2, 3]); - /// assert_eq!(format!("{:?}", array), "Int32[1, 2, 3]"); - /// let sliced = array.slice(1, 1); - /// assert_eq!(format!("{:?}", sliced), "Int32[2]"); - /// // note: `sliced` and `array` share the same memory region. - /// ``` - /// # Panic - /// This function panics iff `offset + length > self.len()`. + /// This operation is `O(1)`. #[inline] - #[must_use] - pub fn slice(&self, offset: usize, length: usize) -> Self { + pub fn slice(&mut self, offset: usize, length: usize) { assert!( offset + length <= self.len(), "offset + length may not exceed length of array" @@ -221,26 +208,22 @@ impl PrimitiveArray { unsafe { self.slice_unchecked(offset, length) } } - /// Returns a clone of this [`PrimitiveArray`] sliced by an offset and length. + /// Slices this [`PrimitiveArray`] by an offset and length. /// # Implementation - /// This operation is `O(1)` as it amounts to increase two ref counts. + /// This operation is `O(1)`. /// # Safety /// The caller must ensure that `offset + length <= self.len()`. #[inline] - #[must_use] - pub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self { - let validity = self - .validity - .clone() - .map(|bitmap| bitmap.slice_unchecked(offset, length)) - .and_then(|bitmap| (bitmap.unset_bits() > 0).then(|| bitmap)); - Self { - data_type: self.data_type.clone(), - values: self.values.clone().slice_unchecked(offset, length), - validity, - } + pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.validity.as_mut().and_then(|bitmap| { + bitmap.slice_unchecked(offset, length); + (bitmap.unset_bits() > 0).then(|| bitmap) + }); + self.values.slice_unchecked(offset, length); } + impl_sliced!(); + /// Returns this [`PrimitiveArray`] with a new validity. /// # Panics /// This function panics iff `validity.len() != self.len()`. @@ -458,12 +441,14 @@ impl Array for PrimitiveArray { self.validity.as_ref() } - fn slice(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice(offset, length)) + fn slice(&mut self, offset: usize, length: usize) { + self.slice(offset, length); } - unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice_unchecked(offset, length)) + + unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.slice_unchecked(offset, length); } + fn with_validity(&self, validity: Option) -> Box { Box::new(self.clone().with_validity(validity)) } diff --git a/src/array/struct_/mod.rs b/src/array/struct_/mod.rs index a8848bb5578..1e2de673b32 100644 --- a/src/array/struct_/mod.rs +++ b/src/array/struct_/mod.rs @@ -165,13 +165,12 @@ impl StructArray { (fields, values, validity) } - /// Creates a new [`StructArray`] that is a slice of `self`. + /// Slices this [`StructArray`]. /// # Panics /// * `offset + length` must be smaller than `self.len()`. /// # Implementation /// This operation is `O(F)` where `F` is the number of fields. - #[must_use] - pub fn slice(&self, offset: usize, length: usize) -> Self { + pub fn slice(&mut self, offset: usize, length: usize) { assert!( offset + length <= self.len(), "offset + length may not exceed length of array" @@ -179,29 +178,23 @@ impl StructArray { unsafe { self.slice_unchecked(offset, length) } } - /// Creates a new [`StructArray`] that is a slice of `self`. + /// Slices this [`StructArray`]. /// # Implementation /// This operation is `O(F)` where `F` is the number of fields. /// # Safety /// The caller must ensure that `offset + length <= self.len()`. - #[must_use] - pub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self { - let validity = self - .validity - .clone() - .map(|bitmap| bitmap.slice_unchecked(offset, length)) - .and_then(|bitmap| (bitmap.unset_bits() > 0).then(|| bitmap)); - Self { - data_type: self.data_type.clone(), - values: self - .values - .iter() - .map(|x| x.slice_unchecked(offset, length)) - .collect(), - validity, - } + pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.validity.as_mut().and_then(|bitmap| { + bitmap.slice_unchecked(offset, length); + (bitmap.unset_bits() > 0).then(|| bitmap) + }); + self.values + .iter_mut() + .for_each(|x| x.slice_unchecked(offset, length)); } + impl_sliced!(); + /// Returns this [`StructArray`] with a new validity. /// # Panics /// This function panics iff `validity.len() != self.len()`. @@ -299,12 +292,12 @@ impl Array for StructArray { self.validity.as_ref() } - fn slice(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice(offset, length)) + fn slice(&mut self, offset: usize, length: usize) { + self.slice(offset, length) } - unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice_unchecked(offset, length)) + unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.slice_unchecked(offset, length) } fn with_validity(&self, validity: Option) -> Box { diff --git a/src/array/union/ffi.rs b/src/array/union/ffi.rs index fd12f15b23c..89cee93e4d3 100644 --- a/src/array/union/ffi.rs +++ b/src/array/union/ffi.rs @@ -50,7 +50,7 @@ impl FromFfi for UnionArray { .collect::>>>()?; if offset > 0 { - types = types.slice(offset, length); + types.slice(offset, length); }; Self::try_new(data_type, types, fields, offsets) diff --git a/src/array/union/mod.rs b/src/array/union/mod.rs index e767aac2b21..79ce6a3e3e2 100644 --- a/src/array/union/mod.rs +++ b/src/array/union/mod.rs @@ -238,7 +238,7 @@ impl UnionArray { /// # Panic /// This function panics iff `offset + length >= self.len()`. #[inline] - pub fn slice(&self, offset: usize, length: usize) -> Self { + pub fn slice(&mut self, offset: usize, length: usize) { assert!( offset + length <= self.len(), "the offset of the new array cannot exceed the existing length" @@ -252,20 +252,17 @@ impl UnionArray { /// # Safety /// The caller must ensure that `offset + length <= self.len()`. #[inline] - pub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self { + pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { debug_assert!(offset + length <= self.len()); - Self { - data_type: self.data_type.clone(), - fields: self.fields.clone(), - map: self.map, - types: self.types.clone().slice_unchecked(offset, length), - offsets: self - .offsets - .clone() - .map(|offsets| offsets.slice_unchecked(offset, length)), - offset: self.offset + offset, + + self.types.slice_unchecked(offset, length); + if let Some(offsets) = self.offsets.as_mut() { + offsets.slice_unchecked(offset, length) } + self.offset += offset; } + + impl_sliced!(); } impl UnionArray { @@ -368,12 +365,14 @@ impl Array for UnionArray { None } - fn slice(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice(offset, length)) + fn slice(&mut self, offset: usize, length: usize) { + self.slice(offset, length) } - unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice_unchecked(offset, length)) + + unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.slice_unchecked(offset, length) } + fn with_validity(&self, _: Option) -> Box { panic!("cannot set validity of a union array") } diff --git a/src/array/utf8/mod.rs b/src/array/utf8/mod.rs index 4ed67f88430..7e1afdf5a76 100644 --- a/src/array/utf8/mod.rs +++ b/src/array/utf8/mod.rs @@ -195,42 +195,34 @@ impl Utf8Array { self.validity.as_ref() } - /// Returns a slice of this [`Utf8Array`]. + /// Slices this [`Utf8Array`]. /// # Implementation - /// This operation is `O(1)` as it amounts to essentially increase two ref counts. - /// # Panic - /// This function panics iff `offset + length >= self.len()`. - #[must_use] - pub fn slice(&self, offset: usize, length: usize) -> Self { + /// This function is `O(1)`. + /// # Panics + /// iff `offset + length > self.len()`. + pub fn slice(&mut self, offset: usize, length: usize) { assert!( offset + length <= self.len(), - "the offset of the new Buffer cannot exceed the existing length" + "the offset of the new array cannot exceed the arrays' length" ); unsafe { self.slice_unchecked(offset, length) } } - /// Returns a slice of this [`Utf8Array`]. + /// Slices this [`Utf8Array`]. /// # Implementation - /// This operation is `O(1)` as it amounts to essentially increase two ref counts. + /// This function is `O(1)` /// # Safety /// The caller must ensure that `offset + length <= self.len()`. - #[must_use] - pub unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Self { - let validity = self - .validity - .clone() - .map(|bitmap| bitmap.slice_unchecked(offset, length)) - .and_then(|bitmap| (bitmap.unset_bits() > 0).then(|| bitmap)); - // + 1: `length == 0` implies that we take the first offset. - let offsets = self.offsets.clone().slice_unchecked(offset, length + 1); - Self { - data_type: self.data_type.clone(), - offsets, - values: self.values.clone(), - validity, - } + pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.validity.as_mut().and_then(|bitmap| { + bitmap.slice_unchecked(offset, length); + (bitmap.unset_bits() > 0).then(|| bitmap) + }); + self.offsets.slice_unchecked(offset, length + 1); } + impl_sliced!(); + /// Boxes self into a [`Box`]. pub fn boxed(self) -> Box { Box::new(self) @@ -553,12 +545,14 @@ impl Array for Utf8Array { self.validity.as_ref() } - fn slice(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice(offset, length)) + fn slice(&mut self, offset: usize, length: usize) { + self.slice(offset, length) } - unsafe fn slice_unchecked(&self, offset: usize, length: usize) -> Box { - Box::new(self.slice_unchecked(offset, length)) + + unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.slice_unchecked(offset, length) } + fn with_validity(&self, validity: Option) -> Box { Box::new(self.clone().with_validity(validity)) } diff --git a/src/bitmap/bitmap_ops.rs b/src/bitmap/bitmap_ops.rs index 46a525a049d..99e256328cb 100644 --- a/src/bitmap/bitmap_ops.rs +++ b/src/bitmap/bitmap_ops.rs @@ -160,7 +160,7 @@ pub(crate) fn align(bitmap: &Bitmap, new_offset: usize) -> Bitmap { .chain(bitmap.iter()) .collect(); - bitmap.slice(new_offset, length) + bitmap.sliced(new_offset, length) } #[inline] diff --git a/src/bitmap/immutable.rs b/src/bitmap/immutable.rs index 9088f004f60..4e3fbaa26a8 100644 --- a/src/bitmap/immutable.rs +++ b/src/bitmap/immutable.rs @@ -35,7 +35,8 @@ use super::{ /// /// // slicing is 'O(1)' (data is shared) /// let bitmap = Bitmap::try_new(vec![0b00001101], 5).unwrap(); -/// let sliced = bitmap.slice(1, 4); +/// let mut sliced = bitmap.clone(); +/// sliced.slice(1, 4); /// assert_eq!(sliced.as_slice(), ([0b00001101u8].as_ref(), 1, 4)); // 1 here is the offset: /// assert_eq!(format!("{:?}", sliced), "[0b___0110_]".to_string()); /// // when sliced (or cloned), it is no longer possible to `into_mut`. @@ -171,8 +172,7 @@ impl Bitmap { /// Panics iff `offset + length > self.length`, i.e. if the offset and `length` /// exceeds the allocated capacity of `self`. #[inline] - #[must_use] - pub fn slice(self, offset: usize, length: usize) -> Self { + pub fn slice(&mut self, offset: usize, length: usize) { assert!(offset + length <= self.length); unsafe { self.slice_unchecked(offset, length) } } @@ -181,7 +181,7 @@ impl Bitmap { /// # Safety /// The caller must ensure that `self.offset + offset + length <= self.len()` #[inline] - pub unsafe fn slice_unchecked(mut self, offset: usize, length: usize) -> Self { + pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { // count the smallest chunk if length < self.length / 2 { // count the null values in the slice @@ -195,6 +195,26 @@ impl Bitmap { } self.offset += offset; self.length = length; + } + + /// Slices `self`, offsetting by `offset` and truncating up to `length` bits. + /// # Panic + /// Panics iff `offset + length > self.length`, i.e. if the offset and `length` + /// exceeds the allocated capacity of `self`. + #[inline] + #[must_use] + pub fn sliced(self, offset: usize, length: usize) -> Self { + assert!(offset + length <= self.length); + unsafe { self.sliced_unchecked(offset, length) } + } + + /// Slices `self`, offseting by `offset` and truncating up to `length` bits. + /// # Safety + /// The caller must ensure that `self.offset + offset + length <= self.len()` + #[inline] + #[must_use] + pub unsafe fn sliced_unchecked(mut self, offset: usize, length: usize) -> Self { + self.slice_unchecked(offset, length); self } diff --git a/src/buffer/immutable.rs b/src/buffer/immutable.rs index 2a6fa2de72e..5b3422012fd 100644 --- a/src/buffer/immutable.rs +++ b/src/buffer/immutable.rs @@ -26,8 +26,9 @@ use super::IntoIter; /// /// // cloning and slicing is `O(1)` (data is shared) /// let mut buffer: Buffer = vec![1, 2, 3].into(); -/// let slice = buffer.clone().slice(1, 1); -/// assert_eq!(slice.as_ref(), [2].as_ref()); +/// let mut sliced = buffer.clone(); +/// sliced.slice(1, 1); +/// assert_eq!(sliced.as_ref(), [2].as_ref()); /// // but cloning forbids getting mut since `slice` and `buffer` now share data /// assert_eq!(buffer.get_mut(), None); /// ``` @@ -121,7 +122,20 @@ impl Buffer { /// # Panics /// Panics iff `offset` is larger than `len`. #[inline] - pub fn slice(self, offset: usize, length: usize) -> Self { + pub fn sliced(self, offset: usize, length: usize) -> Self { + assert!( + offset + length <= self.len(), + "the offset of the new Buffer cannot exceed the existing length" + ); + // Safety: we just checked bounds + unsafe { self.sliced_unchecked(offset, length) } + } + + /// Slices this buffer starting at `offset`. + /// # Panics + /// Panics iff `offset` is larger than `len`. + #[inline] + pub fn slice(&mut self, offset: usize, length: usize) { assert!( offset + length <= self.len(), "the offset of the new Buffer cannot exceed the existing length" @@ -135,10 +149,19 @@ impl Buffer { /// # Safety /// The caller must ensure `offset + length <= self.len()` #[inline] - pub unsafe fn slice_unchecked(mut self, offset: usize, length: usize) -> Self { + #[must_use] + pub unsafe fn sliced_unchecked(mut self, offset: usize, length: usize) -> Self { + self.slice_unchecked(offset, length); + self + } + + /// Slices this buffer starting at `offset`. + /// # Safety + /// The caller must ensure `offset + length <= self.len()` + #[inline] + pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { self.offset += offset; self.length = length; - self } /// Returns a pointer to the start of this buffer. diff --git a/src/compute/comparison/primitive.rs b/src/compute/comparison/primitive.rs index e0df8b12186..0142ec7ce33 100644 --- a/src/compute/comparison/primitive.rs +++ b/src/compute/comparison/primitive.rs @@ -332,9 +332,9 @@ mod tests { #[test] fn test_primitive_array_eq_with_slice() { let a = Int64Array::from_slice([6, 7, 8, 8, 10]); - let b = Int64Array::from_slice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); - let c = b.slice(5, 5); - let d = eq(&c, &a); + let mut b = Int64Array::from_slice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + b.slice(5, 5); + let d = eq(&b, &a); assert_eq!(d, BooleanArray::from_slice([true, true, true, false, true])); } @@ -560,10 +560,10 @@ mod tests { #[test] fn test_primitive_array_compare_slice() { - let a = (0..100).map(Some).collect::>(); - let a = a.slice(50, 50); - let b = (100..200).map(Some).collect::>(); - let b = b.slice(50, 50); + let mut a = (0..100).map(Some).collect::>(); + a.slice(50, 50); + let mut b = (100..200).map(Some).collect::>(); + b.slice(50, 50); let actual = lt(&a, &b); let expected: BooleanArray = (0..50).map(|_| Some(true)).collect(); assert_eq!(expected, actual); @@ -571,8 +571,8 @@ mod tests { #[test] fn test_primitive_array_compare_scalar_slice() { - let a = (0..100).map(Some).collect::>(); - let a = a.slice(50, 50); + let mut a = (0..100).map(Some).collect::>(); + a.slice(50, 50); let actual = lt_scalar(&a, 200); let expected: BooleanArray = (0..50).map(|_| Some(true)).collect(); assert_eq!(expected, actual); diff --git a/src/compute/limit.rs b/src/compute/limit.rs index d9f88c221c7..c0e74b473dd 100644 --- a/src/compute/limit.rs +++ b/src/compute/limit.rs @@ -10,5 +10,5 @@ use crate::array::Array; /// * it slices from offset 0 pub fn limit(array: &dyn Array, num_elements: usize) -> Box { let lim = num_elements.min(array.len()); - array.slice(0, lim) + array.sliced(0, lim) } diff --git a/src/compute/take/fixed_size_list.rs b/src/compute/take/fixed_size_list.rs index 31fc04d65e9..6d23084e995 100644 --- a/src/compute/take/fixed_size_list.rs +++ b/src/compute/take/fixed_size_list.rs @@ -32,7 +32,7 @@ pub fn take( .iter() .map(|index| { let index = index.to_usize(); - let slice = values.slice(index, 1); + let slice = values.clone().sliced(index, 1); capacity += slice.len(); slice }) diff --git a/src/compute/take/list.rs b/src/compute/take/list.rs index 6abc1d10155..b2f7692ecd9 100644 --- a/src/compute/take/list.rs +++ b/src/compute/take/list.rs @@ -34,7 +34,7 @@ pub fn take( .iter() .map(|index| { let index = index.to_usize(); - let slice = values.slice(index, 1); + let slice = values.clone().sliced(index, 1); capacity += slice.len(); slice }) diff --git a/src/compute/window.rs b/src/compute/window.rs index fb999318e60..b9200fe3a37 100644 --- a/src/compute/window.rs +++ b/src/compute/window.rs @@ -49,7 +49,7 @@ pub fn shift(array: &dyn Array, offset: i64) -> Result> { // Compute slice let slice_offset = clamp(-offset, 0, array.len() as i64) as usize; let length = array.len() - abs(offset) as usize; - let slice = array.slice(slice_offset, length); + let slice = array.sliced(slice_offset, length); // Generate array with remaining `null` items let nulls = abs(offset) as usize; diff --git a/src/ffi/array.rs b/src/ffi/array.rs index b50fdaa7259..d1bc82adfc0 100644 --- a/src/ffi/array.rs +++ b/src/ffi/array.rs @@ -239,7 +239,7 @@ unsafe fn create_buffer( let offset = buffer_offset(array, data_type, index); let bytes = Bytes::from_foreign(ptr, len, owner); - Ok(Buffer::from_bytes(bytes).slice(offset, len - offset)) + Ok(Buffer::from_bytes(bytes).sliced(offset, len - offset)) } /// returns the buffer `i` of `array` interpreted as a [`Bitmap`]. @@ -260,7 +260,7 @@ unsafe fn create_bitmap( let bytes_len = bytes_for(offset + len); let bytes = Bytes::from_foreign(ptr, bytes_len, owner); - Ok(Bitmap::from_bytes(bytes, offset + len).slice(offset, len)) + Ok(Bitmap::from_bytes(bytes, offset + len).sliced(offset, len)) } fn buffer_offset(array: &ArrowArray, data_type: &DataType, i: usize) -> usize { diff --git a/src/io/ipc/write/serialize.rs b/src/io/ipc/write/serialize.rs index 03e716ce83a..0e9aa38ab7d 100644 --- a/src/io/ipc/write/serialize.rs +++ b/src/io/ipc/write/serialize.rs @@ -224,7 +224,7 @@ fn write_list( write( array .values() - .slice(first.to_usize(), last.to_usize() - first.to_usize()) + .sliced(first.to_usize(), last.to_usize() - first.to_usize()) .as_ref(), buffers, arrow_data, @@ -352,7 +352,7 @@ fn write_map( write( array .field() - .slice(first as usize, last as usize - first as usize) + .sliced(first as usize, last as usize - first as usize) .as_ref(), buffers, arrow_data, diff --git a/src/io/parquet/write/binary/nested.rs b/src/io/parquet/write/binary/nested.rs index 28cdde53d7f..1e9e45517eb 100644 --- a/src/io/parquet/write/binary/nested.rs +++ b/src/io/parquet/write/binary/nested.rs @@ -28,7 +28,7 @@ where let (start, len) = slice_nested_leaf(nested); let mut nested = nested.to_vec(); - let array = array.slice(start, len); + let array = array.clone().sliced(start, len); if let Some(Nested::Primitive(_, _, c)) = nested.last_mut() { *c = len; } else { diff --git a/src/io/parquet/write/boolean/nested.rs b/src/io/parquet/write/boolean/nested.rs index 0e6ce9f5718..d86c9b64567 100644 --- a/src/io/parquet/write/boolean/nested.rs +++ b/src/io/parquet/write/boolean/nested.rs @@ -24,7 +24,7 @@ pub fn array_to_page( let (start, len) = slice_nested_leaf(nested); let mut nested = nested.to_vec(); - let array = array.slice(start, len); + let array = array.clone().sliced(start, len); if let Some(Nested::Primitive(_, _, c)) = nested.last_mut() { *c = len; } else { diff --git a/src/io/parquet/write/dictionary.rs b/src/io/parquet/write/dictionary.rs index 8cbe6c766e6..cd03b8a51ea 100644 --- a/src/io/parquet/write/dictionary.rs +++ b/src/io/parquet/write/dictionary.rs @@ -115,7 +115,7 @@ fn serialize_keys( let (start, len) = slice_nested_leaf(nested); let mut nested = nested.to_vec(); - let array = array.slice(start, len); + let array = array.clone().sliced(start, len); if let Some(Nested::Primitive(_, _, c)) = nested.last_mut() { *c = len; } else { diff --git a/src/io/parquet/write/mod.rs b/src/io/parquet/write/mod.rs index 3fc13f74031..6e10a19b164 100644 --- a/src/io/parquet/write/mod.rs +++ b/src/io/parquet/write/mod.rs @@ -186,7 +186,7 @@ pub fn slice_parquet_array<'a>( if is_nested { (array.to_boxed(), nested) } else { - (array.slice(offset, length), nested) + (array.to_boxed().sliced(offset, length), nested) } } diff --git a/src/io/parquet/write/primitive/nested.rs b/src/io/parquet/write/primitive/nested.rs index 6c7bf59f5a2..c8bf625dc25 100644 --- a/src/io/parquet/write/primitive/nested.rs +++ b/src/io/parquet/write/primitive/nested.rs @@ -35,7 +35,7 @@ where let (start, len) = slice_nested_leaf(nested); let mut nested = nested.to_vec(); - let array = array.slice(start, len); + let array = array.clone().sliced(start, len); if let Some(Nested::Primitive(_, _, c)) = nested.last_mut() { *c = len; } else { diff --git a/src/io/parquet/write/utf8/nested.rs b/src/io/parquet/write/utf8/nested.rs index 1054de6e311..e7fe4dc5e0e 100644 --- a/src/io/parquet/write/utf8/nested.rs +++ b/src/io/parquet/write/utf8/nested.rs @@ -27,7 +27,7 @@ where let (start, len) = slice_nested_leaf(nested); let mut nested = nested.to_vec(); - let array = array.slice(start, len); + let array = array.clone().sliced(start, len); if let Some(Nested::Primitive(_, _, c)) = nested.last_mut() { *c = len; } else { diff --git a/src/offset.rs b/src/offset.rs index 458e9ac7b5a..52d8e6c9672 100644 --- a/src/offset.rs +++ b/src/offset.rs @@ -393,7 +393,22 @@ impl OffsetsBuffer { self.0.as_slice() } - /// Returns the last offset of this container, which is guaranteed to exist. + /// Returns the range of the offsets. + #[inline] + pub fn range(&self) -> O { + *self.last() - *self.first() + } + + /// Returns the first offset. + #[inline] + pub fn first(&self) -> &O { + match self.0.first() { + Some(element) => element, + None => unsafe { unreachable_unchecked() }, + } + } + + /// Returns the last offset. #[inline] pub fn last(&self) -> &O { match self.0.last() { @@ -423,13 +438,12 @@ impl OffsetsBuffer { (start, end) } - /// Returns a new [`OffsetsBuffer`] that is a slice of this buffer starting at `offset`. - /// Doing so allows the same memory region to be shared between buffers. + /// Slices this [`OffsetsBuffer`] starting at `offset`. /// # Safety /// The caller must ensure `offset + length <= self.len()` #[inline] - pub unsafe fn slice_unchecked(self, offset: usize, length: usize) -> Self { - Self(self.0.slice_unchecked(offset, length)) + pub unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) { + self.0.slice_unchecked(offset, length); } /// Returns an iterator with the lengths of the offsets diff --git a/tests/it/array/binary/mod.rs b/tests/it/array/binary/mod.rs index a36a2a9d256..1418fce8918 100644 --- a/tests/it/array/binary/mod.rs +++ b/tests/it/array/binary/mod.rs @@ -38,7 +38,7 @@ fn basics() { ); assert_eq!(array, array2); - let array = array.slice(1, 2); + let array = array.sliced(1, 2); assert_eq!(array.value(0), b""); assert_eq!(array.value(1), b"hello2"); // note how this keeps everything: the offsets were sliced diff --git a/tests/it/array/boolean/mod.rs b/tests/it/array/boolean/mod.rs index 73d143d1d2b..cd6ad0c77af 100644 --- a/tests/it/array/boolean/mod.rs +++ b/tests/it/array/boolean/mod.rs @@ -34,7 +34,7 @@ fn basics() { ); assert_eq!(array, array2); - let array = array.slice(1, 2); + let array = array.sliced(1, 2); assert!(!array.value(0)); assert!(!array.value(1)); } diff --git a/tests/it/array/equal/boolean.rs b/tests/it/array/equal/boolean.rs index 1fc9082de55..9a43f226e6c 100644 --- a/tests/it/array/equal/boolean.rs +++ b/tests/it/array/equal/boolean.rs @@ -31,12 +31,12 @@ fn test_boolean_equal_offset() { let b = BooleanArray::from_slice(vec![true, false, false, false, true, false, true, true]); test_equal(&a, &b, false); - let a_slice = a.slice(2, 3); - let b_slice = b.slice(3, 3); + let a_slice = a.sliced(2, 3); + let b_slice = b.sliced(3, 3); test_equal(&a_slice, &b_slice, true); - let a_slice = a.slice(3, 4); - let b_slice = b.slice(4, 4); + let a_slice = a.sliced(3, 4); + let b_slice = b.sliced(4, 4); test_equal(&a_slice, &b_slice, false); // Elements fill in `u8`'s exactly. diff --git a/tests/it/array/equal/fixed_size_list.rs b/tests/it/array/equal/fixed_size_list.rs index 14591ce5cd0..3df32c574f2 100644 --- a/tests/it/array/equal/fixed_size_list.rs +++ b/tests/it/array/equal/fixed_size_list.rs @@ -70,15 +70,15 @@ fn test_fixed_list_offsets() { let b = create_fixed_size_list_array([Some(&[1, 2, 3]), None, None, Some(&[3, 6, 9]), None, None]); - let a_slice = a.slice(0, 3); - let b_slice = b.slice(0, 3); + let a_slice = a.clone().sliced(0, 3); + let b_slice = b.clone().sliced(0, 3); test_equal(&a_slice, &b_slice, true); - let a_slice = a.slice(0, 5); - let b_slice = b.slice(0, 5); + let a_slice = a.clone().sliced(0, 5); + let b_slice = b.clone().sliced(0, 5); test_equal(&a_slice, &b_slice, false); - let a_slice = a.slice(4, 1); - let b_slice = b.slice(4, 1); + let a_slice = a.sliced(4, 1); + let b_slice = b.sliced(4, 1); test_equal(&a_slice, &b_slice, true); } diff --git a/tests/it/array/equal/list.rs b/tests/it/array/equal/list.rs index bf2ccf22d8b..d54c59de6e9 100644 --- a/tests/it/array/equal/list.rs +++ b/tests/it/array/equal/list.rs @@ -51,16 +51,16 @@ fn test_list_offsets() { let a = create_list_array([Some(&[1, 2]), None, None, Some(&[3, 4]), None, None]); let b = create_list_array([Some(&[1, 2]), None, None, Some(&[3, 5]), None, None]); - let a_slice = a.slice(0, 3); - let b_slice = b.slice(0, 3); + let a_slice = a.clone().sliced(0, 3); + let b_slice = b.clone().sliced(0, 3); test_equal(&a_slice, &b_slice, true); - let a_slice = a.slice(0, 5); - let b_slice = b.slice(0, 5); + let a_slice = a.clone().sliced(0, 5); + let b_slice = b.clone().sliced(0, 5); test_equal(&a_slice, &b_slice, false); - let a_slice = a.slice(4, 1); - let b_slice = b.slice(4, 1); + let a_slice = a.sliced(4, 1); + let b_slice = b.sliced(4, 1); test_equal(&a_slice, &b_slice, true); } @@ -78,7 +78,7 @@ fn test_bla() { ])); let validity = Bitmap::from([true, false, true]); let lhs = ListArray::::new(data_type, offsets, values, Some(validity)); - let lhs = lhs.slice(1, 2); + let lhs = lhs.sliced(1, 2); let offsets = vec![0, 0, 3].try_into().unwrap(); let data_type = ListArray::::default_datatype(DataType::Int32); diff --git a/tests/it/array/equal/primitive.rs b/tests/it/array/equal/primitive.rs index cee9bed9b62..58793bc8c3b 100644 --- a/tests/it/array/equal/primitive.rs +++ b/tests/it/array/equal/primitive.rs @@ -81,9 +81,9 @@ fn test_primitive_slice() { for (lhs, slice_lhs, rhs, slice_rhs, expected) in cases { let lhs = Int32Array::from(&lhs); - let lhs = lhs.slice(slice_lhs.0, slice_lhs.1); + let lhs = lhs.sliced(slice_lhs.0, slice_lhs.1); let rhs = Int32Array::from(&rhs); - let rhs = rhs.slice(slice_rhs.0, slice_rhs.1); + let rhs = rhs.sliced(slice_rhs.0, slice_rhs.1); test_equal(&lhs, &rhs, expected); } diff --git a/tests/it/array/fixed_size_binary/mod.rs b/tests/it/array/fixed_size_binary/mod.rs index aa1ae6de7e4..c5524248ff5 100644 --- a/tests/it/array/fixed_size_binary/mod.rs +++ b/tests/it/array/fixed_size_binary/mod.rs @@ -16,7 +16,7 @@ fn basics() { assert_eq!(array.value(0), [1, 2]); assert_eq!(array.value(2), [5, 6]); - let array = array.slice(1, 2); + let array = array.sliced(1, 2); assert_eq!(array.value(1), [5, 6]); } diff --git a/tests/it/array/fixed_size_list/mod.rs b/tests/it/array/fixed_size_list/mod.rs index 23d37a2a212..c2a4b11e62c 100644 --- a/tests/it/array/fixed_size_list/mod.rs +++ b/tests/it/array/fixed_size_list/mod.rs @@ -30,7 +30,7 @@ fn basics() { assert_eq!(array.value(0).as_ref(), Int32Array::from_slice([10, 20])); assert_eq!(array.value(1).as_ref(), Int32Array::from_slice([0, 0])); - let array = array.slice(1, 1); + let array = array.sliced(1, 1); assert_eq!(array.value(0).as_ref(), Int32Array::from_slice([0, 0])); } diff --git a/tests/it/array/growable/binary.rs b/tests/it/array/growable/binary.rs index a54a8f9f805..5b8bb3e9349 100644 --- a/tests/it/array/growable/binary.rs +++ b/tests/it/array/growable/binary.rs @@ -23,7 +23,7 @@ fn no_offsets() { #[test] fn with_offsets() { let array = BinaryArray::::from([Some("a"), Some("bc"), None, Some("defh")]); - let array = array.slice(1, 3); + let array = array.sliced(1, 3); let mut a = GrowableBinary::new(vec![&array], false, 0); @@ -39,7 +39,7 @@ fn with_offsets() { #[test] fn test_string_offsets() { let array = BinaryArray::::from([Some("a"), Some("bc"), None, Some("defh")]); - let array = array.slice(1, 3); + let array = array.sliced(1, 3); let mut a = GrowableBinary::new(vec![&array], false, 0); @@ -72,7 +72,7 @@ fn test_multiple_with_validity() { #[test] fn test_string_null_offset_validity() { let array = BinaryArray::::from([Some("a"), Some("bc"), None, Some("defh")]); - let array = array.slice(1, 3); + let array = array.sliced(1, 3); let mut a = GrowableBinary::new(vec![&array], true, 0); diff --git a/tests/it/array/growable/fixed_binary.rs b/tests/it/array/growable/fixed_binary.rs index a14aecaef3b..c3bcb630551 100644 --- a/tests/it/array/growable/fixed_binary.rs +++ b/tests/it/array/growable/fixed_binary.rs @@ -26,7 +26,7 @@ fn basic() { fn offsets() { let array = FixedSizeBinaryArray::from_iter(vec![Some(b"ab"), Some(b"bc"), None, Some(b"fh")], 2); - let array = array.slice(1, 3); + let array = array.sliced(1, 3); let mut a = GrowableFixedSizeBinary::new(vec![&array], false, 0); @@ -60,7 +60,7 @@ fn multiple_with_validity() { #[test] fn null_offset_validity() { let array = FixedSizeBinaryArray::from_iter(vec![Some("aa"), Some("bc"), None, Some("fh")], 2); - let array = array.slice(1, 3); + let array = array.sliced(1, 3); let mut a = GrowableFixedSizeBinary::new(vec![&array], true, 0); @@ -78,7 +78,7 @@ fn null_offset_validity() { fn sized_offsets() { let array = FixedSizeBinaryArray::from_iter(vec![Some(&[0, 0]), Some(&[0, 1]), Some(&[0, 2])], 2); - let array = array.slice(1, 2); + let array = array.sliced(1, 2); // = [[0, 1], [0, 2]] due to the offset = 1 let mut a = GrowableFixedSizeBinary::new(vec![&array], false, 0); diff --git a/tests/it/array/growable/fixed_size_list.rs b/tests/it/array/growable/fixed_size_list.rs index 10474befdc0..d48e40338c5 100644 --- a/tests/it/array/growable/fixed_size_list.rs +++ b/tests/it/array/growable/fixed_size_list.rs @@ -39,7 +39,7 @@ fn null_offset() { Some(vec![Some(6i32), Some(7), Some(8)]), ]; let array = create_list_array(data); - let array = array.slice(1, 2); + let array = array.sliced(1, 2); let mut a = GrowableFixedSizeList::new(vec![&array], false, 0); a.extend(0, 1, 1); diff --git a/tests/it/array/growable/list.rs b/tests/it/array/growable/list.rs index f40dbdc4ecf..45006b6e3d6 100644 --- a/tests/it/array/growable/list.rs +++ b/tests/it/array/growable/list.rs @@ -70,7 +70,7 @@ fn null_offset() { Some(vec![Some(6i32), Some(7), Some(8)]), ]; let array = create_list_array(data); - let array = array.slice(1, 2); + let array = array.sliced(1, 2); let mut a = GrowableList::new(vec![&array], false, 0); a.extend(0, 1, 1); @@ -92,7 +92,7 @@ fn null_offsets() { Some(vec![Some(6i32), None, Some(8)]), ]; let array = create_list_array(data); - let array = array.slice(1, 2); + let array = array.sliced(1, 2); let mut a = GrowableList::new(vec![&array], false, 0); a.extend(0, 1, 1); diff --git a/tests/it/array/growable/primitive.rs b/tests/it/array/growable/primitive.rs index 305a2affcc9..f308a0c49b5 100644 --- a/tests/it/array/growable/primitive.rs +++ b/tests/it/array/growable/primitive.rs @@ -19,7 +19,7 @@ fn basics() { #[test] fn offset() { let b = PrimitiveArray::::from(vec![Some(1), Some(2), Some(3)]); - let b = b.slice(1, 2); + let b = b.sliced(1, 2); let mut a = GrowablePrimitive::new(vec![&b], false, 2); a.extend(0, 0, 2); assert_eq!(a.len(), 2); @@ -32,7 +32,7 @@ fn offset() { #[test] fn null_offset() { let b = PrimitiveArray::::from(vec![Some(1), None, Some(3)]); - let b = b.slice(1, 2); + let b = b.sliced(1, 2); let mut a = GrowablePrimitive::new(vec![&b], false, 2); a.extend(0, 0, 2); assert_eq!(a.len(), 2); @@ -44,7 +44,7 @@ fn null_offset() { #[test] fn null_offset_validity() { let b = PrimitiveArray::::from(&[Some(1), Some(2), Some(3)]); - let b = b.slice(1, 2); + let b = b.sliced(1, 2); let mut a = GrowablePrimitive::new(vec![&b], true, 2); a.extend(0, 0, 2); a.extend_validity(3); diff --git a/tests/it/array/growable/struct_.rs b/tests/it/array/growable/struct_.rs index 30ecb45ad27..9596f23961d 100644 --- a/tests/it/array/growable/struct_.rs +++ b/tests/it/array/growable/struct_.rs @@ -41,7 +41,7 @@ fn basic() { let expected = StructArray::new( fields, - vec![values[0].slice(1, 2), values[1].slice(1, 2)], + vec![values[0].sliced(1, 2), values[1].sliced(1, 2)], None, ); assert_eq!(result, expected) @@ -51,7 +51,7 @@ fn basic() { fn offset() { let (fields, values) = some_values(); - let array = StructArray::new(fields.clone(), values.clone(), None).slice(1, 3); + let array = StructArray::new(fields.clone(), values.clone(), None).sliced(1, 3); let mut a = GrowableStruct::new(vec![&array], false, 0); @@ -61,7 +61,7 @@ fn offset() { let expected = StructArray::new( fields, - vec![values[0].slice(2, 2), values[1].slice(2, 2)], + vec![values[0].sliced(2, 2), values[1].sliced(2, 2)], None, ); @@ -86,8 +86,8 @@ fn nulls() { let expected = StructArray::new( fields, - vec![values[0].slice(1, 2), values[1].slice(1, 2)], - Some(Bitmap::from_u8_slice([0b00000010], 5).slice(1, 2)), + vec![values[0].sliced(1, 2), values[1].sliced(1, 2)], + Some(Bitmap::from_u8_slice([0b00000010], 5).sliced(1, 2)), ); assert_eq!(result, expected) diff --git a/tests/it/array/growable/union.rs b/tests/it/array/growable/union.rs index 0d69e2a2a82..21cbbebabf4 100644 --- a/tests/it/array/growable/union.rs +++ b/tests/it/array/growable/union.rs @@ -28,7 +28,7 @@ fn sparse() -> Result<()> { a.extend(0, index, length); assert_eq!(a.len(), length); - let expected = array.slice(index, length); + let expected = array.clone().sliced(index, length); let result: UnionArray = a.into(); @@ -61,7 +61,7 @@ fn dense() -> Result<()> { a.extend(0, index, length); assert_eq!(a.len(), length); - let expected = array.slice(index, length); + let expected = array.clone().sliced(index, length); let result: UnionArray = a.into(); diff --git a/tests/it/array/growable/utf8.rs b/tests/it/array/growable/utf8.rs index 14221dc97fd..2568116dc3a 100644 --- a/tests/it/array/growable/utf8.rs +++ b/tests/it/array/growable/utf8.rs @@ -23,7 +23,7 @@ fn validity() { #[test] fn offsets() { let array = Utf8Array::::from([Some("a"), Some("bc"), None, Some("defh")]); - let array = array.slice(1, 3); + let array = array.sliced(1, 3); let mut a = GrowableUtf8::new(vec![&array], false, 0); @@ -39,7 +39,7 @@ fn offsets() { #[test] fn offsets2() { let array = Utf8Array::::from([Some("a"), Some("bc"), None, Some("defh")]); - let array = array.slice(1, 3); + let array = array.sliced(1, 3); let mut a = GrowableUtf8::new(vec![&array], false, 0); @@ -72,7 +72,7 @@ fn multiple_with_validity() { #[test] fn null_offset_validity() { let array = Utf8Array::::from([Some("a"), Some("bc"), None, Some("defh")]); - let array = array.slice(1, 3); + let array = array.sliced(1, 3); let mut a = GrowableUtf8::new(vec![&array], true, 0); diff --git a/tests/it/array/map/mod.rs b/tests/it/array/map/mod.rs index f58936dc3f6..285bc8b39b3 100644 --- a/tests/it/array/map/mod.rs +++ b/tests/it/array/map/mod.rs @@ -39,7 +39,7 @@ fn basics() { )) as Box ); - let sliced = array.slice(1, 1); + let sliced = array.sliced(1, 1); assert_eq!( sliced.value(0), Box::new(StructArray::new( diff --git a/tests/it/array/primitive/mod.rs b/tests/it/array/primitive/mod.rs index c3e3369f3ef..e8a78435311 100644 --- a/tests/it/array/primitive/mod.rs +++ b/tests/it/array/primitive/mod.rs @@ -31,7 +31,7 @@ fn basics() { ); assert_eq!(array, array2); - let array = array.slice(1, 2); + let array = array.sliced(1, 2); assert_eq!(array.value(0), 0); assert_eq!(array.value(1), 10); assert_eq!(array.values().as_slice(), &[0, 10]); diff --git a/tests/it/array/union.rs b/tests/it/array/union.rs index 48a1d98f975..4a8c3aee214 100644 --- a/tests/it/array/union.rs +++ b/tests/it/array/union.rs @@ -75,7 +75,7 @@ fn slice() -> Result<()> { let array = UnionArray::new(data_type.clone(), types, fields.clone(), None); - let result = array.slice(1, 2); + let result = array.sliced(1, 2); let sliced_types = Buffer::from(vec![0, 1]); let sliced_fields = vec![ @@ -169,7 +169,7 @@ fn iter_sparse_slice() -> Result<()> { ]; let array = UnionArray::new(data_type, types, fields.clone(), None); - let array_slice = array.slice(1, 1); + let array_slice = array.sliced(1, 1); let mut iter = array_slice.iter(); assert_eq!( @@ -196,7 +196,7 @@ fn iter_dense_slice() -> Result<()> { ]; let array = UnionArray::new(data_type, types, fields.clone(), Some(offsets)); - let array_slice = array.slice(1, 1); + let array_slice = array.sliced(1, 1); let mut iter = array_slice.iter(); assert_eq!( diff --git a/tests/it/array/utf8/mod.rs b/tests/it/array/utf8/mod.rs index 7f48aebcfe9..9a437bb8681 100644 --- a/tests/it/array/utf8/mod.rs +++ b/tests/it/array/utf8/mod.rs @@ -35,7 +35,7 @@ fn basics() { ); assert_eq!(array, array2); - let array = array.slice(1, 2); + let array = array.sliced(1, 2); assert_eq!(array.value(0), ""); assert_eq!(array.value(1), "hello2"); // note how this keeps everything: the offsets were sliced diff --git a/tests/it/bitmap/assign_ops.rs b/tests/it/bitmap/assign_ops.rs index caf5586cf94..5f367a7990c 100644 --- a/tests/it/bitmap/assign_ops.rs +++ b/tests/it/bitmap/assign_ops.rs @@ -30,10 +30,10 @@ fn binary_assign_oob() { let b = MutableBitmap::from_iter(std::iter::repeat(true).take(65)); let a: Bitmap = a.into(); - let a = a.slice(10, 20); + let a = a.sliced(10, 20); let b: Bitmap = b.into(); - let b = b.slice(10, 20); + let b = b.sliced(10, 20); let mut a = a.make_mut(); diff --git a/tests/it/bitmap/immutable.rs b/tests/it/bitmap/immutable.rs index aa6c8133acf..d4d56ea10c2 100644 --- a/tests/it/bitmap/immutable.rs +++ b/tests/it/bitmap/immutable.rs @@ -13,7 +13,7 @@ fn as_slice() { #[test] fn as_slice_offset() { let b = Bitmap::from([true, true, true, true, true, true, true, true, true]); - let b = b.slice(8, 1); + let b = b.sliced(8, 1); let (slice, offset, length) = b.as_slice(); assert_eq!(slice, &[0b1]); @@ -24,7 +24,7 @@ fn as_slice_offset() { #[test] fn as_slice_offset_middle() { let b = Bitmap::from_u8_slice([0, 0, 0, 0b00010101], 27); - let b = b.slice(22, 5); + let b = b.sliced(22, 5); let (slice, offset, length) = b.as_slice(); assert_eq!(slice, &[0, 0b00010101]); @@ -35,7 +35,7 @@ fn as_slice_offset_middle() { #[test] fn debug() { let b = Bitmap::from([true, true, false, true, true, true, true, true, true]); - let b = b.slice(2, 7); + let b = b.sliced(2, 7); assert_eq!(format!("{b:?}"), "[0b111110__, 0b_______1]"); } diff --git a/tests/it/bitmap/mod.rs b/tests/it/bitmap/mod.rs index 59d51004152..04026e4ac43 100644 --- a/tests/it/bitmap/mod.rs +++ b/tests/it/bitmap/mod.rs @@ -21,7 +21,7 @@ pub(crate) fn bitmap_strategy() -> impl Strategy { }) .prop_flat_map(|(vec, index, len)| { let bitmap = Bitmap::from(&vec); - let bitmap = bitmap.slice(index, len); + let bitmap = bitmap.sliced(index, len); Just(bitmap) }) } @@ -49,12 +49,12 @@ fn eq_len() { #[test] fn eq_slice() { - let lhs = create_bitmap([0b10101010], 8).slice(1, 7); - let rhs = create_bitmap([0b10101011], 8).slice(1, 7); + let lhs = create_bitmap([0b10101010], 8).sliced(1, 7); + let rhs = create_bitmap([0b10101011], 8).sliced(1, 7); assert!(lhs == rhs); - let lhs = create_bitmap([0b10101010], 8).slice(2, 6); - let rhs = create_bitmap([0b10101110], 8).slice(2, 6); + let lhs = create_bitmap([0b10101010], 8).sliced(2, 6); + let rhs = create_bitmap([0b10101110], 8).sliced(2, 6); assert!(lhs != rhs); } @@ -89,9 +89,9 @@ fn or_large() { #[test] fn and_offset() { - let lhs = create_bitmap([0b01101011], 8).slice(1, 7); - let rhs = create_bitmap([0b01001111], 8).slice(1, 7); - let expected = create_bitmap([0b01001010], 8).slice(1, 7); + let lhs = create_bitmap([0b01101011], 8).sliced(1, 7); + let rhs = create_bitmap([0b01001111], 8).sliced(1, 7); + let expected = create_bitmap([0b01001010], 8).sliced(1, 7); assert_eq!(&lhs & &rhs, expected); } @@ -115,11 +115,11 @@ fn subslicing_gives_correct_null_count() { let base = Bitmap::from([false, true, true, false, false, true, true, true]); assert_eq!(base.unset_bits(), 3); - let view1 = base.clone().slice(0, 1); - let view2 = base.slice(1, 7); + let view1 = base.clone().sliced(0, 1); + let view2 = base.sliced(1, 7); assert_eq!(view1.unset_bits(), 1); assert_eq!(view2.unset_bits(), 2); - let view3 = view2.slice(0, 1); + let view3 = view2.sliced(0, 1); assert_eq!(view3.unset_bits(), 0); } diff --git a/tests/it/bitmap/utils/slice_iterator.rs b/tests/it/bitmap/utils/slice_iterator.rs index 97fca44c31e..cab565456d8 100644 --- a/tests/it/bitmap/utils/slice_iterator.rs +++ b/tests/it/bitmap/utils/slice_iterator.rs @@ -132,7 +132,7 @@ fn past_end_should_not_be_returned() { #[test] fn sliced() { let values = Bitmap::from_u8_slice([0b11111010, 0b11111011], 16); - let values = values.slice(8, 2); + let values = values.sliced(8, 2); let iter = SlicesIterator::new(&values); let chunks = iter.collect::>(); @@ -144,7 +144,7 @@ fn sliced() { #[test] fn remainder_1() { let values = Bitmap::from_u8_slice([0, 0, 0b00000000, 0b00010101], 27); - let values = values.slice(22, 5); + let values = values.sliced(22, 5); let iter = SlicesIterator::new(&values); let chunks = iter.collect::>(); assert_eq!(chunks, vec![(2, 1), (4, 1)]); diff --git a/tests/it/bitmap/utils/zip_validity.rs b/tests/it/bitmap/utils/zip_validity.rs index 9c6187f60a6..dc162962202 100644 --- a/tests/it/bitmap/utils/zip_validity.rs +++ b/tests/it/bitmap/utils/zip_validity.rs @@ -71,7 +71,7 @@ fn byte() { #[test] fn offset() { - let a = Bitmap::from([true, false, true, false, false, true, true, false, true]).slice(1, 8); + let a = Bitmap::from([true, false, true, false, false, true, true, false, true]).sliced(1, 8); let a = Some(a.iter()); let values = vec![0, 1, 2, 3, 4, 5, 6, 7]; let zip = ZipValidity::new(values.into_iter(), a); @@ -94,7 +94,7 @@ fn none() { #[test] fn rev() { - let a = Bitmap::from([true, false, true, false, false, true, true, false, true]).slice(1, 8); + let a = Bitmap::from([true, false, true, false, false, true, true, false, true]).sliced(1, 8); let a = Some(a.iter()); let values = vec![0, 1, 2, 3, 4, 5, 6, 7]; let zip = ZipValidity::new(values.into_iter(), a); diff --git a/tests/it/buffer/immutable.rs b/tests/it/buffer/immutable.rs index 7e5661fdd59..1960efcebd9 100644 --- a/tests/it/buffer/immutable.rs +++ b/tests/it/buffer/immutable.rs @@ -17,7 +17,7 @@ fn from_slice() { #[test] fn slice() { let buffer = Buffer::::from(vec![0, 1, 2, 3]); - let buffer = buffer.slice(1, 2); + let buffer = buffer.sliced(1, 2); assert_eq!(buffer.len(), 2); assert_eq!(buffer.as_slice(), &[1, 2]); } @@ -32,7 +32,7 @@ fn from_iter() { #[test] fn debug() { let buffer = Buffer::::from(vec![0, 1, 2, 3]); - let buffer = buffer.slice(1, 2); + let buffer = buffer.sliced(1, 2); let a = format!("{buffer:?}"); assert_eq!(a, "[1, 2]") } diff --git a/tests/it/compute/boolean.rs b/tests/it/compute/boolean.rs index c8c743b806e..8c505a164fc 100644 --- a/tests/it/compute/boolean.rs +++ b/tests/it/compute/boolean.rs @@ -126,8 +126,8 @@ fn array_and_sliced_same_offset() { false, false, false, false, false, false, false, false, false, true, false, true, ]); - let a = a.slice(8, 4); - let b = b.slice(8, 4); + let a = a.sliced(8, 4); + let b = b.sliced(8, 4); let c = and(&a, &b); let expected = BooleanArray::from_slice(vec![false, false, false, true]); @@ -144,8 +144,8 @@ fn array_and_sliced_same_offset_mod8() { false, false, false, false, false, false, false, false, false, true, false, true, ]); - let a = a.slice(0, 4); - let b = b.slice(8, 4); + let a = a.sliced(0, 4); + let b = b.sliced(8, 4); let c = and(&a, &b); @@ -161,7 +161,7 @@ fn array_and_sliced_offset1() { ]); let b = BooleanArray::from_slice(vec![false, true, false, true]); - let a = a.slice(8, 4); + let a = a.sliced(8, 4); let c = and(&a, &b); @@ -177,7 +177,7 @@ fn array_and_sliced_offset2() { false, false, false, false, false, false, false, false, false, true, false, true, ]); - let b = b.slice(8, 4); + let b = b.sliced(8, 4); let c = and(&a, &b); @@ -189,7 +189,7 @@ fn array_and_sliced_offset2() { #[test] fn array_and_validity_offset() { let a = BooleanArray::from(vec![None, Some(false), Some(true), None, Some(true)]); - let a = a.slice(1, 4); + let a = a.sliced(1, 4); let a = a.as_any().downcast_ref::().unwrap(); let b = BooleanArray::from(vec![ @@ -201,7 +201,7 @@ fn array_and_validity_offset() { Some(true), ]); - let b = b.slice(2, 4); + let b = b.sliced(2, 4); let b = b.as_any().downcast_ref::().unwrap(); let c = and(a, b); @@ -225,7 +225,7 @@ fn test_nonnull_array_is_null() { #[test] fn test_nonnull_array_with_offset_is_null() { let a = Int32Array::from_slice(vec![1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1]); - let a = a.slice(8, 4); + let a = a.sliced(8, 4); let res = is_null(&a); @@ -248,7 +248,7 @@ fn test_nonnull_array_is_not_null() { #[test] fn test_nonnull_array_with_offset_is_not_null() { let a = Int32Array::from_slice([1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 3, 2, 1]); - let a = a.slice(8, 4); + let a = a.sliced(8, 4); let res = is_not_null(&a); @@ -289,7 +289,7 @@ fn test_nullable_array_with_offset_is_null() { None, None, ]); - let a = a.slice(8, 4); + let a = a.sliced(8, 4); let res = is_null(&a); @@ -330,7 +330,7 @@ fn test_nullable_array_with_offset_is_not_null() { None, None, ]); - let a = a.slice(8, 4); + let a = a.sliced(8, 4); let res = is_not_null(&a); diff --git a/tests/it/compute/cast.rs b/tests/it/compute/cast.rs index 72bed91f99b..0631ba20baf 100644 --- a/tests/it/compute/cast.rs +++ b/tests/it/compute/cast.rs @@ -102,7 +102,7 @@ fn i32_to_u8() { #[test] fn i32_to_u8_sliced() { let array = Int32Array::from_slice([-5, 6, -7, 8, 100000000]); - let array = array.slice(2, 3); + let array = array.sliced(2, 3); let b = cast(&array, &DataType::UInt8, CastOptions::default()).unwrap(); let expected = UInt8Array::from(&[None, Some(8), None]); let c = b.as_any().downcast_ref::().unwrap(); @@ -170,7 +170,7 @@ fn i32_to_list_f64_nullable_sliced() { let array = Int32Array::from(input); - let array = array.slice(2, 4); + let array = array.sliced(2, 4); let b = cast( &array, &DataType::List(Box::new(Field::new("item", DataType::Float64, true))), diff --git a/tests/it/compute/comparison.rs b/tests/it/compute/comparison.rs index bc9e5a2319d..a63bb39ce01 100644 --- a/tests/it/compute/comparison.rs +++ b/tests/it/compute/comparison.rs @@ -114,7 +114,7 @@ fn test_eq_scalar() { fn test_eq_with_slice() { let a = BooleanArray::from_slice([true, true, false]); let b = BooleanArray::from_slice([true, true, true, true, false]); - let c = b.slice(2, 3); + let c = b.sliced(2, 3); let d = eq(&c, &a); assert_eq!(d, BooleanArray::from_slice([true, true, true])); } diff --git a/tests/it/compute/concatenate.rs b/tests/it/compute/concatenate.rs index fe1d715d037..b5c5e52905f 100644 --- a/tests/it/compute/concatenate.rs +++ b/tests/it/compute/concatenate.rs @@ -72,9 +72,9 @@ fn primitive_arrays() -> Result<()> { #[test] fn primitive_array_slices() -> Result<()> { - let input_1 = Int64Array::from(&[Some(-1), Some(-1), Some(2), None, None]).slice(1, 3); + let input_1 = Int64Array::from(&[Some(-1), Some(-1), Some(2), None, None]).sliced(1, 3); - let input_2 = Int64Array::from(&[Some(101), Some(102), Some(103), None]).slice(1, 3); + let input_2 = Int64Array::from(&[Some(101), Some(102), Some(103), None]).sliced(1, 3); let arr = concatenate(&[&input_1, &input_2])?; let expected_output = Int64Array::from(&[Some(-1), Some(2), None, Some(102), Some(103), None]); diff --git a/tests/it/compute/filter.rs b/tests/it/compute/filter.rs index 98df9079d0b..08a7f6cbcad 100644 --- a/tests/it/compute/filter.rs +++ b/tests/it/compute/filter.rs @@ -4,8 +4,8 @@ use arrow2::compute::filter::*; #[test] fn array_slice() { - let a = Int32Array::from_slice([5, 6, 7, 8, 9]).slice(1, 4); - let b = BooleanArray::from_slice(vec![true, true, false, false, true]).slice(1, 4); + let a = Int32Array::from_slice([5, 6, 7, 8, 9]).sliced(1, 4); + let b = BooleanArray::from_slice(vec![true, true, false, false, true]).sliced(1, 4); let c = filter(&a, &b).unwrap(); let expected = Int32Array::from_slice([6, 9]); diff --git a/tests/it/compute/sort/lex_sort.rs b/tests/it/compute/sort/lex_sort.rs index fb04589f310..8cefae1dd87 100644 --- a/tests/it/compute/sort/lex_sort.rs +++ b/tests/it/compute/sort/lex_sort.rs @@ -8,14 +8,14 @@ fn test_lex_sort_arrays(input: Vec, expected: Vec>) { let sorted = lexsort::(&input, Some(4)).unwrap(); let expected = expected .into_iter() - .map(|x| x.slice(0, 4)) + .map(|x| x.sliced(0, 4)) .collect::>(); assert_eq!(sorted, expected); let sorted = lexsort::(&input, Some(2)).unwrap(); let expected = expected .into_iter() - .map(|x| x.slice(0, 2)) + .map(|x| x.sliced(0, 2)) .collect::>(); assert_eq!(sorted, expected); } diff --git a/tests/it/ffi/data.rs b/tests/it/ffi/data.rs index 4ac2066408f..e5675ac60fe 100644 --- a/tests/it/ffi/data.rs +++ b/tests/it/ffi/data.rs @@ -27,7 +27,7 @@ fn test_round_trip(expected: impl Array + Clone + 'static) -> Result<()> { _test_round_trip(array.clone(), clone(expected.as_ref()))?; // sliced - _test_round_trip(array.slice(1, 2), expected.slice(1, 2)) + _test_round_trip(array.sliced(1, 2), expected.sliced(1, 2)) } fn test_round_trip_schema(field: Field) -> Result<()> { @@ -53,7 +53,7 @@ fn bool() -> Result<()> { #[test] fn bool_nullable_sliced() -> Result<()> { - let bitmap = Bitmap::from([true, false, false, true]).slice(1, 3); + let bitmap = Bitmap::from([true, false, false, true]).sliced(1, 3); let data = BooleanArray::try_new(DataType::Boolean, [true, true, false].into(), Some(bitmap))?; test_round_trip(data) } @@ -72,7 +72,7 @@ fn u32() -> Result<()> { #[test] fn u32_sliced() -> Result<()> { - let bitmap = Bitmap::from([true, false, false, true]).slice(1, 3); + let bitmap = Bitmap::from([true, false, false, true]).sliced(1, 3); let data = Int32Array::try_new(DataType::Int32, vec![1, 2, 3].into(), Some(bitmap))?; test_round_trip(data) } @@ -112,7 +112,7 @@ fn utf8() -> Result<()> { #[test] fn utf8_sliced() -> Result<()> { - let bitmap = Bitmap::from([true, false, false, true]).slice(1, 3); + let bitmap = Bitmap::from([true, false, false, true]).sliced(1, 3); let data = Utf8Array::::try_new( DataType::Utf8, vec![0, 1, 1, 2].try_into().unwrap(), @@ -142,7 +142,7 @@ fn binary() -> Result<()> { #[test] fn binary_sliced() -> Result<()> { - let bitmap = Bitmap::from([true, false, false, true]).slice(1, 3); + let bitmap = Bitmap::from([true, false, false, true]).sliced(1, 3); let data = BinaryArray::::try_new( DataType::Binary, vec![0, 1, 1, 2].try_into().unwrap(), @@ -180,7 +180,7 @@ fn fixed_size_binary_nullable() -> Result<()> { #[test] fn fixed_size_binary_sliced() -> Result<()> { - let bitmap = Bitmap::from([true, false, false, true]).slice(1, 3); + let bitmap = Bitmap::from([true, false, false, true]).sliced(1, 3); let data = FixedSizeBinaryArray::try_new( DataType::FixedSizeBinary(2), b"ababab".to_vec().into(), @@ -207,7 +207,7 @@ fn list() -> Result<()> { #[test] fn list_sliced() -> Result<()> { - let bitmap = Bitmap::from([true, false, false, true]).slice(1, 3); + let bitmap = Bitmap::from([true, false, false, true]).sliced(1, 3); let array = ListArray::::try_new( DataType::List(Box::new(Field::new("a", DataType::Int32, true))), @@ -253,7 +253,7 @@ fn fixed_size_list() -> Result<()> { #[test] fn fixed_size_list_sliced() -> Result<()> { - let bitmap = Bitmap::from([true, false, false, true]).slice(1, 3); + let bitmap = Bitmap::from([true, false, false, true]).sliced(1, 3); let array = FixedSizeListArray::try_new( DataType::FixedSizeList(Box::new(Field::new("a", DataType::Int32, true)), 2), diff --git a/tests/it/io/ipc/mmap.rs b/tests/it/io/ipc/mmap.rs index ef317b9a69c..b52bfa55e47 100644 --- a/tests/it/io/ipc/mmap.rs +++ b/tests/it/io/ipc/mmap.rs @@ -26,7 +26,7 @@ fn round_trip(array: Box) -> Result<()> { #[test] fn utf8() -> Result<()> { let array = Utf8Array::::from([None, None, Some("bb")]) - .slice(1, 2) + .sliced(1, 2) .boxed(); round_trip(array) } @@ -34,24 +34,24 @@ fn utf8() -> Result<()> { #[test] fn fixed_size_binary() -> Result<()> { let array = FixedSizeBinaryArray::from([None, None, Some([1, 2])]) - .slice(1, 2) - .boxed(); + .boxed() + .sliced(1, 2); round_trip(array) } #[test] fn primitive() -> Result<()> { let array = PrimitiveArray::::from([None, None, Some(3)]) - .slice(1, 2) - .boxed(); + .boxed() + .sliced(1, 2); round_trip(array) } #[test] fn boolean() -> Result<()> { let array = BooleanArray::from([None, None, Some(true)]) - .slice(1, 2) - .boxed(); + .boxed() + .sliced(1, 2); round_trip(array) } @@ -73,7 +73,7 @@ fn fixed_size_list() -> Result<()> { array.try_extend(data)?; let array: FixedSizeListArray = array.into(); - round_trip(array.slice(1, 2).boxed()) + round_trip(array.sliced(1, 2).boxed()) } #[test] @@ -86,7 +86,7 @@ fn list() -> Result<()> { let mut array = MutableListArray::>::new(); array.try_extend(data).unwrap(); - let array = array.into_box().slice(1, 2); + let array = array.into_box().sliced(1, 2); round_trip(array) } @@ -99,8 +99,8 @@ fn struct_() -> Result<()> { vec![array], Some([true, true, false, true, false].into()), ) - .slice(1, 4) - .boxed(); + .boxed() + .sliced(1, 4); round_trip(array) } @@ -112,8 +112,8 @@ fn dict() -> Result<()> { let values = PrimitiveArray::::from_slice([0, 1, 2, 3, 4, 5]).boxed(); let array = DictionaryArray::try_from_keys(keys, values)? - .slice(1, 4) - .boxed(); + .boxed() + .sliced(1, 4); round_trip(array) } diff --git a/tests/it/io/ipc/write/file.rs b/tests/it/io/ipc/write/file.rs index 888d8bc206d..5562f803c50 100644 --- a/tests/it/io/ipc/write/file.rs +++ b/tests/it/io/ipc/write/file.rs @@ -337,7 +337,7 @@ fn write_boolean() -> Result<()> { #[cfg_attr(miri, ignore)] // compression uses FFI, which miri does not support fn write_sliced_utf8() -> Result<()> { let array = Utf8Array::::from_slice(["aa", "bb"]) - .slice(1, 1) + .sliced(1, 1) .boxed(); let schema = Schema::from(vec![Field::new("a", array.data_type().clone(), true)]); let columns = Chunk::try_new(vec![array])?; @@ -354,7 +354,7 @@ fn write_sliced_list() -> Result<()> { let mut array = MutableListArray::>::new(); array.try_extend(data).unwrap(); - let array: Box = array.into_box().slice(1, 2); + let array: Box = array.into_box().sliced(1, 2); let schema = Schema::from(vec![Field::new("a", array.data_type().clone(), true)]); let columns = Chunk::try_new(vec![array])?; diff --git a/tests/it/io/ndjson/read.rs b/tests/it/io/ndjson/read.rs index a4afd53afe7..2c8872ce1a0 100644 --- a/tests/it/io/ndjson/read.rs +++ b/tests/it/io/ndjson/read.rs @@ -139,7 +139,7 @@ fn read_nested_struct_batched() -> Result<()> { // create a chunked array by batch_size from the (un-chunked) expected let expected: Vec> = (0..(expected.len() + batch_size - 1) / batch_size) - .map(|offset| expected.slice(offset * batch_size, batch_size)) + .map(|offset| expected.sliced(offset * batch_size, batch_size)) .collect(); let data_type = infer(&ndjson)?; diff --git a/tests/it/io/parquet/mod.rs b/tests/it/io/parquet/mod.rs index f42aa6be36f..4a7cc7fc99b 100644 --- a/tests/it/io/parquet/mod.rs +++ b/tests/it/io/parquet/mod.rs @@ -1553,7 +1553,7 @@ fn assert_roundtrip( let expected = chunk .into_arrays() .into_iter() - .map(|x| x.slice(0, limit)) + .map(|x| x.sliced(0, limit)) .collect::>(); Chunk::new(expected) } else {