-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Expose dateutil.rrule functionality #1
Comments
That's really interesting. I've been thinking I'd like to expose the dateutil module`s rrule (recurrence rule) stuff as functions, so maybe that could serve a similar purpose here? https://dateutil.readthedocs.io/en/stable/rrule.html SQLite does have a mechanism for creating virtual tables but it's not exposed in the Python So I wonder... if I had a function that returned a JSON list of every date in a given year, would that address these kind of use-cases? |
Let's try that out, with a JSON array representing just a single month:
select * from json_each('["2020-10-01", "2020-10-02", "2020-10-03", "2020-10-04", "2020-10-05", "2020-10-06", "2020-10-07", "2020-10-08", "2020-10-09", "2020-10-10", "2020-10-11", "2020-10-12", "2020-10-13", "2020-10-14", "2020-10-15", "2020-10-16", "2020-10-17", "2020-10-18", "2020-10-19", "2020-10-20", "2020-10-21", "2020-10-22", "2020-10-23", "2020-10-24", "2020-10-25", "2020-10-26", "2020-10-27", "2020-10-28", "2020-10-29", "2020-10-30", "2020-10-31"]')
|
So it looks to me like custom SQL functions that return JSON arrays of date strings could be used in joins! |
I think the easiest way to do this would be to expose the I thought about exposing custom SQL functions for each of the different types of recur rules - The nice thing about select dateutil_rrulestr("FREQ=DAILY;INTERVAL=10;COUNT=5");
-- returns ["2020-09-28", "2020-10-08", "2020-10-18", "2020-10-28", "2020-11-07"] Would need to limit the number of records that can come back from this - maybe to 100 or 1000 - otherwise someone could enter a malicious string that exhausts all memory. |
I could add some additional SQL functions with nicer syntax for really common cases - like "give me every day between date X and date Y": select dateutil_dates_between('2020-01-01', '2020-03-01'); This would be inclusive (so you'd get everything from select dateutil_dates_between('2020-01-01', '2020-03-01', 0); This would return |
|
The documentation will have to show people how to use these with with dates as (
select
value
from
json_each('["2020-10-01", "2020-10-02", "2020-10-03"]')
)
select
*
from
dates; With the proposed with dates as (
select
value
from
json_each(dateutil_rrulestr("FREQ=DAILY;INTERVAL=10;COUNT=5"))
)
select
*
from
dates; |
Rather than cutting off at 1000 items I think the functions should raise an exception, because otherwise people may write queries that were truncated at 1000 without realizing it. |
A limit of 10,000 values returned in a single JSON list might work fine, and would allow people with even pretty extreme use-cases some head room. |
This still needs documentation. I also haven't implemented |
I'm not crazy about this: select dateutil_rrule('DTSTART:20200101
FREQ=DAILY;INTERVAL=10;COUNT=5') The Instead, how about an optional second argument which you can pass a datetime to - or the result of select dateutil_rrule('FREQ=DAILY;INTERVAL=10;COUNT=5', dateutil_parse('10 october 2020')) |
Actually I'll have that second argument always parsed by select dateutil_rrule('FREQ=DAILY;INTERVAL=10;COUNT=5', '10 october 2020') |
Added documentation on how to use this with |
This is a really great plugin and definitely simplifies preparing dates for SQlite!
I'm not sure if this would fit into this plugin (I realize that it is pretty aligned with the python library), but something that could potentially be really helpful would be an easy way to generate a date table. This would make it really easy to aggregated by different date parts to see a zoomed out picture, analyze seasonality, account for days where zero events happened, or calculate days between timestamps while excluding weekends or holidays.
Power BI, for instance, provides a function to generate all days that have been observed in the tables of the data model.
Emile posted details about how her team generated a date table when she was at GitLab. You may not be familiar with dbt so the syntax might look a bit strange to you (though also familiar since it's jinja2). I found where the table is currently documented to see what the code actually looks like (check "Compiled" under "Code"). It takes a while for that page to load for some reason.
That technique for generating a sequence is used because it works on pretty much all databases, but many databases have a function to do it. It looks like there's one for SQlite
The text was updated successfully, but these errors were encountered: