-
Notifications
You must be signed in to change notification settings - Fork 2k
perf: Cache num_output_rows in sort merge join to avoid O(n) recount #20478
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
Open
andygrove
wants to merge
2
commits into
apache:main
Choose a base branch
from
andygrove:cache-smj-num-output-rows
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.
+13
−16
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -126,6 +126,8 @@ pub(super) struct StreamedBatch { | |
| pub join_arrays: Vec<ArrayRef>, | ||
| /// Chunks of indices from buffered side (may be nulls) joined to streamed | ||
| pub output_indices: Vec<StreamedJoinedChunk>, | ||
| /// Total number of output rows across all chunks in `output_indices` | ||
| pub num_output_rows: usize, | ||
| /// Index of currently scanned batch from buffered data | ||
| pub buffered_batch_idx: Option<usize>, | ||
| /// Indices that found a match for the given join filter | ||
|
|
@@ -142,6 +144,7 @@ impl StreamedBatch { | |
| idx: 0, | ||
| join_arrays, | ||
| output_indices: vec![], | ||
| num_output_rows: 0, | ||
| buffered_batch_idx: None, | ||
| join_filter_matched_idxs: HashSet::new(), | ||
| } | ||
|
|
@@ -153,17 +156,15 @@ impl StreamedBatch { | |
| idx: 0, | ||
| join_arrays: vec![], | ||
| output_indices: vec![], | ||
| num_output_rows: 0, | ||
| buffered_batch_idx: None, | ||
| join_filter_matched_idxs: HashSet::new(), | ||
| } | ||
| } | ||
|
|
||
| /// Number of unfrozen output pairs in this streamed batch | ||
| fn num_output_rows(&self) -> usize { | ||
| self.output_indices | ||
| .iter() | ||
| .map(|chunk| chunk.streamed_indices.len()) | ||
| .sum() | ||
| self.num_output_rows | ||
| } | ||
|
|
||
| /// Appends new pair consisting of current streamed index and `buffered_idx` | ||
|
|
@@ -173,20 +174,20 @@ impl StreamedBatch { | |
| buffered_batch_idx: Option<usize>, | ||
| buffered_idx: Option<usize>, | ||
| batch_size: usize, | ||
| num_unfrozen_pairs: usize, | ||
| ) { | ||
| // If no current chunk exists or current chunk is not for current buffered batch, | ||
| // create a new chunk | ||
| if self.output_indices.is_empty() || self.buffered_batch_idx != buffered_batch_idx | ||
| { | ||
| // Compute capacity only when creating a new chunk (infrequent operation). | ||
| // The capacity is the remaining space to reach batch_size. | ||
| // This should always be >= 1 since we only call this when num_unfrozen_pairs < batch_size. | ||
| // This should always be >= 1 since we only call this when num_output_rows < batch_size. | ||
| debug_assert!( | ||
| batch_size > num_unfrozen_pairs, | ||
| "batch_size ({batch_size}) must be > num_unfrozen_pairs ({num_unfrozen_pairs})" | ||
| batch_size > self.num_output_rows, | ||
| "batch_size ({batch_size}) must be > num_output_rows ({})", | ||
| self.num_output_rows | ||
| ); | ||
| let capacity = batch_size - num_unfrozen_pairs; | ||
| let capacity = batch_size - self.num_output_rows; | ||
| self.output_indices.push(StreamedJoinedChunk { | ||
| buffered_batch_idx, | ||
| streamed_indices: UInt64Builder::with_capacity(capacity), | ||
|
|
@@ -203,6 +204,7 @@ impl StreamedBatch { | |
| } else { | ||
| current_chunk.buffered_indices.append_null(); | ||
| } | ||
| self.num_output_rows += 1; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1100,13 +1102,10 @@ impl SortMergeJoinStream { | |
| let scanning_idx = self.buffered_data.scanning_idx(); | ||
| if join_streamed { | ||
| // Join streamed row and buffered row | ||
| // Pass batch_size and num_unfrozen_pairs to compute capacity only when | ||
| // creating a new chunk (when buffered_batch_idx changes), not on every iteration. | ||
| self.streamed_batch.append_output_pair( | ||
| Some(self.buffered_data.scanning_batch_idx), | ||
| Some(scanning_idx), | ||
| self.batch_size, | ||
| self.num_unfrozen_pairs(), | ||
| ); | ||
| } else { | ||
| // Join nulls and buffered row for FULL join | ||
|
|
@@ -1132,13 +1131,10 @@ impl SortMergeJoinStream { | |
| // For Mark join we store a dummy id to indicate the row has a match | ||
| let scanning_idx = mark_row_as_match.then_some(0); | ||
|
|
||
| // Pass batch_size=1 and num_unfrozen_pairs=0 to get capacity of 1, | ||
| // since we only append a single null-joined pair here (not in a loop). | ||
| self.streamed_batch.append_output_pair( | ||
| scanning_batch_idx, | ||
| scanning_idx, | ||
| 1, | ||
| 0, | ||
| self.batch_size, | ||
| ); | ||
| self.buffered_data.scanning_finish(); | ||
| self.streamed_joined = true; | ||
|
|
@@ -1437,6 +1433,7 @@ impl SortMergeJoinStream { | |
| } | ||
|
|
||
| self.streamed_batch.output_indices.clear(); | ||
| self.streamed_batch.num_output_rows = 0; | ||
|
Contributor
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 we should probably encapsulate this reset pattern into its own function? (self.reset() calls .clear() and sets num_output_rows = 0) |
||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
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.
nit: i think this function can be removed, we can call
num_unfrozen_pairs()asstreamed_batch.num_output_rows. and add a small comment for thenum_output_rowsfield to declare that it represents unfrozen pairs