1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-05 07:21:24 +03:00

Disallow partitionwise grouping when collations don't match

If the collation of any grouping column doesn’t match the collation of
the corresponding partition key, partitionwise grouping can yield
incorrect results. For example, rows that would be grouped under the
grouping collation may end up in different partitions under the
partitioning collation. In such cases, full partitionwise grouping
would produce results that differ from those without partitionwise
grouping, so disallowed that.

Partial partitionwise aggregation is still allowed, as the Finalize
step reconciles partition-level aggregates with grouping requirements
across all partitions, ensuring that the final output remains
consistent.

This commit also fixes group_by_has_partkey() by ensuring the
RelabelType node is stripped from grouping expressions when matching
them to partition key expressions to avoid false mismatches.

Bug: #18568
Reported-by: Webbo Han <1105066510@qq.com>
Author: Webbo Han <1105066510@qq.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: Aleksander Alekseev <aleksander@timescale.com>
Reviewed-by: Jian He <jian.universality@gmail.com>
Discussion: https://postgr.es/m/18568-2a9afb6b9f7e6ed3@postgresql.org
Discussion: https://postgr.es/m/tencent_9D9103CDA420C07768349CC1DFF88465F90A@qq.com
Discussion: https://postgr.es/m/CAHewXNno_HKiQ6PqyLYfuqDtwp7KKHZiH1J7Pqyz0nr+PS2Dwg@mail.gmail.com
Backpatch-through: 12
This commit is contained in:
Amit Langote
2024-11-08 16:07:22 +09:00
parent f00ab1fd15
commit 90fe6251c8
3 changed files with 163 additions and 8 deletions

View File

@ -4094,9 +4094,10 @@ create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
* If this is the topmost relation or if the parent relation is doing
* full partitionwise aggregation, then we can do full partitionwise
* aggregation provided that the GROUP BY clause contains all of the
* partitioning columns at this level. Otherwise, we can do at most
* partial partitionwise aggregation. But if partial aggregation is
* not supported in general then we can't use it for partitionwise
* partitioning columns at this level and the collation used by GROUP
* BY matches the partitioning collation. Otherwise, we can do at
* most partial partitionwise aggregation. But if partial aggregation
* is not supported in general then we can't use it for partitionwise
* aggregation either.
*
* Check parse->groupClause not processed_groupClause, because it's
@ -8105,8 +8106,8 @@ create_partitionwise_grouping_paths(PlannerInfo *root,
/*
* group_by_has_partkey
*
* Returns true, if all the partition keys of the given relation are part of
* the GROUP BY clauses, false otherwise.
* Returns true if all the partition keys of the given relation are part of
* the GROUP BY clauses, including having matching collation, false otherwise.
*/
static bool
group_by_has_partkey(RelOptInfo *input_rel,
@ -8134,13 +8135,40 @@ group_by_has_partkey(RelOptInfo *input_rel,
foreach(lc, partexprs)
{
ListCell *lg;
Expr *partexpr = lfirst(lc);
Oid partcoll = input_rel->part_scheme->partcollation[cnt];
if (list_member(groupexprs, partexpr))
foreach(lg, groupexprs)
{
found = true;
break;
Expr *groupexpr = lfirst(lg);
Oid groupcoll = exprCollation((Node *) groupexpr);
/*
* Note: we can assume there is at most one RelabelType node;
* eval_const_expressions() will have simplified if more than
* one.
*/
if (IsA(groupexpr, RelabelType))
groupexpr = ((RelabelType *) groupexpr)->arg;
if (equal(groupexpr, partexpr))
{
/*
* Reject a match if the grouping collation does not match
* the partitioning collation.
*/
if (OidIsValid(partcoll) && OidIsValid(groupcoll) &&
partcoll != groupcoll)
return false;
found = true;
break;
}
}
if (found)
break;
}
/*