You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am voting to a ultimate solution which can adaptively select between option1 and option2 according to the estimation of processing time. For short term, I think we might ASAP adding Kyro to core.
Hi team, I’d like to share some findings from my exploration to provide you more ideas as we evaluate different strategies. cc: @penghuo
Summary
Option 3: Reusing the V2 Script Engine – Leverages the existing opensearch_query_expression engine by converting Calcite expressions into the V2 expression tree.
Option 4: Registering a New Script Language – Introduces a expression script language that safely executes allowlisted expressions, fully abstracting the execution details from users.
In the near term, Option 4 offers a practical path to unblock integration by avoiding the security concerns of Options 1 and 2 and minimizing effort. It also decouples Calcite’s centralized planning from execution—important given ongoing debate over whether Calcite’s physical plan should be a universal model and intrude into the execution layer. Looking ahead, Option 5 could evolve into a more formal, engine-agnostic expression layer that bridges planning and execution without tight dependence on Calcite.
Option 3: Reusing the V2 Script Engine
Instead of introducing a new script engine, we can reuse the existing one (opensearch_query_expression) from the V2 engine. In this approach, the script engine is treated as a standalone script language (similar to Painless), independent of both the V2 engine which is being deprecated and Calcite engine which is focused on logical optimization.
Lightweight integration: Simplifies Calcite library integration with CLI and Spark outside OpenSearch.
Backward compatibility: Maintains full or partial compatibility with older plugin versions (1.x through 3.1).
Code reuse: Leverages existing V2 filter pushdown logic to avoid to reimplement from scratch.
Cons:
Incomplete coverage: For unsupported filter expressions, we’ll need to either extend current script engine or handle them via post-processing. [Need to confirm how many not supported]
Semantic mismatches: There may be subtle differences between Calcite’s SqlStdOperatorTable and their counterparts in the V2 engine. [Need to verify if any]
Conversion overhead: While the the initial benchmark shows low latency (~3 ms first, ~1 ms after), performance should be compared against other approaches for large-scale usage.
POST localhost:9200/_plugins/_ppl
{
"query": "source = test | where abs(age) <= 123 and name = 'bob'"
}
[9749ddcd-9c7c-4c34-b71c-c496440b6922] Incoming request source=test | where abs(age) <= *** and name = ***
Convert to V2 expression in [3] ms: and(<=(abs(age), 123), =(name, "bob smith"))
Push down to DSL in [21] ms: {
"bool" : {
"filter" : [
{
"script" : {
"script" : {
"source" : "rO0ABXNyADRvcmcub3BlbnNlYXJj......",
"lang" : "opensearch_query_expression"
},
"boost" : 1.0
}
},
{
"term" : {
"name.keyword" : {
"value" : "bob smith",
"boost" : 1.0
}
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
[2429d733-1564-4d78-91c3-a11e974f25a3] Incoming request source=test | where abs(age) <= *** and name = ***
Convert to V2 expression in [1] ms: and(<=(abs(age), 123), =(name, "bob smith"))
Push down to DSL in [0] ms: ...
Option 4: Registering a new Script Language
This approach introduces a new script language that accepts readable, SQL-like expressions (e.g., abs(age) = 123) for use in OpenSearch DSL queries. Unlike Option 3, which uses serialized expression trees from the V2 engine, or Options 1 and 2, which allow arbitrary Java code, this option keeps both the final executable expression and its execution mechanism (interpretation or compilation) entirely transparent to users. Internally, the engine parses and safely executes a controlled, allowlisted expressions, such as arithmetic, comparisons, and logical operations.
Readable: Accepts human-readable, SQL-style expressions rather than serialized byte code or raw Java code.
Transparent: Users are abstracted from how expressions are compiled or interpreted, and whether they originate from Calcite or V2 expression.
Extensible: Easier to incrementally add support for new functions or even switch to an alternative expression engine in the future.
Cons
Overhead: The script engine must re-parse/analyze expressions at execution time, similar to Painless script, which may introduce performance costs compared to precompiled expressions.
This example demonstrates how the unified_query_expression script language can be used not only for filtering, but also in metric aggregations, group-by expressions, and post-aggregation bucket filtering:
Option 5: Register a New Script Engine for Serialized RexCall [TODO]
Unlike Options 1 and 2, this approach defers Java-side compilation of Calcite expressions. Instead, it introduces a new script engine with limited, allowlisted support for specific RexCall expressions. The goal is to push down serialized RexCall trees directly into OpenSearch, where the custom script engine can interpret or compile them dynamically.
Execution Flow:
Coordinator: RexCall -> Serialized RexCall
Worker: Serialized RexCall -> RexCall -> Linq4j expression -> Interpret or compile to execute
TODO: Verify whether RexCall (and associated RexNode) can be reliably serialized and deserialized. More context in #3812 (comment)
I'm thinking, once we introduce script push down for Calcite engine, it will have both scan with range query and scan with script query in the candidate plans for the above case. We'd better have a mechanism to ensure picking the scan with range query instead of the other one. Maybe adding a COST_FACTOR > 1 for script query builder so its cost will be greater than the primitive DSL query builder. @LantaoJin@songkant-aws
We have figured out Calcite has a builtin JSON serialization mechanism to satisfy Option 5: Register a New Script Engine for Serialized RexCall. See description and implementation in this PR: #3859
Activity
qianheng-aws commentedon May 20, 2025
Solution we have currently, need POC to verify their feasibility.
Option1:
Client: RexNode -> Linq4j Expression -> Compile to Java Code -> Submit Java Code String to Script API
Server: Receive Java Code -> Java Code execution
RPOS: faster when handling big data
CONS: the step of compiling and execution requires more developing effort
Option2:
Client: RexNode -> Linq4j Expression -> Kyro serialization -> Submit byte string to Script API
Server: Receive byte string -> deserialize to Linq4j Expression -> invoke the evaluate API of Linq4j Expression
PROS: faster when handling small data, since saving the step of compile
CONS: introduce the dependency of Kyro both in opensearch core and sql plugin.
model-collapse commentedon May 20, 2025
I am voting to a ultimate solution which can adaptively select between option1 and option2 according to the estimation of processing time. For short term, I think we might ASAP adding Kyro to core.
dai-chen commentedon Jun 26, 2025
Hi team, I’d like to share some findings from my exploration to provide you more ideas as we evaluate different strategies. cc: @penghuo
Summary
opensearch_query_expressionengine by converting Calcite expressions into the V2 expression tree.In the near term, Option 4 offers a practical path to unblock integration by avoiding the security concerns of Options 1 and 2 and minimizing effort. It also decouples Calcite’s centralized planning from execution—important given ongoing debate over whether Calcite’s physical plan should be a universal model and intrude into the execution layer. Looking ahead, Option 5 could evolve into a more formal, engine-agnostic expression layer that bridges planning and execution without tight dependence on Calcite.
Option 3: Reusing the V2 Script Engine
Instead of introducing a new script engine, we can reuse the existing one (
opensearch_query_expression) from the V2 engine. In this approach, the script engine is treated as a standalone script language (similar to Painless), independent of both the V2 engine which is being deprecated and Calcite engine which is focused on logical optimization.SqlStdOperatorTableand their counterparts in the V2 engine. [Need to verify if any]POST localhost:9200/_plugins/_ppl { "query": "source = test | where abs(age) <= 123 and name = 'bob'" } [9749ddcd-9c7c-4c34-b71c-c496440b6922] Incoming request source=test | where abs(age) <= *** and name = *** Convert to V2 expression in [3] ms: and(<=(abs(age), 123), =(name, "bob smith")) Push down to DSL in [21] ms: { "bool" : { "filter" : [ { "script" : { "script" : { "source" : "rO0ABXNyADRvcmcub3BlbnNlYXJj......", "lang" : "opensearch_query_expression" }, "boost" : 1.0 } }, { "term" : { "name.keyword" : { "value" : "bob smith", "boost" : 1.0 } } } ], "adjust_pure_negative" : true, "boost" : 1.0 } } [2429d733-1564-4d78-91c3-a11e974f25a3] Incoming request source=test | where abs(age) <= *** and name = *** Convert to V2 expression in [1] ms: and(<=(abs(age), 123), =(name, "bob smith")) Push down to DSL in [0] ms: ...Option 4: Registering a new Script Language
This approach introduces a new script language that accepts readable, SQL-like expressions (e.g., abs(age) = 123) for use in OpenSearch DSL queries. Unlike Option 3, which uses serialized expression trees from the V2 engine, or Options 1 and 2, which allow arbitrary Java code, this option keeps both the final executable expression and its execution mechanism (interpretation or compilation) entirely transparent to users. Internally, the engine parses and safely executes a controlled, allowlisted expressions, such as arithmetic, comparisons, and logical operations.
{ "bool" : { "filter" : [ { "script" : { "script" : { "source" : "abs(`age`) = 123", "lang" : "unified_query_expression" }, "boost" : 1.0 } }, { "term" : { "name.keyword" : { "value" : "bob smith", "boost" : 1.0 } } } ], "adjust_pure_negative" : true, "boost" : 1.0 } }This example demonstrates how the
unified_query_expressionscript language can be used not only for filtering, but also in metric aggregations, group-by expressions, and post-aggregation bucket filtering:# SELECT SUM(bytes + 10) AS total_bytes # ... # GROUP BY age / 10 # HAVING total_bytes > 10000 { "aggs": { "age_groups": { "terms": { "script": { "source": "`age` / 10", "lang": "unified_query_expression" } }, "aggs": { "total_bytes": { "sum": { "script": { "source": "`bytes` + 10", "lang": "unified_query_expression" } } }, "having_filter": { "bucket_selector": { "buckets_path": { "bytes": "total_bytes" }, "script": { "source": "`bytes` > 10000", "lang": "unified_query_expression" } } } } } } }Option 5: Register a New Script Engine for Serialized RexCall [TODO]
Unlike Options 1 and 2, this approach defers Java-side compilation of Calcite expressions. Instead, it introduces a new script engine with limited, allowlisted support for specific
RexCallexpressions. The goal is to push down serialized RexCall trees directly into OpenSearch, where the custom script engine can interpret or compile them dynamically.Execution Flow:
RexCall-> SerializedRexCallRexCall->RexCall-> Linq4j expression -> Interpret or compile to executeTODO: Verify whether
RexCall(and associatedRexNode) can be reliably serialized and deserialized. More context in #3812 (comment)qianheng-aws commentedon Jul 9, 2025
Inspired by this issue, #3842
I'm thinking, once we introduce script push down for Calcite engine, it will have both
scan with range queryandscan with script queryin the candidate plans for the above case. We'd better have a mechanism to ensure picking thescan with range queryinstead of the other one. Maybe adding aCOST_FACTOR > 1for script query builder so its cost will be greater than the primitive DSL query builder. @LantaoJin @songkant-awssongkant-aws commentedon Jul 10, 2025
We have figured out Calcite has a builtin JSON serialization mechanism to satisfy Option 5: Register a New Script Engine for Serialized RexCall. See description and implementation in this PR: #3859