nhibernate-core
nhibernate-core copied to clipboard
NH-3445 - Select, Distinct, OrderBy, Take fails with NotSupportedException
Mark Junker created an issue — :
A combination of Select, Distinct, OrderBy and Take fails with a NotSupportedException with the following stack trace:
bei NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.GetClassName(IASTNode querySource) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Hql\Ast\ANTLR\PolymorphicQuerySourceDetector.cs:Zeile 59. bei NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.Process(IASTNode tree) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Hql\Ast\ANTLR\PolymorphicQuerySourceDetector.cs:Zeile 27. bei NHibernate.Hql.Ast.ANTLR.AstPolymorphicProcessor.Process() in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Hql\Ast\ANTLR\AstPolymorphicProcessor.cs:Zeile 30. bei NHibernate.Hql.Ast.ANTLR.AstPolymorphicProcessor.Process(IASTNode ast, ISessionFactoryImplementor factory) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Hql\Ast\ANTLR\AstPolymorphicProcessor.cs:Zeile 24. bei NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode ast, String queryIdentifier, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Hql\Ast\ANTLR\ASTQueryTranslatorFactory.cs:Zeile 29. bei NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Hql\Ast\ANTLR\ASTQueryTranslatorFactory.cs:Zeile 24. bei NHibernate.Engine.Query.QueryExpressionPlan.CreateTranslators(IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Engine\Query\QueryExpressionPlan.cs:Zeile 25. bei NHibernate.Engine.Query.QueryExpressionPlan..ctor(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Engine\Query\QueryExpressionPlan.cs:Zeile 12. bei NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Engine\Query\QueryPlanCache.cs:Zeile 67. bei NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Impl\AbstractSessionImpl.cs:Zeile 412. bei NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Impl\AbstractSessionImpl.cs:Zeile 372. bei NHibernate.Linq.DefaultQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\DefaultQueryProvider.cs:Zeile 70. bei NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\DefaultQueryProvider.cs:Zeile 34. bei NHibernate.Linq.DefaultQueryProvider.Execute<TResult>(Expression expression) in c:\Users\Mark\Desktop\nhcore\src\NHibernate\Linq\DefaultQueryProvider.cs:Zeile 41. bei Remotion.Linq.QueryableBase`1.System.Collections.IEnumerable.GetEnumerator()I'll add a patch for a test case.
Mark Junker added a comment — :
Test case for master/head
Alexander Zaytsev added a comment — :
The problem in HQL
om2804 added a comment — :
I use Select().OrderBy().Distinct() instead Select().Distinct().OrderBy(). It solved my problem with throw NotSupportedException
Take() seems not to be the issue. I ran into the same NotSupportedException in 5.5.0 with a Select().Distinct().OrderBy() type query.
I can confirm this workaround:
I use Select().OrderBy().Distinct() instead Select().Distinct().OrderBy(). It solved my problem
FYI A bit more context from my scenario:
I had a query like this:
IOrderedQueryable<Customer> GetAllWithOrdersBefore(DateTime date)
=> nhSession.Query<Order>()
.Where(o => o.Id != 0 && o.Payment.Id != 0 && o.IsConfirmed)
.Where(o => o.Date <= date)
.Where(o => o.Customer != null)
.Select(o => o.Customer).Distinct().OrderBy(c => c.Name);
which yielded a NotSupportedException thrown by PolymorphicQuerySourceDetector.GetClassName() with the following message containing the HQL
query ( query ( select_from ( from ( range Order o ) ( join left ( . o Customer ) _0 ) ) ( select distinct _0 ) ) ( where ( and ( and ( and ( and ( ne ( . o Id ) ( : p1 ) ) ( ne ( . ( . o Payment ) Id ) ( : p2 ) ) ) ( . o IsConfirmed ) ) ( le ( . o Date ) ( : p3 ) ) ) ( is not null ( . o Customer ) ) ) ) )
The query works fine when using .OrderBy(c => c.Name).Distinct() instead of .Distinct().OrderBy(c => c.Name).