bito-code-review[bot] commented on code in PR #44501:
URL: https://github.com/apache/superset/pull/44501#discussion_r4073508265


##########
superset/models/helpers.py:
##########
@@ -3820,6 +3839,200 @@ def get_from_clause(
 
         return from_clause, cte
 
+    @staticmethod
+    def _qualified_tables(
+        script: SQLScript, catalog: Optional[str], schema: Optional[str]
+    ) -> set[Table]:
+        """Every table referenced by ``script``, qualified with the dataset's
+        catalog/schema so the two parses in
+        ``_authorize_request_resolved_tables`` compare like for like."""
+        return {
+            table.qualify(catalog=catalog, schema=schema)
+            for statement in script.statements
+            for table in statement.tables
+        }
+
+    @staticmethod
+    def _neutralize_jinja(sql: str) -> str:
+        """
+        Static fallback for ``_declared_tables`` when the stored SQL cannot be
+        rendered: replace every Jinja expression, block, and comment with a
+        benign placeholder so the remaining SQL can be parsed for the tables 
the
+        dataset references. A ``{{ ... }}`` expression becomes a random
+        placeholder identifier (so a templated ``FROM``/``JOIN`` target does 
not
+        vanish and silently reduce the set, and a caller cannot name a real
+        table that matches the placeholder to hide it); ``{% ... %}`` control
+        blocks and ``{# ... #}`` comments are dropped. Unlike the render path
+        this neutralizes identity macros too, so an identity-parameterized 
table
+        may then read as request-introduced; that is the conservative
+        (fail-closed) outcome only reached when rendering is unavailable.
+        """
+        placeholder = f"superset_declared_{uuid.uuid4().hex}"
+        sql = re.sub(r"\{\{.*?\}\}", placeholder, sql, flags=re.DOTALL)
+        sql = re.sub(r"\{%.*?%\}", " ", sql, flags=re.DOTALL)
+        sql = re.sub(r"\{#.*?#\}", " ", sql, flags=re.DOTALL)
+        return sql
+
+    def _render_declared_sql(self, raw_sql: str) -> Optional[str]:
+        """
+        Render the stored SQL with only the request-controllable macros
+        (``JinjaTemplateProcessor.REQUEST_CONTROLLABLE_MACROS``) replaced by a
+        neutral sentinel, while identity macros (``current_user*``) and
+        author-baked macros
+        (``dataset``/``metric``) render with their real values. Tables keyed 
off
+        identity (e.g. ``tenant_{{ current_username() }}.sales``) therefore
+        resolve to the same name as the live render and are not mistaken for a
+        request-introduced table; only tables a request value can steer differ.
+
+        Returns ``None`` when a template processor is unavailable or the render
+        fails, so the caller falls back to static neutralization.
+        """
+        from superset.jinja_context import safe_proxy  # noqa: PLC0415

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Inline import without cycle</b></div>
   <div id="fix">
   
   `safe_proxy` is imported inline at line 3890 even though this module already 
imports from `superset.jinja_context` at module level (line 117), so no 
circular dependency justifies the inline import. Move it into the existing 
module-level import and drop the `# noqa: PLC0415`.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #f22a44</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
superset/models/helpers.py:
##########
@@ -3820,6 +3839,200 @@ def get_from_clause(
 
         return from_clause, cte
 
+    @staticmethod
+    def _qualified_tables(
+        script: SQLScript, catalog: Optional[str], schema: Optional[str]
+    ) -> set[Table]:
+        """Every table referenced by ``script``, qualified with the dataset's
+        catalog/schema so the two parses in
+        ``_authorize_request_resolved_tables`` compare like for like."""
+        return {
+            table.qualify(catalog=catalog, schema=schema)
+            for statement in script.statements
+            for table in statement.tables
+        }
+
+    @staticmethod
+    def _neutralize_jinja(sql: str) -> str:
+        """
+        Static fallback for ``_declared_tables`` when the stored SQL cannot be
+        rendered: replace every Jinja expression, block, and comment with a
+        benign placeholder so the remaining SQL can be parsed for the tables 
the
+        dataset references. A ``{{ ... }}`` expression becomes a random
+        placeholder identifier (so a templated ``FROM``/``JOIN`` target does 
not
+        vanish and silently reduce the set, and a caller cannot name a real
+        table that matches the placeholder to hide it); ``{% ... %}`` control
+        blocks and ``{# ... #}`` comments are dropped. Unlike the render path
+        this neutralizes identity macros too, so an identity-parameterized 
table
+        may then read as request-introduced; that is the conservative
+        (fail-closed) outcome only reached when rendering is unavailable.
+        """
+        placeholder = f"superset_declared_{uuid.uuid4().hex}"

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Duplicated sentinel format</b></div>
   <div id="fix">
   
   Lines 3870 and 3901 both build the sentinel as 
`f"superset_declared_{uuid.uuid4().hex}"`. If the format ever changes (quoting, 
length limits for engine identifiers), the two sites can silently diverge and 
break the declared-vs-rendered set cancellation in `_declared_tables`. Extract 
one shared helper/constant so both call sites produce the identical placeholder 
format.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #f22a44</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



-- 
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