Home > other >  Using data build tool(dbt) and snowflake, how can I check if a column is a date field?
Using data build tool(dbt) and snowflake, how can I check if a column is a date field?

Time:01-08

I'm a Junior so apologies if my explanation isn't that great.

I've created a macro on dbt to add a default row with defined values or default values based on data type.

What I'm trying to achieve is to check if the column is a datatype date field, then it will return the default variable {{ date_vi }} which I've defined as '1900-00-00', but I'm getting an error:

dbt.adapters.snowflake.column.SnowflakeColumn object' has no attribute 'isdate which tells me there is no is_date() which is confusing because is_date() works on snowflake normally.

I have now noticed on the dbt docs:

https://docs.getdbt.com/reference/dbt-classes#column

and the source code on github for snowflake:

https://github.com/dbt-labs/dbt-snowflake/blob/main/dbt/adapters/snowflake/column.py

That is_date() isn't actually available with the snowflake adapter, the code I was trying to get working was: {% elif col.is_date() %}{{ date_vl }} so I'm wondering what would be the be best way to check if a column is a date datatype? Hopefully I explained it enough as I'm still fairly new.

Cheers.

CodePudding user response:

You can use https://github.com/calogica/dbt-expectations#expect_column_values_to_be_of_type

Or look at their implementation at https://github.com/calogica/dbt-expectations/blob/main/macros/schema_tests/column_values_basic/expect_column_values_to_be_in_type_list.sql for ideas of your custom implementation.

  •  Tags:  
  • Related