-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix problems when altering a column from binary to varbinary
#1628
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
Conversation
MySQL's binlog strips trailing 0x00 bytes from binary(N) columns. PR github#915 fixed this for unique key columns only, but the same issue affects all binary columns in INSERT/UPDATE operations. Remove the isUniqueKeyColumn condition so all binary(N) columns are padded to their declared length. Fixes a variation of github#909 where the affected column is not a primary key.
There was a problem hiding this 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 addresses incorrect handling of BINARY(N) values read from MySQL binlog events during migrations that alter a column to VARBINARY(M), where trailing 0x00 bytes can be stripped and thus written incorrectly to the ghost table. The fix generalizes the existing padding logic so it applies to all binary columns, not just unique-key columns.
Changes:
- Apply trailing-zero padding for all
BinaryColumnTypecolumns during argument conversion (not only unique key columns). - Add unit tests covering binary padding behavior in
convertArg. - Add a new local integration test (
localtests/binary-to-varbinary) to reproduce thebinary -> varbinarybinlog truncation scenario.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
go/sql/types.go |
Expands binary padding logic to apply to all binary columns. |
go/sql/types_test.go |
Adds unit tests verifying padding behavior for truncated/full binary values. |
localtests/binary-to-varbinary/create.sql |
Creates a repro scenario with binlog-driven inserts/updates involving trailing-zero binary values. |
localtests/binary-to-varbinary/extra_args |
Supplies an --alter that changes data from binary(20) to varbinary(32). |
Comments suppressed due to low confidence (1)
go/sql/types.go:87
- The padding logic builds a
bytes.Bufferdirectly fromarg2Bytes. Sincebytes.NewBuffercan reuse the provided slice as its backing store,Writemay mutate the input byte slice in-place (depending on capacity). Now that this code runs for all binary columns, it would be safer to avoid mutating the input by copying into a new buffer/slice before padding (and ideally keep the return type as[]byteto avoid unnecessary string conversions).
buf := bytes.NewBuffer(arg2Bytes)
for i := uint(0); i < (this.BinaryOctetLength - uint(size)); i++ {
buf.Write([]byte{0})
}
arg = buf.String()
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
In this case, the input is binary, and the column type is `binary`. So the output should be binary, not text.
|
Thanks for the fix. I do wonder why the author of #915 chose to apply the padding only to the unique key 🤔 |
I think this comment has a clue:
Guess that wasn't quite true... |
Description
Fixes #1627.
The issue is that binary values are incorrectly read from the binlog. This PR fixes it by changing a previously-implemented fix to apply to all binary columns, not only primary keys.
Two new tests are added: a unit test for the function being changed, and a localtest. (Oddly, the existing test in
localtests/varbinarydoesn't seem to actually involve anyvarbinarycolumns.)script/cibuildreturns with no formatting errors, build errors or unit test errors.I wasn't able to figure out how to runFigured it out, it was trivial - my version of Go was too new.script/buildorscript/cibuild. They both try to download and use a vendored Go installation, but this doesn't work for me.