diff --git a/superset/jinja_context.py b/superset/jinja_context.py index 4b02a69a75b..3bd8a9c2ecf 100644 --- a/superset/jinja_context.py +++ b/superset/jinja_context.py @@ -590,6 +590,12 @@ class BaseTemplateProcessor: self._context.update(kwargs) self._context.update(context_addons()) + def get_context(self) -> dict[str, Any]: + """ + Returns the current template context. + """ + return self._context.copy() + def process_template(self, sql: str, **kwargs: Any) -> str: """Processes a sql template diff --git a/superset/sql_parse.py b/superset/sql_parse.py index 8fae4507efa..e82e88ea754 100644 --- a/superset/sql_parse.py +++ b/superset/sql_parse.py @@ -972,12 +972,12 @@ def extract_tables_from_jinja_sql(sql: str, database: Database) -> set[Table]: node.data = "NULL" # re-render template back into a string - rendered_template = Template(template).render() + rendered_sql = Template(template).render(processor.get_context()) return ( tables | ParsedQuery( - sql_statement=processor.process_template(rendered_template), + sql_statement=processor.process_template(rendered_sql), engine=database.db_engine_spec.engine, ).tables )