Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e4qUd-0005qN-2P for pgsql-bugs@arkaria.postgresql.org; Wed, 18 Oct 2017 15:42:51 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e4qUc-0005Ei-Jv for pgsql-bugs@arkaria.postgresql.org; Wed, 18 Oct 2017 15:42:50 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e4qUb-0005Dh-O0 for pgsql-bugs@postgresql.org; Wed, 18 Oct 2017 15:42:49 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e4qUZ-0004EB-8o for pgsql-bugs@postgresql.org; Wed, 18 Oct 2017 15:42:48 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v9IFgcl7017921; Wed, 18 Oct 2017 11:42:38 -0400 From: Tom Lane To: Andrew Gierth cc: Heikki Linnakangas , Pg Bugs Subject: Re: Improper const-evaluation of HAVING with grouping sets and subquery pullup In-reply-to: <26650.1508339040@sss.pgh.pa.us> References: <7dbdcf5c-b5a6-ef89-4958-da212fe10176@iki.fi> <1197.1507820787@sss.pgh.pa.us> <87efq3lq93.fsf@news-spur.riddles.org.uk> <87a80rli2w.fsf@news-spur.riddles.org.uk> <32319.1508163874@sss.pgh.pa.us> <8760bfku2u.fsf@news-spur.riddles.org.uk> <2431.1508166918@sss.pgh.pa.us> <871sm3ktl7.fsf@news-spur.riddles.org.uk> <87tvyyjg2s.fsf@news-spur.riddles.org.uk> <26650.1508339040@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Wed, 18 Oct 2017 11:04:00 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <17890.1508341312.0@sss.pgh.pa.us> Date: Wed, 18 Oct 2017 11:42:38 -0400 Message-ID: <17920.1508341358@sss.pgh.pa.us> List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <17890.1508341312.1@sss.pgh.pa.us> I wrote: > I think that Heikki's expectation is the correct one, and the reason the > output looks the way it does is that setrefs.c is dropping the ball > somehow and confusing the two "g" references. ... > We should have used ressortgroupref matching to prevent this, but without > having checked the code right now, I think that we might only apply that > logic to non-Var tlist entries. If the Agg output tlist had mentioned > column 2 not column 1 of the child node, I bet we'd get the right answer. Indeed, the attached patch passes all regression tests and produces the same answers for both of Heikki's examples: regression=# SELECT g as newalias1, g as newalias3 FROM generate_series(1,3) g GROUP BY newalias1, ROLLUP(newalias3); newalias1 | newalias3 -----------+----------- 1 | 1 3 | 3 2 | 2 2 | 3 | 1 | (6 rows) regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="check-sortgroupref-for-Vars-too.patch"; charset="us-ascii" Content-ID: <17890.1508341312.2@sss.pgh.pa.us> Content-Description: check-sortgroupref-for-Vars-too.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/p= lan/setrefs.c index 1382b67..9aeaf87 100644 *** a/src/backend/optimizer/plan/setrefs.c --- b/src/backend/optimizer/plan/setrefs.c *************** set_upper_references(PlannerInfo *root,=20 *** 1744,1751 **** TargetEntry *tle =3D (TargetEntry *) lfirst(l); Node *newexpr; =20=20 ! /* If it's a non-Var sort/group item, first try to match by sortref */ ! if (tle->ressortgroupref !=3D 0 && !IsA(tle->expr, Var)) { newexpr =3D (Node *) search_indexed_tlist_for_sortgroupref(tle->expr, --- 1744,1751 ---- TargetEntry *tle =3D (TargetEntry *) lfirst(l); Node *newexpr; =20=20 ! /* If it's a sort/group item, first try to match by sortref */ ! if (tle->ressortgroupref !=3D 0) { newexpr =3D (Node *) search_indexed_tlist_for_sortgroupref(tle->expr, *************** search_indexed_tlist_for_non_var(Expr *n *** 2113,2119 **** =20=20 /* * search_indexed_tlist_for_sortgroupref --- find a sort/group expression - * (which is assumed not to be just a Var) * * If a match is found, return a Var constructed to reference the tlist i= tem. * If no match, return NULL. --- 2113,2118 ---- ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--