efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Multiple left joins cause client side eval

Open EricABC opened this issue 10 years ago • 14 comments

Steps to reproduce

Alpha1-22262, should be after several left-join related fixes, such as #4588. I was going to wait to post since this is on the edge of whats being worked on, but I couldn't find anything tracking this. Removing the comments causes the query to fail. With the comments, a single query is correctly executed. I encountered the problem through a large variety of queries, but the one common factor seems to be more than one left join, which this simplified example shows.

                    var q3 = from i in items
                             join innerD0 in values on i.Id equals innerD0.SparseItemId into d0g
                             from d0pv in d0g.DefaultIfEmpty()
                             //join innerD1 in values on i.Id equals innerD1.SparseItemId into d1g
                             //from d1pv in d1g.DefaultIfEmpty()
                             select new { SparseItemId = i.Id };

Queries executed:

Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory:Information: Executed DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [i].[Id], [innerD0].[SparseItemId], [innerD0].[PropertyId], [innerD0].[Data], [innerD0].[DateTimeOffsetValue], [innerD0].[DecimalValue], [innerD0].[DoubleValue], [innerD0].[StringValue]
FROM [SparseItem] AS [i]
LEFT JOIN [PropertyValue] AS [innerD0] ON [i].[Id] = [innerD0].[SparseItemId]
ORDER BY [i].[Id]
Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory:Information: Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [innerD1].[SparseItemId], [innerD1].[PropertyId], [innerD1].[Data], [innerD1].[DateTimeOffsetValue], [innerD1].[DecimalValue], [innerD1].[DoubleValue], [innerD1].[StringValue]
FROM [PropertyValue] AS [innerD1]

The query shaper pulls the second join source from a new query, causing the entire table to be selected.

EricABC avatar Sep 29 '16 13:09 EricABC

@anpete should this be fixed by the fix for #4588?

divega avatar Sep 29 '16 19:09 divega

@divega @anpete The alpha I pulled was only 4 hrs old when i tried it again this morning from ci feed, so I think that was already in it?

EricABC avatar Sep 29 '16 21:09 EricABC

@EricABC Thanks for reporting. When you say "fail" do you mean that an exception is raised, or do you just mean that it client evals?

BTW should //from d1pv in d0g.DefaultIfEmpty() be //from d1pv in d1g.DefaultIfEmpty() ?

anpete avatar Sep 29 '16 22:09 anpete

Sorry, I just mean the client eval.

EricABC avatar Sep 29 '16 23:09 EricABC

I heard from @smitpatel that the fix hasn't made it to nightly builds yet.

divega avatar Sep 30 '16 04:09 divega

Just to be sure, I pulled dev and verified the issue still occurs using dev branch locally.

EricABC avatar Sep 30 '16 13:09 EricABC

Also if it helps, the first group join will get to here. If you comment out the second one, it gets inside. Otherwise the value will be 2 and it will proceed to generate a new RelationalQueryModelVisitor and go client-side. Hacking on it to force it to process the first left join shows that several things get out of alignment when there is a tree of left joins. It's either not getting back to the original query source joined on, or its not able to perceive the previous source as the new effective flattened query after the first join.

EricABC avatar Sep 30 '16 15:09 EricABC

@EricABC That's why I was asking the question above. It looks like a typo in the query that d0g is referenced twice.

anpete avatar Sep 30 '16 16:09 anpete

Woops, sorry for the typo (edited now), but I verified the issue persists.

EricABC avatar Sep 30 '16 16:09 EricABC

OK, thanks for checking.

anpete avatar Sep 30 '16 19:09 anpete

One other note, it might be nice to have QueryClientEvaluationWarning fire when we end up resolving another main from-clause in a separate visitor, or perhaps add a RelationalEventId.MultipleQueriesExecutedWarning? Seems like this would be the spot where that would be considered.

EricABC avatar Sep 30 '16 21:09 EricABC

@anpete we wanted to get your thoughts on putting this in 1.1.0-preview1. If that's not feasible, then we should punt to 1.2.0.

rowanmiller avatar Oct 03 '16 18:10 rowanmiller

Confirmed and trying to get this into preview1

anpete avatar Oct 06 '16 00:10 anpete

@anpete It seems like this is fixed in 1.1, if we are using LTS release, does it have any workaround to have multiple left outer join in single query?

rohits-dev avatar Jan 24 '17 15:01 rohits-dev