Skip to content

fix: EXPOSED-987 Add withParentheses to set operations for correct precedence - #2776

Open
Christopher Jenkins (chrisjenx) wants to merge 1 commit into
JetBrains:mainfrom
chrisjenx:fix/EXPOSED-987-set-operation-parentheses
Open

fix: EXPOSED-987 Add withParentheses to set operations for correct precedence#2776
Christopher Jenkins (chrisjenx) wants to merge 1 commit into
JetBrains:mainfrom
chrisjenx:fix/EXPOSED-987-set-operation-parentheses

Conversation

@chrisjenx

Copy link
Copy Markdown

Summary

  • Adds withParentheses parameter (default true) to union(), unionAll(), intersect(), except() extension functions in both JDBC and R2DBC modules
  • When true, wraps any preceding set operation in parentheses so the DSL chain order matches SQL evaluation order
  • Fixes Except.copy() which incorrectly returned Intersect instead of Except

Motivation

When chaining query1.union(query2).intersect(query3), SQL operator precedence causes INTERSECT to bind tighter than UNION, producing incorrect results. This adds parenthesization so the DSL chain order is respected: (SELECT 1 UNION SELECT 2) INTERSECT SELECT 3.

Approach suggested by @chantal-loncle in EXPOSED-987.

Test plan

  • Updated testIntersectWithThreeQueries to verify deterministic results with default parentheses
  • Added testIntersectWithoutParentheses to verify withParentheses = false preserves legacy behavior
  • Added testSetOperationGeneratesParentheses to verify SQL string output
  • ./gradlew :exposed-tests:test_h2_v2 --tests "*.UnionTests" — all pass
  • ./gradlew :exposed-r2dbc-tests:test_h2_v2 --tests "*.UnionTests" — all pass
  • ./gradlew detekt — clean
  • ./gradlew apiCheck — clean

Generated with Claude Code

…ecedence

Add a withParentheses parameter (default true) to union(), unionAll(),
intersect(), and except() extension functions in both JDBC and R2DBC
modules. When true, wraps any preceding set operation in parentheses so
the DSL chain order matches the SQL evaluation order.

Also fixes Except.copy() which incorrectly returned Intersect.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant