You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This originally came up when fielding a user's question in a community Slack thread. In their own words:
this works fine - is there another way to do the 2nd half of this, to “directly” construct the new record without the […] and the drop ?
echo '[{"key":"bear","name":"fozzy"},
{"key":"bear","name":"smokey the"},
{"key":"cat","name":"morris"}]' |
zq -J 'over this | collect(this) by key | this[this.key]:=this.collect | drop key,collect' -
i was trying something like
zq -J 'over this | collect(this) by key | {(this.key):this.collect}' -
but that’s not valid syntax
the tricky part (to me) being the name of the new field needs to be “dynamic” - set to the value.
@nwt ultimately showed the user a way to more directly get their desired result, but also noted:
As you’ve noticed, dynamic field names aren’t currently allowed in record expressions by design, but I don’t see any reason that couldn’t change.
My example in the opening issue text is a simplified take on illustrating this limitation. It's currently possible to get the desired result in a roundabout way, e.g.,
In a group discussion about this one, I was reminded that while this isn't yet possible via record expressions, we did previously make this possible via put and cut (#4795), which provides a cleaner interim workaround, e.g.,
Back on the subject of doing this via record expressions, @mccanne did express some nervousness about the potential for users to explode the number of types if we make it super easy to create dynamic keys via record expressions, though we certainly understand it's something people will sometimes want to do. An approach the group seemed to like was to point users toward using Zed map types whenever possible (e.g., create them via the collect_map aggregate function), then make it easy to cast those maps into record types if that's what they ultimately want.
tl;dr
It's not currently possible to specify a dynamic field name in a record expression, such as my attempt here to get to output
{hi:"bye"}
.Details
Repro is with Zed commit 343ac63.
This originally came up when fielding a user's question in a community Slack thread. In their own words:
@nwt ultimately showed the user a way to more directly get their desired result, but also noted:
My example in the opening issue text is a simplified take on illustrating this limitation. It's currently possible to get the desired result in a roundabout way, e.g.,
However it's probably a tall order to expect users to come up with that on their own.
The text was updated successfully, but these errors were encountered: