Skip to content

fix(optimizer): do not push predicates into a subquery with LIMIT/OFFSET [CLAUDE]#7901

Merged
georgesittas merged 1 commit into
tobymao:mainfrom
chuenchen309:fix/pushdown-predicates-limit
Jul 20, 2026
Merged

fix(optimizer): do not push predicates into a subquery with LIMIT/OFFSET [CLAUDE]#7901
georgesittas merged 1 commit into
tobymao:mainfrom
chuenchen309:fix/pushdown-predicates-limit

Conversation

@chuenchen309

Copy link
Copy Markdown
Contributor

pushdown_predicates already refuses to push a predicate into a single-table
subquery that carries a GROUP BY or a window function, since either would
change the result. LIMIT / OFFSET has the same issue: it selects rows
before the outer predicate runs, so pushing the filter in changes which rows
survive the limit.

SELECT s.x FROM (SELECT x, y FROM t ORDER BY x LIMIT 10) AS s WHERE s.y > 5
  before: ... (SELECT ... FROM t WHERE y > 5 ORDER BY x LIMIT 10) ... WHERE TRUE

The rewrite filters y > 5 before the LIMIT 10, so it returns rows the
original query would never see — a silent change of results. The same happens
with a bare OFFSET.

Extended the existing guard to also bail when the subquery has a limit or
offset. Subqueries without either still get the predicate pushed down as
before. Added two fixture cases (LIMIT and OFFSET) that assert the query is
left unchanged; make unit passes.


This PR was authored by an AI coding agent (Claude Code) running on this account:
the AI found the bug, ran the repro, wrote the tests, and wrote this description.
The human account holder reviews every change and is accountable for it. The
verification above is real and re-runnable from the diff. If this isn't the kind
of contribution you want, say so and I'll close it.

…ET [CLAUDE]

pushdown_predicates already refuses to push a predicate into a subquery
that carries a GROUP BY or a window function, because doing so would change
the result. A LIMIT or OFFSET has the same problem: it picks rows before the
outer predicate runs, so filtering inside the subquery changes which rows the
limit keeps. For example

  SELECT s.x FROM (SELECT x, y FROM t ORDER BY x LIMIT 10) AS s WHERE s.y > 5

was rewritten to filter y > 5 *before* the LIMIT, silently returning
different rows. Extend the existing guard to also bail on limit/offset.

Co-authored-by AI: Claude (Claude Code). [CLAUDE]
@georgesittas georgesittas self-assigned this Jul 20, 2026

@georgesittas georgesittas left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you.

@georgesittas
georgesittas merged commit ae702b1 into tobymao:main Jul 20, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants