yesodweb / yesodweb/persistent

Can 'persistent-postgresql' support infinity/-infinity?

Open
#972 4 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

It seems that we cannot use infinity value (e.g. Date) with persistent-postgresql.

The following is my solution now:

  1. Patch persistent-postgresql

https://github.com/txkaduo/persistent/commit/8188599e1029f9be9e1daa2afc924c6b504b4639

  1. Create orphan instances for Unbounded a
instance PersistField a => PersistField (Unbounded a) where
  toPersistValue PosInfinity = PersistText "infinity"
  toPersistValue NegInfinity = PersistText "-infinity"
  toPersistValue (Finite x)  = toPersistValue x

  fromPersistValue (PersistText "infinity")        = pure PosInfinity
  fromPersistValue (PersistText "-infinity")       = pure NegInfinity
  fromPersistValue (PersistDbSpecific "infinity")  = pure PosInfinity
  fromPersistValue (PersistDbSpecific "-infinity") = pure NegInfinity
  fromPersistValue (PersistByteString "infinity")  = pure PosInfinity
  fromPersistValue (PersistByteString "-infinity") = pure NegInfinity
  fromPersistValue x                               = fmap Finite $ fromPersistValue x

instance PersistFieldSql a => PersistFieldSql (Unbounded a) where
  sqlType _ = sqlType (Proxy :: Proxy a)


instance FromJSON a => FromJSON (Unbounded a) where
  parseJSON (A.String "infinity")  = pure PosInfinity
  parseJSON (A.String "-infinity") = pure NegInfinity
  parseJSON x                      = fmap Finite $ parseJSON x

instance ToJSON a => ToJSON (Unbounded a) where
  toJSON PosInfinity = A.String "infinity"
  toJSON NegInfinity = A.String "-infinity"
  toJSON (Finite x)  = toJSON x

It works for my application.

Is this proper solution?

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 with the persistent-postgresql support discussed in the issue and review the linked patch, then compare it with the proposed Unbounded instances. The work is done when PostgreSQL infinity and -infinity values can be stored and read correctly without breaking finite values.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell, postgresql
Domain
databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.