---
setup:
  - do:
      indices.create:
        index:  test
        body:
          settings:
            number_of_shards: 1
          mappings:
            properties:
              data:
                type: long
              data_d:
                type: double
              count:
                type: long
              count_d:
                type: double
              time:
                type: long
              color:
                type: keyword
              text:
                type: text

  - do:
      cluster.health: # older versions of ESQL don't wait for the nodes to become available.
        wait_for_no_initializing_shards: true
        wait_for_events: languid

  - do:
      bulk:
        index: "test"
        refresh: true
        body:
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275187, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275188, "color": "blue", "text": "bb blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275189, "color": "green", "text": "gg green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275190, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275191, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275192, "color": "blue", "text": "bb blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275193, "color": "green", "text": "gg green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275194, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275195, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275196, "color": "blue", "text": "bb blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275197, "color": "green", "text": "gg green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275198, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275199, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275200, "color": "blue", "text": "bb blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275201, "color": "green", "text": "gg green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275202, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275203, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275204, "color": "blue", "text": "bb blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275205, "color": "green", "text": "gg green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275206, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275207, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275208, "color": "blue", "text": "bb blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275209, "color": "green", "text": "gg green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275210, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275211, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275212, "color": "blue", "text": "bb blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275213, "color": "green", "text": "gg green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275214, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275215, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275216, "color": "blue", "text": "bb blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275217, "color": "green", "text": "gg green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275218, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275219, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275220, "color": "blue", "text": "bb blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275221, "color": "green", "text": "gg green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275222, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275223, "color": "red", "text": "rr red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275224, "color": "blue", "text": "bb blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275225, "color": "green", "text": "gg green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275226, "color": "red", "text": "rr red" }

---
avg 8.14 or after:
  - requires:
      cluster_features: ["gte_v8.14.0"]
      reason: "avg changed starting 8.14"

  - do:
      esql.query:
        body:
          query: 'FROM test | STATS AVG(data) | LIMIT 1'
          columnar: true
          profile: true

  - match: {columns.0.name: "AVG(data)"}
  - match: {columns.0.type: "double"}
  - match: {values.0.0: 1.5}
  - match: {profile.drivers.0.operators.0.operator: /ExchangeSourceOperator|LuceneSourceOperator.+/}
  - gte: {profile.drivers.0.took_nanos: 0}
  - gte: {profile.drivers.0.cpu_nanos: 0}
  - gte: {profile.drivers.1.took_nanos: 0}
  - gte: {profile.drivers.1.cpu_nanos: 0}
# It's hard to assert much about these because they don't come back in any particular order.

---
documents found:
  - requires:
      test_runner_features: [capabilities, contains]
      capabilities:
        - method: POST
          path: /_query
          parameters: []
          capabilities: [documents_found_and_values_loaded]
      reason: "checks for documents_found and values_loaded"

  - do:
      esql.query:
        body:
          query: 'FROM test | LIMIT 1'
          profile: true

  - length: {profile.drivers: 3}
  - match: {profile.drivers.0.operators.0.operator: /ExchangeSourceOperator|LuceneSourceOperator.+/}
  - gte: {profile.drivers.0.documents_found: 0}
  - gte: {profile.drivers.0.values_loaded: 0}
  - gte: {profile.drivers.1.documents_found: 0}
  - gte: {profile.drivers.1.values_loaded: 0}
  - gte: {profile.drivers.2.documents_found: 0}
  - gte: {profile.drivers.2.values_loaded: 0}
