You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Emulate UNSIGNED integer semantics for CAST and CONVERT #359
MySQL's CAST(expr AS UNSIGNED) and CONVERT(expr, UNSIGNED) wrap negative values using modular arithmetic (2^64). For example, CAST('-10' AS UNSIGNED) returns 18446744073709551606 in MySQL.
SQLite has no unsigned integer type — its integers are always 64-bit signed. Currently, CAST('-10' AS UNSIGNED) returns -10 in the SQLite driver, which is incorrect.
Scope
The UNSIGNED semantics may need to be emulated in multiple places:
CAST(expr AS UNSIGNED) expressions.
CONVERT(expr, UNSIGNED) expressions.
Potentially other contexts where UNSIGNED integer behavior is expected (e.g., column types, arithmetic).
Hi, @JanJakes I'd like to pick this up. I compared trunk with MySQL 8.4 and found two problems with CAST/CONVERT to UNSIGNED:
Negative values don't wrap around.CAST(-10 AS UNSIGNED) returns -10. MySQL returns 2^64 − 10.
Large values are capped at the signed max.CAST('18446744073709551615' AS UNSIGNED) (the unsigned max) returns 2^63 − 1 instead of itself.
Design question: the correct results are often above the signed 64-bit max, so SQLite can't store them as integers. There are two options:
TEXT: the value stays exact and matches what PDO MySQL returns (a string). But SQLite compares text differently from numbers, so comparisons and ORDER BY against integers would silently give wrong results.
REAL: comparisons behave like numbers, but precision is lost. For example, CAST(-1 AS UNSIGNED) and CAST(-10 AS UNSIGNED) would come out as the same value.
Activity
the-hercules commentedon Nov 13, 2026
Hi, @JanJakes I'd like to pick this up. I compared trunk with MySQL 8.4 and found two problems with
CAST/CONVERTtoUNSIGNED:CAST(-10 AS UNSIGNED)returns-10. MySQL returns 2^64 − 10.CAST('18446744073709551615' AS UNSIGNED)(the unsigned max) returns 2^63 − 1 instead of itself.Design question: the correct results are often above the signed 64-bit max, so SQLite can't store them as integers. There are two options:
ORDER BYagainst integers would silently give wrong results.CAST(-1 AS UNSIGNED)andCAST(-10 AS UNSIGNED)would come out as the same value.Which would you prefer?