pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: andrea@lombardoni.ch
To: pgsql-bugs@postgresql.org
Subject: BUG #8237: CASE Expression - Order of expression processing
Date: Tue, 18 Jun 2013 13:17:14 +0000
Message-ID: <E1Uovmc-0007FT-R4@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

The following bug has been logged on the website:

Bug reference:      8237
Logged by:          Andrea Lombardoni
Email address:      andrea@lombardoni.ch
PostgreSQL version: 9.0.4
Operating system:   Linux
Description:        

I observed the following behaviour (I tested the following statements in
9.0.4, 9.0.5 and 9.3beta1):


$ psql template1
template1=# SELECT CASE WHEN 0=0 THEN 0 ELSE 1/0 END;
 case 
------
    0
(1 row)


template1=# SELECT CASE WHEN 1=0 THEN 0 ELSE 1/0 END;
ERROR:  division by zero


In this case the CASE behaves as expected.


But in the following expression:


template1=# SELECT CASE WHEN (SELECT 0)=0 THEN 0 ELSE 1/0 END;
ERROR:  division by zero


(Just to be sure, a "SELECT (SELECT 0)=0;" returns true)


What I expect:


template1=# SELECT CASE WHEN (SELECT 0)=0 THEN 0 ELSE 1/0 END;
0
template1=# SELECT CASE WHEN (SELECT 1)=0 THEN 0 ELSE 1/0 END;
ERROR: division by zero


It seems that when the "CASE WHEN expression" is a query, the evaluation
order changes.
According to the documentation, this behaviour is wrong.


http://www.postgresql.org/docs/9.0/static/sql-expressions.html (4.2.13.
Expression Evaluation Rules):
"When it is essential to force evaluation order, a CASE construct (see
Section 9.16) can be used. "


http://www.postgresql.org/docs/9.0/static/functions-conditional.html
(9.16.1. CASE):
"If the condition's result is true, the value of the CASE expression is the
result that follows the condition, and the remainder of the CASE expression
is not processed."
"A CASE expression does not evaluate any subexpressions that are not needed
to determine the result."


The discussion on postgresql-general (
http://www.postgresql.org/message-id/CAMQ5dGq4SuJPbhT2-9XLAPAsvKNUL2-bb0cPyci2Fp+pfSfc3g@mail.gmail....
) also seems to indicate that this is a bug.


At least it is a discrepancy between documentation and behaviour. 



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (5+ messages)  latest in thread

Message-ID: <E1Uovmc-0007FT-R4@wrigleys.postgresql.org>
Permalink:  ../E1Uovmc-0007FT-R4@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/E1Uovmc-0007FT-R4@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: andrea@lombardoni.ch
  Subject: Re: BUG #8237: CASE Expression - Order of expression processing
  In-Reply-To: <E1Uovmc-0007FT-R4@wrigleys.postgresql.org>
  References: <E1Uovmc-0007FT-R4@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox