Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#51586
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
YangKeao authored and ti-chi-bot committed Mar 28, 2024
1 parent e921620 commit ad26779
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,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 pkg/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
39 changes: 39 additions & 0 deletions tests/integrationtest/r/expression/json.result
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,42 @@ json_extract('[{"a": [1,2,3,4]}]', '$[0].a[0 to last]')
select json_extract('[{"a": [1,2,3,4]}]', '$[0].a[0 to 2]');
json_extract('[{"a": [1,2,3,4]}]', '$[0].a[0 to 2]')
[1, 2, 3]
<<<<<<< HEAD
=======
drop table if exists t;
create table t (a json);
insert into t values ('"-1"');
insert into t values ('"18446744073709551615"');
insert into t values ('"18446744073709552000"');
select a, cast(a as unsigned) from t;
a cast(a as unsigned)
"-1" 18446744073709551615
"18446744073709551615" 18446744073709551615
"18446744073709552000" 18446744073709551615
select a, cast(a as signed) from t;
a cast(a as signed)
"-1" -1
"18446744073709551615" -1
"18446744073709552000" -1
select cast(binary 'aa' as json);
cast(binary 'aa' as json)
"base64:type254:YWE="
drop table if exists t;
create table t (vb VARBINARY(10), b BINARY(10), vc VARCHAR(10), c CHAR(10));
insert into t values ('1', '1', '1', '1');
select cast(vb as json), cast(b as json), cast(vc as json), cast(c as json) from t;
cast(vb as json) cast(b as json) cast(vc as json) cast(c as json)
"base64:type15:MQ==" "base64:type254:MQAAAAAAAAAAAA==" 1 1
select 1 from t where cast(vb as json) = '1';
1
select 1 from t where cast(b as json) = '1';
1
select 1 from t where cast(vc as json) = '1';
1
select 1 from t where cast(c as json) = '1';
1
select 1 from t where cast(BINARY vc as json) = '1';
1
select 1 from t where cast(BINARY c as json) = '1';
1
>>>>>>> 661f6d61aee (json: don't resize slice to get extra zero when the length is unspecified (#51586))
26 changes: 26 additions & 0 deletions tests/integrationtest/t/expression/json.test
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,29 @@ select json_extract('[{"a": [1,2,3,4]}]', '$[0].a[1 to 100]');
select json_extract('[{"a": [1,2,3,4]}]', '$[0].a[0 to last]');
select json_extract('[{"a": [1,2,3,4]}]', '$[0].a[0 to 2]');

<<<<<<< HEAD
=======
# TestCastJSONStringToInteger
drop table if exists t;
create table t (a json);
insert into t values ('"-1"');
insert into t values ('"18446744073709551615"');
insert into t values ('"18446744073709552000"');
-- sorted_result
select a, cast(a as unsigned) from t;
-- sorted_result
select a, cast(a as signed) from t;

# TestCastBinaryStringToJSON
select cast(binary 'aa' as json);
drop table if exists t;
create table t (vb VARBINARY(10), b BINARY(10), vc VARCHAR(10), c CHAR(10));
insert into t values ('1', '1', '1', '1');
select cast(vb as json), cast(b as json), cast(vc as json), cast(c as json) from t;
select 1 from t where cast(vb as json) = '1';
select 1 from t where cast(b as json) = '1';
select 1 from t where cast(vc as json) = '1';
select 1 from t where cast(c as json) = '1';
select 1 from t where cast(BINARY vc as json) = '1';
select 1 from t where cast(BINARY c as json) = '1';
>>>>>>> 661f6d61aee (json: don't resize slice to get extra zero when the length is unspecified (#51586))

0 comments on commit ad26779

Please sign in to comment.