Skip to content

Define and use Array::to_arc - #12420

Merged
scovich merged 3 commits into
apache:mainfrom
scovich:array-to-arc
Nov 24, 2026
Merged

scovich merged 3 commits into
apache:mainfrom
scovich:array-to-arc

Conversation

@scovich

@scovich scovich commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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_arc method that returns a shallow clone of its receiver:

  • Provided impl is self.slice(0, self.len()), to avoid a breaking change
  • Concrete T: Array does Arc::new(self.clone())
  • ArrayRef does Arc::clone(self)
  • Wrapper types (&T, TypedDictionaryArray, TypedRunArray) forward to the underlying type

Several 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.

@boring-cyborg boring-cyborg Bot added arrow-core Core crates related to representing arrow data arrow-kernels Crates that transform arrow data (e.g. compute, cast) labels Nov 23, 2026
@scovich
scovich requested review from Jefffrey and alamb November 23, 2026 15:10
@scovich

scovich commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Questions for reviewers:

  1. Should we call it clone_as_arc instead? (initial reaction: no, shorter to_arc is clear enough)
  2. Should we just require Array: Clone? (initial reaction: no, breaking change just to support a provided method that becomes a footgun for e.g. the wrapper types)

@alamb

alamb commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Should we call it clone_as_arc instead? (initial reaction: no, shorter to_arc is clear enough)

I agree to_arc is better

Should we just require Array: Clone? (initial reaction: no, breaking change just to support a provided method that becomes a footgun for e.g. the wrapper types)

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 alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread arrow-array/src/array/mod.rs Resolved
assert!(Arc::ptr_eq(&array_ref.to_arc(), &array_ref));
assert_eq!(Array::to_arc(&&primitive).as_ref(), &primitive);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add a test that takes &dyn Array and passes in an &Arc<&dyn Array> -- and confirm it returns the same Arc?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add a test that takes &dyn Array and 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));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this to the doctest (as well as updating the doc comment to make clear that to_arc uses Arc::clone when possible).

@mbutrovich

Copy link
Copy Markdown
Contributor

Anything for Ryan Johnson! Let me take a quick look, but the description makes it sound like a good idea.

@mbutrovich mbutrovich left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Comment thread arrow-array/src/array/mod.rs Resolved
Comment thread arrow-array/src/array/mod.rs Resolved
@mbutrovich

Copy link
Copy Markdown
Contributor

I ran arrow-pbt at 055ddcb against 08a6e4fb. The properties that reach the changed call sites passed at 10 seeds of 10,000 cases each, and all properties passed at 3 seeds of 1,000:

ARROW_PBT_REASONS=1 cargo run -q -- run cast,cast_text,temporal_cast,nullif,shift,concat 1-10 10000

The cases took same-type cast 8,022 times, shift by 0 13,623 times, nullif of an empty or Null array 5,009 times, and single-array concat 25,177 times. No property casts to run-end encoded, so the to_arc in that branch isn't covered.

One output change the properties can't see: make_array(array.to_data()) dropped a null buffer with no nulls (data.rs#L2465), and to_arc keeps it. So same-type cast, shift by 0, and nullif of an empty array now return the input's buffer. Nothing documents either behavior, and the only code I found that treats a present buffer as nulls is RunArray::try_new, which I filed as #12427.

@mbutrovich mbutrovich left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @scovich!

@Jefffrey Jefffrey added the enhancement New feature/addition label Nov 24, 2026
@Jefffrey Jefffrey changed the title Define and use Array::to_arc Define and use Array::to_arc Nov 24, 2026

@Jefffrey Jefffrey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this supersede the need for #12318?

@scovich

scovich commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

would this supersede the need for #12318?

AFAICT, that PR made three changes, all getting rid of make_array calls:

  1. Early return for same type: use slice
  2. Int64 -> Timestamp conversions: use make_timestamp_array
  3. Int64 -> Duration conversions: use make_duration_array

AFAIK this PR only addresses the first item?

@Jefffrey

Copy link
Copy Markdown
Contributor

would this supersede the need for #12318?

AFAICT, that PR made three changes, all getting rid of make_array calls:

1. Early return for same type: use `slice`

2. Int64 -> Timestamp conversions: use `make_timestamp_array`

3. Int64 -> Duration conversions: use `make_duration_array`

AFAIK this PR only addresses the first item?

the last two items are because the make_array (and also slice) paths were slow, since we didnt have a fast way to retrieve an ArrayRef from a &dyn Array; if we had this fast way we wouldn't need to specialize the cast paths as #12318 does (which i was hoping this PR might provide, but maybe im misunderstanding)

@scovich

scovich commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

would this supersede the need for #12318?

AFAICT, that PR made three changes, all getting rid of make_array calls:

1. Early return for same type: use `slice`

2. Int64 -> Timestamp conversions: use `make_timestamp_array`

3. Int64 -> Duration conversions: use `make_duration_array`

AFAIK this PR only addresses the first item?

the last two items are because the make_array (and also slice) paths were slow, since we didnt have a fast way to retrieve an ArrayRef from a &dyn Array; if we had this fast way we wouldn't need to specialize the cast paths as #12318 does (which i was hoping this PR might provide, but maybe im misunderstanding)

Oh, because then the recursive cast_with_options call from the existing (_, Timestamp(..)) match arm is fast? That could very well be, now that you mention it.

@scovich
scovich enabled auto-merge November 24, 2026 22:13
@scovich
scovich added this pull request to the merge queue Nov 24, 2026
Merged via the queue into apache:main with commit ddaedbb Nov 24, 2026
22 checks passed
Mola-maker pushed a commit to Mola-maker/arrow-rs that referenced this pull request Nov 27, 2026
# 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow-core Core crates related to representing arrow data arrow-kernels Crates that transform arrow data (e.g. compute, cast) enhancement New feature/addition

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants