From f47375da9849ecd6e6f8179d6b28d75764280be0 Mon Sep 17 00:00:00 2001
From: Tender Wang <tndrwang@gmail.com>
Date: Tue, 6 Aug 2024 16:38:03 +0800
Subject: [PATCH v1] Fix collation different between columan collation and
 partkey collation

---
 src/backend/optimizer/util/plancat.c         |  2 +-
 src/test/regress/expected/partition_info.out | 34 ++++++++++++++++++++
 src/test/regress/sql/partition_info.sql      | 19 +++++++++++
 3 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c
index 78a3cfafde..29e33150f1 100644
--- a/src/backend/optimizer/util/plancat.c
+++ b/src/backend/optimizer/util/plancat.c
@@ -2531,7 +2531,7 @@ set_baserel_partition_key_exprs(Relation relation,
 			partexpr = (Expr *) makeVar(varno, attno,
 										partkey->parttypid[cnt],
 										partkey->parttypmod[cnt],
-										partkey->parttypcoll[cnt], 0);
+										partkey->partcollation[cnt], 0);
 		}
 		else
 		{
diff --git a/src/test/regress/expected/partition_info.out b/src/test/regress/expected/partition_info.out
index 42b6bc77ca..13b096307e 100644
--- a/src/test/regress/expected/partition_info.out
+++ b/src/test/regress/expected/partition_info.out
@@ -349,3 +349,37 @@ SELECT pg_partition_root('ptif_li_child');
 DROP VIEW ptif_test_view;
 DROP MATERIALIZED VIEW ptif_test_matview;
 DROP TABLE ptif_li_parent, ptif_li_child;
+-- Test collation
+CREATE COLLATION case_insensitive (
+       provider = icu,
+       locale = 'und-u-ks-level2',
+       deterministic = false);
+CREATE TABLE pagg_tab_col (c text collate case_insensitive) PARTITION BY LIST(c collate "C");
+CREATE TABLE pagg_tab_col_p1 PARTITION OF pagg_tab_col FOR VALUES IN ('a', 'b', 'c', 'd');
+CREATE TABLE pagg_tab_col_p2 PARTITION OF pagg_tab_col FOR VALUES IN ('e', 'f','A');
+CREATE TABLE pagg_tab_col_p3 PARTITION OF pagg_tab_col FOR VALUES IN ('B', 'C', 'D', 'E');
+INSERT INTO pagg_tab_col SELECT substr('abcdeABCDE', (i % 10) +1 , 1) FROM generate_series(0, 2999) i;
+ANALYZE pagg_tab_col;
+EXPLAIN SELECT c collate case_insensitive, count(c) FROM
+pagg_tab_col GROUP BY c collate case_insensitive;
+                                          QUERY PLAN                                          
+----------------------------------------------------------------------------------------------
+ HashAggregate  (cost=75.00..75.05 rows=5 width=10)
+   Group Key: pagg_tab_col.c
+   ->  Append  (cost=0.00..60.00 rows=3000 width=2)
+         ->  Seq Scan on pagg_tab_col_p2 pagg_tab_col_1  (cost=0.00..9.00 rows=600 width=2)
+         ->  Seq Scan on pagg_tab_col_p3 pagg_tab_col_2  (cost=0.00..18.00 rows=1200 width=2)
+         ->  Seq Scan on pagg_tab_col_p1 pagg_tab_col_3  (cost=0.00..18.00 rows=1200 width=2)
+(6 rows)
+
+SELECT c collate case_insensitive, count(c) FROM
+pagg_tab_col GROUP BY c collate case_insensitive;
+ c | count 
+---+-------
+ e |   600
+ D |   600
+ C |   600
+ B |   600
+ A |   600
+(5 rows)
+
diff --git a/src/test/regress/sql/partition_info.sql b/src/test/regress/sql/partition_info.sql
index b5060bec7f..5c27c82217 100644
--- a/src/test/regress/sql/partition_info.sql
+++ b/src/test/regress/sql/partition_info.sql
@@ -127,3 +127,22 @@ SELECT pg_partition_root('ptif_li_child');
 DROP VIEW ptif_test_view;
 DROP MATERIALIZED VIEW ptif_test_matview;
 DROP TABLE ptif_li_parent, ptif_li_child;
+
+-- Test collation
+CREATE COLLATION case_insensitive (
+       provider = icu,
+       locale = 'und-u-ks-level2',
+       deterministic = false);
+
+CREATE TABLE pagg_tab_col (c text collate case_insensitive) PARTITION BY LIST(c collate "C");
+CREATE TABLE pagg_tab_col_p1 PARTITION OF pagg_tab_col FOR VALUES IN ('a', 'b', 'c', 'd');
+CREATE TABLE pagg_tab_col_p2 PARTITION OF pagg_tab_col FOR VALUES IN ('e', 'f','A');
+CREATE TABLE pagg_tab_col_p3 PARTITION OF pagg_tab_col FOR VALUES IN ('B', 'C', 'D', 'E');
+INSERT INTO pagg_tab_col SELECT substr('abcdeABCDE', (i % 10) +1 , 1) FROM generate_series(0, 2999) i;
+ANALYZE pagg_tab_col;
+
+EXPLAIN SELECT c collate case_insensitive, count(c) FROM
+pagg_tab_col GROUP BY c collate case_insensitive;
+
+SELECT c collate case_insensitive, count(c) FROM
+pagg_tab_col GROUP BY c collate case_insensitive;
-- 
2.34.1

