Skip to content
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

Merged
merged 1 commit into from
Nov 17, 2023
Merged

Conversation

silviustanimir
Copy link
Contributor

Description

  • Implement charindex macro for Spark compatbility.

Release

  • Direct release (main)
  • Merge to dev (or other) branch
    • Why:

Did you consider?

  • Does it Work on Automation Suite / SQL Server
  • Does it Work on Automation Cloud / Snowflake
  • What is the performance impact?
  • The version number in dbt_project.yml

@silviustanimir silviustanimir self-assigned this Nov 14, 2023
@silviustanimir silviustanimir force-pushed the feat/dateadd branch 8 times, most recently from 64d0b5b to 3de08a2 Compare November 14, 2023 17:46
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.
Copy link
Contributor

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 }})))
Copy link
Contributor

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 -%}
Copy link
Contributor

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 }}))
Copy link
Contributor

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

@silviustanimir silviustanimir force-pushed the feat/dateadd branch 6 times, most recently from 4c5b27d to 86f7c1d Compare November 16, 2023 13:14
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.
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@silviustanimir silviustanimir merged commit 3221bc2 into main Nov 17, 2023
3 checks passed
@silviustanimir silviustanimir deleted the feat/dateadd branch November 17, 2023 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants