prql icon indicating copy to clipboard operation
prql copied to clipboard

`take` doesn't support parameters

Open not-my-profile opened this issue 2 years ago • 8 comments

from foo
take $count

fails with:

Error: 
   ╭─[:2:6]
   │
 2 │ take $count
   │      ───┬──  
   │         ╰──── `take` expected int or range, but found $count

───╯

not-my-profile avatar Jul 14 '23 16:07 not-my-profile

You cannot use $.

This works:

let foo_count = 50
from foo
take foo_count

vanillajonathan avatar Jul 14 '23 19:07 vanillajonathan

We are quite strict about what's allowed in take parameters, as there are limitations on some database engines. But I do think we should be able to support parameters.

aljazerzen avatar Jul 14 '23 19:07 aljazerzen

Ref #1772

aljazerzen avatar Jul 14 '23 19:07 aljazerzen

Definitely agree that the language should support this...

max-sixty avatar Jul 19 '23 18:07 max-sixty

Hello. Is there any progress on this?

alex-knyaz avatar Sep 12 '23 06:09 alex-knyaz

Hello. Is there any progress on this?

Not yet, no.

If folks would be up for contributing but aren't sure where to start, let us know and we can write up some guidance.

max-sixty avatar Sep 12 '23 08:09 max-sixty

I'm not sure whom 'folks' are (English not my first language), but I would like to try to work on this in my free time.

alex-knyaz avatar Sep 12 '23 09:09 alex-knyaz

I'm not sure whom 'folks' are (English not my first language),

Sorry, an Americanism, despite my European roots. Just "if anyone wants to contribute"...

I'm not sure whom 'folks' are (English not my first language), but I would like to try to work on this in my free time.

Great!

How much rust experience do you have?

You can see the Take struct here: https://github.com/PRQL/prql/blob/04b5174cba01bcc79837e69f0697486210d3d6dd/crates/prql-compiler/src/ir/rq/transform.rs#L32-L37. That probably needs to hold an Expr rather than a Range, and then it can hold parameters.

One approach to start is replacing Range with Expr, and then fix the errors that the compiler shows.

That struct is then converted into another struct a bit later in the pipeline here: https://github.com/PRQL/prql/blob/04b5174cba01bcc79837e69f0697486210d3d6dd/crates/prql-compiler/src/sql/srq/ast.rs#L95. If it's easier, you could start by only replacing it for the initial part of the pipeline, commit that, and then work on the next. (Though it won't work until it flows all the way through, but often breaking up work makes it much easier).

Does that make sense? You're welcome to ask questions here, or PR something partially complete.

CC @aljazerzen, who knows this code best.

max-sixty avatar Sep 13 '23 18:09 max-sixty