-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
GH-43926: [C++] Compute: RowEncoder eliminates offsets when all columns are fixed-sized #43931
Draft
mapleFU
wants to merge
6
commits into
apache:main
Choose a base branch
from
mapleFU:row-encoder-optimize-fixed-width
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−6
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7deeb8c
RowEncoder: remove calling for AddLength if all column are fixed
mapleFU 4d005c2
bugfix
mapleFU e3f2efa
debug cases
mapleFU df49d68
fixbug
mapleFU 6123737
Apply suggestions from code review
mapleFU 34e3474
fix typo
mapleFU File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,13 @@ struct ARROW_EXPORT KeyEncoder { | |
static bool IsNull(const uint8_t* encoded_bytes) { | ||
return encoded_bytes[0] == kNullByte; | ||
} | ||
|
||
struct EncoderInfo { | ||
bool is_fixed_width; | ||
int32_t fixed_width; | ||
}; | ||
|
||
virtual EncoderInfo GetEncoderInfo() const = 0; | ||
}; | ||
|
||
struct ARROW_EXPORT BooleanKeyEncoder : KeyEncoder { | ||
|
@@ -99,6 +106,10 @@ struct ARROW_EXPORT BooleanKeyEncoder : KeyEncoder { | |
|
||
Result<std::shared_ptr<ArrayData>> Decode(uint8_t** encoded_bytes, int32_t length, | ||
MemoryPool* pool) override; | ||
|
||
EncoderInfo GetEncoderInfo() const override { | ||
return EncoderInfo{/*is_fixed_width=*/true, 2}; | ||
} | ||
}; | ||
|
||
struct ARROW_EXPORT FixedWidthKeyEncoder : KeyEncoder { | ||
|
@@ -118,6 +129,10 @@ struct ARROW_EXPORT FixedWidthKeyEncoder : KeyEncoder { | |
Result<std::shared_ptr<ArrayData>> Decode(uint8_t** encoded_bytes, int32_t length, | ||
MemoryPool* pool) override; | ||
|
||
EncoderInfo GetEncoderInfo() const override { | ||
return EncoderInfo{/*is_fixed_width=*/true, byte_width_ + 1}; | ||
} | ||
|
||
std::shared_ptr<DataType> type_; | ||
const int byte_width_; | ||
}; | ||
|
@@ -132,6 +147,8 @@ struct ARROW_EXPORT DictionaryKeyEncoder : FixedWidthKeyEncoder { | |
Result<std::shared_ptr<ArrayData>> Decode(uint8_t** encoded_bytes, int32_t length, | ||
MemoryPool* pool) override; | ||
|
||
// Uses `GetEncoderInfo` in `FixedWidthKeyEncoder` | ||
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. What's this line for? |
||
|
||
MemoryPool* pool_; | ||
std::shared_ptr<Array> dictionary_; | ||
}; | ||
|
@@ -248,6 +265,10 @@ struct ARROW_EXPORT VarLengthKeyEncoder : KeyEncoder { | |
|
||
explicit VarLengthKeyEncoder(std::shared_ptr<DataType> type) : type_(std::move(type)) {} | ||
|
||
EncoderInfo GetEncoderInfo() const override { | ||
return EncoderInfo{/*is_fixed_width=*/false, /*fixed_width=*/5}; | ||
} | ||
|
||
std::shared_ptr<DataType> type_; | ||
}; | ||
|
||
|
@@ -267,6 +288,10 @@ struct ARROW_EXPORT NullKeyEncoder : KeyEncoder { | |
MemoryPool* pool) override { | ||
return ArrayData::Make(null(), length, {NULLPTR}, length); | ||
} | ||
|
||
EncoderInfo GetEncoderInfo() const override { | ||
return EncoderInfo{/*is_fixed_width=*/true, /*fixed_width=*/0}; | ||
} | ||
}; | ||
|
||
/// RowEncoder encodes ExecSpan to a variable length byte sequence | ||
|
@@ -348,23 +373,49 @@ class ARROW_EXPORT RowEncoder { | |
return std::string(reinterpret_cast<const char*>(encoded_nulls_.data()), | ||
encoded_nulls_.size()); | ||
} | ||
int32_t row_length = offsets_[i + 1] - offsets_[i]; | ||
return std::string(reinterpret_cast<const char*>(bytes_.data() + offsets_[i]), | ||
int32_t row_length = 0; | ||
int32_t row_offset = 0; | ||
if (IsFixedWidth()) { | ||
row_length = fixed_width_length_; | ||
row_offset = i * fixed_width_length_; | ||
} else { | ||
row_length = offsets_[i + 1] - offsets_[i]; | ||
row_offset = offsets_[i]; | ||
} | ||
return std::string(reinterpret_cast<const char*>(bytes_.data() + row_offset), | ||
row_length); | ||
} | ||
|
||
int32_t num_rows() const { | ||
if (IsFixedWidth()) { | ||
return fixed_width_row_count_; | ||
} | ||
return offsets_.empty() ? 0 : static_cast<int32_t>(offsets_.size() - 1); | ||
} | ||
|
||
private: | ||
Status EncodeAndAppendForFixedWidth(const ExecSpan& batch); | ||
bool IsFixedWidth() const noexcept { | ||
return fixed_width_length_ != kInvalidFixedWidthOffset; | ||
} | ||
|
||
private: | ||
static constexpr int32_t kInvalidFixedWidthOffset = 1; | ||
ExecContext* ctx_{nullptr}; | ||
std::vector<std::shared_ptr<KeyEncoder>> encoders_; | ||
// When all columns in a row are fixed-width or NA, the encoded row | ||
// doesn't need to maintain the row offsets. In this case, the | ||
// offsets_.size() would be also empty. | ||
int32_t fixed_width_length_{kInvalidFixedWidthOffset}; | ||
int32_t fixed_width_row_count_{0}; | ||
// offsets_ vector stores the starting position (offset) of each encoded row | ||
// within the bytes_ vector. This allows for quick access to individual rows. | ||
// | ||
// The size would be num_rows + 1 if not empty, the last element is the total | ||
// length of the bytes_ vector. | ||
// | ||
// When all columns in a row are fixed-width or NA, the offsets_ can be | ||
// eliminated, and the encoded row can be accessed by fixed_width_length_. | ||
std::vector<int32_t> offsets_; | ||
// The encoded bytes of all non "kRowIdForNulls" rows. | ||
std::vector<uint8_t> bytes_; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I would suggest using a more descriptive name -
XxInfo
generally doesn't give much information aboutXx
. Maybe something likeEncodeColumnMeta
?