Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id DB326B5DBE5 for ; Wed, 25 May 2011 14:07:22 -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 84842-01 for ; Wed, 25 May 2011 17:07:15 +0000 (UTC) X-Greylist: delayed 00:06:30.712955 by SQLgrey-1.7.6 Received: from exprod7og104.obsmtp.com (exprod7og104.obsmtp.com [64.18.2.161]) by mail.postgresql.org (Postfix) with ESMTP id AD4A6B5DC36 for ; Wed, 25 May 2011 14:07:14 -0300 (ADT) Received: from mail-ew0-f41.google.com ([209.85.215.41]) (using TLSv1) by exprod7ob104.postini.com ([64.18.6.12]) with SMTP ID DSNKTd03QUCKMLEU4DrfaKSxTGorgOrIzG6Z@postini.com; Wed, 25 May 2011 10:07:15 PDT Received: by mail-ew0-f41.google.com with SMTP id 9so3532478ewy.14 for ; Wed, 25 May 2011 10:07:13 -0700 (PDT) Received: by 10.14.4.102 with SMTP id 78mr1701042eei.186.1306342841999; Wed, 25 May 2011 10:00:41 -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 y3sm6192837eeh.6.2011.05.25.10.00.40 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 25 May 2011 10:00:40 -0700 (PDT) Message-ID: <4DDD35B6.2050203@enterprisedb.com> Date: Wed, 25 May 2011 20:00:38 +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: Tom Lane CC: PostgreSQL-development Subject: Re: Nested CASE-WHEN scoping References: <4DDCEEB8.50602@enterprisedb.com> <5487.1306334876@sss.pgh.pa.us> In-Reply-To: <5487.1306334876@sss.pgh.pa.us> 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/1285 X-Sequence-Number: 188319 On 25.05.2011 17:47, Tom Lane wrote: > Heikki Linnakangas writes: >> 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. > > [ scratches head ... ] Why does the save/restore in ExecEvalCase not > take care of this? The mistake happens during planning, when the SQL function is inlined and pre-evaluated. It's a bit hard to see what happened once the planning is finished because the whole expression is folded into a constant, but here's goes: The original expression is: CASE now() WHEN 29 THEN 'foo' ELSE 'bar' END; In parse analysis, it is turned into this: CASE WHEN CaseTestExpr = 29 THEN 'foo' ELSE 'bar' END; where CaseTestExpr stands for the now(). Next the planner tries to simplify the WHEN condition, "CaseTestExpr = 29". The equality operator is implemented by the evileq(timestamptz, int4) SQL function, defined as: CASE $2 WHEN length($1::text) THEN true ELSE false END; That SQL-function is transformed at parse analysis into: CASE CaseTestExpr = length($1::text) THEN true ELSE false END; This CaseTestExpr stands for the Param to the function, $2. When that tranformed SQL function body is inlined into the outer WHEN clause, "CaseTestExpr = 29", and Params are substituted, it becomes: CASE CaseTestExpr = length(CaseTestExpr::text) THEN true ELSE false END. (you can see the expression tree for that if you print out 'newexpr' in inline_function(), just after the call to substitute_actual_parameters()) At this point it's easy to see that we have screwed up. The first CaseTestExpr stands for the inner CASE-value, which is $2, which stands for 29, and the second CaseTestExpr stands for the *outer* CASE-value, which is supposed to be now(). The planner cannot distinguish between the two anymore. Both CaseTestExprs are then incorrectly replaced with constant 29, and the whole expression is constant-folded into 'bar'. >> So I'm going to put the BETWEEN/IN fix aside for now, and refactor the >> placeholder infrastructure to handle several simultaneous placeholders, > > That sounds like a mess, and I'm not even convinced it'll solve the > problem ... Hmm, it would solve the above by if we can keep the CaseTestExprs separate. It's not quite as straightforward as I originally thought, as the parse analysis of the inlined SQL function needs to use placeholder numbers that are different from those used in the outer context. But it seems doable. BTW, i just stumbled into this: postgres=# explain verbose SELECT CASE now() WHEN (29+random()::int4) THEN 'foo' ELSE 'bar' END; ERROR: unexpected CASE WHEN clause: 326 Looks like ruleutils.c is also not prepared for the case that the implicit equality operation gets inlined into something else than an OpExpr. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com