Repository navigation
GH-45594: [C++][Parquet] POC: Optimize Parquet DecodeArrow in DeltaLengthByteArray #45622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b998842
4a4847f
231e67a
875c538
cfcca08
207c2b6
93f645b
7360253
fb67d77
cc3f8c2
c21878c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -496,6 +496,8 @@ struct ArrowBinaryHelper<ByteArrayType> { | |
| 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<ByteArrayType> { | |
| 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,13 +1675,90 @@ 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<ByteArrayType>::Accumulator* out, | ||
| int* out_num_values) { | ||
| 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."); | ||
| } | ||
| const int32_t* length_ptr = buffered_length_->data_as<int32_t>() + length_idx_; | ||
| int bytes_offset = len_ - decoder_->bytes_left(); | ||
| const uint8_t* data_ptr = data_ + bytes_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 < num_non_null_values; ++i) { | ||
| if (ARROW_PREDICT_FALSE(length_ptr[i] < 0)) { | ||
| return Status::Invalid("negative string delta length"); | ||
| } | ||
| accum_length += length_ptr[i]; | ||
| } | ||
| { | ||
| // 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<int32_t>::max(). | ||
| if (ARROW_PREDICT_FALSE(accum_length > std::numeric_limits<int32_t>::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 | ||
| 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; | ||
| int length_idx = 0; | ||
| RETURN_NOT_OK(VisitNullBitmapInline( | ||
| valid_bits, valid_bits_offset, num_values, null_count, | ||
| [&]() { | ||
| offsets_builder->UnsafeAppend( | ||
| static_cast<int32_t>(initial_offset + accum_length)); | ||
| accum_length += length_ptr[length_idx]; | ||
| ++length_idx; | ||
| return Status::OK(); | ||
| }, | ||
| [&]() { | ||
| offsets_builder->UnsafeAppend( | ||
| static_cast<int32_t>(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 * accum_length))) { | ||
| ParquetException::EofException(); | ||
| } | ||
| 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(); | ||
| } | ||
|
|
||
| Status DecodeArrowDense(int num_values, int null_count, const uint8_t* valid_bits, | ||
| int64_t valid_bits_offset, | ||
| typename EncodingTraits<ByteArrayType>::Accumulator* out, | ||
| int* out_num_values) { | ||
| ArrowBinaryHelper<ByteArrayType> 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); | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder why we need the slow path below at all. Instead, we could just expose
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Emmm I don't know what does this mean, when Page's buffer is larger than a chunk can handle, we need find "maximum buffer we can pushinto", and segment the arrow array to multiple sub-chunks, then switch to next batch in this case?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, basically something like this: int64_t start = 0;
while (start < num_non_null_values) {
// Find the number of values that we can write in this chunk
int64_t chunk_data_length = 0;
for (int64_t end = start; end < num_non_null_values; ++end) {
const int64_t length = length_ptr[end];
if (ARROW_PREDICT_FALSE(length < 0)) {
return Status::Invalid("negative string delta length");
}
if (chunk_data_length + length > out->chunk_space_remaining()) {
break;
}
chunk_data_length += length;
}
// Write chunk [start, end)
...
out->PushChunk();
start = end;
}
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this should also find out that, given a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahem... that's a good point. Ideally we would need something like
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, seems this requires a underlying "loop" abstraction which could hint "next-value", "end", "break"...This also requires handling the all-nulls values. So I prefer using this fast-path firstly. This abstraction might be like:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So should I move forward with some slow stride checking or leave it a fastpath here? @pitrou
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah! Well, I was underestimating the complexity of the clean solution, so leaving it as a fast path sounds fine.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The remaining problem, though, is that the fallback isn't tested?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think fallback is tested but I'll check this, it's a bit hard to reserve so large memory for testing though |
||
| std::vector<ByteArray> 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)) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But what happens if
accum_length > chunk_space_remaining_. Would it just fail? It should probably append a new chunk.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would never happens,
chunk_space_remaining_ >= decoder_->bytes_left() >= accum_length, so this would not happensThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But why is
chunk_space_remaining_ >= decoder_->bytes_left()?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, this is because we know that
CanFit(decoder_->bytes_left()), right? Adding a comment might help...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added here: c21878c