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

expression, cmd: let crc32() support gbk #30900

Merged
merged 4 commits into from
Dec 21, 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
11 changes: 11 additions & 0 deletions cmd/explaintest/r/new_character_set_builtin.result
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,14 @@ select hex(aes_encrypt(a, '123')), hex(aes_encrypt(b, '123')), hex(aes_encrypt(c
hex(aes_encrypt(a, '123')) hex(aes_encrypt(b, '123')) hex(aes_encrypt(c, '123'))
C54279F381B0710E145E94106F03C94C 7A747EC6F1906276D036B1F3CE27BAAB A0E5E01289017B8A3691CCFBDE81A59ED4A9D5BF50A298D41287E395CDDCAD56
set @@tidb_enable_vectorized_expression = false;
drop table if exists t;
create table t (a char(20) charset utf8mb4, b char(20) charset gbk, c binary(20));
insert into t values ('一二三', '一二三', '一二三');
select crc32(a), crc32(b), crc32(c) from t;
crc32(a) crc32(b) crc32(c)
1785250883 3461331449 4092198678
set @@tidb_enable_vectorized_expression = true;
select crc32(a), crc32(b), crc32(c) from t;
crc32(a) crc32(b) crc32(c)
1785250883 3461331449 4092198678
set @@tidb_enable_vectorized_expression = false;
9 changes: 9 additions & 0 deletions cmd/explaintest/t/new_character_set_builtin.test
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,12 @@ select hex(aes_encrypt(a, '123', '1234567890123456')), hex(aes_encrypt(b, '123',
set @@block_encryption_mode='aes-128-ecb';
select hex(aes_encrypt(a, '123')), hex(aes_encrypt(b, '123')), hex(aes_encrypt(c, '123')) from t;
set @@tidb_enable_vectorized_expression = false;

-- test for builtin crc32()
drop table if exists t;
create table t (a char(20) charset utf8mb4, b char(20) charset gbk, c binary(20));
insert into t values ('一二三', '一二三', '一二三');
select crc32(a), crc32(b), crc32(c) from t;
set @@tidb_enable_vectorized_expression = true;
select crc32(a), crc32(b), crc32(c) from t;
set @@tidb_enable_vectorized_expression = false;
2 changes: 2 additions & 0 deletions expression/builtin_convert_charset.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ var convertActionMap = map[funcProp][]string{
ast.Like, ast.Strcmp,
/* regex */
ast.Regexp,
/* math */
ast.CRC32,
},
}

Expand Down
43 changes: 26 additions & 17 deletions expression/builtin_math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/charset"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/testkit/trequire"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
Expand Down Expand Up @@ -531,27 +532,35 @@ func TestTruncate(t *testing.T) {
func TestCRC32(t *testing.T) {
ctx := createContext(t)
tbl := []struct {
Arg []interface{}
Ret interface{}
input []interface{}
chs string
result int64
isNull bool
}{
{[]interface{}{nil}, nil},
{[]interface{}{""}, 0},
{[]interface{}{-1}, 808273962},
{[]interface{}{"-1"}, 808273962},
{[]interface{}{"mysql"}, 2501908538},
{[]interface{}{"MySQL"}, 3259397556},
{[]interface{}{"hello"}, 907060870},
{[]interface{}{nil}, "utf8", 0, true},
{[]interface{}{""}, "utf8", 0, false},
{[]interface{}{-1}, "utf8", 808273962, false},
{[]interface{}{"-1"}, "utf8", 808273962, false},
{[]interface{}{"mysql"}, "utf8", 2501908538, false},
{[]interface{}{"MySQL"}, "utf8", 3259397556, false},
{[]interface{}{"hello"}, "utf8", 907060870, false},
{[]interface{}{"一二三"}, "utf8", 1785250883, false},
{[]interface{}{"一"}, "utf8", 2416838398, false},
{[]interface{}{"一二三"}, "gbk", 3461331449, false},
{[]interface{}{"一"}, "gbk", 2925846374, false},
}

Dtbl := tblToDtbl(tbl)

for _, tt := range Dtbl {
fc := funcs[ast.CRC32]
f, err := fc.getFunction(ctx, datumsToConstants(tt["Arg"]))
for _, c := range tbl {
err := ctx.GetSessionVars().SetSystemVar(variable.CharacterSetConnection, c.chs)
require.NoError(t, err)
v, err := evalBuiltinFunc(f, chunk.Row{})
f, err := newFunctionForTest(ctx, ast.CRC32, primitiveValsToConstants(ctx, c.input)...)
require.NoError(t, err)
trequire.DatumEqual(t, tt["Ret"][0], v)
d, err := f.Eval(chunk.Row{})
require.NoError(t, err)
if c.isNull {
require.True(t, d.IsNull())
} else {
require.Equal(t, c.result, d.GetInt64())
}
}
}

Expand Down