Skip to content

Commit 5d0aaf5

Browse files
JSOD11Gene Bordegaray
authored andcommitted
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 #123` indicates that this PR will close issue #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 e95b7a2 commit 5d0aaf5

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
@@ -1282,6 +1282,79 @@ fn range_full_hash_join_rehashes_incompatible_range_partitioning() -> Result<()>
12821282
Ok(())
12831283
}
12841284

1285+
#[test]
1286+
fn range_left_mark_hash_join_reuses_range_partitioning() -> Result<()> {
1287+
let left = parquet_exec_with_output_partitioning(range_partitioning(
1288+
"a",
1289+
[10, 20, 30],
1290+
SortOptions::default(),
1291+
)?);
1292+
let right = parquet_exec_with_output_partitioning(range_partitioning(
1293+
"a",
1294+
[10, 20, 30],
1295+
SortOptions::default(),
1296+
)?);
1297+
let join_on = vec![(
1298+
Arc::new(Column::new_with_schema("a", &left.schema())?) as _,
1299+
Arc::new(Column::new_with_schema("a", &right.schema())?) as _,
1300+
)];
1301+
let join = hash_join_exec(left, right, &join_on, &JoinType::LeftMark);
1302+
1303+
let plan = TestConfig::default()
1304+
.with_query_execution_partitions(4)
1305+
.to_plan(join, &DISTRIB_DISTRIB_SORT);
1306+
1307+
assert_plan!(
1308+
plan,
1309+
@r"
1310+
HashJoinExec: mode=Partitioned, join_type=LeftMark, on=[(a@0, a@0)]
1311+
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
1312+
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
1313+
"
1314+
);
1315+
1316+
Ok(())
1317+
}
1318+
1319+
#[test]
1320+
fn range_left_anti_hash_join_rehashes_incompatible_null_options() -> Result<()> {
1321+
let left = parquet_exec_with_output_partitioning(range_partitioning(
1322+
"a",
1323+
[10, 20, 30],
1324+
SortOptions::default(),
1325+
)?);
1326+
let right = parquet_exec_with_output_partitioning(range_partitioning(
1327+
"a",
1328+
[10, 20, 30],
1329+
SortOptions {
1330+
descending: false,
1331+
nulls_first: false,
1332+
},
1333+
)?);
1334+
let join_on = vec![(
1335+
Arc::new(Column::new_with_schema("a", &left.schema())?) as _,
1336+
Arc::new(Column::new_with_schema("a", &right.schema())?) as _,
1337+
)];
1338+
let join = hash_join_exec(left, right, &join_on, &JoinType::LeftAnti);
1339+
1340+
let plan = TestConfig::default()
1341+
.with_query_execution_partitions(4)
1342+
.to_plan(join, &DISTRIB_DISTRIB_SORT);
1343+
1344+
assert_plan!(
1345+
plan,
1346+
@r"
1347+
HashJoinExec: mode=Partitioned, join_type=LeftAnti, on=[(a@0, a@0)]
1348+
RepartitionExec: partitioning=Hash([a@0], 4), input_partitions=4
1349+
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
1350+
RepartitionExec: partitioning=Hash([a@0], 4), input_partitions=4
1351+
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
1352+
"
1353+
);
1354+
1355+
Ok(())
1356+
}
1357+
12851358
#[test]
12861359
fn multi_hash_joins() -> Result<()> {
12871360
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
@@ -1274,17 +1274,9 @@ impl ExecutionPlan for HashJoinExec {
12741274
]),
12751275
};
12761276

1277-
if self.mode == PartitionMode::Partitioned
1278-
&& matches!(
1279-
self.join_type,
1280-
JoinType::Inner
1281-
| JoinType::Full
1282-
| JoinType::Right
1283-
| JoinType::RightSemi
1284-
| JoinType::RightAnti
1285-
| JoinType::RightMark
1286-
)
1287-
{
1277+
if self.mode == PartitionMode::Partitioned {
1278+
// Compatible Range inputs co-locate equal join keys, which
1279+
// satisfies the co-partitioned requirement for hash joins.
12881280
requirements.allow_range_satisfaction_for_key_partitioning()
12891281
} else {
12901282
requirements

0 commit comments

Comments
 (0)