Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table: set the datum collation correctly in CastValue() #30931

Merged
merged 7 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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