From b998842151fa2bad09ee33ddc84500036fa351dc Mon Sep 17 00:00:00 2001 From: mwish Date: Tue, 25 Feb 2025 17:54:36 +0800 Subject: [PATCH 1/8] Optimize Decode DeltaLengthByteArray --- cpp/src/arrow/array/builder_binary.h | 60 +++++++++++++++++++++++++++ cpp/src/parquet/decoder.cc | 38 ++++++++++++++++- cpp/src/parquet/encoding_benchmark.cc | 26 ++++++++++++ 3 files changed, 122 insertions(+), 2 deletions(-) diff --git a/cpp/src/arrow/array/builder_binary.h b/cpp/src/arrow/array/builder_binary.h index 442e4a26320a..0801af8f98fd 100644 --- a/cpp/src/arrow/array/builder_binary.h +++ b/cpp/src/arrow/array/builder_binary.h @@ -39,6 +39,7 @@ #include "arrow/util/binary_view_util.h" #include "arrow/util/macros.h" #include "arrow/util/visibility.h" +#include "arrow/visit_data_inline.h" namespace arrow { @@ -303,6 +304,65 @@ class BaseBinaryBuilder return Status::OK(); } + Status AppendBinaryWithLengths(std::string_view binary, const int32_t* value_lengths, + int64_t length) { + ARROW_RETURN_NOT_OK(Reserve(length)); + UnsafeAppendToBitmap(/*valid_bytes=*/nullptr, length); + // All values is valid + int64_t accum_length = 0; + for (int64_t i = 0; i < length; ++i) { + accum_length += value_lengths[i]; + } + if (ARROW_PREDICT_FALSE(binary.size() < accum_length)) { + return Status::Invalid("Binary data is too short"); + } + std::string_view sub_data = binary.substr(0, accum_length); + ARROW_RETURN_NOT_OK(value_data_builder_.Append( + reinterpret_cast(sub_data.data()), sub_data.size())); + accum_length = 0; + const int64_t initialize_offset = value_data_builder_.length(); + for (int64_t i = 0; i < length; ++i) { + offsets_builder_.UnsafeAppend(initialize_offset + accum_length); + accum_length += value_lengths[i]; + } + return Status::OK(); + } + + Status AppendBinaryWithLengths(std::string_view binary, const int32_t* value_lengths, + int64_t length, int64_t null_count, + const uint8_t* valid_bits, int64_t valid_bits_offset) { + if (valid_bits == nullptr || null_count == 0) { + return AppendBinaryWithLengths(binary, value_lengths, length); + } + ARROW_RETURN_NOT_OK(Reserve(length)); + int64_t accum_length = 0; + for (int64_t i = 0; i < length; ++i) { + accum_length += value_lengths[i]; + } + if (ARROW_PREDICT_FALSE(binary.size() < accum_length)) { + return Status::Invalid("Binary data is too short"); + } + const int64_t original_offset = value_data_builder_.length(); + std::string_view sub_data = binary.substr(0, accum_length); + ARROW_RETURN_NOT_OK(value_data_builder_.Append( + reinterpret_cast(sub_data.data()), sub_data.size())); + int64_t length_idx = 0; + accum_length = 0; + RETURN_NOT_OK(VisitNullBitmapInline( + valid_bits, valid_bits_offset, length, null_count, + [&]() { + offsets_builder_.UnsafeAppend(original_offset + accum_length); + accum_length += value_lengths[length_idx]; + ++length_idx; + return Status::OK(); + }, + [&]() { + offsets_builder_.UnsafeAppend(original_offset + accum_length); + return Status::OK(); + })); + return Status::OK(); + } + void Reset() override { ArrayBuilder::Reset(); offsets_builder_.Reset(); diff --git a/cpp/src/parquet/decoder.cc b/cpp/src/parquet/decoder.cc index 7063f423096d..859af5aec341 100644 --- a/cpp/src/parquet/decoder.cc +++ b/cpp/src/parquet/decoder.cc @@ -496,6 +496,8 @@ struct ArrowBinaryHelper { return acc_->builder->AppendNull(); } + bool CanFit(int64_t length) const { return length <= chunk_space_remaining_; } + private: Status PushChunk() { ARROW_ASSIGN_OR_RAISE(auto chunk, acc_->builder->Finish()); @@ -504,8 +506,6 @@ struct ArrowBinaryHelper { return Status::OK(); } - bool CanFit(int64_t length) const { return length <= chunk_space_remaining_; } - Accumulator* acc_; int64_t entries_remaining_; int64_t chunk_space_remaining_; @@ -1675,6 +1675,35 @@ class DeltaLengthByteArrayDecoder : public DecoderImpl, num_valid_values_ = num_length; } + Status DecodeArrowDenseFastPath( + int num_values, int null_count, const uint8_t* valid_bits, + int64_t valid_bits_offset, typename EncodingTraits::Accumulator* out, + int* out_num_values) { + int max_values = num_values - null_count; + if (num_values - null_count > num_valid_values_) { + throw ParquetException("Expected to decode ", num_values - null_count, + " values, but can decode decoded ", num_valid_values_, + " values."); + } + const int32_t* length_ptr = buffered_length_->data_as() + length_idx_; + int bytes_offset = len_ - decoder_->bytes_left(); + const uint8_t* data_ptr = data_ + bytes_offset; + const int64_t origin_data_size = out->builder->value_data_length(); + RETURN_NOT_OK(out->builder->AppendBinaryWithLengths( + reinterpret_cast(data_ptr), length_ptr, null_count, null_count, + valid_bits, valid_bits_offset)); + if (ARROW_PREDICT_FALSE(!decoder_->Advance( + 8 * static_cast(out->builder->value_data_length() - + origin_data_size)))) { + ParquetException::EofException(); + } + length_idx_ += max_values; + this->num_values_ -= max_values; + num_valid_values_ -= max_values; + *out_num_values = num_values - null_count; + return Status::OK(); + } + Status DecodeArrowDense(int num_values, int null_count, const uint8_t* valid_bits, int64_t valid_bits_offset, typename EncodingTraits::Accumulator* out, @@ -1682,6 +1711,11 @@ class DeltaLengthByteArrayDecoder : public DecoderImpl, ArrowBinaryHelper helper(out, num_values); RETURN_NOT_OK(helper.Prepare()); + if (helper.CanFit(decoder_->bytes_left())) { + return DecodeArrowDenseFastPath(num_values, null_count, valid_bits, + valid_bits_offset, out, out_num_values); + } + std::vector values(num_values - null_count); const int num_valid_values = Decode(values.data(), num_values - null_count); if (ARROW_PREDICT_FALSE(num_values - null_count != num_valid_values)) { diff --git a/cpp/src/parquet/encoding_benchmark.cc b/cpp/src/parquet/encoding_benchmark.cc index 34d12a624f6f..7ce1675020f0 100644 --- a/cpp/src/parquet/encoding_benchmark.cc +++ b/cpp/src/parquet/encoding_benchmark.cc @@ -1267,6 +1267,27 @@ class BM_ArrowBinaryPlain : public BenchmarkDecodeArrowByteArray { } }; +class BM_ArrowBinaryDeltaLength : public BenchmarkDecodeArrowByteArray { + public: + void DoEncodeArrow() override { + auto encoder = MakeTypedEncoder(Encoding::DELTA_LENGTH_BYTE_ARRAY); + encoder->Put(*input_array_); + buffer_ = encoder->FlushValues(); + } + + void DoEncodeLowLevel() override { + auto encoder = MakeTypedEncoder(Encoding::DELTA_LENGTH_BYTE_ARRAY); + encoder->Put(values_.data(), num_values_); + buffer_ = encoder->FlushValues(); + } + + std::unique_ptr InitializeDecoder() override { + auto decoder = MakeTypedDecoder(Encoding::DELTA_LENGTH_BYTE_ARRAY); + decoder->SetData(num_values_, buffer_->data(), static_cast(buffer_->size())); + return decoder; + } +}; + BENCHMARK_DEFINE_F(BM_ArrowBinaryPlain, EncodeArrow) (benchmark::State& state) { EncodeArrowBenchmark(state); } BENCHMARK_REGISTER_F(BM_ArrowBinaryPlain, EncodeArrow)->Range(1 << 18, 1 << 20); @@ -1284,6 +1305,11 @@ BENCHMARK_DEFINE_F(BM_ArrowBinaryPlain, DecodeArrowNonNull_Dense) BENCHMARK_REGISTER_F(BM_ArrowBinaryPlain, DecodeArrowNonNull_Dense) ->Range(MIN_RANGE, MAX_RANGE); +BENCHMARK_DEFINE_F(BM_ArrowBinaryDeltaLength, DL_DecodeArrow_Dense) +(benchmark::State& state) { DecodeArrowDenseBenchmark(state); } +BENCHMARK_REGISTER_F(BM_ArrowBinaryDeltaLength, DL_DecodeArrow_Dense) + ->Range(MIN_RANGE, MAX_RANGE); + BENCHMARK_DEFINE_F(BM_ArrowBinaryPlain, DecodeArrow_Dict) (benchmark::State& state) { DecodeArrowDictBenchmark(state); } BENCHMARK_REGISTER_F(BM_ArrowBinaryPlain, DecodeArrow_Dict)->Range(MIN_RANGE, MAX_RANGE); From 4a4847f89bf522dab690bcd7cb5f8e52ef20a212 Mon Sep 17 00:00:00 2001 From: mwish Date: Tue, 25 Feb 2025 18:55:24 +0800 Subject: [PATCH 2/8] try fix ci --- cpp/src/arrow/array/builder_binary.h | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/cpp/src/arrow/array/builder_binary.h b/cpp/src/arrow/array/builder_binary.h index 0801af8f98fd..903dc677121a 100644 --- a/cpp/src/arrow/array/builder_binary.h +++ b/cpp/src/arrow/array/builder_binary.h @@ -307,22 +307,27 @@ class BaseBinaryBuilder Status AppendBinaryWithLengths(std::string_view binary, const int32_t* value_lengths, int64_t length) { ARROW_RETURN_NOT_OK(Reserve(length)); - UnsafeAppendToBitmap(/*valid_bytes=*/nullptr, length); + UnsafeAppendToBitmap(/*valid_bytes=*/NULLPTR, length); // All values is valid int64_t accum_length = 0; for (int64_t i = 0; i < length; ++i) { accum_length += value_lengths[i]; } - if (ARROW_PREDICT_FALSE(binary.size() < accum_length)) { + if (ARROW_PREDICT_FALSE(binary.size() < static_cast(accum_length))) { return Status::Invalid("Binary data is too short"); } + if (ARROW_PREDICT_FALSE(binary.size() + value_data_builder_.length() > + std::numeric_limits::max())) { + return Status::Invalid("Append binary data too long"); + } std::string_view sub_data = binary.substr(0, accum_length); ARROW_RETURN_NOT_OK(value_data_builder_.Append( reinterpret_cast(sub_data.data()), sub_data.size())); accum_length = 0; const int64_t initialize_offset = value_data_builder_.length(); for (int64_t i = 0; i < length; ++i) { - offsets_builder_.UnsafeAppend(initialize_offset + accum_length); + offsets_builder_.UnsafeAppend( + static_cast(initialize_offset + accum_length)); accum_length += value_lengths[i]; } return Status::OK(); @@ -331,7 +336,7 @@ class BaseBinaryBuilder Status AppendBinaryWithLengths(std::string_view binary, const int32_t* value_lengths, int64_t length, int64_t null_count, const uint8_t* valid_bits, int64_t valid_bits_offset) { - if (valid_bits == nullptr || null_count == 0) { + if (valid_bits == NULLPTR || null_count == 0) { return AppendBinaryWithLengths(binary, value_lengths, length); } ARROW_RETURN_NOT_OK(Reserve(length)); @@ -339,10 +344,15 @@ class BaseBinaryBuilder for (int64_t i = 0; i < length; ++i) { accum_length += value_lengths[i]; } - if (ARROW_PREDICT_FALSE(binary.size() < accum_length)) { + if (ARROW_PREDICT_FALSE(binary.size() < static_cast(accum_length))) { return Status::Invalid("Binary data is too short"); } const int64_t original_offset = value_data_builder_.length(); + if (ARROW_PREDICT_FALSE(original_offset + accum_length) > + std::numeric_limits::max()) { + return Status::Invalid("Append binary data too long"); + } + std::string_view sub_data = binary.substr(0, accum_length); ARROW_RETURN_NOT_OK(value_data_builder_.Append( reinterpret_cast(sub_data.data()), sub_data.size())); @@ -351,13 +361,15 @@ class BaseBinaryBuilder RETURN_NOT_OK(VisitNullBitmapInline( valid_bits, valid_bits_offset, length, null_count, [&]() { - offsets_builder_.UnsafeAppend(original_offset + accum_length); + offsets_builder_.UnsafeAppend( + static_cast(original_offset + accum_length)); accum_length += value_lengths[length_idx]; ++length_idx; return Status::OK(); }, [&]() { - offsets_builder_.UnsafeAppend(original_offset + accum_length); + offsets_builder_.UnsafeAppend( + static_cast(original_offset + accum_length)); return Status::OK(); })); return Status::OK(); From 875c538e59de384db7584dfad5264231bf734f23 Mon Sep 17 00:00:00 2001 From: mwish Date: Mon, 3 Mar 2025 13:02:51 +0800 Subject: [PATCH 3/8] apply suggestions & bugfix --- cpp/src/arrow/array/builder_binary.h | 73 +--------------- cpp/src/parquet/decoder.cc | 122 +++++++++++++++++++++++++-- 2 files changed, 119 insertions(+), 76 deletions(-) diff --git a/cpp/src/arrow/array/builder_binary.h b/cpp/src/arrow/array/builder_binary.h index 903dc677121a..470e83c161bb 100644 --- a/cpp/src/arrow/array/builder_binary.h +++ b/cpp/src/arrow/array/builder_binary.h @@ -304,76 +304,9 @@ class BaseBinaryBuilder return Status::OK(); } - Status AppendBinaryWithLengths(std::string_view binary, const int32_t* value_lengths, - int64_t length) { - ARROW_RETURN_NOT_OK(Reserve(length)); - UnsafeAppendToBitmap(/*valid_bytes=*/NULLPTR, length); - // All values is valid - int64_t accum_length = 0; - for (int64_t i = 0; i < length; ++i) { - accum_length += value_lengths[i]; - } - if (ARROW_PREDICT_FALSE(binary.size() < static_cast(accum_length))) { - return Status::Invalid("Binary data is too short"); - } - if (ARROW_PREDICT_FALSE(binary.size() + value_data_builder_.length() > - std::numeric_limits::max())) { - return Status::Invalid("Append binary data too long"); - } - std::string_view sub_data = binary.substr(0, accum_length); - ARROW_RETURN_NOT_OK(value_data_builder_.Append( - reinterpret_cast(sub_data.data()), sub_data.size())); - accum_length = 0; - const int64_t initialize_offset = value_data_builder_.length(); - for (int64_t i = 0; i < length; ++i) { - offsets_builder_.UnsafeAppend( - static_cast(initialize_offset + accum_length)); - accum_length += value_lengths[i]; - } - return Status::OK(); - } - - Status AppendBinaryWithLengths(std::string_view binary, const int32_t* value_lengths, - int64_t length, int64_t null_count, - const uint8_t* valid_bits, int64_t valid_bits_offset) { - if (valid_bits == NULLPTR || null_count == 0) { - return AppendBinaryWithLengths(binary, value_lengths, length); - } - ARROW_RETURN_NOT_OK(Reserve(length)); - int64_t accum_length = 0; - for (int64_t i = 0; i < length; ++i) { - accum_length += value_lengths[i]; - } - if (ARROW_PREDICT_FALSE(binary.size() < static_cast(accum_length))) { - return Status::Invalid("Binary data is too short"); - } - const int64_t original_offset = value_data_builder_.length(); - if (ARROW_PREDICT_FALSE(original_offset + accum_length) > - std::numeric_limits::max()) { - return Status::Invalid("Append binary data too long"); - } - - std::string_view sub_data = binary.substr(0, accum_length); - ARROW_RETURN_NOT_OK(value_data_builder_.Append( - reinterpret_cast(sub_data.data()), sub_data.size())); - int64_t length_idx = 0; - accum_length = 0; - RETURN_NOT_OK(VisitNullBitmapInline( - valid_bits, valid_bits_offset, length, null_count, - [&]() { - offsets_builder_.UnsafeAppend( - static_cast(original_offset + accum_length)); - accum_length += value_lengths[length_idx]; - ++length_idx; - return Status::OK(); - }, - [&]() { - offsets_builder_.UnsafeAppend( - static_cast(original_offset + accum_length)); - return Status::OK(); - })); - return Status::OK(); - } + TypedBufferBuilder* offsets_builder() { return &offsets_builder_; } + TypedBufferBuilder* value_data_builder() { return &value_data_builder_; } + using ArrayBuilder::UnsafeAppendToBitmap; void Reset() override { ArrayBuilder::Reset(); diff --git a/cpp/src/parquet/decoder.cc b/cpp/src/parquet/decoder.cc index 859af5aec341..0b35b8e82555 100644 --- a/cpp/src/parquet/decoder.cc +++ b/cpp/src/parquet/decoder.cc @@ -1585,6 +1585,77 @@ class DeltaBitPackDecoder : public DecoderImpl, public TypedDecoderImpl { // ---------------------------------------------------------------------- // DELTA_LENGTH_BYTE_ARRAY decoder +// Status AppendBinaryWithLengths(std::string_view binary, const int32_t* value_lengths, +// int64_t length) { +// ARROW_RETURN_NOT_OK(Reserve(length)); +// UnsafeAppendToBitmap(/*valid_bytes=*/NULLPTR, length); +// // All values is valid +// int64_t accum_length = 0; +// for (int64_t i = 0; i < length; ++i) { +// accum_length += value_lengths[i]; +// } +// if (ARROW_PREDICT_FALSE(binary.size() < static_cast(accum_length))) { +// return Status::Invalid("Binary data is too short"); +// } +// if (ARROW_PREDICT_FALSE(binary.size() + value_data_builder_.length() > +// std::numeric_limits::max())) { +// return Status::Invalid("Append binary data too long"); +// } +// std::string_view sub_data = binary.substr(0, accum_length); +// ARROW_RETURN_NOT_OK(value_data_builder_.Append( +// reinterpret_cast(sub_data.data()), sub_data.size())); +// accum_length = 0; +// const int64_t initialize_offset = value_data_builder_.length(); +// for (int64_t i = 0; i < length; ++i) { +// offsets_builder_.UnsafeAppend( +// static_cast(initialize_offset + accum_length)); +// accum_length += value_lengths[i]; +// } +// return Status::OK(); +// } +// +// Status AppendBinaryWithLengths(std::string_view binary, const int32_t* value_lengths, +// int64_t length, int64_t null_count, +// const uint8_t* valid_bits, int64_t valid_bits_offset) { +// if (valid_bits == NULLPTR || null_count == 0) { +// return AppendBinaryWithLengths(binary, value_lengths, length); +// } +// ARROW_RETURN_NOT_OK(Reserve(length)); +// int64_t accum_length = 0; +// for (int64_t i = 0; i < length; ++i) { +// accum_length += value_lengths[i]; +// } +// if (ARROW_PREDICT_FALSE(binary.size() < static_cast(accum_length))) { +// return Status::Invalid("Binary data is too short"); +// } +// const int64_t original_offset = value_data_builder_.length(); +// if (ARROW_PREDICT_FALSE(original_offset + accum_length) > +// std::numeric_limits::max()) { +// return Status::Invalid("Append binary data too long"); +// } +// +// std::string_view sub_data = binary.substr(0, accum_length); +// ARROW_RETURN_NOT_OK(value_data_builder_.Append( +// reinterpret_cast(sub_data.data()), sub_data.size())); +// int64_t length_idx = 0; +// accum_length = 0; +// RETURN_NOT_OK(VisitNullBitmapInline( +// valid_bits, valid_bits_offset, length, null_count, +// [&]() { +// offsets_builder_.UnsafeAppend( +// static_cast(original_offset + accum_length)); +// accum_length += value_lengths[length_idx]; +// ++length_idx; +// return Status::OK(); +// }, +// [&]() { +// offsets_builder_.UnsafeAppend( +// static_cast(original_offset + accum_length)); +// return Status::OK(); +// })); +// return Status::OK(); +// } + class DeltaLengthByteArrayDecoder : public DecoderImpl, public TypedDecoderImpl { public: @@ -1688,13 +1759,52 @@ class DeltaLengthByteArrayDecoder : public DecoderImpl, const int32_t* length_ptr = buffered_length_->data_as() + length_idx_; int bytes_offset = len_ - decoder_->bytes_left(); const uint8_t* data_ptr = data_ + bytes_offset; - const int64_t origin_data_size = out->builder->value_data_length(); - RETURN_NOT_OK(out->builder->AppendBinaryWithLengths( - reinterpret_cast(data_ptr), length_ptr, null_count, null_count, - valid_bits, valid_bits_offset)); + const int64_t initial_offset = out->builder->value_data_length(); + auto* offsets_builder = out->builder->offsets_builder(); + auto* value_data_builder = out->builder->value_data_builder(); + // Phase1: get total length of binary data and append to value_data_builder + int64_t accum_length = 0; + for (int i = 0; i < max_values; ++i) { + accum_length += length_ptr[i]; + } + if (ARROW_PREDICT_FALSE(decoder_->bytes_left() < static_cast(accum_length))) { + return Status::Invalid("Binary data is too short"); + } + RETURN_NOT_OK(out->builder->ValidateOverflow(accum_length)); + // Append the binary data + ARROW_RETURN_NOT_OK(value_data_builder->Append(data_ptr, accum_length)); + // Phase2: append offsets + RETURN_NOT_OK(offsets_builder->Reserve(num_values)); + accum_length = 0; + if (valid_bits == nullptr) { + for (int i = 0; i < max_values; ++i) { + RETURN_NOT_OK( + offsets_builder->Append(static_cast(initial_offset + accum_length))); + accum_length += length_ptr[i]; + } + } else { + int length_idx = 0; + RETURN_NOT_OK(VisitNullBitmapInline( + valid_bits, valid_bits_offset, num_values, null_count, + [&]() { + offsets_builder->UnsafeAppend( + static_cast(initial_offset + accum_length)); + accum_length += length_ptr[length_idx]; + ++length_idx; + return Status::OK(); + }, + [&]() { + offsets_builder->UnsafeAppend( + static_cast(initial_offset + accum_length)); + return Status::OK(); + })); + } + // Phase3: Append nulls + out->builder->UnsafeAppendToBitmap(valid_bits, valid_bits_offset, num_values); + // Phase4: update the encoder internal status. if (ARROW_PREDICT_FALSE(!decoder_->Advance( - 8 * static_cast(out->builder->value_data_length() - - origin_data_size)))) { + 8 * + static_cast(out->builder->value_data_length() - initial_offset)))) { ParquetException::EofException(); } length_idx_ += max_values; From cfcca084633aad055723319a1ae44684a04160ea Mon Sep 17 00:00:00 2001 From: mwish Date: Mon, 3 Mar 2025 13:11:18 +0800 Subject: [PATCH 4/8] doc string --- cpp/src/arrow/array/builder_binary.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cpp/src/arrow/array/builder_binary.h b/cpp/src/arrow/array/builder_binary.h index 470e83c161bb..37472ed9696b 100644 --- a/cpp/src/arrow/array/builder_binary.h +++ b/cpp/src/arrow/array/builder_binary.h @@ -39,7 +39,6 @@ #include "arrow/util/binary_view_util.h" #include "arrow/util/macros.h" #include "arrow/util/visibility.h" -#include "arrow/visit_data_inline.h" namespace arrow { @@ -304,7 +303,9 @@ class BaseBinaryBuilder return Status::OK(); } + /// Get the builder to keep track of the offsets. TypedBufferBuilder* offsets_builder() { return &offsets_builder_; } + /// Get the builder to keep track of the value binary data. TypedBufferBuilder* value_data_builder() { return &value_data_builder_; } using ArrayBuilder::UnsafeAppendToBitmap; From 207c2b61c066db266fb6f4b7adc84881dabeb08a Mon Sep 17 00:00:00 2001 From: mwish Date: Mon, 3 Mar 2025 13:38:32 +0800 Subject: [PATCH 5/8] try to fix lint --- cpp/src/parquet/decoder.cc | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cpp/src/parquet/decoder.cc b/cpp/src/parquet/decoder.cc index 0b35b8e82555..ec478b258e68 100644 --- a/cpp/src/parquet/decoder.cc +++ b/cpp/src/parquet/decoder.cc @@ -1765,9 +1765,15 @@ class DeltaLengthByteArrayDecoder : public DecoderImpl, // Phase1: get total length of binary data and append to value_data_builder int64_t accum_length = 0; for (int i = 0; i < max_values; ++i) { + if (ARROW_PREDICT_FALSE(length_ptr[i] < 0)) { + return Status::Invalid("negative string delta length"); + } accum_length += length_ptr[i]; } - if (ARROW_PREDICT_FALSE(decoder_->bytes_left() < static_cast(accum_length))) { + if (ARROW_PREDICT_FALSE(accum_length > std::numeric_limits::max())) { + return Status::Invalid("excess expansion in DELTA_BYTE_ARRAY"); + } + if (ARROW_PREDICT_FALSE(decoder_->bytes_left() < accum_length)) { return Status::Invalid("Binary data is too short"); } RETURN_NOT_OK(out->builder->ValidateOverflow(accum_length)); @@ -1802,9 +1808,7 @@ class DeltaLengthByteArrayDecoder : public DecoderImpl, // Phase3: Append nulls out->builder->UnsafeAppendToBitmap(valid_bits, valid_bits_offset, num_values); // Phase4: update the encoder internal status. - if (ARROW_PREDICT_FALSE(!decoder_->Advance( - 8 * - static_cast(out->builder->value_data_length() - initial_offset)))) { + if (ARROW_PREDICT_FALSE(!decoder_->Advance(8 * accum_length))) { ParquetException::EofException(); } length_idx_ += max_values; From 93f645bbbce6cb441ac8ef52fe77106246fc1abc Mon Sep 17 00:00:00 2001 From: mwish Date: Mon, 3 Mar 2025 13:40:06 +0800 Subject: [PATCH 6/8] update logic --- cpp/src/parquet/decoder.cc | 71 -------------------------------------- 1 file changed, 71 deletions(-) diff --git a/cpp/src/parquet/decoder.cc b/cpp/src/parquet/decoder.cc index ec478b258e68..16a8948322c3 100644 --- a/cpp/src/parquet/decoder.cc +++ b/cpp/src/parquet/decoder.cc @@ -1585,77 +1585,6 @@ class DeltaBitPackDecoder : public DecoderImpl, public TypedDecoderImpl { // ---------------------------------------------------------------------- // DELTA_LENGTH_BYTE_ARRAY decoder -// Status AppendBinaryWithLengths(std::string_view binary, const int32_t* value_lengths, -// int64_t length) { -// ARROW_RETURN_NOT_OK(Reserve(length)); -// UnsafeAppendToBitmap(/*valid_bytes=*/NULLPTR, length); -// // All values is valid -// int64_t accum_length = 0; -// for (int64_t i = 0; i < length; ++i) { -// accum_length += value_lengths[i]; -// } -// if (ARROW_PREDICT_FALSE(binary.size() < static_cast(accum_length))) { -// return Status::Invalid("Binary data is too short"); -// } -// if (ARROW_PREDICT_FALSE(binary.size() + value_data_builder_.length() > -// std::numeric_limits::max())) { -// return Status::Invalid("Append binary data too long"); -// } -// std::string_view sub_data = binary.substr(0, accum_length); -// ARROW_RETURN_NOT_OK(value_data_builder_.Append( -// reinterpret_cast(sub_data.data()), sub_data.size())); -// accum_length = 0; -// const int64_t initialize_offset = value_data_builder_.length(); -// for (int64_t i = 0; i < length; ++i) { -// offsets_builder_.UnsafeAppend( -// static_cast(initialize_offset + accum_length)); -// accum_length += value_lengths[i]; -// } -// return Status::OK(); -// } -// -// Status AppendBinaryWithLengths(std::string_view binary, const int32_t* value_lengths, -// int64_t length, int64_t null_count, -// const uint8_t* valid_bits, int64_t valid_bits_offset) { -// if (valid_bits == NULLPTR || null_count == 0) { -// return AppendBinaryWithLengths(binary, value_lengths, length); -// } -// ARROW_RETURN_NOT_OK(Reserve(length)); -// int64_t accum_length = 0; -// for (int64_t i = 0; i < length; ++i) { -// accum_length += value_lengths[i]; -// } -// if (ARROW_PREDICT_FALSE(binary.size() < static_cast(accum_length))) { -// return Status::Invalid("Binary data is too short"); -// } -// const int64_t original_offset = value_data_builder_.length(); -// if (ARROW_PREDICT_FALSE(original_offset + accum_length) > -// std::numeric_limits::max()) { -// return Status::Invalid("Append binary data too long"); -// } -// -// std::string_view sub_data = binary.substr(0, accum_length); -// ARROW_RETURN_NOT_OK(value_data_builder_.Append( -// reinterpret_cast(sub_data.data()), sub_data.size())); -// int64_t length_idx = 0; -// accum_length = 0; -// RETURN_NOT_OK(VisitNullBitmapInline( -// valid_bits, valid_bits_offset, length, null_count, -// [&]() { -// offsets_builder_.UnsafeAppend( -// static_cast(original_offset + accum_length)); -// accum_length += value_lengths[length_idx]; -// ++length_idx; -// return Status::OK(); -// }, -// [&]() { -// offsets_builder_.UnsafeAppend( -// static_cast(original_offset + accum_length)); -// return Status::OK(); -// })); -// return Status::OK(); -// } - class DeltaLengthByteArrayDecoder : public DecoderImpl, public TypedDecoderImpl { public: From fb67d77f8ed47139dac19306247a7388e6c4f403 Mon Sep 17 00:00:00 2001 From: mwish Date: Fri, 7 Mar 2025 11:03:16 +0800 Subject: [PATCH 7/8] apply suggestions --- cpp/src/parquet/decoder.cc | 24 +++++++++--------------- cpp/src/parquet/encoding_benchmark.cc | 9 +++++++-- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/cpp/src/parquet/decoder.cc b/cpp/src/parquet/decoder.cc index 16a8948322c3..f15fe6fed1e0 100644 --- a/cpp/src/parquet/decoder.cc +++ b/cpp/src/parquet/decoder.cc @@ -1679,9 +1679,9 @@ class DeltaLengthByteArrayDecoder : public DecoderImpl, int num_values, int null_count, const uint8_t* valid_bits, int64_t valid_bits_offset, typename EncodingTraits::Accumulator* out, int* out_num_values) { - int max_values = num_values - null_count; - if (num_values - null_count > num_valid_values_) { - throw ParquetException("Expected to decode ", num_values - null_count, + int num_non_null_values = num_values - null_count; + if (num_non_null_values > num_valid_values_) { + throw ParquetException("Expected to decode ", num_non_null_values, " values, but can decode decoded ", num_valid_values_, " values."); } @@ -1693,7 +1693,7 @@ class DeltaLengthByteArrayDecoder : public DecoderImpl, auto* value_data_builder = out->builder->value_data_builder(); // Phase1: get total length of binary data and append to value_data_builder int64_t accum_length = 0; - for (int i = 0; i < max_values; ++i) { + for (int i = 0; i < num_non_null_values; ++i) { if (ARROW_PREDICT_FALSE(length_ptr[i] < 0)) { return Status::Invalid("negative string delta length"); } @@ -1710,14 +1710,8 @@ class DeltaLengthByteArrayDecoder : public DecoderImpl, ARROW_RETURN_NOT_OK(value_data_builder->Append(data_ptr, accum_length)); // Phase2: append offsets RETURN_NOT_OK(offsets_builder->Reserve(num_values)); - accum_length = 0; - if (valid_bits == nullptr) { - for (int i = 0; i < max_values; ++i) { - RETURN_NOT_OK( - offsets_builder->Append(static_cast(initial_offset + accum_length))); - accum_length += length_ptr[i]; - } - } else { + { + accum_length = 0; int length_idx = 0; RETURN_NOT_OK(VisitNullBitmapInline( valid_bits, valid_bits_offset, num_values, null_count, @@ -1740,9 +1734,9 @@ class DeltaLengthByteArrayDecoder : public DecoderImpl, if (ARROW_PREDICT_FALSE(!decoder_->Advance(8 * accum_length))) { ParquetException::EofException(); } - length_idx_ += max_values; - this->num_values_ -= max_values; - num_valid_values_ -= max_values; + length_idx_ += num_non_null_values; + this->num_values_ -= num_non_null_values; + num_valid_values_ -= num_non_null_values; *out_num_values = num_values - null_count; return Status::OK(); } diff --git a/cpp/src/parquet/encoding_benchmark.cc b/cpp/src/parquet/encoding_benchmark.cc index 7ce1675020f0..670e7ceabc7d 100644 --- a/cpp/src/parquet/encoding_benchmark.cc +++ b/cpp/src/parquet/encoding_benchmark.cc @@ -1305,9 +1305,14 @@ BENCHMARK_DEFINE_F(BM_ArrowBinaryPlain, DecodeArrowNonNull_Dense) BENCHMARK_REGISTER_F(BM_ArrowBinaryPlain, DecodeArrowNonNull_Dense) ->Range(MIN_RANGE, MAX_RANGE); -BENCHMARK_DEFINE_F(BM_ArrowBinaryDeltaLength, DL_DecodeArrow_Dense) +BENCHMARK_DEFINE_F(BM_ArrowBinaryDeltaLength, DecodeArrow_Dense) (benchmark::State& state) { DecodeArrowDenseBenchmark(state); } -BENCHMARK_REGISTER_F(BM_ArrowBinaryDeltaLength, DL_DecodeArrow_Dense) +BENCHMARK_REGISTER_F(BM_ArrowBinaryDeltaLength, DecodeArrow_Dense) + ->Range(MIN_RANGE, MAX_RANGE); + +BENCHMARK_DEFINE_F(BM_ArrowBinaryDeltaLength, DecodeArrowNotNull_Dense) +(benchmark::State& state) { DecodeArrowNonNullDenseBenchmark(state); } +BENCHMARK_REGISTER_F(BM_ArrowBinaryDeltaLength, DecodeArrowNotNull_Dense) ->Range(MIN_RANGE, MAX_RANGE); BENCHMARK_DEFINE_F(BM_ArrowBinaryPlain, DecodeArrow_Dict) From c21878c99889aaefdc49cf3771daf7adf5c689c5 Mon Sep 17 00:00:00 2001 From: mwish Date: Thu, 27 Mar 2025 11:15:43 +0800 Subject: [PATCH 8/8] add safety comment --- cpp/src/parquet/decoder.cc | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/cpp/src/parquet/decoder.cc b/cpp/src/parquet/decoder.cc index f15fe6fed1e0..c436b80b8741 100644 --- a/cpp/src/parquet/decoder.cc +++ b/cpp/src/parquet/decoder.cc @@ -1699,11 +1699,17 @@ class DeltaLengthByteArrayDecoder : public DecoderImpl, } accum_length += length_ptr[i]; } - if (ARROW_PREDICT_FALSE(accum_length > std::numeric_limits::max())) { - return Status::Invalid("excess expansion in DELTA_BYTE_ARRAY"); - } - if (ARROW_PREDICT_FALSE(decoder_->bytes_left() < accum_length)) { - return Status::Invalid("Binary data is too short"); + { + // Actually these checks would not happen since DecodeArrowDenseFastPath + // would be called only when helper.CanFit(decoder_->bytes_left()), + // so, accum_length <= decoder_->bytes_left() << chunk_space_remaining_ + // <= std::numeric_limits::max(). + if (ARROW_PREDICT_FALSE(accum_length > std::numeric_limits::max())) { + return Status::Invalid("excess expansion in DELTA_BYTE_ARRAY"); + } + if (ARROW_PREDICT_FALSE(decoder_->bytes_left() < accum_length)) { + return Status::Invalid("Binary data is too short"); + } } RETURN_NOT_OK(out->builder->ValidateOverflow(accum_length)); // Append the binary data