Skip to content

Avoid unnecessary join for entity comparisons in with clause #2078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/NHibernate.Test/Async/Hql/EntityJoinHqlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//------------------------------------------------------------------------------


using System.Text.RegularExpressions;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.Test.Hql.EntityJoinHqlTestEntities;
Expand Down Expand Up @@ -152,6 +153,67 @@ public async Task EntityJoinFoSubqueryAsync()
}
}

[Test]
public async Task EntityJoinWithEntityComparisonInWithClausShouldNotAddJoinAsync()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
EntityComplex entityComplex =
await (session
.CreateQuery(
"select ex "
+ "from EntityComplex ex "
+ "inner join EntityComplex st with st = ex.SameTypeChild "
).SetMaxResults(1)
.UniqueResultAsync<EntityComplex>());

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "EntityComplex").Count, Is.EqualTo(2));
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
}
}

[Test]
public async Task EntityJoinWithEntityAssociationComparisonShouldAddJoinAsync()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
EntityComplex entityComplex =
await (session
.CreateQuery(
"select ex "
+ "from EntityComplex ex "
+ "inner join EntityComplex st with st = ex.SameTypeChild.SameTypeChild "
).SetMaxResults(1)
.UniqueResultAsync<EntityComplex>());

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "EntityComplex").Count, Is.EqualTo(3));
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
}
}

[Test]
public async Task EntityJoinWithEntityAssociationComparison2ShouldAddJoinAsync()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
EntityComplex entityComplex =
await (session
.CreateQuery(
"select ex "
+ "from EntityComplex ex "
+ "inner join EntityComplex st with st.Version = ex.SameTypeChild.Version "
).SetMaxResults(1)
.UniqueResultAsync<EntityComplex>());

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "EntityComplex").Count, Is.EqualTo(3));
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
}
}


[Test, Ignore("Failing for unrelated reasons")]
public async Task CrossJoinAndWithClauseAsync()
{
Expand Down
64 changes: 63 additions & 1 deletion src/NHibernate.Test/Hql/EntityJoinHqlTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NHibernate.Cfg.MappingSchema;
using System.Text.RegularExpressions;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NHibernate.Test.Hql.EntityJoinHqlTestEntities;
using NUnit.Framework;
Expand Down Expand Up @@ -141,6 +142,67 @@ public void EntityJoinFoSubquery()
}
}

[Test]
public void EntityJoinWithEntityComparisonInWithClausShouldNotAddJoin()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
EntityComplex entityComplex =
session
.CreateQuery(
"select ex "
+ "from EntityComplex ex "
+ "inner join EntityComplex st with st = ex.SameTypeChild "
).SetMaxResults(1)
.UniqueResult<EntityComplex>();

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "EntityComplex").Count, Is.EqualTo(2));
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
}
}

[Test]
public void EntityJoinWithEntityAssociationComparisonShouldAddJoin()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
EntityComplex entityComplex =
session
.CreateQuery(
"select ex "
+ "from EntityComplex ex "
+ "inner join EntityComplex st with st = ex.SameTypeChild.SameTypeChild "
).SetMaxResults(1)
.UniqueResult<EntityComplex>();

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "EntityComplex").Count, Is.EqualTo(3));
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
}
}

[Test]
public void EntityJoinWithEntityAssociationComparison2ShouldAddJoin()
{
using (var sqlLog = new SqlLogSpy())
using (var session = OpenSession())
{
EntityComplex entityComplex =
session
.CreateQuery(
"select ex "
+ "from EntityComplex ex "
+ "inner join EntityComplex st with st.Version = ex.SameTypeChild.Version "
).SetMaxResults(1)
.UniqueResult<EntityComplex>();

Assert.That(Regex.Matches(sqlLog.GetWholeLog(), "EntityComplex").Count, Is.EqualTo(3));
Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
}
}


[Test, Ignore("Failing for unrelated reasons")]
public void CrossJoinAndWithClause()
{
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/DotNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,9 @@ private void DereferenceEntity(EntityType entityType, bool implicitJoin, string
// this is the regression style determination which matches the logic of the classic translator
joinIsNeeded = generateJoin && ( !Walker.IsInSelect || !Walker.IsShallowQuery);
}
else
else
{
joinIsNeeded = generateJoin || ( (Walker.IsInSelect && !Walker.IsInCase ) || Walker.IsInFrom );
joinIsNeeded = generateJoin || ((Walker.IsInSelect && !Walker.IsInCase) || (Walker.IsInFrom && !Walker.IsComparativeExpressionClause));
}

if ( joinIsNeeded )
Expand Down