Repository navigation
Description
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:
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:
-
CalcitePPLClickBenchITdoes assert the explain plan for every ClickBench query againstinteg-test/src/test/resources/expectedOutput/calcite/clickbench/q{i}.yaml(PPLClickBenchIT.java#L78) — butq30.yamlwas regenerated in Widen narrow integer operands in PPL +/-/* to prevent overflow #5603, so the regressed plan became the new expectation. The diff showsCalciteEnumerableIndexScanlosing itsAGGREGATIONpushdown and gaining anEnumerableAggregateparent; that's easy to miss in a large expected-output churn. -
PPLAggregateConvertRuleTestcan't see it. Its only positive case feeds the rule aBIGINTRexInputRef(PPLAggregateConvertRuleTest.java#L81), which needs no widening cast, so it never exercises the narrow-int path that real PPL produces.
Related
- Regressed by Widen narrow integer operands in PPL +/-/* to prevent overflow #5603 (
4985a39ef) - Adjacent change (not the cause): [BugFix] Detect BIGINT overflow in SUM and fix wrong AVG on the Calcite engine #5612 (
de95ffbe9)
What is your host/environment?
- OpenSearch 3.8, 3.9, and current
main(reproduced from the checked-in expected explain plans on branches3.6–3.9) - SQL/PPL plugin, Calcite engine (
plugins.calcite.enabled=true, the default)
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
Restore SUM(narrow_int + literal) aggregation pushdown
Restore SUM(narrow_int + literal) aggregation pushdown (opensearch-pr…