efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Query: Entity Framework core 1 Group By after Union throw “must be reducible node” Exception

Open MohammadAkbari opened this issue 10 years ago • 8 comments

I need unoin two query and after union Group By that, but throw "must be reducible node" Exception. in following sample code are:

public class ForumPost
{
    public int ForumPostId { get; set; }
    public string Message { get; set; }
    public bool Deleted { get; set; }
    public int ForumTopicId { get; set; }
    public virtual ForumTopic ForumTopic { get; set; }
}

var query1 = _dbContext.ForumPosts
    .Where(e => e.Deleted == true);

var query2 = _dbContext.ForumPosts
    .Where(e => e.Deleted == false);

var query =  query1.Union(query2);

var list = query.GroupBy(e => e.ForumTopicId)
    .Select(group => new
    {
        group.Key,
        Total = group.Count()
    }).Take(10)
    .ToList();

Does anyone faced with this problem?

MohammadAkbari avatar Oct 01 '16 17:10 MohammadAkbari

Is there an ETA on this? I have the same exception, and I have to call ToArray() before filtering, which obviously is bad for performances.

adrien-constant avatar Nov 01 '16 15:11 adrien-constant

It's on our list to investigate for the next release (1.2.0). Once we understand the work required, we'll have a better idea of where this fits.

rowanmiller avatar Nov 01 '16 17:11 rowanmiller

This still reproduces on current bits. We generate the following query plan:

(QueryContext queryContext) => IEnumerable<<>f__AnonymousType0<int, int>> _InterceptExceptions(
    source: IEnumerable<<>f__AnonymousType0<int, int>> _Select(
        source: IEnumerable<IGrouping<int, ForumPost>> Take(
            source: IEnumerable<IGrouping<int, ForumPost>> _GroupBy(
                source: IEnumerable<ForumPost> Union(
                    first: IEnumerable<ForumPost> _ShapedQuery(
                        queryContext: queryContext, 
                        shaperCommandContext: SelectExpression: 
                            SELECT [e1].[ForumPostId], [e1].[Deleted], [e1].[ForumTopicId], [e1].[Message]
                            FROM [ForumPosts] AS [e1]
                            WHERE [e1].[Deleted] = 1, 
                        shaper: BufferedEntityShaper<ForumPost>), 
                    second: IEnumerable<ForumPost> _ShapedQuery(
                        queryContext: queryContext, 
                        shaperCommandContext: SelectExpression: 
                            SELECT [e2].[ForumPostId], [e2].[Deleted], [e2].[ForumTopicId], [e2].[Message]
                            FROM [ForumPosts] AS [e2]
                            WHERE [e2].[Deleted] = 0, 
                        shaper: BufferedEntityShaper<ForumPost>)), 
                keySelector: (ForumPost e) =>  ---> [e] <--- .ForumTopicId, 
                elementSelector: (ForumPost e) =>  ---> [e] <--- ), 
            count: int GetParameterValue(
                queryContext: queryContext, 
                parameterName: "__p_0")), 
        selector: (IGrouping<int, ForumPost> group) => new <>f__AnonymousType0<int, int>(
            group.Key, 
            int Count(group)
        )), 
    contextType: Repro6658.MyContext, 
    logger: SensitiveDataLogger<IQueryCompilationContextFactory>, 
    queryContext: queryContext)

maumar avatar Apr 12 '17 21:04 maumar

Which node is non-reducible?

smitpatel avatar Apr 12 '17 22:04 smitpatel

key selector and element selector on the GroupBy, updated the query plan with debug version instead

maumar avatar Apr 12 '17 22:04 maumar

So is this problem solved?

qqliuyifanqq avatar Jan 06 '19 12:01 qqliuyifanqq

Re-opening to add more coverage for set operations and group by interactions.

smitpatel avatar Jul 02 '19 23:07 smitpatel

Filed #17607 for further test coverage.

smitpatel avatar Sep 04 '19 06:09 smitpatel