-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Prune hash join probe side via a build-side key range bitmap #25602
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
gruuya
wants to merge
2
commits into
apache:main
Choose a base branch
from
splitgraph:hash-join-dynamic-pruning-bitmap
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.
+774
−42
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 | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -40,6 +40,7 @@ use crate::joins::hash_join::stream::{ | |||||||||||||||||||||||||||||
| BuildSide, BuildSideInitialState, HashJoinStream, HashJoinStreamState, | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
| use crate::joins::join_hash_map::{JoinHashMapU32, JoinHashMapU64}; | ||||||||||||||||||||||||||||||
| use crate::joins::key_range_bitmap::KeyRangeBitmap; | ||||||||||||||||||||||||||||||
| use crate::joins::utils::{ | ||||||||||||||||||||||||||||||
| OnceAsync, OnceFut, asymmetric_join_output_partitioning, emits_unmatched_left_rows, | ||||||||||||||||||||||||||||||
| is_existence_join, reorder_output_after_swap, swap_join_projection, update_hash, | ||||||||||||||||||||||||||||||
|
|
@@ -3084,7 +3085,9 @@ async fn collect_left_input( | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| let map = Arc::new(join_hash_map); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| let membership = if num_rows == 0 { | ||||||||||||||||||||||||||||||
| // Nothing reads the strategy unless the dynamic filter accumulator exists, | ||||||||||||||||||||||||||||||
| // and that exists only when the pushdown is enabled. | ||||||||||||||||||||||||||||||
| let membership = if num_rows == 0 || !should_compute_dynamic_filters { | ||||||||||||||||||||||||||||||
| PushdownStrategy::Empty | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| // If the build side is small enough we can use IN list pushdown. | ||||||||||||||||||||||||||||||
|
|
@@ -3094,19 +3097,41 @@ async fn collect_left_input( | |||||||||||||||||||||||||||||
| .iter() | ||||||||||||||||||||||||||||||
| .map(|arr| arr.get_array_memory_size()) | ||||||||||||||||||||||||||||||
| .sum::<usize>(); | ||||||||||||||||||||||||||||||
| if left_values.is_empty() | ||||||||||||||||||||||||||||||
| || left_values[0].is_empty() | ||||||||||||||||||||||||||||||
| || estimated_size > config.optimizer.hash_join_inlist_pushdown_max_size | ||||||||||||||||||||||||||||||
| || map.num_of_distinct_key() | ||||||||||||||||||||||||||||||
| > config | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| let pushdown_inlist = !left_values.is_empty() | ||||||||||||||||||||||||||||||
| && !left_values[0].is_empty() | ||||||||||||||||||||||||||||||
| && estimated_size <= config.optimizer.hash_join_inlist_pushdown_max_size | ||||||||||||||||||||||||||||||
| && map.num_of_distinct_key() | ||||||||||||||||||||||||||||||
| <= config | ||||||||||||||||||||||||||||||
| .optimizer | ||||||||||||||||||||||||||||||
| .hash_join_inlist_pushdown_max_distinct_values | ||||||||||||||||||||||||||||||
| .hash_join_inlist_pushdown_max_distinct_values; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if pushdown_inlist | ||||||||||||||||||||||||||||||
| && let Some(in_list_values) = build_struct_inlist_values(&left_values)? | ||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||
| PushdownStrategy::Map(Arc::clone(&map)) | ||||||||||||||||||||||||||||||
| } else if let Some(in_list_values) = build_struct_inlist_values(&left_values)? { | ||||||||||||||||||||||||||||||
| PushdownStrategy::InList(in_list_values) | ||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||
| PushdownStrategy::Map(Arc::clone(&map)) | ||||||||||||||||||||||||||||||
| // Past the InList threshold use a bucket bitmap for container pruning. | ||||||||||||||||||||||||||||||
| let pruning_bitmap = match (left_values.as_slice(), bounds.as_ref()) { | ||||||||||||||||||||||||||||||
| ([keys], Some(bounds)) if !keys.is_empty() => bounds | ||||||||||||||||||||||||||||||
| .get_column_bounds(0) | ||||||||||||||||||||||||||||||
| .and_then(|b| { | ||||||||||||||||||||||||||||||
| KeyRangeBitmap::try_new( | ||||||||||||||||||||||||||||||
| keys, | ||||||||||||||||||||||||||||||
| &b.min, | ||||||||||||||||||||||||||||||
| &b.max, | ||||||||||||||||||||||||||||||
| map.num_of_distinct_key(), | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||
| .map(Arc::new), | ||||||||||||||||||||||||||||||
| _ => None, | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
| if let Some(bitmap) = pruning_bitmap.as_ref() { | ||||||||||||||||||||||||||||||
| // Held for the join's lifetime, so charge it like the maps. | ||||||||||||||||||||||||||||||
| reservation.try_grow(bitmap.size())?; | ||||||||||||||||||||||||||||||
| metrics.build_mem_used.add(bitmap.size()); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+3129
to
+3133
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. The bitmap is only a pruning aid, but
Suggested change
|
||||||||||||||||||||||||||||||
| PushdownStrategy::Map(Arc::clone(&map), pruning_bitmap) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
@@ -3264,6 +3289,53 @@ mod tests { | |||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| #[tokio::test] | ||||||||||||||||||||||||||||||
| async fn no_pruning_state_without_dynamic_filters() -> Result<()> { | ||||||||||||||||||||||||||||||
| let schema = Arc::new(Schema::new(vec![Field::new("k", DataType::Int64, false)])); | ||||||||||||||||||||||||||||||
| let build = RecordBatch::try_new( | ||||||||||||||||||||||||||||||
| Arc::clone(&schema), | ||||||||||||||||||||||||||||||
| vec![Arc::new(Int64Array::from_iter_values( | ||||||||||||||||||||||||||||||
| (0..151).map(|i| i * 10_000), | ||||||||||||||||||||||||||||||
| ))], | ||||||||||||||||||||||||||||||
| )?; | ||||||||||||||||||||||||||||||
| let probe = RecordBatch::try_new( | ||||||||||||||||||||||||||||||
| Arc::clone(&schema), | ||||||||||||||||||||||||||||||
| vec![Arc::new(Int64Array::from_iter_values([ | ||||||||||||||||||||||||||||||
| 0, 500_000, 1_500_000, | ||||||||||||||||||||||||||||||
| ]))], | ||||||||||||||||||||||||||||||
| )?; | ||||||||||||||||||||||||||||||
| let on = vec![( | ||||||||||||||||||||||||||||||
| Arc::new(Column::new_with_schema("k", &schema)?) as _, | ||||||||||||||||||||||||||||||
| Arc::new(Column::new_with_schema("k", &schema)?) as _, | ||||||||||||||||||||||||||||||
| )]; | ||||||||||||||||||||||||||||||
| let join = join( | ||||||||||||||||||||||||||||||
| TestMemoryExec::try_new_exec(&[vec![build]], Arc::clone(&schema), None)?, | ||||||||||||||||||||||||||||||
| TestMemoryExec::try_new_exec(&[vec![probe]], Arc::clone(&schema), None)?, | ||||||||||||||||||||||||||||||
| on, | ||||||||||||||||||||||||||||||
| &JoinType::Inner, | ||||||||||||||||||||||||||||||
| NullEquality::NullEqualsNothing, | ||||||||||||||||||||||||||||||
| )?; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Bounds are still collected to test perfect-hash-join candidacy, so 151 | ||||||||||||||||||||||||||||||
| // keys over 1.5M would size a bitmap at the 128 KiB cap - far past this. | ||||||||||||||||||||||||||||||
| let runtime = RuntimeEnvBuilder::new() | ||||||||||||||||||||||||||||||
| .with_memory_limit(100_000, 1.0) | ||||||||||||||||||||||||||||||
| .build_arc()?; | ||||||||||||||||||||||||||||||
| let mut config = SessionConfig::new(); | ||||||||||||||||||||||||||||||
| config | ||||||||||||||||||||||||||||||
| .options_mut() | ||||||||||||||||||||||||||||||
| .optimizer | ||||||||||||||||||||||||||||||
| .enable_join_dynamic_filter_pushdown = false; | ||||||||||||||||||||||||||||||
| let task_ctx = Arc::new( | ||||||||||||||||||||||||||||||
| TaskContext::default() | ||||||||||||||||||||||||||||||
| .with_runtime(runtime) | ||||||||||||||||||||||||||||||
| .with_session_config(config), | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
| let batches = common::collect(join.execute(0, task_ctx)?).await?; | ||||||||||||||||||||||||||||||
| assert_eq!(batches.iter().map(|b| b.num_rows()).sum::<usize>(), 3); | ||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| #[derive(Debug)] | ||||||||||||||||||||||||||||||
| struct PartitionedTestExec { | ||||||||||||||||||||||||||||||
| cache: Arc<PlanProperties>, | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
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
Oops, something went wrong.
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.
[P2] Skip bitmap construction when dynamic filtering is inactive
boundscan exist solely for perfect-hash-join candidacy even whenshould_compute_dynamic_filtersis false. This branch still constructs and reserves a pruning bitmap before those bounds are cleared below, so a join with dynamic filtering disabled can now fail for memory that provides no pruning benefit.Using a native
CollectLeftjoin with 151 Int64 build keysi * 10_000(i = 0..151), probe keys[0, 500_000, 1_500_000], andenable_join_dynamic_filter_pushdown=false(otherwise default configuration), base714956b3succeeds in a 100,000-byte memory pool with 6,220 bytes reserved. Headadbb3ce4fails withResourcesExhaustedrequesting another 131,072 bytes. At a 1,000,000-byte limit both return the exact expected rows, but head reserves 137,292 bytes. I also reproduced the same failure with a Full join.Could we gate this bitmap construction on
should_compute_dynamic_filtersand cover the disabled-filter case with a memory-limit regression test?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.
Nice catch. I also noticed that the same problem happens for the InList path too.
So instead of guarding just the (bit)map path with
should_compute_dynamic_filters, i made it guard both by extending thePushdownStrategy::Emptyarm with|| !should_compute_dynamic_filters; let me know if you see issues with this.Also a unit test added.