Skip to content

Commit

Permalink
table: set the collation correctly in CastValue
Browse files Browse the repository at this point in the history
  • Loading branch information
tangenta committed Dec 22, 2021
1 parent 3bd732f commit e576e1d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 14 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,20 @@ func TestReplace(t *testing.T) {
tk.MustExec("drop table t1, t2")
}

func TestReplaceWithCICollation(t *testing.T) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")

tk.MustExec("create table t (a varchar(20) charset utf8mb4 collate utf8mb4_general_ci primary key);")
tk.MustExec("replace into t(a) values (_binary'A '),(_binary'A');")
tk.MustQuery("select a from t use index(primary);").Check(testkit.Rows("A"))
tk.MustQuery("select a from t ignore index(primary);").Check(testkit.Rows("A"))
}

func TestGeneratedColumnForInsert(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
Expand Down
6 changes: 3 additions & 3 deletions table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,18 +363,18 @@ func validateStringDatum(ctx sessionctx.Context, origin, casted *types.Datum, co
src := casted.GetBytes()
encBytes, err := enc.Transform(nil, src, charset.OpDecode)
if err != nil {
casted.SetBytesAsString(encBytes, charset.CollationUTF8MB4, 0)
casted.SetBytesAsString(encBytes, col.Collate, 0)
nSrc := charset.CountValidBytesDecode(enc, src)
return handleWrongCharsetValue(ctx, col, src, nSrc)
}
casted.SetBytesAsString(encBytes, charset.CollationUTF8MB4, 0)
casted.SetBytesAsString(encBytes, col.Collate, 0)
return nil
}
// Check if the string is valid in the given column charset.
str := casted.GetBytes()
if !charset.IsValid(enc, str) {
replace, _ := enc.Transform(nil, str, charset.OpReplace)
casted.SetBytesAsString(replace, charset.CollationUTF8MB4, 0)
casted.SetBytesAsString(replace, col.Collate, 0)
nSrc := charset.CountValidBytes(enc, str)
return handleWrongCharsetValue(ctx, col, str, nSrc)
}
Expand Down

0 comments on commit e576e1d

Please sign in to comment.