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
GroupBy with outer reference not translating properly, causing data loss #27102
Some query logic is optimized away in the following query. No exception is thrown, the query executes successfully, returns the correct shape but bad data. This is a regression, it used to work in EF Core 5.
Possibly related to #27094.
classProgram{publicclassTable{publicintId{get;set;}publicint?Value{get;set;}}publicclassTestDb:DbContext{publicDbSet<Table>Table{get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder){optionsBuilder.UseSqlServer("data source=dummy");}}staticvoidMain(){usingvardb=newTestDb();varq=fromtindb.Table
group t.Id by t.Value into tgselectnew{A=tg.Key,B=db.Table.Where(t =>t.Value==tg.Max()*6).Max(t =>(int?)t.Id),};varsql=q.ToQueryString();}}
Expected translation (approx):
SELECTm0.ValueAS A, (
SELECTMAX(m.Id)
FROM Table AS m
WHEREm.Value== (MAX(m0.Id) *6)) AS B
FROM Table AS m0
GROUP BYm0.Value
Actual translation:
SELECTm.ValueAS A, MAX(m.Id) AS B
FROM Table AS m
GROUP BYm.Value
provider and version information
EF Core version: 6.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer 6.0.1
Target framework: .NET 6.0
Operating system: Windows 10 21H1
IDE: Visual Studio 2022 17.0.4
SELECT [t].[Value] AS [A], (
SELECTMAX([t0].[Id])
FROM [Table] AS [t0]
WHERE ([t0].[Value] = ((
SELECTMAX([t1].[Id])
FROM [Table] AS [t1]
WHERE ([t].[Value] = [t1].[Value]) OR ([t].[Value] IS NULLAND [t1].[Value] IS NULL)) *6)) OR ([t0].[Value] IS NULLAND (
SELECTMAX([t1].[Id])
FROM [Table] AS [t1]
WHERE ([t].[Value] = [t1].[Value]) OR ([t].[Value] IS NULLAND [t1].[Value] IS NULL)) IS NULL)) AS [B]
FROM [Table] AS [t]
GROUP BY [t].[Value]
Is the query complexity due to UseRelationalNulls(false)?
This may have some perf issues since the base query is repeated in the subquery.
Edit/clarification:
by perf issue I didn't mean the extra code to compensate for c# null semantics but that the subquery is hitting the table more times than necessary. With db null semantics I'd expect this would be the translation:
SELECT [t].[Value] AS [A], (
SELECTMAX([t0].[Id])
FROM [Table] AS [t0]
WHERE ([t0].[Value] = (
SELECTMAX([t1].[Id])
FROM [Table] AS [t1]
WHERE [t].[Value] = [t1].[Value]) *6)) AS [B]
FROM [Table] AS [t]
GROUP BY [t].[Value]
Activity
Query: Lift GroupByAggregate when correlation predicate matches
smitpatel commentedon Jan 5, 2022
Generated SQL after fix
bachratyg commentedon Jan 5, 2022
Is the query complexity due to
UseRelationalNulls(false)?This may have some perf issues since the base query is repeated in the subquery.
Edit/clarification:
by perf issue I didn't mean the extra code to compensate for c# null semantics but that the subquery is hitting the table more times than necessary. With db null semantics I'd expect this would be the translation:
smitpatel commentedon Jan 5, 2022
Valueand the result of max both can be null hence we need additional comparison to match results to C#.Query: Lift GroupByAggregate when correlation predicate matches
Query: Lift GroupByAggregate when correlation predicate matches (#27108)