pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Heikki Linnakangas <hlinnaka@iki.fi>
Cc: Pg Bugs <pgsql-bugs@postgresql.org>
Subject: Re: Improper const-evaluation of HAVING with grouping sets and subquery pullup
Date: Thu, 12 Oct 2017 11:06:27 -0400
Message-ID: <1197.1507820787@sss.pgh.pa.us> (raw)
In-Reply-To: <7dbdcf5c-b5a6-ef89-4958-da212fe10176@iki.fi>
References: <7dbdcf5c-b5a6-ef89-4958-da212fe10176@iki.fi>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Heikki Linnakangas <hlinnaka@iki.fi> writes:
> This query produces an incorrect result:
> regression=# select four, x
>    from (select four, ten, 'foo'::text as x from tenk1 ) as t
>    group by grouping sets(four, x) having x = 'foo' order by four;

> The "having x = 'foo'" clause should've filtered out the rows where x is 
> NULL, leaving only the last row as the result. Even though x is a 
> constant 'foo' in the subquery, HAVING clause is supposed to be 
> evaluated after grouping. What happens is that subquery pullup replaces 
> x with the constant, and the "'foo' = 'foo'" qual is later 
> const-evaluated to true.

Ouch.

> I propose the attached patch to fix that. It forces the use of 
> PlaceHolderVars in subquery pullup, if the parent query has grouping 
> sets and HAVING. I'm not 100% sure that's the right approach or a misuse 
> of the placeholder system, so comments welcome.

Seems like the point is that grouping sets can inject null values of
columns, in more or less the same way that outer joins can.  So it
seems like using PlaceHolderVars, which were invented to account
for that property of outer joins, is a reasonable approach to a fix.
I don't have time to review the patch in detail right now though;
do you want to put it in the CF queue?

One thing I'm wondering is why only the HAVING clause would be subject
to the problem.  I'm a bit surprised that the "x" in the targetlist
didn't become a constant as well.  This may be pointing to some
klugery in the GROUPING SETS patch that we could clean up if we
use placeholders for this.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (21+ messages)  latest in thread

Message-ID: <1197.1507820787@sss.pgh.pa.us>
Permalink:  ../1197.1507820787@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1197.1507820787@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, hlinnaka@iki.fi
  Subject: Re: Improper const-evaluation of HAVING with grouping sets and subquery pullup
  In-Reply-To: <1197.1507820787@sss.pgh.pa.us>
  References: <7dbdcf5c-b5a6-ef89-4958-da212fe10176@iki.fi> <1197.1507820787@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