Skip to content

Commit f7a950c

Browse files
youknowriadclaude
andcommitted
Fix BINARY and VARBINARY column defaults
Literal defaults on these columns were stored as written and then treated as text. SHOW CREATE TABLE turned x'00' into the string 'x''00''', which MySQL rejects, and inserts that omitted the column failed because a STRICT BLOB column can't hold a TEXT default. Store the defaults in hex like MySQL's INFORMATION_SCHEMA, write them to SQLite as blobs, and read the forms that older versions stored. Related to #551. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 70bdaf3 commit f7a950c

3 files changed

Lines changed: 197 additions & 2 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7895,6 +7895,12 @@ private function format_sqlite_column_default( array $column ): string {
78957895
return (string) bindec( substr( $column['COLUMN_DEFAULT'], 2, -1 ) );
78967896
}
78977897

7898+
// A BLOB column in a STRICT table only accepts a BLOB default.
7899+
if ( 'binary' === $column['DATA_TYPE'] || 'varbinary' === $column['DATA_TYPE'] ) {
7900+
$bytes = WP_SQLite_Information_Schema_Builder::get_binary_default_bytes( $column['COLUMN_DEFAULT'] );
7901+
return sprintf( "x'%s'", bin2hex( $bytes ) );
7902+
}
7903+
78987904
return $this->quote_sqlite_value( $column['COLUMN_DEFAULT'] );
78997905
}
79007906

@@ -7910,6 +7916,14 @@ private function format_mysql_column_default( array $column ): string {
79107916
return $column['COLUMN_DEFAULT'];
79117917
}
79127918

7919+
// Like MySQL, use a hex literal for bytes that aren't valid UTF-8.
7920+
if ( 'binary' === $column['DATA_TYPE'] || 'varbinary' === $column['DATA_TYPE'] ) {
7921+
$bytes = WP_SQLite_Information_Schema_Builder::get_binary_default_bytes( $column['COLUMN_DEFAULT'] );
7922+
return 1 === preg_match( '//u', $bytes )
7923+
? $this->quote_mysql_utf8_string_literal( $bytes )
7924+
: '0x' . strtoupper( bin2hex( $bytes ) );
7925+
}
7926+
79137927
return $this->quote_mysql_utf8_string_literal( $column['COLUMN_DEFAULT'] );
79147928
}
79157929

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php‎

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,34 @@ public function record_drop_index( WP_Parser_Node $node ): void {
878878
$this->record_drop_index_data( $table_is_temporary, $table_name, $index_name );
879879
}
880880

881+
/**
882+
* Get the bytes of a BINARY or VARBINARY column default.
883+
*
884+
* Defaults are stored as "0x..." hex literals. In tables from older versions,
885+
* they can also be any other hex or bit literal as written, or the bytes of
886+
* a text default.
887+
*
888+
* @param string $default_value A stored default, or a hex or bit literal.
889+
* @return string The default value bytes.
890+
*/
891+
public static function get_binary_default_bytes( string $default_value ): string {
892+
if ( 1 === preg_match( "/\A(?|0x([0-9a-fA-F]+)|[xX]'([0-9a-fA-F]*)')\z/", $default_value, $matches ) ) {
893+
// MySQL pads odd-length 0x literals with a leading zero.
894+
return hex2bin( 1 === strlen( $matches[1] ) % 2 ? '0' . $matches[1] : $matches[1] );
895+
}
896+
897+
if ( 1 === preg_match( "/\A(?|0b([01]+)|[bB]'([01]*)')\z/", $default_value, $matches ) ) {
898+
$bits = str_pad( $matches[1], (int) ceil( strlen( $matches[1] ) / 8 ) * 8, '0', STR_PAD_LEFT );
899+
$bytes = '';
900+
for ( $i = 0; $i < strlen( $bits ); $i += 8 ) {
901+
$bytes .= chr( bindec( substr( $bits, $i, 8 ) ) );
902+
}
903+
return $bytes;
904+
}
905+
906+
return $default_value;
907+
}
908+
881909
/**
882910
* Analyze ADD COLUMN definition and record data in the information schema.
883911
*
@@ -2274,6 +2302,10 @@ private function get_literal_default( WP_Parser_Node $literal, string $data_type
22742302
return null;
22752303
}
22762304

2305+
if ( 'binary' === $data_type || 'varbinary' === $data_type ) {
2306+
return $this->get_binary_literal_default( $literal );
2307+
}
2308+
22772309
// DEFAULT TRUE or DEFAULT FALSE
22782310
if ( $literal->has_child_node( 'boolLiteral' ) ) {
22792311
$bool_literal = $literal->get_first_child_node( 'boolLiteral' );
@@ -2315,11 +2347,14 @@ private function normalize_literal_default( string $default_value, string $data_
23152347
return $bit_default;
23162348
}
23172349

2350+
if ( 'binary' === $data_type || 'varbinary' === $data_type ) {
2351+
return $this->format_binary_default( $default_value );
2352+
}
2353+
23182354
/*
2319-
* @TODO: Non-BIT literal defaults, except for temporal zero values, are
2355+
* @TODO: Other literal defaults, except for temporal zero values, are
23202356
* currently stored verbatim. To match MySQL, they should be normalized.
23212357
* For example:
2322-
* - BINARY/VARBINARY defaults stored as 0x hex literals.
23232358
* - Hex and bit literals coerced to raw bytes for text columns and to
23242359
* their decimal value for numeric columns.
23252360
* - The charset introducer dropped from text-literal defaults.
@@ -2328,6 +2363,42 @@ private function normalize_literal_default( string $default_value, string $data_
23282363
return $default_value;
23292364
}
23302365

2366+
/**
2367+
* Get the default of a BINARY or VARBINARY column from a literal.
2368+
*
2369+
* Hex and bit literals are byte strings. Other literals give the bytes of
2370+
* their text, so the text '0x41' stays distinct from the hex literal 0x41.
2371+
*
2372+
* @param WP_Parser_Node $literal The "literal" AST node.
2373+
* @return string The default value as stored in information schema.
2374+
*/
2375+
private function get_binary_literal_default( WP_Parser_Node $literal ): string {
2376+
$byte_string = $literal->get_first_child_token( WP_MySQL_Lexer::HEX_NUMBER )
2377+
?? $literal->get_first_child_token( WP_MySQL_Lexer::BIN_NUMBER );
2378+
if ( null !== $byte_string ) {
2379+
return $this->format_binary_default( self::get_binary_default_bytes( $byte_string->get_value() ) );
2380+
}
2381+
2382+
$bool_literal = $literal->get_first_child_node( 'boolLiteral' );
2383+
if ( null !== $bool_literal ) {
2384+
return $this->format_binary_default( $bool_literal->has_child_token( WP_MySQL_Lexer::TRUE_SYMBOL ) ? '1' : '0' );
2385+
}
2386+
2387+
return $this->format_binary_default( $this->get_value( $literal ) );
2388+
}
2389+
2390+
/**
2391+
* Format the bytes of a BINARY or VARBINARY default for the information schema.
2392+
*
2393+
* Like in MySQL, the bytes are shown in hex, except for an empty default.
2394+
*
2395+
* @param string $bytes The default value bytes.
2396+
* @return string The default value as stored in information schema.
2397+
*/
2398+
private function format_binary_default( string $bytes ): string {
2399+
return '' === $bytes ? '' : '0x' . strtoupper( bin2hex( $bytes ) );
2400+
}
2401+
23312402
/**
23322403
* Get the zero value of a temporal column when its default is zero.
23332404
*

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,116 @@ public function testShowCreateTableWithDefaultValues(): void {
772772
);
773773
}
774774

775+
/**
776+
* @dataProvider data_binary_column_defaults
777+
*/
778+
public function testBinaryColumnDefaults(
779+
string $column_type,
780+
string $default_value,
781+
string $expected_create_default,
782+
string $expected_information_schema_default,
783+
string $expected_value
784+
): void {
785+
$this->assertQuery( "CREATE TABLE t (id INT PRIMARY KEY, v $column_type NOT NULL DEFAULT $default_value)" );
786+
787+
$this->assertQuery( 'SHOW CREATE TABLE t' );
788+
$this->assertStringContainsString(
789+
"`v` $column_type NOT NULL DEFAULT $expected_create_default,",
790+
$this->last_result[0]->{'Create Table'}
791+
);
792+
793+
$this->assertQuery( "SELECT COLUMN_DEFAULT FROM information_schema.columns WHERE table_name = 't' AND column_name = 'v'" );
794+
$this->assertSame( $expected_information_schema_default, $this->last_result[0]->COLUMN_DEFAULT );
795+
796+
$this->assertQuery( 'INSERT INTO t (id) VALUES (1)' );
797+
$this->assertQuery( "INSERT INTO t VALUES (2, 'z')" );
798+
$this->assertQuery( 'UPDATE t SET v = DEFAULT WHERE id = 2' );
799+
$this->assertQuery( 'SELECT HEX(v) AS v FROM t ORDER BY id' );
800+
$this->assertSame(
801+
array( $expected_value, $expected_value ),
802+
array_column( $this->last_result, 'v' )
803+
);
804+
}
805+
806+
public static function data_binary_column_defaults(): array {
807+
return array(
808+
'hex number' => array( 'varbinary(4)', '0xDEADBEEF', '0xDEADBEEF', '0xDEADBEEF', 'DEADBEEF' ),
809+
'hex string' => array( 'varbinary(4)', "x'deadbeef'", '0xDEADBEEF', '0xDEADBEEF', 'DEADBEEF' ),
810+
'uppercase string' => array( 'varbinary(4)', "X'deadbeef'", '0xDEADBEEF', '0xDEADBEEF', 'DEADBEEF' ),
811+
'odd-length hex' => array( 'varbinary(4)', '0xa', "'\\n'", '0x0A', '0A' ),
812+
'bit string' => array( 'varbinary(4)', "b'01000001'", "'A'", '0x41', '41' ),
813+
'bit number' => array( 'varbinary(4)', '0b01000001', "'A'", '0x41', '41' ),
814+
'zero bytes' => array( 'binary(2)', "x'0000'", "'\\0\\0'", '0x0000', '0000' ),
815+
'escaped zero bytes' => array( 'binary(2)', "'\\0\\0'", "'\\0\\0'", '0x0000', '0000' ),
816+
'text' => array( 'varbinary(4)', "'F'", "'F'", '0x46', '46' ),
817+
'quoted text' => array( 'varbinary(4)', "'it''s'", "'it''s'", '0x69742773', '69742773' ),
818+
'empty text' => array( 'varbinary(4)', "''", "''", '', '' ),
819+
'text like a number' => array( 'varbinary(4)', "'0x41'", "'0x41'", '0x30783431', '30783431' ),
820+
'text like a string' => array( 'varbinary(8)', "'x''41'''", "'x''41'''", '0x7827343127', '7827343127' ),
821+
'number' => array( 'varbinary(4)', '5', "'5'", '0x35', '35' ),
822+
'negative number' => array( 'varbinary(4)', '-5', "'-5'", '0x2D35', '2D35' ),
823+
'true' => array( 'varbinary(4)', 'TRUE', "'1'", '0x31', '31' ),
824+
'false' => array( 'varbinary(4)', 'FALSE', "'0'", '0x30', '30' ),
825+
);
826+
}
827+
828+
public function testBinaryColumnSetDefault(): void {
829+
$this->assertQuery( "CREATE TABLE t (id INT PRIMARY KEY, v VARBINARY(4) NOT NULL DEFAULT 'a')" );
830+
831+
$this->assertQuery( 'ALTER TABLE t ALTER COLUMN v SET DEFAULT 0xDEADBEEF' );
832+
833+
$this->assertQuery( 'SHOW CREATE TABLE t' );
834+
$this->assertStringContainsString(
835+
'`v` varbinary(4) NOT NULL DEFAULT 0xDEADBEEF,',
836+
$this->last_result[0]->{'Create Table'}
837+
);
838+
$this->assertQuery( 'INSERT INTO t (id) VALUES (1)' );
839+
$this->assertQuery( 'SELECT HEX(v) AS v FROM t' );
840+
$this->assertSame( 'DEADBEEF', $this->last_result[0]->v );
841+
}
842+
843+
/**
844+
* Older versions stored most BINARY and VARBINARY defaults as written.
845+
*
846+
* @dataProvider data_binary_column_defaults_from_older_versions
847+
*/
848+
public function testBinaryColumnDefaultsFromOlderVersions(
849+
string $stored_default,
850+
string $expected_create_default,
851+
string $expected_value
852+
): void {
853+
$this->assertQuery( 'CREATE TABLE t (id INT PRIMARY KEY, v VARBINARY(16) NOT NULL)' );
854+
$statement = $this->sqlite->prepare(
855+
"UPDATE _wp_sqlite_mysql_information_schema_columns SET COLUMN_DEFAULT = ? WHERE TABLE_NAME = 't' AND COLUMN_NAME = 'v'"
856+
);
857+
$statement->execute( array( $stored_default ) );
858+
859+
$this->assertQuery( 'SHOW CREATE TABLE t' );
860+
$this->assertStringContainsString(
861+
"`v` varbinary(16) NOT NULL DEFAULT $expected_create_default,",
862+
$this->last_result[0]->{'Create Table'}
863+
);
864+
865+
// A table rebuild writes the column default to SQLite as bytes.
866+
$this->assertQuery( 'ALTER TABLE t MODIFY COLUMN id BIGINT' );
867+
$this->assertQuery( 'INSERT INTO t (id) VALUES (1)' );
868+
$this->assertQuery( 'SELECT HEX(v) AS v FROM t' );
869+
$this->assertSame( $expected_value, $this->last_result[0]->v );
870+
}
871+
872+
public static function data_binary_column_defaults_from_older_versions(): array {
873+
return array(
874+
'hex string' => array( "x'00000000000000000000000000000000'", "'\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0'", '00000000000000000000000000000000' ),
875+
'uppercase hex string' => array( "X'DEADBEEF'", '0xDEADBEEF', 'DEADBEEF' ),
876+
'hex number' => array( '0xdeadbeef', '0xDEADBEEF', 'DEADBEEF' ),
877+
'bit string' => array( "b'01000001'", "'A'", '41' ),
878+
'bit number' => array( '0b01000001', "'A'", '41' ),
879+
'zero bytes' => array( "\0\0", "'\\0\\0'", '0000' ),
880+
'text' => array( 'abcd', "'abcd'", '61626364' ),
881+
'empty text' => array( '', "''", '' ),
882+
);
883+
}
884+
775885
public function testShowCreateTableWithBitDefaultValues(): void {
776886
$this->assertQuery(
777887
"CREATE TABLE _tmp_bit_defaults (

0 commit comments

Comments
 (0)