-
Notifications
You must be signed in to change notification settings - Fork 936
Simplify ParquetRecordBatchReader::next
control logic
#7512
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
Merged
Merged
Changes from all commits
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 hidden or 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 |
---|---|---|
|
@@ -800,24 +800,33 @@ impl Iterator for ParquetRecordBatchReader { | |
type Item = Result<RecordBatch, ArrowError>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
self.next_inner() | ||
.map_err(|arrow_err| arrow_err.into()) | ||
.transpose() | ||
} | ||
} | ||
|
||
impl ParquetRecordBatchReader { | ||
/// Returns the next `RecordBatch` from the reader, or `None` if the reader | ||
/// has reached the end of the file. | ||
/// | ||
/// Returns `Result<Option<..>>` rather than `Option<Result<..>>` to | ||
/// simplify error handling with `?` | ||
fn next_inner(&mut self) -> Result<Option<RecordBatch>> { | ||
let mut read_records = 0; | ||
match self.selection.as_mut() { | ||
Some(selection) => { | ||
while read_records < self.batch_size && !selection.is_empty() { | ||
let front = selection.pop_front().unwrap(); | ||
if front.skip { | ||
let skipped = match self.array_reader.skip_records(front.row_count) { | ||
Ok(skipped) => skipped, | ||
Err(e) => return Some(Err(e.into())), | ||
}; | ||
let skipped = self.array_reader.skip_records(front.row_count)?; | ||
|
||
if skipped != front.row_count { | ||
return Some(Err(general_err!( | ||
return Err(general_err!( | ||
"failed to skip rows, expected {}, got {}", | ||
front.row_count, | ||
skipped | ||
) | ||
.into())); | ||
)); | ||
} | ||
continue; | ||
} | ||
|
@@ -839,35 +848,27 @@ impl Iterator for ParquetRecordBatchReader { | |
} | ||
_ => front.row_count, | ||
}; | ||
match self.array_reader.read_records(to_read) { | ||
Ok(0) => break, | ||
Ok(rec) => read_records += rec, | ||
Err(error) => return Some(Err(error.into())), | ||
} | ||
match self.array_reader.read_records(to_read)? { | ||
0 => break, | ||
rec => read_records += rec, | ||
}; | ||
} | ||
} | ||
None => { | ||
if let Err(error) = self.array_reader.read_records(self.batch_size) { | ||
return Some(Err(error.into())); | ||
} | ||
self.array_reader.read_records(self.batch_size)?; | ||
} | ||
}; | ||
|
||
match self.array_reader.consume_batch() { | ||
Err(error) => Some(Err(error.into())), | ||
Ok(array) => { | ||
let struct_array = array.as_struct_opt().ok_or_else(|| { | ||
ArrowError::ParquetError( | ||
"Struct array reader should return struct array".to_string(), | ||
) | ||
}); | ||
let array = self.array_reader.consume_batch()?; | ||
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 this is a pretty good example of how the logic gets simpler |
||
let struct_array = array.as_struct_opt().ok_or_else(|| { | ||
ArrowError::ParquetError("Struct array reader should return struct array".to_string()) | ||
})?; | ||
|
||
match struct_array { | ||
Err(err) => Some(Err(err)), | ||
Ok(e) => (e.len() > 0).then(|| Ok(RecordBatch::from(e))), | ||
} | ||
} | ||
} | ||
Ok(if struct_array.len() > 0 { | ||
Some(RecordBatch::from(struct_array)) | ||
} else { | ||
None | ||
}) | ||
} | ||
} | ||
|
||
|
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.
Basically this PR consolidates the conversion of converting to
ParquetError
and transposing to a single location rather than inlining it several places innext()