Skip to content

fix: invoke AsRef once in bytes dictionary builder - #12463

Merged
Jefffrey merged 1 commit into
apache:mainfrom
compiler-segfault:fix/dict-builder-hash-store-same-bytes
Nov 26, 2026
Merged

Jefffrey merged 1 commit into
apache:mainfrom
compiler-segfault:fix/dict-builder-hash-store-same-bytes

Conversation

@compiler-segfault

Copy link
Copy Markdown
Contributor

Fixes #12424.

GenericByteDictionaryBuilder::get_or_insert_key hashed value.as_ref() to look the value up, then called value.as_ref() a second time inside the insert closure through storage.append_value(value). AsRef is not required to be idempotent, so the stored bytes could differ from the bytes that were hashed: the dictionary then holds a value filed under the wrong hash, and a later append of the value that was actually hashed adds a duplicate entry.

The builder now stores the same &T::Native that it hashed. This is the same fix #12397 made for FixedSizeBinaryDictionaryBuilder, which had the identical pattern.

Verification:

  • New test test_dictionary_builder_hashes_and_stores_the_same_bytes uses an AsRef<str> that returns a different value on each call; on main it fails with left: 2, right: 1 (the dictionary ends up with two entries and keys [0, 1] instead of one entry with [0, 0]). It passes with this change.
  • cargo test -p arrow-array: 767 passed, 1 ignored.
  • cargo fmt --all -- --check and cargo clippy -p arrow-array --all-targets --all-features -- -D warnings are clean.

The text above was drafted with an AI assistant; I ran the tests myself.

@boring-cyborg boring-cyborg Bot added the arrow-core Core crates related to representing arrow data label Nov 25, 2026

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.

we should just shadow this like

        let value: &T::Native = value.as_ref();

Comment on lines +278 to +279
// Store the bytes that were hashed above, not a second s_ref()
// of alue, which is not required to be idempotent.

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.

Suggested change
// Store the bytes that were hashed above, not a second �s_ref()
// of �alue, which is not required to be idempotent.

not necessary


/// A legal `AsRef<str>` whose result changes between calls, so the value
/// that is hashed can differ from the value that is stored.
struct AlternatingValue(Cell<u32>);

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 is overkill and unnecessary, please remove these tests

@compiler-segfault
compiler-segfault force-pushed the fix/dict-builder-hash-store-same-bytes branch from b60e3ba to 9a55ab6 Compare November 26, 2026 08:12
`GenericByteDictionaryBuilder::get_or_insert_key` hashed `value.as_ref()` and
then called `value.as_ref()` again through `storage.append_value(value)`. `AsRef`
is not required to be idempotent, so the stored bytes could differ from the
hashed bytes: the dictionary then holds a value filed under the wrong hash, and
a later append of the value that was actually hashed adds a duplicate entry.

Store the same `&T::Native` that was hashed, the way apache#12397 did it for the
fixed-size binary dictionary builder.

Fixes apache#12424.
@compiler-segfault
compiler-segfault force-pushed the fix/dict-builder-hash-store-same-bytes branch from 9a55ab6 to c4b2754 Compare November 26, 2026 08:18
@compiler-segfault

Copy link
Copy Markdown
Contributor Author

Applied the shadowing as suggested: the argument is now rebound to &T::Native so hash_one and the stored value use the same reference. Dropped the comment and the added tests.

cargo test -p arrow-array --lib dictionary -> 71 passed, cargo fmt -p arrow-array -- --check clean.

@Jefffrey Jefffrey changed the title fix: dictionary builder hashes and stores the same bytes fix: invoke AsRef once in bytes dictionary builder Nov 26, 2026
@Jefffrey Jefffrey added the bug Squash those bugs label Nov 26, 2026
@Jefffrey
Jefffrey added this pull request to the merge queue Nov 26, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 26, 2026
@Jefffrey
Jefffrey added this pull request to the merge queue Nov 26, 2026
Merged via the queue into apache:main with commit e6977de Nov 26, 2026
22 checks passed
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 bug Squash those bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GenericByteDictionaryBuilder::append calls as_ref() twice, so the stored value can differ from the hashed value

2 participants