diff --git a/components/tidb_query_expr/src/impl_cast.rs b/components/tidb_query_expr/src/impl_cast.rs index 53750d02d2d..6ce43695213 100644 --- a/components/tidb_query_expr/src/impl_cast.rs +++ b/components/tidb_query_expr/src/impl_cast.rs @@ -1385,8 +1385,9 @@ fn cast_string_as_json( let mut vec; if typ.tp() == FieldTypeTp::String { vec = (*val).to_owned(); - // the `flen` of string is always greater than zero - vec.resize(typ.flen().try_into().unwrap(), 0); + if typ.flen() > 0 { + vec.resize(typ.flen().try_into().unwrap(), 0); + } buf = &vec; } @@ -7021,6 +7022,17 @@ mod tests { Json::from_opaque(FieldTypeTp::String, &[97]).unwrap(), true, ), + ( + FieldTypeBuilder::new() + .tp(FieldTypeTp::VarChar) + .flen(UNSPECIFIED_LENGTH) + .charset(CHARSET_BIN) + .collation(Collation::Binary) + .build(), + "a".to_string(), + Json::from_opaque(FieldTypeTp::String, &[97]).unwrap(), + true, + ), ]; for (arg_type, input, expect, parse_to_json) in cs { let arg_value = ScalarValue::Bytes(Some(input.clone().into_bytes()));