john-bodley commented on code in PR #27470: URL: https://github.com/apache/superset/pull/27470#discussion_r1522006662
########## superset/sql_parse.py: ########## @@ -284,19 +286,64 @@ def _extract_tables_from_sql(self) -> set[Table]: Extract all table references in a query. Note: this uses sqlglot, since it's better at catching more edge cases. + + Due to Jinja templating a multiphase approach is necessary as the referenced SQL + statement is likely ill-defined (due to the presence of the Jinja macros) and + thus non-parsable by SQLGlot. + + Firstly, we extract any tables referenced within the confines of specific Jinja + macros. Secondly, we replace these non-SQL Jinja calls with a pseudo-benign SQL + expression to help ensure that the resulting SQL statements are parsable by + SQLGlot. """ + + from superset.jinja_context import ( # pylint: disable=import-outside-toplevel + get_template_processor, + ) + + tables = set() + sql = self.stripped() + + # Mock the required database as the processor signature is exposed publically. + processor = get_template_processor(database=Mock(backend=self._dialect)) + template = processor.env.parse(sql) + + for node in template.find_all(nodes.Call): + if isinstance(node.node, nodes.Getattr) and node.node.attr in ( Review Comment: @michael-s-molina see https://github.com/apache/superset/pull/27464#issuecomment-1992376478. -- 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: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org