-
Notifications
You must be signed in to change notification settings - Fork 860
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
fix: first none/empty list in ListArray
panics in cast_with_options
#7065
Open
irenjj
wants to merge
4
commits into
apache:main
Choose a base branch
from
irenjj:cast_firt_none
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.
Open
Changes from 1 commit
Commits
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
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -88,11 +88,16 @@ where | |||||||||||||||||||||||||||||||||||||||||||
let mut mutable = MutableArrayData::new(vec![&values], nullable, cap); | ||||||||||||||||||||||||||||||||||||||||||||
// The end position in values of the last incorrectly-sized list slice | ||||||||||||||||||||||||||||||||||||||||||||
let mut last_pos = 0; | ||||||||||||||||||||||||||||||||||||||||||||
let mut null_first = false; | ||||||||||||||||||||||||||||||||||||||||||||
for (idx, w) in array.offsets().windows(2).enumerate() { | ||||||||||||||||||||||||||||||||||||||||||||
let start_pos = w[0].as_usize(); | ||||||||||||||||||||||||||||||||||||||||||||
let end_pos = w[1].as_usize(); | ||||||||||||||||||||||||||||||||||||||||||||
let len = end_pos - start_pos; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if start_pos == 0 && end_pos == 0 { | ||||||||||||||||||||||||||||||||||||||||||||
null_first = true; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if len != size as usize { | ||||||||||||||||||||||||||||||||||||||||||||
if cast_options.safe || array.is_null(idx) { | ||||||||||||||||||||||||||||||||||||||||||||
if last_pos != start_pos { | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -112,16 +117,15 @@ where | |||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
let values = match last_pos { | ||||||||||||||||||||||||||||||||||||||||||||
0 => array.values().slice(0, cap), // All slices were the correct length | ||||||||||||||||||||||||||||||||||||||||||||
_ => { | ||||||||||||||||||||||||||||||||||||||||||||
if mutable.len() != cap { | ||||||||||||||||||||||||||||||||||||||||||||
// Remaining slices were all correct length | ||||||||||||||||||||||||||||||||||||||||||||
let remaining = cap - mutable.len(); | ||||||||||||||||||||||||||||||||||||||||||||
mutable.extend(0, last_pos, last_pos + remaining) | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
make_array(mutable.freeze()) | ||||||||||||||||||||||||||||||||||||||||||||
let values = if last_pos == 0 && !null_first { | ||||||||||||||||||||||||||||||||||||||||||||
array.values().slice(0, cap) // All slices were the correct length | ||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||
if mutable.len() != cap { | ||||||||||||||||||||||||||||||||||||||||||||
// Remaining slices were all correct length | ||||||||||||||||||||||||||||||||||||||||||||
let remaining = cap - mutable.len(); | ||||||||||||||||||||||||||||||||||||||||||||
mutable.extend(0, last_pos, last_pos + remaining) | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
make_array(mutable.freeze()) | ||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
nit: using guard in the match arm would reduce the diff happening here |
||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
// Cast the inner values if necessary | ||||||||||||||||||||||||||||||||||||||||||||
|
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
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 don't believe it's about nulls being first, rather about a potentially empty list being first. Consider this test case which also panics during cast:
This distinction is important as nullability isn't determined by the gap between offsets, rather it's determined by the null buffer.
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.
Thanks @Jefffrey for your review, in my local testing,
null_first
can identify both None and empty vec (the namenull_first
might be a bit misleading). My idea is to usenull_first
to prevent entering thelet values = match last_pos { 0 => }
situation.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 think I understand what's happening now. It seems its an edge case of the algorithm used here, where a 0 sized list element precedes a list of the proper size, e.g.
Where
last_pos
is never updated from 0 becauseend_pos
for the first two elements is0
andlast_pos
doesn't get updated when the list element already matches the fixed size to cast to.(Sorry if this was already obvious, just needed to air my thoughts out)
Whilst this fix is correct, I'd like some more documentation, and especially rename
null_first
to something more accurate (as the problem is with empty size lists too), to explain this edge case so it's obvious to anyone else modifying the code here in the future.We could also look into moving the:
Check outside of the for loop as we only need to check it once upfront.