A predicate on a multi-valued field matches only when it is pushed down

Open
#5,834 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
java, sql
Domain
backend, databases

Research direction

Start by reproducing the four queries at the /_plugins/_ppl endpoint and comparing their _explain output, including with pushdown and Calcite fallback disabled. Trace how scalar comparisons receive multi-valued fields in the v2 and Calcite paths, then compare the existing pushdown behavior. Done means predicates such as =, !=, in, and like have consistent multi-value semantics regardless of pipeline position, without silent row loss or type errors.

Written by the indexing model from the issue text.

Description

bug calcite PPL
What is wrong

An OpenSearch field can hold several values; PPL types it as a scalar. A term query on a multi-valued field has ANY semantics in the index — it matches when any value equals the term — but the in-engine comparison compares the whole list against the literal, so it matches nothing.

Whether a predicate is evaluated in the index or in the engine depends on whether it can be pushed down, which depends on where it sits in the pipeline. So the same predicate over the same data returns different rows depending on what precedes it.

Reproduce

Default settings. Nothing unusual in the mapping.

PUT /arr
{"settings":{"number_of_shards":1},
 "mappings":{"properties":{"s":{"type":"keyword"},
                           "k":{"type":"keyword"},
                           "n":{"type":"integer"}}}}

POST /arr/_bulk?refresh=true
{"index":{"_id":"1"}}
{"s":"multi","k":["a","b"],"n":[80,443]}
{"index":{"_id":"2"}}
{"s":"single","k":"c","n":22}
POST /_plugins/_ppl   {"query": "source=arr | where k = 'a'"}
POST /_plugins/_ppl   {"query": "source=arr | head 2 | where k = 'a'"}
query rows returned filter reached the DSL
source=arr | where k = 'a' 1 yes
source=arr | head 2 | where k = 'a' 0 no
source=arr | where n = 80 1 yes
source=arr | head 2 | where n = 80 0 no

Document 1 satisfies the predicate in all four. A head in front of the filter changes the answer. The last column is from _explain: FILTER-> is present in the scan's pushdown context for the first and third, absent for the second and fourth.

plugins.calcite.pushdown.enabled=false reaches the same gap without a head.

Both engines

plugins.calcite.enabled=false (v2) gives the same answers, so this is not a Calcite regression — it is long-standing behaviour in both engines.

The mechanism

With plugins.calcite.fallback.allowed=false, the numeric case stops being silent:

POST /_plugins/_ppl  {"query":"source=arr | head 2 | where n = 80"}

HTTP 500, code UNKNOWN
java.sql.SQLException: exception while executing query:
class java.util.ArrayList cannot be cast to class java.lang.Integer

The type the engine works with is a scalar while the value it receives is a list:

describe arr                                 -> n: int,  k: string
source=arr | eval t=typeof(n) | fields t     -> INT
source=arr | fields k, n                     -> [["a","b"], [80,443]]

Generated code casts the value to the declared scalar type and fails. One array value anywhere in the column is enough: head 2 | where n = 22 fails the same way, even though the document it should match holds a scalar 22.

With fallback allowed the failure is swallowed and the query is answered by the v2 engine instead, which is why the default configuration shows a missing row rather than an error. A keyword column does not throw at all — it just does not match.

Why it matters
  • The wrong answer is silent in the default configuration: fewer rows, no warning.
  • It applies to every multi-valued field, not to a particular mapping type.
  • head, sort and any other operator that blocks pushdown moves a predicate from one semantics to the other without the query saying anything about it.
Directions
  1. Represent a multi-valued field as an ARRAY in the type system and lower = on one to a quantified comparison (= SOME (...)), which Calcite supports natively. Correct, but it touches the declared type of every field that can hold several values.
  2. Keep the scalar type and give the comparison a multi-value-aware form that the pushdown recognizer and the in-engine path agree on. Narrower, and it can be introduced per operator (=, !=, in, like).

Either way the fix belongs to the comparison, not to any one field type, and every field benefits.

Context

Found while reviewing #5830, which adds flat_object support. A flat_object leaf holding several values behaves the same way, since it is just another multi-valued column; that PR inherits this rather than introducing it, and says so.

Environment

OpenSearch 3.9.0-SNAPSHOT, opensearch-sql 3.9.0.0-SNAPSHOT. Reproduced on 3.10.0-SNAPSHOT as well.

Dominant language
Java
Stars
176
Forks
237
Avg merge
2d 12h
Merged PRs (30d)
34

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from opensearch-project/sql

All issues in opensearch-project/sql

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.