[FEATURE]: Date and datetime functions for DQL
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 21.8k
- Forks
- 1.6k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 9
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked discussions and the math-on-value-variables documentation, then compare the proposed YEAR(created_date) and datetime(created_date, year) queries with PostgreSQL's date extraction examples. Define the supported date parts, query syntax, and aggregation behavior before locating the relevant DQL implementation and tests. Done means the agreed functions work for the stated aggregation cases and are covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100