prql icon indicating copy to clipboard operation
prql copied to clipboard

Error: "Cannot lower to IR Expr" when using count_distinct

Open thisismiller opened this issue 2 years ago • 2 comments

I was playing with trying to translate a SQL query to PRQL, and hit an error:

"Cannot lower to IR expr: Expr { id: Some(137), kind: List([Expr { id: Some(134), kind: Ident(Ident { path: [\"_frame\", \"S\"], name: \"store\" }), span: Some(span-chars-1069-1076), target_id: Some(65), ty: Some(infer), needs_window: false, alias: None }, Expr { id: Some(135), kind: Ident(Ident { path: [\"_frame\", \"S\"], name: \"till\" }), span: Some(span-chars-1078-1084), target_id: Some(65), ty: Some(infer), needs_window: false, alias: None }, Expr { id: Some(136), kind: Ident(Ident { path: [\"_frame\", \"S\"], name: \"transaction\" }), span: Some(span-chars-1086-1099), target_id: Some(65), ty: Some(infer), needs_window: false, alias: None }]), span: Some(span-chars-1068-1100), target_id: None, ty: Some(list), needs_window: false, alias: None }"

And I'm assuming this represents some sort of compiler bug.

This is using the online playground, and the PRQL query to reproduce is available at https://gist.github.com/thisismiller/33726cce15c764f852313f4c404e6ec0

thisismiller avatar Dec 29 '22 05:12 thisismiller

Great bug report, thanks for posting @thisismiller . To the extent it's possible to minimize the example into a smaller one, that would be even better.

Either way, we'll take a look.

max-sixty avatar Dec 29 '22 07:12 max-sixty

Ah, the identifiers mentioned point towards count_distinct, which lead to an easy minimal repro:

from T 
group [x] (
  derive [
    transactions = count_distinct [y]
  ]
)

thisismiller avatar Dec 29 '22 19:12 thisismiller

This error should say "expected a column, found a list of columns".

With #1397 the result now is:

Error:
    ╭─[:60:35]
    │
 60 │     transactions = count_distinct [y]
    ·                                   ─┬─
    ·                                    ╰─── unexpected `[_frame.`T`.y]`
    ·
    · Help: this is probably a 'bad type' error (we are working on that)
────╯

Again, this will be improved when we improve typing.


As for your query, use:

from T 
group [x] (
  derive [
    transactions = count_distinct y
  ]
)

aljazerzen avatar Jan 02 '23 14:01 aljazerzen