gortiz opened a new issue, #20290:
URL: https://github.com/apache/pinot/issues/20290
In the multi-stage engine, `SUM(CASE WHEN <cond> THEN x ELSE 0 END)` returns
`NULL` when no row in the group matches `<cond>`. The correct result is `0`:
every row adds either `x` or `0`, so the sum is never empty.
This is a wrong result, not only a cosmetic difference. A user who writes
`ELSE 0` expects a number, and a `NULL` changes the meaning of downstream
arithmetic.
I reproduced it on current master (e8d3e582e4) with this
`ResourceBasedQueriesTest` file. All four queries fail against H2, with both
`usePhysicalOptimizer=false` and `true`:
```json
{
"sum_case_else_zero_no_match": {
"tables": {
"tbl": {
"schema": [
{"name": "k", "type": "STRING"},
{"name": "s", "type": "STRING"},
{"name": "v", "type": "LONG"},
{"name": "t_ms", "type": "LONG"}
],
"inputs": [
["a", "x", 10, 1],
["a", "x", 20, 2],
["b", "y", 30, 1],
["c", "x", 40, 1]
]
}
},
"queries": [
{"sql": "SELECT SUM(CASE WHEN s = 'zzz' THEN v ELSE 0 END) FROM
{tbl}"},
{"sql": "SELECT SUM(CASE WHEN s = 'zzz' THEN v ELSE 0 END) FROM
(SELECT s, v, ROW_NUMBER() OVER (PARTITION BY k ORDER BY t_ms DESC) AS rn FROM
{tbl}) t WHERE rn = 1"},
{"sql": "SELECT s, SUM(CASE WHEN v > 1000 THEN v ELSE 0 END) FROM
(SELECT s, v, ROW_NUMBER() OVER (PARTITION BY k ORDER BY t_ms DESC) AS rn FROM
{tbl}) t WHERE rn = 1 GROUP BY s"},
{"sql": "SELECT SUM(CASE WHEN s = 'zzz' THEN m ELSE 0 END) FROM
(SELECT k, s, MAX(v) AS m FROM {tbl} GROUP BY k, s) t"}
]
}
}
```
```
Mismatched value at row id: 0, column id: 0. Expected Row: [0], Actual Row:
[null]
Mismatched value at row id: 0, column id: 1. Expected Row: [x, 0], Actual
Row: [x, null]
```
On a real cluster I see the same `NULL` with `enableNullHandling` set to
`true` and to `false`. There, the aggregation directly on the table (the leaf
stage) returned `0`. Only aggregations that run in an intermediate stage, above
a window or another aggregation, returned `NULL`.
The plan shows where it comes from. Calcite's `AggregateCaseToFilter` turns
the expression into a filtered `$SUM0`:
```
LogicalProject(EXPR$0=[CASE(=($1, 0), null:BIGINT, $0)])
PinotLogicalAggregate(group=[{}], agg#0=[$SUM0($0)], agg#1=[COUNT($1)],
aggType=[FINAL])
PinotLogicalExchange(distribution=[hash])
PinotLogicalAggregate(group=[{}], agg#0=[$SUM0($1) FILTER $2],
agg#1=[COUNT($0)], aggType=[LEAF])
LogicalProject($f0=[CASE(=($3, 'zzz'), $0, 0:BIGINT)], v=[$0],
$f2=[IS TRUE(=($3, 'zzz'))])
...
```
The `COUNT` in the `CASE` counts every row, so the project does not create
the `NULL`. The `$SUM0(x) FILTER cond` call does: when no row passes the
filter, it returns `NULL` instead of `0`. By definition, `$SUM0` returns `0`
for an empty input.
I think the cause is that Pinot treats `SUM0` exactly like `SUM`:
- `AggregationFunctionFactory` builds `SUM0` as `new
SumAggregationFunction(arguments, nullHandlingEnabled)`.
- `PinotAggregateFunctionRewriteRule` rewrites both `SUM` and `SUM0` into
`SUMLONG`/`SUMINT`.
Both lose the "empty input gives 0" contract of `SUM0`. A fix could keep a
`SUM0` variant that returns `0` for an empty group, or wrap the rewritten call
in `COALESCE(..., 0)` in the planner.
A workaround for users today is `COALESCE(SUM(CASE WHEN ... THEN x ELSE 0
END), 0)`.
--
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]