Skip to content

SQLite: Issues around support for unsigned long #36689

Open

Description

@roji

While working on JSON partial updates for ExecuteUpdate (#28766), I ran into some issues with corruption of ulong values - see code sample below. Although the below sample is JSON-specific (and maybe even specific to json_set), I suspect SQLite doesn't actually fully support ulong, and we maybe shouldn't have supported this in M.D.SQLite.

For now, I'll throw for ExecuteUpdate partial updates on ulong properties inside JSON.

using var connection = new SqliteConnection("Data Source=:memory:");
connection.Open();

using var command = connection.CreateCommand();
command.CommandText = """
CREATE TABLE foo (json TEXT);
INSERT INTO foo (json) VALUES ('{ "a": 8 }');
""";
command.ExecuteNonQuery();

// command.CommandText = """UPDATE foo SET json = json_set(json, '$.a', @p)""";
// command.Parameters.Add(new SqliteParameter("p", DbType.UInt64) { Value = ulong.MaxValue - 1 });
command.CommandText = """UPDATE foo SET json = json_set(json, '$.a', 18446744073709551615)""";

command.ExecuteNonQuery();

command.CommandText = "SELECT json FROM foo";
var json = (string)command.ExecuteScalar()!;

Console.WriteLine(json); // {"a":-2} when using a parameter, {"a":1.84467440737096e+19} when using a literal.

var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(json));
reader.Read();
reader.Read();
reader.Read();
var x = reader.GetUInt64(); // FormatException: Either the JSON value is not in a supported format, or is out of bounds for a UInt64

/cc @cincuranet, @AndriySvyryd (this probably also affects the update pipeline)

Activity

  1. added this to the Backlog milestone on Aug 31, 2025
  2. added theissue type on Aug 31, 2025
  3. cincuranet commented on Sep 1, 2025

    @cincuranet
    Contributor

    As the documentation says: UInt64: Large values overflow. Int64 is fine.

    I'm not sure we can do better here.

  4. roji commented on Sep 1, 2025

    @roji
    MemberAuthor

    OK, good that it's documented... And yeah, I agree there's probably not much we can do. I'd argue that if large values overflow, maybe ulong shouldn't be "supported" at all (given that it doesn't support half of the range, which is the main reason one would use ulong rather than long...). In other words, maybe let's bring up the question of removing support for it (breaking change)?

  5. removed this from the Backlog milestone on Sep 1, 2025
  6. added this to the Backlog milestone on Feb 19, 2026
  7. AraHaan commented on Apr 20, 2026

    @AraHaan
    Member

    It seems one can work around that problem by storing the data as TEXT and using a ValueConverter to convert between string and ulong 😂.

    Although it is sad that SQLite lacks support for this, while SQLServer seems to not mind at all with ulong.

  8. roji commented on Apr 20, 2026

    @roji
    MemberAuthor

    @AraHaan AFAIK SQL Server doesn't support ulong either, really - EF simply value-converts implicitly from ulong to bigint (which is signed long).

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

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions