fix: read a cast after a SELECT modifier or in parentheses
Issue for this PR
Closes #1759
Type of change
- Bug fix
What does this PR do?
On Postgres and Redshift, a cast after a SELECT modifier, or in parentheses, now reads as a cast of its column. The answer then names it after the column instead of failing with "has no column". This was found in the review of #1754 and predates it.
- The modifier is read whole:
distinct on (…)up to its matching), with or without a space after it;top N; anddistinctorallas whole words only. - A cast's operand may be parenthesised (
(amount)::numeric,distinct(amount)::numeric), and::andcast()chains may mix (cast(amount as numeric)::int).
The cast reader was rewritten as a small tokenizer with one grammar (in its doc comment), after three review findings in a row came from regex slicing; read it fresh. A table test holds every cast form found so far.
An expression that only starts with a cast (amount::numeric + 1, (amount + 1)::int) keeps the engine's label. The modifier reading is shared, so on any dialect a * after distinct on (…) beside a repeated name now leaves the model unsettled, the safe direction. Nothing is reported unaffected, as before.
How did you verify your code works?
Each shape in the issue has a test that fails on main. Two more guard #1754's parsers (all$x, and a mixed-case DuckDB repeat) and pass on main too. The demo project's answer is unchanged: 11 carry it, 28 may carry it, of 39.
Cost side effects
None (local manifest only).
Checklist
- I have tested my changes locally
- I have not included unrelated changes in this PR
- I checked for cost side effects (model calls, queries or cloud resources that grow with user data) and described them, or wrote "none"
🤖 Generated with Claude Code
https://claude.ai/code/session_01VEmKQwEiHMwQ7BdwgPo9sb
Summary by cubic
Fixes column answer resolution for casts after a SELECT modifier or wrapped in parentheses, so distinct on(id)amount::numeric and (amount)::numeric resolve to their column instead of failing with "has no column".
- Reads
distinct on (…)up to its matching),top N, and whole-worddistinct/allmodifiers;distinct onruns on Postgres,top Non Redshift. - Accepts parenthesized cast operands, mixed
::/cast()chains, and casts with no space beforeas, even beside a quoted name, e.g.cast(amount as numeric)::int,cast((amount)as numeric), andcast("amount"as numeric). - Type names may take modifiers that read as names, so
amount::varchar(max)andamount::geometry(Point,4326)still nameamount, and a quoted name unescapes only its own doubled quote. - A qualified column named like a literal (
q.end,q.null) keeps its name through a cast; only a bare literal (1,1.5,null) is treated as one. - Expressions that only start with a cast (
amount::numeric + 1,(amount + 1)::int) keep the engine's label, unchanged. - Adds tests for each shape in #1759 plus guards for
all$xand mixed-case DuckDB name repeats.
Summary by CodeRabbit
- Bug Fixes
- Improved output column naming for queries using
DISTINCT,DISTINCT ON,ALL, orTOP, including nested and mixed cast expressions. - Improved recognition of column names that resemble SQL modifiers or literals, while avoiding incorrect names for cast expressions that do not reference a column.
- Improved output column naming for queries using