pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
回复:Re: Incorrect sort result caused by ROLLUP and WHERE operation
2+ messages / 1 participants
[nested] [flat]

* 回复:Re: Incorrect sort result caused by ROLLUP and WHERE operation
@ 2025-01-03 01:12 谭忠涛 <zhongtao.tan@seaboxdata.com>
  0 siblings, 0 replies; 2+ messages in thread

From: 谭忠涛 @ 2025-01-03 01:12 UTC (permalink / raw)
  To: Zhang Mingli <zmlpostgres@gmail.com>; pgsql-bugs <pgsql-bugs@lists.postgresql.org>

"Where a = 1" generates a "Equivalenceclass" containing "a" and "1" ,
but in "grouping sets", a maybe generate nulls, so don't generate a "Equivalenceclass"
when a is not in all subsets of grouping sets.
select a,b,sum(10) as s from t1 where a = 1 group by grouping sets((a,b),(a)) order by a, s desc; -- ok,can generate Equivalenceclass
select a,b,sum(10) as s from t1 where a = 1 group by grouping sets((a,b),(b)) order by a, s desc; -- error, can't generate Equivalenceclass


原谅我蹩脚的英文,我们分析了原因,大致就是“a=1”这个条件生成等价类了,把a当成1处理了,但是实际上在带有grouping sets的sql中,
如果grouping sets的子集中有不包含a的group项,则a会被替换成null值,因此结果会错误;
我们的修改方案是生成等价类的时候,判断一下a是否在所有的grouping sets的子集都存在,如果有不存在的则不生产等价类。


谢谢。


Regards.




                    Zhang Mingli<zmlpostgres@gmail.com> 在 2025年1月2日 周四 18:50 写道:

  Hi,
 
 
  

 Zhang Mingli www.hashdata.xyz

 

 Do you mean order of null value (column a)  is wrong?
 Use NULLS FIRST/LAST to order null values.
 
 
 On Jan 2, 2025 at 18:19 +0800, 谭忠涛 <zhongtao.tan@seaboxdata.com>, wrote:
 
 Case:
 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;

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* 回复:Re: Incorrect sort result caused by ROLLUP and WHERE operation
@ 2025-01-03 01:17 谭忠涛 <zhongtao.tan@seaboxdata.com>
  0 siblings, 0 replies; 2+ messages in thread

From: 谭忠涛 @ 2025-01-03 01:17 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>

Thank you for your feedback.


regards


                    Tom Lane<tgl@sss.pgh.pa.us> 在 2025年1月3日 周五 0:04 写道:

"=?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

^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2025-01-03 01:17 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-01-03 01:12 回复:Re: Incorrect sort result caused by ROLLUP and WHERE operation 谭忠涛 <zhongtao.tan@seaboxdata.com>
2025-01-03 01:17 回复:Re: Incorrect sort result caused by ROLLUP and WHERE operation 谭忠涛 <zhongtao.tan@seaboxdata.com>

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