Re: Incorrect sort result caused by ROLLUP and WHERE operation - Mailing list pgsql-bugs

From Tom Lane
Subject Re: Incorrect sort result caused by ROLLUP and WHERE operation
Date
Msg-id 270213.1735833863@sss.pgh.pa.us
Whole thread Raw
List pgsql-bugs
"=?utf-8?B?6LCt5b+g5rab?=" <zhongtao.tan@seaboxdata.com> writes:
> create table t1(a int, b int);
> insert into t1 values(1,1),(1,2);
> select a,b,sum(10) as s from t1 where a = 1 group by rollup(a,b) order by a, s desc;

Yeah.  This is fixed in HEAD (v18-to-be) but the fix is too invasive
to consider back-patching.  Basically the problem is that older
versions don't understand that the post-ROLLUP value of "a" can be
different from the pre-ROLLUP value, so they think the "where a = 1"
clause removes any need to sort by "a".

As a workaround you could write something like "order by a+0, s desc"
to fool the optimizer into considering the ordering column to be
different from the value that's constrained by WHERE.

            regards, tom lane



pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: AW: Commit 5a2fed911a broke parallel query
Next
From: David Rowley
Date:
Subject: Re: BRIN index creation on geometry column causes crash