You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/11/15 03:50:01 UTC

[GitHub] [arrow-rs] tustvold opened a new pull request, #3117: Add downcast_array (#2901)

tustvold opened a new pull request, #3117:
URL: https://github.com/apache/arrow-rs/pull/3117

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #2901
   
   # Rationale for this change
    
   <!--
   Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
   Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   -->
   
   Make this technique more discoverable
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are there any user-facing changes?
   
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!---
   If there are any breaking changes to public APIs, please add the `breaking change` label.
   -->
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] viirya commented on a diff in pull request #3117: Add downcast_array (#2901)

Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #3117:
URL: https://github.com/apache/arrow-rs/pull/3117#discussion_r1022328453


##########
arrow-array/src/cast.rs:
##########
@@ -550,6 +551,38 @@ array_downcast_fn!(as_union_array, UnionArray);
 array_downcast_fn!(as_map_array, MapArray);
 array_downcast_fn!(as_decimal_array, Decimal128Array);
 
+/// Downcasts a `dyn Array` to a concrete type
+///
+/// ```
+/// # use arrow_array::{BooleanArray, Int32Array, RecordBatch, StringArray};
+/// # use arrow_array::cast::downcast_array;
+/// struct ConcreteBatch {
+///     col1: Int32Array,
+///     col2: BooleanArray,
+///     col3: StringArray,
+/// }
+///
+/// impl ConcreteBatch {
+///     fn new(batch: &RecordBatch) -> Self {
+///         Self {
+///             col1: downcast_array(batch.column(0).as_ref()),
+///             col2: downcast_array(batch.column(1).as_ref()),
+///             col3: downcast_array(batch.column(2).as_ref()),
+///         }
+///     }
+/// }
+/// ```
+///
+/// # Panics
+///
+/// Panics if array is not of the correct data type
+pub fn downcast_array<T>(array: &dyn Array) -> T
+where
+    T: From<ArrayData>,
+{
+    T::from(array.data().clone())

Review Comment:
   Okay. Let's merge this so I can play with it in #3115.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3117: Add downcast_array (#2901)

Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #3117:
URL: https://github.com/apache/arrow-rs/pull/3117#discussion_r1022295908


##########
arrow-array/src/cast.rs:
##########
@@ -550,6 +551,38 @@ array_downcast_fn!(as_union_array, UnionArray);
 array_downcast_fn!(as_map_array, MapArray);
 array_downcast_fn!(as_decimal_array, Decimal128Array);
 
+/// Downcasts a `dyn Array` to a concrete type
+///
+/// ```
+/// # use arrow_array::{BooleanArray, Int32Array, RecordBatch, StringArray};
+/// # use arrow_array::cast::downcast_array;
+/// struct ConcreteBatch {
+///     col1: Int32Array,
+///     col2: BooleanArray,
+///     col3: StringArray,
+/// }
+///
+/// impl ConcreteBatch {
+///     fn new(batch: &RecordBatch) -> Self {
+///         Self {
+///             col1: downcast_array(batch.column(0).as_ref()),
+///             col2: downcast_array(batch.column(1).as_ref()),
+///             col3: downcast_array(batch.column(2).as_ref()),
+///         }
+///     }
+/// }
+/// ```
+///
+/// # Panics
+///
+/// Panics if array is not of the correct data type
+pub fn downcast_array<T>(array: &dyn Array) -> T
+where
+    T: From<ArrayData>,
+{
+    T::from(array.data().clone())

Review Comment:
   Provided the source `Array` is dropped first, and there are no other references to the underlying `Buffer`, e.g. as a result of slicing the array, it will work. 
   
   Edit: There isn't really a way to avoid this, even approaches using AsDynAny, etc... will have the same complication



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] alamb commented on pull request #3117: Add downcast_array (#2901)

Posted by GitBox <gi...@apache.org>.
alamb commented on PR #3117:
URL: https://github.com/apache/arrow-rs/pull/3117#issuecomment-1315556472

   👌 


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold merged pull request #3117: Add downcast_array (#2901)

Posted by GitBox <gi...@apache.org>.
tustvold merged PR #3117:
URL: https://github.com/apache/arrow-rs/pull/3117


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3117: Add downcast_array (#2901)

Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #3117:
URL: https://github.com/apache/arrow-rs/pull/3117#discussion_r1022295908


##########
arrow-array/src/cast.rs:
##########
@@ -550,6 +551,38 @@ array_downcast_fn!(as_union_array, UnionArray);
 array_downcast_fn!(as_map_array, MapArray);
 array_downcast_fn!(as_decimal_array, Decimal128Array);
 
+/// Downcasts a `dyn Array` to a concrete type
+///
+/// ```
+/// # use arrow_array::{BooleanArray, Int32Array, RecordBatch, StringArray};
+/// # use arrow_array::cast::downcast_array;
+/// struct ConcreteBatch {
+///     col1: Int32Array,
+///     col2: BooleanArray,
+///     col3: StringArray,
+/// }
+///
+/// impl ConcreteBatch {
+///     fn new(batch: &RecordBatch) -> Self {
+///         Self {
+///             col1: downcast_array(batch.column(0).as_ref()),
+///             col2: downcast_array(batch.column(1).as_ref()),
+///             col3: downcast_array(batch.column(2).as_ref()),
+///         }
+///     }
+/// }
+/// ```
+///
+/// # Panics
+///
+/// Panics if array is not of the correct data type
+pub fn downcast_array<T>(array: &dyn Array) -> T
+where
+    T: From<ArrayData>,
+{
+    T::from(array.data().clone())

Review Comment:
   Provided the source `Array` is dropped first, and there are no other references to the underlying `Buffer`, e.g. as a result of slicing the array, it will work. 



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] viirya commented on a diff in pull request #3117: Add downcast_array (#2901)

Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #3117:
URL: https://github.com/apache/arrow-rs/pull/3117#discussion_r1022293554


##########
arrow-array/src/cast.rs:
##########
@@ -550,6 +551,38 @@ array_downcast_fn!(as_union_array, UnionArray);
 array_downcast_fn!(as_map_array, MapArray);
 array_downcast_fn!(as_decimal_array, Decimal128Array);
 
+/// Downcasts a `dyn Array` to a concrete type
+///
+/// ```
+/// # use arrow_array::{BooleanArray, Int32Array, RecordBatch, StringArray};
+/// # use arrow_array::cast::downcast_array;
+/// struct ConcreteBatch {
+///     col1: Int32Array,
+///     col2: BooleanArray,
+///     col3: StringArray,
+/// }
+///
+/// impl ConcreteBatch {
+///     fn new(batch: &RecordBatch) -> Self {
+///         Self {
+///             col1: downcast_array(batch.column(0).as_ref()),
+///             col2: downcast_array(batch.column(1).as_ref()),
+///             col3: downcast_array(batch.column(2).as_ref()),
+///         }
+///     }
+/// }
+/// ```
+///
+/// # Panics
+///
+/// Panics if array is not of the correct data type
+pub fn downcast_array<T>(array: &dyn Array) -> T
+where
+    T: From<ArrayData>,
+{
+    T::from(array.data().clone())

Review Comment:
   Will this clone make us unable to `Arc::try_unwrap` later?



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] ursabot commented on pull request #3117: Add downcast_array (#2901)

Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #3117:
URL: https://github.com/apache/arrow-rs/pull/3117#issuecomment-1314790838

   Benchmark runs are scheduled for baseline = b0b5d8b4f57049d72403a749e109829ff35a6b64 and contender = 5c2801d08edbe1573d6c23c55b7333f0aedbdc3c. 5c2801d08edbe1573d6c23c55b7333f0aedbdc3c is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
   Conbench compare runs links:
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/e15098cc6bf947cfa34db1f7eee3d9bc...7026aee09a0f4856a0a5187d2fc1a10f/)
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/5f50582867c74deb9dff89cfaf71bedb...e76fed2933d043a690b94974ef013757/)
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/de92ffc536804c9bacfae77ac4339240...19658a612343473492387593b599a6bc/)
   [Skipped :warning: Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/b446711eb3cd4a2a8a11617fd415c630...be5733d904bd41d4af74c23e5d8a00f2/)
   Buildkite builds:
   Supported benchmarks:
   ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
   test-mac-arm: Supported benchmark langs: C++, Python, R
   ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
   ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #3117: Add downcast_array (#2901)

Posted by GitBox <gi...@apache.org>.
tustvold commented on code in PR #3117:
URL: https://github.com/apache/arrow-rs/pull/3117#discussion_r1022291987


##########
arrow-array/src/cast.rs:
##########
@@ -550,6 +551,38 @@ array_downcast_fn!(as_union_array, UnionArray);
 array_downcast_fn!(as_map_array, MapArray);
 array_downcast_fn!(as_decimal_array, Decimal128Array);
 
+/// Downcasts a `dyn Array` to a concrete type
+///
+/// ```
+/// # use arrow_array::{BooleanArray, Int32Array, RecordBatch, StringArray};
+/// # use arrow_array::cast::downcast_array;
+/// struct ConcreteBatch {
+///     col1: Int32Array,
+///     col2: BooleanArray,
+///     col3: StringArray,
+/// }
+///
+/// impl ConcreteBatch {
+///     fn new(batch: &RecordBatch) -> Self {
+///         Self {
+///             col1: downcast_array(batch.column(0).as_ref()),
+///             col2: downcast_array(batch.column(1).as_ref()),
+///             col3: downcast_array(batch.column(2).as_ref()),
+///         }
+///     }
+/// }
+/// ```
+///
+/// # Panics
+///
+/// Panics if array is not of the correct data type
+pub fn downcast_array<T>(array: &dyn Array) -> T
+where
+    T: From<ArrayData>,
+{
+    T::from(array.data().clone())

Review Comment:
   There may be some mechanism to avoid this clone, if/when we work this out this can be optimised. In practice, this clone should be irrelevant, as it all the underlying buffers are reference counted



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org