korbit-ai[bot] commented on code in PR #32175:
URL: https://github.com/apache/superset/pull/32175#discussion_r1945860279


##########
superset/jinja_context.py:
##########
@@ -596,12 +596,24 @@ def process_template(self, sql: str, **kwargs: Any) -> 
str:
         >>> sql = "SELECT '{{ datetime(2017, 1, 1).isoformat() }}'"
         >>> process_template(sql)
         "SELECT '2017-01-01T00:00:00'"
+        This function will attempt to resolve nested Jinja expressions
+        up to 4 iterations.
         """
-        template = self.env.from_string(sql)
+        # Initialize the rendered SQL with the original SQL string.
+        rendered_sql = sql
         kwargs.update(self._context)
-
         context = validate_template_context(self.engine, kwargs)
-        return template.render(context)
+
+        # Render recursively up to 4 levels.
+        for _i in range(4):
+            template = self.env.from_string(rendered_sql)
+            new_rendered_sql = template.render(context)
+            # If rendering no longer changes the SQL, break early.
+            if new_rendered_sql == rendered_sql:
+                break
+            rendered_sql = new_rendered_sql

Review Comment:
   ### Redundant Jinja Template Compilation <sub>![category 
Performance](https://img.shields.io/badge/Performance-4f46e5)</sub>
   
   <details>
     <summary>Tell me more</summary>
   
   ###### What is the issue?
   The Jinja template compilation (from_string) is performed unnecessarily in 
each iteration, even when the template string hasn't changed.
   
   ###### Why this matters
   Jinja template compilation is a relatively expensive operation that parses 
the template string and creates an internal representation. Recompiling the 
same template string multiple times adds unnecessary overhead.
   
   ###### Suggested change ∙ *Feature Preview*
   Move the template compilation outside the loop when rendered_sql hasn't 
changed:
   ```python
   rendered_sql = sql
   kwargs.update(self._context)
   context = validate_template_context(self.engine, kwargs)
   
   # Render recursively up to 4 levels
   for _i in range(4):
       template = self.env.from_string(rendered_sql)
       new_rendered_sql = template.render(context)
       # If rendering no longer changes the SQL, break early
       if new_rendered_sql == rendered_sql:
           break
       rendered_sql = new_rendered_sql
       
   return rendered_sql
   ```
   
   
   </details>
   
   <sub>
   
   [![Report a problem with this 
comment](https://img.shields.io/badge/Report%20a%20problem%20with%20this%20comment-gray.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9IiNmNWVjMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBjbGFzcz0ibHVjaWRlIGx1Y2lkZS10cmlhbmdsZS1hbGVydCI+PHBhdGggZD0ibTIxLjczIDE4LTgtMTRhMiAyIDAgMCAwLTMuNDggMGwtOCAxNEEyIDIgMCAwIDAgNCAyMWgxNmEyIDIgMCAwIDAgMS43My0zIi8+PHBhdGggZD0iTTEyIDl2NCIvPjxwYXRoIGQ9Ik0xMiAxN2guMDEiLz48L3N2Zz4=)](https://app.korbit.ai/feedback/aa91ff46-6083-4491-9416-b83dd1994b51/cd11412c-bfa5-4080-a3fb-f2cb4f2c304a?suggestedFixEnabled=true)
   
   💬 Chat with Korbit by mentioning @korbit-ai.
   </sub>
   
   <!--- korbi internal id:1f120a24-0b10-4492-84ca-9b57bfe21724 -->
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to