From e3158f8b2aafca3685139a193728302dbe332c49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96zg=C3=BCr=20Akkurt?= <91746947+ozgrakkurt@users.noreply.github.com> Date: Fri, 24 Feb 2023 23:49:19 +0300 Subject: [PATCH] Added convenience accessor array.get (#1416) --- src/array/binary/mod.rs | 13 +++++++++++++ src/array/boolean/mod.rs | 13 +++++++++++++ src/array/fixed_size_binary/mod.rs | 13 +++++++++++++ src/array/fixed_size_list/mod.rs | 13 +++++++++++++ src/array/primitive/mod.rs | 13 +++++++++++++ src/array/utf8/mod.rs | 13 +++++++++++++ 6 files changed, 78 insertions(+) diff --git a/src/array/binary/mod.rs b/src/array/binary/mod.rs index 86ebaa73573..84cf3671999 100644 --- a/src/array/binary/mod.rs +++ b/src/array/binary/mod.rs @@ -151,6 +151,19 @@ impl BinaryArray { self.values.get_unchecked(start..end) } + /// Returns the element at index `i` or `None` if it is null + /// # Panics + /// iff `i >= self.len()` + #[inline] + pub fn get(&self, i: usize) -> Option<&[u8]> { + if !self.is_null(i) { + // soundness: Array::is_null panics if i >= self.len + unsafe { Some(self.value_unchecked(i)) } + } else { + None + } + } + /// Returns the [`DataType`] of this array. #[inline] pub fn data_type(&self) -> &DataType { diff --git a/src/array/boolean/mod.rs b/src/array/boolean/mod.rs index 4e959481a28..f470ea6fa1b 100644 --- a/src/array/boolean/mod.rs +++ b/src/array/boolean/mod.rs @@ -143,6 +143,19 @@ impl BooleanArray { self.values.get_bit_unchecked(i) } + /// Returns the element at index `i` or `None` if it is null + /// # Panics + /// iff `i >= self.len()` + #[inline] + pub fn get(&self, i: usize) -> Option { + if !self.is_null(i) { + // soundness: Array::is_null panics if i >= self.len + unsafe { Some(self.value_unchecked(i)) } + } else { + None + } + } + /// Slices this [`BooleanArray`]. /// # Implementation /// This operation is `O(1)` as it amounts to increase up to two ref counts. diff --git a/src/array/fixed_size_binary/mod.rs b/src/array/fixed_size_binary/mod.rs index 5419f30fc54..f1aeee53a98 100644 --- a/src/array/fixed_size_binary/mod.rs +++ b/src/array/fixed_size_binary/mod.rs @@ -157,6 +157,19 @@ impl FixedSizeBinaryArray { .get_unchecked(i * self.size..(i + 1) * self.size) } + /// Returns the element at index `i` or `None` if it is null + /// # Panics + /// iff `i >= self.len()` + #[inline] + pub fn get(&self, i: usize) -> Option<&[u8]> { + if !self.is_null(i) { + // soundness: Array::is_null panics if i >= self.len + unsafe { Some(self.value_unchecked(i)) } + } else { + None + } + } + /// Returns a new [`FixedSizeBinaryArray`] with a different logical type. /// This is `O(1)`. /// # Panics diff --git a/src/array/fixed_size_list/mod.rs b/src/array/fixed_size_list/mod.rs index e8f6f167039..821d219e69c 100644 --- a/src/array/fixed_size_list/mod.rs +++ b/src/array/fixed_size_list/mod.rs @@ -166,6 +166,19 @@ impl FixedSizeListArray { pub unsafe fn value_unchecked(&self, i: usize) -> Box { self.values.sliced_unchecked(i * self.size, self.size) } + + /// Returns the element at index `i` or `None` if it is null + /// # Panics + /// iff `i >= self.len()` + #[inline] + pub fn get(&self, i: usize) -> Option> { + if !self.is_null(i) { + // soundness: Array::is_null panics if i >= self.len + unsafe { Some(self.value_unchecked(i)) } + } else { + None + } + } } impl FixedSizeListArray { diff --git a/src/array/primitive/mod.rs b/src/array/primitive/mod.rs index b8ea5fbb84e..f9942fede3c 100644 --- a/src/array/primitive/mod.rs +++ b/src/array/primitive/mod.rs @@ -196,6 +196,19 @@ impl PrimitiveArray { *self.values.get_unchecked(i) } + /// Returns the element at index `i` or `None` if it is null + /// # Panics + /// iff `i >= self.len()` + #[inline] + pub fn get(&self, i: usize) -> Option { + if !self.is_null(i) { + // soundness: Array::is_null panics if i >= self.len + unsafe { Some(self.value_unchecked(i)) } + } else { + None + } + } + /// Slices this [`PrimitiveArray`] by an offset and length. /// # Implementation /// This operation is `O(1)`. diff --git a/src/array/utf8/mod.rs b/src/array/utf8/mod.rs index e7e83906598..9af4ee76256 100644 --- a/src/array/utf8/mod.rs +++ b/src/array/utf8/mod.rs @@ -171,6 +171,19 @@ impl Utf8Array { std::str::from_utf8_unchecked(slice) } + /// Returns the element at index `i` or `None` if it is null + /// # Panics + /// iff `i >= self.len()` + #[inline] + pub fn get(&self, i: usize) -> Option<&str> { + if !self.is_null(i) { + // soundness: Array::is_null panics if i >= self.len + unsafe { Some(self.value_unchecked(i)) } + } else { + None + } + } + /// Returns the [`DataType`] of this array. #[inline] pub fn data_type(&self) -> &DataType {