prql icon indicating copy to clipboard operation
prql copied to clipboard

Extend `let` beyond relations

Open vanillajonathan opened this issue 2 years ago • 2 comments

This works:

let some = 5

from employees
take some

But this does not:

let some = 5 + 5

from employees
take some

vanillajonathan avatar Feb 10 '23 21:02 vanillajonathan

Yes, currently let needs to resolve to a table / relation.

Relaxing this would be good! It's not trivial in the compiler; and it would be less frequently used than for relations, but it would be an elegant generalization.

I'm going to adjust the title slightly if that's OK @vanillajonathan

max-sixty avatar Feb 11 '23 02:02 max-sixty

I had a look at implementing this. It's not obvious how to do it.

When we lower the expressions, we need to create a table instance, since that's the only thing that RQ can hold.

Currently we do that here: https://github.com/PRQL/prql/blob/9b21e945282130fea5619d994ff1568b4ea46f0d/prql-compiler/src/semantic/lowering.rs#L198

Just above that line, we do allow a Literal in RelationKind: https://github.com/PRQL/prql/blob/9b21e945282130fea5619d994ff1568b4ea46f0d/prql-compiler/src/ast/rq/mod.rs#L41-L46

But should we also allow other types like expressions? Or should we resolve those first?

@aljazerzen if you have thoughts...

max-sixty avatar Feb 18 '23 05:02 max-sixty