Repository navigation
Commit 5effeb5
authored
speed up dict cast key width (apache#11502)
# 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.
-->
- works towards apache/datafusion#24111
- for apache/datafusion#24117 we need to
reduce the cost of selecting a key type that is too small for a given
cardinality. this means making it as cheap as possible to bump the key
type of a dictionary array.
# Rationale for this change
`Dictionary(K1, V) → Dictionary(K2, V)` casts (changing only the key
width) currently take the general path in
`dictionary_to_dictionary_cast`, which does three things that are
unnecessary when the value type is unchanged:
1. Calls `cast_with_options` on the values array — dispatch overhead
even for identity casts, real work for near-identity casts.
2. Rebuilds the dictionary's `ArrayData` graph via
`into_builder().child_data(...).build_unchecked()`.
3. Wraps the result in `DictionaryArray::<K2>::from(data)`, which runs
an `O(N_rows)` bounds-check pass over every key.
**If the source dict was valid and the key cast didn't overflow, the
result is valid by construction**
<!--
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.
-->
# What changes are included in this PR?
A fast path in `dictionary_to_dictionary_cast`: when the source value
type equals the target value type (and the source isn't a nested dict),
delegate to `cast_keys_only`, which casts the keys array and reuses the
values `Arc` via `DictionaryArray::new_unchecked`. Narrowing overflow
remains a hard error regardless of `cast_options.safe`, same contract as
the general path.
<!--
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 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.
-->
# Are there any significant user-facing changes?
no
<!--
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.
-->1 parent 6db2d4f commit 5effeb5
2 files changed
Lines changed: 79 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
135 | 142 | | |
136 | 143 | | |
137 | 144 | | |
| |||
199 | 206 | | |
200 | 207 | | |
201 | 208 | | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
202 | 261 | | |
203 | 262 | | |
204 | 263 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | | - | |
| 28 | + | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| |||
673 | 673 | | |
674 | 674 | | |
675 | 675 | | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
676 | 694 | | |
677 | 695 | | |
678 | 696 | | |
| |||
0 commit comments