I have a udf in DBT in python in a macro as ‘is_valid’ and few others as ‘get_latest_value’ and ‘get_best_value’. I would essentially be using the logic for ‘is_valid’ in all other macros, is there a way to reuse the same code without essentially copy pasting?
My current code is-
{% macro is_valid_udf(str) %}
CREATE OR REPLACE TEMPORARY FUNCTION is_valid(str STRING)
RETURNS BOOLEAN
LANGUAGE python
AS $$
<do something>
return True/False
$$
{% endmacro %}
then another udf as –
{% macro get_longest_str_udf() %}
CREATE OR REPLACE TEMPORARY FUNCTION get_longest_str(arr Array<string>)
RETURNS STRING
LANGUAGE python
AS $$
longest_str=None
for str in arr:
if str and {{ is_valid_udf(str) }}:
if longest_str is None or len(str) > len(longest_str):
longest_str = url
return longest_str
$$
{% endmacro %}
This essentially just replaces the entire macro in this and causes error, any idea on how to do this?
New contributor
Somya Tuteja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.