diff --git a/cmd/explaintest/r/new_character_set_builtin.result b/cmd/explaintest/r/new_character_set_builtin.result index a1e547c4219eb..c4fb7c8c7fca3 100644 --- a/cmd/explaintest/r/new_character_set_builtin.result +++ b/cmd/explaintest/r/new_character_set_builtin.result @@ -81,6 +81,12 @@ hex(convert('1' using gbk)) convert('1' using gbk) select hex(convert('ã…‚' using gbk)), convert('ã…‚' using gbk); hex(convert('ã…‚' using gbk)) convert('ã…‚' using gbk) 3F ? +select hex(convert(0xe240 using gbk)), convert(0xe240 using gbk); +hex(convert(0xe240 using gbk)) convert(0xe240 using gbk) +E240 釦 +select hex(convert(0x1e240 using gbk)), convert(0x1e240 using gbk); +hex(convert(0x1e240 using gbk)) convert(0x1e240 using gbk) +01E240 釦 select convert(a using binary), convert(convert(a using gbk) using binary) from t; convert(a using binary) convert(convert(a using gbk) using binary) 中文 ÖÐÎÄ @@ -117,6 +123,12 @@ hex(convert('1' using gbk)) convert('1' using gbk) select hex(convert('ã…‚' using gbk)), convert('ã…‚' using gbk); hex(convert('ã…‚' using gbk)) convert('ã…‚' using gbk) 3F ? +select hex(convert(0xe240 using gbk)), convert(0xe240 using gbk); +hex(convert(0xe240 using gbk)) convert(0xe240 using gbk) +E240 釦 +select hex(convert(0x1e240 using gbk)), convert(0x1e240 using gbk); +hex(convert(0x1e240 using gbk)) convert(0x1e240 using gbk) +01E240 釦 select convert(a using binary) from t; convert(a using binary) 中文 diff --git a/cmd/explaintest/t/new_character_set_builtin.test b/cmd/explaintest/t/new_character_set_builtin.test index ff4c2a9354f74..fd92ed513f018 100644 --- a/cmd/explaintest/t/new_character_set_builtin.test +++ b/cmd/explaintest/t/new_character_set_builtin.test @@ -44,6 +44,8 @@ select hex(convert('å•Š' using gbk)), convert('å•Š' using gbk); select hex(convert('a' using gbk)), convert('a' using gbk); select hex(convert('1' using gbk)), convert('1' using gbk); select hex(convert('ã…‚' using gbk)), convert('ã…‚' using gbk); +select hex(convert(0xe240 using gbk)), convert(0xe240 using gbk); +select hex(convert(0x1e240 using gbk)), convert(0x1e240 using gbk); select convert(a using binary), convert(convert(a using gbk) using binary) from t; select convert(convert('中文' using gbk) using binary), convert('中文' using binary); select convert(convert('ã…‚' using gbk) using binary), convert('ã…‚' using binary); @@ -54,6 +56,8 @@ select hex(convert('å•Š' using gbk)), convert('å•Š' using gbk); select hex(convert('a' using gbk)), convert('a' using gbk); select hex(convert('1' using gbk)), convert('1' using gbk); select hex(convert('ã…‚' using gbk)), convert('ã…‚' using gbk); +select hex(convert(0xe240 using gbk)), convert(0xe240 using gbk); +select hex(convert(0x1e240 using gbk)), convert(0x1e240 using gbk); select convert(a using binary) from t; select convert(convert('中文' using gbk) using binary), convert('中文' using binary); select convert(convert('ã…‚' using gbk) using binary), convert('ã…‚' using binary); diff --git a/expression/builtin_string.go b/expression/builtin_string.go index 38a5647d49fac..c15e4d4200920 100644 --- a/expression/builtin_string.go +++ b/expression/builtin_string.go @@ -1152,14 +1152,8 @@ func (b *builtinConvertSig) evalString(row chunk.Row) (string, bool, error) { } // if expr is binary string and convert meet error, we should return NULL. if types.IsBinaryStr(b.args[0].GetType()) { - target, _, err := transform.String(encoding.NewEncoder(), expr) - if err != nil { - return "", true, err - } - - // we should convert target into utf8 internal. - exprInternal, _, _ := transform.String(encoding.NewDecoder(), target) - return exprInternal, false, nil + exprInternal, _, err := transform.String(encoding.NewDecoder(), expr) + return exprInternal, err != nil, nil } if types.IsBinaryStr(b.tp) { enc := charset.NewEncoding(b.args[0].GetType().Charset) diff --git a/expression/builtin_string_vec.go b/expression/builtin_string_vec.go index 34293d6103be0..5f51caefa7fec 100644 --- a/expression/builtin_string_vec.go +++ b/expression/builtin_string_vec.go @@ -685,7 +685,6 @@ func (b *builtinConvertSig) vecEvalString(input *chunk.Chunk, result *chunk.Colu if encoding == nil { return errUnknownCharacterSet.GenWithStackByArgs(b.tp.Charset) } - encoder := encoding.NewEncoder() decoder := encoding.NewDecoder() isBinaryStr := types.IsBinaryStr(b.args[0].GetType()) isRetBinary := types.IsBinaryStr(b.tp) @@ -702,13 +701,12 @@ func (b *builtinConvertSig) vecEvalString(input *chunk.Chunk, result *chunk.Colu } exprI := expr.GetString(i) if isBinaryStr { - target, _, err := transform.String(encoder, exprI) + target, _, err := transform.String(decoder, exprI) if err != nil { - return err + result.AppendNull() + continue } - // we should convert target into utf8 internal. - exprInternal, _, _ := transform.String(decoder, target) - result.AppendString(exprInternal) + result.AppendString(target) } else { if isRetBinary { str, err := enc.EncodeString(exprI)