---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- requires:
    cluster_features:
    - "gte_v8.13.0"
    reason: "fixed in 8.13"
    test_runner_features: "warnings"
- do:
    indices.create:
      index: "test"
      body:
        settings:
          number_of_shards: 5
        mappings:
          properties:
            always_null:
              type: "long"
            sometimes_null:
              type: "long"
            never_null:
              type: "long"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    bulk:
      index: "test"
      refresh: true
      body:
      - index: {}
      - sometimes_null: 1
        never_null: 1
      - index: {}
      - sometimes_null: 1
        never_null: 1
      - index: {}
      - never_null: 2
      - index: {}
      - never_null: 2
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
group on null:
- do:
    esql.query:
      body:
        query: "FROM test | STATS med=median(never_null) BY always_null | LIMIT 1"
        columnar: true
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    columns.0.name: "med"
- match:
    columns.0.type: "double"
- match:
    columns.1.name: "always_null"
- match:
    columns.1.type: "long"
- length:
    values: 2
- match:
    values.0:
    - 1.5
- match:
    values.1:
    - null
---
group on null, long:
- do:
    esql.query:
      body:
        query: "FROM test | STATS med=median(sometimes_null) BY always_null, never_null\
          \ | SORT always_null, never_null | LIMIT 10"
        columnar: true
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    columns.0.name: "med"
- match:
    columns.0.type: "double"
- match:
    columns.1.name: "always_null"
- match:
    columns.1.type: "long"
- match:
    columns.2.name: "never_null"
- match:
    columns.2.type: "long"
- length:
    values: 3
- match:
    values.0:
    - 1.0
    - null
- match:
    values.1:
    - null
    - null
- match:
    values.2:
    - 1
    - 2
---
agg on null:
- do:
    esql.query:
      body:
        query: "FROM test | STATS med=median(always_null) | LIMIT 1"
        columnar: true
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    columns.0.name: "med"
- match:
    columns.0.type: "double"
- length:
    values: 1
- match:
    values.0:
    - null
---
agg on missing:
- do:
    catch: "bad_request"
    esql.query:
      body:
        query: "FROM test | STATS med=median(missing) | LIMIT 1"
        columnar: true
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
group on missing:
- do:
    catch: "bad_request"
    esql.query:
      body:
        query: "FROM test | STATS med=median(never_null) BY missing | LIMIT 1"
        columnar: true
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
agg on half missing:
- do:
    indices.create:
      index: "test2"
      body:
        settings:
          number_of_shards: 5
        mappings:
          properties:
            always_null:
              type: "long"
            sometimes_null:
              type: "long"
            never_null:
              type: "long"
            missing:
              type: "long"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    esql.query:
      body:
        query: "FROM test* | STATS med=median(missing) | LIMIT 1"
        columnar: true
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    columns.0.name: "med"
- match:
    columns.0.type: "double"
- length:
    values: 1
- match:
    values.0:
    - null
---
group on half missing:
- do:
    indices.create:
      index: "test2"
      body:
        settings:
          number_of_shards: 5
        mappings:
          properties:
            always_null:
              type: "long"
            sometimes_null:
              type: "long"
            never_null:
              type: "long"
            missing:
              type: "long"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    esql.query:
      body:
        query: "FROM test,test2 | STATS med=median(never_null) BY missing | LIMIT\
          \ 1"
        columnar: true
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    columns.0.name: "med"
- match:
    columns.0.type: "double"
- match:
    columns.1.name: "missing"
- match:
    columns.1.type: "long"
- length:
    values: 2
- match:
    values.0:
    - 1.5
- match:
    values.1:
    - null
