You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add support for ON EMPTY clause in aggregate and window functions
This commit introduces the ON EMPTY clause, allowing users to specify
a default value to be returned when an aggregate or window function
receives an empty input set (zero processed rows). The syntax follows
the pattern:
agg_function(args, default_value ON EMPTY)
The ON EMPTY clause is distinct from standard NULL handling. It is
only invoked when the aggregate processes no rows at all. If the
aggregate processes rows that happen to be NULL, the normal transition
logic applies and the ON EMPTY clause is ignored. Likewise, when all
rows are removed by a FILTER clause the input set is empty and the
ON EMPTY value is returned. For a window aggregate, the default is
returned for any row whose frame contains no rows.
Implementation adds an 'inputReceived' flag to each per-group
aggregate state. A dedicated EEOP_AGG_INPUT_RECEIVED expression step
sets the flag once per row that reaches the aggregate -- after any
FILTER but before the strict-input NULL check -- so that a row whose
input is NULL (and is thus skipped by a strict transition function)
still counts as input. The step is emitted only for aggregates that
carry an ON EMPTY default. At finalization, if the flag is false,
the default expression is evaluated and returned in place of the
normal result (and final function, if any). Because a partial
aggregate's leader only sees workers' combined transition states
rather than the original rows, this flag cannot distinguish the
empty-input case under partial aggregation; such aggregates are
therefore marked non-partial so they are not parallelized.
This patch implements the new step in the expression interpreter only;
the feature is fully functional without LLVM. LLVM/JIT support for
the EEOP_AGG_INPUT_RECEIVED step is added by a separate follow-on
patch.
The default expression must be a constant-like expression coercible to
the aggregate's result type. It may not reference columns (at any
query level) or contain aggregates, window functions, subqueries, or
volatile functions. ON EMPTY cannot be combined with DISTINCT, and is
rejected for non-aggregate window functions; it may be used with an
ordered-set (WITHIN GROUP) aggregate, written before the WITHIN GROUP
clause.
Patch also includes documentation and regression tests for aggregates
and window functions, covering empty tables, filtered results, type
coercion, all-NULL inputs to strict aggregates, and view deparsing.
This commit adds fields to Aggref and WindowFunc (and FuncCall).
These node types are serialized into pg_rewrite (views/rules) and
other stored expression trees, which requires a CATALOG_VERSION_NO
bump. That is omitted here to avoid conflicts with concurrent
commits; the committer should bump it at commit time.
Proposed-by: Peter Eisentraut
Jeevan Chalke
0 commit comments