prql icon indicating copy to clipboard operation
prql copied to clipboard

Error when column names clash with variable names

Open syko opened this issue 8 months ago • 1 comments

What's up?

So this seems to be by design but definitely came off as a bug when I first encountered it.

This does not look like there should be any issue:

prql target:sql.postgres

let name = 'asdf' # Causes error

let foo = (
  from sometable
  select {
    id,
    name
  }
)
from foo

Error: 
    ╭─[:13:6]
    │
 13 │ from foo
    │      ─┬─  
    │       ╰─── This table contains unnamed columns that need to be referenced by name

  1. The error is a bit confusing
  2. If I add select {id, name} to the final from foo then it selects the variable name instead of the column. It would be nice if we could disambiguate the situation by specifying {name = foo.name} or something.
  3. Trying the above results in the following which is even more confusing because without the variable it works:
Error: 
  ╭─[:14:18]
  │
14 │ select {id, name=foo.name}
  │                  ────┬───  
  │                      ╰───── Unknown name `foo.name`
────╯

...which is exactly how I first found this issue when trying to upgrade from 0.8.1. An existing large query was joining tables and somehow one column seemed to go missing half-way through the query xD

syko avatar Nov 27 '23 15:11 syko

Yes, this does seem like a bug. In particular, this works:

let name = 'asdf'

from sometable
select {
  id,
  name
}

...but when in let foo = ... ... from foo, then it raises an error...

max-sixty avatar Nov 28 '23 00:11 max-sixty