Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y1eKu-0002QX-UY for pgsql-bugs@arkaria.postgresql.org; Thu, 18 Dec 2014 16:54:01 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y1eKu-0003Bn-Bt for pgsql-bugs@arkaria.postgresql.org; Thu, 18 Dec 2014 16:54:00 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y1eKt-0003Bh-Ln for pgsql-bugs@postgresql.org; Thu, 18 Dec 2014 16:53:59 +0000 Received: from mwork.nabble.com ([162.253.133.43]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y1eKi-0000oN-DN for pgsql-bugs@postgresql.org; Thu, 18 Dec 2014 16:53:57 +0000 Received: from msam.nabble.com (unknown [162.253.133.85]) by mwork.nabble.com (Postfix) with ESMTP id 1E26BE1C9DB for ; Thu, 18 Dec 2014 08:53:48 -0800 (PST) Date: Thu, 18 Dec 2014 09:53:47 -0700 (MST) From: David G Johnston To: pgsql-bugs@postgresql.org Message-ID: <1418921627518-5831325.post@n5.nabble.com> In-Reply-To: <16630.1418918898@sss.pgh.pa.us> References: <20141218144436.1237.96696@wrigleys.postgresql.org> <16630.1418918898@sss.pgh.pa.us> Subject: Re: BUG #12273: CASE Expression BUG MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -0.3 (/) 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 Tom Lane-2 wrote > jaksits.tibor@ > writes: >> CREATE OR REPLACE FUNCTION __is_numeric_test_does_not_work(a_text_param >> text) >> RETURNS double precision AS >> $BODY$DECLARE >> ret double precision; >> BEGIN >> SELECT __is_numeric_test >> INTO ret >> FROM __is_numeric_test((CASE WHEN is_numeric(a_text_param) THEN >> a_text_param::double precision ELSE 0.0::double precision END)); >> RETURN ret; >> END;$BODY$ >> LANGUAGE plpgsql VOLATILE; > >> But the query "SELECT * FROM __is_numeric_test_does_not_work('')" I get >> an >> error message: >> invalid input syntax for type double precision: "" > > You didn't show us what is_numeric() is, so it's impossible to reproduce > this example, but I imagine what is happening is that the value of > a_text_param is being substituted into the SELECT as a text constant, and > then constant-folding leads to attempting to simplify a_text_param::double > precision immediately. > > We're unlikely to change this, because it would cripple optimization > attempts. The fact that const-simplification doesn't happen in the other > way you wrote the function is not more-correct behavior, it's just an > implementation artifact that you shouldn't rely on. What you need to do > is code this as an if-then-else sequence, not CASE, so that you don't > attempt to evaluate any expressions with undefined constant > subexpressions. Note that this is documented here: http://www.postgresql.org/docs/9.4/interactive/functions-conditional.html#FUNCTIONS-CASE Specifically, the "Note" at the end of 9.17.1 Maybe an example would make this got-cha more memorable but it is noted in the docs right next to the spot where it is described that CASE evaluation does short-circuit during execution - just not always during planning. David J. -- View this message in context: http://postgresql.nabble.com/BUG-12273-CASE-Expression-BUG-tp5831307p5831325.html Sent from the PostgreSQL - bugs mailing list archive at Nabble.com. -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs