scovich commented on code in PR #12455:
URL: https://github.com/apache/arrow-rs/pull/12455#discussion_r4688272850
##########
arrow-array/src/array/boolean_array.rs:
##########
@@ -637,6 +637,10 @@ unsafe impl Array for BooleanArray {
self
}
+ fn into_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
Review Comment:
This `self: Arc<Self>` receiver was the breakthrough that cracked the nut.
It reliably distinguishes e.g. `Arc<StructArray>` (= useful) from
`Arc<Arc<&StructArray>>` (= unusable). Which in turn prevented adding 1-3
additional helper methods to `Array`, depending on the approach.
##########
arrow-array/src/array/mod.rs:
##########
@@ -550,11 +572,106 @@ unsafe impl Array for ArrayRef {
}
}
+// Ergonomics: allow passing e.g. `&Arc<Int32Array>` to methods that expect
`&dyn Array`
+unsafe impl<T: Array + 'static> Array for Arc<T> {
Review Comment:
I kept this as a separate commit on purpose. If it's is too invasive, we can
easily revert it or split it to a different PR. But with arrow-60 on the
horizon, it seemed like a reasonable time to complete ergonomic `impl Array`
collection. Especially because `Arc::as_ref` penalizes both `Array::to_arc` and
`Array::into_any_arc`.
##########
arrow-array/src/array/mod.rs:
##########
@@ -550,11 +572,106 @@ unsafe impl Array for ArrayRef {
}
}
+// Ergonomics: allow passing e.g. `&Arc<Int32Array>` to methods that expect
`&dyn Array`
+unsafe impl<T: Array + 'static> Array for Arc<T> {
+ fn as_any(&self) -> &dyn Any {
+ T::as_any(self)
+ }
+
+ fn into_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
+ T::into_any_arc(Arc::clone(&*self))
+ }
+
+ fn to_arc(&self) -> ArrayRef {
+ Arc::clone(self) as ArrayRef
Review Comment:
Grr missed an LLM stupidity. No cast should be needed here.
##########
arrow-array/src/array/mod.rs:
##########
@@ -67,6 +67,13 @@ use crate::iterator::ArrayIter;
/// An array in the [Arrow Columnar
Format](https://arrow.apache.org/docs/format/Columnar.html)
///
+/// Conceptually, an array is a concrete arrow-rs type that maps one-to-one to
a [`DataType`], such
+/// as [`Int32Array`] for [`DataType::Int32`]. For convenience, other types
also implement this
+/// trait, such as [`ArrayRef`], `&T`, and the typed views. They are not
concrete arrays:
+/// they hold or borrow one, and forward methods such as [`Array::data_type`]
to it. Downcasting
+/// methods such as [`Array::as_any`] and
[`downcast_arc`](crate::cast::downcast_arc) refer to the
+/// concrete array itself.
Review Comment:
Updating the `Array` doc comments, to explain more clearly the tight
coupling between `Array::data_type` and downcasting helpers like
`Array::as_any`. Hopefully that makes it easier for users to reason about what
they're dealing with when they see a `&dyn Array`.
##########
arrow-array/src/array/dictionary_array.rs:
##########
@@ -864,6 +868,10 @@ unsafe impl<K: ArrowDictionaryKeyType, V: Sync> Array for
TypedDictionaryArray<'
self.dictionary
}
+ fn into_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
+ Arc::new(self.dictionary.clone())
Review Comment:
Intentionally the same as `to_arc` below, just coerced to `Arc<dyn Any>`
instead of `Arc<dyn Array>`.
##########
arrow-array/src/array/run_array.rs:
##########
@@ -812,6 +816,10 @@ unsafe impl<R: RunEndIndexType, V: Sync> Array for
TypedRunArray<'_, R, V> {
self.run_array
}
+ fn into_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
+ Arc::new(self.run_array.clone())
Review Comment:
Intentionally the same body as `to_arc`, just coerced to a different dyn
trait
##########
arrow-array/src/array/mod.rs:
##########
@@ -550,11 +572,106 @@ unsafe impl Array for ArrayRef {
}
}
+// Ergonomics: allow passing e.g. `&Arc<Int32Array>` to methods that expect
`&dyn Array`
+unsafe impl<T: Array + 'static> Array for Arc<T> {
+ fn as_any(&self) -> &dyn Any {
+ T::as_any(self)
+ }
+
+ fn into_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
+ T::into_any_arc(Arc::clone(&*self))
Review Comment:
Also, I believe this can be `Arc::unwrap_or_clone` like the other.
##########
arrow-array/src/array/boolean_array.rs:
##########
@@ -637,6 +637,10 @@ unsafe impl Array for BooleanArray {
self
}
+ fn into_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
Review Comment:
I'm torn between `into_any_arc` vs. `into_arc_any`. The latter follows the
return type more closely, but the former may align better with `as_any`.
Any strong opinions?
##########
arrow-array/src/array/mod.rs:
##########
@@ -550,11 +572,106 @@ unsafe impl Array for ArrayRef {
}
}
+// Ergonomics: allow passing e.g. `&Arc<Int32Array>` to methods that expect
`&dyn Array`
+unsafe impl<T: Array + 'static> Array for Arc<T> {
+ fn as_any(&self) -> &dyn Any {
+ T::as_any(self)
+ }
+
+ fn into_any_arc(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> {
+ T::into_any_arc(Arc::clone(&*self))
Review Comment:
We can't just copy the body of `to_arc` here, because `T: Array + 'static`
does not guarantee `T` is a one of the concrete array types. It could also be
e.g. `Arc<Arc<FooArray>`, which `to_arc` would return directly but which we
must unwrap to `Arc<FooArray>`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]