Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for postgresql 'interval' type #60

Closed

Conversation

amarqueslee
Copy link
Contributor

This PR implements support for CalendarDiffTime as an analogue of the Postgresql interval type. My decision to use this type representation is based on the fact that both types claim to support bidirectional conversion to ISO 8601:2004(E) sec. 4.4.3.2.

I'll do a self-review to hopefully help ease the review process.

Comment on lines +131 to +135
. fromString
-- from the docs: "Beware: fromString truncates multi-byte characters to octets".
-- However, I think this is a safe usage, because ISO8601-encoding seems restricted
-- to ASCII output.
. iso8601Show
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to avoid too much logic to do with Builders and Parsers for this type, I thought it would be more maintainable in the long-term to trust the time package's out-of-the-box ISO formatter where possible.

Comment on lines +203 to +204
contents <- takeByteString
iso8601ParseM $ B8.unpack contents
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parser is used here to leverage its instance of MonadFail, which iso8601ParseM needs. Ideally I wanted to use some instance MonadFail (Either String) instance, but apparently this deliberately doesn't exist in base.

See https://gitlab.haskell.org/ghc/ghc/-/issues/12160 for more.

Comment on lines +490 to +495
-- | interval. Requires you to configure intervalstyle as @iso_8601@.
--
-- You can configure intervalstyle on every connection with a @SET@ command,
-- but for better performance you may want to configure it permanently in the
-- file found with @SHOW config_file;@ .
--
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://www.postgresql.org/docs/12/datatype-datetime.html#DATATYPE-INTERVAL-OUTPUT for documentation on different interval output formats.

This was an unexpected stumbling block for me, it seems you have to ask Postgres to give you ISO-compliant intervals, with the default alternative being a vendor-specific postgres format. Searching for intervalstyle reveals that it's quite popular for developers to want to set this to something ISO8601-compliant, so hopefully this won't detract too much from adoption.

The long-term alternative is probably to support the four possible intervalstyles available, so that the user doesn't need to worry about this problem.

Also, this may be a latent issue with UTCTime as well. timestamptz also seems to offer multiple output formats, and I got the sense from browsing the parsers that this library only supports the default one. Check https://www.postgresql.org/docs/12/datatype-datetime.html#DATATYPE-DATETIME-OUTPUT .

, bytestring >=0.10.0.0 && <0.12
, containers >=0.5.0.0 && <0.7
, template-haskell >=2.8.0.0 && <2.17
, text >=1.2.3.0 && <1.3
, time >=1.4.0.1 && <1.12
, time >=1.9.0.0 && <1.12
Copy link
Contributor Author

@amarqueslee amarqueslee Dec 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI noticed that time only supports CalendarDiffTime from version 1.9 onwards. Since 1.9 was released in January 2018, I just bumped the version, but if using #if MIN_VERSION_time would be more appropriate, I'm open to doing that too.

Copy link
Collaborator

@phadej phadej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely needs tests.

@phadej
Copy link
Collaborator

phadej commented Dec 17, 2020

Don't spend time with CI (changing bounds is not good idea, and I rather fix that afterwards). Make it work where it works. Add tests.

@amarqueslee
Copy link
Contributor Author

I'll shift my focus to tests, thanks.

@phadej
Copy link
Collaborator

phadej commented Jan 6, 2021

Cleaned up in #64.
Thanks!

@phadej phadej closed this Jan 6, 2021
@phadej
Copy link
Collaborator

phadej commented Jan 6, 2021

Released in postgresql-0.6.4

@amarqueslee
Copy link
Contributor Author

Likewise, thanks for hoisting my change out of dependency hell and releasing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants