Skip to content

Comments

fix(sqla): parenthesize extras where/having clauses in query generation#38183

Draft
EnxDev wants to merge 2 commits intomasterfrom
enxdev/fix/parenthesize-extras-where-having-clauses
Draft

fix(sqla): parenthesize extras where/having clauses in query generation#38183
EnxDev wants to merge 2 commits intomasterfrom
enxdev/fix/parenthesize-extras-where-having-clauses

Conversation

@EnxDev
Copy link
Contributor

@EnxDev EnxDev commented Feb 23, 2026

When extras.where or extras.having clauses are combined with other query filters using AND, the resulting SQL can produce unexpected evaluation order due to SQL operator precedence.
This wraps user-supplied WHERE and HAVING expressions in parentheses to ensure they are treated as a single logical unit when composed with other clauses.

Also fixes a minor issue where iterating over filter could fail if None was passed instead of an empty list.

SUMMARY

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

When extras.where or extras.having clauses are combined with other
query filters using AND, the resulting SQL can produce unexpected
evaluation order due to SQL operator precedence. This wraps
user-supplied WHERE and HAVING expressions in parentheses to ensure
they are treated as a single logical unit when composed with other
clauses.

Also fixes a minor issue where iterating over filter could fail if
None was passed instead of an empty list.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace raw string wrapping f"({expr})" with SQLAlchemy's Grouping
construct to parenthesize extras where/having clauses. Grouping is the
proper abstraction layer for this — it lets SQLAlchemy handle
parenthesization in a dialect-agnostic way rather than doing string
surgery on raw SQL text.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants