Skip to content

[BUG] Aggregation pushdown lost for sum(int_field + literal): ClickBench q30 regressed from ~700ms to 2min timeout in 3.8 #5823

Closed

Description

@penghuo

What is the bug?

SUM(<narrow-int field> + <literal>) no longer gets its aggregation pushed down to OpenSearch. The Calcite planner falls back to a full source scan plus coordinator-side aggregation.

Any PPL query of the shape stats sum(int_field + N) against a byte/short/integer/long field is affected — the ClickBench query is just an extreme case (90 such aggregates, q30.ppl), which is why it goes from sub-second to unbounded.

How can one reproduce the bug?

plugins.calcite.enabled is true by default in both versions, so no setting change is needed.

Using the ClickBench hits index already in this repo (integ-test/src/test/resources/clickbench/), where ResolutionWidth is mapped as short:

source=hits | stats sum(ResolutionWidth), sum(ResolutionWidth+1), ..., sum(ResolutionWidth+89)

(verbatim query: integ-test/src/test/resources/clickbench/queries/q30.ppl)

A minimal repro is just:

source=hits | stats sum(ResolutionWidth + 1)

Then explain it and look at the PushDownContext.

What is the expected behavior?

The aggregation should be pushed into OpenSearch. PPLAggregateConvertRule is supposed to rewrite SUM(field + k) → SUM(field) + k * COUNT(field), which collapses all 90 aggregates into two pushable metric aggregations.

This is exactly what 3.6/3.7 did — note "size":0 and only sum + value_count:

3.6 / 3.7 explain (abridged)
calcite:
  logical: |
    LogicalSystemLimit(fetch=[10000], type=[QUERY_SIZE_LIMIT])
      LogicalAggregate(group=[{}], sum(ResolutionWidth)=[SUM($0)], sum(ResolutionWidth+1)=[SUM($1)], ... , sum(ResolutionWidth+89)=[SUM($89)])
        LogicalProject(ResolutionWidth=[$80], $f90=[+($80, 1)], $f91=[+($80, 2)], ... , $f178=[+($80, 89)])
          CalciteLogicalIndexScan(table=[[OpenSearch, hits]])
  physical: |
    EnumerableCalc(... sum(ResolutionWidth)=[$t0], sum(ResolutionWidth+1)=[$t3], ...)
      CalciteEnumerableIndexScan(table=[[OpenSearch, hits]],
        PushDownContext=[[
          AGGREGATION->...group={},sum(ResolutionWidth)=SUM($0),sum(ResolutionWidth+1)_COUNT=COUNT($0),
          LIMIT->10000],
        OpenSearchRequestBuilder(sourceBuilder={
          "from":0,"size":0,"timeout":"1m",
          "aggregations":{
            "sum(ResolutionWidth)":{"sum":{"field":"ResolutionWidth"}},
            "sum(ResolutionWidth+1)_COUNT":{"value_count":{"field":"ResolutionWidth"}}}},
        ...)])

What is the actual behavior?

From 3.8 onward the rule stops firing. Nothing but a _source filter is pushed down — no size cap, no aggregation — so every matching document is streamed to the coordinator and 90 CHECKED_LONG_SUM accumulators run in EnumerableAggregate:

3.8 / 3.9 / main explain (abridged)
calcite:
  logical: |
    LogicalSystemLimit(fetch=[10000], type=[QUERY_SIZE_LIMIT])
      LogicalAggregate(group=[{}], sum(ResolutionWidth)=[CHECKED_LONG_SUM($0)], ... , sum(ResolutionWidth+89)=[CHECKED_LONG_SUM($89)])
        LogicalProject(ResolutionWidth=[$80], $f90=[+(CAST($80):BIGINT, 1)], ... , $f178=[+(CAST($80):BIGINT, 89)])
          CalciteLogicalIndexScan(table=[[OpenSearch, hits]])
  physical: |
    EnumerableLimit(fetch=[10000])
      EnumerableAggregate(group=[{}], sum(ResolutionWidth)=[CHECKED_LONG_SUM($0)], ... )   <-- 90 sums in the coordinator
        EnumerableCalc(...)
          CalciteEnumerableIndexScan(table=[[OpenSearch, hits]],
            PushDownContext=[[PROJECT->[ResolutionWidth]],                                 <-- nothing else pushed down
            OpenSearchRequestBuilder(sourceBuilder={
              "from":0,"timeout":"1m","_source":{"includes":["ResolutionWidth"]}},          <-- no "size":0, full scan
            requestedTotalSize=2147483647, ...)])

Root cause

PPLAggregateConvertRule's match predicate requires the arithmetic operand to be a bare input ref:

https://github.com/opensearch-project/sql/blob/main/core/src/main/java/org/opensearch/sql/calcite/plan/rule/PPLAggregateConvertRule.java#L273-L281

private static boolean isCallWithLiteral(RexNode node) {
  if (CONVERTABLE_FUNCTIONS.contains(node.getKind()) && node instanceof RexCall call) {
    RexNode arg1 = call.getOperands().getFirst();
    RexNode arg2 = call.getOperands().getLast();
    return (arg1.getKind() == SqlKind.INPUT_REF && arg2.getKind() == SqlKind.LITERAL)
        || (arg1.getKind() == SqlKind.LITERAL && arg2.getKind() == SqlKind.INPUT_REF);
  }
  return false;
}

#5603 "Widen narrow integer operands in PPL +/-/ to prevent overflow"* (commit 4985a39ef, released in 3.8) now wraps narrow-integer operands in a widening cast, so the operand is CAST($80):BIGINT rather than INPUT_REF. The predicate returns false, the rewrite never happens, the 90 computed aggregates remain, and aggregation pushdown is lost entirely.

I confirmed 4985a39ef alone flips the plan: git show <rev>:integ-test/src/test/resources/expectedOutput/calcite/clickbench/q30.yaml is the fast plan at 4985a39ef^ and the slow plan at 4985a39ef.

CHECKED_LONG_SUM (#5612, de95ffbe9) is not the blocker — its getKind() is SqlKind.SUM, and AggregateAnalyzer already pushes it down (AggregateAnalyzer.java#L488). The widening cast is the sole cause.

getFieldAndLiteral has the same assumption and casts the operand directly to RexInputRef, so it needs updating alongside the predicate.

Why CI didn't catch it

Two independent gaps:

  1. CalcitePPLClickBenchIT does assert the explain plan for every ClickBench query against integ-test/src/test/resources/expectedOutput/calcite/clickbench/q{i}.yaml (PPLClickBenchIT.java#L78) — but q30.yaml was regenerated in Widen narrow integer operands in PPL +/-/* to prevent overflow #5603, so the regressed plan became the new expectation. The diff shows CalciteEnumerableIndexScan losing its AGGREGATION pushdown and gaining an EnumerableAggregate parent; that's easy to miss in a large expected-output churn.

  2. PPLAggregateConvertRuleTest can't see it. Its only positive case feeds the rule a BIGINT RexInputRef (PPLAggregateConvertRuleTest.java#L81), which needs no widening cast, so it never exercises the narrow-int path that real PPL produces.

Related

What is your host/environment?

  • OpenSearch 3.8, 3.9, and current main (reproduced from the checked-in expected explain plans on branches 3.6–3.9)
  • SQL/PPL plugin, Calcite engine (plugins.calcite.enabled=true, the default)

Activity

  1. added
    bug
    PPL
    performance
    calcite
    pushdown
    on Sep 29, 2026
  2. self-assigned this
    on Sep 29, 2026
  3. added a commit that references this issue on Oct 12, 2026
    caf44c4
  4. moved this from Not Started to Done in PPL Trackeron Oct 13, 2026
  5. added a commit that references this issue on Oct 14, 2026
    0026279
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

PPLbugcalciteperformancepushdown

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions