Fix list-type metric filters under models key rendered at parse time#12634
Open
theyostalservice wants to merge 1 commit intomainfrom
Open
Fix list-type metric filters under models key rendered at parse time#12634theyostalservice wants to merge 1 commit intomainfrom
theyostalservice wants to merge 1 commit intomainfrom
Conversation
When v2 metrics defined under the `models:` key use list-type filters
(e.g. `filter: ["{{ Dimension(...) }}", "{{ Dimension(...) }}"]`), the
`deep_map_render` function appends integer indices to the keypath for
list elements. The previous check `keypath[-1] == "filter"` only matched
string filters (where the last element is literally "filter"), but missed
list elements where `keypath[-1]` is an integer like 0 or 1.
Changed to `"filter" in keypath` which correctly matches both forms.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #12634 +/- ##
==========================================
- Coverage 91.40% 91.36% -0.05%
==========================================
Files 203 203
Lines 25677 25677
==========================================
- Hits 23470 23459 -11
- Misses 2207 2218 +11
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When v2 metrics defined under the
models:key use list-type filters, the YAML schema renderer incorrectly attempts to render them at parse time, causing'Dimension' is undefinederrors.The bug
SchemaYamlRenderer.should_render_keypath()usesdeep_map_renderfromdbt_common, which builds a keypath tuple as it traverses nested structures. For dict keys, it appends strings (e.g."filter"). For list elements, it appends integer indices (e.g.0,1).The
modelskey handler checkedkeypath[-1] == "filter", which works for string filters where the keypath looks like('metrics', 0, 'filter'). But for list filters like:...the keypath for each element is
('metrics', 0, 'filter', 0)— andkeypath[-1]is0(int), not"filter". So the check fails, the renderer tries to evaluate the Jinja, andDimensionis not in the parse-time context.The fix
One-line change in
core/dbt/parser/schema_renderer.py:This matches the approach already used by the
metricskey handler (line 107), which checkskeypath[-1] == "filter" or keypath[-2] == "filter"to handle both forms.New tests
General coverage tests (these all passed before the fix — they fill gaps in test coverage for list-type filters and nested filter locations that previously had no unit tests):
test__metrics_list_filter— list-type top-level filters for standalonemetricskeytest__metrics_nested_filter—type_params.numerator.filteras both string and list for standalonemetricskeytest__models_v2_metric_filter_string— string filter undermodelskey (regression test)test__models_v2_nested_metric_filters—numerator.filter,input_metrics[].filter,base_metric.filteras string and list undermodelskeyBug-specific regression test (this test fails without the fix with
'Dimension' is undefined):test__models_v2_metric_filter_list— list-type filter on v2 metrics under themodelskeyTest plan
python -m pytest tests/unit/parser/test_schema_renderer.py -vtest__models_v2_metric_filter_listfails without the fix (verified by reverting the one-line change)test__models_v2_nested_metric_filtersalso fails without the fix for list-form nested filters🤖 Generated with Claude Code