Skip to content

Commit 90fe625

Browse files
committed
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
1 parent f00ab1f commit 90fe625

File tree

3 files changed

+163
-8
lines changed

3 files changed

+163
-8
lines changed

src/backend/optimizer/plan/planner.c

+36-8
Original file line numberDiff line numberDiff line change
@@ -4094,9 +4094,10 @@ create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
40944094
* If this is the topmost relation or if the parent relation is doing
40954095
* full partitionwise aggregation, then we can do full partitionwise
40964096
* aggregation provided that the GROUP BY clause contains all of the
4097-
* partitioning columns at this level. Otherwise, we can do at most
4098-
* partial partitionwise aggregation. But if partial aggregation is
4099-
* not supported in general then we can't use it for partitionwise
4097+
* partitioning columns at this level and the collation used by GROUP
4098+
* BY matches the partitioning collation. Otherwise, we can do at
4099+
* most partial partitionwise aggregation. But if partial aggregation
4100+
* is not supported in general then we can't use it for partitionwise
41004101
* aggregation either.
41014102
*
41024103
* Check parse->groupClause not processed_groupClause, because it's
@@ -8105,8 +8106,8 @@ create_partitionwise_grouping_paths(PlannerInfo *root,
81058106
/*
81068107
* group_by_has_partkey
81078108
*
8108-
* Returns true, if all the partition keys of the given relation are part of
8109-
* the GROUP BY clauses, false otherwise.
8109+
* Returns true if all the partition keys of the given relation are part of
8110+
* the GROUP BY clauses, including having matching collation, false otherwise.
81108111
*/
81118112
static bool
81128113
group_by_has_partkey(RelOptInfo *input_rel,
@@ -8134,13 +8135,40 @@ group_by_has_partkey(RelOptInfo *input_rel,
81348135

81358136
foreach(lc, partexprs)
81368137
{
8138+
ListCell *lg;
81378139
Expr *partexpr = lfirst(lc);
8140+
Oid partcoll = input_rel->part_scheme->partcollation[cnt];
81388141

8139-
if (list_member(groupexprs, partexpr))
8142+
foreach(lg, groupexprs)
81408143
{
8141-
found = true;
8142-
break;
8144+
Expr *groupexpr = lfirst(lg);
8145+
Oid groupcoll = exprCollation((Node *) groupexpr);
8146+
8147+
/*
8148+
* Note: we can assume there is at most one RelabelType node;
8149+
* eval_const_expressions() will have simplified if more than
8150+
* one.
8151+
*/
8152+
if (IsA(groupexpr, RelabelType))
8153+
groupexpr = ((RelabelType *) groupexpr)->arg;
8154+
8155+
if (equal(groupexpr, partexpr))
8156+
{
8157+
/*
8158+
* Reject a match if the grouping collation does not match
8159+
* the partitioning collation.
8160+
*/
8161+
if (OidIsValid(partcoll) && OidIsValid(groupcoll) &&
8162+
partcoll != groupcoll)
8163+
return false;
8164+
8165+
found = true;
8166+
break;
8167+
}
81438168
}
8169+
8170+
if (found)
8171+
break;
81448172
}
81458173

81468174
/*

src/test/regress/expected/collate.icu.utf8.out

+90
Original file line numberDiff line numberDiff line change
@@ -2054,6 +2054,96 @@ SELECT (SELECT count(*) FROM test33_0) <> (SELECT count(*) FROM test33_1);
20542054
t
20552055
(1 row)
20562056

2057+
--
2058+
-- Bug #18568
2059+
--
2060+
-- Partitionwise aggregate (full or partial) should not be used when a
2061+
-- partition key's collation doesn't match that of the GROUP BY column it is
2062+
-- matched with.
2063+
SET max_parallel_workers_per_gather TO 0;
2064+
SET enable_incremental_sort TO off;
2065+
CREATE TABLE pagg_tab3 (a text, c text collate case_insensitive) PARTITION BY LIST(c collate "C");
2066+
CREATE TABLE pagg_tab3_p1 PARTITION OF pagg_tab3 FOR VALUES IN ('a', 'b');
2067+
CREATE TABLE pagg_tab3_p2 PARTITION OF pagg_tab3 FOR VALUES IN ('B', 'A');
2068+
INSERT INTO pagg_tab3 SELECT i % 4 + 1, substr('abAB', (i % 4) + 1 , 1) FROM generate_series(0, 19) i;
2069+
ANALYZE pagg_tab3;
2070+
SET enable_partitionwise_aggregate TO false;
2071+
EXPLAIN (COSTS OFF)
2072+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
2073+
QUERY PLAN
2074+
-----------------------------------------------------------
2075+
Sort
2076+
Sort Key: (upper(pagg_tab3.c)) COLLATE case_insensitive
2077+
-> HashAggregate
2078+
Group Key: pagg_tab3.c
2079+
-> Append
2080+
-> Seq Scan on pagg_tab3_p2 pagg_tab3_1
2081+
-> Seq Scan on pagg_tab3_p1 pagg_tab3_2
2082+
(7 rows)
2083+
2084+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
2085+
upper | count
2086+
-------+-------
2087+
A | 10
2088+
B | 10
2089+
(2 rows)
2090+
2091+
-- No "full" partitionwise aggregation allowed, though "partial" is allowed.
2092+
SET enable_partitionwise_aggregate TO true;
2093+
EXPLAIN (COSTS OFF)
2094+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
2095+
QUERY PLAN
2096+
--------------------------------------------------------------
2097+
Sort
2098+
Sort Key: (upper(pagg_tab3.c)) COLLATE case_insensitive
2099+
-> Finalize HashAggregate
2100+
Group Key: pagg_tab3.c
2101+
-> Append
2102+
-> Partial HashAggregate
2103+
Group Key: pagg_tab3.c
2104+
-> Seq Scan on pagg_tab3_p2 pagg_tab3
2105+
-> Partial HashAggregate
2106+
Group Key: pagg_tab3_1.c
2107+
-> Seq Scan on pagg_tab3_p1 pagg_tab3_1
2108+
(11 rows)
2109+
2110+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
2111+
upper | count
2112+
-------+-------
2113+
A | 10
2114+
B | 10
2115+
(2 rows)
2116+
2117+
-- OK to use full partitionwise aggregate after changing the GROUP BY column's
2118+
-- collation to be the same as that of the partition key.
2119+
EXPLAIN (COSTS OFF)
2120+
SELECT c collate "C", count(c) FROM pagg_tab3 GROUP BY c collate "C" ORDER BY 1;
2121+
QUERY PLAN
2122+
--------------------------------------------------------
2123+
Sort
2124+
Sort Key: ((pagg_tab3.c)::text) COLLATE "C"
2125+
-> Append
2126+
-> HashAggregate
2127+
Group Key: (pagg_tab3.c)::text
2128+
-> Seq Scan on pagg_tab3_p2 pagg_tab3
2129+
-> HashAggregate
2130+
Group Key: (pagg_tab3_1.c)::text
2131+
-> Seq Scan on pagg_tab3_p1 pagg_tab3_1
2132+
(9 rows)
2133+
2134+
SELECT c collate "C", count(c) FROM pagg_tab3 GROUP BY c collate "C" ORDER BY 1;
2135+
c | count
2136+
---+-------
2137+
A | 5
2138+
B | 5
2139+
a | 5
2140+
b | 5
2141+
(4 rows)
2142+
2143+
DROP TABLE pagg_tab3;
2144+
RESET enable_partitionwise_aggregate;
2145+
RESET max_parallel_workers_per_gather;
2146+
RESET enable_incremental_sort;
20572147
-- cleanup
20582148
RESET search_path;
20592149
SET client_min_messages TO warning;

src/test/regress/sql/collate.icu.utf8.sql

+37
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,43 @@ INSERT INTO test33 VALUES (2, 'DEF');
796796
-- they end up in the same partition (but it's platform-dependent which one)
797797
SELECT (SELECT count(*) FROM test33_0) <> (SELECT count(*) FROM test33_1);
798798

799+
--
800+
-- Bug #18568
801+
--
802+
-- Partitionwise aggregate (full or partial) should not be used when a
803+
-- partition key's collation doesn't match that of the GROUP BY column it is
804+
-- matched with.
805+
SET max_parallel_workers_per_gather TO 0;
806+
SET enable_incremental_sort TO off;
807+
808+
CREATE TABLE pagg_tab3 (a text, c text collate case_insensitive) PARTITION BY LIST(c collate "C");
809+
CREATE TABLE pagg_tab3_p1 PARTITION OF pagg_tab3 FOR VALUES IN ('a', 'b');
810+
CREATE TABLE pagg_tab3_p2 PARTITION OF pagg_tab3 FOR VALUES IN ('B', 'A');
811+
INSERT INTO pagg_tab3 SELECT i % 4 + 1, substr('abAB', (i % 4) + 1 , 1) FROM generate_series(0, 19) i;
812+
ANALYZE pagg_tab3;
813+
814+
SET enable_partitionwise_aggregate TO false;
815+
EXPLAIN (COSTS OFF)
816+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
817+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
818+
819+
-- No "full" partitionwise aggregation allowed, though "partial" is allowed.
820+
SET enable_partitionwise_aggregate TO true;
821+
EXPLAIN (COSTS OFF)
822+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
823+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
824+
825+
-- OK to use full partitionwise aggregate after changing the GROUP BY column's
826+
-- collation to be the same as that of the partition key.
827+
EXPLAIN (COSTS OFF)
828+
SELECT c collate "C", count(c) FROM pagg_tab3 GROUP BY c collate "C" ORDER BY 1;
829+
SELECT c collate "C", count(c) FROM pagg_tab3 GROUP BY c collate "C" ORDER BY 1;
830+
831+
DROP TABLE pagg_tab3;
832+
833+
RESET enable_partitionwise_aggregate;
834+
RESET max_parallel_workers_per_gather;
835+
RESET enable_incremental_sort;
799836

800837
-- cleanup
801838
RESET search_path;

0 commit comments

Comments
 (0)