Named args vs assignments
One issue https://github.com/max-sixty/prql/pull/52 raises is how assignments are parsed; specifically:
derive gross_salary: salary + payroll_tax,
Should parse into:
- func: derive
- assign:
- lvalue: gross_salary
- rvalue: salary + payroll_tax
But named args' syntax conflict with this. For example:
aggregate by:[title] [sum salary]
should parse into:
- func: aggregate
- assign:
- lvalue: by
- rvalue: [title]
- arg: [sum salary]
...and not
- func: aggregate
- assign:
- lvalue: by
- rvalue:[title] [sum salary]
...which is what the assign implies.
Options:
- change syntax so only one of these uses
:- Though trying to avoid using
=so we can keep the SQL (and human!) use of this
- Though trying to avoid using
- force the assign to always take one arg, so
gross_salary: salary + payroll_taxneeds to begross_salary: (salary + payroll_tax) - restore the rules around whitespace, such that passing named args disallows whitespace (i.e.
by:[title]and notby: [title]). But this makes semantics very dependent on small changes in whitespace, which could get confusing. An autoformatter isn't going to be able to deal with this
References:
- OCaml approach
- F# approach (broadly similar)
- Swift approach (broadly similar)
Currently going with "restore the rules around whitespace, such that passing named args disallows whitespace" in https://github.com/max-sixty/prql/pull/52, though liable to change
I only just now realized 'by' was a named argument to aggregate. Makes sense, I like your conclusion.
So named arguments will be supported for every function, both built in and user generated ?
So named arguments will be supported for every function, both built in and user generated ?
Yes exactly! The built-in ones are fairly limited though (e.g. by for aggregate, order for sort, etc)
I've also needed some time to realize that by is a named param and not a part of aggregate by. Maybe we should put that into the docs as a hint. It only seemed inconsistent that from does not have : but aggregate by as has. Also, it was confusing that there must not be any spaces after by:.
In my opinion, it is very confusing that there are two key-value constructs in the language. While it would be sensible to have table aliases be assigns (just as derive is) it is very inconsistent when using in combination with side:
...
join side:left e: employees [emp_id]
where side: must not have white space and e: must.
My opinion is that we should have only one key-value token and distinguish between the two cases with better parsing. For example:
> aggregate by: gender [count: count *]
< aggregate:
< by: gender
< - [count: count *]
and
> aggregate by: gender and [count: count *]
< aggregate:
< by: gender and [count: count *]
I believe this would be possible to do, but would require that we parse expressions as a AST-tree, not just a list as we do now (expr = { ( terms | operator | WHITESPACE )+ }).
If this is not possible, I would go with your second option and limit assign to only one term.
Ok, another idea: new operator for assigns: ~
In math, it means "is similar to" or "has same distribution as", which makes it suitable.
from employees ~ e
derive [ year(now() - date_of_birth) ~ age ]
It uses left to right notation (just as SQLs AS) and is similar to pipes that have converted classical notation of foo(bar(baz(x))) into baz(x) | bar | foo. This way, the code is readable in natural way: "derive" "some formula" "which is now known as" "age"
The up side is that is can be used for both derive and table aliases. It is also similar to AS keyword, but promotes concise notation of PRQL.
What are the reasons for using right to left in assign? It is more common for procedural languages and not for functional ones..
Great, very much agree with @aljazerzen 's sentiment. This is quite a compelling case:
In my opinion, it is very confusing that there are two key-value constructs in the language. While it would be sensible to have table aliases be assigns (just as derive is) it is very inconsistent when using in combination with side:
... join side:left e: employees [emp_id]
So I can see two main options, assuming people agree we should change the current approach:
- One-arg: enforce exactly one argument following the
:operator. - New op: Use a different operator between a named arg, and an assign, possibly with a swap between left & right.
Here are a couple examples: what are others?
| Approach | derive |
|---|---|
| Existing | derive gross_salary: salary + payroll_tax |
| One-arg | derive gross_salary: (salary + payroll_tax) |
| New op | derive (salary + payroll_tax) ~ gross_salary |
| Approach | alias |
|---|---|
| Existing | join employees:e |
| One-arg | join (employees: e) |
| New op | join e~employees |
Other threads:
-
Using
aswould be an option... -
Ordering — during the initial burst of feedback, the consensus was that having the "lvalue" (i.e.
gross_salary) on the left was easier to read — that defining the term first seemed more logical.What are the reasons for using right to left in assign? It is more common for procedural languages and not for functional ones
What are examples of having the "lvalue"on the right, apart from SQL?
Well there is also:
| Approach | derive |
|---|---|
| Existing, but with parsed expressions | derive gross_salary: salary + payroll_tax |
| Approach | alias |
|---|---|
| Existing, but with parsed expressions | join employees: e |
This is possible I believe, but we have to implement expression parsing, so we know when an expression ends. So when I write:
a AND b + c OR d e AND f
it can be parsed as:
(a AND (b + c)) OR de AND f
What are examples of having the "lvalue"on the right, apart from SQL?
To me personally, I it easier to read "take expression as use it as this name" than "this name now represents this expression". It's just the order of reading. When I read procedural code:
b = a + 1
c = b / 154
d = 125 * c - 12
it is the opposite of how you read likes in a book:

But if we swap, it reads like normal lines:

But that's just my preference...
This is possible I believe, but we have to implement expression parsing, so we know when an expression ends. So when I write:
But I don't think we have enough information to do that i.e. take the example above:
derive gross_salary: salary + payroll_tax
aggregate by: [title] [sum salary]
The second line needs to be parsed as:
- func: aggregate
- assign:
- lvalue: by
- rvalue: [title]
- arg: [sum salary]
...and not
- func: aggregate
- assign:
- lvalue: by
- rvalue:[title] [sum salary]
...which is obv different to how the first line is parsed.
So I don't think we can't handles these both correctly context-free — we could write something that handles derive differently to aggregate, but it makes it more confusing for both the parser and the user.
In the case above:
a AND b + c OR d e AND f
...how do we know that d e isn't a function call, like sum salary?
Oh yeah, if we have C-style notation you can see there is no operator between d and e, so CFG should be able to parse that.
But with our function calls that's ambiguous. Yeah my bad, there are only the three options you've outlined.
And I like the third one the most, because assign and named param are conceptually different, so they should have different notations. But alias and derive are not, so they should have the same.
I'm liking the third option as well. Especially since we can re-use ~ in other places we need aliases.
I'm still thinking about this. Thanks a lot for the comments.
Left vs. right
On the left/right discussion — when we had this discussion last time (I couldn't find the explicit discussion, though https://github.com/max-sixty/prql/issues/6 is tangential), people though that starting with the variable name was clearer — i.e. lvalue on the left. That resonates with me, at least for reading. It also faster to find a variable among many, since their names are all aligned on the left.
I do hear the point of it being contrary to how a fictional book reads though.
Because every programming language I know starts with lvalues except SQL (are there others?), I think we should be quite confident before changing — it's both less familiar to users, and it's possible we're missing something (Chesterton's fence!).
~ vs :
The only downside I can see for using : for both named-args & assignments is that derive gross_salary: (salary + payroll_tax) is required rather than derive gross_salary: salary + payroll_tax. Otherwise, it's consistent enough with a named arg that I think it's quite clear.
I'm not sure how much that is worth. My guess is that we could provide a good error message so that it wasn't confusing — basically a derive expression has more than one term. And we're not going to need so many parentheses that PRQL becomes like Lisp.
I very much agree that in theory ~ is close to =. But I'm not sure people will see it that way — it's not used like that in programming languages generally (though I don't have much confidence here). It's used in Ocaml in named arguments, but before the term; i.e. ~f:map. If people really want it to be different to :, another option is to use := or <-.
That said, if @qorrect & @aljazerzen both confidently prefer to use lvalue on the right and/or ~, I will relent on this...
Last few days I've been working on a major code change and shelved discussion on this.
During this time, I've written some queries with proposed ~ notation and I have to admit it more readable to have alias on the left. As you said, names are all aligned on the left and we are used to it from other languages.
Then, I worked a lot on the parser, and I've been able construct a grammar expressions are actually parsed as a tree, which means we can go with:
| Approach | derive |
|---|---|
| Existing, but with parsed expressions | derive gross_salary: salary + payroll_tax |
| Approach | alias |
|---|---|
| Existing, but with parsed expressions | join employees: e |
a AND b + c OR d e AND f would parse into a AND (b + c) OR d(e) AND f
I very much agree that in theory
~is close to=. But I'm not sure people will see it that way — it's not used like that in programming languages generally (though I don't have much confidence here). It's used inOcamlin named arguments, but before the term; i.e.~f:map. If people really want it to be different to:, another option is to use:=or<-.
I'd really avoid := if we can. At least in the python community, the decision to use this left an awful taste in a lot of people's mouth (possibly was even the reason Guido resigned as BDFL).