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

Extract Epoch #291

Closed
xnmp opened this issue Apr 20, 2017 · 5 comments
Closed

Extract Epoch #291

xnmp opened this issue Apr 20, 2017 · 5 comments

Comments

@xnmp
Copy link

xnmp commented Apr 20, 2017

Hi everyone, I'm just wondering how to get Postgres functions in Opaleye, in particular how can I get the following?

SELECT
  project_id,
  avg(EXTRACT(EPOCH FROM (accept_timestamp - award_timestamp)))  avg_accept_time
FROM projects
GROUP BY 1;

Thanks in advance.

@tomjaguarpaw
Copy link
Owner

This hasn't been implemented in Opaleye. It could be implemented in a similar way to the existing functions in https://github.com/tomjaguarpaw/haskell-opaleye/blob/master/src/Opaleye/Operators.hs

@xnmp
Copy link
Author

xnmp commented Apr 21, 2017

So how can I implement them? Is there an example somewhere? Thanks.

@xnmp
Copy link
Author

xnmp commented Apr 21, 2017

Answering my own question: I did it like this:

epoch :: Column LocalTime -> Column Int
epoch = extract . epoch'
  where
    extract (Col.Column a) = Col.Column (HPQ.FunExpr "extract" [a])
    epoch' (Col.Column a) = Col.Column (HPQ.FunExpr "epoch from" [a])

@tomjaguarpaw
Copy link
Owner

Great! Isn't it slightly nicer to do

epoch :: Column LocalTime -> Column Int
epoch = Col.Column . extract . epoch' . unColumn
  where
    extract a = HPQ.FunExpr "extract" [a]
    epoch' a = HPQ.FunExpr "epoch from" [a]
    unColumn (Column a) = a

Would you be able to submit this as a PR?

@tomjaguarpaw
Copy link
Owner

This seems resolved. Feel free to reopen if not.

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

No branches or pull requests

2 participants