Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional grep in record index expression seems buggy #5309

Open
philrz opened this issue Sep 27, 2024 · 1 comment
Open

Conditional grep in record index expression seems buggy #5309

philrz opened this issue Sep 27, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@philrz
Copy link
Contributor

philrz commented Sep 27, 2024

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"
}
@philrz philrz added the bug Something isn't working label Sep 27, 2024
@philrz
Copy link
Contributor Author

philrz commented Sep 27, 2024

A preliminary reaction from @mattnibs:

Looks like there's a weird compiler bug here:

$ zed dev compile -s -C 'this[grep(one*, x) ? "one" : "two"]:=x'
reader
| put two:=x
| output main

I'm guessing the lval logic is stripping out regexpSearch expression that grep gets converted into.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant