fix: ProjectionPushdown internal error on NestedLoopJoin mark joins - #22902
Merged
Conversation
kosiew
approved these changes
Jun 17, 2026
| } | ||
|
|
||
| // TODO: split by `col`/`JoinSide` instead so mark joins can also push down to children. | ||
| let is_mark_join = |
Contributor
There was a problem hiding this comment.
Small nit: is_mark_join is only used once here, so I think this could be a little more direct if the matches! guard were inlined where the child pushdown is skipped.
if !matches!(self.join_type(), JoinType::LeftMark | JoinType::RightMark)
&& let Some(JoinData { ... }) = try_pushdown_through_join(...)?
{
...
} else {
try_embed_projection(projection, self)
}The same cleanup looks like it would apply to NestedLoopJoinExec too.
Contributor
Author
There was a problem hiding this comment.
Thank you, I’ve inlined the guard here.
Phoenix500526
added a commit
to Phoenix500526/datafusion
that referenced
this pull request
Jun 25, 2026
…JoinSide try_pushdown_through_join 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, assuming a `left ++ right` output schema. That is wrong for joins whose output is not a plain concatenation -- mark joins append a synthetic `mark` column (JoinSide::None) belonging to neither child -- which is why apache#22902 had to bypass projection pushdown for LeftMark / RightMark. Group the projected columns by the join's output-origin metadata (ColumnIndex.side) instead of output position: Left/Right columns push to the matching child via the child-relative ColumnIndex.index, and a synthetic (JoinSide::None) column makes the pushdown decline so the projection is embedded into the join instead -- no panic, no mis-routing. This lets the LeftMark / RightMark bypass be removed from HashJoinExec and NestedLoopJoinExec. The shared helpers used by cross / symmetric / sort-merge joins (new_join_children, join_table_borders, update_join_on/filter) keep their signatures; the side-grouped path uses a new new_join_children_from_groups and reuses update_join_on/filter with a zero column-index offset. Mark joins still fall back to embedding (pushing their child columns down is a follow-up). Closes apache#23010 Signed-off-by: Jiawei Zhao <Phoenix500526@163.com>
philippemnoel
pushed a commit
to paradedb/datafusion
that referenced
this pull request
Jul 15, 2026
apache#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 apache#123` indicates that this PR will close issue apache#123. --> - Closes apache#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 apache#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>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Which issue does this PR close?
ProjectionPushdowninternal error on NestedLoopJoin mark joins #22901.Rationale for this change
A query whose subquery becomes a mark join (
LeftMark/RightMark) can panic during theProjectionPushdownphysical optimization with an internal assertion error.The pushdown helper
try_pushdown_through_joinassumes the join output schema is the plain concatenation of its two children (left ++ right) and usesjoin_table_bordersto split the projected columns into a left group and a right group by column index. Mark joins break this assumption: they append an extramarkboolean column that does not originate from either child, so the column-index split misroutes columns to the wrong side and the subsequent child-projection rewrite fails its name-match assertion.What changes are included in this PR?
In
HashJoinExec::try_swapping_with_projectionandNestedLoopJoinExec::try_swapping_with_projection, skip thetry_pushdown_through_joinpath for mark joins (LeftMark/RightMark) and fall through to embedding the projection into the join instead.Are these changes tested?
Yes.
Are there any user-facing changes?
Yes, only bug fixes.