zed
zed copied to clipboard
Warn/error on "over ... with this"
tl;dr
A community user recently found the Zed language allowed them to write a lateral subquery preceded by over ... with this
and the resulting overlapping uses of this
values inside the subquery caused confusion. Since a lateral subquery is often used to work with the sequence of outer values via inner references to this
, we recognized it would be helpful to surface a warning or error if a user's program invokes this overlapping usage, since it's probably a mistake.
Details
Repro is with Zed commit 71e35c5.
This issue was surfaced in a community Slack thread. They first started with the query shown below, which I'm executing now with our own simplified aws.json test data:
{
"Reservations": [
{
"Groups": [],
"Instances": [
{
"InstanceId": "i-033bd8fe8b30bb00f",
"Tags": [
{
"Key": "Name",
"Value": "shasta"
}
]
}
]
},
{
"Groups": [],
"Instances": [
{
"InstanceId": "i-019bcf4e640743aa6",
"Tags": [
{
"Key": "Name",
"Value": "phil-win2019"
}
]
}
]
}
]
}
$ zq -version
Version: v1.17.0-20-g71e35c5d
$ zq -z 'over Reservations | over Instances
| over Tags with obj=this => ( where Key=="Name" | {name: this.Value, id:obj.InstanceId} )' aws.json
{name:"shasta",id:"i-033bd8fe8b30bb00f"}
{name:"phil-win2019",id:"i-019bcf4e640743aa6"}
The user then remarked:
Huh - I noticed with a couple small edits - I could say
with this
(vs.with obj=this
) in the lateral subquery ... and by removing thethis
fromthis.Value
since those collide now ... it still does work:zq -z ' over Reservations | over Instances | over Tags with this => ( where Key=="Name" | {name: Value, id:this.InstanceId} )' aws.json {name:"shasta",id:"i-033bd8fe8b30bb00f"} {name:"phil-win2019",id:"i-019bcf4e640743aa6"}
But - that feels ... dangerous? Or odd or something? Maybe it's not.
The way he was making an implied field reference to just Value
was allowing him to still access the "inner" data that would normally be referenced as this.Value
. He ultimately figured out on his own what was going on.
I guess it just means that
this
is going to stomp the "implied"this
inside the subquery. that's at least what it does:zq -z ' over Reservations | over Instances | over Tags with this => ( where Key=="Name" | {name: this.Value, id:this.InstanceId} )' aws.json {name:error("missing"),id:"i-033bd8fe8b30bb00f"} {name:error("missing"),id:"i-019bcf4e640743aa6"}
However, since it required a close reading of the docs to clear up the confusion, some messaging from the tooling would have also been helpful. In the user's opinion:
off the cuff, i’d vote for warning rather than error … it’s not broken per se, just … stompy :hiking_boot: :smile: