Skip to content

Commit

Permalink
cmd, expression: fix convert binary string to gbk (#30004)
Browse files Browse the repository at this point in the history
  • Loading branch information
Defined2014 committed Nov 26, 2021
1 parent 75f6225 commit bccc21b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
12 changes: 12 additions & 0 deletions cmd/explaintest/r/new_character_set_builtin.result
Original file line number Diff line number Diff line change
Expand Up @@ -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)
中文 ����
Expand Down Expand Up @@ -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)
中文
Expand Down
4 changes: 4 additions & 0 deletions cmd/explaintest/t/new_character_set_builtin.test
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
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

0 comments on commit bccc21b

Please sign in to comment.