Skip to content

Commit

Permalink
Merge pull request #3454 from fishtown-analytics/adapter_dispatch_inf…
Browse files Browse the repository at this point in the history
…inite_recursion

Fix macro depends_on recursion error when macros call themselves (dbt…utils.datediff)
  • Loading branch information
gshank authored Jun 11, 2021
2 parents 16501ec + af0fe12 commit 14507a2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Contributors:
- Raise better error if snapshot is missing required configurations ([#3381](https://github.com/fishtown-analytics/dbt/issues/3381), [#3385](https://github.com/fishtown-analytics/dbt/pull/3385))
- Fix `dbt run` errors caused from receiving non-JSON responses from Snowflake with Oauth ([#3350](https://github.com/fishtown-analytics/dbt/issues/3350))
- Fix deserialization of Manifest lock attribute ([#3435](https://github.com/fishtown-analytics/dbt/issues/3435), [#3445](https://github.com/fishtown-analytics/dbt/pull/3445))
- Fix `dbt run` errors caused from receiving non-JSON responses from Snowflake with Oauth ([#3350](https://github.com/fishtown-analytics/dbt/issues/3350)
- Fix infinite recursion when parsing schema tests due to loops in macro calls ([#3444](https://github.com/fishtown-analytics/dbt/issues/3344), [#3454](https://github.com/fishtown-analytics/dbt/pull/3454))

### Docs
- Reversed the rendering direction of relationship tests so that the test renders in the model it is defined in ([docs#181](https://github.com/fishtown-analytics/dbt-docs/issues/181), [docs#183](https://github.com/fishtown-analytics/dbt-docs/pull/183))
Expand Down
2 changes: 2 additions & 0 deletions core/dbt/context/macro_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ def __init__(

def recursively_get_depends_on_macros(self, depends_on_macros, dep_macros):
for macro_unique_id in depends_on_macros:
if macro_unique_id in dep_macros:
continue
dep_macros.append(macro_unique_id)
if macro_unique_id in self.macro_resolver.macros:
macro = self.macro_resolver.macros[macro_unique_id]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% macro datediff(first_date, second_date, datepart) %}
{{ return(adapter.dispatch('datediff', 'local_utils')(first_date, second_date, datepart)) }}
{% endmacro %}


{% macro default__datediff(first_date, second_date, datepart) %}

datediff(
{{ datepart }},
{{ first_date }},
{{ second_date }}
)

{% endmacro %}


{% macro postgres__datediff(first_date, second_date, datepart) %}

{% if datepart == 'year' %}
(date_part('year', ({{second_date}})::date) - date_part('year', ({{first_date}})::date))
{% elif datepart == 'quarter' %}
({{ local_utils.datediff(first_date, second_date, 'year') }} * 4 + date_part('quarter', ({{second_date}})::date) - date_part('quarter', ({{first_date}})::date))
{% else %}
( 1000 )
{% endif %}

{% endmacro %}

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro test_my_datediff(model) %}
select {{ local_utils.datediff() }}
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ models:
tests:
- call_pkg_macro
- local_utils.pkg_and_dispatch
- my_datediff
12 changes: 7 additions & 5 deletions test/integration/008_schema_tests_test/test_schema_v2_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,17 +420,19 @@ def test_postgres_test_context_tests(self):
run_result = self.run_dbt(['test'], expect_pass=False)
results = run_result.results
results = sorted(results, key=lambda r: r.node.name)
self.assertEqual(len(results), 4)
self.assertEqual(len(results), 5)
# call_pkg_macro_model_c_
self.assertEqual(results[0].status, TestStatus.Fail)
# pkg_and_dispatch_model_c_
self.assertEqual(results[1].status, TestStatus.Fail)
# my_datediff
self.assertRegex(results[2].node.compiled_sql, r'1000')
# type_one_model_a_
self.assertEqual(results[2].status, TestStatus.Fail)
self.assertRegex(results[2].node.compiled_sql, r'union all')
# type_two_model_a_
self.assertEqual(results[3].status, TestStatus.Fail)
self.assertEqual(results[3].node.config.severity, 'WARN')
self.assertRegex(results[3].node.compiled_sql, r'union all')
# type_two_model_a_
self.assertEqual(results[4].status, TestStatus.Fail)
self.assertEqual(results[4].node.config.severity, 'WARN')

class TestSchemaTestContextWithMacroNamespace(DBTIntegrationTest):
@property
Expand Down

0 comments on commit 14507a2

Please sign in to comment.