Add get_columns_by_pattern macro#1064
Open
FeatherAnalytics wants to merge 1 commit intodbt-labs:mainfrom
Open
Conversation
This macro returns columns from a relation that match a SQL LIKE pattern. It supports pattern matching with % and _ wildcards and an optional exclude parameter to filter out unwanted columns. Useful for working with dynamic column sets like year-based columns (2024_*, 2023_*) or similarly named fields (*_item, *_revenue, etc.). Implementation follows existing dbt-utils patterns: - Uses adapter.dispatch() for cross-database compatibility - Includes input validation with _is_relation and _is_ephemeral - Case-insensitive matching consistent with SQL standards - Returns simple list of column names for easy composition Includes full integration test suite with seed data and equality assertions. Resolves dbt-labs#1037 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Author
|
Hello, @b-per , I wanted to ask if there's anything I can do to get this PR moving one direction or the other? |
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.
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