Skip to content

Emulate UNSIGNED integer semantics for CAST and CONVERT #359

Open

Description

@JanJakes

Problem

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).

References

Activity

  1. the-hercules commented on Nov 13, 2026

    @the-hercules

    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:

    1. Negative values don't wrap around. CAST(-10 AS UNSIGNED) returns -10. MySQL returns 2^64 − 10.
    2. 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.

    Which would you prefer?

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