Skip to content

[FEATURE] Calcite Engine Framework: Pushdown scripts #3379

Closed
3 of 3 issues completed
Closed

Description

@LantaoJin

Is your feature request related to a problem?
Framework work of the development feature of Calcite Engine.

What solution would you like?

  1. Enable Script Pushdown
  2. Eval expression could be pushdown by script pushdown feature.

Adding all duplicated open issues to its sub-issues list.

Sub-issues

Sub-issues

3 of 3 Issues completed
Loading 3 sub-issues

Activity

  1. added
    enhancement
    on Mar 5, 2025
  2. added
    calcite
    and removed on Mar 5, 2025
  3. qianheng-aws commented on May 20, 2025

    @qianheng-aws
    Collaborator

    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.

  4. model-collapse commented on May 20, 2025

    @model-collapse

    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.

  5. added
    pushdown
    on May 30, 2025
  6. moved this from Not Started to In progress in PPL Trackeron Jun 25, 2025
  7. dai-chen commented on Jun 26, 2025

    @dai-chen
    Collaborator

    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.

    • PoC code: dai-chen@357e622
    • Pros:
      • 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.

    {
      "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_expression script 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 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)

  8. qianheng-aws commented on Jul 9, 2025

    @qianheng-aws
    Collaborator

    Inspired by this issue, #3842

    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

  9. songkant-aws commented on Jul 10, 2025

    @songkant-aws
    Collaborator

    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

  10. moved this from In progress to Done in PPL Trackeron Jul 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

calciteenhancementpushdown

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions