X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 868B8D1B4F6; Mon, 8 Sep 2003 16:11:13 +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 75005-03; Mon, 8 Sep 2003 13:10:41 -0300 (ADT) Received: from trolak.mydnsbox2.com (ns1.mydnsbox2.com [207.44.142.118]) by svr1.postgresql.org (Postfix) with ESMTP id C969ED1B4FD; Mon, 8 Sep 2003 13:10:38 -0300 (ADT) Received: from dunslane.net (x.ncshp.org [199.90.235.43]) (authenticated (0 bits)) by trolak.mydnsbox2.com (8.11.6/8.11.6) with ESMTP id h88G2rR10717; Mon, 8 Sep 2003 11:02:53 -0500 Message-ID: <3F5CA9F3.7000805@dunslane.net> Date: Mon, 08 Sep 2003 12:10:27 -0400 From: Andrew Dunstan User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030703 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-hackers@postgresql.org, pgsql-sql@postgresql.org Subject: Re: [HACKERS] plpgsql doesn't coerce boolean expressions to boolean References: <15403.1063035632@sss.pgh.pa.us> In-Reply-To: <15403.1063035632@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/75 X-Sequence-Number: 15053 Tom Lane wrote: >Following up this gripe >http://archives.postgresql.org/pgsql-sql/2003-09/msg00044.php >I've realized that plpgsql just assumes that the test expression >of an IF, WHILE, or EXIT statement is a boolean expression. It >doesn't take any measures to ensure this is the case or convert >the value if it's not the case. This seems pretty bogus to me. > >However ... with the code as it stands, for pass-by-reference datatypes >any nonnull value will appear TRUE, while for pass-by-value datatypes >any nonzero value will appear TRUE. I fear that people may actually be >depending on these behaviors, particularly the latter one which is >pretty reasonable if you're accustomed to C. So while I'd like to throw >an error if the argument isn't boolean, I'm afraid of breaking people's >function definitions. > >Here are some possible responses, roughly in order of difficulty >to implement: > >1. Leave well enough alone (and perhaps document the behavior). > >2. Throw an error if the expression doesn't return boolean. > >3. Try to convert nonbooleans to boolean using plpgsql's usual method > for cross-type coercion, ie run the type's output proc to get a > string and feed it to bool's input proc. (This seems unlikely to > avoid throwing an error in very many cases, but it'd be the most > consistent with other parts of plpgsql.) > >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.) > >Any opinions about what to do? > > > It won't bite me so maybe I don't have a right to express an opinion :-) plpgsql is not C - it appears to be in the Algol/Pascal/Ada family, which do tend to avoid implicit type conversion. On that basis, option 2 seems like it might be the right answer and also the one most likely to break lots of existing functions. Maybe the right thing would be to deprecate relying on implicit conversion to boolean for one release cycle and then make it an error. cheers andrew