Re: BUG #14301: function in case expression called when it should not be

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: rikard(at)ngs(dot)hr
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #14301: function in case expression called when it should not be
Date: 2016-08-29 15:28:34
Message-ID: 7178.1472484514@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

rikard(at)ngs(dot)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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2016-08-29 16:24:43 Re: BUG #14300: Empty cube representation
Previous Message rikard 2016-08-29 15:14:33 BUG #14301: function in case expression called when it should not be