Skip to content
Draft
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
24 changes: 24 additions & 0 deletions cpp/src/arrow/util/rle_encoding_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,9 @@ class RleBitPackedEncoder {
/// This value must be representable with bit_width_ bits.
bool Put(uint64_t value);

/// Encode the same value up to count times and return how many were accepted
int PutRepeated(uint64_t value, int count);

/// Flushes any pending values to the underlying buffer.
/// Returns the total number of bytes written
Comment thread
MashrafeeAryan marked this conversation as resolved.
Resolved
int Flush();
Expand Down Expand Up @@ -1526,6 +1529,27 @@ inline bool RleBitPackedEncoder::Put(uint64_t value) {
return true;
}

inline int RleBitPackedEncoder::PutRepeated(uint64_t value, int count) {
ARROW_DCHECK_GE(count, 0);
ARROW_DCHECK(bit_width_ == 64 || value < (1ULL << bit_width_));

int encoded = 0;
while (encoded < count && !(current_value_ == value && repeat_count_ > 8)) {
if (!Put(value)) {
return encoded;
}
++encoded;
}
if (encoded < count && !buffer_full_) {
// Extend an existing repeat by adding to its count instead of visiting each value
const int added =
std::min(count - encoded, std::numeric_limits<int>::max() - repeat_count_);
repeat_count_ += added;
encoded += added;
}
return encoded;
}

inline void RleBitPackedEncoder::FlushLiteralRun(bool update_indicator_byte) {
if (literal_indicator_byte_ == NULL) {
// The literal indicator byte has not been reserved yet, get one now.
Expand Down
71 changes: 71 additions & 0 deletions cpp/src/arrow/util/rle_encoding_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <bit>
#include <cstdint>
#include <cstring>
#include <limits>
#include <random>
#include <span>
#include <vector>
Expand Down Expand Up @@ -1453,4 +1454,74 @@ TEST(RleBitPacked, GetBatchSpacedRoundtripUint64) {
DoTestGetBatchSpacedRoundtrip<uint64_t>();
}

// Check that writing repeated values together gives the same bytes as one at a time
TEST(RleBitPacked, PutRepeatedMatchesPut) {
for (int width : {0, 1, 2, 3, 8, 16, 32, 64}) {
const uint64_t maximum =
width == 64 ? std::numeric_limits<uint64_t>::max() : ((uint64_t{1} << width) - 1);
for (uint64_t value : {uint64_t{0}, maximum}) {
for (int prefix : {0, 1, 7, 8, 9, 503, 504, 505}) {
for (int count : {0, 1, 7, 8, 9, 63, 64, 767, 768, 4096}) {
const int total = prefix + count + 9;
const int capacity =
static_cast<int>(RleBitPackedEncoder::MaxBufferSize(width, total) +
RleBitPackedEncoder::MinBufferSize(width));
std::vector<uint8_t> expected(capacity);
std::vector<uint8_t> actual(capacity);
RleBitPackedEncoder reference(expected.data(), capacity, width);
RleBitPackedEncoder encoder(actual.data(), capacity, width);
for (int i = 0; i < prefix; ++i) {
const uint64_t item = static_cast<uint64_t>(i & 1) & maximum;
ASSERT_TRUE(reference.Put(item));
ASSERT_TRUE(encoder.Put(item));
}
for (int i = 0; i < count; ++i) {
ASSERT_TRUE(reference.Put(value));
}
const int first = count / 2;
ASSERT_EQ(first, encoder.PutRepeated(value, first));
ASSERT_EQ(count - first, encoder.PutRepeated(value, count - first));
for (int i = 0; i < 9; ++i) {
const uint64_t item = static_cast<uint64_t>(i & 1) & maximum;
ASSERT_TRUE(reference.Put(item));
ASSERT_TRUE(encoder.Put(item));
}
expected.resize(reference.Flush());
actual.resize(encoder.Flush());
ASSERT_EQ(expected, actual);
}
}
}
}
}

// Check that both ways of writing stop at the same point when the buffer fills up
TEST(RleBitPacked, PutRepeatedFullBufferMatchesPut) {
for (int width : {1, 3, 16}) {
const int capacity = static_cast<int>(RleBitPackedEncoder::MinBufferSize(width));
std::vector<uint8_t> expected(capacity);
std::vector<uint8_t> actual(capacity);
RleBitPackedEncoder reference(expected.data(), capacity, width);
RleBitPackedEncoder encoder(actual.data(), capacity, width);
bool stopped = false;
for (int run = 0; run < 4096; ++run) {
const uint64_t value = run & 1;
const int count = run % 17 + 1;
int accepted = 0;
while (accepted < count && reference.Put(value)) {
++accepted;
}
ASSERT_EQ(accepted, encoder.PutRepeated(value, count));
if (accepted != count) {
stopped = true;
break;
}
}
ASSERT_TRUE(stopped);
expected.resize(reference.Flush());
actual.resize(encoder.Flush());
ASSERT_EQ(expected, actual);
}
}

} // namespace arrow::util
Loading
Loading