Repository navigation
Parenthesize the item of ANY/ALL when its precedence requires it - #4130
Conversation
roji
left a comment
There was a problem hiding this comment.
Recommendation: Proceed with this PR. The bug is reproducible, the fix is appropriately scoped, and I found no blocking issues.
Problem
#4122 describes a real SQL-generation bug, not just unnecessary parentheses:
bool[] availability = [false];
var query = db.Products.Where(p => availability.Contains(p.IsListed && p.InStock));With four products covering every combination of the two Boolean properties, the pre-change generator emits IsListed AND InStock = ANY (@availability). That returns only the listed, out-of-stock product instead of all three products for which the conjunction is false. Comparison-valued items can instead fail with PostgreSQL error 42601.
Expected approach
Use the existing precedence-aware RequiresParentheses logic when emitting the item, producing (IsListed AND InStock) = ANY (@availability). This should be a small provider SQL-generation change, with result and SQL assertions in existing query suites. It needs no public API, persisted-format, dependency, or conceptual-documentation changes.
Alignment
The implementation follows that approach. One small private helper keeps ANY/ALL item emission consistent without introducing a new abstraction or changing precedence rules. The array and list regressions assert both results and exact SQL using the existing shared model. The change is necessary and minimal.
Gaps
No correctness or scope changes are required. The committed regressions exercise binary-expression grouping; manual checks also confirmed that the same fix handles unary NOT and nested Contains. In particular, flags.Contains(!p.IsListed) was wrong before the fix when flags was empty or contained both Boolean values, and is correct afterward.
Manual validation
I compiled a separate EF consumer against both the exact base commit and PR head and executed it against isolated PostgreSQL 18 Testcontainers databases, comparing results with LINQ-to-Objects. Across 13 scenarios, the base had six result mismatches and two syntax errors; the PR had none. These covered array/list AND, OR, NOT with singleton/mixed/empty arrays, comparisons, nested Contains with both null-semantics settings, and unchanged simple-item/LIKE ALL/ILIKE ANY behavior. This was direct API/database validation, not just static inspection or unit tests. Separately, both affected array-query test classes passed: 114 tests, no failures or skips.
Posted by copilot
roji
left a comment
There was a problem hiding this comment.
Thanks, LGTM! Will backport to 10.
Fixes #4122
VisitArrayAnyandVisitArrayAllwrote the item without checking whether it needs parentheses. With anAND/ORitem,array.Contains(e.A && e.B)becamee."A" AND e."B" = ANY (@array), which PostgreSQL reads ase."A" AND (e."B" = ANY (@array)).Both now go through
RequiresParentheses, likeVisitArrayIndexand the other PG expressions.PgAnyExpressionandPgAllExpressionalready have a precedence (800) inTryGetOperatorInfo, so only items that bind less tightly get parentheses and existing SQL baselines stay the same.Added
Array_param_Contains_logical_expressiontoArrayArrayQueryTestandArrayListQueryTest. On main it fails with42601: syntax error at or near "=", because its item ends in a comparison.I ran the whole suite against PostgreSQL 18 with PostGIS on Linux. Three tests fail there with and without this change:
View_column_collection_Contains_with_GIN_index_uses_containment(line endings) and bothCompare_timestamp_column_to_local_DateTime_parametercases.