dbt-labs / dbt-labs/dbt-utils

Add a new macro - get_columns_by_pattern

Open
#1,037 1 comment 4 reactions 0 assignees View on GitHub
enhancement Stale triage
Dominant language
Makefile
Stars
1.8k
Forks
632
PR merge metrics
No merged PRs in 30d

Description

### 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](https://github.com/dbt-labs/dbt-utils/blob/main/macros/sql/get_relations_by_pattern.sql) 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.