zed
zed copied to clipboard
Conditional grep in record index expression seems buggy
tl;dr
The output from the following seems incorrect compared to when we compute each part separately and combine.
this[grep(one*, x) ? "one" : "two"]:=x
Details
Repro is with Zed commit d103420. I bumped into this while trying to write the Zed equivalent of the CASE
logic in mgbench bench3/q6.
The following uses a conditional in a record index expression and works as expected:
$ zq -version
Version: v1.18.0-4-gd1034203
$ echo '{x:1} {x:2}' | zq -Z 'this[x==1 ? "one" : "two"]:=x' -
{
x: 1,
one: 1
}
{
x: 2,
two: 2
}
However, if I try what I think is the equivalent with strings and grep()
, I no longer see the field name one
in the output.
$ echo '{x:"one is a bun"} {x:"two is a shoe"}' | zq -Z 'this[grep(one*, x) ? "one" : "two"]:=x' -
{
x: "one is a bun",
two: "one is a bun"
}
{
x: "two is a shoe",
two: "two is a shoe"
}
It feels like it should work the same since grep()
is supposed to return a boolean. And it does what I expect if I yield
the result of the conditional on its own or launder the field name through an intermediate value.
$ echo '{x:"one is a bun"} {x:"two is a shoe"}' | zq -Z 'yield grep(one*, x) ? "one" : "two"' -
"one"
"two"
$ echo '{x:"one is a bun"} {x:"two is a shoe"}' | zq -Z '_fieldname := grep(one*, x) ? "one" : "two" | this[_fieldname] := x | drop _fieldname' -
{
x: "one is a bun",
one: "one is a bun"
}
{
x: "two is a shoe",
two: "two is a shoe"
}
$ echo '{x:"one is a bun"} {x:"two is a shoe"}' | zq -Z '_fieldname := grep(one*, x) ? "one" : "two" | this[_fieldname] := x | drop _fieldname' -
{
x: "one is a bun",
one: "one is a bun"
}
{
x: "two is a shoe",
two: "two is a shoe"
}