Skip to content

Don't apply optimized null expansion to the projection of an IN subquery - #39232

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/null-semantics-in-subquery-projection
Oct 14, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
ilkertskn:fix/null-semantics-in-subquery-projection

Conversation

@ilkertskn

Copy link
Copy Markdown
Contributor

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.

  • [ x] I've read the guidelines for contributing and seen the walkthrough
  • [x ] 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
  • [x ] The code builds and tests pass locally (also verified by our automated build checks)
  • [x ] Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • [x ] Tests for the changes have been added (for bug fixes / features)
  • [ x] Code follows the same patterns and style as existing code in this repo

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.

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

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 Contains values, 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

Copy link
Copy Markdown
Member

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit 8ef66e9 into dotnet:main Oct 14, 2026
20 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 19, 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.

3 participants