Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitff65f69

Browse files
committed
Disallow partitionwise grouping when collations don't match
If the collation of any grouping column doesn’t match the collation ofthe corresponding partition key, partitionwise grouping can yieldincorrect results. For example, rows that would be grouped under thegrouping collation may end up in different partitions under thepartitioning collation. In such cases, full partitionwise groupingwould produce results that differ from those without partitionwisegrouping, so disallowed that.Partial partitionwise aggregation is still allowed, as the Finalizestep reconciles partition-level aggregates with grouping requirementsacross all partitions, ensuring that the final output remainsconsistent.This commit also fixes group_by_has_partkey() by ensuring theRelabelType node is stripped from grouping expressions when matchingthem to partition key expressions to avoid false mismatches.Bug: #18568Reported-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.orgDiscussion:https://postgr.es/m/tencent_9D9103CDA420C07768349CC1DFF88465F90A@qq.comDiscussion:https://postgr.es/m/CAHewXNno_HKiQ6PqyLYfuqDtwp7KKHZiH1J7Pqyz0nr+PS2Dwg@mail.gmail.comBackpatch-through: 12
1 parentebbfa2a commitff65f69

File tree

3 files changed

+163
-8
lines changed

3 files changed

+163
-8
lines changed

‎src/backend/optimizer/plan/planner.c

Lines changed: 36 additions & 8 deletions
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
staticbool
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+
Oidpartcoll=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+
Oidgroupcoll=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

Lines changed: 90 additions & 0 deletions
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

Lines changed: 37 additions & 0 deletions
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 (SELECTcount(*)FROM test33_0)<> (SELECTcount(*)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 TO0;
757+
SET enable_incremental_sort TO off;
758+
759+
CREATETABLEpagg_tab3 (atext, ctext collate case_insensitive) PARTITION BY LIST(c collate"C");
760+
CREATETABLEpagg_tab3_p1 PARTITION OF pagg_tab3 FORVALUESIN ('a','b');
761+
CREATETABLEpagg_tab3_p2 PARTITION OF pagg_tab3 FORVALUESIN ('B','A');
762+
INSERT INTO pagg_tab3SELECT 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+
SELECTupper(c collate case_insensitive),count(c)FROM pagg_tab3GROUP BY c collate case_insensitiveORDER BY1;
768+
SELECTupper(c collate case_insensitive),count(c)FROM pagg_tab3GROUP BY c collate case_insensitiveORDER BY1;
769+
770+
-- No "full" partitionwise aggregation allowed, though "partial" is allowed.
771+
SET enable_partitionwise_aggregate TO true;
772+
EXPLAIN (COSTS OFF)
773+
SELECTupper(c collate case_insensitive),count(c)FROM pagg_tab3GROUP BY c collate case_insensitiveORDER BY1;
774+
SELECTupper(c collate case_insensitive),count(c)FROM pagg_tab3GROUP BY c collate case_insensitiveORDER BY1;
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_tab3GROUP BY c collate"C"ORDER BY1;
780+
SELECT c collate"C",count(c)FROM pagg_tab3GROUP BY c collate"C"ORDER BY1;
781+
782+
DROPTABLE 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)

[8]ページ先頭

©2009-2025 Movatter.jp