Repository navigation
Description
Describe the bug
GenericByteDictionaryBuilder::get_or_insert_key (used by append, append_value and append_option) calls value.as_ref() once to hash and look up the value, then calls it again inside the insert closure through storage.append_value(value). If as_ref() does not return the same bytes on both calls, the stored bytes are not the bytes that were hashed. The dictionary then contains a value under the wrong hash, and a later append of the value that was really hashed adds a duplicate entry.
This is the same pattern that #12397 fixed in FixedSizeBinaryDictionaryBuilder (and #12408 in FixedSizeBinaryBuilder). The generic bytes builder was not changed there.
To Reproduce
use arrow_array::builder::StringDictionaryBuilder;
use arrow_array::types::Int32Type;
use std::cell::Cell;
// A legal AsRef<str> whose as_ref() is not idempotent (interior mutability).
struct Flip(Cell<u32>);
impl AsRef<str> for Flip {
fn as_ref(&self) -> &str {
let n = self.0.get();
self.0.set(n + 1);
if n % 2 == 0 { "a" } else { "b" }
}
}
fn main() {
let mut b = StringDictionaryBuilder::<Int32Type>::new();
// The first as_ref() call yields "a" (hashed/looked up); the insert closure calls it again.
b.append_value(Flip(Cell::new(0)));
b.append_value("a");
let arr = b.finish();
let vals = arr.values();
let n = vals.len();
let strs = vals
.as_any()
.downcast_ref::<arrow_array::StringArray>()
.unwrap();
let got: Vec<&str> = (0..n).map(|i| strs.value(i)).collect();
println!("EXPECTED: dictionary [\"a\"] (both appends are \"a\" per the hashed as_ref)");
println!("ACTUAL: dictionary {:?}, keys {:?}", got, arr.keys());
if got != ["a"] {
std::process::exit(1);
}
}Output on main (6747ecd), same on 60.2.0:
EXPECTED: dictionary ["a"] (both appends are "a" per the hashed as_ref)
ACTUAL: dictionary ["b", "a"], keys PrimitiveArray<Int32>
[
0,
1,
]
Expected behavior
The bytes that are hashed and looked up are the bytes that get stored, so the dictionary is ["a"] and both keys are 0. append documents that it returns "an existing index if already present in the values array or a new index if the value is appended". FixedSizeBinaryDictionaryBuilder already reads as_ref() once and appends the bytes it hashed (fixed_size_binary_dictionary_builder.rs, after #12397).
Additional context
Root cause: arrow-array/src/builder/generic_bytes_dictionary_builder.rs, get_or_insert_key reads value.as_ref() for the hash and equality check, and the insert closure then calls storage.append_value(value), which reads it again. A fix would append the bytes already obtained instead of value.
This only matters for AsRef types that are not pure; I found no panic or unsafety, only a wrong or duplicated dictionary. Built with rustc 1.99.1 (Linux, debug).
I'm happy to send a fix with a regression test if that helps.
Willingness to contribute: I would like to contribute a fix for this bug, but request guidance from the community
Activity
fix: dictionary builder hashes and stores the same bytes
fix: dictionary builder hashes and stores the same bytes
fix: invoke `AsRef` once in bytes dictionary builder (apache#12463)