dbt-utils icon indicating copy to clipboard operation
dbt-utils copied to clipboard

Add a new macro - get_columns_by_pattern

Open BryanRacic opened this issue 4 months ago • 0 comments

Describe the feature

get_columns_by_pattern(table, pattern) A macro that only returns columns matching a given search pattern.

Example For a table containing the following columns:

user_id, current_item, last_item, state

A macro with the search expression: %_item

dbt_utils.get_columns_by_pattern(table=ref('table_name'), pattern='%_item')

returns just the column names ending with _item (current_item,last_item)

Describe alternatives you've considered

It's pretty easy to write this with just a for loop in Jinja, but I'm unaware of any other idiomatic approach. I've searched around quite frequently because it seems to me like something that should already exist.

Additional context

Implementation could likely mirror the existing get_relations_by_pattern/get_tables_by_pattern code

Who will this benefit?

(Mostly me) but also any other developer who's been unfortunate enough to ingest a data source with fields formatted in the style of 2024_season, 2023_season, 2022_season etc.

It could be just me, but as a frequent user of the star and get_relations_by_pattern macros, I feel like I'm committing a small crime by writing the required 3 lines of Jinja

Are you interested in contributing this feature?

I already have a simpler version of this macro that I use for my own work, I'd be more than happy to contribute

BryanRacic avatar Aug 20 '25 02:08 BryanRacic