[SQL] Support the MODE aggregate function by desugaring into GROUP BY + COUNT + ARG_MAX - #6806
[SQL] Support the MODE aggregate function by desugaring into GROUP BY + COUNT + ARG_MAX#6806mihaibudiu wants to merge 1 commit into
MODE aggregate function by desugaring into GROUP BY + COUNT + ARG_MAX#6806Conversation
… COUNT + ARG_MAX Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
|
We reject |
mythical-fred
left a comment
There was a problem hiding this comment.
APPROVE.
Nice desugaring: MODE(v) → inner Aggregate(G∪{v}, COUNT(v)) → outer Aggregate(G, ARG_MAX(v, cnt)), joined back with other aggregates via IS NOT DISTINCT FROM on the group keys. Using COUNT(v) rather than COUNT(*) is what makes the NULL semantics fall out naturally — the all-NULL sub-group gets cnt=0 and only wins when it is the only candidate, so MODE of an all-NULL group is NULL and NULLs never beat a real value. The FILTER case is handled by wrapping the value in CASE(cnt > 0, value, NULL) so groups whose rows are all filtered out survive with a NULL mode. Repeated MODE calls with the same (arg, filterArg) share one branch; the final projection reassembles the original column order. Tests cover the happy path, FILTER (including a group with only NULLs passing the filter and a group with no passing rows), interleaved aggregates with a shared MODE branch, empty input, and DISTINCT rejection.
A couple of small observations, none blocking:
RejectUnsupportedPlansis a good pattern — worth reusing for other Calcite-accepted-but-unsupported constructs later. Consider giving it its own file once a second case appears.- The rule silently returns for
GROUPING SETS/CUBE/ROLLUP(non-SIMPLE group type). That will fall through to whatever the backend does with a rawMODE, which today is nothing. If you want a clean error rather than a mysterious backend failure,RejectUnsupportedPlansis the natural place to also rejectMODEunder non-SIMPLE groupings. - Minor: docs say "The rule for selecting the value is not specified if multiple values are tied for the highest frequency" — worth noting that ties in practice depend on
ARG_MAX's ordering (which forMODEis a count, and for equal counts is effectively arbitrary). Not a change request, just a doc-precision thought for future.
LGTM.
Checklist