Repository navigation
Visit each IN value once when compensating for nullable values - #39414
Merged
AndriySvyryd merged 3 commits intoOct 30, 2026
Merged
AndriySvyryd merged 3 commits into
AndriySvyryd merged 3 commits into
Conversation
- 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
…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.
6 tasks done
AndriySvyryd
approved these changes
Oct 30, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
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.


Visit each IN value once when compensating for nullable values
Fixes #39413
Summary
When an inline collection's
Containsbecomes anINand one of its values is nullable,SqlNullabilityProcessor.VisitIntakes that value out of theINand 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:The results were correct, because the repeated disjuncts are redundant. Only the SQL was longer.
Cause.
VisitInvisited each value up to three times.ProcessInExpressionValuesvisited the values to find the nullable ones. When there were any, it ran again over the already-visited values to remove them from theIN. Then each removed value was visited a third time, as part of theitem = valueequality built from it. The single-value path ([var singleValue]) built its equality from the visited value too, so it visited it twice.a = b OR (a IS NULL AND b IS NULL)again expandsa = bagain, so each extra visit added anotherOR (... 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.INneeds full compensation, e.g. in a projection. In aWHEREwith a non-nullable item,VisitInreturns after the first visit.Containsover 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.0andrelease/11.0behave the same. It isn't a regression.Change
INby reference, without visiting them again.item = valueequalities 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 newProcessComparisonhelper with the nullability already computed for each.ProcessComparisonis the equality handling pulled out ofVisitSqlBinary(OptimizeComparison, thenRewriteNullSemantics), andVisitSqlBinarynow calls it too. In optimized mode,item = TRUEstill collapses toitem, as it did when the equality was visited.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.ProcessInExpressionValuesno longer removes nullable values itself, so itsremoveNullablesparameter 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 threeIS NULLpairs 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_nulland..._in_projection: three levels ofnew bool?[] { true, null }.Contains(...). Both collapse to[o].[EmployeeID] = 5 AND [o].[EmployeeID] IS NOT NULL. On main, the projection variant repeatedAND [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
SqlNullabilityProcessormost heavily locally against LocalDB, with no other baseline changes:NullSemanticsQuery,NorthwindAggregateOperatorsQuery,PrimitiveCollectionsQuery,NorthwindWhereQuery,NorthwindMiscellaneousQuery,GearsOfWarQueryandComplexNavigationsQuery.NorthwindAggregateOperatorsQuery, emulator)