Re: [BUGS] BUG #11661: CASE ELSE is evaluated although condition is true - Mailing list pgsql-docs

From Marti Raudsepp
Subject Re: [BUGS] BUG #11661: CASE ELSE is evaluated although condition is true
Date
Msg-id CABRT9RDMWvoAW-NASCvO08AMZqED5N0jytYHPsoeor=O0VdmMg@mail.gmail.com
Whole thread Raw
Responses Re: [BUGS] BUG #11661: CASE ELSE is evaluated although condition is true  (Michael Paquier <michael.paquier@gmail.com>)
List pgsql-docs
On Mon, Oct 13, 2014 at 2:05 PM,  <michael@aplaypowered.com> wrote:
> case when sum( column1 ) = 0
>             then 0
>             else round( sum(   price
>                              * hours
>                              / column1 ), 2 )

> I would expect that the else part would never be evaluated.
> Documentation only describes that this happens for immediates.

I explained this behavior in the Stack Overflow answer, it is caused
by way aggregates are evaluated in a SELECT (or HAVING) clause.

Our documentation should be clearer about the fact that all aggregates
are evaluated strictly before their surrounding SELECT expressions,
the attached documentation patch adds a note about this to the
"general processing of SELECT" explanation.

I also created a section "Implementation Notes" in the "Conditional
Expressions" chapter to explain this behavior, since conditionals is
where this behavior is most visible.

This similarly affects the HAVING clause:
db=# select sum(1/i) from unnest(array[0])i having min(i)>0;
ERROR:  division by zero

Regards,
Marti

Attachment

pgsql-docs by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: Huge pages section needs to describe hugetlb_shm_group, memlock limit
Next
From: Josh Kupershmidt
Date:
Subject: Re: Documentation for CREATE USER