You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
LimitPushPastWindows only pushes a limit past the first window in a plan #26659
Is your feature request related to a problem or challenge?
LimitPushPastWindows turns the SortExec below a window into a TopK when a LIMIT sits above the window. It only does this for the first such window it meets. In a UNION ALL of two windowed queries with limits, the second branch keeps a full sort:
CREATETABLEt(a INT, x INT) ASVALUES (1, 1), (2, 2), (3, 3), (4, 4), (5, 5);
EXPLAIN SELECT*FROM (SELECT a, sum(x) OVER (ORDER BY a ROWS UNBOUNDED PRECEDING) s FROM t ORDER BY a LIMIT2)
UNION ALLSELECT*FROM (SELECT a, sum(x) OVER (ORDER BY a ROWS UNBOUNDED PRECEDING) s FROM t ORDER BY a LIMIT3);
The same happens for any later window in the plan, not only under a union.
Describe the solution you'd like
Both branches get a TopK. Two things in datafusion/physical-optimizer/src/limit_pushdown_past_window.rs cause this:
After setting the fetch, apply_limit returns Transformed::complete(...) (:201). That sets TreeNodeRecursion::Stop, which stops the whole transform_down, not just the subtree below the sort. TreeNodeRecursion::Jump would skip only the sort's children.
phase (:83) becomes Phase::Apply at the first window and is never set back. The reset closure (:86-94) clears the limit but not the phase, so later limits are never picked up.
Activity
cool9850311 commentedon Oct 23, 2026
take
fix: push a limit past every window in a plan, not just the first
fix: push a limit past every window in a plan, not just the first
fix: push a limit past every window in a plan, not just the first
fix: push a limit past every window in a plan, not just the first
fix: pick up a limit below a window the rule already grew past
fix: push a limit past every window in a plan, not just the first
fix: pick up a limit below a window the rule already grew past
fix: push a limit past every window in a plan, not just the first
fix: pick up a limit below a window the rule already grew past
fix: push a limit past every window in a plan, not just the first
fix: pick up a limit below a window the rule already grew past
fix: push a limit past every window in a plan, not just the first (ap…