回复:Re: Incorrect sort result caused by ROLLUP and WHERE operation

From: 谭忠涛 <zhongtao(dot)tan(at)seaboxdata(dot)com>
To: Zhang Mingli <zmlpostgres(at)gmail(dot)com>, pgsql-bugs <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: 回复:Re: Incorrect sort result caused by ROLLUP and WHERE operation
Date: 2025-01-03 01:12:03
Message-ID: tencent_6C58DF4466355D9E12876406@qq.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

"Where a = 1" generates a "Equivalenceclass" containing "a" and "1" ,
but in "grouping sets", a maybe generate nulls, so don't generate&nbsp;a "Equivalenceclass"
when a is not in all subsets of grouping sets.
select&nbsp;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&nbsp;Equivalenceclass
select&nbsp;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&nbsp;generate&nbsp;Equivalenceclass

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

谢谢。

Regards.

Zhang Mingli<zmlpostgres(at)gmail(dot)com&gt;&nbsp;在 2025年1月2日 周四 18:50 写道:

Hi,


Zhang Mingli www.hashdata.xyz

Do you mean order of null value (column a) &nbsp;is wrong?
Use&nbsp;NULLS FIRST/LAST to order null values.


On Jan 2, 2025 at 18:19 +0800, 谭忠涛 <zhongtao(dot)tan(at)seaboxdata(dot)com&gt;, 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;

Browse pgsql-bugs by date

  From Date Subject
Next Message 谭忠涛 2025-01-03 01:17:58 回复:Re: Incorrect sort result caused by ROLLUP and WHERE operation
Previous Message Tomas Vondra 2025-01-03 00:25:54 Re: BRIN index creation on geometry column causes crash