pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: hubert depesz lubaczewski <depesz@depesz.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: tobias.wendorff@tu-dortmund.de
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #17071: ORDER BY gets ignored when result set has only one row, but another one gets added by rollup()
Date: Thu, 24 Jun 2021 08:23:34 +0200
Message-ID: <20210624062334.GA3131@depesz.com> (raw)
In-Reply-To: <2350897.1624476008@sss.pgh.pa.us>
References: <17071-24dc13fbfa29672d@postgresql.org>
	<2350897.1624476008@sss.pgh.pa.us>

On Wed, Jun 23, 2021 at 03:20:08PM -0400, Tom Lane wrote:
> PG Bug reporting form <noreply@postgresql.org> writes:
> > SELECT
> >     '2021-01-01'::date AS month
> > GROUP BY
> >     rollup(month)
> > ORDER BY
> >     month NULLS FIRST;
> > [ produces unsorted output ]
> 
> Hm, that's certainly a bug, but so far as I can tell it's specific to the
> case of a constant value being used as the GROUP BY/ORDER BY target.
> Which doesn't seem very likely to be interesting in practice.  Do you have
> a non-toy example where things go wrong?
> 
> The issue here is that ORDER BY a constant is normally deemed to be a
> no-op.  Our parse representation fails to make it clear that in this
> situation, the "constant" column isn't so constant after the GROUP BY has
> been applied.  There's been some discussion of changing that, but it's a
> large task and isn't likely to happen overnight (much less be a plausible
> candidate for back-patching).  So I'm wondering if this was reduced from
> a more realistic example that we might be able to fix in some other way.

Well, the problem happens only when there is only one row, so I'd say
all examples are kinda toy-examples.

But - it can be reproduced using:

$ select relkind, count(*) from pg_class where relkind = 'r' group by rollup(relkind) order by relkind nulls first;
 relkind │ count 
─────────┼───────
 r       │    68
 [null]  │    68
(2 rows)

if there are more rows, nulls got to proper position:

$ select relkind, count(*) from pg_class where relkind in( 'r', 'i') group by rollup(relkind) order by relkind nulls first;
 relkind │ count 
─────────┼───────
 [null]  │   227
 i       │   159
 r       │    68
(3 rows)

Best regards,

depesz






view thread (5+ messages)

Message-ID: <20210624062334.GA3131@depesz.com>
Permalink:  ../20210624062334.GA3131@depesz.com/
Also on:    postgresql.org/message-id/20210624062334.GA3131@depesz.com

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: depesz@depesz.com, tgl@sss.pgh.pa.us, tobias.wendorff@tu-dortmund.de, pgsql-bugs@lists.postgresql.org
  Subject: Re: BUG #17071: ORDER BY gets ignored when result set has only one row, but another one gets added by rollup()
  In-Reply-To: <20210624062334.GA3131@depesz.com>

* 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