diff --git a/pkg/expression/builtin_cast.go b/pkg/expression/builtin_cast.go index e47124282d5c0..4ca799994736f 100644 --- a/pkg/expression/builtin_cast.go +++ b/pkg/expression/builtin_cast.go @@ -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) diff --git a/pkg/expression/builtin_cast_vec.go b/pkg/expression/builtin_cast_vec.go index d4df1f3b60cae..c414375e62601 100644 --- a/pkg/expression/builtin_cast_vec.go +++ b/pkg/expression/builtin_cast_vec.go @@ -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) diff --git a/tests/integrationtest/r/expression/json.result b/tests/integrationtest/r/expression/json.result index 8a1472456f31d..d2ad35eb6ee30 100644 --- a/tests/integrationtest/r/expression/json.result +++ b/tests/integrationtest/r/expression/json.result @@ -602,3 +602,24 @@ 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] +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 diff --git a/tests/integrationtest/t/expression/json.test b/tests/integrationtest/t/expression/json.test index ca41e0b701fea..a7b613438623c 100644 --- a/tests/integrationtest/t/expression/json.test +++ b/tests/integrationtest/t/expression/json.test @@ -359,3 +359,15 @@ 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]'); +# 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';