Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 68CA2B5DBDC for ; Wed, 25 May 2011 08:57:58 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 04348-09 for ; Wed, 25 May 2011 11:57:50 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from exprod7og124.obsmtp.com (exprod7og124.obsmtp.com [64.18.2.26]) by mail.postgresql.org (Postfix) with ESMTP id CEE9DB5D801 for ; Wed, 25 May 2011 08:57:49 -0300 (ADT) Received: from mail-ew0-f50.google.com ([209.85.215.50]) (using TLSv1) by exprod7ob124.postini.com ([64.18.6.12]) with SMTP ID DSNKTdzuvVy+OtGPGKH2W3AugHA6THRUe6lO@postini.com; Wed, 25 May 2011 04:57:50 PDT Received: by ewy10 with SMTP id 10so2417769ewy.9 for ; Wed, 25 May 2011 04:57:47 -0700 (PDT) Received: by 10.14.16.150 with SMTP id h22mr1438795eeh.62.1306324667670; Wed, 25 May 2011 04:57:47 -0700 (PDT) Received: from [192.168.1.183] (dsl-hkibrasgw2-ff7ac300-240.dhcp.inet.fi [88.195.122.240]) by mx.google.com with ESMTPS id y18sm6025725eeh.10.2011.05.25.04.57.46 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 25 May 2011 04:57:46 -0700 (PDT) Message-ID: <4DDCEEB8.50602@enterprisedb.com> Date: Wed, 25 May 2011 14:57:44 +0300 From: Heikki Linnakangas User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; fi-FI; rv:1.9.1.16) Gecko/20110505 Icedove/3.0.11 MIME-Version: 1.0 To: PostgreSQL-development Subject: Nested CASE-WHEN scoping Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201105/1234 X-Sequence-Number: 188268 While looking at fixing the multiple-evaluation issue in IN and BETWEEN discussed a while ago, I realized that the current assumption that only one CaseTestExpr placeholder needs to be valid at any given time is not true. Here's a bit contrived example: CREATE FUNCTION evileq (timestamptz, int4) returns boolean AS $$ SELECT case $2 WHEN length($1::text) THEN true ELSE false END; $$ language sql; CREATE OPERATOR = (procedure = evileq, leftarg = timestamptz, rightarg = int4); postgres=# SELECT now() = 29, CASE now() WHEN 29 THEN 'foo' ELSE 'bar' END; ?column? | case ----------+------ t | bar (1 row) Direct call to the operator, "now () = 29" returns true, but when used in CASE-WHEN, which implicitly does the same comparison, the result is false. Admittedly that's pretty far-fetched, but nevertheless it's a bug. As part of the BETWEEN/IN fix, I was going to refactor CaseTestExpr and CoerceToDomainValue placeholder node types into one generic placeholder node. BETWEEN needs three placeholder slots in the worst case [*], and now it seems that we need to handle an arbitrary number of simultaneous placeholders even for CASE-WHEN. So I'm going to put the BETWEEN/IN fix aside for now, and refactor the placeholder infrastructure to handle several simultaneous placeholders, and replace CaseTestExpr and CoerceToDomainValue with it. Actually AggRef and WindowFunc nodes look a lot like CaseTestExpr and CoerceToDomainValue too, but I'm a bit scared of touching those. PS. This is all 9.2 material, in case you're wondering. We're talking about pretty big patches. [*] a BETWEEN SYMMETRIC b AND c is handled as "(a <= b AND a >= c) OR (a >= b AND a <= c)", leading to multiple evaluation of all three operands if placeholders are not used -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com