pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedIncorrect sort result caused by ROLLUP and WHERE operation
3+ messages / 3 participants
[nested] [flat]
* Incorrect sort result caused by ROLLUP and WHERE operation
@ 2025-01-02 07:28 谭忠涛 <zhongtao.tan@seaboxdata.com>
2025-01-02 10:49 ` Re: Incorrect sort result caused by ROLLUP and WHERE operation Zhang Mingli <zmlpostgres@gmail.com>
2025-01-02 16:04 ` Re: Incorrect sort result caused by ROLLUP and WHERE operation Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 3+ messages in thread
From: 谭忠涛 @ 2025-01-02 07:28 UTC (permalink / raw)
To: pgsql-bugs <pgsql-bugs@lists.postgresql.org>; +Cc: 谭忠涛 <zhongtao.tan@seaboxdata.com>
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;
Regards
谭忠涛 数据研究院数据库事业部
北京东方金信科技股份有限公司
地址:海淀区知春路7号致真大厦 D 座 14 层
路线:地铁10号线-西土城站-A口
手机:13920129934
公众号:东方金信
网址:www.seaboxdata.com
关于东方金信
北京东方金信科技股份有限公司成立于2013年2月。公司拥有ISO9001、 ISO27001和CMMI5级认证;中国大数据企业50强;首批通过工信部数据中心联盟大数据基础能力和性能双认证;首个国家信息中心“数据资产研究中心”授牌单位;全国信标委智慧城市及大数据标准工作组成员,2017年入选Gartner全球大数据行业和人工智能报告;2019年与腾讯云成为战略合作伙伴并获腾讯数亿元投资;2020年获中金、海淀区国资委入股。
Attachments:
B0DB84E8@BAB2C14C.1940766700000000.png (application/octet-stream, 190.0K) download
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Incorrect sort result caused by ROLLUP and WHERE operation
2025-01-02 07:28 Incorrect sort result caused by ROLLUP and WHERE operation 谭忠涛 <zhongtao.tan@seaboxdata.com>
@ 2025-01-02 10:49 ` Zhang Mingli <zmlpostgres@gmail.com>
1 sibling, 0 replies; 3+ messages in thread
From: Zhang Mingli @ 2025-01-02 10:49 UTC (permalink / raw)
To: pgsql-bugs <pgsql-bugs@lists.postgresql.org>; 谭忠涛 <zhongtao.tan@seaboxdata.com>
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] 3+ messages in thread
* Re: Incorrect sort result caused by ROLLUP and WHERE operation
2025-01-02 07:28 Incorrect sort result caused by ROLLUP and WHERE operation 谭忠涛 <zhongtao.tan@seaboxdata.com>
@ 2025-01-02 16:04 ` Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 3+ messages in thread
From: Tom Lane @ 2025-01-02 16:04 UTC (permalink / raw)
To: 谭忠涛 <zhongtao.tan@seaboxdata.com>; +Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>
"=?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] 3+ messages in thread
end of thread, other threads:[~2025-01-02 16:04 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-01-02 07:28 Incorrect sort result caused by ROLLUP and WHERE operation 谭忠涛 <zhongtao.tan@seaboxdata.com>
2025-01-02 10:49 ` Zhang Mingli <zmlpostgres@gmail.com>
2025-01-02 16:04 ` Tom Lane <tgl@sss.pgh.pa.us>
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