prql icon indicating copy to clipboard operation
prql copied to clipboard

this.<name>` resolves to the source relation instead of a same-named colum

Open lukapeschke opened this issue 4 months ago • 2 comments

What happened?

Related to #6020 . When a column has the same name as a relation, referencing it resolves to the source table instead of the columns.

In a sort, this causes the compilation to fail. In a select, it results in a wildcard select

PRQL input

# Sort case
from status | sort {this.status}

# Select case
from status | select {this.status}

SQL output

# Sort case
from `status` | sort {this.`status`}
                      ──────┬──────
                            ╰──────── table instance cannot be referenced directly

# Select case
SELECT
  *
FROM
  status

Expected SQL output

# Sort case
SELECT * FROM status ORDER BY status.status

# Select case
SELECT status FROM status

MVCE confirmation

  • [x] Minimal example
  • [x] New issue

Anything else?

No response

lukapeschke avatar Jul 07 '26 10:07 lukapeschke

Thanks for the clear MVCE. I reproduced both cases on main, and also confirmed they still fail on the #6020 branch — so #6020 (which handles a derived column shadowing the relation name) doesn't cover this one; it's a related-but-distinct root cause.

Root cause. A leaf reference like this.status is resolved by Module::lookup, whose lookup_in helper short-circuits to the relation's table instance (NS_SELF) as soon as the leaf name matches an input relation — module.rs#L164-L171. Because that lookup returns a single match (the table instance), resolve_ident_core returns it directly and never reaches the NS_INFER fallback that would infer a same-named column — names.rs#L107-L151. A table instance in select lowers to a wildcard (SELECT *); in sort it hits the table instance cannot be referenced directly error in lowering.rs#L962.

Why it's not a one-line fix. That same short-circuit is what makes referencing a bare relation name expand to all its columns, which is a deliberate feature:

from a | join b (==id) | select {a}   # → SELECT a.* FROM a INNER JOIN b ...
from status | select {status}         # → SELECT * FROM status

Note also that bare status and this.status resolve identically — resolve_ident_core prepends the default this namespace to the bare form, so both take the same lookup path and can't be told apart there.

I tried the naive fix (suppress the NS_SELF short-circuit when the relation is still inferable). It does produce the expected SELECT status FROM status / ORDER BY status here, but it regresses the wildcard behavior above — from a | join b (==id) | select {a} then errors with Ambiguous name (a.a / b.a), and select {status} becomes SELECT status instead of SELECT *. So resolving this comes down to a design decision about how a name that's both a relation and a (possibly inferred) column should disambiguate, and whether leaf this.<relname> should diverge from bare <relname>.

Given that tradeoff and the overlap with the shadowing work in #6020, I'll leave it for a maintainer to decide the intended semantics rather than ship a fix that changes the wildcard behavior.

prql-bot avatar Jul 07 '26 11:07 prql-bot

Some detail on the two silent cases, from checking on main (95671d53):

  • GROUP BY * is emitted on purpose, per dialect. DialectHandler::stars_in_group defaults to true. Only SQLite overrides it, so --target sql.sqlite already rejects the group {dkey} query with Target dialect does not support * in this position., while sql.generic and sql.postgres emit GROUP BY *. So erroring on it is really about changing that default, i.e. which dialects actually accept GROUP BY *. It's a different choice from the name-resolution question. Grouping by a wildcard isn't wrong in itself: group {this.*} (take 1) lowers to SELECT DISTINCT *, which is valid, so a check can't go into lowering. It would need to sit where the aggregate's GROUP BY is generated.
  • derive {k = dkey} loses the alias before it reaches lowering. I tried rejecting an aliased table instance in declare_as_columns (the branch that expands a relation reference into its columns). That check never fires, because by then the alias is gone. Reading the code, it looks like finish_expr_resolve takes the alias on anything that has a lineage and renames the relation with it. That's the mechanism behind relation aliases like join x = (...). A useful error would have to tell "aliasing a relation in a transform argument" apart from that legitimate rename. I didn't take that further, so I haven't opened a PR.

One more thing: when the relation's columns are known, the bare name resolves differently again. With let a = (from t | select {x, y}), from a | group {a} (...) gives GROUP BY a and from a | derive {k = a} gives SELECT x, y, a. Both refer to a column a that doesn't exist.

prql-bot avatar Oct 18 '26 19:10 prql-bot