Repository navigation
Don't apply optimized null expansion to the projection of an IN subquery - #39232
Merged
AndriySvyryd merged 1 commit intoOct 14, 2026
Merged
AndriySvyryd merged 1 commit into
AndriySvyryd merged 1 commit into
Conversation
SqlNullabilityProcessor.VisitIn visited the subquery projection with the
allowOptimizedExpansion of the enclosing IN. Optimized expansion assumes a
NULL result can be treated as false, which only holds for the predicate
itself; the projection is a value compared against the item. A nullable
comparison projected out of the subquery was therefore left as-is, so e.g.
ss.Set<B>().Where(x => x.Id == e.Id).Select(x => x.NullableInt > 1).Contains(false)
was translated on SQLite to `0 IN (SELECT "x"."NullableInt" > 1 ...)`, which
yields NULL instead of true when NullableInt is NULL, silently filtering out
rows (18 instead of 27 in the test). SQL Server happened to be unaffected
because the bool projection is wrapped in a CASE for bit conversion.
Always visit the projection without optimized expansion.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The focused fix preserves projected null semantics and has regression coverage for both affected query shapes.
0 open findings
What changed in this PR
This PR fixes null handling in relational IN subqueries so a nullable comparison projected by the subquery retains its value semantics.
Changes:
- Disable optimized null expansion when visiting the subquery projection.
- Add regression coverage for constant and column
Containsvalues, with SQLite and SQL Server SQL assertions.
| File | Description |
|---|---|
test/EFCore.SqlServer.FunctionalTests/Query/NullSemanticsQuerySqlServerTest.cs |
Adds SQL Server SQL assertions. |
test/EFCore.Sqlite.FunctionalTests/Query/NullSemanticsQuerySqliteTest.cs |
Adds SQLite SQL assertions for the corrected projection. |
test/EFCore.Relational.Specification.Tests/Query/NullSemanticsQueryTestBase.cs |
Adds shared regression queries. |
src/EFCore.Relational/Query/SqlNullabilityProcessor.cs |
Visits subquery projections without optimized null expansion. |
🧠 Review effort: Lite
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
AndriySvyryd
approved these changes
Oct 14, 2026
Member
|
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.
SqlNullabilityProcessor.VisitIn visited the subquery projection with the allowOptimizedExpansion of the enclosing IN. Optimized expansion assumes a NULL result can be treated as false, which only holds for the predicate itself; the projection is a value compared against the item. A nullable comparison projected out of the subquery was therefore left as-is, so e.g.
was translated on SQLite to
0 IN (SELECT "x"."NullableInt" > 1 ...), which yields NULL instead of true when NullableInt is NULL, silently filtering out rows (18 instead of 27 in the test). SQL Server happened to be unaffected because the bool projection is wrapped in a CASE for bit conversion.Always visit the projection without optimized expansion.