-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
feat(python,rust,cli): add DATE
function for SQL
#11541
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like you also need to update the features in polars-sql
@@ -173,6 +173,10 @@ pub(crate) enum PolarsSqlFunctions { | |||
/// ``` | |||
Round, | |||
|
|||
// Date Functions | |||
// ---- | |||
Date, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe postgres (which we are trying to mimic where possible) calls this to_date
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see - thanks.
So should Date
and date
be replaced with ToDate
and to_date
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe postgres (which we are trying to mimic where possible) calls this to_date
Ahhh, TO_DATE
is actually a separate function that requires a second argument (a format string which is not strftime, but rather one of the "YYYY-MM-DD" type ones; we should probably skip this for now until we can map from that flavour of format string to strftime 🤔).
In this case DATE
is appropriate as it valid and only needs the string - can be called like so:
SELECT DATE '2020-12-31' as d1, DATE('2222-02-22') as d2
(I just quickly checked this on a local PostgreSQL connection ;))
@universalmind303 Ah, thanks - so Do I just need to add this to dtype-date = ["polars-lazy/dtype-date"] |
Adding @cmdlineluser: once you finish-up |
@alexander-beedie Ah. They didn't mention the use of Does that mean it's just dispatching to pl.select(pl.lit('2023-01-01').cast(pl.Date))
# ComputeError: strict conversion from `str` to `date` failed for column: literal, value(s) ["2023-01-01"]; if you were trying to cast Utf8 to temporal dtypes, consider using `strptime` Although in the sql context there is no error, it just returns null. pl.select(pl.sql_expr("CAST('2023-01-01' as DATE)"))
# shape: (1, 1)
# ┌─────────┐
# │ literal │
# │ --- │
# │ date │
# ╞═════════╡
# │ null │
# └─────────┘ |
Or, perhaps slightly better, redirect to |
exactly, that can already work today and auto-detects the standard kinds of date formats which you'd probably expect here (like %Y-%m-%d) In case it helps with motivation to get this across the finish line, there's been some interest in this on linkedin https://www.linkedin.com/posts/daniel-beach-6ab8b4132_dataengineering-polars-activity-7117879064404664321-BlB3?utm_source=share&utm_medium=member_desktop |
@cmdlineluser any news on this one? Would be great to merge this. |
I just added a commit to add the |
Oops - Apologies everyone, missed the notifications on this one. Thanks for update @MarcoGorelli Perhaps I was unclear in my wording previously as it was already dispatching to I don't think anything else is missing? I was wondering if |
In that case, how about we merge this one now, and you can add DATETIME in a new PR? |
Can you do a rebase @cmdlineluser? Then we can merge this one. |
995d37b
to
e08f828
Compare
Apologies @ritchie46 - first time learning about Hopefully I haven't messed that up. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
almost correct...looks like you just duplicated test_sql_unary_ops_8890
in the rebase
Alright here goes. Thank you @cmdlineluser, happy to learn you more git tricks ;) |
@cmdlineluser interested in following up with the datetime funciton? |
I did take an initial look @ritchie46 but got a bit stuck. I did not realize that polars/py-polars/src/expr/string.rs Lines 26 to 52 in 89cc1e2
I'm not sure if/how these are supposed to be exposed via polars-sql. From what I can tell, POSTGRES supports "named args" with The current |
https://stackoverflow.com/questions/77227895/polars-sql-context-filter-by-date-or-datetime
This was a recent question on SO and the mentioned
DATE
function doesn't seem to exist yet.I was interested in seeing how
polars-sql
worked and thought I'd have a look.Should
DATETIME
also be added as part of this?