[BUG] dedup after sort returns 500 on a number field whose type differs across indices
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 with the dedup sort branch in opensearch/src/main/java/org/opensearch/sql/opensearch/request/AggregateAnalyzer.java at the linked lines, then trace how field mappings determine the sort request. Reproduce with the supplied byte and long mappings and compare the pushed top_hits sort with a request that sets numeric_type. Done when the sorted dedup query returns the expected smallest f value across indices with differing numeric types.
Written by the indexing model from the issue text.
Description
Query Information
PPL Command/Query:
source=dedup_* | sort f | dedup g
f is mapped as byte in dedup_byte and as long in dedup_long. Both indices hold a document with g set to x.
Expected Result: One row for g = x, holding the document with the smallest f, which is 1. The same query with plugins.calcite.pushdown.enabled set to false returns that row, and each index queried alone returns 200 with its own row.
Actual Result: HTTP 500.
Pushdown on:
{
"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
}
Pushdown off:
{
"schema": [
{
"name": "f",
"type": "bigint"
},
{
"name": "g",
"type": "string"
}
],
"datarows": [
[
1,
"x"
]
],
"total": 1,
"size": 1
}
The error body does not name the cause. The same request sent as a raw _search fails with class java.lang.Integer cannot be cast to class java.lang.Long, shown under Issue Summary.
Dataset Information
Dataset/Schema Type
- OpenTelemetry (OTEL)
- Simple Schema for Observability (SS4O)
- Open Cybersecurity Schema Framework (OCSF)
- Custom (details below)
Index Mapping
PUT dedup_byte { "mappings": { "properties": { "f": { "type": "byte" }, "g": { "type": "keyword" } } } }
PUT dedup_long { "mappings": { "properties": { "f": { "type": "long" }, "g": { "type": "keyword" } } } }
Sample Data
POST dedup_byte/_doc/1?refresh=true { "f": 1, "g": "x" }
POST dedup_long/_doc/1?refresh=true { "f": 3, "g": "x" }
Bug Description
Issue Summary:
A pushed dedup after sort becomes a composite aggregation on the dedup field with a top_hits of size 1 inside it, sorted on the sort field. The sort is added in the dedup branch of AggregateAnalyzer as SortBuilders.fieldSort(key.field()).order(order).missing(missing). The composite groups on g, and the top_hits sorts on f with no numeric_type.
Pushed request from _explain
{
"from": 0,
"size": 0,
"timeout": "1m",
"aggregations": {
"composite_buckets": {
"composite": {
"size": 10000,
"sources": [
{
"g": {
"terms": {
"field": "g",
"missing_bucket": false,
"order": "asc"
}
}
}
]
},
"aggregations": {
"$f1": {
"top_hits": {
"from": 0,
"size": 1,
"version": false,
"seq_no_primary_term": false,
"explain": false,
"fields": [
{
"field": "g"
},
{
"field": "f"
}
],
"sort": [
{
"f": {
"order": "asc",
"missing": "_first"
}
}
]
}
}
}
}
}
}
Each shard returns its sort values in its own field's Java type, an Integer from the byte index and a Long from the long index. When both shards return hits for the same bucket, InternalTopHits.reduce merges them with TopDocs.merge, which compares the two values directly and throws. Sent as a raw _search to dedup_*?error_trace=true, the pushed request returns 500 with class java.lang.Integer cannot be cast to class java.lang.Long.
Stack from the raw _search
java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long
at java.base/java.lang.Long.compareTo(Long.java:73)
at org.apache.lucene.search.FieldComparator.compareValues(FieldComparator.java:115)
at org.apache.lucene.search.TopDocs$MergeSortQueue.lessThan(TopDocs.java:188)
at org.apache.lucene.search.TopDocs$MergeSortQueue.lessThan(TopDocs.java:126)
at org.apache.lucene.util.PriorityQueue.upHeap(PriorityQueue.java:308)
at org.apache.lucene.util.PriorityQueue.add(PriorityQueue.java:192)
at org.apache.lucene.search.TopDocs.mergeAux(TopDocs.java:306)
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.bucket.composite.InternalComposite.reduceBucket(InternalComposite.java:280)
at org.opensearch.search.aggregations.bucket.composite.InternalComposite.reduce(InternalComposite.java:229)
A plain sort f on dedup_* returns 200 with both rows, so the failure is specific to the top_hits sort. The failure does not depend on which type PPL picks when it merges the index mappings, since the request names only the field.
f in index 0 |
f in index 1 |
Merged types seen | sort f | dedup g |
sort - f | dedup g |
dedup g with no sort |
|---|---|---|---|---|---|
byte |
long |
tinyint, bigint |
500 | 500 | 200 |
integer |
long |
int |
500 | 500 | 200 |
integer |
double |
double, int |
500 | 500 | 200 |
float |
double |
float |
500 | 500 | 200 |
short |
integer |
smallint, int |
200 | 200 | 200 |
Each row used two indices built like the repro, with two documents each, g set to x and y, and f values of 1 and 2 for byte, 3 and 4 for short, 5 and 6 for integer, 7 and 8 for long, 1.5 and 2.5 for float, and 3.25 and 4.75 for double. Each pair ran under three sets of index names. The merged type depends on the index names, so the column lists every type seen. Every index queried alone returned 200 for sort f | dedup g. The short and integer row passes because both shards return the same Java type for the sort value.
Setting numeric_type on the sort makes the raw request pass. With "numeric_type": "long" and with "numeric_type": "double", the raw top_hits returned 200 for byte with long, integer with long, integer with double and float with double. Which of the two the plugin should set depends on the field's types.
Steps to Reproduce:
curl -s -XPUT "localhost:9200/dedup_byte" -H 'Content-Type: application/json' \
-d '{"mappings":{"properties":{"f":{"type":"byte"},"g":{"type":"keyword"}}}}'
curl -s -XPUT "localhost:9200/dedup_long" -H 'Content-Type: application/json' \
-d '{"mappings":{"properties":{"f":{"type":"long"},"g":{"type":"keyword"}}}}'
curl -s -XPOST "localhost:9200/dedup_byte/_doc/1?refresh=true" -H 'Content-Type: application/json' -d '{"f":1,"g":"x"}'
curl -s -XPOST "localhost:9200/dedup_long/_doc/1?refresh=true" -H 'Content-Type: application/json' -d '{"f":3,"g":"x"}'
# 1. dedup after sort returns HTTP 500
curl -s -XPOST "localhost:9200/_plugins/_ppl" -H 'Content-Type: application/json' \
-d '{"query":"source=dedup_* | sort f | dedup g"}' -w '\nHTTP %{http_code}\n'
# 2. dedup without the sort returns HTTP 200
curl -s -XPOST "localhost:9200/_plugins/_ppl" -H 'Content-Type: application/json' \
-d '{"query":"source=dedup_* | dedup g"}' -w '\nHTTP %{http_code}\n'
# 3. With pushdown off, query 1 returns HTTP 200 with the row for f = 1
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=dedup_* | sort f | dedup g"}' -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:
Any dedup after a sort on a rollover or wildcard source fails once one index maps the sort field to a type whose sort values have a different Java type, such as an integer field that a later index maps as long.
Environment Information
OpenSearch Version: 3.10.0-SNAPSHOT, sql plugin built from main at f36c99f84.
Additional Details: plugins.calcite.enabled is true, plugins.calcite.pushdown.enabled is true and plugins.calcite.fallback.allowed is false, all defaults. With plugins.calcite.enabled set to false the query returns the same body as pushdown off.
- 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