From ecc4d496f937d9e7e316da8591502c23d656109e Mon Sep 17 00:00:00 2001 From: "jinli.zjw" Date: Wed, 2 Dec 2026 15:55:46 +0800 Subject: [PATCH] Optimize null-free Parquet byte-array dictionary decoding --- cpp/src/parquet/decoder.cc | 62 +++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/cpp/src/parquet/decoder.cc b/cpp/src/parquet/decoder.cc index 076a44cfc0b..cf2dbd87bf6 100644 --- a/cpp/src/parquet/decoder.cc +++ b/cpp/src/parquet/decoder.cc @@ -1301,14 +1301,8 @@ class DictByteArrayDecoderImpl : public DictDecoderImpl { int64_t valid_bits_offset, typename EncodingTraits::Accumulator* out) override { int result = 0; - if (null_count == 0) { - PARQUET_THROW_NOT_OK(DecodeArrowDense(num_values, null_count, - /*valid_bits=*/nullptr, valid_bits_offset, - out, &result)); - } else { - PARQUET_THROW_NOT_OK(DecodeArrowDense(num_values, null_count, valid_bits, - valid_bits_offset, out, &result)); - } + PARQUET_THROW_NOT_OK(DecodeArrowDense(num_values, null_count, valid_bits, + valid_bits_offset, out, &result)); return result; } @@ -1325,30 +1319,48 @@ class DictByteArrayDecoderImpl : public DictDecoderImpl { const int values_to_decode = num_values - null_count; int values_decoded = 0; int num_indices = 0; - int pos_indices = 0; + auto refill_indices = [&]() { + const int batch_size = + std::min(kBufferSize, values_to_decode - values_decoded); + num_indices = idx_decoder_.GetBatch(indices, batch_size); + if (ARROW_PREDICT_FALSE(num_indices < 1)) { + return Status::Invalid("Invalid number of indices: ", num_indices); + } + return Status::OK(); + }; + auto append_indices = [&](const int32_t* batch, int length) { + for (int i = 0; i < length; ++i) { + const auto index = batch[i]; + RETURN_NOT_OK(IndexInBounds(index)); + const auto& val = dict_values[index]; + RETURN_NOT_OK(helper->AppendValue(val.ptr, static_cast(val.len))); + } + values_decoded += length; + return Status::OK(); + }; + if (null_count == 0) { + while (values_decoded < num_values) { + RETURN_NOT_OK(refill_indices()); + RETURN_NOT_OK(append_indices(indices, num_indices)); + } + *out_num_values = values_decoded; + return Status::OK(); + } + + int pos_indices = 0; auto visit_bit_run = [&](int64_t position, int64_t length, bool valid) { if (valid) { while (length > 0) { if (num_indices == pos_indices) { - // Refill indices buffer - const auto max_batch_size = - std::min(kBufferSize, values_to_decode - values_decoded); - num_indices = idx_decoder_.GetBatch(indices, max_batch_size); - if (ARROW_PREDICT_FALSE(num_indices < 1)) { - return Status::Invalid("Invalid number of indices: ", num_indices); - } + RETURN_NOT_OK(refill_indices()); pos_indices = 0; } - const auto batch_size = std::min(num_indices - pos_indices, length); - for (int64_t j = 0; j < batch_size; ++j) { - const auto index = indices[pos_indices++]; - RETURN_NOT_OK(IndexInBounds(index)); - const auto& val = dict_values[index]; - RETURN_NOT_OK(helper->AppendValue(val.ptr, static_cast(val.len))); - } - values_decoded += static_cast(batch_size); - length -= static_cast(batch_size); + const int batch_size = + std::min(num_indices - pos_indices, static_cast(length)); + RETURN_NOT_OK(append_indices(indices + pos_indices, batch_size)); + pos_indices += batch_size; + length -= batch_size; } } else { for (int64_t i = 0; i < length; ++i) {