Repository navigation
Define and use Array::to_arc - #12420
Conversation
|
Questions for reviewers:
|
I agree
Yeah, and I don't think cloning is really what we want here -- we want an Arc<...> and if we already have an Arc<...> (which is common) we can just return that |
alamb
left a comment
There was a problem hiding this comment.
I personally think this is great. Thank you @scovich
cc @Rich-T-kid and @Jefffrey and @mbutrovich as I think you are interested in this type of thing
| assert!(Arc::ptr_eq(&array_ref.to_arc(), &array_ref)); | ||
| assert_eq!(Array::to_arc(&&primitive).as_ref(), &primitive); | ||
| } | ||
|
|
There was a problem hiding this comment.
Can we also add a test that takes &dyn Array and passes in an &Arc<&dyn Array> -- and confirm it returns the same Arc?
There was a problem hiding this comment.
Can we also add a test that takes
&dyn Arrayand passes in an&Arc<&dyn Array>-- and confirm it returns the same Arc?
Arc<&dyn Array> doesn't implement Array, so I read this as &ArrayRef passed as &dyn Array. The existing array_ref.to_arc() call resolves to <ArrayRef as Array>::to_arc at compile time, so it doesn't go through a &dyn Array. Something like this would cover it:
fn to_arc_dyn(array: &dyn Array) -> ArrayRef {
array.to_arc()
}
assert!(Arc::ptr_eq(&to_arc_dyn(&array_ref), &array_ref));There was a problem hiding this comment.
I added this to the doctest (as well as updating the doc comment to make clear that to_arc uses Arc::clone when possible).
|
Anything for Ryan Johnson! Let me take a quick look, but the description makes it sound like a good idea. |
mbutrovich
left a comment
There was a problem hiding this comment.
Thanks @scovich. This is a nice cleanup, and providing a default through slice keeps the new method additive. I filed apache/datafusion#27687 and apache/datafusion-comet#7755 to switch the downstream workarounds to to_arc once it ships.
I left a reply to one of @alamb's comments, and some minor suggestions on docs and testing. Thanks again!
|
I ran arrow-pbt at ARROW_PBT_REASONS=1 cargo run -q -- run cast,cast_text,temporal_cast,nullif,shift,concat 1-10 10000The cases took same-type One output change the properties can't see: |
mbutrovich
left a comment
There was a problem hiding this comment.
LGTM, thanks @scovich!
AFAICT, that PR made three changes, all getting rid of
AFAIK this PR only addresses the first item? |
the last two items are because the |
Oh, because then the recursive |
# Which issue does this PR close? <!-- It is not necessary to raise an issue, but it is encouraged if it is a non-trivial enhancement/bug. If this PR relates to an existing issue, but does not fully resolve it, please change the wording below from "Closes #NNN" to "Relates to #NNN" or "Part of #NNN" to avoid false closing an issue. --> - Relates to apache#9128, apache#11717 # 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. Please try to explain the problem you are trying to solve in terms of the user-visible behavior, rather than the implementation. --> `make_array(array.to_data())` is a bit expensive (allocates buffer list, validates, drops). # 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. --> - ~~Special-case i64 casts to avoid `ArrayData`.~~ - ~~Use `Array::slice` instead of `make_array(array.to_data)`~~ superseded by apache#12420 - Two new benches and a test for i64 duration cast. # Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests, if so identify these tests)? If this PR claims a performance improvement, please include evidence such as benchmark results. DO NOT simply paste a list of commands ran to verify the code; this is what our CI is for and it pollutes the PR. ONLY explain if testing was included with this PR, or why it wasn't. --> | bench | main | https://github.com/apache/arrow-rs/pull/12318/changes/f626d103b74d4ec13ea6443c0758178167b142f5| change | |---|---:|---:|---:| | cast i64 to timestamp_ns 512 | 129.2 ns | 36.2 ns | −72% | | cast i64 to duration_ns 512 | 119.2 ns | 33.1 ns | −72% | (the array length does not matter, the overhead is per array cast) The bench applies within 5% after apache#12420. # Are there any significant user-facing changes? <!-- Significant user-facing changes include: - Breaking public API change - Significant change to existing functionality (e.g. function returns something different) These need to be explicitly called out to ensure we have proper documentation if required and to ensure we follow proper semantic versioning. DO NOT restate the changes of this PR; if a bug was fixed and already explained above, we don't need this to be restated as a user-facing change. --> No.
Which issue does this PR close?
Rationale for this change
There's no really clean way to clone a
&dyn Array. The two most common workarounds are:make_array(array.to_data())array.slice(0, array.len())The latter is cheaper than the former, but both do extra work and both are roundabout ways of obtaining a shallow copy of an array.
What changes are included in this PR?
New
Array::to_arcmethod that returns a shallow clone of its receiver:self.slice(0, self.len()), to avoid a breaking changeT: ArraydoesArc::new(self.clone())ArrayRefdoesArc::clone(self)&T,TypedDictionaryArray,TypedRunArray) forward to the underlying typeSeveral call sites that were relying on workarounds are updated to use the new method.
Are these changes tested?
A new unit test validates each flavor of implementation. I'm on the fence about how much value it really adds, but it's only ~20 LoC so I kept it.
Are there any significant user-facing changes?
See above.
AI usage disclosure
This PR is a significantly cleaned up version of a vibe-coded rough draft.
The PR description is mine alone, and I can answer for every line of the diff.