brimdata / zed

A novel data lake based on super-structured data
https://zed.brimdata.io/
BSD 3-Clause "New" or "Revised" License
1.34k stars 67 forks source link

Panic when accessing variable inside switch-in-lateral-query-in-a-switch #5076

Closed philrz closed 3 months ago

philrz commented 3 months ago

Repro is with Zed commit f1be6a4.

This is a simplified [ 🤪 ] example of a panic I bumped into while trying to come up with a response to a community user's inquiry in a Slack thread.

$ zq -version
Version: v1.14.0-17-gf1be6a4a

$ cat repro.zed 
const a = 1
const b = 2
switch a (
  case 1 => over this with val=this => (
    switch b (
      case 2 => yield val
    )
  )
)

$ echo '[1,2]' | zq -I repro.zed -
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
runtime/debug.Stack()
    /usr/local/opt/go@1.21/libexec/src/runtime/debug/stack.go:24 +0x5e
github.com/brimdata/zed/runtime/sam/op.(*Catcher).Pull.func1()
    /Users/phil/work/zed/runtime/sam/op/catcher.go:25 +0x3d
panic({0x2550000?, 0xc000916000?})
    /usr/local/opt/go@1.21/libexec/src/runtime/panic.go:914 +0x21f
github.com/brimdata/zed/runtime/sam/expr.Var.Eval(...)
    /Users/phil/work/zed/runtime/sam/expr/var.go:14
github.com/brimdata/zed/runtime/sam/op/yield.(*Op).Pull(0xc0006e0a20, 0x10?)
    /Users/phil/work/zed/runtime/sam/op/yield/yield.go:31 +0x432
github.com/brimdata/zed/runtime/sam/op/traverse.(*Exit).pullPlatoon(0xc0006e0a50)
    /Users/phil/work/zed/runtime/sam/op/traverse/scope.go:222 +0x32
github.com/brimdata/zed/runtime/sam/op/traverse.(*Exit).Pull(0xc0006e0a50, 0x5?)
    /Users/phil/work/zed/runtime/sam/op/traverse/scope.go:208 +0x2b
github.com/brimdata/zed/runtime/sam/op.(*Single).Pull(0xc000407398, 0x60)
    /Users/phil/work/zed/runtime/sam/op/mux.go:134 +0x2b
github.com/brimdata/zed/runtime/sam/op.(*Catcher).Pull(0x3cc1108?, 0xc0?)
    /Users/phil/work/zed/runtime/sam/op/catcher.go:28 +0x62
github.com/brimdata/zed/runtime/exec.(*Query).Pull(0x4abf4498?, 0xd8?)
    /Users/phil/work/zed/runtime/exec/query.go:59 +0x3e
github.com/brimdata/zed/zbuf.CopyPuller({0x4abf4498, 0xc000406ee8}, {0x28c1520, 0xc0006e0a80})
    /Users/phil/work/zed/zbuf/batch.go:162 +0x4d
github.com/brimdata/zed/cli/zq.(*Command).Run(0xc000124a80, {0xc0000400b0, 0x1, 0x1})
    /Users/phil/work/zed/cli/zq/command.go:162 +0x79e
github.com/brimdata/zed/pkg/charm.path.run({0xc00007a2d0?, 0x1, 0x1}, {0xc0000400b0?, 0x1, 0x0?})
    /Users/phil/work/zed/pkg/charm/path.go:11 +0x79
github.com/brimdata/zed/pkg/charm.(*Spec).ExecRoot(0x100874b?, {0xc000040090, 0x3, 0x3})
    /Users/phil/work/zed/pkg/charm/charm.go:63 +0x3f
main.main()
    /Users/phil/work/zed/cmd/zq/main.go:11 +0x5b
philrz commented 3 months ago

Verified in Zed commit 3e5febe.

The program shown in the repro above no longer generates a panic.

$ zq -version
Version: v1.14.0-21-g3e5febe7

$ echo '[1,2]' | zq -I repro.zed -
[1,2]
[1,2]

Thanks @mattnibs!