Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions cpp/src/arrow/compute/kernels/vector_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,11 @@ class NullHashKernel : public HashKernel {

class DictionaryHashKernel : public HashKernel {
public:
explicit DictionaryHashKernel(std::unique_ptr<HashKernel> indices_kernel,
std::shared_ptr<DataType> dictionary_value_type)
DictionaryHashKernel(std::unique_ptr<HashKernel> indices_kernel,
std::shared_ptr<DataType> index_type,
std::shared_ptr<DataType> dictionary_value_type)
: indices_kernel_(std::move(indices_kernel)),
index_type_(std::move(index_type)),
dictionary_value_type_(std::move(dictionary_value_type)) {}

Status Reset() override { return indices_kernel_->Reset(); }
Expand Down Expand Up @@ -478,6 +480,23 @@ class DictionaryHashKernel : public HashKernel {
auto in_array = arr.ToArray();
const auto& in_dict_array =
arrow::internal::checked_cast<const DictionaryArray&>(*in_array);
if (index_type_->bit_width() < 32) {
// The transposed indices keep the input index type, which may be too narrow
// for the unified dictionary. Only the indices actually used (non-null)
// matter, not the size of the unified dictionary: check them before
// narrowing, as the transposition itself would silently wrap around.
ARROW_ASSIGN_OR_RAISE(
auto wide,
in_dict_array.Transpose(::arrow::dictionary(int32(), dictionary_value_type_),
in_dict_array.dictionary(), transpose));
ArraySpan wide_indices(
*checked_cast<const DictionaryArray&>(*wide).indices()->data());
if (!::arrow::internal::IntegersCanFit(wide_indices, *index_type_).ok()) {
return Status::Invalid(
"These dictionaries cannot be combined. The unified dictionary requires "
"a larger index type.");
}
}
ARROW_ASSIGN_OR_RAISE(
auto tmp, in_dict_array.Transpose(arr.type->GetSharedPtr(),
in_dict_array.dictionary(), transpose));
Expand Down Expand Up @@ -522,6 +541,7 @@ class DictionaryHashKernel : public HashKernel {
private:
std::unique_ptr<HashKernel> indices_kernel_;
std::shared_ptr<Array> first_dictionary_;
std::shared_ptr<DataType> index_type_;
std::shared_ptr<DataType> dictionary_value_type_;
std::unique_ptr<DictionaryUnifier> dictionary_unifier_;
};
Expand Down Expand Up @@ -597,7 +617,7 @@ Result<std::unique_ptr<KernelState>> DictionaryHashInit(KernelContext* ctx,
ARROW_ASSIGN_OR_RAISE(auto indices_hasher,
GetHashInit<Action>(dict_type.index_type()->id())(ctx, args));
return std::make_unique<DictionaryHashKernel>(
checked_pointer_cast<HashKernel>(std::move(indices_hasher)),
checked_pointer_cast<HashKernel>(std::move(indices_hasher)), dict_type.index_type(),
dict_type.value_type());
}

Expand Down
69 changes: 69 additions & 0 deletions cpp/src/arrow/compute/kernels/vector_hash_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <functional>
#include <locale>
#include <memory>
#include <numeric>
#include <stdexcept>
#include <string>
#include <utility>
Expand All @@ -30,6 +31,7 @@

#include "arrow/array.h"
#include "arrow/array/builder_decimal.h"
#include "arrow/array/concatenate.h"
#include "arrow/buffer.h"
#include "arrow/chunked_array.h"
#include "arrow/status.h"
Expand Down Expand Up @@ -764,6 +766,73 @@ TEST_F(TestHashKernel, DictionaryUniqueAndValueCounts) {
}
}

TEST_F(TestHashKernel, DictionaryUniqueAndValueCountsIndexOverflow) {
// GH-45010: chunks with different dictionaries are unified and their indices
// transposed into the unified dictionary, but the output keeps the input index type.
// Only the indices actually used must fit in that type, not the whole unified
// dictionary.
auto dict_ty = dictionary(int8(), int64());
// Dictionary [first_value, first_value + length)
auto make_dict = [](int64_t first_value, int64_t length) {
std::vector<int64_t> values(length);
std::iota(values.begin(), values.end(), first_value);
std::shared_ptr<Array> dict;
ArrayFromVector<Int64Type, int64_t>(values, &dict);
return dict;
};
auto make_chunk = [&](const std::string& indices_json,
const std::shared_ptr<Array>& dict) {
return std::make_shared<DictionaryArray>(dict_ty, ArrayFromJSON(int8(), indices_json),
dict);
};
// The unified dictionary: all of dict_1 followed by all of dict_2
auto unified_dict = [&](const std::shared_ptr<Array>& dict_1,
const std::shared_ptr<Array>& dict_2) {
return *Concatenate({dict_1, dict_2});
};

auto dict_64 = make_dict(0, 64);
{
// Valid input: 64 + 65 = 129 unified entries (more than int8 can address), but the
// used indices are 0 and 64 which fit
auto dict_65 = make_dict(100, 65);
auto input = *ChunkedArray::Make(
{make_chunk("[0]", dict_64), make_chunk("[0]", dict_65)}, dict_ty);
auto ex_uniques = std::make_shared<DictionaryArray>(
dict_ty, ArrayFromJSON(int8(), "[0, 64]"), unified_dict(dict_64, dict_65));
CheckUnique(input, ex_uniques);
CheckValueCounts(input, ex_uniques, ArrayFromJSON(int64(), "[1, 1]"));

// Masked nulls don't use any index
input = *ChunkedArray::Make(
{make_chunk("[0, null]", dict_64), make_chunk("[0, 0, null]", dict_65)}, dict_ty);
ex_uniques = std::make_shared<DictionaryArray>(
dict_ty, ArrayFromJSON(int8(), "[0, null, 64]"), unified_dict(dict_64, dict_65));
CheckUnique(input, ex_uniques);
CheckValueCounts(input, ex_uniques, ArrayFromJSON(int64(), "[1, 2, 2]"));
}
{
// Valid input: 64 + 64 = 128 unified entries, the highest index is 127
auto dict_64_b = make_dict(100, 64);
auto input = *ChunkedArray::Make(
{make_chunk("[63]", dict_64), make_chunk("[63]", dict_64_b)}, dict_ty);
auto ex_uniques = std::make_shared<DictionaryArray>(
dict_ty, ArrayFromJSON(int8(), "[63, 127]"), unified_dict(dict_64, dict_64_b));
CheckUnique(input, ex_uniques);
CheckValueCounts(input, ex_uniques, ArrayFromJSON(int64(), "[1, 1]"));
}
{
// Invalid input: a used index is transposed to 128, which doesn't fit in int8
auto dict_65 = make_dict(100, 65);
auto input = *ChunkedArray::Make(
{make_chunk("[0]", dict_64), make_chunk("[0, 64]", dict_65)}, dict_ty);
EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, ::testing::HasSubstr("larger index type"),
Unique(input));
EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, ::testing::HasSubstr("larger index type"),
ValueCounts(input));
}
}

/* TODO(ARROW-4124): Determine if we want to do something that is reproducible with
* floats.
TEST_F(TestHashKernel, ValueCountsFloat) {
Expand Down