Re: BUG #14301: function in case expression called when it should not be - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #14301: function in case expression called when it should not be
Date
Msg-id 7178.1472484514@sss.pgh.pa.us
Whole thread Raw
In response to BUG #14301: function in case expression called when it should not be  (rikard@ngs.hr)
List pgsql-bugs
rikard@ngs.hr writes:
> select case when doSlow then sum(slowFunction()) else sum(1) end as total
> from something group by doSlow

You are misunderstanding how aggregates work.  The aggregates are
evaluated first, then the surrounding expressions are done once at
the end of the query (or group).  The fact that the CASE might choose
the other branch at the end doesn't eliminate the necessity to run
both aggregates across all the rows.

            regards, tom lane

pgsql-bugs by date:

Previous
From: rikard@ngs.hr
Date:
Subject: BUG #14301: function in case expression called when it should not be
Next
From: Tom Lane
Date:
Subject: Re: BUG #14300: Empty cube representation