Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Uovmt-0000nI-7o for pgsql-bugs@arkaria.postgresql.org; Tue, 18 Jun 2013 13:17:31 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Uovms-0005ZM-N3 for pgsql-bugs@arkaria.postgresql.org; Tue, 18 Jun 2013 13:17:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Uovms-0005ZG-26 for pgsql-bugs@postgresql.org; Tue, 18 Jun 2013 13:17:30 +0000 Received: from mahout.postgresql.org ([2001:4800:7903:4::123]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Uovmj-0002R4-Hw for pgsql-bugs@postgresql.org; Tue, 18 Jun 2013 13:17:29 +0000 Received: from [204.145.124.246] (helo=wrigleys.postgresql.org) by mahout.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Uovmi-0003NF-9t for pgsql-bugs@postgresql.org; Tue, 18 Jun 2013 13:17:20 +0000 Received: from www-data by wrigleys.postgresql.org with local (Exim 4.80) (envelope-from ) id 1Uovmc-0007FT-R4 for pgsql-bugs@postgresql.org; Tue, 18 Jun 2013 13:17:14 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: BUG #8237: CASE Expression - Order of expression processing To: pgsql-bugs@postgresql.org From: andrea@lombardoni.ch Message-Id: Date: Tue, 18 Jun 2013 13:17:14 +0000 X-Pg-Spam-Score: -3.1 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org 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.com ) 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