-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Folllow up: Support for python "re" module for doing regex in jinja templates #2851
Folllow up: Support for python "re" module for doing regex in jinja templates #2851
Conversation
First time contributing a PR! Assuming this is accepted, if I push a commit for the |
Thanks for this PR, @hochoy, and the detailed description! I'm totally in favor of pulling in regex functionality to the Jinja layer. It's also a great help during dbt development, even if we end up rewriting the trickiest bits in python.
Any commit you add to this open PR will automatically re-trigger the Circle CI tests. @drewbanin Do you have any concerns about pulling in
I think that's most of what we'd want, anyway, but I also don't have specific reasons to oppose the rest. |
Thank you for the feedback! I hope this will speed up the (already fast) workflow for all dbt devs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution @hochoy!
Do I have to install this as a package? If so, can you tell me how? |
Maybe there's a doc out there somewhere, but I'm also confused as to how to use the regex functions inside of dbt-jinja. I wrote a SO question that I'll answer myself once I find out. |
No need to install additional packages. You can access methods from https://docs.getdbt.com/reference/dbt-jinja-functions/modules#re |
dbt comes with |
beautiful thanks! for reference I was trying to filter an Agate table as given in the Agate cookbook this:
|
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://github.com/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.