Skip to content

keep fetch when merge FilterExec in FilterPushdown - #21070

Merged
adriangb merged 2 commits into
apache:mainfrom
haohuaijin:filter-pushdown-changes
Mar 20, 2026
Merged

adriangb merged 2 commits into
apache:mainfrom
haohuaijin:filter-pushdown-changes

Conversation

@haohuaijin

@haohuaijin haohuaijin commented Mar 20, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

What changes are included in this PR?

When FilterPushdown merges/eliminates a FilterExec, it now correctly propagates the fetch limit that was on the FilterExec.

Previously, if a FilterExec had a fetch (e.g., LIMIT 10) and all its predicates were fully pushed down to a under FilterExec, the FilterExec node was dropped — and the fetch was silently lost, potentially returning more rows than requested.
The fix:

  • If the FilterExec being eliminated has a fetch, propagate it to the child node via with_fetch(), if the clid node do not support with_fetch add a local limit node.
  • If both the outer and inner nodes have a fetch, use min(outer, inner) to preserve the tighter constraint

Are these changes tested?

yes, add some test cases, trying to reproduce use sql, current not able

Are there any user-facing changes?

@github-actions github-actions Bot added core Core DataFusion crate physical-plan Changes to the physical-plan crate labels Mar 20, 2026
Comment thread datafusion/physical-plan/src/filter.rs Resolved

@adriangb adriangb 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.

Looks good to me!

@adriangb
adriangb added this pull request to the merge queue Mar 20, 2026
Merged via the queue into apache:main with commit 8f721a6 Mar 20, 2026
34 checks passed
@haohuaijin
haohuaijin deleted the filter-pushdown-changes branch March 21, 2026 01:24
@haohuaijin

Copy link
Copy Markdown
Contributor Author

Thanks for you reviews @adriangb

haohuaijin added a commit to haohuaijin/arrow-datafusion that referenced this pull request Mar 21, 2026
## Which issue does this PR close?

- Closes apache#21069

## Rationale for this change

- see apache#21069

## What changes are included in this PR?

When `FilterPushdown` merges/eliminates a `FilterExec`, it now correctly
propagates the `fetch` limit that was on the FilterExec.

Previously, if a `FilterExec` had a fetch (e.g., LIMIT 10) and all its
predicates were fully pushed down to a under `FilterExec`, the
FilterExec node was dropped — and the fetch was silently lost,
potentially returning more rows than requested.
The fix:
- If the FilterExec being eliminated has a fetch, propagate it to the
child node via `with_fetch()`
- If both the outer and inner nodes have a fetch, use min(outer, inner)
to preserve the tighter constraint

## Are these changes tested?

yes, add some test cases, trying to reproduce use sql, current not able

## 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.
-->
de-bgunter pushed a commit to de-bgunter/datafusion that referenced this pull request Mar 24, 2026
## Which issue does this PR close?

- Closes apache#21069

## Rationale for this change

- see apache#21069

## What changes are included in this PR?

When `FilterPushdown` merges/eliminates a `FilterExec`, it now correctly
propagates the `fetch` limit that was on the FilterExec.

Previously, if a `FilterExec` had a fetch (e.g., LIMIT 10) and all its
predicates were fully pushed down to a under `FilterExec`, the
FilterExec node was dropped — and the fetch was silently lost,
potentially returning more rows than requested.
The fix:
- If the FilterExec being eliminated has a fetch, propagate it to the
child node via `with_fetch()`
- If both the outer and inner nodes have a fetch, use min(outer, inner)
to preserve the tighter constraint

## Are these changes tested?

yes, add some test cases, trying to reproduce use sql, current not able

## 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.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FilterPushdown discard the fetch when merge FilterExec

2 participants