cases:
  - note: non_opt
    query: |
      z := data.foo.q
    modules:
      - |
        package foo
        q = 1
    want_result:
      - z: 1
  - note: simplest
    query: |
      x := "q"; z := data.foo[x]
    modules:
      - |
        package foo
        q = 1
    want_result:
      - x: q
        z: 1
  - note: two packages, one irrelevant
    query: |
      x := "q"; z := data.foo[x]
    modules:
      - |
        package foo
        q = 1
      - |
        package bar
        q = 2
    want_result:
      - x: q
        z: 1
  - note: more layers
    query: |
      x := "aaa"; u := "ccc"; z := "q"; w := data.foo[x].bar[u].baz[z]
    modules:
      - |
        package foo.aaa.bar.ccc.baz
        q = 1
      - |
        package foo.aaa.bar.ddd.baz
        q = 2
    want_result:
      - x: aaa
        u: ccc
        w: 1
        z: q
  - note: leftover ref when optimization planned
    query: |
      x := "aaa"; z := "q"; w := data.foo[x].bar[z].foo.baz
    modules:
      - |
        package foo.aaa.bar
        q = {
          "foo": {
            "baz": 100
          }
        }
    want_result:
      - x: aaa
        z: q
        w: 100
  - note: lookup involving 'with'
    query: |
      x := "q"; a := "bar"; z := data.foo[a][x] with data.foo.baz as 200
    modules:
      - |
        package foo.bar
        q = 1
    want_result:
      - x: q
        a: bar
        z: 1
  - note: memoization
    query: |
      x := "q"; z := data.foo[x]; w := data.foo[x]
    modules:
      - |
        package foo
        import rego.v1
        
        q = 1 if {
          custom_builtin_test_memoization() == 100
        }
    want_result:
      - x: q
        z: 1
        w: 1
  - note: data ref used after failed lookup
    query: data.foo.p = x
    modules:
      - |
        package foo
        import rego.v1
        
        p = y if {
          k := "q"
          y := data.bar[k].baz
        }
      - |
        package bar
        p = { "baz": 100 }
    data:
      bar:
        q:
          baz: 8
    want_result:
      - x: 8
  - note: data ref not used after successful lookup
    query: data.foo.p = x
    modules:
      - |
        package foo
        import rego.v1
        
        p = y if {
          k := "p"
          y := data.bar[k].baz
        }
      - |
        package bar
        p = { "baz": 100 }
    data:
      bar:
        q:
          baz: 8
    want_result:
      - x: 100
  - note: func lookup unsuccessful, data deref unsuccessful
    query: data.test.p = x
    modules:
      - |
        package test
        import rego.v1

        p if {
          x := "foo"
          data.other[x].p
        }
      - |
        package other.bar # not "foo", but existence triggers dynamic lookup

        p = true
    data: {}
    want_defined: false
  - note: func lookup successful, but call yields undefined
    query: data.test.p = x
    modules:
      - |
        package test
        import rego.v1

        p if {
          x := "foo"
          data.other[x].p
        }
      - package other.foo # empty
      - |
        package other.bar # not foo

        p = true
    data: {}
    want_defined: false
  - note: func lookup successful, but call yields undefined (in a loop)
    query: data.test.p = x
    modules:
      - |
        package test
        import rego.v1
        
        xs := {"foo", "baz", "123"}
        p if {
          xs[x]
          data.other[x].p
        }
      - package other.foo # empty
      - |
        package other.bar # not foo

        p = true
    data: {}
    want_defined: false
  - note: func lookup in a loop, one call yields a result
    query: data.test.p = x
    modules:
      - |
        package test
        import rego.v1
        
        xs := {"fox", "baz", "123", "xyz"}
        p = y if {
          xs[x]
          y := data.other[x].p
        }
      - package other.foo # empty
      - |
        package other.xyz

        p = "yay"
    data: {}
    want_result:
      - x: yay
