codewithsruthi opened a new pull request, #20365:
URL: https://github.com/apache/pinot/pull/20365
Fixes #20290.
### Problem
In the multi-stage query engine, `SUM(CASE WHEN <cond> THEN x ELSE 0 END)`
returned `NULL` instead of `0` when no row in the group matched `<cond>`.
Calcite's `PinotAggregateReduceFunctionsRule` reduces `SUM(...)` using
`$SUM0`, and `AggregateCaseToFilterRule` rewrites `$SUM0(CASE WHEN c THEN x
ELSE 0 END)` into `$SUM0(x) FILTER (WHERE c IS TRUE)`. That rewrite depends on
Calcite's `$SUM0` contract: returning `0` (not `NULL`) when no non-null rows
are aggregated.
Previously:
- `AggregationFunctionFactory` instantiated `SumAggregationFunction` for
`SUM0`, which returns `null` when nothing is aggregated under
`nullHandlingEnabled = true` (always enabled in MSE's `AggregateOperator`, and
in leaf stages when `enableNullHandling=true`).
- `PinotAggregateFunctionRewriteRule` rewrote `SUM0` on `INT`/`BIGINT` to
bare `SUMINT`/`SUMLONG`, which also return `null` on empty/all-null input.
### Changes
- **`pinot-core`**: Added `Sum0AggregationFunction` (extending
`SumAggregationFunction`) and wired `AggregationFunctionType.SUM0` to it in
`AggregationFunctionFactory`. It skips null rows when null handling is enabled
while using primitive `DoubleAggregationResultHolder` /
`DoubleGroupByResultHolder` initialized to `0.0` and resolving
`extractFinalResult(null)` to `0.0`.
- **`pinot-segment-spi`**: Mapped `AggregationFunctionType.SUM0` to
`AggregationFunctionType.SUM` in
`AggregationFunctionColumnPair.getStoredType(...)` so Star-Tree indexes built
on `SUM(col)` continue to match `SUM0(col)`.
- **`pinot-query-runtime`**: Aligned `MultistageGroupByExecutor` (`FINAL`
with `leafReturnFinalResult=true`) with `MultistageAggregationExecutor` so
`null` merged results still pass through `extractFinalResult(null)` (preserving
mixed-version rolling upgrade compatibility when an older leaf returns `null`
for `SUM0`).
- **`pinot-query-planner`**: Updated `PinotAggregateFunctionRewriteRule` so
rewritten `SUM0` calls (`SUMINT`/`SUMLONG`) have a nullable return type and are
wrapped in `CASE(IS NOT NULL(sum), sum, 0)` to preserve `SUM0`'s non-null
empty-is-zero contract when `autoRewriteAggregationType=true` /
`AggregateFunctionRewrite` is enabled.
- **Tests**:
- Added `Sum0AggregationFunctionTest` and updated
`AggregationFunctionNullContractTest`.
- Added `AggregationFunctionColumnPairTest.testGetStoredTypeForSum0`.
- Added
`QueryCompilationTest.testAggregateFunctionRewritePreservesSum0EmptyContract`.
- Added
`AggregateOperatorTest.testLeafReturnFinalResultGroupByResolvesNullSum0`.
- Added end-to-end MSE query cases (including all 4 repro queries from
#20290, `enableNullHandling=false`, `autoRewriteAggregationType=true`,
`is_leaf_return_final_result='true'`, nullable values, and `ELSE NULL` contrast
cases) to `FilterAggregates.json`.
- Updated explain expectations in `OfflineTimestampIndexIntegrationTest`
and `MultiStageEngineExplainIntegrationTest`.
### Testing
- `./mvnw -pl
pinot-segment-spi,pinot-core,pinot-query-planner,pinot-query-runtime -am
-Dtest=AggregationFunctionColumnPairTest,Sum0AggregationFunctionTest,AggregationFunctionNullContractTest,IntermediateResultByteBufferSerDeTest,QueryCompilationTest,AggregateOperatorTest,ResourceBasedQueriesTest
-Dpinot.fileFilter=FilterAggregates -Dsurefire.failIfNoSpecifiedTests=false
test`
- `./mvnw spotless:apply license:format checkstyle:check license:check -pl
pinot-segment-spi,pinot-core,pinot-query-planner,pinot-query-runtime,pinot-integration-tests`
- `./mvnw test-compile -pl
pinot-segment-spi,pinot-core,pinot-query-planner,pinot-query-runtime,pinot-integration-tests
-am`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]