-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix: Assertion fail in external sort #15469
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
Show all changes
3 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
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 |
---|---|---|
|
@@ -216,10 +216,8 @@ struct ExternalSorter { | |
// STATE BUFFERS: | ||
// Fields that hold intermediate data during sorting | ||
// ======================================================================== | ||
/// Potentially unsorted in memory buffer | ||
/// Unsorted input batches stored in the memory buffer | ||
in_mem_batches: Vec<RecordBatch>, | ||
/// if `Self::in_mem_batches` are sorted | ||
in_mem_batches_sorted: bool, | ||
|
||
/// During external sorting, in-memory intermediate data will be appended to | ||
/// this file incrementally. Once finished, this file will be moved to [`Self::finished_spill_files`]. | ||
|
@@ -284,7 +282,6 @@ impl ExternalSorter { | |
Self { | ||
schema, | ||
in_mem_batches: vec![], | ||
in_mem_batches_sorted: false, | ||
in_progress_spill_file: None, | ||
finished_spill_files: vec![], | ||
expr: expr.into(), | ||
|
@@ -320,7 +317,6 @@ impl ExternalSorter { | |
} | ||
|
||
self.in_mem_batches.push(input); | ||
self.in_mem_batches_sorted = false; | ||
Ok(()) | ||
} | ||
|
||
|
@@ -397,16 +393,13 @@ impl ExternalSorter { | |
self.metrics.spill_metrics.spill_file_count.value() | ||
} | ||
|
||
/// When calling, all `in_mem_batches` must be sorted (*), and then all of them will | ||
/// be appended to the in-progress spill file. | ||
/// | ||
/// (*) 'Sorted' here means globally sorted for all buffered batches when the | ||
/// memory limit is reached, instead of partially sorted within the batch. | ||
async fn spill_append(&mut self) -> Result<()> { | ||
assert!(self.in_mem_batches_sorted); | ||
|
||
// we could always get a chance to free some memory as long as we are holding some | ||
if self.in_mem_batches.is_empty() { | ||
/// Appending globally sorted batches to the in-progress spill file, and clears | ||
/// the `globally_sorted_batches` (also its memory reservation) afterwards. | ||
async fn consume_and_spill_append( | ||
&mut self, | ||
globally_sorted_batches: &mut Vec<RecordBatch>, | ||
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. thank you for the name here -- much better |
||
) -> Result<()> { | ||
if globally_sorted_batches.is_empty() { | ||
return Ok(()); | ||
} | ||
|
||
|
@@ -416,21 +409,25 @@ impl ExternalSorter { | |
Some(self.spill_manager.create_in_progress_file("Sorting")?); | ||
} | ||
|
||
self.organize_stringview_arrays()?; | ||
Self::organize_stringview_arrays(globally_sorted_batches)?; | ||
|
||
debug!("Spilling sort data of ExternalSorter to disk whilst inserting"); | ||
|
||
let batches = std::mem::take(&mut self.in_mem_batches); | ||
let batches_to_spill = std::mem::take(globally_sorted_batches); | ||
self.reservation.free(); | ||
|
||
let in_progress_file = self.in_progress_spill_file.as_mut().ok_or_else(|| { | ||
internal_datafusion_err!("In-progress spill file should be initialized") | ||
})?; | ||
|
||
for batch in batches { | ||
for batch in batches_to_spill { | ||
in_progress_file.append_batch(&batch)?; | ||
} | ||
|
||
if !globally_sorted_batches.is_empty() { | ||
return internal_err!("This function consumes globally_sorted_batches, so it should be empty after taking."); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
|
@@ -449,7 +446,7 @@ impl ExternalSorter { | |
Ok(()) | ||
} | ||
|
||
/// Reconstruct `self.in_mem_batches` to organize the payload buffers of each | ||
/// Reconstruct `globally_sorted_batches` to organize the payload buffers of each | ||
/// `StringViewArray` in sequential order by calling `gc()` on them. | ||
/// | ||
/// Note this is a workaround until <https://github.com/apache/arrow-rs/issues/7185> is | ||
|
@@ -478,10 +475,12 @@ impl ExternalSorter { | |
/// | ||
/// Then when spilling each batch, the writer has to write all referenced buffers | ||
/// repeatedly. | ||
fn organize_stringview_arrays(&mut self) -> Result<()> { | ||
let mut organized_batches = Vec::with_capacity(self.in_mem_batches.len()); | ||
fn organize_stringview_arrays( | ||
globally_sorted_batches: &mut Vec<RecordBatch>, | ||
) -> Result<()> { | ||
let mut organized_batches = Vec::with_capacity(globally_sorted_batches.len()); | ||
|
||
for batch in self.in_mem_batches.drain(..) { | ||
for batch in globally_sorted_batches.drain(..) { | ||
let mut new_columns: Vec<Arc<dyn Array>> = | ||
Vec::with_capacity(batch.num_columns()); | ||
|
||
|
@@ -507,20 +506,17 @@ impl ExternalSorter { | |
organized_batches.push(organized_batch); | ||
} | ||
|
||
self.in_mem_batches = organized_batches; | ||
*globally_sorted_batches = organized_batches; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Sorts the in_mem_batches in place | ||
/// Sorts the in_mem_batches and potentially spill the sorted batches. | ||
/// | ||
/// Sorting may have freed memory, especially if fetch is `Some`. If | ||
/// the memory usage has dropped by a factor of 2, then we don't have | ||
/// to spill. Otherwise, we spill to free up memory for inserting | ||
/// more batches. | ||
/// The factor of 2 aims to avoid a degenerate case where the | ||
/// memory required for `fetch` is just under the memory available, | ||
/// causing repeated re-sorting of data | ||
/// If the memory usage has dropped by a factor of 2, it might be a sort with | ||
/// fetch (e.g. sorting 1M rows but only keep the top 100), so we keep the | ||
/// sorted entries inside `in_mem_batches` to be sorted in the next iteration. | ||
/// Otherwise, we spill the sorted run to free up memory for inserting more batches. | ||
/// | ||
/// # Arguments | ||
/// | ||
|
@@ -539,10 +535,18 @@ impl ExternalSorter { | |
|
||
let mut sorted_stream = | ||
self.in_mem_sort_stream(self.metrics.baseline.intermediate())?; | ||
// After `in_mem_sort_stream()` is constructed, all `in_mem_batches` is taken | ||
// to construct a globally sorted stream. | ||
if !self.in_mem_batches.is_empty() { | ||
return internal_err!( | ||
"in_mem_batches should be empty after constructing sorted stream" | ||
); | ||
} | ||
// 'global' here refers to all buffered batches when the memory limit is | ||
// reached. This variable will buffer the sorted batches after | ||
// sort-preserving merge and incrementally append to spill files. | ||
let mut globally_sorted_batches: Vec<RecordBatch> = vec![]; | ||
|
||
// `self.in_mem_batches` is already taken away by the sort_stream, now it is empty. | ||
// We'll gradually collect the sorted stream into self.in_mem_batches, or directly | ||
// write sorted batches to disk when the memory is insufficient. | ||
let mut spilled = false; | ||
while let Some(batch) = sorted_stream.next().await { | ||
let batch = batch?; | ||
|
@@ -551,12 +555,12 @@ impl ExternalSorter { | |
// Although the reservation is not enough, the batch is | ||
// already in memory, so it's okay to combine it with previously | ||
// sorted batches, and spill together. | ||
self.in_mem_batches.push(batch); | ||
self.spill_append().await?; // reservation is freed in spill() | ||
globally_sorted_batches.push(batch); | ||
self.consume_and_spill_append(&mut globally_sorted_batches) | ||
.await?; // reservation is freed in spill() | ||
spilled = true; | ||
} else { | ||
self.in_mem_batches.push(batch); | ||
self.in_mem_batches_sorted = true; | ||
globally_sorted_batches.push(batch); | ||
} | ||
} | ||
|
||
|
@@ -570,12 +574,27 @@ impl ExternalSorter { | |
if (self.reservation.size() > before / 2) || force_spill { | ||
// We have not freed more than 50% of the memory, so we have to spill to | ||
// free up more memory | ||
self.spill_append().await?; | ||
self.consume_and_spill_append(&mut globally_sorted_batches) | ||
.await?; | ||
spilled = true; | ||
} | ||
|
||
if spilled { | ||
// There might be some buffered batches that haven't trigger a spill yet. | ||
self.consume_and_spill_append(&mut globally_sorted_batches) | ||
.await?; | ||
self.spill_finish().await?; | ||
} else { | ||
// If the memory limit has reached before calling this function, and it | ||
// didn't spill anything, it means this is a sorting with fetch top K | ||
// element: after sorting only the top K elements will be kept in memory. | ||
// For simplicity, those sorted top K entries are put back to unsorted | ||
// `in_mem_batches` to be consumed by the next sort/merge. | ||
if !self.in_mem_batches.is_empty() { | ||
return internal_err!("in_mem_batches should be cleared before"); | ||
} | ||
|
||
self.in_mem_batches = std::mem::take(&mut globally_sorted_batches); | ||
} | ||
|
||
// Reserve headroom for next sort/merge | ||
|
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.
Removing this field is an improvement 👍 thanks @2010YOUY01 and @comphead
Given there are still three fields whose relationship is tied, I wonder if it would improve the code if we encoded that relationship in an actual rust
enum
-- for example🤔
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.
👍🏼 will try it.