Skip to content

Calculated column expressions not parenthesized in WHERE clause, breaking filter precedence #39733

@bdonovan1

Description

@bdonovan1

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:

    WHERE __time >= '2026-01-01' AND __time < '2026-02-01' AND status = 'active' OR status = 'pending' IS TRUE
  4. SQL parses this as:

    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

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):

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. Other keywords like 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    explore:filterRelated to filters in Explorevalidation:requiredA committer should validate the issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions