Add get_columns_by_pattern macro #1064
Open
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.
resolves #1037
Problem
Users working with datasets that have dynamically named columns (like year-based columns:
2024_revenue,2023_revenue, or similarly named fields:current_item,last_item) need a way to programmatically select columns matching a pattern. While this can be done with a for loop in Jinja, there's no idiomatic dbt-utils macro for this common use case.Solution
This PR adds a new
get_columns_by_patternmacro that returns columns from a relation matching a SQL LIKE pattern.Features:
%for wildcard,_for single character)excludeparameter to filter out unwanted columnsImplementation follows existing dbt-utils patterns:
adapter.dispatch()for cross-database compatibility_is_relationand_is_ephemeralif not executeget_filtered_columns_in_relationExample usage:
Checklist