Replies: 2 comments 4 replies
-
@Bilbottom based on your implementation is it possible to make this a package so we don't have to wait for the kind @jtcohen6 and @dbeatty10 to consider this? Happy to contribute if so! cc: @kentkr |
Beta Was this translation helpful? Give feedback.
0 replies
-
This seems pretty sought after but discussions like this pr make me think it won't be a part of the core functionality. A package/adapter seems like the answer, if there is one. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Describe the feature
I love that dbt allows us to template SQL with Jinja -- it adds so much flexibility and enriches the development experience with dbt!
...until we start having very Jinja-heavy files (I'm mainly thinking of macros). While Jinja is awesome for templating and definitely has a place in the dbt world, it's not as feature-rich, as user-friendly, or as readable as Python when there's some moderately complicated logic.
Including support for some of the Python standard library modules was an awesome addition: this discussion is about extending this support to any Python module that the user needs*, including local Python modules which could then replace clunky custom Jinja code.
I'll go into some detail below about an MWE implementation that has worked for me in a small personal project (but I've not tested it at scale so it's not a solution that's ready to go).
Note that this has only been tested with
.sql
files since it exposes the modules only to the Jinja context. (I may follow up with a comment about how this plays with Python models.)Implementation notes
I've raised a PR on my fork of dbt-core which is an MWE implementation with some implementation notes, available at:
To summarise the implementation, it does 2 things:
modules.yml
, which lists the Python modules/packages to expose to the Jinja context.For example, suppose the content of my
modules.yml
file was:Then I could reference these
math
constants in a Jinja context, such as:(This is a bit of a superficial example since most databases expose a
PI()
function which should be used instead.)The real power of this feature is being able to use custom Python code -- I could define some helper functions in a Python module in the same repo as my dbt project, and call them in the Jinja context:
When custom Python can be exposed to the Jinja context, the sky's the limit -- this unlocks even more potential for dbt!
Why does this help?
I'll call out 3 main reasons:
As I mentioned above, this does introduce the potential for people abusing this -- but I think the pros outweigh the cons, and it would certainly encourage more people that I know to adopt dbt since they're far more comfortable writing Python than they are writing Jinja.
Describe alternatives you've considered
The only other thing that I could find to support this is fal, but this becomes another tool to add to the stack rather than exposing the functionality in dbt-core.
My company also has a POC to append modules to the existing modules dictionary (
{{ modules.*** }}
) at runtime, which is working for us (but I can't share details of this, and is built specifically for our setup).The approach described above extends dbt-core itself, so would require very little work from the developer to start using custom Python packages in their dbt project.
EDIT: As per the comments, the following PR also requested this feature:
As per the top comment (not the bot), this wouldn't be accepted into dbt-core so this discussion can probably be closed. However, I'll leave it open (for now) since I like the idea of exposing this through a new adapter instead.
Who will this benefit?
This is another one where the answer is, hopefully, "everyone"!
A lot of the resistance I've seen to dbt at scale is people complaining about clunky Jinja, so this unlocks the freedom to keep Jinja to what it's good at (templating) while still being able to inject custom Python at runtime.
Next steps
Regardless of how this discussion goes, I'll keep working on this in my fork since I have loads of plans to enrich my dbt projects with extra Python packages -- so I'm very happy to play a significant part in contributing this functionality to dbt-core if there's interest 😄
Beta Was this translation helpful? Give feedback.
All reactions