Skip to content

Commit 505eb8e

Browse files
alambgabotechs
andauthored
[57_maintenance] Revert "Seal Array trait (#9092)", mark Array as unsafe (#9234) (#9313)
- Part of #9240 - Related to #9184 This is a backport of the following PR to the 57 line - #9234 from @gabotechs As discussed in #9184, rather than sealing the Array trait which broke several downstream crates, this PR markes the trait as `unsafe` so that projects have time to find alternatives for their use-cases that are now satisfied by a custom Array implementation. Co-authored-by: Gabriel <45515538+gabotechs@users.noreply.github.com>
1 parent 74cf914 commit 505eb8e

15 files changed

Lines changed: 42 additions & 62 deletions

arrow-array/src/array/boolean_array.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,8 @@ impl BooleanArray {
286286
}
287287
}
288288

289-
impl super::private::Sealed for BooleanArray {}
290-
291-
impl Array for BooleanArray {
289+
/// SAFETY: Correctly implements the contract of Arrow Arrays
290+
unsafe impl Array for BooleanArray {
292291
fn as_any(&self) -> &dyn Any {
293292
self
294293
}

arrow-array/src/array/byte_array.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,8 @@ impl<T: ByteArrayType> std::fmt::Debug for GenericByteArray<T> {
462462
}
463463
}
464464

465-
impl<T: ByteArrayType> super::private::Sealed for GenericByteArray<T> {}
466-
467-
impl<T: ByteArrayType> Array for GenericByteArray<T> {
465+
/// SAFETY: Correctly implements the contract of Arrow Arrays
466+
unsafe impl<T: ByteArrayType> Array for GenericByteArray<T> {
468467
fn as_any(&self) -> &dyn Any {
469468
self
470469
}

arrow-array/src/array/byte_view_array.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -854,9 +854,8 @@ impl<T: ByteViewType + ?Sized> Debug for GenericByteViewArray<T> {
854854
}
855855
}
856856

857-
impl<T: ByteViewType + ?Sized> super::private::Sealed for GenericByteViewArray<T> {}
858-
859-
impl<T: ByteViewType + ?Sized> Array for GenericByteViewArray<T> {
857+
/// SAFETY: Correctly implements the contract of Arrow Arrays
858+
unsafe impl<T: ByteViewType + ?Sized> Array for GenericByteViewArray<T> {
860859
fn as_any(&self) -> &dyn Any {
861860
self
862861
}

arrow-array/src/array/dictionary_array.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,8 @@ impl<'a, T: ArrowDictionaryKeyType> FromIterator<&'a str> for DictionaryArray<T>
697697
}
698698
}
699699

700-
impl<T: ArrowDictionaryKeyType> super::private::Sealed for DictionaryArray<T> {}
701-
702-
impl<T: ArrowDictionaryKeyType> Array for DictionaryArray<T> {
700+
/// SAFETY: Correctly implements the contract of Arrow Arrays
701+
unsafe impl<T: ArrowDictionaryKeyType> Array for DictionaryArray<T> {
703702
fn as_any(&self) -> &dyn Any {
704703
self
705704
}
@@ -858,9 +857,7 @@ impl<'a, K: ArrowDictionaryKeyType, V> TypedDictionaryArray<'a, K, V> {
858857
}
859858
}
860859

861-
impl<K: ArrowDictionaryKeyType, V: Sync> super::private::Sealed for TypedDictionaryArray<'_, K, V> {}
862-
863-
impl<K: ArrowDictionaryKeyType, V: Sync> Array for TypedDictionaryArray<'_, K, V> {
860+
unsafe impl<K: ArrowDictionaryKeyType, V: Sync> Array for TypedDictionaryArray<'_, K, V> {
864861
fn as_any(&self) -> &dyn Any {
865862
self.dictionary
866863
}

arrow-array/src/array/fixed_size_binary_array.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,8 @@ impl std::fmt::Debug for FixedSizeBinaryArray {
602602
}
603603
}
604604

605-
impl super::private::Sealed for FixedSizeBinaryArray {}
606-
607-
impl Array for FixedSizeBinaryArray {
605+
/// SAFETY: Correctly implements the contract of Arrow Arrays
606+
unsafe impl Array for FixedSizeBinaryArray {
608607
fn as_any(&self) -> &dyn Any {
609608
self
610609
}

arrow-array/src/array/fixed_size_list_array.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,8 @@ impl From<FixedSizeListArray> for ArrayData {
462462
}
463463
}
464464

465-
impl super::private::Sealed for FixedSizeListArray {}
466-
467-
impl Array for FixedSizeListArray {
465+
/// SAFETY: Correctly implements the contract of Arrow Arrays
466+
unsafe impl Array for FixedSizeListArray {
468467
fn as_any(&self) -> &dyn Any {
469468
self
470469
}

arrow-array/src/array/list_array.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,8 @@ impl<OffsetSize: OffsetSizeTrait> GenericListArray<OffsetSize> {
525525
}
526526
}
527527

528-
impl<OffsetSize: OffsetSizeTrait> super::private::Sealed for GenericListArray<OffsetSize> {}
529-
530-
impl<OffsetSize: OffsetSizeTrait> Array for GenericListArray<OffsetSize> {
528+
/// SAFETY: Correctly implements the contract of Arrow Arrays
529+
unsafe impl<OffsetSize: OffsetSizeTrait> Array for GenericListArray<OffsetSize> {
531530
fn as_any(&self) -> &dyn Any {
532531
self
533532
}

arrow-array/src/array/list_view_array.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,8 @@ impl<OffsetSize: OffsetSizeTrait> ArrayAccessor for &GenericListViewArray<Offset
415415
}
416416
}
417417

418-
impl<OffsetSize: OffsetSizeTrait> super::private::Sealed for GenericListViewArray<OffsetSize> {}
419-
420-
impl<OffsetSize: OffsetSizeTrait> Array for GenericListViewArray<OffsetSize> {
418+
/// SAFETY: Correctly implements the contract of Arrow Arrays
419+
unsafe impl<OffsetSize: OffsetSizeTrait> Array for GenericListViewArray<OffsetSize> {
421420
fn as_any(&self) -> &dyn Any {
422421
self
423422
}

arrow-array/src/array/map_array.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,8 @@ impl MapArray {
361361
}
362362
}
363363

364-
impl super::private::Sealed for MapArray {}
365-
366-
impl Array for MapArray {
364+
/// SAFETY: Correctly implements the contract of Arrow Arrays
365+
unsafe impl Array for MapArray {
367366
fn as_any(&self) -> &dyn Any {
368367
self
369368
}

arrow-array/src/array/mod.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,17 @@ pub use list_view_array::*;
7878

7979
use crate::iterator::ArrayIter;
8080

81-
mod private {
82-
/// Private marker trait to ensure [`super::Array`] can not be implemented outside this crate
83-
pub trait Sealed {}
84-
85-
impl<T: Sealed> Sealed for &T {}
86-
}
87-
8881
/// An array in the [arrow columnar format](https://arrow.apache.org/docs/format/Columnar.html)
8982
///
90-
/// This trait is sealed as it is not intended for custom array types, rather only
91-
/// those defined in this crate.
92-
pub trait Array: std::fmt::Debug + Send + Sync + private::Sealed {
83+
/// # Safety
84+
///
85+
/// Implementations of this trait must ensure that all methods implementations comply with
86+
/// the Arrow specification. No safety guards are placed and failing to comply with it can
87+
/// translate into panics or undefined behavior. For example, a value computed based on `len`
88+
/// may be used as a direct index into memory regions without checks.
89+
///
90+
/// Use it at your own risk knowing that this trait might be sealed in the future.
91+
pub unsafe trait Array: std::fmt::Debug + Send + Sync {
9392
/// Returns the array as [`Any`] so that it can be
9493
/// downcasted to a specific implementation.
9594
///
@@ -351,10 +350,8 @@ pub trait Array: std::fmt::Debug + Send + Sync + private::Sealed {
351350
/// A reference-counted reference to a generic `Array`
352351
pub type ArrayRef = Arc<dyn Array>;
353352

354-
impl private::Sealed for ArrayRef {}
355-
356353
/// Ergonomics: Allow use of an ArrayRef as an `&dyn Array`
357-
impl Array for ArrayRef {
354+
unsafe impl Array for ArrayRef {
358355
fn as_any(&self) -> &dyn Any {
359356
self.as_ref().as_any()
360357
}
@@ -433,7 +430,7 @@ impl Array for ArrayRef {
433430
}
434431
}
435432

436-
impl<T: Array> Array for &T {
433+
unsafe impl<T: Array> Array for &T {
437434
fn as_any(&self) -> &dyn Any {
438435
T::as_any(self)
439436
}

0 commit comments

Comments
 (0)