diff --git a/.changes/unreleased/Fixes-20230615-142949.yaml b/.changes/unreleased/Fixes-20230615-142949.yaml new file mode 100644 index 00000000000..2693c81ab7f --- /dev/null +++ b/.changes/unreleased/Fixes-20230615-142949.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Skip jinja parsing of metric filters +time: 2023-06-15T14:29:49.900201-07:00 +custom: + Author: QMalcolm + Issue: "7864" diff --git a/core/dbt/parser/schema_renderer.py b/core/dbt/parser/schema_renderer.py index 3f3badb7723..e0c54f247da 100644 --- a/core/dbt/parser/schema_renderer.py +++ b/core/dbt/parser/schema_renderer.py @@ -67,8 +67,8 @@ def should_render_keypath(self, keypath: Keypath) -> bool: elif self._is_norender_key(keypath[0:]): return False elif self.key == "metrics": - # back compat: "expression" is new name, "sql" is old name - if keypath[0] in ("expression", "sql"): + # This ensures all key paths that end in 'filter' for a metric are skipped + if keypath[-1] == "filter": return False elif self._is_norender_key(keypath[0:]): return False diff --git a/tests/functional/duplicates/test_duplicate_metric.py b/tests/functional/duplicates/test_duplicate_metric.py index 3237f533658..3a9c3205796 100644 --- a/tests/functional/duplicates/test_duplicate_metric.py +++ b/tests/functional/duplicates/test_duplicate_metric.py @@ -25,7 +25,7 @@ type_params: measure: name: "years_tenure" - filter: "loves_dbt is true" + filter: "{{dimension('loves_dbt')}} is true" """ diff --git a/tests/functional/metrics/fixtures.py b/tests/functional/metrics/fixtures.py index 84b348a9ecd..3191e8441bd 100644 --- a/tests/functional/metrics/fixtures.py +++ b/tests/functional/metrics/fixtures.py @@ -43,7 +43,7 @@ type_params: measure: name: "years_tenure" - filter: "loves_dbt is true" + filter: "{{dimension('loves_dbt')}} is true" - name: average_tenure label: "Average tenure" @@ -86,7 +86,7 @@ type_params: measure: name: years_tenure - filter: "loves_dbt is true" + filter: "{{dimension('loves_dbt')}} is true" - name: collective_window label: "Collective window" @@ -95,7 +95,7 @@ type_params: measure: name: years_tenure - filter: "loves_dbt is true" + filter: "{{dimension('loves_dbt')}} is true" window: 14 days """ @@ -377,7 +377,7 @@ type_params: measure: name: years_tenure - filter: "loves_dbt is true" + filter: "{{dimension('loves_dbt')}} is true" """ @@ -404,7 +404,7 @@ type_params: measure: name: years_tenure - filter: "loves_dbt is true" + filter: "{{dimension('loves_dbt')}} is true" """ diff --git a/tests/functional/partial_parsing/fixtures.py b/tests/functional/partial_parsing/fixtures.py index 5af604f2355..164dcb75a07 100644 --- a/tests/functional/partial_parsing/fixtures.py +++ b/tests/functional/partial_parsing/fixtures.py @@ -353,7 +353,7 @@ type_params: measure: name: customers - filter: "loves_dbt is true" + filter: "{{dimension('loves_dbt')}} is true" +meta: is_okr: True tags: @@ -441,7 +441,7 @@ type_params: measure: name: years_tenure - filter: "loves_dbt is true" + filter: "{{dimension('loves_dbt')}} is true" """ @@ -588,7 +588,7 @@ type_params: measure: name: years_tenure - filter: "loves_dbt is true" + filter: "{{dimension('loves_dbt')}} is true" """ @@ -977,7 +977,7 @@ type_params: measure: name: years_tenure - filter: "loves_dbt is true" + filter: "{{dimension('loves_dbt')}} is true" """ diff --git a/tests/unit/test_yaml_renderer.py b/tests/unit/test_yaml_renderer.py index c65a6e41333..783cc69905e 100644 --- a/tests/unit/test_yaml_renderer.py +++ b/tests/unit/test_yaml_renderer.py @@ -99,22 +99,20 @@ def test__macros(self): self.assertEqual(dct, expected) def test__metrics(self): - context = {"my_time_grains": "[day]"} + context = {"metric_name_end": "_metric"} renderer = SchemaYamlRenderer(context, "metrics") dct = { - "name": "my_source", + "name": "test{{ metric_name_end }}", "description": "{{ docs('my_doc') }}", - "expression": "select {{ var('my_var') }} from my_table", - "time_grains": "{{ my_time_grains }}", + "filter": "{{ dimension('my_dim') }} = false", } # We expect the expression and description will not be rendered, but # other fields will be expected = { - "name": "my_source", + "name": "test_metric", "description": "{{ docs('my_doc') }}", - "expression": "select {{ var('my_var') }} from my_table", - "time_grains": "[day]", + "filter": "{{ dimension('my_dim') }} = false", } dct = renderer.render_data(dct) self.assertEqual(dct, expected)