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

オープン 初心者向け
#12,424 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
80/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust
領域
data

調査の方向性

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

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/arrow-rs のほかの issue

apache/arrow-rs の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。