Skip to content

Save binary strings to BIT columns as their integer value - #650

Open
youknowriad wants to merge 7 commits into
translate-charset-introducersfrom
save-binary-strings-to-bit-columns
Open

youknowriad wants to merge 7 commits into
translate-charset-introducersfrom
save-binary-strings-to-bit-columns

Conversation

@youknowriad

@youknowriad youknowriad commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Based on #649, which makes the driver run _binary '…' literals. Merge that one first.

Summary

This PR saves binary strings to BIT columns as the integer value of their bytes, as MySQL does. b'101', 0x05, and _binary '\x05' all save 5. This applies to INSERT, REPLACE, and UPDATE.

Why

The driver stores BIT columns as SQLite integers. Hex, bit-value, and _binary literals are blobs in SQLite, which casts a blob to an integer as if it was text. So in non-strict mode, every such value was saved as 0, and in strict mode, the query failed.

Value saved to a BIT(8) column MySQL Before, non-strict mode Before, strict mode After
b'101' 5 0 cannot store BLOB value in INTEGER column 5
0x05 5 0 cannot store BLOB value in INTEGER column 5
_binary '<0x01 byte>' 1 0 cannot store BLOB value in INTEGER column 1
_binary '\0' 0 0 cannot store BLOB value in INTEGER column 0

mysqldump writes BIT values as _binary '…' strings, and imports run in non-strict mode, so every BIT value of an imported dump was silently saved as 0. This was reported for a site that uses Ninja Forms, whose nf3_forms table has four bit(1) columns: Automattic/studio#6406. A test imports a row of that table as mysqldump writes it.

Notes

  • Leading zero bytes are ignored, as MySQL does for binary strings. A value of more than 64 bits fails with an error. MySQL also rejects it in strict mode, but it saves the column's largest value in non-strict mode.
  • Values that use the 64th bit fail with an error. MySQL stores them, but SQLite integers are signed, and a table without STRICT would silently save them as a REAL (see Fix long bit literals and validate BIT widths and defaults #636).
  • In an UPDATE or ON DUPLICATE KEY UPDATE, the value is evaluated for each row, as before.
  • The MySQL results in this PR and its tests were checked against MySQL 8.4.13.
  • This PR doesn't change other BIT differences from MySQL:
    • Quoted strings are still read as numbers, so '1' saves 1. MySQL reads them as binary strings too, and saves 49.
    • The column width isn't checked, for any value.
    • Hex and bit-value literals of more than 8 bytes are saved when their extra bytes are zeros. MySQL rejects them, but the driver can't tell them apart from binary strings when it saves them.

🤖 Generated with Claude Code

In MySQL, a binary string saved to a BIT column stores the integer that its
bytes represent in big-endian order, so b'101', 0x05, and _binary '\x05' all
store 5. These literals are blobs in SQLite, which casts a blob to an integer
as text, so the driver saved them as 0 in non-strict mode, and it failed with
"cannot store BLOB value in INTEGER column" in strict mode.

On INSERT, REPLACE, and UPDATE, a blob saved to a BIT column is now converted
to its integer value. A value of more than 64 bits fails with an error.

See Automattic/studio#6406

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 17eb9cd6-00e7-43e0-b911-eeeeaa206894

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

youknowriad and others added 2 commits December 3, 2026 18:16
PDO SQLite in PHP < 8.4 truncates integers that user-defined functions return
to 32 bits, so a BIT value above 2^31 - 1 was saved wrongly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@youknowriad
youknowriad requested a review from JanJakes December 3, 2026 17:43
Checked against MySQL 8.4: it ignores leading zero bytes of a binary string
and rejects one with more than 8 other bytes with "Data too long". The test now
uses binary strings, because MySQL rejects hex literals of more than 8 bytes
even when the extra bytes are zeros.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@JanJakes JanJakes left a comment

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.

Thanks for fixing this! I left a few comments inline.

youknowriad and others added 3 commits December 5, 2026 07:17
SQLite evaluates a subquery that references no column only once, so an UPDATE
or ON DUPLICATE KEY UPDATE with an expression such as RAND() saved the same BIT
value to all rows. The conversion subquery now references the target column.

A BIT value that uses the 64th bit is now rejected. SQLite integers are signed,
and a table without STRICT silently saved such a value as a REAL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants