Skip to content

Commit 37d11af

Browse files
Phoenix500526alamb
andauthored
refactor: make join projection pushdown schema-aware via ColumnIndex/… (#23185)
## 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 #23010 ## Rationale for this change <!-- 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. --> `try_pushdown_through_join` (`datafusion/physical-plan/src/projection.rs`) decided whether each projected join-output column belonged to the left or right child by comparing its output index against the left child's field count (`join_table_borders`) — i.e. it assumed the join output is a plain `left ++ right` concatenation. That assumption does not hold for all join types. A `LeftMark` / `RightMark` join appends a synthetic `mark` boolean column that has `JoinSide::None` and originates from neither child, so reasoning from output position can route the `mark` column to the wrong child. This is the panic that #22902 worked around by **disabling** projection pushdown for `LeftMark` / `RightMark` in `HashJoinExec` and `NestedLoopJoinExec` — correct, but it left the helper reasoning from output position rather than from the join's actual output-origin contract. ## What changes are included in this PR? <!-- 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. --> - Thread the join's output-origin metadata (`&[ColumnIndex]`, already computed by `build_join_schema`) into `try_pushdown_through_join`. - Replace the output-position split (`join_table_borders` + `index < left_field_count`) with grouping by `ColumnIndex.side`: - `Left` / `Right` columns are collected per child using the child-relative `ColumnIndex.index`; - a `JoinSide::None` (synthetic, e.g. `mark`) column makes the pushdown decline (`Ok(None)`), so the projection is embedded into the join instead — no panic, no mis-routing. - Remove the `LeftMark | RightMark` bypass from `HashJoinExec::try_swapping_with_projection` and `NestedLoopJoinExec::try_swapping_with_projection`; they now call `try_pushdown_through_join` uniformly. - The shared helpers used by cross / symmetric-hash / sort-merge join pushdown (`new_join_children`, `join_table_borders`, `join_allows_pushdown`, `update_join_on`, `update_join_filter`) keep their signatures. The side-grouped path uses a new private `new_join_children_from_groups` and reuses `update_join_on` / `update_join_filter` with a zero column-index offset (the groups already carry child-relative indices). ## Are these changes tested? <!-- 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)? --> Yes. - New `subquery.slt` cases place a subset/reordering projection over a mark join — hash `LeftMark` (`IN` under `OR`), negated mark (`NOT EXISTS` under `OR`), and nested-loop mark (non-equi correlated) — asserting both the query result and the `EXPLAIN` plan shape. - Existing coverage is preserved: `cargo test -p datafusion-physical-plan projection` (incl. the filter-pushdown and `join_table_borders` unit tests), the join `*.slt` suite, and `subquery.slt` all pass. - `cargo clippy -p datafusion-physical-plan -- -D warnings` and `cargo fmt --all` are clean. ## 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. --> No behavior change: the produced physical plans are unchanged for the supported cases (mark joins keep falling back to the same embedded-projection plan as before, regular joins push down as before). One signature change to a `pub` helper: `try_pushdown_through_join` gains a `column_indices: &[ColumnIndex]` parameter (all in-tree callers are updated). If the project treats this helper as part of the public API surface, please add the `api change` label. --------- Signed-off-by: Jiawei Zhao <Phoenix500526@163.com> Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
1 parent dd1c30b commit 37d11af

5 files changed

Lines changed: 418 additions & 134 deletions

File tree

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use crate::joins::{JoinOn, JoinOnRef, PartitionMode, SharedBitmapBuilder};
4949
use crate::metrics::{Count, MetricBuilder, MetricCategory};
5050
use crate::projection::{
5151
EmbeddedProjection, JoinData, ProjectionExec, try_embed_projection,
52-
try_pushdown_through_join,
52+
try_pushdown_through_join_with_column_indices,
5353
};
5454
use crate::repartition::REPARTITION_RANDOM_STATE;
5555
use crate::statistics::{ChildStats, StatisticsArgs};
@@ -1564,23 +1564,21 @@ impl ExecutionPlan for HashJoinExec {
15641564
return Ok(None);
15651565
}
15661566

1567-
// TODO: split by `col`/`JoinSide` instead so mark joins can also push down to children.
15681567
let schema = self.schema();
1569-
if !matches!(self.join_type(), JoinType::LeftMark | JoinType::RightMark)
1570-
&& let Some(JoinData {
1571-
projected_left_child,
1572-
projected_right_child,
1573-
join_filter,
1574-
join_on,
1575-
}) = try_pushdown_through_join(
1576-
projection,
1577-
self.left(),
1578-
self.right(),
1579-
self.on(),
1580-
&schema,
1581-
self.filter(),
1582-
)?
1583-
{
1568+
if let Some(JoinData {
1569+
projected_left_child,
1570+
projected_right_child,
1571+
join_filter,
1572+
join_on,
1573+
}) = try_pushdown_through_join_with_column_indices(
1574+
projection,
1575+
self.left(),
1576+
self.right(),
1577+
self.on(),
1578+
&schema,
1579+
self.filter(),
1580+
self.column_indices.as_slice(),
1581+
)? {
15841582
self.builder()
15851583
.with_new_children(vec![
15861584
Arc::new(projected_left_child),

datafusion/physical-plan/src/joins/nested_loop_join.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use crate::metrics::{
4040
};
4141
use crate::projection::{
4242
EmbeddedProjection, JoinData, ProjectionExec, try_embed_projection,
43-
try_pushdown_through_join,
43+
try_pushdown_through_join_with_column_indices,
4444
};
4545
use crate::statistics::{ChildStats, StatisticsArgs};
4646
use crate::{
@@ -741,23 +741,21 @@ impl ExecutionPlan for NestedLoopJoinExec {
741741
return Ok(None);
742742
}
743743

744-
// TODO: split by `col`/`JoinSide` instead so mark joins can also push down to children.
745744
let schema = self.schema();
746-
if !matches!(self.join_type(), JoinType::LeftMark | JoinType::RightMark)
747-
&& let Some(JoinData {
748-
projected_left_child,
749-
projected_right_child,
750-
join_filter,
751-
..
752-
}) = try_pushdown_through_join(
753-
projection,
754-
self.left(),
755-
self.right(),
756-
&[],
757-
&schema,
758-
self.filter(),
759-
)?
760-
{
745+
if let Some(JoinData {
746+
projected_left_child,
747+
projected_right_child,
748+
join_filter,
749+
..
750+
}) = try_pushdown_through_join_with_column_indices(
751+
projection,
752+
self.left(),
753+
self.right(),
754+
&[],
755+
&schema,
756+
self.filter(),
757+
self.column_indices.as_slice(),
758+
)? {
761759
Ok(Some(Arc::new(NestedLoopJoinExec::try_new(
762760
Arc::new(projected_left_child),
763761
Arc::new(projected_right_child),

datafusion/physical-plan/src/joins/symmetric_hash_join.rs

Lines changed: 29 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ use crate::joins::utils::{
4747
matchable_join_keys, symmetric_join_output_partitioning, update_hash,
4848
};
4949
use crate::projection::{
50-
ProjectionExec, join_allows_pushdown, join_table_borders, new_join_children,
51-
physical_to_column_exprs, update_join_filter, update_join_on,
50+
JoinData, ProjectionExec, try_pushdown_through_join_with_column_indices,
5251
};
5352
use crate::stream::EmptyRecordBatchStream;
5453
use crate::{
@@ -598,69 +597,36 @@ impl ExecutionPlan for SymmetricHashJoinExec {
598597
&self,
599598
projection: &ProjectionExec,
600599
) -> Result<Option<Arc<dyn ExecutionPlan>>> {
601-
// Convert projected PhysicalExpr's to columns. If not possible, we cannot proceed.
602-
let Some(projection_as_columns) = physical_to_column_exprs(projection.expr())
603-
else {
604-
return Ok(None);
605-
};
606-
607-
let (far_right_left_col_ind, far_left_right_col_ind) = join_table_borders(
608-
self.left().schema().fields().len(),
609-
&projection_as_columns,
610-
);
611-
612-
if !join_allows_pushdown(
613-
&projection_as_columns,
614-
&self.schema(),
615-
far_right_left_col_ind,
616-
far_left_right_col_ind,
617-
) {
618-
return Ok(None);
619-
}
620-
621-
let Some(new_on) = update_join_on(
622-
&projection_as_columns[0..=far_right_left_col_ind as _],
623-
&projection_as_columns[far_left_right_col_ind as _..],
624-
self.on(),
625-
self.left().schema().fields().len(),
626-
) else {
627-
return Ok(None);
628-
};
629-
630-
let new_filter = if let Some(filter) = self.filter() {
631-
match update_join_filter(
632-
&projection_as_columns[0..=far_right_left_col_ind as _],
633-
&projection_as_columns[far_left_right_col_ind as _..],
634-
filter,
635-
self.left().schema().fields().len(),
636-
) {
637-
Some(updated_filter) => Some(updated_filter),
638-
None => return Ok(None),
639-
}
640-
} else {
641-
None
642-
};
643-
644-
let (new_left, new_right) = new_join_children(
645-
&projection_as_columns,
646-
far_right_left_col_ind,
647-
far_left_right_col_ind,
600+
let schema = self.schema();
601+
if let Some(JoinData {
602+
projected_left_child,
603+
projected_right_child,
604+
join_filter,
605+
join_on,
606+
}) = try_pushdown_through_join_with_column_indices(
607+
projection,
648608
self.left(),
649609
self.right(),
650-
)?;
651-
652-
SymmetricHashJoinExec::try_new(
653-
Arc::new(new_left),
654-
Arc::new(new_right),
655-
new_on,
656-
new_filter,
657-
self.join_type(),
658-
self.null_equality(),
659-
self.right().output_ordering().cloned(),
660-
self.left().output_ordering().cloned(),
661-
self.partition_mode(),
662-
)
663-
.map(|e| Some(Arc::new(e) as _))
610+
self.on(),
611+
&schema,
612+
self.filter(),
613+
self.column_indices.as_slice(),
614+
)? {
615+
SymmetricHashJoinExec::try_new(
616+
Arc::new(projected_left_child),
617+
Arc::new(projected_right_child),
618+
join_on,
619+
join_filter,
620+
self.join_type(),
621+
self.null_equality(),
622+
self.right().output_ordering().cloned(),
623+
self.left().output_ordering().cloned(),
624+
self.partition_mode(),
625+
)
626+
.map(|e| Some(Arc::new(e) as _))
627+
} else {
628+
Ok(None)
629+
}
664630
}
665631
}
666632

0 commit comments

Comments
 (0)