Skip to content

Commit ff65f69

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 ebbfa2a commit ff65f69

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
@@ -4082,9 +4082,10 @@ create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
40824082
* If this is the topmost relation or if the parent relation is doing
40834083
* full partitionwise aggregation, then we can do full partitionwise
40844084
* aggregation provided that the GROUP BY clause contains all of the
4085-
* partitioning columns at this level. Otherwise, we can do at most
4086-
* partial partitionwise aggregation. But if partial aggregation is
4087-
* not supported in general then we can't use it for partitionwise
4085+
* partitioning columns at this level and the collation used by GROUP
4086+
* BY matches the partitioning collation. Otherwise, we can do at
4087+
* most partial partitionwise aggregation. But if partial aggregation
4088+
* is not supported in general then we can't use it for partitionwise
40884089
* aggregation either.
40894090
*/
40904091
if (extra->patype == PARTITIONWISE_AGGREGATE_FULL &&
@@ -7721,8 +7722,8 @@ create_partitionwise_grouping_paths(PlannerInfo *root,
77217722
/*
77227723
* group_by_has_partkey
77237724
*
7724-
* Returns true, if all the partition keys of the given relation are part of
7725-
* the GROUP BY clauses, false otherwise.
7725+
* Returns true if all the partition keys of the given relation are part of
7726+
* the GROUP BY clauses, including having matching collation, false otherwise.
77267727
*/
77277728
static bool
77287729
group_by_has_partkey(RelOptInfo *input_rel,
@@ -7750,13 +7751,40 @@ group_by_has_partkey(RelOptInfo *input_rel,
77507751

77517752
foreach(lc, partexprs)
77527753
{
7754+
ListCell *lg;
77537755
Expr *partexpr = lfirst(lc);
7756+
Oid partcoll = input_rel->part_scheme->partcollation[cnt];
77547757

7755-
if (list_member(groupexprs, partexpr))
7758+
foreach(lg, groupexprs)
77567759
{
7757-
found = true;
7758-
break;
7760+
Expr *groupexpr = lfirst(lg);
7761+
Oid groupcoll = exprCollation((Node *) groupexpr);
7762+
7763+
/*
7764+
* Note: we can assume there is at most one RelabelType node;
7765+
* eval_const_expressions() will have simplified if more than
7766+
* one.
7767+
*/
7768+
if (IsA(groupexpr, RelabelType))
7769+
groupexpr = ((RelabelType *) groupexpr)->arg;
7770+
7771+
if (equal(groupexpr, partexpr))
7772+
{
7773+
/*
7774+
* Reject a match if the grouping collation does not match
7775+
* the partitioning collation.
7776+
*/
7777+
if (OidIsValid(partcoll) && OidIsValid(groupcoll) &&
7778+
partcoll != groupcoll)
7779+
return false;
7780+
7781+
found = true;
7782+
break;
7783+
}
77597784
}
7785+
7786+
if (found)
7787+
break;
77607788
}
77617789

77627790
/*

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

+90
Original file line numberDiff line numberDiff line change
@@ -1950,6 +1950,96 @@ SELECT (SELECT count(*) FROM test33_0) <> (SELECT count(*) FROM test33_1);
19501950
t
19511951
(1 row)
19521952

1953+
--
1954+
-- Bug #18568
1955+
--
1956+
-- Partitionwise aggregate (full or partial) should not be used when a
1957+
-- partition key's collation doesn't match that of the GROUP BY column it is
1958+
-- matched with.
1959+
SET max_parallel_workers_per_gather TO 0;
1960+
SET enable_incremental_sort TO off;
1961+
CREATE TABLE pagg_tab3 (a text, c text collate case_insensitive) PARTITION BY LIST(c collate "C");
1962+
CREATE TABLE pagg_tab3_p1 PARTITION OF pagg_tab3 FOR VALUES IN ('a', 'b');
1963+
CREATE TABLE pagg_tab3_p2 PARTITION OF pagg_tab3 FOR VALUES IN ('B', 'A');
1964+
INSERT INTO pagg_tab3 SELECT i % 4 + 1, substr('abAB', (i % 4) + 1 , 1) FROM generate_series(0, 19) i;
1965+
ANALYZE pagg_tab3;
1966+
SET enable_partitionwise_aggregate TO false;
1967+
EXPLAIN (COSTS OFF)
1968+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
1969+
QUERY PLAN
1970+
-----------------------------------------------------------
1971+
Sort
1972+
Sort Key: (upper(pagg_tab3.c)) COLLATE case_insensitive
1973+
-> HashAggregate
1974+
Group Key: pagg_tab3.c
1975+
-> Append
1976+
-> Seq Scan on pagg_tab3_p2 pagg_tab3_1
1977+
-> Seq Scan on pagg_tab3_p1 pagg_tab3_2
1978+
(7 rows)
1979+
1980+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
1981+
upper | count
1982+
-------+-------
1983+
A | 10
1984+
B | 10
1985+
(2 rows)
1986+
1987+
-- No "full" partitionwise aggregation allowed, though "partial" is allowed.
1988+
SET enable_partitionwise_aggregate TO true;
1989+
EXPLAIN (COSTS OFF)
1990+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
1991+
QUERY PLAN
1992+
--------------------------------------------------------------
1993+
Sort
1994+
Sort Key: (upper(pagg_tab3.c)) COLLATE case_insensitive
1995+
-> Finalize HashAggregate
1996+
Group Key: pagg_tab3.c
1997+
-> Append
1998+
-> Partial HashAggregate
1999+
Group Key: pagg_tab3.c
2000+
-> Seq Scan on pagg_tab3_p2 pagg_tab3
2001+
-> Partial HashAggregate
2002+
Group Key: pagg_tab3_1.c
2003+
-> Seq Scan on pagg_tab3_p1 pagg_tab3_1
2004+
(11 rows)
2005+
2006+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
2007+
upper | count
2008+
-------+-------
2009+
A | 10
2010+
B | 10
2011+
(2 rows)
2012+
2013+
-- OK to use full partitionwise aggregate after changing the GROUP BY column's
2014+
-- collation to be the same as that of the partition key.
2015+
EXPLAIN (COSTS OFF)
2016+
SELECT c collate "C", count(c) FROM pagg_tab3 GROUP BY c collate "C" ORDER BY 1;
2017+
QUERY PLAN
2018+
--------------------------------------------------------
2019+
Sort
2020+
Sort Key: ((pagg_tab3.c)::text) COLLATE "C"
2021+
-> Append
2022+
-> HashAggregate
2023+
Group Key: (pagg_tab3.c)::text
2024+
-> Seq Scan on pagg_tab3_p2 pagg_tab3
2025+
-> HashAggregate
2026+
Group Key: (pagg_tab3_1.c)::text
2027+
-> Seq Scan on pagg_tab3_p1 pagg_tab3_1
2028+
(9 rows)
2029+
2030+
SELECT c collate "C", count(c) FROM pagg_tab3 GROUP BY c collate "C" ORDER BY 1;
2031+
c | count
2032+
---+-------
2033+
A | 5
2034+
B | 5
2035+
a | 5
2036+
b | 5
2037+
(4 rows)
2038+
2039+
DROP TABLE pagg_tab3;
2040+
RESET enable_partitionwise_aggregate;
2041+
RESET max_parallel_workers_per_gather;
2042+
RESET enable_incremental_sort;
19532043
-- cleanup
19542044
RESET search_path;
19552045
SET client_min_messages TO warning;

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

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

750+
--
751+
-- Bug #18568
752+
--
753+
-- Partitionwise aggregate (full or partial) should not be used when a
754+
-- partition key's collation doesn't match that of the GROUP BY column it is
755+
-- matched with.
756+
SET max_parallel_workers_per_gather TO 0;
757+
SET enable_incremental_sort TO off;
758+
759+
CREATE TABLE pagg_tab3 (a text, c text collate case_insensitive) PARTITION BY LIST(c collate "C");
760+
CREATE TABLE pagg_tab3_p1 PARTITION OF pagg_tab3 FOR VALUES IN ('a', 'b');
761+
CREATE TABLE pagg_tab3_p2 PARTITION OF pagg_tab3 FOR VALUES IN ('B', 'A');
762+
INSERT INTO pagg_tab3 SELECT i % 4 + 1, substr('abAB', (i % 4) + 1 , 1) FROM generate_series(0, 19) i;
763+
ANALYZE pagg_tab3;
764+
765+
SET enable_partitionwise_aggregate TO false;
766+
EXPLAIN (COSTS OFF)
767+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
768+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
769+
770+
-- No "full" partitionwise aggregation allowed, though "partial" is allowed.
771+
SET enable_partitionwise_aggregate TO true;
772+
EXPLAIN (COSTS OFF)
773+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
774+
SELECT upper(c collate case_insensitive), count(c) FROM pagg_tab3 GROUP BY c collate case_insensitive ORDER BY 1;
775+
776+
-- OK to use full partitionwise aggregate after changing the GROUP BY column's
777+
-- collation to be the same as that of the partition key.
778+
EXPLAIN (COSTS OFF)
779+
SELECT c collate "C", count(c) FROM pagg_tab3 GROUP BY c collate "C" ORDER BY 1;
780+
SELECT c collate "C", count(c) FROM pagg_tab3 GROUP BY c collate "C" ORDER BY 1;
781+
782+
DROP TABLE pagg_tab3;
783+
784+
RESET enable_partitionwise_aggregate;
785+
RESET max_parallel_workers_per_gather;
786+
RESET enable_incremental_sort;
750787

751788
-- cleanup
752789
RESET search_path;

0 commit comments

Comments
 (0)