Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 718E0B5DBC0 for ; Thu, 16 Jun 2011 17:56:41 -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 54574-05 for ; Thu, 16 Jun 2011 20:56:35 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id C2B44B5D91F for ; Thu, 16 Jun 2011 17:56:34 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p5GKuUdu025827; Thu, 16 Jun 2011 16:56:30 -0400 (EDT) To: Heikki Linnakangas cc: PostgreSQL-development Subject: Re: Nested CASE-WHEN scoping In-reply-to: <4DE93A08.90309@enterprisedb.com> References: <4DDCEEB8.50602@enterprisedb.com> <5487.1306334876@sss.pgh.pa.us> <4DDD35B6.2050203@enterprisedb.com> <27772.1306359110@sss.pgh.pa.us> <4DE38CCD.80702@enterprisedb.com> <23055.1306765290@sss.pgh.pa.us> <4DE512FC.9070707@enterprisedb.com> <4DE93A08.90309@enterprisedb.com> Comments: In-reply-to Heikki Linnakangas message dated "Fri, 03 Jun 2011 22:46:16 +0300" Date: Thu, 16 Jun 2011 16:56:30 -0400 Message-ID: <25826.1308257790@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201106/1451 X-Sequence-Number: 190164 Heikki Linnakangas writes: > The complicated part is to ensure that levelsup is always set correctly. > At parse time, levelsup is always set to 0, as the syntax doesn't allow > referencing upper levels directly. When an SQL function is inlined, any > ExpressionParams in the expressions that are substituted for Params need > to have their levelsup adjusted, so that it still refers to the right > value if there's CASE expressions in the inlined function. Also, when an > ExpressionParam is replaced with a Const, the levelsup fields of any > other ExpressionParams within the CaseExpr referring to higher levels > need to have their levelsup decremented to account for the fact that the > CaseExpr doesn't push the expression parameter anymore. I believe this is an unworkably complex, and almost certainly buggy Rube Goldberg device. Even if it manages to work today, it's going to be impossible to maintain those levelsup values correctly during any sort of expression rearrangement or optimization. Please take another look at just assigning a PARAM_EXEC parameter per Case expression. regards, tom lane