`GenericByteDictionaryBuilder::append` calls `as_ref()` twice, so the stored value can differ from the hashed value
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
arrow-array/src/builder/generic_bytes_dictionary_builder.rs の get_or_insert_key から始めます。これは value.as_ref() をハッシュして比較し、その後 value を挿入クロージャに渡し、そこで storage.append_value がもう一度読み取ります。fixed_size_binary_dictionary_builder.rs(#12397 で修正済み)をモデルにします。借用したバイト列を一度取得し、元の value ではなくそのバイト列を追加します。issue の再現を arrow-array の builder テストにおける回帰テストに変えます。完了とは、結果の辞書が ["a"] でキーが [0, 0] であり、既存の builder テストが引き続き通ることを意味します。
索引モデルが issue の本文から書いたものです。
説明
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 (6747ecda), 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
- 主要言語
- Rust
- スター
- 3.7k
- フォーク
- 1.5k
- 平均マージ
- 2日 11時間
- マージ済み PR(30日)
- 195
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
apache/arrow-rs のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
[Variant] `variant_get` string and binary cast errors show Rust builder type names再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 61/100
メンテナーはふだん 1 日以内に返信
-
parquet: GZip/Brotli page decompression is not bounded by the page's uncompressed size対応中かも @Subodh-17 が 5 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
development-process
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
apache/arrow-rs#12145 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
parquet: panic "Decoder for dict should have been set" reading a primitive column whose dictionary page is missing対応中かも @cryptus-neoxys が 12 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
apache/arrow-rs#12129 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
apache/arrow-rs の issue をすべて見る
似ている issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
symposium-dev/symposium#530 ·
メンテナーはふだん 5 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
topgrade-rs/topgrade#2540 ·
メンテナーはふだん 1 日以内に返信
-
Invalid JSON options for ngram and edge_ngram silently use defaults対応中かも @Tilendlesa が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
noemaforge/vimanam#153 ·
メンテナーはふだん 1 日以内に返信