Repository navigation
Count NULL as a value in Distinct().Count() over a grouping - #39766
Open
seragAhmed wants to merge 2 commits into
Open
seragAhmed wants to merge 2 commits into
seragAhmed wants to merge 2 commits into
Conversation
Contributor
Author
|
The Cosmos job's failures are |
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Distinct Count/LongCount translation can emit invalid SQL Server SQL for coalesced selectors and return incorrect counts by evaluating volatile selectors twice.
2 open issues
What changed in this PR
This PR addresses #39759 by updating EF Core’s relational translation of grouped Distinct().Count() and LongCount() to count null as one distinct value, matching LINQ semantics.
Changes:
- Adds a null-count compensation term through provider-specific aggregate translators, preserving SQL Server’s
COUNT_BIG. - Applies aggregate predicates to the compensation term.
- Adds shared regression tests and SQL Server/SQLite baselines for nullable selectors and filters.
| File | Description |
|---|---|
| test/EFCore.SqlServer.FunctionalTests/Query/NorthwindGroupByQuerySqlServerTest.cs | Adds regression baselines and updates existing nullable distinct-count SQL. |
| test/EFCore.Sqlite.FunctionalTests/Query/NorthwindGroupByQuerySqliteTest.cs | Adds SQLite baselines for nullable distinct counts and predicates. |
| test/EFCore.Specification.Tests/Query/NorthwindGroupByQueryTestBase.cs | Adds shared Count/LongCount tests using nullable customer regions. |
| src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.cs | Adds nullability checks and compensation for grouped distinct counts. |
Review effort: Balanced
COUNT(DISTINCT x) skips NULL, while LINQ's Distinct().Count() and Distinct().LongCount() count null as one more distinct value. - When the counted operand can be NULL, add a distinct count of CASE WHEN x IS NULL THEN 1 END (0 or 1, never NULL) through the provider's own count translation, so SQL Server keeps COUNT_BIG. - A predicate on the count also guards the NULL term; a predicate that only excludes NULL from the same column skips it. Fixes dotnet#39759
seragAhmed
force-pushed
the
fix/39759-distinct-count-null
branch
from
November 25, 2026 03:56
c1d3ede to
58f2d76
Compare
Null semantics can find the counted expression non-nullable after translation (a parameter, a null-checked predicate) and drop the marker's only WHEN, leaving COUNT(DISTINCT (NULL)), which SQL Server rejects. Give the CASE a typed NULL ELSE so it collapses to CAST(NULL AS int) instead, and treat COALESCE with a non-nullable argument and CASE with non-nullable results as non-nullable so those get no marker at all.
seragAhmed
force-pushed
the
fix/39759-distinct-count-null
branch
from
November 25, 2026 03:56
58f2d76 to
142b129
Compare
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.


Fixes #39759
Inside a GroupBy,
g.Select(x => x.Nullable).Distinct().Count()/LongCount()translated toCOUNT(DISTINCT x), which skips NULL, while LINQ counts null as one distinct value.When the counted operand can be NULL, the translation now adds a distinct count of
CASE WHEN x IS NULL THEN 1 END(0 or 1, never NULL), translated through the provider's own count translator so SQL Server keepsCOUNT_BIG. A predicate on the count is applied to that term as well; a predicate that only excludes NULL on the same column (Distinct().Count(x => x != null)) leaves the SQL unchanged, as do non-nullable operands.SQL Server:
Added GroupBy spec tests over Customer.Region (many NULLs in Northwind) with SQL Server and Sqlite baselines; three existing SQL Server baselines over nullable columns pick up the extra term. This may need a rebase on top of #39451, which reworks this area.