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

cmd, expression: fix convert binary string to gbk #30004

Merged
merged 5 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Binary file modified cmd/explaintest/r/new_character_set_builtin.result
Binary file not shown.
7 changes: 7 additions & 0 deletions cmd/explaintest/t/new_character_set_builtin.test
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ select hex(convert('ㅂ' using gbk)), convert('ㅂ' 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);
drop table if exists t;
create table t(a binary(10));
insert into t values (0xe240), (0x01e240);
set @@tidb_enable_vectorized_expression = true;
select hex(convert(a using gbk)), convert(a using gbk) from t;
set @@tidb_enable_vectorized_expression = false;
select hex(convert(a using gbk)), convert(a using gbk) from t;

-- test for builtin function md5()
drop table if exists t;
Expand Down
10 changes: 2 additions & 8 deletions expression/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions expression/builtin_string_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down