Skip to content

Hex literals return wrong results in numeric context and text comparisons #575

Open

Description

@JanJakes

Problem

The driver translates every hexadecimal literal to an SQLite blob (x'…'). MySQL treats a hex literal as a binary string by default, but as a number in numeric context. MySQL also compares a hex literal with a text value byte by byte. SQLite does neither: a blob converts to 0 in arithmetic, and a blob never equals a text value.

Reproduction

SELECT 0x1 + 0, 5 & 0x1, CAST(0x711 AS UNSIGNED);

CREATE TABLE t (id INT PRIMARY KEY, title TEXT, n INT);
INSERT INTO t VALUES (1, 'AB', 1);
SELECT COUNT(*) FROM t WHERE title = 0x4142;
INSERT INTO t VALUES (2, NULL, 0x711);
MySQL Driver
0x1 + 0 1 0
5 & 0x1 1 0
CAST(0x711 AS UNSIGNED) 1809 0
WHERE title = 0x4142 1 row 0 rows
Insert 0x711 into an INT column Stores 1809 Error 19: cannot store BLOB value in INTEGER column t.n

MySQL results are from MySQL 8.4. Driver results are from #573 at cd4384bc, with PHP 8.5.12 and SQLite 3.54.1. Even-length literals, such as 0x01 + 0, behave the same way on trunk.

This is uncommon in WordPress. Hex literals there mostly come from SQL dumps of binary columns, such as mysqldump --hex-blob, and those already work. Only the INSERT fails with an error. The other cases return wrong results silently.

Related

#538 is a similar binary-versus-text comparison problem with UNHEX().

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions