Repository navigation
Do not broadcast join inputs when they are already co-partitioned. #25304
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -167,10 +167,15 @@ fn can_swap_hash_join(hash_join: &HashJoinExec) -> bool { | |
|
|
||
| /// Tries to create a [`HashJoinExec`] in [`PartitionMode::CollectLeft`] when possible. | ||
| /// | ||
| /// This function will first consider the given join type and check whether the | ||
| /// `CollectLeft` mode is applicable. Otherwise, it will try to swap the join sides. | ||
| /// When the `ignore_threshold` is false, this function will also check left | ||
| /// and right sizes in bytes or rows. | ||
| /// This function will first check whether the join inputs already satisfy the | ||
| /// distribution requirements for [`PartitionMode::Partitioned`]. If so, it returns | ||
| /// `None` to allow the caller to preserve or select [`PartitionMode::Partitioned`] | ||
| /// (task-local join with 0 shuffle) rather than degrading to broadcast (`CollectLeft`). | ||
| /// | ||
| /// Otherwise, it will consider the given join type and check whether the | ||
| /// `CollectLeft` mode is applicable. It will also try to swap the join sides if beneficial. | ||
| /// When the `ignore_threshold` is false, this function will check left | ||
| /// and right sizes in bytes or rows against the configured thresholds. | ||
| /// | ||
| /// Used configurations | ||
| /// - `optimizer.hash_join_single_partition_threshold`: byte threshold for `CollectLeft` | ||
|
|
@@ -181,6 +186,10 @@ pub(crate) fn try_collect_left( | |
| ignore_threshold: bool, | ||
| context: &dyn PhysicalOptimizerContext, | ||
| ) -> Result<Option<Arc<dyn ExecutionPlan>>> { | ||
| if hash_join.inputs_satisfy_partitioned_requirements()? { | ||
| return Ok(None); | ||
| } | ||
|
|
||
|
Comment on lines
+189
to
+192
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. Thanks, @stuhood. I tested the Hive-partitioned case and found a wrong-results regression with preserve_file_partitions=1.
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. This can drop rows with preserve_file_partitions=1: Hive scans advertise Hash([k], n), but different value sets can place matching keys in different partitions (#23436).
Joining on k returns 0 rows, versus 3 with this hunk reverted. The underlying bug already affects non-broadcast joins; this PR extends it to small tables previously protected by CollectLeft.
Contributor
Author
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 believe that that is a bug in another portion of the codebase (as you pointed out): #25279 is out in order to fix it.
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. Thanks for pointing me to #25279, I missed that existing fix. That looks like the right place to address this.
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 agree if hive scans advertise something different that what actually comes out that is the bug |
||
| let left = hash_join.left(); | ||
| let right = hash_join.right(); | ||
| let optimizer_config = &context.config_options().optimizer; | ||
|
|
||
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.
@stuhood thanks for this! When the co-partitioned inputs have fewer partitions than
target_partitionsandpreserve_file_partitionsis off,EnsureRequirementshash-repartitions both inputs of the now-Partitionedjoin. So the large probe side gets shuffled whereCollectLeftonly coalesced the small side, which made my probe about 1.35x slower attarget_partitions=8. It also adds those repartitions underrepartition_joins=false, where the physical planner choseCollectLeft. Could we keepPartitionedonly when the existing partitioning will survive?with this helper next to
try_collect_left. It mirrorsChildPolicy::needs_hash_repartitioninenforce_distribution.rs; sharing onepub(crate)function there would also work:Timings, and a test that runs the full optimizer (fails on this PR, passes with the change)
Two
ListingTables declared Range-partitioned into 3 partitions (12M-row fact, 300-row dim),SELECT count(*), sum(f.v) FROM fact f JOIN dim d ON f.k = d.k, release build, median of 7 runs over 3 rounds:CollectLeft+RoundRobinBatch(8)on the probe side)Hash([k], 8)on both sides)The existing co-partitioned tests then need
config.execution.target_partitions = 2incheck_hash_join_mode, because their inputs have 2 partitions. The test also needsuse datafusion_physical_optimizer::optimizer::PhysicalOptimizer;.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.
This is a good idea, maybe we could do that in follow-ups, some extra test would be needed for it.