Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression, json: fix cast json as string with shorter length #39970

Merged
merged 2 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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