Skip to content

Commit

Permalink
fix cast json as string with shorter length
Browse files Browse the repository at this point in the history
Signed-off-by: YangKeao <yangkeao@chunibyo.icu>
  • Loading branch information
YangKeao committed Dec 15, 2022
1 parent 79b4e63 commit ba822f9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 5 additions & 1 deletion expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,11 @@ func (b *builtinCastJSONAsStringSig) evalString(row chunk.Row) (res string, isNu
if isNull || err != nil {
return res, isNull, err
}
return val.String(), false, nil
s, err := types.ProduceStrWithSpecifiedTp(val.String(), b.tp, b.ctx.GetSessionVars().StmtCtx, false)
if err != nil {
return res, false, err
}
return s, false, nil
}

type builtinCastJSONAsTimeSig struct {
Expand Down
9 changes: 9 additions & 0 deletions expression/builtin_cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,13 @@ func TestCastFuncSig(t *testing.T) {
3,
chunk.MutRowFromDatums([]types.Datum{types.NewStringDatum("你好world")}),
},
// cast json as string
{
&Column{RetType: types.NewFieldType(mysql.TypeJSON), Index: 0},
fmt.Sprintf(`"%s`, curTimeString[:2]),
3,
chunk.MutRowFromDatums([]types.Datum{jsonTime}),
},
}
for i, c := range castToStringCases2 {
args := []Expression{c.before}
Expand All @@ -741,6 +748,8 @@ func TestCastFuncSig(t *testing.T) {
case 5:
stringFunc.tp.SetCharset(charset.CharsetUTF8)
sig = &builtinCastStringAsStringSig{stringFunc}
case 6:
sig = &builtinCastJSONAsStringSig{stringFunc}
}
res, isNull, err := sig.evalString(c.row.ToRow())
require.False(t, isNull)
Expand Down
6 changes: 5 additions & 1 deletion expression/builtin_cast_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,11 @@ func (b *builtinCastJSONAsStringSig) vecEvalString(input *chunk.Chunk, result *c
result.AppendNull()
continue
}
result.AppendString(buf.GetJSON(i).String())
s, err := types.ProduceStrWithSpecifiedTp(buf.GetJSON(i).String(), b.tp, b.ctx.GetSessionVars().StmtCtx, false)
if err != nil {
return err
}
result.AppendString(s)
}
return nil
}
Expand Down

0 comments on commit ba822f9

Please sign in to comment.