You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "mohamedsigservice (via GitHub)" <gi...@apache.org> on 2026/05/24 08:55:36 UTC
[D] RLS condition inconsistently applied inside dataset metric subqueries [superset]
GitHub user mohamedsigservice created a discussion: RLS condition inconsistently applied inside dataset metric subqueries
Hello,
I am having an issue with Row Level Security (RLS) behavior in Apache Superset.
I created an RLS rule like this:
```sql
warehouse_id = XX OR warehouse_id IS NULL
```
The problem is that this clause is applied inconsistently depending on the query structure.
In some cases, the RLS condition is also injected inside subqueries used by dataset metrics, while in other cases it is only applied to the outer query.
This creates incorrect KPI calculations because some metrics are filtered internally while others are not.
What I want:
* Control where the RLS clause is applied
* Prevent it from being injected inside specific subqueries/metrics
* Or force it to always apply at a specific level of the query
Questions:
1. How does Superset decide where to inject RLS clauses?
2. Why are dataset metric subqueries sometimes affected and sometimes not?
3. Is there a way to control RLS scope/query injection behavior?
4. What is the recommended architecture for datasets/metrics when using RLS with complex SQL and subqueries?
Environment:
* Apache Superset (Docker)
* PostgreSQL
* Datasets using custom SQL, metrics, and subqueries
Thanks.
GitHub link: https://github.com/apache/superset/discussions/40400
----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: notifications-unsubscribe@superset.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [D] RLS condition inconsistently applied inside dataset metric subqueries [superset]
Posted by "mohamedsigservice (via GitHub)" <gi...@apache.org>.
GitHub user mohamedsigservice added a comment to the discussion: RLS condition inconsistently applied inside dataset metric subqueries
@Dosu Thanks for the detailed explanation.
I have `ALLOW_ADHOC_SUBQUERY = True` enabled, and I can confirm that RLS is correctly applied in the outer query. However, I'm observing that RLS is **not injected inside subqueries used as denominators** in two specific metrics (`Taux de succès` and `Taux d'activation`) in a dataset called `tableau_brand`.
Here is the pattern used in these metrics:
```sql
SELECT SUM(value_2::int)
FROM (SELECT * FROM tableau_brand) AS sub
WHERE type = 'ts'
AND product_category IS NULL
AND date >= ...
AND date < ...
```
The outer query correctly receives the RLS clause:
```sql
WHERE (warehouse_id = 23 OR warehouse_id IS NULL)
OR (warehouse_id = 40 OR warehouse_id IS NULL)
...
AND (brand_id = 1 OR brand_id IS NULL)
```
But the subquery inside the metric does **not** receive the same RLS injection.
**Important context:** `tableau_brand` is a **virtual dataset** in Superset (defined by a custom SQL query), not a physical PostgreSQL table. My hypothesis is that sqlglot does not recognize it as an `exp.Table` node since it has no physical existence in the database, and therefore skips RLS injection inside the subquery.
For comparison, a similar subquery pattern in another dataset (`kpi_groupe`, which is also a virtual dataset) seems to behave differently — though I'm still investigating whether RLS is truly being injected there or simply producing consistent results by coincidence.
**Questions:**
- Is there a known difference in RLS injection behavior between physical tables and virtual datasets when referenced inside metric subqueries?
- Does sqlglot resolve virtual dataset names against the Superset metadata layer, or does it treat them purely as SQL identifiers?
- Is there a way to confirm/debug exactly which `exp.Table` nodes are detected and transformed during RLS injection for a given query?
Environment: Apache Superset (Docker), PostgreSQL, `ALLOW_ADHOC_SUBQUERY = True`.
GitHub link: https://github.com/apache/superset/discussions/40400#discussioncomment-17040632
----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: notifications-unsubscribe@superset.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [D] RLS condition inconsistently applied inside dataset metric subqueries [superset]
Posted by "mohamedsigservice (via GitHub)" <gi...@apache.org>.
GitHub user mohamedsigservice added a comment to the discussion: RLS condition inconsistently applied inside dataset metric subqueries
@dosu
Thanks, this clarifies a lot.
I would like more details about how sqlglot detects tables/subqueries during RLS injection.
For example:
* Does Superset/sqlglot traverse the full AST recursively and inject RLS into every `SELECT` node?
* What exactly is considered a "table reference"?
* Are CTEs (`WITH` clauses), scalar subqueries, correlated subqueries, and derived tables all treated the same way?
* Is the behavior based only on `FROM/JOIN` detection?
* Why would some metric expressions trigger recursive RLS injection while others do not?
I am trying to better understand which SQL patterns are considered safe vs unsafe when creating dataset metrics.
For example, are these cases handled differently?
```sql id="yvj8q1"
SUM(amount)
```
vs
```sql id="q3m7kd"
(
SELECT SUM(s.amount)
FROM sales s
WHERE s.product_id = main.product_id
)
```
vs
```sql id="w6t2bn"
SUM(
CASE WHEN EXISTS (
SELECT 1
FROM stock st
WHERE st.product_id = product_id
)
THEN qty
END)
```
Also:
* does sqlglot resolve aliases/scopes before injecting RLS?
* is there a way to inspect/debug the transformed AST or the SQL after RLS injection but before execution?
* does the sqlglot/RLS parsing happen before or after Jinja templating is rendered?
For example, if a metric contains Jinja-generated SQL:
* is the AST built from the raw template,
* or from the final rendered SQL after Jinja execution?
Thanks.
GitHub link: https://github.com/apache/superset/discussions/40400#discussioncomment-17039345
----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: notifications-unsubscribe@superset.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [D] RLS condition inconsistently applied inside dataset metric subqueries [superset]
Posted by "dosubot[bot] (via GitHub)" <gi...@apache.org>.
GitHub user dosubot[bot] added a comment to the discussion: RLS condition inconsistently applied inside dataset metric subqueries
<!-- Answer -->
Great questions — here's a detailed breakdown of how the RLS/sqlglot machinery works internally:
## AST Traversal: Not Every SELECT — Every `exp.Table` Node
The RLS transformers don't inject into every SELECT. Instead, they operate at the **`exp.Table` node level**. When `self._parsed.transform(transformer)` is called, sqlglot recursively walks the entire AST and the transformer's `__call__` method fires on every node — but it only acts on `exp.Table` nodes [[1]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/sql/parse.py#L203-L204). Other nodes pass through unchanged.
- **AS_PREDICATE**: Finds the parent context (FROM → appends to WHERE; JOIN → appends to ON clause) [[2]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/sql/parse.py#L216-L232)
- **AS_SUBQUERY**: Replaces the table node with `(SELECT * FROM table WHERE <rls>) AS alias` [[3]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/sql/parse.py#L268-L276)
## What Counts as a "Table Reference"
Table detection uses `traverse_scope()` from sqlglot to iterate all scopes and collect `exp.Table` sources [[4]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/sql/parse.py#L1316-L1321):
```python
sources = [
source
for scope in traverse_scope(statement)
for source in scope.sources.values()
if isinstance(source, exp.Table) and not is_cte(source, scope)
]
```
**Detected as tables**: base tables, schema-qualified tables, aliased tables, JOIN targets.
**NOT detected as tables**: CTE references (explicitly filtered), derived tables (subqueries in FROM — these are `exp.Subquery`, not `exp.Table`), table-valued functions.
## CTEs, Scalar Subqueries, Correlated Subqueries, Derived Tables
They are **not** treated the same:
- **CTEs**: Explicitly excluded from table detection via `is_cte()` [[5]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/sql/parse.py#L1333-L1352). But tables *inside* the CTE definition body are detected and receive RLS. This prevents bypass attacks like `WITH t AS (SELECT * FROM restricted) SELECT * FROM t`.
- **Derived tables** (`FROM (SELECT ...)`): The derived table itself is not an `exp.Table`, but tables *inside* the subquery are found via scope traversal and get RLS.
- **Scalar/correlated subqueries**: Same — the subquery isn't a table, but tables referenced within it are discovered and injected with RLS.
So yes, detection is fundamentally **FROM/JOIN-based** at the scope level — it finds `exp.Table` nodes that appear as sources in any scope.
## Your Three Metric Examples
```sql
SUM(amount)
```
→ **No subquery detected.** `has_subquery()` returns `False` [[6]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/sql/parse.py#L809-L816). No special RLS processing of the metric expression itself. RLS is applied only to the dataset's main query.
```sql
(SELECT SUM(s.amount) FROM sales s WHERE s.product_id = main.product_id)
```
→ **Subquery detected.** `has_subquery()` finds the nested SELECT [[6]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/sql/parse.py#L809-L816). If `ALLOW_ADHOC_SUBQUERY` is `False` (default): **raises `SupersetSecurityException`**. If enabled: `apply_rls()` injects RLS into the `sales` table reference inside the subquery [[7]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/models/helpers.py#L136-L138).
```sql
SUM(CASE WHEN EXISTS (SELECT 1 FROM stock st WHERE st.product_id = product_id) THEN qty END)
```
→ **Subquery detected** (the `SELECT 1 FROM stock` is a nested SELECT found by AST walking). Same behavior as above — blocked or has RLS applied to `stock`.
## Alias/Scope Resolution
The transformer **does not resolve aliases before injection**. Instead, it qualifies RLS predicate columns with the table's alias (or name if no alias) [[8]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/sql/parse.py#L212-L214):
```python
for column in predicate.find_all(exp.Column):
column.set("table", node.alias or node.this)
```
So if your table is `FROM sales AS s`, the RLS predicate `warehouse_id = XX` becomes `s.warehouse_id = XX`. This is literal qualification, not semantic resolution — the transformer doesn't "understand" scope context, it just stamps the table/alias onto predicate columns.
## Jinja vs RLS Ordering
**Jinja renders FIRST, then RLS is applied to the rendered SQL.**
The pipeline is [[9]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/sqllab/query_render.py#L60-L63) [[10]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/sql_lab.py#L392-L409):
1. **Jinja rendering** → produces final SQL string
2. **SQL parsing** → sqlglot parses into AST
3. **RLS injection** → transforms AST
4. **Format back to SQL** → string output
5. **Execution**
So the AST is built from the **final rendered SQL**, not the raw Jinja template. Your Jinja-generated SQL is fully expanded before sqlglot ever sees it.
## Debugging / Inspecting Transformed SQL
A few options:
- **SQL Lab query history**: The `executed_sql` field on the Query model stores the final SQL post-RLS [[11]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/sql_lab.py#L478). Visible in the SQL Lab UI query history panel.
- **Debug logging**: Set `logging.getLogger('superset.sql_lab').setLevel(logging.DEBUG)` — the final SQL is logged at debug level [[12]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/sql_lab.py#L1230).
- **QUERY_LOGGER callback** in `superset_config.py` [[13]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/config.py#L963-L972): receives the final transformed SQL before execution.
- **Chart "View Query" button**: In Explore view, the displayed SQL should reflect post-RLS transformation.
## Practical Takeaway for "Safe" Metric Patterns
| Pattern | RLS behavior |
|---------|-------------|
| Simple aggregates (`SUM(col)`, `COUNT(*)`) | No subquery → no RLS on metric itself; RLS only on main dataset query |
| Expressions with no SELECT (`CASE WHEN ... THEN col END`) | Same — safe, no subquery detection |
| Metrics with subqueries (correlated or scalar) | Triggers subquery detection → blocked by default, or RLS injected into all tables in the subquery |
| CTEs used in dataset SQL | CTE *references* excluded from RLS, but tables *inside* CTE definitions receive RLS |
The safest approach: **keep metrics as simple aggregate expressions** without embedded SELECT statements. Move complex filtering logic into the dataset's base SQL (virtual dataset definition) where RLS will be applied uniformly at the outer level.
<!-- Dosu Comment Footer -->
*To reply, just mention [@dosu](https://go.dosu.dev/dosubot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=reply-with-mention&utm_term=apache%2Fsuperset).*
---
Share context across your team and agents. Try [Dosu](https://dosu.dev?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-tagline&utm_term=apache%2Fsuperset).
[](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-feedback&utm_term=apache%2Fsuperset&message_id=115c7cfa-4e16-4ce9-9746-2ee4cd7e709e) [](https://github.dosu.com/apache/superset?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-learn-repo&utm_term=apache%2Fsuperset) [](https://app.dosu.dev/signup?referrer=openSource&source=github-footer&utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-add-team&utm_term=apache%2Fsuperset)
GitHub link: https://github.com/apache/superset/discussions/40400#discussioncomment-17039404
----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: notifications-unsubscribe@superset.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [D] RLS condition inconsistently applied inside dataset metric subqueries [superset]
Posted by "dosubot[bot] (via GitHub)" <gi...@apache.org>.
GitHub user dosubot[bot] added a comment to the discussion: RLS condition inconsistently applied inside dataset metric subqueries
<!-- Answer -->
Your hypothesis is close, but the root cause is slightly different than you might expect. Sqlglot **does** detect `tableau_brand` as an `exp.Table` node in your metric subquery — the issue is in the **RLS predicate lookup**, not in table detection.
## How RLS Predicate Matching Actually Works
When `apply_rls()` processes your metric subquery `SELECT * FROM tableau_brand`, it:
1. Extracts the table name `"tableau_brand"` via `traverse_scope()` [[1]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/sql/parse.py#L1316-L1330)
2. Looks up RLS predicates by querying `SqlaTable` with an **exact name match** [[2]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/utils/rls.py#L85-L102):
```python
dataset = db.session.query(SqlaTable).filter(
and_(
SqlaTable.database_id == database.id,
catalog_predicate,
SqlaTable.schema == table.schema,
SqlaTable.table_name == table.table, # exact match on "tableau_brand"
)
).one_or_none()
if not dataset:
return [] # No predicates found → no RLS injection
return [str(predicate.compile(...)) for predicate in dataset.get_sqla_row_level_filters()]
```
## Why RLS Isn't Injected in Your Case
The most likely explanation: **your RLS rules are associated with the underlying physical tables** (the tables referenced *inside* the virtual dataset's SQL), not with the `tableau_brand` virtual dataset record itself.
When the metric subquery says `FROM tableau_brand`:
- Sqlglot sees it as `exp.Table(name="tableau_brand")`
- The lookup finds the `SqlaTable` record for `tableau_brand`
- But if no RLS rules are directly associated with that `SqlaTable` record → empty predicates → no injection
**There is no recursive resolution.** Superset does NOT:
- Detect that `tableau_brand` is a virtual dataset
- Parse its underlying SQL to find physical tables
- Cascade RLS rules from those physical tables into the metric subquery
This is an architectural limitation — `apply_rls()` receives only `(database, catalog, schema, parsed_statement)` and has no access to the dataset object or its virtual SQL [[3]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/utils/rls.py#L32-L36).
## Why the Outer Query Gets RLS Correctly
For the main dataset query, Superset uses a completely different code path: `get_sqla_row_level_filters()` is called directly on the dataset object [[4]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/models/helpers.py#L1928), which knows its own RLS rules. The outer query RLS is applied via SQLAlchemy filter expressions at the ORM level, not through the sqlglot-based `apply_rls()` mechanism.
## The `kpi_groupe` Difference
If `kpi_groupe` appears to work, check whether:
- It has RLS rules directly associated with the virtual dataset record itself (not just underlying tables)
- Or the consistent results are coincidental (the metric doesn't filter out rows that RLS would affect)
## Debugging
To confirm which `exp.Table` nodes are detected and what predicates are resolved, you can add temporary logging in `superset/utils/rls.py` at the `apply_rls()` function [[5]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/utils/rls.py#L47-L54):
```python
for table in parsed_statement.tables:
logger.debug(f"RLS: detected table={table}, predicates={get_predicates_for_table(table, database, catalog)}")
```
## Recommended Workarounds
1. **Associate RLS rules directly with the `tableau_brand` virtual dataset** — this way the exact name match succeeds and predicates are injected into the subquery.
2. **Reference the physical table name in your metric subqueries** instead of the virtual dataset name — e.g., `SELECT SUM(value_2::int) FROM physical_table_name AS sub WHERE ...`. Physical table RLS rules will then apply.
3. **Avoid subqueries in metrics entirely** — restructure as simple aggregations (`SUM(CASE WHEN type = 'ts' THEN value_2::int END)`) and let the dataset's main query handle RLS uniformly at the outer level.
4. **Embed security in the virtual dataset SQL** using Jinja:
```sql
SELECT * FROM physical_brands WHERE {{ filter_values('warehouse_id') }}
```
Option 3 is the safest architectural pattern — it ensures RLS is applied once, consistently, at the dataset query level without relying on recursive subquery injection.
<!-- Dosu Comment Footer -->
*To reply, just mention [@dosu](https://go.dosu.dev/dosubot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=reply-with-mention&utm_term=apache%2Fsuperset).*
---
Docs are dead. Just use [Dosu](https://dosu.dev?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-tagline&utm_term=apache%2Fsuperset).
[](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-feedback&utm_term=apache%2Fsuperset&message_id=ce0dd4d6-adea-4f5c-907f-a73b12522bef) [](https://github.dosu.com/apache/superset?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-ask-repo&utm_term=apache%2Fsuperset) [](https://app.dosu.dev/signup?referrer=openSource&source=github-footer&utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-share-team&utm_term=apache%2Fsuperset)
GitHub link: https://github.com/apache/superset/discussions/40400#discussioncomment-17040759
----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: notifications-unsubscribe@superset.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [D] RLS condition inconsistently applied inside dataset metric subqueries [superset]
Posted by "dosubot[bot] (via GitHub)" <gi...@apache.org>.
GitHub user dosubot[bot] added a comment to the discussion: RLS condition inconsistently applied inside dataset metric subqueries
<!-- Answer -->
I want to correct one important point in your understanding: **tables inside CTE bodies DO receive RLS injection**. The `is_cte()` exclusion only prevents CTE *references* (i.e., `SELECT * FROM cte_name`) from being treated as real tables — this is a security measure against name-shadowing attacks [[1]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/sql/parse.py#L1335-L1342). The actual tables *within* the CTE definition are still detected via `traverse_scope()` and receive RLS predicates.
For example:
```sql
WITH sub AS (SELECT * FROM tableau_brand WHERE type = 'ts')
SELECT COUNT(*) FROM sub
```
Here:
- `tableau_brand` inside the CTE body **will** be detected as an `exp.Table` and RLS will be attempted on it
- `sub` in `FROM sub` will be **excluded** (it's a CTE reference, not a real table)
So if you're not seeing RLS inside your CTEs, the issue is likely the same predicate lookup problem we discussed earlier — `tableau_brand` is found as a table, but `get_predicates_for_table()` either returns empty predicates (no RLS rules directly associated with that dataset name) or the schema qualification doesn't match [[2]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/utils/rls.py#L85-L102).
## On Database-Native RLS as a Long-Term Approach
Your instinct is sound. Given the architectural limitations:
- Superset's application-level RLS has coverage gaps: it's not applied in `Database.get_df()`, MCP tools, or SQL Lab by default [[3]](https://github.com/apache/superset/pull/36529) [[4]](https://github.com/apache/superset/discussions/33308#discussioncomment-12999029)
- There is no planned support for user-configurable CTE-aware RLS injection scope
- No roadmap items exist for recursive RLS resolution in virtual dataset/metric subqueries
PostgreSQL native RLS applied at the base table level would enforce security **regardless** of how Superset constructs the query — no dependency on AST transformation, schema matching, or dataset metadata resolution. It's the most robust approach for your use case with complex correlated denominators.
## Caveats with Database-Native RLS
There's no official Superset guidance on this pattern, but consider:
- **Connection pooling**: If Superset connects with a single service account, PostgreSQL RLS policies need to use session variables (e.g., `SET app.current_tenant = ...`) rather than `current_user`. You'd need to configure Superset's `DB_CONNECTION_MUTATOR` or equivalent to set these variables per-request.
- **Complementary, not replacement**: You may still want Superset-level RLS for dashboard/UI-level filtering and the DB-level RLS as the enforcement backstop.
- **Performance**: PostgreSQL RLS with proper indexes on policy columns performs well, often better than Superset injecting complex OR clauses into deeply nested subqueries.
## Summary
For your specific pattern (virtual dataset + correlated metric denominators + complex Jinja filters), database-native RLS is the most architecturally sound long-term solution. There are no current plans for CTE-scope-aware RLS or recursive virtual dataset resolution in Superset's RLS system.
<!-- Dosu Comment Footer -->
*To reply, just mention [@dosu](https://go.dosu.dev/dosubot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=reply-with-mention&utm_term=apache%2Fsuperset).*
---
Docs are dead. Just use [Dosu](https://dosu.dev?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-tagline&utm_term=apache%2Fsuperset).
[](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-feedback&utm_term=apache%2Fsuperset&message_id=7bba409e-9e28-4040-80b4-9dc45caa4ed1) [](https://github.dosu.com/apache/superset?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-ask-repo&utm_term=apache%2Fsuperset) [](https://app.dosu.dev/signup?referrer=openSource&source=github-footer&utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-share-team&utm_term=apache%2Fsuperset)
GitHub link: https://github.com/apache/superset/discussions/40400#discussioncomment-17060626
----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: notifications-unsubscribe@superset.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [D] RLS condition inconsistently applied inside dataset metric subqueries [superset]
Posted by "mohamedsigservice (via GitHub)" <gi...@apache.org>.
GitHub user mohamedsigservice added a comment to the discussion: RLS condition inconsistently applied inside dataset metric subqueries
@dosu
Thanks for the detailed explanation. To summarize my specific situation and close the loop:
I have a virtual dataset built on tableau_brand (a materialized view) with complex Jinja2 filters. My metrics require correlated denominators — specifically COUNT(DISTINCT ...) and SUM(...) calculated over a filtered subset of rows that doesn't match the main query's GROUP BY dimension. This forced me to use either:
- Subqueries inside metrics → RLS is injected inside the subquery via ALLOW_ADHOC_SUBQUERY, but inconsistently (sometimes the RLS clause references columns not present on the subquery's table, causing errors or wrong results)
- CTEs inside the dataset SQL → RLS is NOT injected inside CTE bodies referencing the base table directly, making CTEs an effective bypass of RLS — which is the opposite of what I want
Based on your explanation, the root cause is clear: CTE references are explicitly excluded from exp.Table detection, so tables inside CTE definitions that are named after the base table don't get RLS injected.
The only viable workaround I see given the current architecture is to move the aggregation logic to PostgreSQL-level views or functions, and apply PostgreSQL native Row Level Security directly on the base tables, so RLS is enforced at the DB level regardless of how Superset constructs the query.
Is this the recommended long-term approach? Is there any planned support for CTE-aware RLS injection, or a way to explicitly declare that a CTE body should inherit the RLS policy of its base table?
GitHub link: https://github.com/apache/superset/discussions/40400#discussioncomment-17060537
----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: notifications-unsubscribe@superset.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [D] RLS condition inconsistently applied inside dataset metric subqueries [superset]
Posted by "dosubot[bot] (via GitHub)" <gi...@apache.org>.
GitHub user dosubot[bot] added a comment to the discussion: RLS condition inconsistently applied inside dataset metric subqueries
<!-- Greeting -->
Hi @mohamedsigservice! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the superset team.
<!-- Answer -->
Here's what I found about how RLS injection works in Superset and the behavior you're experiencing:
**How Superset decides where to inject RLS:**
Superset uses sqlglot's AST (Abstract Syntax Tree) transformation to recursively detect **all tables** in a query—including those inside subqueries and metric definitions—and applies RLS predicates to each one [[1]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/sql/parse.py). The injection method depends on the database engine's capabilities [[2]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/db_engine_specs/base.py#L440-L452):
- **AS_PREDICATE**: Appends RLS directly to WHERE/ON clauses
- **AS_SUBQUERY**: Wraps each table in a filtered subquery (used when the DB supports subqueries and aliases)
**Why metric subqueries are sometimes affected:**
When the `ALLOW_ADHOC_SUBQUERY` feature flag is enabled, Superset's `validate_adhoc_subquery()` function parses ad-hoc SQL expressions (including metric definitions) and calls `apply_rls()` on any subqueries it detects [[3]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/models/helpers.py#L108-L139). This means RLS gets injected into tables at **all nesting levels**. The inconsistency you're seeing likely depends on whether a given metric's SQL contains explicit table references that sqlglot can detect vs. simple column references.
**Can you control RLS scope?**
Unfortunately, **there is no mechanism to selectively exclude specific subqueries or metrics from RLS injection** [[4]](https://github.com/apache/superset/issues/37695). The recursive table detection applies RLS to every table it finds. Your only levers are:
- `RLS_IN_SQLLAB` feature flag (controls whether RLS applies in SQL Lab) [[5]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/config.py#L517-L519)
- Role-based filter types (`REGULAR` vs `BASE`) to control which roles get the filter
- `group_key` to OR filters together within a group [[6]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/connectors/sqla/models.py#L740-L753)
**Recommended workarounds for your use case:**
1. **Restructure metrics** so they don't reference tables directly in subqueries—instead, rely on the dataset's main query to pre-filter rows before aggregation.
2. **Use physical datasets** where possible, as virtual datasets with JOINs can trigger double-application of RLS (a known regression in Superset 6) [[7]](https://github.com/apache/superset/pull/38601).
3. **Design your RLS clause** to be safe when applied at any level—e.g., ensure `warehouse_id` exists on all referenced tables, or make the clause a no-op on tables without that column by using conditions that always evaluate to true on irrelevant tables.
4. **Specify explicit dataset IDs** in RLS rules rather than applying them globally, so the clause only targets datasets where it makes sense.
This is a known architectural limitation. There's ongoing work related to RLS double-application in virtual datasets ([PR #37395](https://github.com/apache/superset/pull/37395)), but no fine-grained scope control for RLS injection is currently planned.
<!-- Dosu Comment Footer -->
*To reply, just mention [@dosu](https://go.dosu.dev/dosubot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=reply-with-mention&utm_term=apache%2Fsuperset).*
---
Docs are dead. Just use [Dosu](https://dosu.dev?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-tagline&utm_term=apache%2Fsuperset).
[](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-feedback&utm_term=apache%2Fsuperset&message_id=e15c1286-f240-4c8b-a790-8286e66d9587) [](https://github.dosu.com/apache/superset?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-ask-repo&utm_term=apache%2Fsuperset) [](https://app.dosu.dev/signup?referrer=openSource&source=github-footer&utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-share-team&utm_term=apache%2Fsuperset)
GitHub link: https://github.com/apache/superset/discussions/40400#discussioncomment-17039190
----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: notifications-unsubscribe@superset.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org