From 359e0df36bda61e3e95a6a0f978774edabd418e0 Mon Sep 17 00:00:00 2001 From: suncan <1006949218@qq.com> Date: Tue, 7 Nov 2023 16:22:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9alter=20table=20convert=20to?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../optimizer/commands/tablecmds.cpp | 2 +- .../expected/test_b_format_collate.out | 71 +++++++++++++++++++ .../regress/sql/test_b_format_collate.sql | 19 +++++ 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/gausskernel/optimizer/commands/tablecmds.cpp b/src/gausskernel/optimizer/commands/tablecmds.cpp index e1ea6f041..98e1d17e6 100755 --- a/src/gausskernel/optimizer/commands/tablecmds.cpp +++ b/src/gausskernel/optimizer/commands/tablecmds.cpp @@ -8791,7 +8791,7 @@ static void sqlcmd_alter_prep_convert_charset(AlteredTableInfo* tab, Relation re Form_pg_attribute attTup = (Form_pg_attribute)GETSTRUCT(tuple); int attnum = attTup->attnum; if (attnum <= 0 || attTup->attisdropped || !type_is_collatable(attTup->atttypid) || - get_charset_by_collation(attTup->attcollation) == cc->charset) + attTup->attcollation == targetcollid) continue; transform = (Node*)makeVar(1, attnum, attTup->atttypid, attTup->atttypmod, attTup->attcollation, 0); diff --git a/src/test/regress/expected/test_b_format_collate.out b/src/test/regress/expected/test_b_format_collate.out index 4a060549d..918ea7830 100644 --- a/src/test/regress/expected/test_b_format_collate.out +++ b/src/test/regress/expected/test_b_format_collate.out @@ -2455,6 +2455,77 @@ select distinct c3 from test_utf8mb4_bin; fxlP7sW8vA9hcYdKqRHLwDzRSaAjV1VrMZFYRsmjb9JpsIPdGu7Gpi6OzaOqmR (1 row) +-- test alter table convert to +SET b_format_behavior_compat_options = 'enable_multi_charset'; +drop table if exists test_convert_to; +NOTICE: table "test_convert_to" does not exist, skipping +create table test_convert_to(a text, b char(10))collate utf8mb4_general_ci; +insert into test_convert_to values('abcd'),('中文'); +select pg_get_tabledef('test_convert_to'); + pg_get_tabledef +--------------------------------------------------------------------- + SET search_path = public; + + CREATE TABLE test_convert_to ( + + a text CHARACTER SET "UTF8" COLLATE utf8mb4_general_ci, + + b character(10) CHARACTER SET "UTF8" COLLATE utf8mb4_general_ci+ + ) + + CHARACTER SET = "UTF8" COLLATE = "utf8mb4_general_ci" + + WITH (orientation=row, compression=no); +(1 row) + +alter table test_convert_to convert to charset utf8mb4 collate utf8mb4_bin; +select pg_get_tabledef('test_convert_to'); + pg_get_tabledef +-------------------------------------------------------------- + SET search_path = public; + + CREATE TABLE test_convert_to ( + + a text CHARACTER SET "UTF8" COLLATE utf8mb4_bin, + + b character(10) CHARACTER SET "UTF8" COLLATE utf8mb4_bin+ + ) + + CHARACTER SET = "UTF8" COLLATE = "utf8mb4_bin" + + WITH (orientation=row, compression=no); +(1 row) + +alter table test_convert_to convert to charset gbk collate gbk_bin; +select pg_get_tabledef('test_convert_to'); + pg_get_tabledef +--------------------------------------------------------- + SET search_path = public; + + CREATE TABLE test_convert_to ( + + a text CHARACTER SET "GBK" COLLATE gbk_bin, + + b character(10) CHARACTER SET "GBK" COLLATE gbk_bin+ + ) + + CHARACTER SET = "GBK" COLLATE = "gbk_bin" + + WITH (orientation=row, compression=no); +(1 row) + +select * from test_convert_to; + a | b +------+--- + abcd | + 中文 | +(2 rows) + +alter table test_convert_to convert to charset default; +select pg_get_tabledef('test_convert_to'); + pg_get_tabledef +--------------------------------------------------------------------- + SET search_path = public; + + CREATE TABLE test_convert_to ( + + a text CHARACTER SET "UTF8" COLLATE utf8mb4_general_ci, + + b character(10) CHARACTER SET "UTF8" COLLATE utf8mb4_general_ci+ + ) + + CHARACTER SET = "UTF8" COLLATE = "utf8mb4_general_ci" + + WITH (orientation=row, compression=no); +(1 row) + +select * from test_convert_to; + a | b +------+--- + abcd | + 中文 | +(2 rows) + create database b_ascii encoding = 0; \c b_ascii set client_encoding = utf8; diff --git a/src/test/regress/sql/test_b_format_collate.sql b/src/test/regress/sql/test_b_format_collate.sql index 5d385e8b5..3a888680c 100644 --- a/src/test/regress/sql/test_b_format_collate.sql +++ b/src/test/regress/sql/test_b_format_collate.sql @@ -579,6 +579,25 @@ select count(*) from test_utf8mb4_bin group by c2, c3; select distinct c2 from test_utf8mb4_bin; select distinct c3 from test_utf8mb4_bin; +-- test alter table convert to +SET b_format_behavior_compat_options = 'enable_multi_charset'; +drop table if exists test_convert_to; +create table test_convert_to(a text, b char(10))collate utf8mb4_general_ci; +insert into test_convert_to values('abcd'),('中文'); +select pg_get_tabledef('test_convert_to'); + +alter table test_convert_to convert to charset utf8mb4 collate utf8mb4_bin; +select pg_get_tabledef('test_convert_to'); + +alter table test_convert_to convert to charset gbk collate gbk_bin; +select pg_get_tabledef('test_convert_to'); +select * from test_convert_to; + +alter table test_convert_to convert to charset default; +select pg_get_tabledef('test_convert_to'); +select * from test_convert_to; + + create database b_ascii encoding = 0; \c b_ascii set client_encoding = utf8;