Skip to content

Visit each IN value once when compensating for nullable values - #39414

Merged
AndriySvyryd merged 3 commits into
dotnet:mainfrom
benedict-odonovan:fix/39413-in-values-null-compensation
Oct 30, 2026
Merged

AndriySvyryd merged 3 commits into
dotnet:mainfrom
benedict-odonovan:fix/39413-in-values-null-compensation

Conversation

@benedict-odonovan

@benedict-odonovan benedict-odonovan commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Visit each IN value once when compensating for nullable values
Fixes #39413

  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

Summary

When an inline collection's Contains becomes an IN and one of its values is nullable, SqlNullabilityProcessor.VisitIn takes that value out of the IN and compares it to the item separately. A value with comparisons of its own, such as a correlated subquery, came back with its null compensation repeated:

// CustomerId is a nullable string.
context.Orders.Select(o => new
{
    o.Id,
    HasTwo = new[] { context.Orders.Count(o2 => o2.CustomerId == o.CustomerId), 0 }.Contains(2)
});
-- before: in both copies of the subquery
WHERE [o0].[CustomerId] = [o].[CustomerId] OR ([o0].[CustomerId] IS NULL AND [o].[CustomerId] IS NULL) OR ([o0].[CustomerId] IS NULL AND [o].[CustomerId] IS NULL) OR ([o0].[CustomerId] IS NULL AND [o].[CustomerId] IS NULL)

-- after, and what `subquery == 2` already produced without an inline collection
WHERE [o0].[CustomerId] = [o].[CustomerId] OR ([o0].[CustomerId] IS NULL AND [o].[CustomerId] IS NULL)

The results were correct, because the repeated disjuncts are redundant. Only the SQL was longer.

Cause.

  • VisitIn visited each value up to three times. ProcessInExpressionValues visited the values to find the nullable ones. When there were any, it ran again over the already-visited values to remove them from the IN. Then each removed value was visited a third time, as part of the item = value equality built from it. The single-value path ([var singleValue]) built its equality from the visited value too, so it visited it twice.
  • Null compensation isn't idempotent. Visiting a = b OR (a IS NULL AND b IS NULL) again expands a = b again, so each extra visit added another OR (... IS NULL AND ... IS NULL). Plain columns and parameters come out the same however often they're visited, which is why this only shows for compound values.
  • It only happens where the IN needs full compensation, e.g. in a projection. In a WHERE with a non-nullable item, VisitIn returns after the first visit.
  • Visiting the equality also visited the item again. When the item is itself a Contains over an inline collection, each level of nesting doubled the work for the levels below it.

The second visit came in with #31046, so release/10.0 and release/11.0 behave the same. It isn't a regression.

Change

  • The nullable values found in the first visit are removed from the IN by reference, without visiting them again.
  • The item = value equalities built for the nullable values, and for the single-value case, are no longer visited. They're built from the already-visited item and values and passed to a new ProcessComparison helper with the nullability already computed for each. ProcessComparison is the equality handling pulled out of VisitSqlBinary (OptimizeComparison, then RewriteNullSemantics), and VisitSqlBinary now calls it too. In optimized mode, item = TRUE still collapses to item, as it did when the equality was visited.
  • In the single-value case with a null in the values (item IS NULL OR item = value), the equality is processed with the item as non-nullable, because it only matters when the item isn't null.
  • With that, ProcessInExpressionValues no longer removes nullable values itself, so its removeNullables parameter is gone.

The parameter-collection and relational-null-semantics paths are unchanged.

Tests

New tests in NorthwindAggregateOperatorsQueryTestBase, with SQL Server baselines:

  • Contains_on_inline_collection_with_nullable_subquery_value: a correlated subquery as one of several values. Before the fix, its predicate had three IS NULL pairs in each copy of the subquery.
  • Contains_on_inline_collection_with_single_nullable_subquery_value_and_null: a correlated subquery as the only non-null value, which goes through the single-value path. Before the fix, it had two pairs.
  • Contains_on_nested_inline_collections_with_null and ..._in_projection: three levels of new bool?[] { true, null }.Contains(...). Both collapse to [o].[EmployeeID] = 5 AND [o].[EmployeeID] IS NOT NULL. On main, the projection variant repeated AND [o].[EmployeeID] IS NOT NULL.

Since the spec tests compare against LINQ-to-Objects, they also check that the results are unchanged.

On Cosmos, the two correlated-subquery tests assert translation failure. The nested tests are skipped, because the Cosmos SQL generator doesn't parenthesize nested IN (c["EmployeeID"] = 5 IN (...) IN (...)), which Cosmos rejects. That's a separate bug.

Ran the query test classes that exercise SqlNullabilityProcessor most heavily locally against LocalDB, with no other baseline changes: NullSemanticsQuery, NorthwindAggregateOperatorsQuery, PrimitiveCollectionsQuery, NorthwindWhereQuery, NorthwindMiscellaneousQuery, GearsOfWarQuery and ComplexNavigationsQuery.

Provider Tests
SQL Server 4,249, none failed
SQLite 4,205, none failed
Cosmos (NorthwindAggregateOperatorsQuery, emulator) 476, none failed

- VisitIn visited the values of an inline IN once to find the nullable ones, again to take them out of the IN, and a third time
  when visiting the equality it builds for each of them from the already-visited value. Null compensation isn't idempotent -
  visiting a = b OR (a IS NULL AND b IS NULL) again expands a = b again - so a nullable value with comparisons of its own, such as
  a correlated subquery, got its compensation repeated: three times with several values, twice with a single one.
- The nullable values are now taken out of the IN by reference, and the equalities are built from the item and values as they
  were before being visited, so each is visited once. ProcessInExpressionValues no longer needs to remove nullables itself.
- Add tests for a correlated subquery as one of several values, and as the only non-null value, of an inline collection.

Fixes dotnet#39413
@benedict-odonovan
benedict-odonovan requested a review from a team as a code owner October 26, 2026 12:09
Copilot AI balanced review requested due to automatic review settings October 26, 2026 12:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Missing Cosmos test overrides break the existing override guard, and revisiting original items introduces exponential traversal for nested Contains expressions.

2 open issues

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 29, 2026 12:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

…hem again

Visiting each item = value equality walked the already-visited item again, so
nested Contains over inline collections doubled the work at every level. The
equality handling is pulled out of VisitSqlBinary into ProcessComparison, which
VisitIn now calls with the nullability it already computed.
Copilot AI balanced review requested due to automatic review settings October 29, 2026 12:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the changes to inline-collection Contains null compensation.

No open issues 🎉

Review effort: Balanced

@AndriySvyryd AndriySvyryd 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 for your contribution!

@AndriySvyryd
AndriySvyryd merged commit 7aa24aa into dotnet:main Oct 30, 2026
18 of 19 checks passed
@benedict-odonovan
benedict-odonovan deleted the fix/39413-in-values-null-compensation branch October 30, 2026 09:10
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 31, 2026
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.

Null compensation is repeated for a nullable subquery used as a value of an inline collection Contains

3 participants