{{ config(materialized="materialized_view") }}
{#
This will re-create the UDF when this model is run specifically.
This is quite convenient due to the fact that if you drop an UDF and
it is currently used in a MW -> then the MW will use the old version of the UDF.
Having it like this, coupled to the model, we can at least ensure that it gets updated
whenever the model also updates.
#}
{% call set_sql_header(config) %}
DROP FUNCTION IF EXISTS gcd(a int, b int));
CREATE FUNCTION gcd(a int, b int) RETURNS int LANGUAGE javascript AS $$
if(a == null || b == null) {
return null;
}
while (b != 0) {
let t = b;
b = a % b;
a = t;
}
return a;
$$;
{% endcall %}
select *, gcd(column_a, column_b) as gcd
from {{ ref("upstream_model") }}
Support usage of e.g. https://docs.getdbt.com/reference/resource-configs/sql_header. Useful for declaring UDFs that are tightly connected to the MW, for example: