Skip to content
New issue

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

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

Already on GitHub? # to your account

[FEATURE]: Date and datetime functions for DQL #8687

Open
mrwunderbar666 opened this issue Feb 20, 2023 · 4 comments
Open

[FEATURE]: Date and datetime functions for DQL #8687

mrwunderbar666 opened this issue Feb 20, 2023 · 4 comments
Labels
area/querylang Issues related to the query language specification and implementation. community Issue or PR created by the community. kind/feature Something completely new we should consider.

Comments

@mrwunderbar666
Copy link

mrwunderbar666 commented Feb 20, 2023

Use case

It would be extremely useful to cast datetime types to other formats, such as year, month, or day. This way, we could generate meaningful aggregations on datetime types. Example scenario: a simple forum where users can make posts and comments. Each post and comment has a "created date" which is a datetime timestamp. We could then count all posts made in a month (or on a particular day)

Links to Discuss, RFC or previous Issues and PRs

https://discuss.dgraph.io/t/how-to-groupby-date/3040
https://discuss.dgraph.io/t/datetime-functions-are-needed/9694
https://discuss.dgraph.io/t/what-is-dgraph-lacking/16010/79?u=mrwunderbar666

Links to examples and research

For example see postgresql where you can cast a timestamp to year:

created_date::year
OR
date_part('year', created_date)

Current state

According to my knowledge the current workaround is to create separate fields for year, month, day (in the client application for DGraph) and then run aggregations on these self generated fields. Another solution is the math() function with since(): https://dgraph.io/docs/query-language/math-on-value-variables/

Solution proposal

Example query to aggregate on year:

q(func: has(created_date)) @groupby(YEAR(created_date)) {
    count(uid)
}

Other variant:

q(func: has(created_date)) @groupby(datetime(created_date, year)) {
    count(uid)
}

Additional Information

No response

@mrwunderbar666 mrwunderbar666 added the kind/feature Something completely new we should consider. label Feb 20, 2023
@rderbier rderbier added the area/querylang Issues related to the query language specification and implementation. label Feb 21, 2023
@MichelDiz
Copy link
Contributor

@mrwunderbar666 what is the difference between the two examples?

BTW, once this #8538 is merged into main. I think this request will be partially addressed.

And then to add the YEAR(created_date) function we could sanitize the other values like hour, day and month and leave just the year.

So YEAR() could be just a method that returns only the year.

@mrwunderbar666
Copy link
Author

That's good news.

You mean the difference between the PostgreSQL examples? This is just two ways to so the same thing in PostgreSQL: cast datetime to year. I just find the double colon syntax very ergonomic :)

@MichelDiz MichelDiz added the community Issue or PR created by the community. label Mar 7, 2023
Copy link

This issue has been stale for 60 days and will be closed automatically in 7 days. Comment to keep it open.

@github-actions github-actions bot added the Stale label Jul 25, 2024
@mrwunderbar666
Copy link
Author

Still important I think

@github-actions github-actions bot removed the Stale label Jul 27, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
area/querylang Issues related to the query language specification and implementation. community Issue or PR created by the community. kind/feature Something completely new we should consider.
Development

No branches or pull requests

3 participants