[BUG] sort returns 500 when a field is keyword or ip in one index and a number, date or boolean in another

未关闭
#5,825 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
java, sql
领域
backend, databases

调研方向

Reproduce the failure with the provided curl commands, then read SearchPhaseController.createSort and AbstractCalciteIndexScan.pushDownSort to trace how mixed field types reach the sort. Check OpenSearchDescribeIndexRequest.getFieldTypes and BackgroundSearchScanner.getCurrentResponse for the type information and error propagation. Done means mixed keyword and numeric/date fields no longer produce an uninformative HTTP 500.

由索引模型根据 Issue 内容生成。

描述

bug PPL

Query Information

PPL Command/Query:

source=logs_* | sort f | fields host

f is keyword in logs_a and long in logs_b, which is what a rollover mapping change leaves behind.

Expected Result: both documents sorted by f, or a 400 naming the conflicting types.

Actual Result: HTTP 500, and details does not name the cause.

{
  "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 over the same indices. Adding ?error_trace=true to that search gives the stack below.

java.lang.ClassCastException: class java.lang.Long 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.action.search.SearchPhaseController.mergeTopDocs(SearchPhaseController.java:240)
	at org.opensearch.action.search.SearchPhaseController.sortDocs(SearchPhaseController.java:187)
	at org.opensearch.action.search.SearchPhaseController.reducedQueryPhase(SearchPhaseController.java:534)
	at org.opensearch.action.search.QueryPhaseResultConsumer.reduce(QueryPhaseResultConsumer.java:187)

Every command that pushes a sort on f returns 500 when one side is keyword or ip and the other a number, date or boolean. The first two sort by term bytes and the others sort numerically.

f in one index f in the other merged type of f sort f sort - f sort f | reverse sort f | head 1 self join on f
keyword byte string or tinyint 500 500 500 500 500
keyword integer int or string 500 500 500 500 500
keyword long string or bigint 500 500 500 500 500
keyword date timestamp or string 500 500 500 500 500
keyword boolean string or boolean 500 500 500 500 500
ip long ip or bigint 500 500 500 500 500
ip date ip or timestamp 500 500 500 500 500
ip boolean ip or boolean 500 500 500 500 500
keyword text string 200 200 200 200 200
byte integer tinyint or int 200 200 200 200 200
byte long tinyint or bigint 200 200 200 200 200
integer long int or bigint 200 200 200 200 200
byte date tinyint or timestamp 200 200 200 200 200, 1 or 2 of 2 rows
integer date int or timestamp 200 200 200 200 200, 1 or 2 of 2 rows
long date bigint or timestamp 200 200 200 200 200, 1 or 2 of 2 rows

The merged type is whichever index the mapping merge reads last, see #5752. It differed between runs on the same node, and a cell shows both types where it did. Every failing pair failed under every merged type seen, so the failure does not depend on which type wins. A boolean field fails only against keyword or ip, with java.lang.Integer in place of java.lang.Long in the ClassCastException. join fails because each side of the EnumerableMergeJoin pushes its own sort on f, with a SearchPhaseExecutionException whose reason is Error occurred in OpenSearch engine: and nothing after the colon.

Other pairs of the same sort kind also return 200, such as boolean against a number or date, and long against double. The join cells marked 1 or 2 of 2 are a separate defect. Under a numeric merged type the date document's f reads as null, so that document cannot join to itself.

One more pair returns 200 with a wrong result, and it reproduces with a raw _search and no plugin. keyword against ip merges one page correctly but not the next. A search_after past the keyword value a fails on the ip shard with 'a' is not an IP string literal., and that shard is dropped from the 200, so the second page returns 2 rows where 5 remain.

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": "long" } } } }

Sample Data

POST logs_a/_doc/1?refresh=true  { "host": "a", "f": "300" }
POST logs_b/_doc/1?refresh=true  { "host": "b", "f": 5 }

Each index alone sorts fine, and source=logs_* | fields host, f returns 200, so the merge is not what fails.

Bug Description

Issue Summary

OpenSearch core cannot merge sorted hits across shards when the sort field sorts by term bytes on one shard, as keyword and ip do, and numerically on another, as numbers, date and boolean do. SearchPhaseController.createSort takes the first shard's SortField and widens it only when every shard is numeric, and its own TODO: should we check the compatibility between types marks the gap. With a term shard in the mix, TermOrdValComparator receives a Long or an Integer and throws. A raw _search with "sort":[{"f":"asc"}] over the same two indices returns the same 500 without the plugin.

PPL pushes the sort into that request without checking that f has one type across the indices. AbstractCalciteIndexScan.pushDownSort guards only binary fields, at line 397, and then emits a bare fieldSort(f). The per-index types that would reveal the conflict are already gone by then. OpenSearchDescribeIndexRequest.getFieldTypes merges them into one map and keeps only the result, next to its own TODO possible collision if two indices have fields with the same name and different mappings.

details does not name the cause because the core exception carries an empty message, visible as "reason": "" on the raw search. BackgroundSearchScanner.getCurrentResponse builds the PPL error from e.getMessage() at line 141, and that text stops at Failed to execute phase [fetch], . The ClassCastException survives only in the Java cause chain, wrapped in turn by ExecutionException, NonFallbackCalciteException, SQLException and RuntimeException, so a user sees neither the field nor the types.

Pushdown off returns both rows in order

The same query with plugins.calcite.pushdown.enabled=false returns HTTP 200.

{
  "schema": [
    {
      "name": "host",
      "type": "string"
    }
  ],
  "datarows": [
    [
      "b"
    ],
    [
      "a"
    ]
  ],
  "total": 2,
  "size": 2
}

With pushdown off the sort runs in memory over the merged f, which was bigint on this run, so b with 5 sorts ahead of a with 300. Adding f to the projection returns ["b", 5] then ["a", 300] with f typed bigint.

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":"long"}}}}'
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":5}'

# 1. Returns HTTP 500
curl -s -XPOST "localhost:9200/_plugins/_ppl" -H 'Content-Type: application/json' \
  -d '{"query":"source=logs_* | sort f | fields host"}' -w '\nHTTP %{http_code}\n'

# 2. The same sort as a raw search, no plugin, also returns 500 with the ClassCastException
curl -s -XPOST "localhost:9200/logs_*/_search?error_trace=true" -H 'Content-Type: application/json' \
  -d '{"sort":[{"f":"asc"}]}' -w '\nHTTP %{http_code}\n'

# 3. With pushdown off, query 1 returns 200 with both rows
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_* | sort f | fields host"}' -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

In an index-pattern deployment, one rolled-over index that maps a field as a number, date or boolean where older indices map it as keyword or ip breaks every sort on that field. That includes head after sort and join on the field, with a 500 that names neither the field nor the conflict.

Environment Information

OpenSearch Version: main at 67aed0ee8, 3.10.0-SNAPSHOT, one node, with the defaults plugins.calcite.enabled=true, plugins.calcite.pushdown.enabled=true and plugins.calcite.fallback.allowed=false.

Additional Details:

  • #5811 is the same kind of conflict in composite aggregation, through stats ... by, top and dedup.
  • A text field against any other type can fail silently. When the other type wins the merge, as with integer, long, date or ip, PPL pushes the sort, it fails on the text shard because text has no fielddata, and OpenSearch drops that shard from the result. Every command in the table then returns 200 with only the other index's row. When text wins, PPL does not push the sort and both rows come back. With pushdown off both rows always come back.
主要语言
Java
星标
176
派生
237
平均合并
2 天 12 小时
30 天内合并 PR
34

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

opensearch-project/sql 的其他 Issue

查看 opensearch-project/sql 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。