Skip to content

Commit

Permalink
Use charset literals in partial JSON expression
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Lord <[email protected]>
  • Loading branch information
mattlord committed Dec 9, 2024
1 parent 2aed1aa commit 7b75ed6
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 5 deletions.
8 changes: 7 additions & 1 deletion go/mysql/binlog/binlog_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ func ParseBinaryJSONDiff(data []byte) (sqltypes.Value, error) {
path := data[pos : uint64(pos)+pathLen]
pos += int(pathLen)
log.Errorf("DEBUG: json diff path: %s", string(path))
diff.WriteString(fmt.Sprintf("'%s', ", path))
// We have to specify the unicode character set for the strings we
// use in the expression as the connection can be using a different
// character set (e.g. vreplication always uses set names binary).
diff.WriteString(fmt.Sprintf("_utf8mb4'%s', ", path))

if opType == jsonDiffOpRemove { // No value for remove
diff.WriteString(")")
Expand All @@ -124,6 +127,9 @@ func ParseBinaryJSONDiff(data []byte) (sqltypes.Value, error) {
return sqltypes.Value{}, fmt.Errorf("cannot read JSON diff value for path %s: %w", path, err)
}
log.Errorf("DEBUG: json diff value: %v", value)
if value.Type() == json.TypeString {
diff.WriteString("_utf8mb4")
}
diff.WriteString(fmt.Sprintf("%s)", value))

return sqltypes.MakeTrusted(sqltypes.Expression, diff.Bytes()), nil
Expand Down
2 changes: 1 addition & 1 deletion go/mysql/binlog_event_mysql56_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func TestMySQL56PartialUpdateRowsEvent(t *testing.T) {
vals, err := ev.StringValuesForTests(tm, i)
require.NoError(t, err)
// The third column is the JSON column.
require.Equal(t, vals[2], `JSON_INSERT(%s, '$.role', "manager")`)
require.Equal(t, `JSON_INSERT(%s, _utf8mb4'$.role', _utf8mb4"manager")`, vals[2])
t.Logf("Rows: %v", vals)
}
}
2 changes: 1 addition & 1 deletion go/vt/proto/binlogdata/binlogdata.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go/vt/vttablet/tabletmanager/rpc_vreplication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1753,7 +1753,7 @@ func addInvariants(dbClient *binlogplayer.MockDBClient, vreplID, sourceTabletUID
fmt.Sprintf("%s||0|0|Stopped|1|%s|0|0", position, workflow),
))
dbClient.AddInvariant(setSessionTZ, &sqltypes.Result{})
//dbClient.AddInvariant(setNames, &sqltypes.Result{})
dbClient.AddInvariant(setNames, &sqltypes.Result{})
dbClient.AddInvariant(setNetReadTimeout, &sqltypes.Result{})
dbClient.AddInvariant(setNetWriteTimeout, &sqltypes.Result{})

Expand Down
5 changes: 5 additions & 0 deletions go/vt/vttablet/tabletmanager/vreplication/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ func setDBClientSettings(dbClient binlogplayer.DBClient, workflowConfig *vttable
if _, err := dbClient.ExecuteFetch("set @@session.time_zone = '+00:00'", maxRows); err != nil {
return err
}
// Tables may have varying character sets. To ship the bits without interpreting them
// we set the character set to be binary.
if _, err := dbClient.ExecuteFetch("set names 'binary'", maxRows); err != nil {
return err
}
if _, err := dbClient.ExecuteFetch(fmt.Sprintf("set @@session.net_read_timeout = %v",
workflowConfig.NetReadTimeout), maxRows); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion proto/binlogdata.proto
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ message RowChange {
// set if the value in the after image is a partial JSON value that
// is represented as an expression of
// JSON_[INSERT|REPLACE|REMOVE](%s, '$.path', value) which then is
// used to add/update/replace a path in the JSON document. When the
// used to add/update/remove a path in the JSON document. When the
// value is used the fmt directive must be replaced by the actual
// column name of the JSON field.
Bitmap json_partial_values = 4;
Expand Down

0 comments on commit 7b75ed6

Please sign in to comment.