fix: subquery alias in RLS (#34374)

This commit is contained in:
Beto Dealmeida
2025-07-28 22:58:15 -04:00
committed by GitHub
parent 914ce9aa4f
commit 122bb68e5a
3 changed files with 38 additions and 11 deletions

View File

@@ -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)
}

View File

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

View File

@@ -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()
)