Re: Add PRODUCT() aggregate function
Re: Add PRODUCT() aggregate function
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Vik Fearing writes: > It should be called PRODUCT and it should accept the syntax > PRODUCT(col, 1 ON EMPTY) > for when there are no non-nulls in the input. Sigh. The committee really loves to invent randomly creative syntax, don't they. At least this one won't force us to create any new fully-reserved words. > This will be required by the next edition of the SQL standard. The exact > value "1" is required by the standard, but I think we should allow any > a_expr there. I think that this may be trickier than it looks. You'd really want that to act like a "direct" argument, ie evaluate once not once per row. Also, if PRODUCT can use this, SUM could use it even more, and probably other aggregates too (but likely values would be different from "1"). Did they generalize at all, or is this a PRODUCT-specific wart? Anyway, I'd counsel thinking of this ON EMPTY business as an orthogonal feature to PRODUCT. My real concern about PRODUCT as such is that it seems enormously prone to overflow. It'd make little sense to invent variants emitting anything except numeric or float8. regards, tom lane
Re: Add PRODUCT() aggregate function
Vik Fearing writes: > On 10/09/2026 18:16, Tom Lane wrote: >> I think that this may be trickier than it looks. You'd really want >> that to act like a "direct" argument, ie evaluate once not once per >> row. > That's fair, but it isn't what happens for string_agg, for example, > which can have a different separator per value. Sure, but that's not the same thing. You can do something credible with a separator-per-value in string_agg, but it's nonsense to suppose that ON EMPTY is a per-row value. If it were per-row, which value would you use? I assume it applies even if there are zero input rows, not only if there are some inputs but they happen to all be null. The committee is evidently choosing to sidestep the how-many-evaluations question by insisting on a constant value, which may well be sufficient for all real-world cases. If we want it to be "any a_expr" though, we have to think about that. > It's for both PRODUCT and SUM (1 and 0 respectively) and not generalized > beyond that. OK, at least the SUM case occurred to them ;-). But I think for our purposes we definitely want to allow it for any aggregate. regards, tom lane
Re: Add PRODUCT() aggregate function
От:
Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Дата:
On Fri, Sep 11, 2026 at 5:16 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Vik Fearing <vik@postgresfriends.org> writes:
> On 10/09/2026 18:16, Tom Lane wrote:
>> I think that this may be trickier than it looks. You'd really want
>> that to act like a "direct" argument, ie evaluate once not once per
>> row.
> That's fair, but it isn't what happens for string_agg, for example,
> which can have a different separator per value.
Sure, but that's not the same thing. You can do something credible
with a separator-per-value in string_agg, but it's nonsense to suppose
that ON EMPTY is a per-row value. If it were per-row, which value
would you use? I assume it applies even if there are zero input rows,
not only if there are some inputs but they happen to all be null.
In my currently proposed patch (https://www.postgresql.org/message-id/CAM2+6=VS=fSKxfimW6Th9iu_xjbxOEAKg4eYwaa=SMg3X8pHaQ@mail.gmail.com), the
ON EMPTY value is strictly returned only when there are zero input rows. Rows containing NULL are treated as valid rows and do not trigger the ON EMPTY clause.
The committee is evidently choosing to sidestep the
how-many-evaluations question by insisting on a constant value,
which may well be sufficient for all real-world cases. If we want
it to be "any a_expr" though, we have to think about that.
> It's for both PRODUCT and SUM (1 and 0 respectively) and not generalized
> beyond that.
OK, at least the SUM case occurred to them ;-). But I think for
our purposes we definitely want to allow it for any aggregate.
Yes, the proposed patch supports this for all aggregate functions and is not restricted to specific ones at the moment.
Additionally, while the grammar accepts an
a_expr, the backend code includes checks to ensure the provided expression is a constant value that is type-coercible to the aggregate's result type.I would be very happy to receive any feedback or comments on that thread as well.
Thanks,
regards, tom lane
Re: Add PRODUCT() aggregate function
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Jeevan Chalke writes: > In my currently proposed patch ( > https://www.postgresql.org/message-id/CAM2+6=VS=fSKxfimW6Th9iu_xjbxOEAKg4eYwaa=SMg3X8pHaQ@mail.gmail.com), > the ON EMPTY value is strictly returned only when there are zero input > rows. Rows containing NULL are treated as valid rows and do not trigger the ON > EMPTY clause. [ ... not having read the patch ... ] There is a critical distinction here between strict and non-strict aggregates. My interpretation of how this should work is that ON EMPTY should trigger if zero rows were fed to the aggregate's transition function. A row containing NULL is valid input if the transition function is non-strict, otherwise it is not. What I gather from Vik's comments is that the SQL committee only formalized the behavior for strict aggregates (since both PRODUCT and SUM ignore nulls). So we're somewhat out on a limb here for the non-strict case, but I think we have to define that one as being "null inputs count as inputs". regards, tom lane
Re: Add PRODUCT() aggregate function
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
... btw, there is another interesting definitional question here. AFAICS, "ON EMPTY" effectively is an override for the aggregate's final function. Does it actually make sense when there is a final function? Specifically, if the final function were willing to provide non-null output for zero rows in, should we still override it? SUM and PRODUCT don't provide a lot of guidance here. I'm also wondering idly how this interacts with "inverse transition functions" for aggregates used as window functions. regards, tom lane
Re: Add PRODUCT() aggregate function
От:
Vik Fearing <vik@postgresfriends.org>
Дата:
On 11/09/2026 05:54, Jeevan Chalke wrote:
On Fri, Sep 11, 2026 at 8:01 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:Jeevan Chalke <jeevan.chalke@enterprisedb.com> writes:
> In my currently proposed patch (
> https://www.postgresql.org/message-id/CAM2+6=VS=fSKxfimW6Th9iu_xjbxOEAKg4eYwaa=SMg3X8pHaQ@mail.gmail.com),
> the ON EMPTY value is strictly returned only when there are zero input
> rows. Rows containing NULL are treated as valid rows and do not trigger the ON
> EMPTY clause.
[ ... not having read the patch ... ] There is a critical distinction
here between strict and non-strict aggregates. My interpretation of
how this should work is that ON EMPTY should trigger if zero rows were
fed to the aggregate's transition function. A row containing NULL is
valid input if the transition function is non-strict, otherwise it is
not.
What I gather from Vik's comments is that the SQL committee only
formalized the behavior for strict aggregates (since both PRODUCT
and SUM ignore nulls). So we're somewhat out on a limb here for
the non-strict case, but I think we have to define that one as
being "null inputs count as inputs".Agree with the strict/non-strict point. But the SQL standard text for this is
not available yet, so I am not sure what exact behaviour we should follow here.
Do you or Vik have more details on what the committee is going with? That willhelp us decide the correct semantics instead of guessing.
Its definition in the standard is exactly:
PRODUCT(SQ VE, 1 ON EMPTY) => COALESCE(PRODUCT(SQ VE), 1)
where SQ is ALL/DISTINCT and VE is the value expression.
--
Vik Fearing
Re: Add PRODUCT() aggregate function
От:
Vik Fearing <vik@postgresfriends.org>
Дата:
On 11/09/2026 03:21, Tom Lane wrote: > Isaac Morland writes: >> Out of curiosity, was there any thought given to the idea that it should be >> an attribute of the aggregate itself? > SUM over no rows has been defined to return NULL for decades. They > can't just up and change that. They could have instead defined a > new aggregate (SUM0, maybe) but chose not to. I was arguing for SUM(expr, 0) but it wasn't deemed to be sufficiently "English." -- Vik Fearing