zed icon indicating copy to clipboard operation
zed copied to clipboard

Put query throws error when record fields have special character (hyphen)

Open muthu-rk opened this issue 2 years ago • 1 comments

This is similar to #3888 but for put query.

min_5level.zng.zip

@nwt: Can you suggest a workaround for put query when fields have special characters? It throws "illegal left-hand side of assignment" error

Ex: I wanted to edit level0.level1.**level-2**.value="2-changed". This is the query I tried.

update.zed:
over arr | switch id (
case 2 => put level0.level1["level-2"].value:="2-changed"
default => pass
)
| merge id
| arr:=collect(this)
$ zq -I update.zed min_5level.zng | zq -Z -pretty=2 'over arr | id==2' -
illegal left-hand side of assignment'

However the field-derefence works for yield operation. Like this.

$ zq -i zng -Z 'arr[0].level0.level1["level-2"]["level-3"]["level-4"]' min_5level.zng
{
tags: [
1,
2,
3
],
value: "4",
"level-5": {
tags: [
1,
2,
3
],
value: "5"
}
}

muthu-rk avatar May 13 '22 17:05 muthu-rk

@nwt @mccanne Any workaround available for this issue?

muthu-rk avatar May 16 '22 05:05 muthu-rk

I've confirmed this issue is still present as of Zed commit 8b0cd3e. I'm unfortunately not aware of any workarounds. Here's a simplified repro:

$ zq -version
Version: v1.2.0-39-g8b0cd3e9

$ echo '{"level0":{"level1":{"level-2":{"value":"original value"}}}}' | zq 'put level0.level1["level-2"].value:="2-changed"' -
illegal left-hand side of assignment'

Looking closer at https://zed.brimdata.io/docs/language/overview#65-field-dereference, it seems like this may be undocumented territory. For instance, that doc shows an example of referencing a top-level record field name that contains special characters, but it's not clear from the doc if the syntax shown above is intended to work for such a field name nested further down the hierarchy. A Python-syntax-inspired alternative (which also doesn't currently work) might be:

$ echo '{"level0":{"level1":{"level-2":{"value":"original value"}}}}' | zq 'put this["level0"]["level1"]["level-2"]["value"]:="2-changed"' -
illegal left-hand side of assignment'

philrz avatar Sep 02 '22 18:09 philrz

#4132 fixed this. Both commands in the previous comment now work as expected.

$ zq -version
Version: v1.2.0-85-ge9624c3b

$ echo '{"level0":{"level1":{"level-2":{"value":"original value"}}}}' | zq 'put level0.level1["level-2"].value:="2-changed"' -
{level0:{level1:{"level-2":{value:"2-changed"}}}}

$ echo '{"level0":{"level1":{"level-2":{"value":"original value"}}}}' | zq 'put this["level0"]["level1"]["level-2"]["value"]:="2-changed"' -
{level0:{level1:{"level-2":{value:"2-changed"}}}}

nwt avatar Oct 12 '22 19:10 nwt