-
Notifications
You must be signed in to change notification settings - Fork 36
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
[CT-3208] [Feature] Cross-database date
macro
#192
Comments
to_date
macroto_date
macro
FWIW, I had a lot of problems with dates and timestamps(and the lack of
Examples passed in:
dbt_cloud == redshift. local == SQL Server. Edit to add:
|
After discussion with @graciegoheen and @Jstein77, we'd like to enable usage like this (with 3 parameters): {{ dbt.to_date(2023, 10, 4) }} Putting it all together: select cast(date_day as date) as date_day
from ({{ dbt.date_spine("day", dbt.to_date(2023, 5, 11), dbt.to_date(2023, 9, 17)) }}) We might be able to just re-use the definition of the |
After thinking about it a little more, I think we should choose between the names If three arguments, each integers:
If two arguments, both strings:
The former would line up with these: The latter would line up with these:
That way, we can avoid confusion with any of these pre-existing function names. |
@dbeatty10 need a 🍐 on this or are you good flying solo? |
Opened a new issue in dbt-labs/docs.getdbt.com: dbt-labs/docs.getdbt.com#5427 |
to_date
macrodate
macro
Is this your first time submitting a feature request?
Describe the feature
To support a built-in
metricflow_time_spine
in dbt-labs/dbt-core#8825, we want to create ato_date
macro that converts an ISO 8601-formatted date string into a SQLDATE
type.Rationale for the name
to_date
Even though
to_date
isn't within the SQL standard, databricks, postgres, redshift, and snowflake all have ato_date
function that does what we want. Although bigquery is an outlier, nothing we can't solve with a little dispatch magic ✨Prototype of
to_date()
Assuming
to_date()
is a cross-database macro that takes an ISO 8601 (YYYY-MM-DD
) date string as input, here's a completely untested prototype for dbt-postgres:Pulling it all together for
metricflow_time_spine
The cross-database Jinja template might look like this:
Appendix
Validation and error checking
If we want, we could always add some format validation to the default implementation of
to_date()
by using thedatetime
module:dbt-bigquery
The implementation for Bigquery may or may not be simple 🤷 If all else fails, something like this might work (completely untested!):
type_date
macroWe may (or may not) want to also create a cross-database
type_date
macro (which doesn't exist today). I haven't seen any database that doesn't call this data typeDATE
, so that makes it either easy-peasy or extraneous depending how you look at it.Describe alternatives you've considered
I can't think of a good alternative to creating a cross-database macro that converts a SQL
string
into a SQLdate
.Who will this benefit?
This supports a built-in
metricflow_time_spine
in dbt-labs/dbt-core#8825, we want ato_date
macro that converts an ISO 8601-formatted date string into a SQLDATE
type.Are you interested in contributing this feature?
No response
Anything else?
For refinement
Theoretically, we could choose to add an optional 2nd parameter that specifies the format to use. e.g. something like
mm/dd/yyyy
.There are two options for the supported formats for that parameter:
datetime
format codesIf we add this 2nd parameter, I would advocate for using the Python format codes since they would give us solid ground to stand on. Then from there, we could convert the resulting
datetime
using the format specifications that are specific to each adapter as-needed.I suspect that we could be in a world of hurt if we choose to use SQL-native format specifications instead, because they may differ on a per-adapter basis. I don't know this for sure, just a suspicion.
The text was updated successfully, but these errors were encountered: