From f9df5fade1b66ab294d10a25fdc21d09da059242 Mon Sep 17 00:00:00 2001 From: winkyao Date: Mon, 18 Feb 2019 14:59:22 +0800 Subject: [PATCH] ddl: make TiDB treat charset and collate case insensitive #8577 (#9338) --- ddl/integration_test.go | 13 +++++++++++++ util/charset/charset.go | 2 ++ util/charset/charset_test.go | 6 ++++++ 3 files changed, 21 insertions(+) diff --git a/ddl/integration_test.go b/ddl/integration_test.go index 33814371f7185..2e0246533f50f 100644 --- a/ddl/integration_test.go +++ b/ddl/integration_test.go @@ -118,6 +118,19 @@ func (s *testIntegrationSuite) TestEndIncluded(c *C) { tk.MustExec("admin check table t") } +func (s *testIntegrationSuite) TestCaseInsensitiveCharsetAndCollate(c *C) { + tk := testkit.NewTestKit(c, s.store) + + tk.MustExec("create database if not exists test_charset_collate") + defer tk.MustExec("drop database test_charset_collate") + tk.MustExec("use test_charset_collate") + tk.MustExec("create table t(id int) ENGINE=InnoDB DEFAULT CHARSET=UTF8 COLLATE=UTF8_BIN;") + tk.MustExec("create table t1(id int) ENGINE=InnoDB DEFAULT CHARSET=UTF8 COLLATE=uTF8_BIN;") + tk.MustExec("create table t2(id int) ENGINE=InnoDB DEFAULT CHARSET=Utf8 COLLATE=utf8_BIN;") + tk.MustExec("create table t3(id int) ENGINE=InnoDB DEFAULT CHARSET=Utf8mb4 COLLATE=utf8MB4_BIN;") + tk.MustExec("create table t4(id int) ENGINE=InnoDB DEFAULT CHARSET=Utf8mb4 COLLATE=utf8MB4_general_ci;") +} + func newStoreWithBootstrap() (kv.Storage, *domain.Domain, error) { store, err := mockstore.NewMockTikvStore() if err != nil { diff --git a/util/charset/charset.go b/util/charset/charset.go index abfc570c6fc65..41ec1f8fd0f44 100644 --- a/util/charset/charset.go +++ b/util/charset/charset.go @@ -86,6 +86,7 @@ func ValidCharsetAndCollation(cs string, co string) bool { cs = "utf8" } + cs = strings.ToLower(cs) c, ok := charsets[cs] if !ok { return false @@ -94,6 +95,7 @@ func ValidCharsetAndCollation(cs string, co string) bool { if co == "" { return true } + co = strings.ToLower(co) _, ok = c.Collations[co] if !ok { return false diff --git a/util/charset/charset_test.go b/util/charset/charset_test.go index c400d97f183be..9492a562d6517 100644 --- a/util/charset/charset_test.go +++ b/util/charset/charset_test.go @@ -49,6 +49,12 @@ func (s *testCharsetSuite) TestValidCharset(c *C) { {"utf8", "utf8_invalid_ci", false}, {"utf16", "utf16_bin", false}, {"gb2312", "gb2312_chinese_ci", false}, + {"UTF8", "UTF8_BIN", true}, + {"UTF8", "utf8_bin", true}, + {"UTF8MB4", "utf8mb4_bin", true}, + {"UTF8MB4", "UTF8MB4_bin", true}, + {"UTF8MB4", "UTF8MB4_general_ci", true}, + {"Utf8", "uTf8_bIN", true}, } for _, tt := range tests { testValidCharset(c, tt.cs, tt.co, tt.succ)