Folllow up: Support for python "re" module for doing regex in jinja templates #2851
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 #1755
This is a follow-up PR for the same exact feature posted by @whisperstream at #1755 . Descriptions below will almost exactly match. My first attempt at contributing to
dbt
😄 ! Please do not hesitate to point out any possible improvements.Description
Describe the feature
"I have a case where I'd like to be able to do a regex in a jinja template for matching a particular set of values. It would be handier to have a access to python's re module than having to use normal string manipulation and matching" ~ @whisperstream
Describe alternatives you've considered
Similar to the original issue, I have used the following to work around not having RegEx
Additional context
"This change is not database specific, it would apply to all jinja templates in the same way that
datetime
andpytz
modules are made available" ~ @whisperstreamAdditionally, I checked the list of exported functions from the native python
re
module and they do not introduce any unsafe behaviour with respect to database connections, exposed secrets, etc:https:/python/cpython/blob/83c86cf54b36a7325f615f5adf22b28e48f0e72d/Lib/re.py#L135-L141
If there is a concern that future versions of
re
will introduce unsafe behaviour, I can close this and create another PR with explicitre
imports instead of__all__
.Who will this benefit?
"Anybody who wants to do more complex string manipulation and string checking in a more straight-forward way than is possible today" ~ @whisperstream
For us, this comes up often when an input array of strings to a macro is "dirty" and the strings need to be cleaned/filtered via RegEx. Another case if we are looking for certain string occurrences that might decide the logical
IF, ELSE
flow of our macros.Checklist
CHANGELOG.md
and added information about my change to the "dbt next" section.