-
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
Implement dateadd macro DNA-15944 [DNA-20679] #80
Conversation
64d0b5b
to
3de08a2
Compare
README.md
Outdated
@@ -79,6 +80,12 @@ In case you want to create the index on a source table, refer to the table using | |||
) }} | |||
``` | |||
|
|||
#### dateadd ([source](macros/multiple_databases/dateadd.sql)) | |||
This macro adds a specified number value (as a signed integer) to a specified datepart of an input date value, and then returns that modified value. The datepart can be any of the following values for SQL Server and Snowflake: year, quarter, month, week, day, hour, minute, second, millisecond. For Databricks, the datepart can be any of the following values: year, day, hour, minute, second. |
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.
We are expecting a date or a datetime as input for this function, right? Can this be mentioned here explicitly. Now it states "input date value" and in the example uses "expression" (which can be a string)
@@ -0,0 +1,22 @@ | |||
{%- macro dateadd(datepart, number, datetime_field) -%} | |||
{%- if target.type == 'snowflake' -%} | |||
dateadd({{ datepart }}, {{ number }}, try_to_timestamp(to_varchar({{ datetime_field }}))) |
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.
If we know the datetime_field is a date or a datetime, we can use to_timestamp({{datetime_field}})
{%- elif target.type == 'sqlserver' -%} | ||
dateadd({{ datepart }}, {{ number }}, try_convert(datetime2, {{ datetime_field }})) | ||
{%- elif target.type == 'databricks' -%} | ||
{%- set clock_component -%} |
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.
can we rename this value to time_value? It represents the time only part of the datetime
dateadd({{ datepart }}, {{ number }}, try_convert(datetime2, {{ datetime_field }})) | ||
{%- elif target.type == 'databricks' -%} | ||
{%- set clock_component -%} | ||
to_unix_timestamp({{ datetime_field }}) - to_unix_timestamp(date_trunc('DD', {{ datetime_field }})) |
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.
If we convert the input value using to_unix_timestamp, we will lose the millisecond precision. This makes it different from the Snowflake and SQL Server implementations
4c5b27d
to
86f7c1d
Compare
README.md
Outdated
@@ -86,6 +87,12 @@ In case you want to create the index on a source table, refer to the table using | |||
) }} | |||
``` | |||
|
|||
#### dateadd ([source](macros/multiple_databases/dateadd.sql)) | |||
This macro adds a specified number value (as a signed integer) to a specified datepart of an input date or datetime, and then returns that modified value. The datepart can be any of the following values for SQL Server and Snowflake: year, quarter, month, week, day, hour, minute, second, millisecond. For Databricks, the datepart can be any of the following values: year, quarter, month, week, day, hour, minute, second, millisecond. |
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.
- Adds a number to a datepart --> Adds a specified number value of the specified datepart ...?
- The list of options supported is the same for Snowflake, SQL Server and Databricks, so we only need to list them once.
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.
Actually there are more for SQL Server, but I will remove the second list.
86f7c1d
to
e6611d2
Compare
30d2c4f
to
d23b18c
Compare
Description
Release
main
)dev
(or other) branchDid you consider?
What is the performance impact?The version number indbt_project.yml