Skip to content

Commit bdd8238

Browse files
authored
feat(physical-plan): Allow co-partitioned Partitioning::Range inputs for left-side hash joins (apache#23487)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes apache#123` indicates that this PR will close issue apache#123. --> - Closes apache#23452 ## Rationale for this change Allows compatible `Partitioning::Range` inputs to satisfy partitioned hash join distribution requirements for left-side joins, avoiding unnecessary hash repartitioning. <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? - Enables range co-partitioning satisfaction for `Left`, `LeftSemi`, `LeftAnti`, and `LeftMark` hash joins. - Adds optimizer and sqllogictest coverage for compatible and incompatible range layouts. - Covers matched/unmatched rows and LeftMark null-related marker behavior. <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? Yes. Added/updated physical optimizer tests and `range_partitioning.slt`. <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 050046a commit bdd8238

3 files changed

Lines changed: 300 additions & 58 deletions

File tree

‎datafusion/core/tests/physical_optimizer/enforce_distribution.rs‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,79 @@ fn range_full_hash_join_rehashes_incompatible_range_partitioning() -> Result<()>
10901090
Ok(())
10911091
}
10921092

1093+
#[test]
1094+
fn range_left_mark_hash_join_reuses_range_partitioning() -> Result<()> {
1095+
let left = parquet_exec_with_output_partitioning(range_partitioning(
1096+
"a",
1097+
[10, 20, 30],
1098+
SortOptions::default(),
1099+
)?);
1100+
let right = parquet_exec_with_output_partitioning(range_partitioning(
1101+
"a",
1102+
[10, 20, 30],
1103+
SortOptions::default(),
1104+
)?);
1105+
let join_on = vec![(
1106+
Arc::new(Column::new_with_schema("a", &left.schema())?) as _,
1107+
Arc::new(Column::new_with_schema("a", &right.schema())?) as _,
1108+
)];
1109+
let join = hash_join_exec(left, right, &join_on, &JoinType::LeftMark);
1110+
1111+
let plan = TestConfig::default()
1112+
.with_query_execution_partitions(4)
1113+
.to_plan(join, &DISTRIB_DISTRIB_SORT);
1114+
1115+
assert_plan!(
1116+
plan,
1117+
@r"
1118+
HashJoinExec: mode=Partitioned, join_type=LeftMark, on=[(a@0, a@0)]
1119+
DataSourceExec: file_groups={4 groups: [[p0], [p1], [p2], [p3]]}, projection=[a, b, c, d, e], output_partitioning=Range([a@0 ASC], [(10), (20), (30)], 4), file_type=parquet
1120+
DataSourceExec: file_groups={4 groups: [[p0], [p1], [p2], [p3]]}, projection=[a, b, c, d, e], output_partitioning=Range([a@0 ASC], [(10), (20), (30)], 4), file_type=parquet
1121+
"
1122+
);
1123+
1124+
Ok(())
1125+
}
1126+
1127+
#[test]
1128+
fn range_left_anti_hash_join_rehashes_incompatible_null_options() -> Result<()> {
1129+
let left = parquet_exec_with_output_partitioning(range_partitioning(
1130+
"a",
1131+
[10, 20, 30],
1132+
SortOptions::default(),
1133+
)?);
1134+
let right = parquet_exec_with_output_partitioning(range_partitioning(
1135+
"a",
1136+
[10, 20, 30],
1137+
SortOptions {
1138+
descending: false,
1139+
nulls_first: false,
1140+
},
1141+
)?);
1142+
let join_on = vec![(
1143+
Arc::new(Column::new_with_schema("a", &left.schema())?) as _,
1144+
Arc::new(Column::new_with_schema("a", &right.schema())?) as _,
1145+
)];
1146+
let join = hash_join_exec(left, right, &join_on, &JoinType::LeftAnti);
1147+
1148+
let plan = TestConfig::default()
1149+
.with_query_execution_partitions(4)
1150+
.to_plan(join, &DISTRIB_DISTRIB_SORT);
1151+
1152+
assert_plan!(
1153+
plan,
1154+
@r"
1155+
HashJoinExec: mode=Partitioned, join_type=LeftAnti, on=[(a@0, a@0)]
1156+
RepartitionExec: partitioning=Hash([a@0], 4), input_partitions=4
1157+
DataSourceExec: file_groups={4 groups: [[p0], [p1], [p2], [p3]]}, projection=[a, b, c, d, e], output_partitioning=Range([a@0 ASC], [(10), (20), (30)], 4), file_type=parquet
1158+
RepartitionExec: partitioning=Hash([a@0], 4), input_partitions=4
1159+
DataSourceExec: file_groups={4 groups: [[p0], [p1], [p2], [p3]]}, projection=[a, b, c, d, e], output_partitioning=Range([a@0 ASC NULLS LAST], [(10), (20), (30)], 4), file_type=parquet
1160+
"
1161+
);
1162+
1163+
Ok(())
1164+
}
1165+
10931166
#[test]
10941167
fn multi_hash_joins() -> Result<()> {
10951168
let left = parquet_exec();

‎datafusion/physical-plan/src/joins/hash_join/exec.rs‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,17 +1293,9 @@ impl ExecutionPlan for HashJoinExec {
12931293
]),
12941294
};
12951295

1296-
if self.mode == PartitionMode::Partitioned
1297-
&& matches!(
1298-
self.join_type,
1299-
JoinType::Inner
1300-
| JoinType::Full
1301-
| JoinType::Right
1302-
| JoinType::RightSemi
1303-
| JoinType::RightAnti
1304-
| JoinType::RightMark
1305-
)
1306-
{
1296+
if self.mode == PartitionMode::Partitioned {
1297+
// Compatible Range inputs co-locate equal join keys, which
1298+
// satisfies the co-partitioned requirement for hash joins.
13071299
requirements.allow_range_satisfaction_for_key_partitioning()
13081300
} else {
13091301
requirements

0 commit comments

Comments
 (0)