Skip to content

fix: ProjectionPushdown internal error on NestedLoopJoin mark joins - #22902

Merged
kosiew merged 6 commits into
apache:mainfrom
lyne7-sc:fix/mark_join_pushdown
Jun 18, 2026
Merged

fix: ProjectionPushdown internal error on NestedLoopJoin mark joins#22902
kosiew merged 6 commits into
apache:mainfrom
lyne7-sc:fix/mark_join_pushdown

Conversation

@lyne7-sc

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

A query whose subquery becomes a mark join (LeftMark/RightMark) can panic during the ProjectionPushdown physical optimization with an internal assertion error.

The pushdown helper try_pushdown_through_join assumes the join output schema is the plain concatenation of its two children (left ++ right) and uses join_table_borders to split the projected columns into a left group and a right group by column index. Mark joins break this assumption: they append an extra mark boolean 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_projection and NestedLoopJoinExec::try_swapping_with_projection, skip the try_pushdown_through_join path 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.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) physical-plan Changes to the physical-plan crate labels Jun 11, 2026

@kosiew kosiew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lyne7-sc
Thanks for the fix. I do not see any blocking issues, just one small cleanup suggestion.

}

// TODO: split by `col`/`JoinSide` instead so mark joins can also push down to children.
let is_mark_join =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I’ve inlined the guard here.

@kosiew
kosiew added this pull request to the merge queue Jun 18, 2026
Merged via the queue into apache:main with commit 2836480 Jun 18, 2026
38 checks passed
@lyne7-sc
lyne7-sc deleted the fix/mark_join_pushdown branch June 21, 2026 13:43
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-plan Changes to the physical-plan crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ProjectionPushdown internal error on NestedLoopJoin mark joins

2 participants