[BUG] stats max and stats min return 500 when a field is keyword or ip and boolean or a number in another
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
Research direction
Start by reproducing the supplied logs_a/logs_b mappings and stats max(f) and stats min(f) queries through the /_plugins/_ppl endpoint; compare with pushdown disabled. Then read the top_hits pushdown for non-numeric max and min introduced in #4281 and trace how the merged field type affects the aggregation. Done means mixed mappings no longer produce an uninformative HTTP 500: the query returns a valid maximum or minimum, or a 400 that identifies the conflicting types.
Written by the indexing model from the issue text.
Description
Query Information
PPL Command/Query:
source=logs_* | stats max(f)
f is keyword in logs_a and boolean in logs_b, which is what a rollover mapping change leaves behind.
Expected Result: the maximum over both indices, or a 400 naming the conflicting types.
Actual Result: HTTP 500, and details does not name the cause. stats min(f) fails the same way.
{
"error": {
"context": {
"stage": "executing",
"stage_description": "Running the query"
},
"reason": "java.sql.SQLException: exception while executing query: Failed to fetch data from the index: the background task failed or interrupted.\n Inner error: Failed to execute phase [fetch], ",
"details": "java.sql.SQLException: exception while executing query: Failed to fetch data from the index: the background task failed or interrupted.\n Inner error: Failed to execute phase [fetch], ",
"location": ["while running the query"],
"code": "UNKNOWN",
"type": "RuntimeException"
},
"status": 500
}
The cause shows up in the node log and in caused_by.reason on a raw _search that sends the DSL from _explain to logs_*. Adding ?error_trace=true to that search gives the stack below.
java.lang.ClassCastException: class java.lang.Integer cannot be cast to class org.apache.lucene.util.BytesRef
at org.apache.lucene.search.comparators.TermOrdValComparator.compareValues(TermOrdValComparator.java:53)
...
at org.apache.lucene.search.TopDocs.merge(TopDocs.java:261)
at org.opensearch.search.aggregations.metrics.InternalTopHits.reduce(InternalTopHits.java:150)
at org.opensearch.search.aggregations.InternalAggregations.reduce(InternalAggregations.java:188)
at org.opensearch.search.aggregations.InternalAggregations.topLevelReduce(InternalAggregations.java:135)
at org.opensearch.action.search.SearchPhaseController.reduceAggs(SearchPhaseController.java:561)
Dataset Information
Dataset/Schema Type
- OpenTelemetry (OTEL)
- Simple Schema for Observability (SS4O)
- Open Cybersecurity Schema Framework (OCSF)
- Custom (details below)
Index Mapping
PUT logs_a { "mappings": { "properties": { "host": { "type": "keyword" }, "f": { "type": "keyword" } } } }
PUT logs_b { "mappings": { "properties": { "host": { "type": "keyword" }, "f": { "type": "boolean" } } } }
Sample Data
POST logs_a/_doc/1?refresh=true { "host": "a", "f": "300" }
POST logs_b/_doc/1?refresh=true { "host": "b", "f": true }
Bug Description
Issue Summary:
PPL pushes max and min on a field that is not a number or date type, such as string, ip or boolean, as a top_hits of size 1 sorted on f. Over a wildcard, PPL picks between the native aggregation and top_hits from the merged type of f, while each shard sorts f by its own mapping. A keyword or ip shard returns a BytesRef sort value, and a boolean shard returns an Integer. InternalTopHits.reduce merges them with one Lucene comparator, which throws a ClassCastException.
Each index alone returns 200. The merged type is whichever index the mapping merge reads last, see #5752. It changes with the index names, so every pair below ran under 10 different name prefixes on fresh indices. In the last column both indices hold a document with host set to s, and the query is where host = 's' | stats max(f) by host.
f in one index |
f in the other |
merged type | stats max(f) |
stats min(f) |
stats max(f) by host, one host in both indices |
|---|---|---|---|---|---|
keyword |
boolean |
boolean |
500 | 500 | 500 |
keyword |
boolean |
string |
500 | 500 | 500 |
ip |
boolean |
boolean |
500 | 500 | 500 |
ip |
boolean |
ip |
500 | 500 | 500 |
keyword |
long |
string |
500 | 500 | 500 |
keyword |
long |
bigint |
200, "300" dropped |
200, "300" dropped |
200, "300" dropped |
ip |
long |
ip |
500 | 500 | 500 |
boolean |
long |
boolean |
500 | 500 | 500 |
keyword |
ip |
string or ip |
200, wrong for some values | 200, wrong for some values | 200, wrong for some values |
integer |
long |
int or bigint |
200 | 200 | 200 |
The values were "300" for keyword, 5 for the integer types, true for boolean and 10.0.0.1 for ip.
Every 500 in the table is this bug. The keyword and boolean pair and the ip and boolean pair fail under both merged types. The other failing pairs fail only when the merged type is string, ip or boolean, because a numeric or date merged type pushes a native max or min with no sort to merge. keyword against ip never fails, since both sides sort as BytesRef. The 200 cells marked wrong or dropped are other defects, and the dropped value is #5687.
With plugins.calcite.pushdown.enabled set to false, the same query returns 200, since no top_hits reaches OpenSearch. With Calcite off, the v2 engine rejects both functions on logs_* under boolean with a 400.
#5825 is the same TopDocs.merge failure reached through a pushed sort, with the top-level SearchPhaseController.sortDocs in place of InternalTopHits.reduce. The sort here sits inside an aggregation, so it does not go through the sort pushdown that #5825 covers.
Steps to Reproduce:
curl -s -XPUT "localhost:9200/logs_a" -H 'Content-Type: application/json' \
-d '{"mappings":{"properties":{"host":{"type":"keyword"},"f":{"type":"keyword"}}}}'
curl -s -XPUT "localhost:9200/logs_b" -H 'Content-Type: application/json' \
-d '{"mappings":{"properties":{"host":{"type":"keyword"},"f":{"type":"boolean"}}}}'
curl -s -XPOST "localhost:9200/logs_a/_doc/1?refresh=true" -H 'Content-Type: application/json' -d '{"host":"a","f":"300"}'
curl -s -XPOST "localhost:9200/logs_b/_doc/1?refresh=true" -H 'Content-Type: application/json' -d '{"host":"b","f":true}'
# 1. Returns HTTP 500, whichever type the merge picks for f
curl -s -XPOST "localhost:9200/_plugins/_ppl" -H 'Content-Type: application/json' \
-d '{"query":"source=logs_* | stats max(f)"}' -w '\nHTTP %{http_code}\n'
# 2. stats min returns the same 500
curl -s -XPOST "localhost:9200/_plugins/_ppl" -H 'Content-Type: application/json' \
-d '{"query":"source=logs_* | stats min(f)"}' -w '\nHTTP %{http_code}\n'
# 3. Each index alone returns 200
curl -s -XPOST "localhost:9200/_plugins/_ppl" -H 'Content-Type: application/json' \
-d '{"query":"source=logs_a | stats max(f)"}' -w '\nHTTP %{http_code}\n'
curl -s -XPOST "localhost:9200/_plugins/_ppl" -H 'Content-Type: application/json' \
-d '{"query":"source=logs_b | stats max(f)"}' -w '\nHTTP %{http_code}\n'
# 4. A top_hits sorted on f, the shape PPL pushes, sent as a raw search with no plugin,
# also returns 500, with the ClassCastException in caused_by
curl -s -XPOST "localhost:9200/logs_*/_search?error_trace=true" -H 'Content-Type: application/json' \
-d '{"size":0,"aggs":{"m":{"top_hits":{"size":1,"sort":[{"f":{"order":"desc"}}]}}}}' -w '\nHTTP %{http_code}\n'
# 5. With pushdown off, queries 1 and 2 return 200
curl -s -XPUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' \
-d '{"transient":{"plugins.calcite.pushdown.enabled":false}}'
curl -s -XPOST "localhost:9200/_plugins/_ppl" -H 'Content-Type: application/json' \
-d '{"query":"source=logs_* | stats max(f)"}' -w '\nHTTP %{http_code}\n'
curl -s -XPOST "localhost:9200/_plugins/_ppl" -H 'Content-Type: application/json' \
-d '{"query":"source=logs_* | stats min(f)"}' -w '\nHTTP %{http_code}\n'
# Restore the default
curl -s -XPUT "localhost:9200/_cluster/settings" -H 'Content-Type: application/json' \
-d '{"transient":{"plugins.calcite.pushdown.enabled":null}}'
Impact:
After a rollover changes the mapping of a field, stats max and stats min on that field over the data stream or its wildcard return 500, and the response does not say which field or which types conflict.
Environment Information
OpenSearch Version: main at 803ed31c8, 3.10.0-SNAPSHOT, one node, one shard per index. plugins.calcite.enabled is true, plugins.calcite.pushdown.enabled is true and plugins.calcite.fallback.allowed is false.
Additional Details:
- #4281 introduced the
top_hitspushdown formaxandminon non-numeric fields.
- Dominant language
- Java
- Stars
- 176
- Forks
- 237
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 34
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from opensearch-project/sql
-
bug untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
opensearch-project/sql#5990 ·
Maintainers usually reply within 1 day
-
bug untriaged
Difficulty 1/5 Under an hour Newbie friendliness 85/100
opensearch-project/sql#5983 ·
Maintainers usually reply within 1 day
-
[BUG] PromQL queries fail with InvalidTypeIdException when metric has a label named "type"Possibly taken @nagendramohan claimed this 107 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
opensearch-project/sql#5684 · 4 comments ·
Maintainers usually reply within 1 day
-
[DOC] Calcite settings documentation missing examplesPossibly taken @AzazelSensei claimed this 64 days ago. Opendocumentation PPL
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
opensearch-project/sql#4806 · 1 comment ·
Maintainers usually reply within 1 day
-
calcite enhancement Meta performance PPL untriaged
Difficulty 5/5 Over a week Newbie friendliness 8/100
opensearch-project/sql#5991 ·
Maintainers usually reply within 1 day
All issues in opensearch-project/sql
Similar issues
-
Document the isv:HDFGroup Linux package repositories and signing keyPossibly taken @brtnfld claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
checkstyle/checkstyle#23174 ·
Maintainers usually reply within 1 day
-
bug follow-up lane:backlog
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Sanne/incus-spawn#2620 · 1 comment ·
Maintainers usually reply within 1 day
-
[BUG] Execute API reports every non-400 error, including 404 "not found", with reason "System Error"Possibly taken A pull request linked to this issue is open or already merged. Openuntriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
opensearch-project/ml-commons#5296 · 1 comment ·
Maintainers usually reply within 1 day