Skip to content

Add a new macro - get_columns_by_pattern #1037

@BryanRacic

Description

@BryanRacic

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions