---
setup:
  - requires:
      test_runner_features: [capabilities, contains, allowed_warnings]
      capabilities:
        - method: POST
          path: /_query
          parameters: []
          capabilities: [enable_lookup_join_on_aliases]
      reason: "uses LOOKUP JOIN on aliases"
  - do:
      indices.create:
        index: test
        body:
          mappings:
            properties:
              key:
                type: long
              color:
                type: keyword
  - do:
      indices.create:
        index: test-mv
        body:
          mappings:
            properties:
              key:
                type: long
              color:
                type: keyword
  - do:
      indices.create:
        index: test-lookup-1
        body:
          settings:
            index:
              mode: lookup
          mappings:
            properties:
              key:
                type: long
              color:
                type: keyword
  - do:
      indices.create:
        index: test-lookup-2
        body:
          settings:
            index:
              mode: lookup
          mappings:
            properties:
              key:
                type: long
              color:
                type: keyword
  - do:
      indices.create:
        index: test-lookup-mv
        body:
          settings:
            index:
              mode: lookup
          mappings:
            properties:
              key:
                type: long
              color:
                type: keyword
  - do:
      indices.create:
        index: test-lookup-no-key
        body:
          settings:
            index:
              mode: lookup
          mappings:
            properties:
              no-key:
                type: long
              color:
                type: keyword
  - do:
      indices.update_aliases:
        body:
          actions:
            - add:
                index: test-lookup-1
                alias: test-lookup-alias
            - add:
                index: test-lookup-*
                alias: test-lookup-alias-pattern-multiple
            - add:
                index: test-lookup-1*
                alias: test-lookup-alias-pattern-single
  - do:
      bulk:
        index: "test"
        refresh: true
        body:
          - { "index": { } }
          - { "key": 1, "color": "red" }
          - { "index": { } }
          - { "key": 2, "color": "blue" }
  - do:
      bulk:
        index: "test-lookup-1"
        refresh: true
        body:
          - { "index": { } }
          - { "key": 1, "color": "cyan" }
          - { "index": { } }
          - { "key": 2, "color": "yellow" }
  - do:
      bulk:
        index: "test-mv"
        refresh: true
        body:
          - { "index": { } }
          - { "key": 1, "color": "red" }
          - { "index": { } }
          - { "key": 2, "color": "blue" }
          - { "index": { } }
          - { "key": [0, 1, 2], "color": null }
  - do:
      bulk:
        index: "test-lookup-mv"
        refresh: true
        body:
          - { "index": { } }
          - { "key": 1, "color": "cyan" }
          - { "index": { } }
          - { "key": 2, "color": "yellow" }
          - { "index": { } }
          - { "key": [0, 1, 2], "color": "green" }
  - do:
      bulk:
        index: "test-lookup-no-key"
        refresh: true
        body:
          - { "index": { } }
          - { "no-key": 1, "color": "cyan" }
          - { "index": { } }
          - { "no-key": 2, "color": "yellow" }

---
alias-as-lookup-index:
  - do:
      esql.query:
        body:
          query: 'FROM test | SORT key | LOOKUP JOIN test-lookup-alias ON key | LIMIT 3'

  - match: {columns.0.name: "key"}
  - match: {columns.0.type: "long"}
  - match: {columns.1.name: "color"}
  - match: {columns.1.type: "keyword"}
  - match: {values.0: [1, "cyan"]}
  - match: {values.1: [2, "yellow"]}

---
alias-repeated-alias:
  - do:
      esql.query:
        body:
          query: 'FROM test-lookup-alias | SORT key | LOOKUP JOIN test-lookup-alias ON key | LIMIT 3'

  - match: {columns.0.name: "key"}
  - match: {columns.0.type: "long"}
  - match: {columns.1.name: "color"}
  - match: {columns.1.type: "keyword"}
  - match: {values.0: [1, "cyan"]}
  - match: {values.1: [2, "yellow"]}

---
alias-repeated-index:
  - do:
      esql.query:
        body:
          query: 'FROM test-lookup-1 | SORT key | LOOKUP JOIN test-lookup-alias ON key | LIMIT 3'

  - match: {columns.0.name: "key"}
  - match: {columns.0.type: "long"}
  - match: {columns.1.name: "color"}
  - match: {columns.1.type: "keyword"}
  - match: {values.0: [1, "cyan"]}
  - match: {values.1: [2, "yellow"]}

---
fails when alias or pattern resolves to multiple v2:
  - requires:
      capabilities:
        - method: POST
          path: /_query
          parameters: []
          capabilities: [enable_lookup_join_on_remote]
      reason: "checks updated error messages"
  - do:
      esql.query:
        body:
          query: 'FROM test-lookup-1 | LOOKUP JOIN test-lookup-alias-pattern-multiple ON key'
      catch: "bad_request"

  - match: { error.type: "verification_exception" }
  - contains: { error.reason: "Lookup Join requires a single lookup mode index; [test-lookup-alias-pattern-multiple] resolves to multiple indices" }

---
alias-pattern-single:
  - do:
      esql.query:
        body:
          query: 'FROM test | SORT key | LOOKUP JOIN test-lookup-alias-pattern-single ON key | LIMIT 3'

  - match: {columns.0.name: "key"}
  - match: {columns.0.type: "long"}
  - match: {columns.1.name: "color"}
  - match: {columns.1.type: "keyword"}
  - match: {values.0: [1, "cyan"]}
  - match: {values.1: [2, "yellow"]}
