Open gekas93 opened 1 month ago
Thanks for reaching out @gekas93 !
This looks like https://github.com/dbt-labs/dbt-redshift/issues/659 where Redshift treats null
values as VARCHAR/TEXT even when you say something like cast(null as date)
or null::date
.
https://github.com/dbt-labs/dbt-redshift/issues/659#issuecomment-1924345236 discusses our suggested workaround which is to add something like this to the beginning of your model to handle this Redshift-specific behavior:
select
'9999-12-31'::date as plant_start_date,
'9999-12-31'::date as plant_end_date,
# ... the rest of your columns here
where 1=0
union all
Could you try that out and see if it resolves the issue?
Thanks for your quick response @dbeatty10 !
Sorry, because I tried to find some topics related with mine but I didn't found anything.
Yes, with your approach it's working but it's a problem when you have a big number of columns. In my case, in this table I have like 50 columns, so I had to add more than 50 code lines and 50 manual casts.
There will be some approach in the future that will solve this?
Obviously, if I add the IFNULL operation in a previously model and it's materialized as a table, I don't have this issue. But if it's materialized as a view (in my case has to be like that), it is still there.
Is this a new bug in dbt-core?
Current Behavior
I'm having a strange issue with the contracts in my dbt model and working with redshift.
I have fixed some fields with a data_type = 'date' and they were working well but I have added a NULLIF condition like this:
Before of adding the NULLIF function, everything works as expected.
I have tried to cast in each place (before, after, in the middle more than one time) but I always have the same issue. I tried to use a case instead of NULLIF but the same happens.
I have token a look in the macros and I have see that it's this macro the responsible of the behaviour:
I have added two logs at the end to confirm it. The sql_file_provided_columns is giving me the dates as a varchar.
Expected Behavior
DBT detects that the definition type is DATE for plant_start_date and plant_end_date
Steps To Reproduce
cast(NULLIF(<date_field>, '9999-12-31') as date) as <date_field_name>
Relevant log output
Environment
Which database adapter are you using with dbt?
redshift
Additional Context
No response