Skip to content

Commit

Permalink
json: don't resize slice to get extra zero when the length is unspeci…
Browse files Browse the repository at this point in the history
…fied (#51586) (#52195)

close #51547
  • Loading branch information
ti-chi-bot committed Apr 15, 2024
1 parent 639ca73 commit 53ef4f0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ func (b *builtinCastStringAsJSONSig) evalJSON(row chunk.Row) (res types.BinaryJS
typ := b.args[0].GetType()
if types.IsBinaryStr(typ) {
buf := []byte(val)
if typ.GetType() == mysql.TypeString {
if typ.GetType() == mysql.TypeString && typ.GetFlen() > 0 {
// the tailing zero should also be in the opaque json
buf = make([]byte, typ.GetFlen())
copy(buf, val)
Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_cast_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ func (b *builtinCastStringAsJSONSig) vecEvalJSON(input *chunk.Chunk, result *chu

val := buf.GetBytes(i)
resultBuf := val
if typ.GetType() == mysql.TypeString {
if typ.GetType() == mysql.TypeString && typ.GetFlen() > 0 {
// only for BINARY: the tailing zero should also be in the opaque json
resultBuf = make([]byte, typ.GetFlen())
copy(resultBuf, val)
Expand Down
19 changes: 19 additions & 0 deletions expression/integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8045,3 +8045,22 @@ func TestIssue51765(t *testing.T) {
tk.MustQuery("select collation(ifnull(concat(NULL),id)) from t1;").Check(testkit.Rows("utf8mb4_general_ci"))
tk.MustQuery("select collation(ifnull(concat(NULL),ifnull(concat(NULL),id))) from t1;").Check(testkit.Rows("utf8mb4_general_ci"))
}

func TestCastBinaryStringToJSON(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustQuery("select cast(binary 'aa' as json);").Check(testkit.Rows(`"base64:type254:YWE="`))

tk.MustExec("use test")
tk.MustExec("create table t (vb VARBINARY(10), b BINARY(10), vc VARCHAR(10), c CHAR(10));")
tk.MustExec("insert into t values ('1', '1', '1', '1');")
tk.MustQuery("select cast(vb as json), cast(b as json), cast(vc as json), cast(c as json) from t;").Check(
testkit.Rows(`"base64:type15:MQ==" "base64:type254:MQAAAAAAAAAAAA==" 1 1`))
tk.MustQuery("select 1 from t where cast(vb as json) = '1';").Check(testkit.Rows())
tk.MustQuery("select 1 from t where cast(b as json) = '1';").Check(testkit.Rows())
tk.MustQuery("select 1 from t where cast(vc as json) = '1';").Check(testkit.Rows())
tk.MustQuery("select 1 from t where cast(c as json) = '1';").Check(testkit.Rows())
tk.MustQuery("select 1 from t where cast(BINARY vc as json) = '1';").Check(testkit.Rows())
tk.MustQuery("select 1 from t where cast(BINARY c as json) = '1';").Check(testkit.Rows())
}

0 comments on commit 53ef4f0

Please sign in to comment.