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, parser: support GBK for builtin function char() #29543

Merged
merged 9 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
21 changes: 21 additions & 0 deletions cmd/explaintest/r/new_character_set_builtin.result
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,24 @@ select md5(b) from t where md5(b) = 'a45d4af7b243e7f393fa09bed72ac73e';
md5(b)
a45d4af7b243e7f393fa09bed72ac73e
set @@tidb_enable_vectorized_expression = false;
drop table if exists t;
create table t (a char(20));
insert into t values ('65'), ('123456'), ('123456789');
select char(a using gbk), char(a using utf8), char(a) from t;
char(a using gbk) char(a using utf8) char(a)
A A A
釦 �@ �@
NULL [� [�
select char(12345678 using gbk);
char(12345678 using gbk)
糰N
set @@tidb_enable_vectorized_expression = true;
select char(a using gbk), char(a using utf8), char(a) from t;
char(a using gbk) char(a using utf8) char(a)
A A A
釦 �@ �@
NULL [� [�
select char(12345678 using gbk);
char(12345678 using gbk)
糰N
set @@tidb_enable_vectorized_expression = false;
10 changes: 10 additions & 0 deletions cmd/explaintest/t/new_character_set_builtin.test
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,13 @@ set @@tidb_enable_vectorized_expression = true;
select md5(b) from t where md5(b) = 'a45d4af7b243e7f393fa09bed72ac73e';
set @@tidb_enable_vectorized_expression = false;

-- test for builtin function char()
drop table if exists t;
create table t (a char(20));
insert into t values ('65'), ('123456'), ('123456789');
select char(a using gbk), char(a using utf8), char(a) from t;
select char(12345678 using gbk);
set @@tidb_enable_vectorized_expression = true;
select char(a using gbk), char(a using utf8), char(a) from t;
select char(12345678 using gbk);
set @@tidb_enable_vectorized_expression = false;
10 changes: 8 additions & 2 deletions expression/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2446,8 +2446,14 @@ func (b *builtinCharSig) evalString(row chunk.Row) (string, bool, error) {
}
bigints = append(bigints, val)
}
result := string(b.convertToBytes(bigints))
return result, false, nil

dBytes := b.convertToBytes(bigints)
resultBytes, err := charset.NewEncoding(b.tp.Charset).Decode(nil, dBytes)
if err != nil {
b.ctx.GetSessionVars().StmtCtx.AppendWarning(err)
return "", true, nil
}
return string(resultBytes), false, nil
}

type charLengthFunctionClass struct {
Expand Down
49 changes: 26 additions & 23 deletions expression/builtin_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1430,34 +1430,37 @@ func TestChar(t *testing.T) {
}()

tbl := []struct {
str string
iNum int64
fNum float64
result string
str string
iNum int64
fNum float64
charset interface{}
result interface{}
warnings int
}{
{"65", 66, 67.5, "ABD"}, // float
{"65", 16740, 67.5, "AAdD"}, // large num
{"65", -1, 67.5, "A\xff\xff\xff\xffD"}, // nagtive int
{"a", -1, 67.5, "\x00\xff\xff\xff\xffD"}, // invalid 'a'
{"65", 66, 67.5, "utf8", "ABD", 0}, // float
{"65", 16740, 67.5, "utf8", "AAdD", 0}, // large num
{"65", -1, 67.5, nil, "A\xff\xff\xff\xffD", 0}, // nagtive int
{"a", -1, 67.5, nil, "\x00\xff\xff\xff\xffD", 0}, // invalid 'a'
// TODO: Uncomment it when issue #29685 be closed
// {"65", -1, 67.5, "utf8", nil, 1}, // with utf8, return nil
// {"a", -1, 67.5, "utf8", nil, 2}, // with utf8, return nil
// TODO: Uncomment it when gbk be added into charsetInfos
Copy link
Contributor

@zimulala zimulala Nov 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the charsetInfos here?
If it is the session variable, we can try ctx.GetSessionVars().SetSystemVar(variable.CharacterSetConnection, v.charset).

Copy link
Contributor Author

@Defined2014 Defined2014 Nov 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

// {"1234567", 1234567, 1234567, "gbk", "謬謬謬", 0}, // test char for gbk
// {"123456789", 123456789, 123456789, "gbk", nil, 3}, // invalid 123456789 in gbk
}
for _, v := range tbl {
for _, char := range []interface{}{"utf8", nil} {
fc := funcs[ast.CharFunc]
f, err := fc.getFunction(ctx, datumsToConstants(types.MakeDatums(v.str, v.iNum, v.fNum, char)))
require.NoError(t, err)
require.NotNil(t, f)
r, err := evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
trequire.DatumEqual(t, types.NewDatum(v.result), r)
fc := funcs[ast.CharFunc]
f, err := fc.getFunction(ctx, datumsToConstants(types.MakeDatums(v.str, v.iNum, v.fNum, v.charset)))
require.NoError(t, err)
require.NotNil(t, f)
r, err := evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
trequire.DatumEqual(t, types.NewDatum(v.result), r)
if v.warnings != 0 {
warnings := ctx.GetSessionVars().StmtCtx.GetWarnings()
require.Equal(t, v.warnings, len(warnings))
}
}

fc := funcs[ast.CharFunc]
f, err := fc.getFunction(ctx, datumsToConstants(types.MakeDatums("65", 66, nil)))
require.NoError(t, err)
r, err := evalBuiltinFunc(f, chunk.Row{})
require.NoError(t, err)
trequire.DatumEqual(t, types.NewDatum("AB"), r)
}

func TestCharLength(t *testing.T) {
Expand Down
14 changes: 12 additions & 2 deletions expression/builtin_string_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2301,16 +2301,26 @@ func (b *builtinCharSig) vecEvalString(input *chunk.Chunk, result *chunk.Column)
for i := 0; i < l-1; i++ {
bufint[i] = buf[i].Int64s()
}
var resultBytes []byte
enc := charset.NewEncoding(b.tp.Charset)
for i := 0; i < n; i++ {
bigints = bigints[0:0]
for j := 0; j < l-1; j++ {
if buf[j].IsNull(i) {
result.AppendNull()
continue
}
bigints = append(bigints, bufint[j][i])
}
tempString := string(b.convertToBytes(bigints))
result.AppendString(tempString)
dBytes := b.convertToBytes(bigints)

resultBytes, err := enc.Decode(resultBytes, dBytes)
if err != nil {
b.ctx.GetSessionVars().StmtCtx.AppendWarning(err)
result.AppendNull()
continue
}
result.AppendString(string(resultBytes))
}
return nil
}
Expand Down