Skip to content

Parenthesize the item of ANY/ALL when its precedence requires it - #4130

Merged
roji merged 1 commit into
npgsql:mainfrom
efeesrefinal:fix/any-all-item-parentheses
Nov 17, 2026
Merged

roji merged 1 commit into
npgsql:mainfrom
efeesrefinal:fix/any-all-item-parentheses

Conversation

@efeesrefinal

Copy link
Copy Markdown
Contributor

Fixes #4122

VisitArrayAny and VisitArrayAll wrote the item without checking whether it needs parentheses. With an AND/OR item, array.Contains(e.A && e.B) became e."A" AND e."B" = ANY (@array), which PostgreSQL reads as e."A" AND (e."B" = ANY (@array)).

Both now go through RequiresParentheses, like VisitArrayIndex and the other PG expressions. PgAnyExpression and PgAllExpression already have a precedence (800) in TryGetOperatorInfo, so only items that bind less tightly get parentheses and existing SQL baselines stay the same.

Added Array_param_Contains_logical_expression to ArrayArrayQueryTest and ArrayListQueryTest. On main it fails with 42601: 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 both Compare_timestamp_column_to_local_DateTime_parameter cases.

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM! Will backport to 10.

@roji
roji enabled auto-merge (squash) November 17, 2026 13:39
@roji
roji merged commit 6135755 into npgsql:main Nov 17, 2026
10 checks passed
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.

= ANY with an AND/OR item omits parentheses, returning wrong results

2 participants