Skip to content

Commit

Permalink
Don't worry about matching MySQL's messages on the wire if it would r…
Browse files Browse the repository at this point in the history
…equire round-trip deserializing/serializing a JSON Document.
  • Loading branch information
nicktobey committed Apr 26, 2024
1 parent 9044561 commit 4010670
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions sql/types/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,31 @@ func (t JsonType) SQL(ctx *sql.Context, dest []byte, v interface{}) (sqltypes.Va
return sqltypes.NULL, nil
}

// Convert to jsonType
jsVal, _, err := t.Convert(v)
if err != nil {
return sqltypes.NULL, err
}
js := jsVal.(sql.JSONWrapper)
var val []byte

str, err := MarshallJson(js)
if err != nil {
return sqltypes.NULL, err
// If we read the JSON from a table, pass through the bytes to avoid a deserialization and reserialization round-trip.
// This is kind of a hack, and it means that reading JSON from tables no longer matches MySQL byte-for-byte.
// But its worth it to avoid the round-trip, which can be very slow.
if j, ok := v.(*LazyJSONDocument); ok {
str, err := MarshallJson(j)
if err != nil {
return sqltypes.NULL, err
}
val = AppendAndSliceBytes(dest, str)
} else {
// Convert to jsonType
jsVal, _, err := t.Convert(v)
if err != nil {
return sqltypes.NULL, err
}
js := jsVal.(sql.JSONWrapper)

str, err := StringifyJSON(js)
if err != nil {
return sqltypes.NULL, err
}
val = AppendAndSliceString(dest, str)
}
val := AppendAndSliceBytes(dest, str)

return sqltypes.MakeTrusted(sqltypes.TypeJSON, val), nil
}
Expand Down

0 comments on commit 4010670

Please sign in to comment.