Re: [BUGS] BUG #11661: CASE ELSE is evaluated although condition is true

Lists: pgsql-bugspgsql-docs
From: michael(at)aplaypowered(dot)com
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #11661: CASE ELSE is evaluated although condition is true
Date: 2014-10-13 11:05:42
Message-ID: 20141013110542.25460.3900@wrigleys.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-docs

The following bug has been logged on the website:

Bug reference: 11661
Logged by: Michael Stieler
Email address: michael(at)aplaypowered(dot)com
PostgreSQL version: 9.2.6
Operating system: RHEL
Description:

I am not sure if it is a bug, a lack in documentation or just a
misunderstanding.
This issue refers to a StackOverflow question at
https://stackoverflow.com/questions/26297035/posgtres-case-condition-with-sum-aggregation-evaluates-not-needed-else-part

The following query:
case when sum( column1 ) = 0
then 0
else round( sum( price
* hours
/ column1 ), 2 )
raises a division by zero error, although the condition sum(column1) = 0 is
true. I would expect that the else part would never be evaluated.
Documentation only describes that this happens for immediates.


From: Marti Raudsepp <marti(at)juffo(dot)org>
To: michael(at)aplaypowered(dot)com
Cc: pgsql-bugs(at)postgresql(dot)org, pgsql-docs <pgsql-docs(at)postgresql(dot)org>
Subject: Re: [BUGS] BUG #11661: CASE ELSE is evaluated although condition is true
Date: 2014-10-13 22:54:12
Message-ID: CABRT9RDMWvoAW-NASCvO08AMZqED5N0jytYHPsoeor=O0VdmMg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-docs

On Mon, Oct 13, 2014 at 2:05 PM, <michael(at)aplaypowered(dot)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 Content-Type Size
0001-doc-Clarify-evaluation-order-of-aggregates-wrt-condi.patch binary/octet-stream 2.3 KB

From: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
To: Marti Raudsepp <marti(at)juffo(dot)org>
Cc: michael(at)aplaypowered(dot)com, pgsql-bugs(at)postgresql(dot)org, pgsql-docs <pgsql-docs(at)postgresql(dot)org>
Subject: Re: [DOCS] BUG #11661: CASE ELSE is evaluated although condition is true
Date: 2014-11-11 07:14:47
Message-ID: CAB7nPqRWwGchRvtCPNw0eUADiT6FxDoXRs_WVMyToTdnWn9=Mw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-docs

On Tue, Oct 14, 2014 at 7:54 AM, Marti Raudsepp <marti(at)juffo(dot)org> wrote:
> 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.

This patch does not compile properly as a </sect2> is missing, and I
think that you actually want to add this sect2 block at the bottom the
sect1 block for conditional functions. "implementation" is as well a
bit too general to name such a section, something like
"functions-conditional-implementation" would be better. Also, I am a
bit dubious about adding a mention aggregate functions and their
elements being evaluated in a paragraph for GROUP BY and HAVING
clause. All together only mentioning that in the conditional function
block sound enough. All those things grouped together result in the
attached. Marti, feel free to comment and correct what you think is
wrong :)
Regards,
--
Michael

Attachment Content-Type Size
20141111_conditional_funcs_doc.patch binary/octet-stream 1003 bytes

From: Marti Raudsepp <marti(at)juffo(dot)org>
To: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
Cc: michael(at)aplaypowered(dot)com, pgsql-bugs(at)postgresql(dot)org, pgsql-docs <pgsql-docs(at)postgresql(dot)org>
Subject: Re: [BUGS] BUG #11661: CASE ELSE is evaluated although condition is true
Date: 2014-11-11 07:37:42
Message-ID: CABRT9RAnfbAxD2XQOsGDBnMz5Q2_botfBj_Xz3+v_mVSpbKSXw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-docs

Hi

On Tue, Nov 11, 2014 at 9:14 AM, Michael Paquier
<michael(dot)paquier(at)gmail(dot)com> wrote:
> This patch does not compile properly as a </sect2> is missing, and I
> think that you actually want to add this sect2 block at the bottom the
> sect1 block for conditional functions.

Sorry, I never got around to testing the build because I found a
working DocBook envrionment a PITA to set up. Should have mentioned
that in my email.

> "implementation" is as well a
> bit too general to name such a section, something like
> "functions-conditional-implementation" would be better.

+1

> Also, I am a
> bit dubious about adding a mention aggregate functions and their
> elements being evaluated in a paragraph for GROUP BY and HAVING
> clause.

Why? This page explains in what order the expressions in a SELECT
query are processed. Aggregates can only be found in SELECT queries
and their semantics are non-trivial, so it seems like a big omission
to me that their relation to other clauses is not discussed.

I think that's the most appropriate place for that, because GROUP BY,
HAVING and aggregates are closely related. And their execution occurs
after evaluating GROUP BY keys and before HAVING predicates, where
else would one put it?

Regards,
Marti


From: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
To: Marti Raudsepp <marti(at)juffo(dot)org>
Cc: michael(at)aplaypowered(dot)com, pgsql-bugs(at)postgresql(dot)org, pgsql-docs <pgsql-docs(at)postgresql(dot)org>
Subject: Re: [BUGS] BUG #11661: CASE ELSE is evaluated although condition is true
Date: 2014-11-11 07:52:42
Message-ID: CAB7nPqT-73CCO-1WnrTa51iNpBdsUmQs_5ADmVkPBWM+oRu76A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-docs

On Tue, Nov 11, 2014 at 4:37 PM, Marti Raudsepp <marti(at)juffo(dot)org> wrote:
> Why? This page explains in what order the expressions in a SELECT
> query are processed. Aggregates can only be found in SELECT queries
> and their semantics are non-trivial, so it seems like a big omission
> to me that their relation to other clauses is not discussed.
>
> I think that's the most appropriate place for that, because GROUP BY,
> HAVING and aggregates are closely related. And their execution occurs
> after evaluating GROUP BY keys and before HAVING predicates, where
> else would one put it?
Ah, OK. I wasn't aware of this execution order. Now we could as well
split the description flow into more tiny paragraphs but this does not
seem worth it. So what about the attached then?
--
Michael

Attachment Content-Type Size
20141111_conditional_funcs_doc_v2.patch application/x-patch 2.0 KB

From: Marti Raudsepp <marti(at)juffo(dot)org>
To: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
Cc: michael(at)aplaypowered(dot)com, pgsql-bugs(at)postgresql(dot)org, pgsql-docs <pgsql-docs(at)postgresql(dot)org>
Subject: Re: [BUGS] BUG #11661: CASE ELSE is evaluated although condition is true
Date: 2014-11-11 08:26:02
Message-ID: CABRT9RAdLMs2J-Q-39wOXcaO_KTFuc2VqhXsZGArG_-F0RXFsQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-docs

On Tue, Nov 11, 2014 at 9:52 AM, Michael Paquier
<michael(dot)paquier(at)gmail(dot)com> wrote:
> Ah, OK. I wasn't aware of this execution order. Now we could as well
> split the description flow into more tiny paragraphs but this does not
> seem worth it.

Agreed.

Actually now that I read it...

+ values. All aggregate functions and their argument expressions
+ in the <command>SELECT</> and <literal>HAVING</> are evaluated.

Perhaps should be:
<command>SELECT</> and <literal>HAVING</> clauses are evaluated.

Other than that, looks good to me.

Regards,
Marti


From: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
To: Marti Raudsepp <marti(at)juffo(dot)org>
Cc: michael(at)aplaypowered(dot)com, pgsql-bugs(at)postgresql(dot)org, pgsql-docs <pgsql-docs(at)postgresql(dot)org>
Subject: Re: [DOCS] BUG #11661: CASE ELSE is evaluated although condition is true
Date: 2014-11-11 13:20:13
Message-ID: CAB7nPqQ5WGhq4QBSiW+74SRCH5G=BO4qp9RGbXeSbR+QRjB3NQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-docs

On Tue, Nov 11, 2014 at 5:26 PM, Marti Raudsepp <marti(at)juffo(dot)org> wrote:
> Perhaps should be:
> <command>SELECT</> and <literal>HAVING</> clauses are evaluated.
>
> Other than that, looks good to me.
Let's do so then and mark that as ready for committer.
Thanks,
--
Michael

Attachment Content-Type Size
20141111_conditional_funcs_doc_v3.patch application/x-patch 2.1 KB

From: Marti Raudsepp <marti(at)juffo(dot)org>
To: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
Cc: michael(at)aplaypowered(dot)com, pgsql-bugs(at)postgresql(dot)org, pgsql-docs <pgsql-docs(at)postgresql(dot)org>
Subject: Re: [DOCS] BUG #11661: CASE ELSE is evaluated although condition is true
Date: 2014-11-11 14:19:12
Message-ID: CABRT9RDewN0E2tiQwSEG-OuOSEVFph2LgjZQ9C-OZHj1ip7VFg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-docs

On Tue, Nov 11, 2014 at 3:20 PM, Michael Paquier
<michael(dot)paquier(at)gmail(dot)com> wrote:
> On Tue, Nov 11, 2014 at 5:26 PM, Marti Raudsepp <marti(at)juffo(dot)org> wrote:
>> Perhaps should be:
>> <command>SELECT</> and <literal>HAVING</> clauses are evaluated.

> Let's do so then

Oops, that should be my task, but you got there first. :)

> and mark that as ready for committer.

Thanks!

Regards,
Marti


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
Cc: Marti Raudsepp <marti(at)juffo(dot)org>, michael(at)aplaypowered(dot)com, pgsql-bugs(at)postgresql(dot)org, pgsql-docs <pgsql-docs(at)postgresql(dot)org>
Subject: Re: [DOCS] BUG #11661: CASE ELSE is evaluated although condition is true
Date: 2014-11-14 22:21:19
Message-ID: 15150.1416003679@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs pgsql-docs

Michael Paquier <michael(dot)paquier(at)gmail(dot)com> writes:
> On Tue, Nov 11, 2014 at 5:26 PM, Marti Raudsepp <marti(at)juffo(dot)org> wrote:
>> Perhaps should be:
>> <command>SELECT</> and <literal>HAVING</> clauses are evaluated.
>>
>> Other than that, looks good to me.

> Let's do so then and mark that as ready for committer.

I rewrote this somewhat and committed it.

regards, tom lane