diff --git a/superset/sql/parse.py b/superset/sql/parse.py index 391bf0d9e08..c9ed22ff0f5 100644 --- a/superset/sql/parse.py +++ b/superset/sql/parse.py @@ -262,8 +262,16 @@ class RLSAsSubqueryTransformer(RLSTransformer): return node if predicate := self.get_predicate(node): - # use alias or name - alias = node.alias or node.sql() + if node.alias: + alias = node.alias + else: + name = ".".join( + part + for part in (node.catalog or "", node.db or "", node.name) + if part + ) + alias = exp.TableAlias(this=exp.Identifier(this=name, quoted=True)) + node.set("alias", None) node = exp.Subquery( this=exp.Select( @@ -683,7 +691,10 @@ class SQLStatement(BaseSQLStatement[exp.Expression]): """ return { - eq.this.sql(comments=False): eq.expression.sql(comments=False) + eq.this.sql( + dialect=self._dialect, + comments=False, + ): eq.expression.sql(comments=False) for set_item in self._parsed.find_all(exp.SetItem) for eq in set_item.find_all(exp.EQ) } diff --git a/tests/unit_tests/sql/parse_tests.py b/tests/unit_tests/sql/parse_tests.py index 1ab7174a184..458caf5fa11 100644 --- a/tests/unit_tests/sql/parse_tests.py +++ b/tests/unit_tests/sql/parse_tests.py @@ -1851,7 +1851,7 @@ FROM ( FROM some_table WHERE id = 42 -) AS some_table +) AS "some_table" WHERE 1 = 1 """.strip(), @@ -1868,7 +1868,7 @@ FROM ( FROM table WHERE id = 42 -) AS table +) AS "table" WHERE 1 = 1 """.strip(), @@ -1925,7 +1925,7 @@ JOIN ( FROM other_table WHERE id = 42 -) AS other_table +) AS "other_table" ON table.id = other_table.id """.strip(), ), @@ -1961,7 +1961,7 @@ FROM ( FROM some_table WHERE id = 42 - ) AS some_table + ) AS "some_table" ) """.strip(), ), @@ -1977,7 +1977,7 @@ FROM ( FROM table WHERE id = 42 -) AS table +) AS "table" UNION ALL SELECT * @@ -2000,7 +2000,7 @@ FROM ( FROM other_table WHERE id = 42 -) AS other_table +) AS "other_table" """.strip(), ), ( @@ -2039,6 +2039,22 @@ INNER JOIN tbl_b AS b ON a.col = b.col """.strip(), ), + ( + "SELECT * FROM public.flights LIMIT 100", + {Table("flights", "public", "catalog1"): "\"AIRLINE\" like 'A%'"}, + """ +SELECT + * +FROM ( + SELECT + * + FROM public.flights + WHERE + "AIRLINE" LIKE 'A%' +) AS "public.flights" +LIMIT 100 + """.strip(), + ), ], ) def test_rls_subquery_transformer( diff --git a/tests/unit_tests/sql_lab_test.py b/tests/unit_tests/sql_lab_test.py index 60bc3c37104..67125833255 100644 --- a/tests/unit_tests/sql_lab_test.py +++ b/tests/unit_tests/sql_lab_test.py @@ -259,13 +259,13 @@ FROM ( FROM t1 WHERE c1 = 1 -) AS t1, ( +) AS "t1", ( SELECT * FROM t2 WHERE c2 = 2 -) AS t2 +) AS "t2" """.strip() )