回复: BUG #18568: BUG: Result wrong when do group by on partition table!
狂奔的蜗牛 <1105066510@qq.com>
From: 狂奔的蜗牛 <1105066510@qq.com>
To: Tender Wang <tndrwang@gmail.com>
Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>, aleksander <aleksander@timescale.com>
Date: 2024-08-07T08:06:15Z
Lists: pgsql-bugs
PartCollMatchesExprColl() is not strict enough. If partcoll == InvalidOid and groupcoll != InvalidOid, PartCollMatchesExprColl() return true always. Just determine whether groupcoll is equal to partcoll, like this "partcoll == groupcoll". We cannot delete "if (IsA(groupexpr, RelableTyple) " branch, becasuse if groupexpr is RelabelType and partcoll equal to groupcoll, the "equal(groupexpr, partexpr) && PartKeyCollMatchesExprColl(partcoll, groupexpr_coll)" condition return false!!! This is not what we expect. We could rename "groupexpr_coll" to groupcoll, it looks more elegant. 狂奔的蜗牛 1105066510@qq.com ------------------ 原始邮件 ------------------ 发件人: "Tender Wang" <tndrwang@gmail.com>; 发送时间: 2024年8月7日(星期三) 下午2:57 收件人: "狂奔的蜗牛"<1105066510@qq.com>; 抄送: "pgsql-bugs"<pgsql-bugs@lists.postgresql.org>;"aleksander"<aleksander@timescale.com>; 主题: Re: BUG #18568: BUG: Result wrong when do group by on partition table! 狂奔的蜗牛 <1105066510@qq.com> 于2024年8月7日周三 13:35写道: this case will enter `if (IsA(groupexpr, RelabelType))` branch. We set "C" as groupkey's collation, and it's not equal to column's. In transformGroupClause(), syntax `COLLATE xxx` will create CollateExpr node, and CollateExpr's arg is Var node. And then, planner will call eval_const_expressions_mutator() to transform CollateExpr to RelableType if CollateExpr->collOid not equal to Collate->arg's collation. Yech. Thanks for the explanation. Because exprCollation() return the resultcollid if the nodetag is Relabletype, the expr() && PartKeyCollMatchesExprColl() can handle this situation. So we don't need "if (IsA(groupexpr, RelableTyple) " this branch. About V3 patch, PartCollMatchesExprColl() may be not suitable, because collation of partkey must be equal to groupkey's, even though they are all InvalidOid. I'm not sure about what you said. I uesd it because partition prune do this way. I keep it temporarily. The test case in v3 patch only has EXPLAIN statement. I add SELECT statement and make the result more stable according to Aleksander advices. -- Tender Wang
Commits
-
Disallow partitionwise grouping when collations don't match
- 90fe6251c816 18.0 landed
- b6484ca9535e 17.1 landed
- dd2f8ebee221 16.5 landed
- 0a620659c549 15.9 landed
- 96f9b29a3e1e 14.14 landed
- ff65f695c0d3 13.17 landed
- 46d9be5efb1a 12.21 landed
-
For partitionwise join, match on partcollation, not parttypcoll.
- 2af28e603319 11.0 cited