Skip to content

fix(optimizer)!: stop pruning implicit GROUP BY ALL keys during projection pushdown - #8129

Merged
georgesittas merged 4 commits into
mainfrom
optimizer/fix-pushdown-prunes-group-by-all-keys
Aug 12, 2026
Merged

fix(optimizer)!: stop pruning implicit GROUP BY ALL keys during projection pushdown#8129
georgesittas merged 4 commits into
mainfrom
optimizer/fix-pushdown-prunes-group-by-all-keys

Conversation

@fivetran-kwoodbeck

Copy link
Copy Markdown
Collaborator

pushdown_projections prunes columns that aren't referenced by an outer query. But a bare GROUP BY ALL infers its grouping keys from projections in the SELECT list, so pruning an "unused" column silently changes which columns are grouped by, corrupting the aggregation.

Input:

SELECT t.a FROM (SELECT a, b, SUM(b) AS s FROM x GROUP BY ALL) t

Wrong output (b and s are dropped):

SELECT "t"."a" AS "a" FROM (SELECT "x"."a" AS "a" FROM "x" AS "x" GROUP BY ALL) AS "t"

Expected output

SELECT "t"."a" AS "a" FROM (SELECT "x"."a" AS "a", "x"."b" AS "b" FROM "x" AS "x" GROUP BY ALL) AS "t"

Fix

We now check whether the scope has a GROUP BY ALL and keeps projections regardless of outer references.

@github-actions

github-actions Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

SQLGlot Integration Test Results

❌ 1 regression — see details below

Comparing:

  • this branch (sqlglot:optimizer/fix-pushdown-prunes-group-by-all-keys @ sqlglot 9d9796c)
  • baseline (main @ sqlglot 194e12b)

By Dialect

dialect main feature branch transitions links
bigquery -> bigquery 34287/34695 passed (98.8%) 33136/33540 passed (98.8%) 1 pass -> fail full result / delta

Overall

main: 182937 total, 160863 passed (pass rate: 87.9%)

sqlglot:optimizer/fix-pushdown-prunes-group-by-all-keys: 170743 total, 149710 passed (pass rate: 87.7%)

Transitions:
1 pass -> fail

Dialect pair changes: 0 previous results not found, 3 current results not found

❌ 1 regression (view logs)

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

LGTM, one nit re: reintroducing short-circuiting on the AggFunc search.

@fivetran-kwoodbeck the same bug exists for ORDER BY ALL:

-- input
SELECT t.a, t.c FROM (SELECT a, b, c FROM x ORDER BY ALL LIMIT 1) t
-- optimizer output: b is pruned
WITH "t" AS (SELECT "x"."a" AS "a", "x"."c" AS "c" FROM "x" AS "x" ORDER BY ALL LIMIT 1) ...
  • This is only observable when the subquery's ordering matters, i.e. with LIMIT/OFFSET
  • Unlike GROUP BY ALL, there's no aggregate exemption: with ORDER BY ALL every projection is a sort key
  • We should check for a Var presence wrapping "ALL" to capture dialects that actually support this

Comment thread sqlglot/optimizer/pushdown_projections.py Outdated
@georgesittas georgesittas changed the title fix(optimizer): stop pruning implicit GROUP BY ALL keys during projection pushdown fix(optimizer)!: stop pruning implicit GROUP BY ALL keys during projection pushdown Aug 11, 2026
@georgesittas

Copy link
Copy Markdown
Collaborator

I'll get this in and do a quick clean up - I wanna release shortly.

@georgesittas
georgesittas force-pushed the optimizer/fix-pushdown-prunes-group-by-all-keys branch from d6a3038 to 9139c4d Compare August 12, 2026 16:18
@georgesittas
georgesittas merged commit 8a29419 into main Aug 12, 2026
8 checks passed
@georgesittas
georgesittas deleted the optimizer/fix-pushdown-prunes-group-by-all-keys branch August 12, 2026 16:27
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