Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 2144BB5D80B for ; Fri, 7 Oct 2011 05:26:58 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 22226-08 for ; Fri, 7 Oct 2011 08:26:51 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from exprod7og110.obsmtp.com (exprod7og110.obsmtp.com [64.18.2.173]) by mail.postgresql.org (Postfix) with ESMTP id B42E0B5DBE3 for ; Fri, 7 Oct 2011 05:26:50 -0300 (ADT) Received: from mail-ey0-f182.google.com ([209.85.215.182]) (using TLSv1) by exprod7ob110.postini.com ([64.18.6.12]) with SMTP; Fri, 07 Oct 2011 01:26:51 PDT Received: by eyz10 with SMTP id 10so2252575eyz.27 for ; Fri, 07 Oct 2011 01:26:48 -0700 (PDT) Received: by 10.223.62.16 with SMTP id v16mr9639193fah.35.1317976007930; Fri, 07 Oct 2011 01:26: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 y8sm11506536faj.10.2011.10.07.01.26.44 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 07 Oct 2011 01:26:46 -0700 (PDT) Message-ID: <4E8EB7C3.3090008@enterprisedb.com> Date: Fri, 07 Oct 2011 11:26:43 +0300 From: Heikki Linnakangas User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; fi-FI; rv:1.9.2.21) Gecko/20110831 Icedove/3.1.13 MIME-Version: 1.0 To: Sergey CC: pgsql-bugs@postgresql.org Subject: Re: BUG #6242: ERROR: unexpected CASE WHEN clause: 333 References: <201110070729.p977Tdcx075504@wwwmaster.postgresql.org> In-Reply-To: <201110070729.p977Tdcx075504@wwwmaster.postgresql.org> Content-Type: multipart/mixed; boundary="------------050207040004040705070908" 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: 201110/58 X-Sequence-Number: 31328 This is a multi-part message in MIME format. --------------050207040004040705070908 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 07.10.2011 10:29, Sergey wrote: > > The following bug has been logged online: > > Bug reference: 6242 > Logged by: Sergey > Email address: sergey-1987@yandex.ru > PostgreSQL version: 8.4.4 > Operating system: FreeBSD 8.1-RELEASE > Description: ERROR: unexpected CASE WHEN clause: 333 > Details: > > create view test_view as > select > case n when null then 1 when 1 then 2 when 2 then 3 end > from ( > values (null), (1), (2) > ) as t(n) > > pg_dump then fail with error: "ERROR: unexpected CASE WHEN clause: 333" but > select * from test_view does not. > > This error occurs with the configuration "transform_null_equals". Hmm, this seems to be similar to the case of deconstructing SQL function inlining in CASE-WHEN constructs we fixed a while ago (http://archives.postgresql.org/pgsql-hackers/2011-05/msg01319.php, last paragraph). On PostgreSQL 8.4.9, you don't get that error, but this: CREATE VIEW test_view AS SELECT CASE t.n WHEN (CASE_TEST_EXPR IS NULL) THEN 1 WHEN 1 THEN 2 WHEN 2 THEN 3 ELSE NULL::integer END AS "case" FROM (VALUES (NULL::integer), (1), (2)) t(n); That's not much better than the error, though, because that will fail on restore. I think the real bug here is that transform_null_equals affects the evaluation of the "n WHEN null" condition at all. The documentation of transform_null_equals says: > Note that this option only affects the exact form = NULL, not other comparison operators or other expressions that are computationally equivalent to some expression involving the equals operator (such as IN). Thus, this option is not a general fix for bad programming. In that CASE construct there was no "exact form = NULL", so transform_null_equals should have had no effect on it. I'm inclined to do a quick fix in transformAExprOp() function to not apply the transformation if either side of the = operation is a CaseTestExpr, per attached patch. We could potentially have the same problem with any "a = NULL" expression created internally in the parser, but I don't see any more instances of that, aside from this construction of WHEN expressions. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com --------------050207040004040705070908 Content-Type: text/x-diff; name="fix-case-when-transform_null_equals-1.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="fix-case-when-transform_null_equals-1.patch" diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 9f68d26..90ff8d8 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -719,12 +719,15 @@ transformAExprOp(ParseState *pstate, A_Expr *a) /* * Special-case "foo = NULL" and "NULL = foo" for compatibility with * standards-broken products (like Microsoft's). Turn these into IS NULL - * exprs. + * exprs. (If either side is a CaseTestExpr, then the expression was + * generated internally from a CASE-WHEN expression, and + * transform_null_equals does not apply.) */ if (Transform_null_equals && list_length(a->name) == 1 && strcmp(strVal(linitial(a->name)), "=") == 0 && - (exprIsNullConstant(lexpr) || exprIsNullConstant(rexpr))) + (exprIsNullConstant(lexpr) || exprIsNullConstant(rexpr)) && + (!IsA(lexpr, CaseTestExpr) && !IsA(rexpr, CaseTestExpr))) { NullTest *n = makeNode(NullTest); --------------050207040004040705070908--