Function syntax and the pipe operator
I have thoughts about this example function and its usage:
func if_valid x = is_valid_price ? x : null
let return_total = prices_adj | ret | if_valid
The use of is_valid_price bugs me a lot as it's not actually in scope of the function. What would happen if it was called on a table that didn't have that column?
I'd suggest having if_valid take in is_valid_price as an argument, then calling it like this:
let return_total = prices_adj | ret | if_valid(is_valid_price) # or maybe if_valid is_valid_price
There is a bit of a precedent for this with languages like F# and their pipeline operator, where x | foo(a, b, c) is interpreted as foo(x, a, b, c):
let squareAndAddOdd values =
values
|> List.filter (fun x -> x % 2 <> 0)
|> List.map (fun x -> x * x + 1)
In fact, I think there's a fair bit of syntax similarity between the proposed this project and ML languages, so they might be worth taking a look at for syntax ideas.
In this proposal, | or newlines act as a kind of implied infix "pipeline" operator - I think this should be made into an explicit operator, especially given this example:
func ret x = x / (x | lag_day) - 1 + dividend_return
Though I think this specific scenario would be better off written in a more traditional way:
func ret x = x / lag_day(x) - 1 + dividend_return # or maybe (lag_day x)
I suspect there may be some weird syntax scenarios to handle, however, especially with expressions. For example, how would something like this get interpreted:
where a > b
and c < d
You'd want this to be interpreted as where a > b and c < d and not as where a > b | and c < d, since although and c < d may be a valid function call, it would make expression precedence parsing really weird and that statement might also be possible to interpret as applying and c < d to where a > b.
Perhaps having a newline followed by an infix operator can prevent the implied | from getting inserted?
Maybe expressions should not implicitly insert | on new lines, but then how would you tell if an expression ended?
Actually, how do you tell apart where (a | foo b) and (where a) | (foo b)?
Finally, some small thoughts:
- That ternary operator should be replaced with a proper
if ... then ... else if ... then ... else ..., this would be easier to read and is the precedent set by SQL and F#, along with also being used in Rust, the language the current parser is written in - Keep
orandandinstead of using||and&&- with|being as important as it is,||will become an absolutely deadly footgun - I understand why
funcwas chosen as the keyword, but maybefunctionshould be accepted as well since most other keywords appear to not be abbreviations - That raw SQL syntax seems more like some sort of a macro syntax and I'm not sure if it's actually necessary? Even if it is, it really shouldn't use double quotes since that would be very weird.
- Honestly, given the many SQL varieties out there I'd just give up and have both single and double quotes be used purely for strings. For escaping identifiers and column names, maybe backticks (used in MySQL) could work?
- For raw SQL, maybe
func upper col = sql UPPER(col)could work?
Hi @SilverEzhik — thanks a lot for the full and thoughtful issue
I'll answer as much as I can now and please let me know if I miss anything:
-
Definitely this takes inspiration from the ML world. So the pipe is one way of calling a function. Specifically, these three are equivalent:
# 1. x | lag_day # 2. lag_day x # 3. ( x lag_day ) -
The use of is_valid_price bugs me a lot as it's not actually in scope of the function. What would happen if it was called on a table that didn't have that column?
Good question. You can think of a column name like
is_valid_priceas either an identifier than needs to be in scope, or a constant. The existing code works if it's considered a constant which doesn't require scope. If it's required to be in scope, it would make functions much less useful — every column name would need to be passed into every function, IIUC. And in PRQL, column names are just strings to us, they're not representing anything more abstract that we need to bind to. Can you think of a case where the current approach would cause any ambiguity in the transpilation? -
FWIW at least in OCaml,
x | foo(a, b, c)is equivalent tofoo(a, b, c, x)(note the different in location of thexfrom above):# let f x y = (x * 10) + y;; val f : int -> int -> int = <fun> # let g = 1 |> f;; (* g now equals (10 + y), because 1 binds to the first arg *) val g : int -> int = <fun> # 2 |> g ;; - : int = 12So that's the current design of PRQL; first bullet here
-
I agree re the issues with boolean logic and infix operators. I discuss some of the tradeoffs in "Thinking about". I'm open to ideas and approaches there.
-
I tend to agree re the ternary operator. We need to figure out how we transpile to SQL's
if ... thenandcaseso we have a single approach and uses common keywords in a consistent way. -
I agree with your views on single vs double quotes, and the raw sql tradeoffs. Backticks are good. One issue is that they conflict with markdown — someone copies a line into a markdown file and then needs to escape it, and f-strings have been really successful. So that's another area we can think about and iterate on.
We can open issues on any of the final three items if people would like to discuss them.
Thanks @SilverEzhik
@max-sixty
Can you think of a case where the current approach would cause any ambiguity in the transpilation?
If PRQL functions are mapped to SQL functions, wouldn't scope become a problem? Or is the plan to have these functions essentially be macros that get substituted into the translated SQL?
One potential fix would be to require scoping functions inside the query:
from some_table
func foo x = ... # foo has access to columns of some_table
join other_table ... # but not to columns of other_table
Thinking about it, this probably doesn't matter if PRQL is just getting compiled into SQL, but if it was aware of the table schema, this could be used to catch errors before the query gets translated.
Probably a silly question , but for the pipe syntax of x | some_func , how do you pass two parameters to the function, x,y | some_func ?
but if it was aware of the table schema, this could be used to catch errors before the query gets translated.
Oh man, that would be amazing.
how do you pass two parameters to the function, x,y | some_func ?
Piping can only pass one arg, so that would be y | some_func x or some_func x y.
but if it was aware of the table schema, this could be used to catch errors before the query gets translated.
I'll try and write something up on this around a roadmap — it seems like there's lots of excitement around it. It's much much harder than the current plan, but would follow on nicely, assuming we can execute on the current plan.
I think these have been answered or covered elsewhere. It was an excellent set of issues!
Please keep them coming @SilverEzhik & @qorrect .