Skip to content
Closed
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
25 changes: 18 additions & 7 deletions cpp/src/arrow/array/array_dict.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <algorithm>
#include <climits>
#include <cstdint>
#include <cstring>
#include <limits>
#include <utility>
#include <vector>
Expand All @@ -36,6 +37,7 @@
#include "arrow/table.h"
#include "arrow/type.h"
#include "arrow/type_traits.h"
#include "arrow/util/bit_run_reader.h"
#include "arrow/util/bit_util.h"
#include "arrow/util/bitmap_ops.h"
#include "arrow/util/checked_cast.h"
Expand Down Expand Up @@ -187,9 +189,8 @@ Result<std::shared_ptr<ArrayData>> TransposeDictIndices(
}

// Default path: compute a buffer of transposed indices.
ARROW_ASSIGN_OR_RAISE(
auto out_buffer,
AllocateBuffer(data->length * (out_index_type.bit_width() / CHAR_BIT), pool));
const int64_t out_buffer_size = data->length * (out_index_type.bit_width() / CHAR_BIT);
ARROW_ASSIGN_OR_RAISE(auto out_buffer, AllocateBuffer(out_buffer_size, pool));

// Shift null buffer if the original offset is non-zero
std::shared_ptr<Buffer> null_bitmap;
Expand All @@ -203,10 +204,20 @@ Result<std::shared_ptr<ArrayData>> TransposeDictIndices(
auto out_data = ArrayData::Make(out_type, data->length,
{null_bitmap, std::move(out_buffer)}, data->null_count);
out_data->dictionary = dictionary;
RETURN_NOT_OK(internal::TransposeInts(
in_index_type, out_index_type, data->GetValues<uint8_t>(1, 0),
out_data->GetMutableValues<uint8_t>(1, 0), data->offset, out_data->offset,
data->length, transpose_map));
const uint8_t* in_indices = data->GetValues<uint8_t>(1, 0);
uint8_t* out_indices = out_data->GetMutableValues<uint8_t>(1, 0);
// Indices under null slots are unspecified and may be out of bounds for
// transpose_map, so zero them and only transpose the valid runs.
if (data->MayHaveNulls()) {
std::memset(out_indices, 0, static_cast<size_t>(out_buffer_size));
}
RETURN_NOT_OK(internal::VisitSetBitRuns(data->buffers[0], data->offset, data->length,
[&](int64_t position, int64_t length) {
return internal::TransposeInts(
in_index_type, out_index_type, in_indices,
out_indices, data->offset + position,
position, length, transpose_map);
}));
return out_data;
}

Expand Down
30 changes: 30 additions & 0 deletions cpp/src/arrow/array/array_dict_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,36 @@ TEST(TestDictionary, TransposeNulls) {
CheckTranspose(sliced, transpose_map.data(), out_dict_type, out_dict, expected_indices);
}

TEST(TestDictionary, TransposeNullsWithOutOfBoundsIndex) {
// Index values under null slots are not checked by ValidateFull(),
// so they must not be looked up in the transpose map.
auto dict = ArrayFromJSON(utf8(), "[\"A\", \"B\", \"C\"]");
auto dict_type = dictionary(int32(), utf8());
auto valid_indices = ArrayFromJSON(int32(), "[1, null, 0, null]");
auto indices = MakeArray(ArrayData::Make(
int32(), 4,
{valid_indices->data()->buffers[0],
Buffer::FromVector(std::vector<int32_t>{1, 1 << 30, 0, -(1 << 30)})},
/*null_count=*/2));
auto arr = std::make_shared<DictionaryArray>(dict_type, indices, dict);
ASSERT_OK(arr->ValidateFull());

auto out_dict = ArrayFromJSON(utf8(), "[\"Z\", \"A\", \"C\", \"B\"]");
std::vector<int32_t> transpose_map = {1, 3, 2};
CheckTranspose(arr, transpose_map.data(), dict_type, out_dict,
ArrayFromJSON(int32(), "[3, null, 1, null]"));
CheckTranspose(arr->Slice(1, 2), transpose_map.data(), dict_type, out_dict,
ArrayFromJSON(int32(), "[null, 1]"));

ASSERT_OK_AND_ASSIGN(auto compacted, arr->Compact());
ASSERT_OK(compacted->ValidateFull());
ASSERT_OK_AND_ASSIGN(
auto expected,
DictionaryArray::FromArrays(dict_type, ArrayFromJSON(int32(), "[1, null, 0, null]"),
ArrayFromJSON(utf8(), "[\"A\", \"B\"]")));
AssertArraysEqual(*compacted, *expected);
}

TEST(TestDictionary, ListOfDictionary) {
std::unique_ptr<ArrayBuilder> root_builder;
ASSERT_OK(MakeBuilder(default_memory_pool(), list(dictionary(int8(), utf8())),
Expand Down
Loading