-
Notifications
You must be signed in to change notification settings - Fork 972
[Variant] Avoid collecting offset iterator #7934
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
[Variant] Avoid collecting offset iterator #7934
Conversation
Signed-off-by: codephage2020 <[email protected]>
Signed-off-by: codephage2020 <[email protected]>
Signed-off-by: codephage2020 <[email protected]>
CC @friendlymatthew @alamb. Would it be possible for you to review this PR at your convenience? Thank you in advance. |
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.
Thank you @codephage2020 -- I went through this PR carefully and it all makes sense to me and I think it is good to merge
cc @scovich @Samyak2 and @friendlymatthew
return Err(ArrowError::InvalidArgumentError( | ||
"field names not sorted".to_string(), | ||
)); | ||
let mut current_field_name = match field_ids_iter.next() { |
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 wasn't added in this PR, but this check for the field names being sorted doesn't seem right to me -- I thought the only requirement on an object's fields were that the field_ids were sorted (so lookup by field_id can be fast) but the corresponding names of the fields don't have to be sorted
Maybe @friendlymatthew can 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.
Yes, you are right. VariantEncoding.md is described like this.
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.
Hi, this is how I understood the spec
The field ids and field offsets must be in lexicographical order of the corresponding field names in the metadata dictionary
If we have an object with a sorted dictionary, the field ids are already ordered by the lexicographical order of field names.
If we don't have a sorted dictionary, we iterate through the field ids and probe the object for the corresponding field name. If the field names are lexicographically ordered, we can also verify that the field ids are in lexicographical order.
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.
Got it -- I re-read the spec and I was confused and agree this check is doign the right thing
The field ids and field offsets must be in lexicographical order of the corresponding field names in the metadata dictionary. However, the actual value entries do not need to be in any particular order. This implies that the field_offset values may not be monotonically increasing. For example, for the following object:
// we also know all field ids are smaller than the dictionary size and in-bounds. | ||
if let Some(&last_field_id) = field_ids.last() { | ||
if last_field_id >= self.metadata.dictionary_size() { | ||
if current_id >= dictionary_size { |
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 took me a few times to figure out why this (redundant) check was still needed
I am not sure if there is some way to refactor the loop to avoid this (perhaps by keeping previous_id: Option<u32>
as you did in the loop above 🤔
No changes needed, I just figured I would point it out
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.
Sorry, I didn't expect this to be confusing. This loop has considered many ways of writing, and in the end, I referred to https://github.com/apache/arrow-rs/blob/main/parquet-variant/src/variant/list.rs#L225-L232. I think this is an elegant implementation. Perhaps I need to add comments or use the following writing style to make the code more readable.
let mut previous_id: Option<u32> = None;
for field_id in field_ids_iter {
if field_id >= dictionary_size {
return Err(ArrowError::InvalidArgumentError(
"field id is not valid".to_string(),
));
}
if let Some(prev_id) = previous_id {
if field_id <= prev_id {
return Err(ArrowError::InvalidArgumentError(
"field names not sorted".to_string(),
));
}
}
previous_id = Some(field_id);
}
🤖 |
🤖: Benchmark completed Details
|
🤖 |
🤔 interesting that the benchmark seems to imply this approach is slower than collecting in a Vec
I reran to see if we can see a difference |
🤖: Benchmark completed Details
|
Next benchmark run looks good to me |
🚀 |
Thanks agian @codephage2020 and @friendlymatthew |
Which issue does this PR close?
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax.
Rationale for this change
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
What changes are included in this PR?
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
Are these changes tested?
We typically require tests for all PRs in order to:
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
Are there any user-facing changes?
If there are user-facing changes then we may require documentation to be updated before approving the PR.
If there are any breaking changes to public APIs, please call them out.