Fix doubled parentheses in Exists/Any/All with query operands - #743
Open
evanmarshall wants to merge 1 commit into
Open
Fix doubled parentheses in Exists/Any/All with query operands#743evanmarshall wants to merge 1 commit into
evanmarshall wants to merge 1 commit into
Conversation
Exists, Any and All wrapped their operand in a group unconditionally, but a bob.Query already writes its own surrounding parentheses when rendered as an expression (see bob.BaseQuery.WriteSQL). The two stacked into invalid SQL like EXISTS ((SELECT ...)), which fails on SQLite and broke the generated relationship Where filters from stephenafamo#722. Skip the extra group when the operand is a bob.Query. Non-query operands (Arg, Raw, etc.) keep the group, since EXISTS/ANY/ALL require parentheses around their operand. Fixes stephenafamo#742 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
stephenafamo
requested changes
Jul 23, 2026
Comment on lines
+70
to
+80
| // A [bob.Query] writes its own surrounding parentheses when rendered as an | ||
| // expression (see [bob.BaseQuery.WriteSQL]), so wrapping it in a group would | ||
| // produce doubled parentheses such as EXISTS ((SELECT ...)). | ||
| // Any other expression is wrapped in a group since EXISTS/ANY/ALL require | ||
| // parentheses around their operand. | ||
| func subquery(exp bob.Expression) bob.Expression { | ||
| if _, ok := exp.(bob.Query); ok { | ||
| return exp | ||
| } | ||
|
|
||
| return group{exp} |
Owner
There was a problem hiding this comment.
This function name seems wrong... perhaps it should be named something likegroupIfNotQuery or something clearer
This was referenced Aug 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #742
The bug
Exists,AnyandAllinexpr/builder.gowrapped their operand ingroup{}unconditionally. But abob.Queryoperand (such as thebob.BaseQueryreturned bypsql.Select/sqlite.Select/mysql.Select) already writes its own surrounding parentheses when rendered as an expression (bob.BaseQuery.WriteSQL). The two stacked into invalid SQL likeEXISTS ((SELECT ...)), which fails to execute on SQLite and broke the generated relationshipWherefilters from #722 (Parents.R.HasEndpoints(...)etc., which returndialect.Exists(q)).The fix
Exists/Any/Allnow skip the extragroup{}when the operand implementsbob.Query, relying on the query to write its own parentheses — the same assumptionbob.BaseQuery.WriteSQLalready makes for nested queries.I deliberately did not reuse the
ShouldOmitParensguard fromX()as the issue first suggested:Arg,Raw,Clauseetc. reportShouldOmitParens() == truebut do not write their own parentheses, andEXISTS/ANY/ALLrequire parentheses around their operand — so routing through that check would have turnedANY ($1)into invalidANY $1. Non-query operands keep the group exactly as before.Also updated the doc comments in the three dialects'
starters.go, which documented the doubled form (EXISTS ((SELECT 1))).Tests
expr/builder_test.go: exact-string assertions that a query operand renders asEXISTS (SELECT ...)/ANY (...)/ALL (...)with a single set of parentheses, and that non-query operands (Raw,Arg) still get wrapped. Exact matching matters here becausetestutils.Cleancollapses doubled parentheses, which would mask this regression.dialect/sqlite/select_test.go: an end-to-endWHERE EXISTS (subquery)case validated by the SQLite grammar parser.go test ./...passes except the pre-existingTestLibSQLcontainer failure ingen/bobgen-sqlite/driver, which fails identically on a clean checkout ofmain(Docker/testcontainers environment issue, unrelated).🤖 Generated with Claude Code