gianm edited a comment on issue #7953: Exact distinct-COUNT with complex expression (CASE, IN) throws NullPointerException URL: https://github.com/apache/incubator-druid/issues/7953#issuecomment-508869607 Reading through [AggregateExpandDistinctAggregatesRule](https://github.com/apache/calcite/blob/calcite-1.17.0/core/src/main/java/org/apache/calcite/rel/rules/AggregateExpandDistinctAggregatesRule.java) I think the problem is something like this: 1. The NPE occurs on this line of `doRewrite`, because `sourceOf` does not contain the key `aggCall.filterArg`: ```java final int newFilterArg = aggCall.filterArg >= 0 ? sourceOf.get(aggCall.filterArg) : -1; ``` 2. `sourceOf` comes from this section above, in the same `doRewrite` method: ```java // Project the columns of the GROUP BY plus the arguments // to the agg function. final Map<Integer, Integer> sourceOf = new HashMap<>(); createSelectDistinct(relBuilder, aggregate, argList, filterArg, sourceOf); ``` 3. The `createSelectDistinct` function populates `sourceOf` for each arg of `argList` and mixes in the `filterArg` input ref into each one. It does not populate `sourceOf` for the `filterArg` itself (it seems to be trying to rewrite filtered aggregations as a DISTINCT of a CASE statement). 4. The combination o f(1) and (3) above is bad, since `doRewrite` is trying to pull `sourceOf.get(aggCall.filterArg)`, which won't be there, since `createSelectDistinct` is (seemingly intentionally) not populating it. I'm not totally sure where the bug is, but it seems to me that it's probably the part where `doRewrite` tries to create `newFilterArg` (item 1 above). `createSelectDistinct` seemingly intentionally rewrites filtered aggregations as non-filtered aggregations, so `doRewrite` should not be trying to create a `newFilterArg`. @julianhyde - what do you think?
---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
