Skip to content

fix(semantics): check a named window's keys as column references - #262

Merged
kvlonge merged 2 commits into
mainfrom
fix/named-window-keys
Nov 18, 2026
Merged

kvlonge merged 2 commits into
mainfrom
fix/named-window-keys

Conversation

@kvlonge

@kvlonge kvlonge commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Why

A named window's PARTITION BY and ORDER BY keys were never resolved, so SELECT sum(b) OVER w FROM t WINDOW w AS (ORDER BY nope) reported nothing, while the same key written inline in OVER gave E201.

Changes

  • Shared semantics: the keys of a named window in WINDOW, for a SELECT and for a subquery, resolve as column references in the scope an inline window would see, in every dialect: unknown columns give E201 and unknown qualifiers E222.
  • Only windows an OVER reaches are checked, directly or through a window that inherits from them. DuckDB, SQLite and Databricks accept an unused window with a bad key. PostgreSQL and ClickHouse reject it, and those reports are not made.

Verification

  • make check-ci on the final head.
  • Each fix has tests that fail on the previous head: named-window keys in Postgres, Databricks and DuckDB (including an output alias Postgres can read), and an unused window that reports nothing.
  • Cross-dialect comparison, 65 statements in 8 dialects, checked against DuckDB 2.0, SQLite 3.40, PostgreSQL 16 and ClickHouse: no earlier finding is lost or reordered, and every added finding is in a named window's key. Output aliases, USING and NATURAL, LATERAL, correlated keys, ordinals, frames, CTEs, set-operation branches and windows at two levels all agree with the engines.
  • Follow-up probe of 816 statements against the same engines: the unused-window fix only removes findings, and every true positive is kept.

A SELECT's clause check read its joins, filters, groups and orders but
never a WINDOW clause, so an unknown column in a named window's
PARTITION BY or ORDER BY went unreported in every dialect, both in
SELECT ... WINDOW and in a WINDOW written after a parenthesized query.

The keys of each named window are now checked at the level an inline
OVER in a projection reads: output aliases in the dialects that read
them anywhere, input columns only in the others. PostgreSQL, which
follows SQL99 rules for a window definition, so refuses an output
alias there.

Databricks and PostgreSQL cases cover unknown partition and order
keys, a window after the parentheses and an output alias.
The previous change checked the keys of every named window, so a window
nothing used reported E201 or E222 for a key that is never resolved.
DuckDB, SQLite and Databricks accept such a query, and Spark never
resolves an unused window.

A named window's keys are now checked only when an OVER in the query or
in an ORDER BY after it names the window, directly or through a window
that extends it. This holds in every dialect, so PostgreSQL's and
ClickHouse's refusal of an unused bad window is not reported.

DuckDB cases cover unused windows with unknown keys, qualifiers and a
subquery, a used window, a window reached through another, and a key
that reads an output alias. The previous commit's message said its
Databricks cases covered an output alias; only the PostgreSQL ones do.
@kvlonge
kvlonge enabled auto-merge November 18, 2026 21:28
@kvlonge
kvlonge added this pull request to the merge queue Nov 18, 2026
Merged via the queue into main with commit 6e472d2 Nov 18, 2026
9 checks passed
@kvlonge
kvlonge deleted the fix/named-window-keys branch November 18, 2026 22:32
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.

1 participant