You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
No items when projecting out groupings with a null key #32026
This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.
@NiJeTi I'll hopefully get around to investigating this soon (lots on my plate at the moment), no need to open another issue until we confirm what exactly it's about.
@roji Confirmed this still repros on 8.0. See code and output below. Looks to me like AS t1 ON t0."Key" = t1."Key" is wrong when the key is nullable because null won't match null.
info: 1/5/2024 11:02:58.594 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (4ms) [Parameters=[@__ToString_0='cb63d7d2-813f-4642-aa91-760a8f5bd1c3'], CommandType='Text', CommandTimeout='30']
SELECT t0."Key", t0.c, t1."Id", t1."Json"
FROM (
SELECT t."Key", count(*)::int AS c
FROM (
SELECT CAST(e."Json" ->> @__ToString_0 AS uuid) AS "Key"
FROM "Entity" AS e
) AS t
GROUP BY t."Key"
) AS t0
LEFT JOIN (
SELECT e0."Id", e0."Json", CAST(e0."Json" ->> @__ToString_0 AS uuid) AS "Key"
FROM "Entity" AS e0
) AS t1 ON t0."Key" = t1."Key"
ORDER BY t0."Key"
Key: 2347b859-c150-4ebf-b887-30d94f2f232f Count: 1
System.Text.Json.JsonDocument
Key: 4588a331-9cd7-40f5-b5c3-f2b25b22ce5c Count: 1
System.Text.Json.JsonDocument
Key: Count: 1
Sorry, you're probably right. I'll have to investigate to see if this is really an Npgsql issue or something being triggered on the EF side - assigning to myself for investigation.
SELECT [t].[Key], [t].[c], [e0].[Id], [e0].[Key]
FROM (
SELECT [e].[Key], COUNT(*) AS [c]
FROM [Entities] AS [e]
GROUP BY [e].[Key]
) AS [t]
LEFT JOIN [Entities] AS [e0] ON [t].[Key] = [e0].[Key]
ORDER BY [t].[Key]
Note that in order to get the items for each group, we're doing a LEFT JOIN back on the Entities table, with the Key being the join predicate. However, we're using simple equality, so when the Key is null, we get no results. In other words, we're missing null semantics on the join predicate in this case.
I'm not sure whether this is a GroupBy-specific bug - It's worth checking what happens with other joins that aren't produced via GroupBy; i.e. when the user expresses a join in LINQ referencing a nullable join key, do we consistently apply null semantics onto that?
@maumar Assigned this to you as a null compensation issue. Specifically, [t].[Key] = [e0].[Key] should have the additional logic to match null to null.
I looked into this, and it isn't specific to GroupBy. Any correlated collection whose Where compares two nullable values loses its null matches. It still reproduces on current main and on release/11.0 on SQL Server and SQLite.
// Each post with the ids of the posts in the same category. Category is a nullable string; posts 3 and 4 have none.varquery=context.Posts.Select(p =>new{p.Id,SameCategory=context.Posts.Where(q =>q.Category==p.Category).Select(q =>q.Id).ToList()});
SELECT [p].[Category], [p].[Id], [p0].[Id]
FROM [Posts] AS [p]
LEFT JOIN [Posts] AS [p0] ON [p].[Category] = [p0].[Category]
ORDER BY [p].[Id]
Id = 1, SameCategory = [1, 2]
Id = 2, SameCategory = [1, 2]
Id = 3, SameCategory = [] <- expected [3, 4]
Id = 4, SameCategory = [] <- expected [3, 4]
The same correlation inside an aggregate does treat null as equal to null, because it stays a subquery with a WHERE:
SELECT [p].[Id], (
SELECTCOUNT(*)
FROM [Posts] AS [p0]
WHERE [p0].[Category] = [p].[Category] OR ([p0].[Category] IS NULLAND [p].[Category] IS NULL)) AS [Count]
FROM [Posts] AS [p]
This returns Count = 2 for posts 3 and 4. So ToList() and Count() over the same Where disagree about which rows match.
Activity
roji commentedon Oct 11, 2023
This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.
NiJeTi commentedon Oct 13, 2023
Hello again! As you requested I've created a sample application. You can find it here -> https://github.com/NiJeTi/ef-json.
Described problem is still here despite I've used the most recent versions of .NET, EF Core libraries and PostgreSQL database.
ajcvickers commentedon Oct 15, 2023
@roji Looks like this is specific to PostgreSQL mapping of a
JsonDocumentproperty.NiJeTi commentedon Oct 17, 2023
Should I create a copy of this issue to respective PorstreSQL repository then?
roji commentedon Oct 18, 2023
@NiJeTi I'll hopefully get around to investigating this soon (lots on my plate at the moment), no need to open another issue until we confirm what exactly it's about.
ajcvickers commentedon Jan 5, 2024
@roji Confirmed this still repros on 8.0. See code and output below. Looks to me like
AS t1 ON t0."Key" = t1."Key"is wrong when the key is nullable because null won't match null.roji commentedon Jan 5, 2024
@ajcvickers could you see this failing with SQL Server too? It doesn't look like it should be PG-specific...
ajcvickers commentedon Jan 5, 2024
@roji I didn't try SQL Server because of the
JsonDocumentproperty mapping. You don't think that's relevant here?roji commentedon Jan 5, 2024
Sorry, you're probably right. I'll have to investigate to see if this is really an Npgsql issue or something being triggered on the EF side - assigning to myself for investigation.
roji commentedon Jan 10, 2024
Finally investigated this (sorry it took so long).
This isn't PG-specific or related to JsonDocument - here's the same thing failing on SQL Server with GroupBy over a simple nullable column:
The SQL this produces:
Note that in order to get the items for each group, we're doing a LEFT JOIN back on the Entities table, with the Key being the join predicate. However, we're using simple equality, so when the Key is null, we get no results. In other words, we're missing null semantics on the join predicate in this case.
I'm not sure whether this is a GroupBy-specific bug - It's worth checking what happens with other joins that aren't produced via GroupBy; i.e. when the user expresses a join in LINQ referencing a nullable join key, do we consistently apply null semantics onto that?
[-]Cannot retrieve grouped items[/-][+]No items when projecting out groupings with a null key[/+]2 remaining items
ajcvickers commentedon Jan 11, 2024
@maumar Assigned this to you as a null compensation issue. Specifically,
[t].[Key] = [e0].[Key]should have the additional logic to match null to null.benedict-odonovan commentedon Oct 22, 2026
I looked into this, and it isn't specific to
GroupBy. Any correlated collection whoseWherecompares two nullable values loses its null matches. It still reproduces on currentmainand onrelease/11.0on SQL Server and SQLite.The same correlation inside an aggregate does treat null as equal to null, because it stays a subquery with a
WHERE:This returns
Count = 2for posts 3 and 4. SoToList()andCount()over the sameWheredisagree about which rows match.Keep matching null keys when a correlated Where becomes a join (dotne…