X-Original-To: pgsql-hackers-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 4E37BD1B507 for ; Sun, 28 Sep 2003 03:34:05 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 53586-10 for ; Sun, 28 Sep 2003 00:33:34 -0300 (ADT) Received: from smtp012.mail.yahoo.com (smtp012.mail.yahoo.com [216.136.173.32]) by svr1.postgresql.org (Postfix) with SMTP id 6DA03D1B4E2 for ; Sun, 28 Sep 2003 00:33:29 -0300 (ADT) Received: from pcp01341166pcs.wilog301.pa.comcast.net (HELO europa.janwieck.net) (janwieck@68.80.245.191 with login) by smtp.mail.vip.sc5.yahoo.com with SMTP; 28 Sep 2003 03:33:28 -0000 Received: from Yahoo.com (pcp01341166pcs.wilog301.pa.comcast.net [68.80.245.191]) (authenticated) by europa.janwieck.net (8.11.6/8.11.6) with ESMTP id h8S3XQ304090; Sat, 27 Sep 2003 23:33:26 -0400 Message-ID: <3F765672.2020304@Yahoo.com> Date: Sat, 27 Sep 2003 23:33:06 -0400 From: Jan Wieck User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Tom Lane Cc: Jan Wieck , pgsql-hackers@postgresql.org Subject: Re: [SQL] plpgsql doesn't coerce boolean expressions to References: <15403.1063035632@sss.pgh.pa.us> <3F5E0A5D.2010803@Yahoo.com> <3559.1063128729@sss.pgh.pa.us> <3F5E84CF.6090706@Yahoo.com> <3487.1064712513@sss.pgh.pa.us> In-Reply-To: <3487.1064712513@sss.pgh.pa.us> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/1460 X-Sequence-Number: 44472 Tom Lane wrote: > Jan Wieck writes: >> Tom Lane wrote: >>>> 4. Use the parser's coerce_to_boolean procedure, so that nonbooleans >>>> will be accepted in exactly the same cases where they'd be accepted >>>> in a boolean-requiring SQL construct (such as CASE). (By default, >>>> none are, so this isn't really different from #2. But people could >>>> create casts to boolean to override this behavior in a controlled >>>> fashion.) > >> Agreed - #4. > > My first attempt at doing this failed to pass the regression tests, > because it wasn't prepared for this: > > if count(*) = 0 from Room where roomno = new.roomno then > raise exception ''Room % does not exist'', new.roomno; > end if; > > Is this really intended to be a feature? It manages to work because > plpgsql simply sticks "SELECT " in front of whatever appears between > IF and THEN, and passes the result to the main SQL engine. But it sure > surprised the heck out of me. The documentation gives no hint that > you're allowed to write anything but a straight boolean expression in IF. > Does Oracle allow that sort of thing? I have to admit it was less an intention than more a side effect of the actual implementation. It was so easy to simply stick "SELECT " in front of "everything between IF and THEN" and expect the result to be a boolean. In the same way you can do varname := count(*) from Room where roomno = new.roomno; which is straight forward because it's simply sticking "SELECT " in front of "everything between := and ;". Well, this does a bit more in that it tries the typinput(typoutput(result)) casting hack ... I know that you don't like that one. > > I would be inclined to think that a more reasonable expression of the > intent would be > > if (select count(*) from Room where roomno = new.roomno) = 0 then > > Certainly we'd have a big problem supporting the existing coding if we > ever reimplement plpgsql with more awareness of what expressions are. Without parsing much, much more, and finally parsing basically the whole SQL grammar in the PL/pgSQL parser, I don't see how you can do that. Jan -- #======================================================================# # It's easier to get forgiveness for being wrong than for being right. # # Let's break this rule - forgive me. # #================================================== JanWieck@Yahoo.com #