yesodweb / yesodweb/persistent

Word64 does not roundtrip through a database correctly

Open
#1,552 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haskell
Stars
486
Forks
306
PR merge metrics
No merged PRs in 30d

Description

Bug Reports

The Word64 instance for PersistField does not roundtrip through databases correctly.
It uses fromIntegral for converting between Word64 and Int64.

While this does technically roundtrip, the database values will be silently wrong because negative values will be saved instead of positive ones.

Example:

This does technically roundtrip:

ghci> import Data.Word
ghci> import Data.Int
ghci> 2^64
18446744073709551616
ghci> fromIntegral (18446744073709551615  :: Word64) :: Int64
-1
ghci> fromIntegral (-1 :: Int64) :: Word64
18446744073709551615

BUT the database will contain -1, which does not sort in the same way.

Solutions

You could have another PersistValue constructor for Word64, but I'm not sure if all the "supported" databases support such a type.
As far as I can tell, sqlite supports larger integers by storing them as reals but I don't know about other databases:

sqlite> create table t1 (i INTEGER);
sqlite> insert into t1 values(18446744073709551615);
sqlite> insert into t1 values(0);
sqlite> insert into t1 values(1);
sqlite> select * from t1 order by i;
0
1
1.84467440737096e+19
sqlite> select typeof(i) from t1;
real
integer
integer

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the Haskell Word64 PersistField instance and the PersistValue handling used by the supported database backends. Compare how each backend stores and orders values beyond Int64, using the SQLite behavior described in the issue as a reference. Done means Word64 values retain their correct values and sort order across supported databases.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
databases
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.