Skip to content

Refactor ListArray hashing to consider only sliced values#19500

Merged
alamb merged 2 commits intoapache:mainfrom
Jefffrey:refactor-list-hash
Jan 22, 2026
Merged

Refactor ListArray hashing to consider only sliced values#19500
alamb merged 2 commits intoapache:mainfrom
Jefffrey:refactor-list-hash

Conversation

@Jefffrey
Copy link
Copy Markdown
Contributor

Which issue does this PR close?

N/A

Rationale for this change

When hashing list arrays we hash all bytes of the child array, even if we slice to a certain range of values. Refactor to slice only the needed bytes; also do some general refactors.

What changes are included in this PR?

Refactor list array hashing.

Are these changes tested?

Added test.

Are there any user-facing changes?

No.

@github-actions github-actions Bot added the common Related to common crate label Dec 26, 2025
Comment on lines +518 to +528
let first_offset = array.value_offsets().first().cloned().unwrap_or_default();
let last_offset = array.value_offsets().last().cloned().unwrap_or_default();
let value_bytes_len = (last_offset - first_offset).as_usize();
let mut values_hashes = vec![0u64; value_bytes_len];
create_hashes(
[array
.values()
.slice(first_offset.as_usize(), value_bytes_len)],
random_state,
&mut values_hashes,
)?;
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.

Main change here

&mut values_hashes,
)?;

if array.null_count() > 0 {
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.

Switching to this count probably doesn't affect much (how often do we see a nullbuffer thats present but has all bits valid?) but it's consistent with how we check for nulls in the other functions

)?;

if array.null_count() > 0 {
for (i, (start, stop)) in array.value_offsets().iter().tuple_windows().enumerate()
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.

Using tuple_windows from itertools makes this more ergonomic

@Jefffrey Jefffrey marked this pull request as ready for review December 26, 2025 09:22
let first_offset = array.value_offsets().first().cloned().unwrap_or_default();
let last_offset = array.value_offsets().last().cloned().unwrap_or_default();
let value_bytes_len = (last_offset - first_offset).as_usize();
let mut values_hashes = vec![0u64; value_bytes_len];
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.

This allocates a fresh values_hashes Vec for every list column hashed.
Could we reuse a buffer (similar to HASH_BUFFER above) or early-return for empty value_bytes_len to trim repeated allocations?

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.

Do you mean reuse a buffer between invocations of hash_list_array() itself? We could look into that, but I'd say thats beyond the scope of changes here especially as the other functions don't do this, so might need more plumbing etc.

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 agree removing the allocation would be good -- however, the existing code also creates vec! so I don't think this change is any worse in this regard

@alamb
Copy link
Copy Markdown
Contributor

alamb commented Jan 22, 2026

Thanks @Jefffrey and @kosiew

@alamb alamb added this pull request to the merge queue Jan 22, 2026
Merged via the queue into apache:main with commit 7cf63f1 Jan 22, 2026
31 checks passed
@Jefffrey Jefffrey deleted the refactor-list-hash branch January 23, 2026 03:07
de-bgunter pushed a commit to de-bgunter/datafusion that referenced this pull request Mar 24, 2026
## 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 apache#123` indicates that this PR will close issue apache#123.
-->

N/A

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

When hashing list arrays we hash all bytes of the child array, even if
we slice to a certain range of values. Refactor to slice only the needed
bytes; also do some general refactors.

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

Refactor list array hashing.

## 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)?
-->

Added test.

## Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

No.

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common Related to common crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants