-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-12254: [Rust][DataFusion] Stop polling limit input once limit is reached #9926
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
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 | ||
|---|---|---|---|---|
|
|
@@ -200,30 +200,39 @@ pub fn truncate_batch(batch: &RecordBatch, n: usize) -> RecordBatch { | |||
|
|
||||
| /// A Limit stream limits the stream to up to `limit` rows. | ||||
| struct LimitStream { | ||||
| /// The maximum number of rows to produce | ||||
| limit: usize, | ||||
| input: SendableRecordBatchStream, | ||||
| // the current count | ||||
| /// The input to read from. This is set to None once the limit is | ||||
| /// reached to enable early termination | ||||
alamb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
| input: Option<SendableRecordBatchStream>, | ||||
| /// Copy of the input schema | ||||
| schema: SchemaRef, | ||||
| // the current number of rows which have been produced | ||||
| current_len: usize, | ||||
| } | ||||
|
|
||||
| impl LimitStream { | ||||
| fn new(input: SendableRecordBatchStream, limit: usize) -> Self { | ||||
| let schema = input.schema(); | ||||
|
||||
| Self { | ||||
| limit, | ||||
| input, | ||||
| input: Some(input), | ||||
| schema, | ||||
| current_len: 0, | ||||
| } | ||||
| } | ||||
|
|
||||
| fn stream_limit(&mut self, batch: RecordBatch) -> Option<RecordBatch> { | ||||
| if self.current_len == self.limit { | ||||
| self.input = None; // clear input so it can be dropped early | ||||
| None | ||||
| } else if self.current_len + batch.num_rows() <= self.limit { | ||||
| self.current_len += batch.num_rows(); | ||||
| Some(batch) | ||||
| } else { | ||||
| let batch_rows = self.limit - self.current_len; | ||||
| self.current_len = self.limit; | ||||
| self.input = None; // clear input so it can be dropped early | ||||
| Some(truncate_batch(&batch, batch_rows)) | ||||
| } | ||||
| } | ||||
|
|
@@ -236,23 +245,29 @@ impl Stream for LimitStream { | |||
| mut self: Pin<&mut Self>, | ||||
| cx: &mut Context<'_>, | ||||
| ) -> Poll<Option<Self::Item>> { | ||||
| self.input.poll_next_unpin(cx).map(|x| match x { | ||||
| Some(Ok(batch)) => Ok(self.stream_limit(batch)).transpose(), | ||||
| other => other, | ||||
| }) | ||||
| match &mut self.input { | ||||
|
||||
| Ok(Box::pin(ParquetStream { |
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.
As far as I understand it now, it can consist of the whole tree of dependent streams. Probably still not a big resource hog but more than a few bytes.
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.
Imagine it is actually a subquery with a group by hash or join with a large hash table :) It may actually be hanging on to a substantial amount of memory I suspect
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, yeah that's right!
Uh oh!
There was an error while loading. Please reload this page.
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.
Is this necessary? It can be based on
self.current_len == self.limitor otherwise a boolean likelimit_exhausted?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.
Snap - #9926 (comment)
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.
haha 👍 😆