This is an automated email from the ASF dual-hosted git repository.
johnbodley pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git
The following commit(s) were added to refs/heads/master by this push:
new c5e7d870f0 fix(sql_parse): Provide more lenient logic when extracting
latest[_sub]_partition (#28152)
c5e7d870f0 is described below
commit c5e7d870f07983ce4ce16a355d91735ad87394b3
Author: John Bodley <[email protected]>
AuthorDate: Thu Apr 25 22:02:25 2024 -0700
fix(sql_parse): Provide more lenient logic when extracting
latest[_sub]_partition (#28152)
---
superset/sql_parse.py | 21 ++++++++++---------
tests/unit_tests/sql_parse_tests.py | 40 ++++++++++++++++++++-----------------
2 files changed, 34 insertions(+), 27 deletions(-)
diff --git a/superset/sql_parse.py b/superset/sql_parse.py
index 152d42ba94..e31d8b6402 100644
--- a/superset/sql_parse.py
+++ b/superset/sql_parse.py
@@ -1554,16 +1554,19 @@ def extract_tables_from_jinja_sql(sql: str, database:
Database) -> set[Table]:
"latest_partition",
"latest_sub_partition",
):
- # Extract the table referenced in the macro.
- tables.add(
- Table(
- *[
- remove_quotes(part.strip())
- for part in node.args[0].as_const().split(".")[::-1]
- if len(node.args) == 1
- ]
+ # Try to extract the table referenced in the macro.
+ try:
+ tables.add(
+ Table(
+ *[
+ remove_quotes(part.strip())
+ for part in
node.args[0].as_const().split(".")[::-1]
+ if len(node.args) == 1
+ ]
+ )
)
- )
+ except nodes.Impossible:
+ pass
# Replace the potentially problematic Jinja macro with some benign
SQL.
node.__class__ = nodes.TemplateData
diff --git a/tests/unit_tests/sql_parse_tests.py
b/tests/unit_tests/sql_parse_tests.py
index 0655b30295..3a78885abe 100644
--- a/tests/unit_tests/sql_parse_tests.py
+++ b/tests/unit_tests/sql_parse_tests.py
@@ -1857,36 +1857,40 @@ def test_sqlstatement() -> None:
],
)
@pytest.mark.parametrize(
- "macro",
- [
- "latest_partition('foo.bar')",
- "latest_partition(' foo.bar ')", # Non-atypical user error which works
- "latest_partition('foo.%s'|format('bar'))",
- "latest_sub_partition('foo.bar', baz='qux')",
- ],
-)
[email protected](
- "sql,expected",
+ "macro,expected",
[
(
- "SELECT '{{{{ {engine}.{macro} }}}}'",
+ "latest_partition('foo.bar')",
+ {Table(table="bar", schema="foo")},
+ ),
+ (
+ "latest_partition(' foo.bar ')", # Non-atypical user error which
works
{Table(table="bar", schema="foo")},
),
(
- "SELECT * FROM foo.baz WHERE quux = '{{{{ {engine}.{macro} }}}}'",
- {Table(table="bar", schema="foo"), Table(table="baz",
schema="foo")},
+ "latest_partition('foo.%s'|format('bar'))",
+ {Table(table="bar", schema="foo")},
+ ),
+ (
+ "latest_sub_partition('foo.bar', baz='qux')",
+ {Table(table="bar", schema="foo")},
+ ),
+ (
+ "latest_partition('foo.%s'|format(str('bar')))",
+ set(),
+ ),
+ (
+ "latest_partition('foo.{}'.format('bar'))",
+ set(),
),
],
)
def test_extract_tables_from_jinja_sql(
- engine: str,
- macro: str,
- sql: str,
- expected: set[Table],
+ engine: str, macro: str, expected: set[Table]
) -> None:
assert (
extract_tables_from_jinja_sql(
- sql=sql.format(engine=engine, macro=macro),
+ sql=f"'{{{{ {engine}.{macro} }}}}'",
database=Mock(),
)
== expected