Skip to content

fix(optimizer)!: merge window issue#7845

Merged
fivetran-kwoodbeck merged 2 commits into
mainfrom
optimizer/bug-merge-window
Jul 9, 2026
Merged

fix(optimizer)!: merge window issue#7845
fivetran-kwoodbeck merged 2 commits into
mainfrom
optimizer/bug-merge-window

Conversation

@fivetran-kwoodbeck

@fivetran-kwoodbeck fivetran-kwoodbeck commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

Fixes a correctness bug in merge_subqueries.py where a window function subquery was incorrectly merged into an outer query that filters or joins on its input rows. This alters the query to where it would produce different / wrong results.

Problem query:

SELECT s.w FROM (SELECT a, ROW_NUMBER() OVER (ORDER BY a) AS w FROM x) AS s WHERE s.a > 5

Before fix:

SELECT ROW_NUMBER() OVER (ORDER BY `a`) AS `w` FROM `_t0` AS `_t0` WHERE `a` > 5

After fix:

SELECT `_t1`.`_c1` AS `w` FROM (SELECT `_c0` AS `_c0`, ROW_NUMBER() OVER (ORDER BY `_c0`) AS `_c1` FROM `_t0` AS `_t0`) AS `_t1` WHERE `_t1`.`_c0` > 5

/integration-tests

@fivetran-kwoodbeck fivetran-kwoodbeck changed the title fixed merge window issue fix(optimizer): merge window issue Jul 8, 2026
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

SQLGlot Integration Test Results

✅ All tests passed

Comparing:

  • this branch (sqlglot:optimizer/bug-merge-window @ sqlglot a99aad7)
  • baseline (main @ sqlglot 77c7814)

Overall

main: 192416 total, 153532 passed (pass rate: 79.8%)

sqlglot:optimizer/bug-merge-window: 192416 total, 153532 passed (pass rate: 79.8%)

Transitions:
No change

✅ All tests passed

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

Nice find!

Comment thread sqlglot/optimizer/merge_subqueries.py Outdated
Comment thread tests/fixtures/optimizer/merge_subqueries.sql
@georgesittas georgesittas changed the title fix(optimizer): merge window issue fix(optimizer)!: merge window issue Jul 9, 2026
@fivetran-kwoodbeck fivetran-kwoodbeck force-pushed the optimizer/bug-merge-window branch from c255bde to ed6bce5 Compare July 9, 2026 14:40
@fivetran-kwoodbeck fivetran-kwoodbeck merged commit 122b44f into main Jul 9, 2026
8 checks passed
@fivetran-kwoodbeck fivetran-kwoodbeck deleted the optimizer/bug-merge-window branch July 9, 2026 20:12
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