Skip to content

vreplication: preserve MySQL JSON types from binlog#20120

Open
herbenderbler wants to merge 2 commits into
vitessio:mainfrom
herbenderbler:ISSUE-19880/json-typo-info-loss-in-vreplication
Open

vreplication: preserve MySQL JSON types from binlog#20120
herbenderbler wants to merge 2 commits into
vitessio:mainfrom
herbenderbler:ISSUE-19880/json-typo-info-loss-in-vreplication

Conversation

@herbenderbler
Copy link
Copy Markdown

Description

VReplication was losing MySQL JSON internal types (DATE, TIME, DATETIME, etc.) on the binlog path: binary JSON was marshaled to plain text JSON before gRPC, and the replicator only re-encoded standard JSON literals on apply.
This PR keeps typed JSON through the pipeline by using MarshalSQLTo in the binlog reader and passing preserialized JSON SQL through replicator_plan instead of round-tripping via MarshalSQLValue.

Fixes #19880

Related Issue(s)

Fixes #19880

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

User-visible correctness fix for VReplication JSON columns with typed values (e.g. JSON_TYPE returns DATE instead of STRING after replicate). No schema or flag changes.

herbenderbler and others added 2 commits May 16, 2026 11:18
Use MarshalSQLTo on the vstreamer binlog path instead of MarshalTo so
DATE, TIME, DATETIME, and other opaque JSON types are not flattened to
text. Teach the replicator to pass through preserialized SQL expressions
via JSONSQLValue and AppendJSONSQL.

Fixes vitessio#19880

Signed-off-by: John Claus <johnclaus@gmail.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Use package-level prefix slices, check JSON_OBJECT first, trim whitespace
once for detection and passthrough, and fast-reject standard JSON text roots.

Signed-off-by: John Claus <johnclaus@gmail.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Copilot AI review requested due to automatic review settings May 16, 2026 17:37
@github-actions github-actions Bot added this to the v25.0.0 milestone May 16, 2026
@vitess-bot vitess-bot Bot added NeedsWebsiteDocsUpdate What it says NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels May 16, 2026
@vitess-bot
Copy link
Copy Markdown
Contributor

vitess-bot Bot commented May 16, 2026

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes VReplication's loss of MySQL JSON internal type tags (DATE, TIME, DATETIME, DECIMAL, BIT, BLOB) by preserving the typed SQL form of JSON values end-to-end. On the binlog read path, CellValue now uses MarshalSQLTo (which emits JSON_OBJECT(...)/JSON_ARRAY(...)/CAST(... as JSON) expressions) instead of MarshalTo (which produced plain JSON text). The replicator's apply path is taught to recognize and pass through these pre-serialized SQL expressions via new helpers JSONSQLValue and AppendJSONSQL, falling back to MarshalSQLValue/AppendMarshalSQL for legacy text JSON (e.g., from initial table copy).

Changes:

  • Switch binlog cell decoding to emit typed SQL JSON expressions (MarshalSQLTo).
  • Add IsPreserializedJSONSQL, JSONSQLValue, and AppendJSONSQL helpers that detect SQL-form JSON by top-level prefix (JSON_OBJECT(/JSON_ARRAY(/CAST() and bypass re-encoding.
  • Route the four VReplication apply call sites through the new helpers and add regression tests at all layers.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
go/mysql/binlog/rbr.go Use MarshalSQLTo so JSON cell values carry SQL with type info.
go/mysql/binlog/rbr_test.go Update expectations to SQL form; add DATE case.
go/mysql/json/marshal.go New prefix-based detection plus JSONSQLValue / AppendJSONSQL.
go/mysql/json/marshal_test.go Unit tests for new helpers and detection heuristic.
go/vt/vttablet/tabletmanager/vreplication/replicator_plan.go Use new helpers at all JSON SQL-binding sites.
go/vt/vttablet/tabletmanager/vreplication/replicator_plan_test.go Regression tests proving typed SQL is preserved.

Comment thread go/mysql/json/marshal.go
Comment on lines +171 to +173
prefixJSONObject = []byte("JSON_OBJECT(")
prefixJSONArray = []byte("JSON_ARRAY(")
prefixCAST = []byte("CAST(")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels very hacky to me. 🤔

Copy link
Copy Markdown
Author

@herbenderbler herbenderbler May 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels very hacky to me. 🤔

The prefix check is a workaround for type erasure on the VReplication wire path. binlog CellValue already returns sqltypes.Expression for typed JSON, but RowToProto3 only ships bytes and the target reconstructs the cell as Type_JSON. That being said, I can’t tell SQL expressions from text JSON without either a heuristic or extra metadata (similar to json_partial_values for partial updates). The prefixes match exactly what MarshalSQLTo emits. text JSON is rejected via {/[/".

I'm happy to switch to an explicit RowChange bitmap in a follow-up (or something else) if you’d prefer that over sniffing.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.05%. Comparing base (70c7a72) to head (19b2d18).
⚠️ Report is 261 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main   #20120       +/-   ##
===========================================
+ Coverage   69.67%   78.05%    +8.38%     
===========================================
  Files        1614       37     -1577     
  Lines      216793     7623   -209170     
===========================================
- Hits       151044     5950   -145094     
+ Misses      65749     1673    -64076     
Flag Coverage Δ
partial 78.05% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Component: TabletManager Component: VTTablet NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug Report: VReplication loses JSON type information during binary-to-text roundtrip

3 participants