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

Update documentation for the datedifff macro: add another set of quotes #163

Closed
louisguitton opened this issue Sep 25, 2019 · 3 comments · Fixed by #164
Closed

Update documentation for the datedifff macro: add another set of quotes #163

louisguitton opened this issue Sep 25, 2019 · 3 comments · Fixed by #164

Comments

@louisguitton
Copy link
Contributor

Hey everyone,
it is my understanding from using dbt_utils.dateadd this morning on a Redshift cluster and failing that the macro is not supported for Redshfit.

Error

Completed with 1 errors:

Database Error in model labelling_set (models/marts/ml-entities/labelling_set.sql)
  function date_add("unknown", integer, integer) does not exist
  HINT:  No function matches the given name and argument types. You may need to add explicit type casts.
  compiled SQL at target/compiled/of_models/marts/ml-entities/labelling_set.sql

Code https://github.com/fishtown-analytics/dbt-utils/blob/master/macros/cross_db_utils/dateadd.sql

I'm willing to open a PR for this and dig deeper into the logic of adapter_macro etc. if that sounds like something that could be useful (for my own usecase I just wrote manually the date_add redshift statement).

@jtcohen6
Copy link
Contributor

Hi @louisguitton, the macro is supported on Redshift via the default implementation. Often, when Redshift returns an error like

function date_add("unknown", integer, integer) does not exist

This indicates an issue with the datatypes of the arguments passed. Specifically, the third argument is from_date_or_timestamp, and it should be of type date or timestamp, not integer.

@louisguitton
Copy link
Contributor Author

louisguitton commented Sep 25, 2019

You're right about the datatype of the argument. I realised that shortly after writing the issue.

But I still can't make the macro work unfortunately.

Working dbt code without the macro

Select
...
where p.published_at between dateadd(day, -5, '2019-09-24') and '2019-09-24'

With the macro - not working

Select
...
where p.published_at between {{ dbt_utils.dateadd('day', -5, '2019-09-24') }} and '2019-09-24'

When I look at the compiled SQL for that latter version, I get

Select
...
where p.published_at between
    dateadd(
        day,
        -5,
        2019-09-24   -- instead of '2019-09-24'
        )
 and '2019-09-24'

which explains the integer datatype.

@louisguitton louisguitton changed the title Add dateadd support for Redshift Potential casting issue for from_date_or_timestamp in dbt_utils.dateadd for Redshift Sep 25, 2019
@drewbanin
Copy link
Contributor

this macro doesn't automatically add quotes to the from_date_or_timestamp argument. This allows you to specify a column name (which you would not want to quote) or a date literal, like 2019-09-24. You can use a second set of quotes to make sure that that your from_date_or_timestamp argument is quoted appropriately, eg:

-- note: there are single-quotes inside of the double-quotes here
{{ dbt_utils.dateadd('day', -5, "'2019-09-24'") }}

There's another macro in this package, literal, that will add this second set of quotes for you. That would look like:

{{ dbt_utils.dateadd('day', -5, dbt_utils.literal("2019-09-24")) }}

Both of these are equivalent, so sort of up to your preference which one you use!

Let's update the documentation here to correctly show usage information for the dateadd macro. You can see a correct example which uses two sets of quotes in the datediff macro right below it.

@drewbanin drewbanin changed the title Potential casting issue for from_date_or_timestamp in dbt_utils.dateadd for Redshift Update documentation for the datedifff macro: add another set of quotes Sep 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants