NHibernate.HibernateException: Query Source could not be identified, when using join group and Sum()

Description

This bug is very similar to the bug (https://nhibernate.jira.com/browse/NH-3001)

A query with join and group by fails when using the Sum() function (the other linked issue had trouble with Count(). When replacing Sum() with Count() in my case, my query works, with Nhibernate 4.0.0.4000.

My query is along the lines of:

And it throws the following exception:
[HibernateException: Query Source could not be identified: ItemName = <generated>_1, ItemType = System.Int32, Expression = from Int32 <generated>_1 in [u]]
NHibernate.Linq.QuerySourceNamer.GetName(IQuerySource querySource) +172
NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitQuerySourceReferenceExpression(QuerySourceReferenceExpression expression) +94
NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression) +1089
NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitNhSum(NhSumExpression expression) +91
NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression) +1498
NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.Visit(Expression expression) +41
NHibernate.Linq.Visitors.SelectClauseVisitor.VisitExpression(Expression expression) +118
Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitAndConvert(T expression, String methodName) +119
Remotion.Linq.Parsing.<>c_DisplayClass6`1.<VisitAndConvert>b_5(T expression) +120
Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitList(ReadOnlyCollection`1 list, Func`2 visitMethod) +231
Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitAndConvert(ReadOnlyCollection`1 expressions, String callerName) +335
Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitNewExpression(NewExpression expression) +126
Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression) +828
NHibernate.Linq.Visitors.SelectClauseVisitor.VisitExpression(Expression expression) +269
NHibernate.Linq.Visitors.SelectClauseVisitor.Visit(Expression expression) +319
NHibernate.Linq.Visitors.QueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel) +183
Remotion.Linq.Clauses.SelectClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel) +94
Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel) +163
NHibernate.Linq.Visitors.QueryModelVisitor.Visit() +62
NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel queryModel, VisitorParameters parameters, Boolean root) +335
NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor sessionFactory, Boolean filter) +202
NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory) +59
NHibernate.Engine.Query.QueryExpressionPlan.CreateTranslators(IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory) +88
NHibernate.Engine.Query.QueryExpressionPlan..ctor(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory) +76
NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters) +296
NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow) +179
NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression) +154
NHibernate.Linq.DefaultQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery) +146
NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression) +64
NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression) +50
Remotion.Linq.QueryableBase`1.GetEnumerator() +128
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +369
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
I'm hoping for a quick fix so it can become part of 4.0 GA

Environment

Windows 8.1, visual studio 2013, .net 4.5

Attachments

1

Activity

Alex Zaytsev 
May 1, 2017 at 3:42 AM

Closing issues resolved in 4.1.0

Duncan Munro 
June 17, 2015 at 7:10 PM

It looks like the GroupBySelectClauseRewriter is not correctly identifying and returning the group element selector.
The key selector gets some in depth comparison, but the element selector is just directly compared so it misses the nested query source reference and doesn't get unwrapped.

I discovered this while investigating

Daniel Laberge 
March 6, 2015 at 5:28 PM
(edited)

I'm also plagued by this issue. I've submitted this pull-request with a simplified test case for this particular issue.

For reference, here is my simplified query, based on Northwind :

And the SQL generated by LinqToSQL with LINQPad :

Does anyone have a clue on how to fix this? This is making writing group-by queries very painful in our project.

Thank you all for your efforts.

Bruno Alexandre 
September 2, 2014 at 7:36 AM

and by the way, the SQL getting generated with LinqPad (using EF) is:

Nicklas Overgaard  
September 2, 2014 at 7:33 AM

As my colleague Bruno is pointing out, we're currently porting from Entity Framework to Nhibernate in an early stage project. All our queries in LINQ was working with EF, so I'm taking a lazy approach by trying to change as little as possible in each query.

The query group "g" (based off r.TotalPoints) is already a numeric value. And as I pointed out COUNT() works, but all other aggregate functions fail. Are these aggregate functions not supposed to have the exact same semantics? I.e if i can do Count() without a lambda, I can use Sum() without a lambda.

The SQL that's being generated by Nhibernate with Count() is like this:

Output sql

So all I want now is to be able to select the Sum() rather than the Count().

Fixed

Details

Assignee

Reporter

Labels

Components

Fix versions

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created September 1, 2014 at 10:23 PM
Updated May 1, 2017 at 3:42 AM
Resolved June 23, 2015 at 10:01 AM
Who's Looking?