fix: pass string to process_template
#31329
Merged
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.
SUMMARY
When extracting tables from a Jinja template we're passing a
jinja2.nodes.Template
object to theprocess_template
method, but it expects a string. This can cause errors depending on theENABLE_TEMPLATE_PROCESSING
feature flag and the SQL dialect.This PR fixes the logic to re-render the modified template before passing it as a string to
process_template
.(Note that a
jinja2.nodes.Template
is an internal object, different from ajinja2.environment.Template
.)Why wasn't this caught before?
This only happens when
ENABLE_TEMPLATE_PROCESSING
is off. When the feature flag is ON we use theJinjaTemplateProcessor
, which parses the string passed toprocess_template
usingenv.from_string
. Despite the name,env.from_string
also works onjinja2.nodes.Template
objects, so the template is rendered correctly andprocess_template
returns valid SQL.When the feature flag is OFF we use the
NoOpTemplateProcessor
to extract tables, which just returnsstr(sql)
in itsprocess_template
. This converts thejinja2.nodes.Template
into a string that can't be parsed correctly most of the time:Confusingly, the string above is a valid SQL expression depending on the dialect:
Fixes #31307 and #31183.
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
TESTING INSTRUCTIONS
I added a unit test capturing the bug. It failed before this fix.
ADDITIONAL INFORMATION