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: parse the value if convert enum/set to json #37402

Merged
merged 2 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion ddl/column_type_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ func TestColumnTypeChangeFromStringToOthers(t *testing.T) {
tk.MustExec("alter table t modify txt json")
tk.MustExec("alter table t modify e json")
tk.MustExec("alter table t modify s json")
tk.MustQuery("select * from t").Check(testkit.Rows("{\"k1\": \"value\"} {\"k1\": \"value\"} {\"k1\": \"value\"} {\"k1\": \"value\"} {\"k1\": \"value\"} {\"k1\": \"value\"} \"{\\\"k1\\\": \\\"value\\\"}\" \"{\\\"k1\\\": \\\"value\\\"}\""))
tk.MustQuery("select * from t").Check(testkit.Rows("{\"k1\": \"value\"} {\"k1\": \"value\"} {\"k1\": \"value\"} {\"k1\": \"value\"} {\"k1\": \"value\"} {\"k1\": \"value\"} {\"k1\": \"value\"} {\"k1\": \"value\"}"))

reset(tk)
tk.MustExec("insert into t values ('123x', 'x123', 'abc', 'datetime', 'timestamp', 'date', '123', '123')")
Expand Down Expand Up @@ -651,7 +651,10 @@ func TestColumnTypeChangeFromStringToOthers(t *testing.T) {
reset(tk)
tk.MustExec("alter table t modify c char(15)")
tk.MustExec("insert into t(c) values ('{\"k1\": \"value\"')")
tk.MustExec("insert into t(e, s) values ('str', 'str')")
tk.MustGetErrCode("alter table t modify c json", errno.ErrInvalidJSONText)
tk.MustGetErrCode("alter table t modify e json", errno.ErrInvalidJSONText)
tk.MustGetErrCode("alter table t modify s json", errno.ErrInvalidJSONText)

// MySQL will get "ERROR 1366 (HY000): Incorrect DECIMAL value: '0' for column '' at row -1" error.
tk.MustExec("insert into t(vc) values ('abc')")
Expand Down
8 changes: 8 additions & 0 deletions types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,14 @@ func (d *Datum) convertToMysqlJSON(_ *stmtctx.StatementContext, _ *FieldType) (r
if j, err = json.ParseBinaryFromString(d.GetString()); err == nil {
ret.SetMysqlJSON(j)
}
case KindMysqlSet, KindMysqlEnum:
var j json.BinaryJSON
var s string
if s, err = d.ToString(); err == nil {
if j, err = json.ParseBinaryFromString(s); err == nil {
ret.SetMysqlJSON(j)
}
}
case KindInt64:
i64 := d.GetInt64()
ret.SetMysqlJSON(json.CreateBinary(i64))
Expand Down