Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YdUSC-0001lw-N8 for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Apr 2015 02:01:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YdUSB-0002P6-Eh for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Apr 2015 02:01:55 +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 1YdUSA-0002Oi-DI for pgsql-bugs@postgresql.org; Thu, 02 Apr 2015 02:01:54 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1YdUS7-0004ja-5b for pgsql-bugs@postgresql.org; Thu, 02 Apr 2015 02:01:52 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1YdUS1-0003ac-UB; Wed, 01 Apr 2015 22:01:45 -0400 Date: Wed, 1 Apr 2015 22:01:45 -0400 From: Bruce Momjian To: Kevin Grittner Cc: "tneumann@users.sourceforge.net" , "pgsql-bugs@postgresql.org" Subject: Re: BUG #12462: NULLIF changes the argument type Message-ID: <20150402020145.GC29404@momjian.us> References: <20150108161155.11500.32736@wrigleys.postgresql.org> <311243186.3874557.1420735545628.JavaMail.yahoo@jws100104.mail.ne1.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <311243186.3874557.1420735545628.JavaMail.yahoo@jws100104.mail.ne1.yahoo.com> User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -1.9 (-) 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 On Thu, Jan 8, 2015 at 04:45:45PM +0000, Kevin Grittner wrote: > "tneumann@users.sourceforge.net" wrote: > > > The SQL standard in Section 6.11, Syntax rule 1 c) says that > > > > "" > > NULLIF (V1, V2) is equivalent to the following : > > CASE WHEN > > V1=V2 THEN > > NULL ELSE V1 > > END > > "" > > > > which is currently not the case in Postgres. Postgres promotes V1 to the > > type of V2, which can lead to behavior changes. > > > > Example query as illustration: It should produce 0,0,0 (and does on SQL > > Server and DB2), but PostgreSQL promotes the type and produces 0,0.5,0 > > > > select 1/2,nullif(1,2.3)/2,case when 1=2.3 then NULL else 1 end/2 > > I agree that this fails to conform to the standard and should be > considered a bug. FWIW, Oracle, SQL Lite, and MySQL also return > matching values for the three columns in your sample query; > although Oracle and MySQL return 0.5,0.5,0.5 instead of 0,0,0. I researched this and it isn't the ordering that is the issue, but rather the case where one type can represent all nullif() arguments. As you can see the order does not matter: test=> select pg_typeof(nullif(2.3, 1)); pg_typeof ----------- numeric (1 row) test=> select pg_typeof(nullif(1, 2.3)); pg_typeof ----------- numeric (1 row) Numeric is chosen because integer and numeric can be represented as numeric. With mismatched types, the order also doesn't matter: test=> select pg_typeof(nullif('a', 2.3)); ERROR: invalid input syntax for type numeric: "a" LINE 1: select pg_typeof(nullif('a', 2.3)); ^ test=> select pg_typeof(nullif(2.3, 'a')); ERROR: invalid input syntax for type numeric: "a" LINE 1: select pg_typeof(nullif(2.3, 'a')); You can see that transformCaseExpr() calls select_common_type() to set the CASE data type. -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + Everyone has their own god. + -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs