---
setup:
  - requires:
      cluster_features: ["gte_v8.11.0"]
      reason: "ESQL is available in 8.11+"
      test_runner_features: allowed_warnings_regex
  - do:
      indices.create:
        index:  test
        body:
          settings:
            number_of_shards: 5
          mappings:
            properties:
              data:
                type: long
              data_d:
                type: double
              count:
                type: long
              count_d:
                type: double
              time:
                type: long
              color:
                type: keyword
  - do:
      bulk:
        index: "test"
        refresh: true
        body:
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275187, "color": "red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275188, "color": "blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275189, "color": "green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275190, "color": "red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275191, "color": "red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275192, "color": "blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275193, "color": "green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275194, "color": "red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275195, "color": "red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275196, "color": "blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275197, "color": "green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275198, "color": "red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275199, "color": "red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275200, "color": "blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275201, "color": "green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275202, "color": "red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275203, "color": "red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275204, "color": "blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275205, "color": "green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275206, "color": "red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275207, "color": "red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275208, "color": "blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275209, "color": "green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275210, "color": "red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275211, "color": "red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275212, "color": "blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275213, "color": "green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275214, "color": "red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275215, "color": "red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275216, "color": "blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275217, "color": "green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275218, "color": "red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275219, "color": "red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275220, "color": "blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275221, "color": "green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275222, "color": "red" }
          - { "index": { } }
          - { "data": 1, "count": 40, "data_d": 1, "count_d": 40, "time": 1674835275223, "color": "red" }
          - { "index": { } }
          - { "data": 2, "count": 42, "data_d": 2, "count_d": 42, "time": 1674835275224, "color": "blue" }
          - { "index": { } }
          - { "data": 1, "count": 44, "data_d": 1, "count_d": 44, "time": 1674835275225, "color": "green" }
          - { "index": { } }
          - { "data": 2, "count": 46, "data_d": 2, "count_d": 46, "time": 1674835275226, "color": "red" }
  - do:
      indices.create:
        index:  test_range
        body:
          settings:
            number_of_shards: 5
          mappings:
            properties:
              date_1:
                type: date
                format: "dd-MM-yyyy-MM-dd HH:mm Z"
              date_2:
                type: date
  - do:
      bulk:
        index: "test_range"
        refresh: true
        body:
          - { "index": { } }
          - { "date_1": "12-01-1981-01-12 15:00 +0000", "date_2": "2000" }
          - { "index": { } }
          - { "date_1": "31-12-1999-12-31 23:59 +0300" }
          - { "index": { } }
          - { "date_1": "31-12-1999-12-31 23:59 -0300" }

---
"Test From":
  - do:
      allowed_warnings_regex:
        - "No limit defined, adding default limit of \\[.*\\]"
      esql.query:
        body:
          query: 'from test'

  - match: {columns.0.name: "color"}
  - match: {columns.0.type: "keyword"}
  - match: {columns.1.name: "count"}
  - match: {columns.1.type: "long"}
  - match: {columns.2.name: "count_d"}
  - match: {columns.2.type: "double"}
  - match: {columns.3.name: "data"}
  - match: {columns.3.type: "long"}
  - match: {columns.4.name: "data_d"}
  - match: {columns.4.type: "double"}
  - match: {columns.5.name: "time"}
  - match: {columns.5.type: "long"}
  - length: {values: 40}

---
"Test From Sort Limit":
  - do:
      esql.query:
        body:
          query: 'from test | sort count | limit 1'

  - match: {columns.1.name: "count"}
  - match: {columns.1.type: "long"}
  - match: {values.0.1: 40}

---
basic:
  - do:
      esql.query:
        body:
          query: 'from test | keep data | sort data | limit 2'
          columnar: true

  - match: {columns.0.name: "data"}
  - match: {columns.0.type: "long"}
  - match: {values.0: [1, 1]}

---
basic with 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 | keep data | sort data | limit 2'
          columnar: true

  - gte:   {documents_found: 2}   # we might early terminate, so the most we can assume is >=2
  - gte:   {values_loaded: 2}     # same as above
  - match: {columns.0.name: "data"}
  - match: {columns.0.type: "long"}
  - match: {values.0: [1, 1]}

---
FROM EVAL SORT LIMIT:
  - do:
      esql.query:
        body:
          query: 'from test | eval x = count + 7 | sort x | limit 1'

  - match: {columns.0.name: "color"}
  - match: {columns.1.name: "count"}
  - match: {columns.2.name: "count_d"}
  - match: {columns.3.name: "data"}
  - match: {columns.4.name: "data_d"}
  - match: {columns.5.name: "time"}
  - match: {columns.6.name: "x"}
  - match: {values.0.6: 47}
  - length: {values: 1}

---
FROM EVAL SORT LIMIT with 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 | eval x = count + 7 | sort x | limit 1'

  - match: {documents_found: 40}
  # We can't be sure quite how many values we'll load. It's at least
  # one per document in the index. And one per top document. But we
  # might load more values because we run in more threads.
  - gte: {values_loaded: 45}
  - match: {columns.0.name: "color"}
  - match: {columns.1.name: "count"}
  - match: {columns.2.name: "count_d"}
  - match: {columns.3.name: "data"}
  - match: {columns.4.name: "data_d"}
  - match: {columns.5.name: "time"}
  - match: {columns.6.name: "x"}
  - match: {values.0.6: 47}
  - length: {values: 1}

---
"Test Eval With Multiple Expressions":
  - do:
      esql.query:
        body:
          query: 'from test | sort time | eval x = data + 1, y = data_d + count, z = x + y | keep data, x, y, z, time | limit 2'

  - match: {columns.0.name: "data"}
  - match: {columns.0.type: "long"}
  - match: {columns.1.name: "x"}
  - match: {columns.1.type: "long"}
  - match: {columns.2.name: "y"}
  - match: {columns.2.type: "double"}
  - match: {columns.3.name: "z"}
  - match: {columns.3.type: "double"}
  - match: {columns.4.name: "time"}
  - match: {columns.4.type: "long"}
  - length: {values: 2}
  - length: {values.0: 5}
  - match: {values.0.0: 1}
  - match: {values.0.1: 2}
  - match: {values.0.2: 41.0}
  - match: {values.0.3: 43.0}
  - length: {values.1: 5}
  - match: {values.1.0: 2}
  - match: {values.1.1: 3}
  - match: {values.1.2: 44.0}
  - match: {values.1.3: 47.0}

---
"Test keep After TopN":
  - do:
      esql.query:
        body:
          query: 'from test | sort time | limit 2 | keep count'
          columnar: true

  - length: {columns: 1}
  - match: {columns.0.name: "count"}
  - match: {columns.0.type: "long"}
  - match: {values.0: [40, 42]}

---
"Test keep After TopN Desc":
  - do:
      esql.query:
        body:
          query: 'from test | sort time desc | limit 2 | keep count'
          columnar: true

  - length: {columns: 1}
  - match: {columns.0.name: "count"}
  - match: {columns.0.type: "long"}
  - match: {values.0: [46, 44]}

---
"Test TopN keep Eval":
  - do:
      esql.query:
        body:
          query: 'from test | sort time | limit 2 | keep count | eval x = count + 1'
          columnar: true

  - length: {columns: 2}
  - match: {columns.0.name: "count"}
  - match: {columns.0.type: "long"}
  - match: {columns.1.name: "x"}
  - match: {columns.1.type: "long"}
  - length: {values: 2}
  - match: {values.0: [40, 42]}
  - match: {values.1: [41, 43]}

---
"Test TopN keep Eval Project":
  - do:
      esql.query:
        body:
          query: 'from test | sort time | limit 2 | keep count | eval x = count + 1 | keep x'
          columnar: true

  - length: {columns: 1}
  - match: {columns.0.name: "x"}
  - match: {columns.0.type: "long"}
  - length: {values: 1}
  - match: {values.0: [41, 43]}

---
"Test Multi Limit Project":
  - do:
      esql.query:
        body:
          query: 'from test | limit 10 | sort time | limit 1'

  - length: {columns: 6}
  - length: {values: 1}

---
"Test Input Params":
  - do:
      allowed_warnings_regex:
        - "No limit defined, adding default limit of \\[.*\\]"
      esql.query:
        body:
          query: 'row a = ? | eval b = ?, c = 1 + ?'
          params: ["foo", 15, 10]

  - length: {columns: 3}
  - match: {columns.0.name: "a"}
  - match: {columns.0.type: "keyword"}
  - match: {columns.1.name: "b"}
  - match: {columns.1.type: "integer"}
  - match: {columns.2.name: "c"}
  - match: {columns.2.type: "integer"}
  - length: {values: 1}
  - match: {values.0: ["foo", 15, 11]}


  - do:
      allowed_warnings_regex:
        - "No limit defined, adding default limit of \\[.*\\]"
      esql.query:
        body:
          query: 'from test | where color == ? and count == ? and time == ? | keep data, count, color'
          params: ["green", 44, 1674835275193]

  - length: {columns: 3}
  - match: {columns.0.name: "data"}
  - match: {columns.0.type: "long"}
  - match: {columns.1.name: "count"}
  - match: {columns.1.type: "long"}
  - match: {columns.2.name: "color"}
  - match: {columns.2.type: "keyword"}
  - length: {values: 1}
  - match: {values.0: [1, 44, "green"]}

---
"Test Unnamed Input Params":
  - do:
      esql.query:
        body:
          query: 'from test | eval x = ?, y = ?, z = ?, t = ?, u = ?, v = ? | keep x, y, z, t, u, v | limit 3'
          params: ["1", 2.0, null, true, 123, 1674835275193]

  - length: {columns: 6}
  - match: {columns.0.name: "x"}
  - match: {columns.0.type: "keyword"}
  - match: {columns.1.name: "y"}
  - match: {columns.1.type: "double"}
  - match: {columns.2.name: "z"}
  - match: {columns.2.type: "null"}
  - match: {columns.3.name: "t"}
  - match: {columns.3.type: "boolean"}
  - match: {columns.4.name: "u"}
  - match: {columns.4.type: "integer"}
  - match: {columns.5.name: "v"}
  - match: {columns.5.type: "long"}
  - length: {values: 3}
  - match: {values.0: ["1",2.0,null,true,123,1674835275193]}
  - match: {values.1: ["1",2.0,null,true,123,1674835275193]}
  - match: {values.2: ["1",2.0,null,true,123,1674835275193]}

---
"Test Unnamed, Multivalued Input Params":
  - requires:
      test_runner_features: [ capabilities ]
      capabilities:
        - method: POST
          path: /_query
          parameters: [ ]
          capabilities: [ query_params_multi_values ]
      reason: "multivalued parameters"
  - do:
      esql.query:
        body:
          query: 'from test | eval x = ?, y = ?, z = ?, t = ?, u = ? | keep x, y, z, t, u | limit 3'
          params: [["1", "2"], [2.0, 3.0], [true, false], [123, 456], [1674835275193, 1674835275193]]

  - length: {columns: 5}
  - match: {columns.0.name: "x"}
  - match: {columns.0.type: "keyword"}
  - match: {columns.1.name: "y"}
  - match: {columns.1.type: "double"}
  - match: {columns.2.name: "z"}
  - match: {columns.2.type: "boolean"}
  - match: {columns.3.name: "t"}
  - match: {columns.3.type: "integer"}
  - match: {columns.4.name: "u"}
  - match: {columns.4.type: "long"}
  - length: {values: 3}
  - match: {values.0: [["1", "2"], [2.0, 3.0], [true, false], [123, 456], [1674835275193, 1674835275193]]}
  - match: {values.1: [["1", "2"], [2.0, 3.0], [true, false], [123, 456], [1674835275193, 1674835275193]]}
  - match: {values.2: [["1", "2"], [2.0, 3.0], [true, false], [123, 456], [1674835275193, 1674835275193]]}

---
"Test Unnamed Input Params Also For Limit And Sample":
  - requires:
        test_runner_features: [ capabilities ]
        capabilities:
          - method: POST
            path: /_query
            parameters: [ ]
            capabilities: [ parameter_for_limit, parameter_for_sample ]
        reason: "named or positional parameters"
  - do:
      esql.query:
        body:
          # The "| sort time" is to work around https://github.com/elastic/elasticsearch/issues/120272
          # TODO: remove it when the issue is fixed
          query: 'from test | sort time | sample ? | eval x = ?, y = ?, z = ?, t = ?, u = ?, v = ? | keep x, y, z, t, u, v | limit ?'
          params: [0.999999999999, "1", 2.0, null, true, 123, 1674835275193, 3]

  - length: {columns: 6}
  - match: {columns.0.name: "x"}
  - match: {columns.0.type: "keyword"}
  - match: {columns.1.name: "y"}
  - match: {columns.1.type: "double"}
  - match: {columns.2.name: "z"}
  - match: {columns.2.type: "null"}
  - match: {columns.3.name: "t"}
  - match: {columns.3.type: "boolean"}
  - match: {columns.4.name: "u"}
  - match: {columns.4.type: "integer"}
  - match: {columns.5.name: "v"}
  - match: {columns.5.type: "long"}
  - length: {values: 3}
  - match: {values.0: ["1",2.0,null,true,123,1674835275193]}
  - match: {values.1: ["1",2.0,null,true,123,1674835275193]}
  - match: {values.2: ["1",2.0,null,true,123,1674835275193]}


---
"Test Named Input Params":
  - requires:
      test_runner_features: [ capabilities ]
      capabilities:
        - method: POST
          path: /_query
          parameters: [ ]
          capabilities: [ parameter_for_limit, parameter_for_sample ]
      reason: "named or positional parameters"

  - do:
      esql.query:
        body:
          # The "| sort time" is to work around https://github.com/elastic/elasticsearch/issues/120272
          # TODO: remove it when the issue is fixed
          query: 'from test | sort time | sample ? | eval x = ?, y = ?, z = ?, t = ?, u = ?, v = ? | keep x, y, z, t, u, v | limit ?'
          params: [{"s": 0.99999999999}, {"n1" : "1"}, {"n2" : 2.0}, {"n3" : null}, {"n4" : true}, {"n5" : 123}, {"n6": 1674835275193}, {"l": 3}]

  - length: {columns: 6}
  - match: {columns.0.name: "x"}
  - match: {columns.0.type: "keyword"}
  - match: {columns.1.name: "y"}
  - match: {columns.1.type: "double"}
  - match: {columns.2.name: "z"}
  - match: {columns.2.type: "null"}
  - match: {columns.3.name: "t"}
  - match: {columns.3.type: "boolean"}
  - match: {columns.4.name: "u"}
  - match: {columns.4.type: "integer"}
  - match: {columns.5.name: "v"}
  - match: {columns.5.type: "long"}
  - length: {values: 3}
  - match: {values.0: ["1",2.0,null,true,123,1674835275193]}
  - match: {values.1: ["1",2.0,null,true,123,1674835275193]}
  - match: {values.2: ["1",2.0,null,true,123,1674835275193]}

---
"Test Named, Multivalued Input Params":
  - requires:
      test_runner_features: [ capabilities ]
      capabilities:
        - method: POST
          path: /_query
          parameters: [ ]
          capabilities: [ parameter_for_limit, parameter_for_sample, query_params_multi_values ]
      reason: "named or positional parameters, multivalued parameters"

  - do:
      esql.query:
        body:
          query: 'from test | eval x = ?n1, y = ?n2, z = ?n3, t = ?n4, u = ?n5 | keep x, y, z, t, u | limit ?l'
          params: [{"n1" : ["1", "2"]}, {"n2" : [2.0, 3.0]}, {"n3" : [true, false]}, {"n4" : [123, 456]}, {"n5": [1674835275193, -1674835275193]}, {"l": 3}]

  - length: {columns: 5}
  - match: {columns.0.name: "x"}
  - match: {columns.0.type: "keyword"}
  - match: {columns.1.name: "y"}
  - match: {columns.1.type: "double"}
  - match: {columns.2.name: "z"}
  - match: {columns.2.type: "boolean"}
  - match: {columns.3.name: "t"}
  - match: {columns.3.type: "integer"}
  - match: {columns.4.name: "u"}
  - match: {columns.4.type: "long"}
  - length: {values: 3}
  - match: {values.0: [["1", "2"], [2.0, 3.0], [true, false] ,[123, 456], [1674835275193, -1674835275193]]}
  - match: {values.1: [["1", "2"], [2.0, 3.0], [true, false] ,[123, 456], [1674835275193, -1674835275193]]}
  - match: {values.2: [["1", "2"], [2.0, 3.0], [true, false] ,[123, 456], [1674835275193, -1674835275193]]}

---
"Test Interval in Input Params":
  - requires:
      test_runner_features: [ capabilities ]
      capabilities:
        - method: POST
          path: /_query
          parameters: [ ]
          capabilities: [ cast_string_literal_to_temporal_amount ]
      reason: "interval in parameters"

  - do:
      allowed_warnings_regex:
        - "No limit defined, adding default limit of \\[.*\\]"
      esql.query:
        body:
          query: 'row x = ?n1::datetime | eval y = x - ?n2::date_period, z = x + ?n3::time_duration'
          params: [{"n1" : "2024-08-06"}, {"n2" : "3 days"}, {"n3" : "3 hours"}]

  - length: {columns: 3}
  - match: {columns.0.name: "x"}
  - match: {columns.0.type: "date"}
  - match: {columns.1.name: "y"}
  - match: {columns.1.type: "date"}
  - match: {columns.2.name: "z"}
  - match: {columns.2.type: "date"}
  - length: {values: 1}
  - match: {values.0: ["2024-08-06T00:00:00.000Z","2024-08-03T00:00:00.000Z","2024-08-06T03:00:00.000Z"]}

---
"Test Named Input Params For Field Names":
  - requires:
      test_runner_features: [ capabilities ]
      capabilities:
        - method: POST
          path: /_query
          parameters: [ ]
          capabilities: [ parameter_for_limit, parameter_for_sample ]
      reason: "named or positional parameters for field names"

  - do:
      esql.query:
        body:
          # The "| sort time" is to work around https://github.com/elastic/elasticsearch/issues/120272
          # TODO: remove it when the issue is fixed
          query: 'from test | sort time | sample ?s | stats x = count(?f1), y = sum(?f2) by ?f3 | sort ?f3 | keep ?f3, x, y  | limit ?l'
          params: [{"s": 0.99999999999}, {"f1" : {"identifier" : "time"}}, {"f2" : { "identifier" : "count" }}, {"f3" : { "identifier" : "color"}}, {"l": 3}]

  - length: {columns: 3}
  - match: {columns.0.name: "color"}
  - match: {columns.0.type: "keyword"}
  - match: {columns.1.name: "x"}
  - match: {columns.1.type: "long"}
  - match: {columns.2.name: "y" }
  - match: {columns.2.type: "long" }
  - length: {values: 3}
  - match: {values.0: ["blue",10,420]}
  - match: {values.1: ["green",10,440]}
  - match: {values.2: ["red",20,860]}


---
"Test wrong LIMIT parameter":
  - requires:
      test_runner_features: [ capabilities ]
      capabilities:
        - method: POST
          path: /_query
          parameters: [ ]
          capabilities: [ normalized_limit_error_message ]
      reason: "named or positional parameters for field names"

  - do:
      catch: "/value of \\[limit \\?l\\] must be a non negative integer, found value \\[\\?l\\] type \\[keyword\\]/"
      esql.query:
        body:
          query: 'from test | limit ?l'
          params: [{"l": "foo"}]



---
version is not allowed:
  - requires:
      cluster_features: ["gte_v8.14.0"]
      reason: version allowed in 8.13.latest
  - do:
      catch: /unknown field \[version\]/
      esql.query:
        body:
          query: 'from test'
          version: cat

---
"Test Internal Range":
  - do:
      allowed_warnings_regex:
        - "No limit defined, adding default limit of \\[.*\\]"
      esql.query:
        body:
          query: 'from test_range | where date_1 > "1990" and date_1 < "2000" or date_2 >= "2000" | sort date_1'

  - length: {columns: 2}
  - match: {columns.0.name: "date_1"}
  - match: {columns.0.type: "date"}
  - match: {columns.1.name: "date_2"}
  - match: {columns.1.type: "date"}
  - length: {values: 2}
  - match: {values.0: ["1981-01-12T15:00:00.000Z","2000-01-01T00:00:00.000Z"]}
  - match: {values.1: ["1999-12-31T20:59:00.000Z", null]}


---
"No columns left after drop":
  - requires:
      test_runner_features: [capabilities, allowed_warnings_regex]
      capabilities:
        - method: POST
          path: /_query
          parameters: []
          capabilities: [fix_no_columns]
      reason: "plans witn no columns only recently supported"

  - do:
      allowed_warnings_regex:
        - "No limit defined, adding default limit of \\[.*\\]"
      esql.query:
        body:
          query: 'from test_range | keep date_1 | drop date_1 | limit 2'

  - length: {columns: 0}
  - length: {values: 2}
  - length: {values.0: 0 }
  - length: {values.1: 0 }


---
"Drop all columns after stats":
  - requires:
      test_runner_features: [capabilities, allowed_warnings_regex]
      capabilities:
        - method: POST
          path: /_query
          parameters: []
          capabilities: [fix_no_columns]
      reason: "plans witn no columns only recently supported"

  - do:
      allowed_warnings_regex:
        - "No limit defined, adding default limit of \\[.*\\]"
      esql.query:
        body:
          query: 'from test | stats by color | keep color | drop color'

  - length: {columns: 0}
  - length: {values: 3}
  - length: {values.0: 0 }
  - length: {values.1: 0 }
  - length: {values.2: 0 }

---
"Drop all columns after eval where false":
  - requires:
      test_runner_features: [capabilities, allowed_warnings_regex]
      capabilities:
        - method: POST
          path: /_query
          parameters: []
          capabilities: [fix_no_columns]
      reason: "plans witn no columns only recently supported"

  - do:
      allowed_warnings_regex:
        - "No limit defined, adding default limit of \\[.*\\]"
      esql.query:
        body:
          query: 'from test | keep color | eval c = 1 | drop c, color | where false'

  - length: {columns: 0}
  - length: {values: 0}

---
"Drop all columns with inline stats ":
  - requires:
      test_runner_features: [capabilities, allowed_warnings_regex]
      capabilities:
        - method: POST
          path: /_query
          parameters: []
          capabilities: [fix_no_columns]
      reason: "plans witn no columns only recently supported"

  - do:
      allowed_warnings_regex:
        - "No limit defined, adding default limit of \\[.*\\]"
      esql.query:
        body:
          query: 'from test | keep color | drop color | eval x = 1 | inline stats a = count(), b = max(x) | limit 3'

  - length: {columns: 3}
  - match: { columns.0.name: "x" }
  - match: { columns.1.name: "a" }
  - match: { columns.2.name: "b" }
  - length: {values: 3}
  - match: {values.0: [1, 40, 1]}
  - match: {values.1: [1, 40, 1]}
  - match: {values.2: [1, 40, 1]}

---
"Drop all columns with inline stats and by":
  - requires:
      test_runner_features: [capabilities, allowed_warnings_regex]
      capabilities:
        - method: POST
          path: /_query
          parameters: []
          capabilities: [fix_no_columns]
      reason: "plans witn no columns only recently supported"

  - do:
      allowed_warnings_regex:
        - "No limit defined, adding default limit of \\[.*\\]"
      esql.query:
        body:
          query: 'from test | keep color | drop color | eval x = 1, y = 1 | inline stats a = count(), b = max(x) by y | limit 3'

  - length: {columns: 4}
  - match: { columns.0.name: "x" }
  - match: { columns.1.name: "a" }
  - match: { columns.2.name: "b" }
  - match: { columns.3.name: "y" }
  - length: {values: 3}
  - match: {values.0: [1, 40, 1, 1]}
  - match: {values.1: [1, 40, 1, 1]}
  - match: {values.2: [1, 40, 1, 1]}

---
"Drop all columns with mutiple indices":
  - requires:
      test_runner_features: [ capabilities, allowed_warnings_regex ]
      capabilities:
        - method: POST
          path: /_query
          parameters: [ ]
          capabilities: [ fix_no_columns ]
      reason: "plans witn no columns only recently supported"

  - do:
      allowed_warnings_regex:
        - "No limit defined, adding default limit of \\[.*\\]"
      esql.query:
        body:
          query: 'from test* | keep color | drop color | eval x = 1, y = 1 | inline stats a = count(), b = max(x) by y | limit 3'
  - length: { columns: 4 }
  - match: { columns.0.name: "x" }
  - match: { columns.1.name: "a" }
  - match: { columns.2.name: "b" }
  - match: { columns.3.name: "y" }
  - length: { values: 3 }
  - match: {values.0: [1, 43, 1, 1]}
  - match: {values.1: [1, 43, 1, 1]}
  - match: {values.2: [1, 43, 1, 1]}

---
"Drop all columns with fork":
  - requires:
      test_runner_features: [ capabilities, allowed_warnings_regex ]
      capabilities:
        - method: POST
          path: /_query
          parameters: [ ]
          capabilities: [ fix_no_columns ]
      reason: "plans witn no columns only recently supported"

  - do:
      allowed_warnings_regex:
        - "No limit defined, adding default limit of \\[.*\\]"
      esql.query:
        body:
          query: 'from test | keep color | drop color | fork (stats c=count()) (stats c=count())'

  - length: { columns: 2 }
  - match: { columns.0.name: "c" }
  - match: { columns.1.name: "_fork" }
  - length: { values: 2 }
  - match: {values.0.0: 40}
  - match: {values.1.0: 40}


---
"Drop all columns with fork drop all":
  - requires:
      test_runner_features: [ capabilities, allowed_warnings_regex ]
      capabilities:
        - method: POST
          path: /_query
          parameters: [ ]
          capabilities: [ fix_no_columns ]
      reason: "plans witn no columns only recently supported"
  - do:
      allowed_warnings_regex:
        - "No limit defined, adding default limit of \\[.*\\]"
      esql.query:
        body:
          query: 'from test | keep color | drop color | fork (stats c=count()) (stats c=count()) | drop _fork*, c'

  - length: { columns: 0 }
  - length: { values: 2 }


---
"Drop all columns with fork drop all count":
  - requires:
      test_runner_features: [ capabilities, allowed_warnings_regex ]
      capabilities:
        - method: POST
          path: /_query
          parameters: [ ]
          capabilities: [ fix_no_columns ]
      reason: "plans witn no columns only recently supported"
  - do:
      allowed_warnings_regex:
        - "No limit defined, adding default limit of \\[.*\\]"
      esql.query:
        body:
          query: 'from test | keep color | drop color | fork (stats c=count()) (stats c=count()) | drop _fork*, c | stats count(*)'

  - length: { columns: 1 }
  - length: { values: 1 }
  - match: {values.0.0: 2}
