Skip to content

Commit

Permalink
parser: forbid alter latin1 to utf8mb3 (#34723)
Browse files Browse the repository at this point in the history
close #34720
  • Loading branch information
Defined2014 authored May 17, 2022
1 parent 0ac659b commit 847b5e1
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 49 deletions.
20 changes: 4 additions & 16 deletions cmd/explaintest/r/collation_misc_disabled.result
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ select * from t;
a
t_value
alter table t modify column a varchar(20) charset utf8;
admin check table t;

select * from t;
a
t_value
Error 8200: Unsupported modify charset from latin1 to utf8
drop table t;
create table t(a varchar(20) charset latin1);
insert into t values ("t_value");
Expand All @@ -31,7 +27,7 @@ t_value
drop table t;
create table t(a varchar(20) charset latin1);
insert into t values ("t_value");
alter table t modify column a varchar(20) charset utf8 collate utf8_bin;
alter table t modify column a varchar(20) charset utf8mb4 collate utf8mb4_general_ci;
admin check table t;

select * from t;
Expand All @@ -40,7 +36,8 @@ t_value
drop table t;
create table t(a varchar(20) charset latin1);
insert into t values ("t_value");
alter table t modify column a varchar(20) charset utf8mb4 collate utf8mb4_general_ci;
alter table t modify column a varchar(20) charset utf8 collate utf8_bin;
Error 8200: Unsupported modify charset from latin1 to utf8
alter table t modify column a varchar(20) charset utf8mb4 collate utf8bin;
[ddl:1273]Unknown collation: 'utf8bin'
alter table t collate LATIN1_GENERAL_CI charset utf8 collate utf8_bin;
Expand All @@ -53,15 +50,6 @@ insert into t values ("t_value");
alter table t modify column a varchar(19) charset utf8mb4;
admin check table t;

select * from t;
a
t_value
drop table t;
create table t(a varchar(20) charset latin1);
insert into t values ("t_value");
alter table t modify column a varchar(19) charset utf8 collate utf8_bin;
admin check table t;

select * from t;
a
t_value
Expand Down
20 changes: 4 additions & 16 deletions cmd/explaintest/r/collation_misc_enabled.result
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ select * from t;
a
t_value
alter table t modify column a varchar(20) charset utf8;
admin check table t;

select * from t;
a
t_value
Error 8200: Unsupported modify charset from latin1 to utf8
drop table t;
create table t(a varchar(20) charset latin1);
insert into t values ("t_value");
Expand All @@ -31,7 +27,7 @@ t_value
drop table t;
create table t(a varchar(20) charset latin1);
insert into t values ("t_value");
alter table t modify column a varchar(20) charset utf8 collate utf8_bin;
alter table t modify column a varchar(20) charset utf8mb4 collate utf8mb4_general_ci;
admin check table t;

select * from t;
Expand All @@ -40,7 +36,8 @@ t_value
drop table t;
create table t(a varchar(20) charset latin1);
insert into t values ("t_value");
alter table t modify column a varchar(20) charset utf8mb4 collate utf8mb4_general_ci;
alter table t modify column a varchar(20) charset utf8 collate utf8_bin;
Error 8200: Unsupported modify charset from latin1 to utf8
alter table t modify column a varchar(20) charset utf8mb4 collate utf8bin;
[ddl:1273]Unknown collation: 'utf8bin'
alter table t collate LATIN1_GENERAL_CI charset utf8 collate utf8_bin;
Expand All @@ -53,15 +50,6 @@ insert into t values ("t_value");
alter table t modify column a varchar(19) charset utf8mb4;
admin check table t;

select * from t;
a
t_value
drop table t;
create table t(a varchar(20) charset latin1);
insert into t values ("t_value");
alter table t modify column a varchar(19) charset utf8 collate utf8_bin;
admin check table t;

select * from t;
a
t_value
Expand Down
14 changes: 3 additions & 11 deletions cmd/explaintest/t/collation_misc.test
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ insert into t values ("t_value");
alter table t modify column a varchar(20) charset latin1;
select * from t;

--error 8200
alter table t modify column a varchar(20) charset utf8;
admin check table t;
select * from t;

drop table t;
create table t(a varchar(20) charset latin1);
Expand All @@ -27,15 +26,15 @@ select * from t;
drop table t;
create table t(a varchar(20) charset latin1);
insert into t values ("t_value");
alter table t modify column a varchar(20) charset utf8 collate utf8_bin;
alter table t modify column a varchar(20) charset utf8mb4 collate utf8mb4_general_ci;
admin check table t;
select * from t;

drop table t;
create table t(a varchar(20) charset latin1);
insert into t values ("t_value");
--error 8200
alter table t modify column a varchar(20) charset utf8mb4 collate utf8mb4_general_ci;
alter table t modify column a varchar(20) charset utf8 collate utf8_bin;
--error 1273
alter table t modify column a varchar(20) charset utf8mb4 collate utf8bin;
--error 1302, 1273
Expand All @@ -51,13 +50,6 @@ alter table t modify column a varchar(19) charset utf8mb4;
admin check table t;
select * from t;

drop table t;
create table t(a varchar(20) charset latin1);
insert into t values ("t_value");
alter table t modify column a varchar(19) charset utf8 collate utf8_bin;
admin check table t;
select * from t;

# TestCharsetDatabase
create database if not exists cd_test_utf8 CHARACTER SET utf8 COLLATE utf8_bin;
--error 1273
Expand Down
7 changes: 2 additions & 5 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -770,9 +770,7 @@ func TestChangingTableCharset(t *testing.T) {
tk.MustGetErrCode("alter table t charset utf8 collate latin1_bin", errno.ErrCollationCharsetMismatch)
tk.MustGetErrCode("alter table t charset utf8 collate utf8mb4_bin;", errno.ErrCollationCharsetMismatch)
tk.MustGetErrCode("alter table t charset utf8 collate utf8_bin collate utf8mb4_bin collate utf8_bin;", errno.ErrCollationCharsetMismatch)

tk.MustExec("alter table t charset utf8")
tk.MustExec("admin check table t")
tk.MustGetErrCode("alter table t charset utf8", errno.ErrUnsupportedDDLOperation)

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a char(10), index i(a)) charset latin1 collate latin1_bin")
Expand Down Expand Up @@ -806,8 +804,7 @@ func TestChangingTableCharset(t *testing.T) {
tk.MustExec("create table t(a varchar(20), key i(a)) charset=latin1")
tk.MustGetErrCode("alter table t convert to charset utf8 collate utf8_unicode_ci", errno.ErrUnsupportedDDLOperation)
tk.MustGetErrCode("alter table t convert to charset utf8 collate utf8_general_ci", errno.ErrUnsupportedDDLOperation)
tk.MustExec("alter table t convert to charset utf8 collate utf8_bin")
tk.MustGetErrCode("alter table t convert to charset latin1", errno.ErrUnsupportedDDLOperation)
tk.MustGetErrCode("alter table t convert to charset utf8 collate utf8_bin", errno.ErrUnsupportedDDLOperation)

// Test when column charset can not convert to the target charset.
tk.MustExec("drop table t;")
Expand Down
1 change: 0 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4178,7 +4178,6 @@ func checkModifyCharsetAndCollation(toCharset, toCollate, origCharset, origColla
if (origCharset == charset.CharsetUTF8 && toCharset == charset.CharsetUTF8MB4) ||
(origCharset == charset.CharsetUTF8 && toCharset == charset.CharsetUTF8) ||
(origCharset == charset.CharsetUTF8MB4 && toCharset == charset.CharsetUTF8MB4) ||
(origCharset == charset.CharsetLatin1 && toCharset == charset.CharsetUTF8) ||
(origCharset == charset.CharsetLatin1 && toCharset == charset.CharsetUTF8MB4) {
// TiDB only allow utf8/latin1 to be changed to utf8mb4, or changing the collation when the charset is utf8/utf8mb4/latin1.
return nil
Expand Down

0 comments on commit 847b5e1

Please sign in to comment.