Repository navigation
Description
Abstract:
I propose to change references to columns from column to .column.
Reasoning:
I'll try to explain how resolver works and how I think about semantics of name and variables in PRQL.
During resolving, there is a major distinction between scoped and ephemeral variables:
- Scoped variables have a definition and live until their scope exists. For example,
std.sumandstd.selectare global so they exist indefinitely, and function parameters exist only within function body. - Ephemeral variables are just references into some other argument of a current function call. For example, when you call
select, all columns of the relation exist as variables during resolution of the first argument.
It is beneficial to distinguish these two mechanism, because of their subtle differences. For example take this query:
func my_transform rel -> (
rel
select [alb.title, artist_id]
)
from alb = albums
my_transfrom
Here, relation is constructed with from and within the relation a name alb is assigned all column from table albums. Note that alb is not a "real" value, it's just a namespace for the columns. When this relation is passed to my_transform, it is stored in the rel parameter. rel is now a scoped variable while alb.title is a reference to one of its columns.
I'm not sure if I've explained that well, please tell me if I haven't.
If I compare this behavior with, say, Python and a dataframe library, scoped variables are all normal idents, while ephemeral variables would be represented with strings. This is a bit more verbose and cannot provide good errors, typing or autocomplete. (This is feature of PRQL that dataframe libraries cannot copy. Only a custom language for relations can construct custom rules for name resolution.)
So because there is distinction in resolving, I suggest we add a distinction in syntax:
func my_transform rel -> (
rel
select [.alb.title, .artist_id]
)
from alb = albums
my_transfrom
sort .title
Pros:
- distinction in syntax hints to the distinction in resolving
- for newcomers, the rule is simple: columns start with a dot
Cons:
- additional syntax we could be without
Activity
snth commentedon Jan 24, 2023
I quite like the idea of a leading . for columns. I don't really know why yet but it feels like it would bring additional consistency. It also reminds me of JDOT (https://github.com/saulpw/jdot).
TBH, I did not understand the name resolution explanation yet but I will try again in the morning (it's close to midnight now). For example why is it
alb.titleinmy_transforminitially and notrel.title? And with the new syntax, why is it still.alb.titleand not just.title(or if thealbis required thenalb.title)?Another possible benefit could be that it might disambiguate a column named "from" from the keyword
fromsince the column would be referred to as.from. (IDK if this is currently a problem for the parser/compiler.)eitsupi commentedon Jan 28, 2023
Perhaps the origin is jq?
https://stedolan.github.io/jq/
I think jq is a very popular language for writing queries to json.
max-sixty commentedon Feb 3, 2023
Sorry to take a while to respond.
I think I'm understanding 85% of this,so forgive me if I'm slow.
I can see two points here;
.foofor some variablesRe the discriminaring — how easy do you think it is to explain when to use a period vs. not to? I worry it's not easy! (But possibly we could make it easier).
Re the periods — I don't have a strong secular objection to it. It would be a big change, and I'm not sure it gets us that much apart from the discrimination. But it is an effective way of allowing columns to be clearly different from functions.
To what extent do you think it's accurate to describe emphermal variables as just having a scope that's limited to that line?
aljazerzen commentedon Feb 3, 2023
It just one point here: use
.foofor ephemeral variables.The rule for when to use the dot is simple: columns start with a dot.
That's pretty accurate. But it may be confusing because even though the scope is limited to current function, almost identical scope could be created for next function in the pipeline.
max-sixty commentedon Feb 3, 2023
Totally, but is there an easy way to define ephemeral variables to beginners?
aljazerzen commentedon Feb 4, 2023
I'm saying that for beginners, ephemeral variables can be equivalent to columns. So the whole rule is columns start with a dot. And we don't even mention ephemeral variables.
That's because we don't have anything other than relations that we'd want to have references into. Maybe in the future, we could add support for referencing properties of JSON objects or structs.
max-sixty commentedon Feb 4, 2023
Yes OK, that is complete in the examples above.
How about when it's a variable; for example:
Thanks for bearing with me...
aljazerzen commentedon Feb 4, 2023
Oh, params are scoped variables so they don't need a leading dot. So like this:
max-sixty commentedon Feb 4, 2023
OK great, I see, thanks.
I think it's tractable. I don't think it's that friendly, and it's much more alien for those who are used to SQL.
Do others share a concern that represents hierarchies inconsistently? For example
albis a relation. But to go into that hierarchy involves adding a period at its start; i.e..alb.title. Generally to move down a hierarchy we'd only add things onto the end likealb.titleoralb["title"]I think this is insightful, and maybe we should discuss it more in our docs...
....I've heard this referred to as "bare words". I find it a great advantage of PRQL over something like python. It makes sense that we promote columns to not require quotes, since columns are so important in tabular data; they're almost like variables to us.
As @eitsupi points out,
jquses the.foosyntax, and that's worked well, though they use it all the way down the hierachy; i.e..alb, never justalb.So my current view is:
How important do you think it is for the development of the lang? Can we instead have a hierarchy of scopes (like many langs do), and resolve ephemeral variables first, and scoped variable after that?
eitsupi commentedon Feb 5, 2023
I recall that in dplyr, it is sometimes difficult to distinguish between variables outside the data frame and column names in the data frame, making the behavior confusing.
It can be specified explicitly by
.dataor.env(but many people rarely do this because it increases the amount of writing).https://rlang.r-lib.org/reference/dot-data.html
I think it is a good balance of clarity and ease of writing to always start column names with a dot.
aljazerzen commentedon Mar 1, 2023
I've implemented the proposal and converted the tests in prql-compiler.
Here are a few examples:
Here is my findings:
.would bring up just columns for current relation,.*to refer to all columns of the relation, where before we could not use*(since that would be parsed as multiplication).Possible alternatives:
rel.first_nameinstead of just.first_name. In this case,rel.prefix would also be optional.max-sixty commentedon Mar 1, 2023
Thanks for the list of findings, that's v helpful to anchor around.
Is this still the same for the full path of columns? Or does
alb.titlework?I think the
.colsyntax is fine from a blank slate, but — overall, in the current state I'm fairly strongly -1.jq, which uses dots, but is consistent across theseOne lens to view this is what we'd write in the Changelog — I'm not sure what we'd write that I'd feel great about...
aljazerzen commentedon Mar 2, 2023
Actually, this is the confusion that this issue is trying to avoid.
It separates these two cases:
References to things in global scope don't have a leading dot:
References into subject of the current pipeline have a leading dot:
So if you are able to refer to
albums, you are still able to refer toalbums.title.aljazerzen commentedon Mar 2, 2023
The implementation complexity hasn't changed enough to weigh into the decision here.
And sharp corners that you mention are intentional - a syntactical spotlight of semantics. So they are actually the main benefit. Think of it as the borrow checker in Rust.
But all that said, this change goes strongly against the concise nature of the language we've been able to maintain.
So my vote is -0.5.
snth commentedon Mar 5, 2023
Thanks for trying this out @aljazerzen . Reading through your examples in #1619 (comment) I'm also struck by how there is this inconsistency between rvalues and the lvalues in
deriveandaggregate. Would it be possible to add the leading.for lvalues as well? (Not saying we should do this as we seemed to be converging on not going ahead with this proposal, just curious if it would be possible in theory since then we could restore consistency?)Overall, I'm still unclear on the ephemeral vs scoped variables. I was seeing the
.colas a shortcut for_frame.coland as such I thought it made some sense. It is quite different to what we/most people know from other SQL/database type systems but I think one could get used to it. The.is a relatively unobtrusive piece of punctuation so I personally don't feel that it gets in the way that much. I would still be open to it if we wanted to explore it more.aljazerzen commentedon Mar 5, 2023
Yes, and it would be quite easy to do actually.
I'll take the liberty to interpret @snth's comment as a vote of +0. Total tally is -1.5, which means that we will not be adding this feature.
We can revisit it when there new features that would work well with this.
max-sixty commentedon Mar 6, 2023
Great, thanks for the productive discussion and exploration effort.
max-sixty commentedon May 1, 2023
I've been working with
jqrecently. They have a take of this, but I think with much easier semantics:..date, or a reference into a struct would be.orders.addressSo for example, the case above would be:
I think the
from Xis almost the only thing that changes from the full examples above — since the discriminant is whether it's referring to data, not the exact scope of the data.max-sixty commentedon Mar 3, 2024
As discussed on the call, I'm not sure my example was correct — instead
.albums.titleis already within the.albumsscope, and so should be:.title$.albums.title..to go up a level —..albums.titlebayareaunicorn commentedon Mar 3, 2024
Great work
max-sixty commentedon Mar 4, 2024
Reopening as this is under consideration again. Possibly we start a new issue synthesizing where we're at, given the amount of history though.