pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andrew Gierth <andrew@tao11.riddles.org.uk>
Cc: Michael Paquier <michael.paquier@gmail.com>
Cc: Heikki Linnakangas <hlinnaka@iki.fi>
Cc: Pg Bugs <pgsql-bugs@postgresql.org>
Subject: Re: [BUGS] Improper const-evaluation of HAVING with grouping sets and subquery pullup
Date: Fri, 12 Jan 2018 12:45:01 -0500
Message-ID: <635.1515779101@sss.pgh.pa.us> (raw)
In-Reply-To: <22850.1515710814@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>
	<aefc657e-edb2-64d5-6df1-a0828f6e9104@iki.fi>
	<87tvyyjg2s.fsf@news-spur.riddles.org.uk>
	<26650.1508339040@sss.pgh.pa.us>
	<17920.1508341358@sss.pgh.pa.us>
	<20857.1508959748@sss.pgh.pa.us>
	<CAB7nPqQ0w228xyjCUg0zPM0eHVNApUXmxtx1RQ4w+XPZUrgTnQ@mail.gmail.com>
	<87lgipwpp2.fsf@news-spur.riddles.org.uk>
	<28755.1511965769@sss.pgh.pa.us>
	<22850.1515710814@sss.pgh.pa.us>

I wrote:
> I think the attached is probably ready to go, though I've not checked yet
> whether it will work pre-9.6.  Anyone want to do more review?

I pushed this back through 9.5.  Oddly, the test case Andrew proposed,

select (x+y)*1, sum(z)
 from (select 1 as x, 2 as y, 3 as z) s
 group by grouping sets (x+y, x);

passes in unmodified 9.5.  I haven't bothered to try to figure out
exactly why the difference in behavior; I have a feeling it's related
to the upper planner pathification changes in 9.6.

However ... we're not out of the woods yet.  Shortly after pushing
it occurred to me that the basic problem, const-simplification of
expressions whose subexpressions should match grouping set expressions,
can happen without any subquery at all.  For instance:

regression=# select q1=q2, not(q1=q2), q1 from int8_tbl
group by grouping sets(q1=q2, q1);
 ?column? | ?column? |        q1        
----------+----------+------------------
 f        |          |                 
 t        |          |                 
          | t        |              123
          | t        | 4567890123456789
(4 rows)

Surely that's wrong.

So, IMO, basically what this shows is that we have to identify and replace
subexpressions that should match grouping set expressions before the
planner starts to do expression processing, rather than just leaving it to
accidentally happen in setrefs.c.  I still like the idea of representing
such subexpressions as Vars referencing a dummy RTE, but that seems
unlikely to lead to a back-patchable fix.  However, I'm not sure that any
back-patchable fix would be practical anyway, so maybe we should just
accept that this is going to stay broken in the back branches.  At least
it doesn't arise for spec-compliant queries.

			regards, tom lane




view thread (21+ messages)

Message-ID: <635.1515779101@sss.pgh.pa.us>
Permalink:  ../635.1515779101@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/635.1515779101@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, andrew@tao11.riddles.org.uk, michael.paquier@gmail.com, hlinnaka@iki.fi
  Subject: Re: [BUGS] Improper const-evaluation of HAVING with grouping sets and subquery pullup
  In-Reply-To: <635.1515779101@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> <aefc657e-edb2-64d5-6df1-a0828f6e9104@iki.fi> <87tvyyjg2s.fsf@news-spur.riddles.org.uk> <26650.1508339040@sss.pgh.pa.us> <17920.1508341358@sss.pgh.pa.us> <20857.1508959748@sss.pgh.pa.us> <CAB7nPqQ0w228xyjCUg0zPM0eHVNApUXmxtx1RQ4w+XPZUrgTnQ@mail.gmail.com> <87lgipwpp2.fsf@news-spur.riddles.org.uk> <28755.1511965769@sss.pgh.pa.us> <22850.1515710814@sss.pgh.pa.us> <635.1515779101@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox