You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "bdonovan1 (via GitHub)" <gi...@apache.org> on 2026/04/28 19:23:14 UTC

[I] Calculated column expressions not parenthesized in WHERE clause, breaking filter precedence [superset]

bdonovan1 opened a new issue, #39733:
URL: https://github.com/apache/superset/issues/39733

   ### Bug description
   
   ## Description
   
   Calculated column expressions containing `OR` are embedded into the `WHERE` clause without parentheses. Due to SQL operator precedence (`AND` binds tighter than `OR`), preceding filter conditions including time range filters are silently dropped.
   
   This is the same class of bug fixed by #38183 for `extras.where`/`extras.having`, but on a different code path. PR #38183 wrapped adhoc SQL filter expressions in `Grouping` at `helpers.py:3244`. Calculated columns used as SIMPLE filters (e.g., `IS TRUE`, `IN`, `=`) go through a separate path - `convert_tbl_column_to_sqla_col()` → `literal_column(expression)` - and are not parenthesized.
   
   ## Steps to Reproduce
   
   1. Create a calculated column with an `OR` expression:
      - Name: `is_active`
      - Expression: `status = 'active' OR status = 'pending'`
   
   2. Create a chart with a time range filter and add filter: `is_active IS TRUE`
   
   3. Generated SQL:
      ```sql
      WHERE __time >= '2026-01-01' AND __time < '2026-02-01' AND status = 'active' OR status = 'pending' IS TRUE
      ```
   
   4. SQL parses this as:
      ```sql
      WHERE (__time >= '2026-01-01' AND __time < '2026-02-01' AND status = 'active')
         OR (status = 'pending' IS TRUE)
      ```
   
      The time range filter is silently dropped from the second branch, and we will end up visualizing 'pending' data outside of the chosen time range.
   
   ## Expected Behavior
   
   ```sql
   WHERE __time >= '2026-01-01' AND __time < '2026-02-01' AND (status = 'active' OR status = 'pending') IS TRUE
   ```
   
   ## Proposed Fix
   
   Following the same pattern as #38183, wrap expression-based columns in `Grouping` when used in the filter path of `get_sqla_query()` (after `sqla_col` is finalized at ~line 3067):
   
   ```python
   if sqla_col is not None and (
       (col_obj and col_obj.expression) or is_adhoc_column(flt_col)
   ):
       sqla_col = Grouping(sqla_col)
   ```
   
   This affects only WHERE/HAVING. SELECT, GROUP BY, and ORDER BY are unchanged. Affects all database backends.
   
   
   ### Screenshots/recordings
   
   _No response_
   
   ### Superset version
   
   master / latest-dev
   
   ### Python version
   
   3.9
   
   ### Node version
   
   16
   
   ### Browser
   
   Chrome
   
   ### Additional context
   
   _No response_
   
   ### Checklist
   
   - [ ] I have searched Superset docs and Slack and didn't find a solution to my problem.
   - [ ] I have searched the GitHub issue tracker and didn't find a similar bug report.
   - [ ] I have checked Superset's logs for errors and if I found a relevant Python stacktrace, I included it here as text in the "additional context" section.


-- 
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-unsubscribe@superset.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [I] Calculated column expressions not parenthesized in WHERE clause, breaking filter precedence [superset]

Posted by "Vitor-Avila (via GitHub)" <gi...@apache.org>.
Vitor-Avila closed issue #39733: Calculated column expressions not parenthesized in WHERE clause, breaking filter precedence
URL: https://github.com/apache/superset/issues/39733


-- 
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-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org