Skip to content
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

Handle both null and not null checks in queries on not-found='ignore' properties #2989

Merged
merged 6 commits into from
Feb 14, 2022
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
12 changes: 12 additions & 0 deletions src/NHibernate.Test/Async/Hql/EntityJoinHqlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,23 @@ public async Task NullableEntityProjectionAsync()
from x2 in session.Query<NullableOwner>()
where x == x2 && x.ManyToOne == null && x.OneToOne.Name == null
select x2).ToListAsync());

var validManyToOne = new OneToOneEntity{Name = "valid"};
await (session.SaveAsync(validManyToOne));
nullableOwner2.ManyToOne = validManyToOne;
await (session.FlushAsync());

//GH-2988
var withNullOrValidList = await (session.Query<NullableOwner>().Where(x => x.ManyToOne.Id == validManyToOne.Id || x.ManyToOne == null).ToListAsync());
var withNullOrValidList2 = await (session.Query<NullableOwner>().Where(x => x.ManyToOne == null || x.ManyToOne.Id == validManyToOne.Id).ToListAsync());

Assert.That(fullList.Count, Is.EqualTo(2));
Assert.That(withValidManyToOneList.Count, Is.EqualTo(0));
Assert.That(withValidManyToOneList2.Count, Is.EqualTo(0));
Assert.That(withNullManyToOneList.Count, Is.EqualTo(2));
Assert.That(withNullManyToOneJoinedList.Count, Is.EqualTo(2));
Assert.That(withNullOrValidList.Count, Is.EqualTo(2));
Assert.That(withNullOrValidList2.Count, Is.EqualTo(2));
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/NHibernate.Test/Hql/EntityJoinHqlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,23 @@ public void NullableEntityProjection()
from x2 in session.Query<NullableOwner>()
where x == x2 && x.ManyToOne == null && x.OneToOne.Name == null
select x2).ToList();

var validManyToOne = new OneToOneEntity{Name = "valid"};
session.Save(validManyToOne);
nullableOwner2.ManyToOne = validManyToOne;
session.Flush();

//GH-2988
var withNullOrValidList = session.Query<NullableOwner>().Where(x => x.ManyToOne.Id == validManyToOne.Id || x.ManyToOne == null).ToList();
var withNullOrValidList2 = session.Query<NullableOwner>().Where(x => x.ManyToOne == null || x.ManyToOne.Id == validManyToOne.Id).ToList();

Assert.That(fullList.Count, Is.EqualTo(2));
Assert.That(withValidManyToOneList.Count, Is.EqualTo(0));
Assert.That(withValidManyToOneList2.Count, Is.EqualTo(0));
Assert.That(withNullManyToOneList.Count, Is.EqualTo(2));
Assert.That(withNullManyToOneJoinedList.Count, Is.EqualTo(2));
Assert.That(withNullOrValidList.Count, Is.EqualTo(2));
Assert.That(withNullOrValidList2.Count, Is.EqualTo(2));
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/NHibernate/Engine/JoinSequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private sealed class Join
{
private readonly IAssociationType associationType;
private readonly IJoinable joinable;
private readonly JoinType joinType;
private JoinType joinType;
private readonly string alias;
private readonly string[] lhsColumns;

Expand Down Expand Up @@ -75,6 +75,7 @@ public IJoinable Joinable
public JoinType JoinType
{
get { return joinType; }
internal set { joinType = value; }
}

public string[] LHSColumns
Expand Down Expand Up @@ -329,5 +330,11 @@ public interface ISelector
public ISessionFactoryImplementor Factory => factory;

internal bool ForceFilter { get; set; }

internal void SetJoinType(JoinType joinType)
{
joins[0].JoinType = joinType;
SetUseThetaStyle(false);
}
}
}
23 changes: 13 additions & 10 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/DotNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,8 @@ private void DereferenceEntity(EntityType entityType, bool implicitJoin, string

if ( joinIsNeeded )
{
if (comparisonWithNullableEntity && Walker.IsNullComparison)
{
implicitJoin = false;
_joinType = JoinType.LeftOuterJoin;
}

DereferenceEntityJoin( classAlias, entityType, implicitJoin, parent );
var forceLeftJoin = comparisonWithNullableEntity && Walker.IsNullComparison;
DereferenceEntityJoin(classAlias, entityType, implicitJoin, parent, forceLeftJoin);
if (comparisonWithNullableEntity)
{
_columns = FromElement.GetIdentityColumns();
Expand Down Expand Up @@ -457,7 +452,7 @@ private void DereferenceEntityIdentifier(string propertyName, DotNode dotParent)
}
}

private void DereferenceEntityJoin(string classAlias, EntityType propertyType, bool impliedJoin, IASTNode parent)
private void DereferenceEntityJoin(string classAlias, EntityType propertyType, bool impliedJoin, IASTNode parent, bool forceLeftJoin)
{
_dereferenceType = DerefEntity;
if ( Log.IsDebugEnabled() )
Expand All @@ -476,7 +471,11 @@ private void DereferenceEntityJoin(string classAlias, EntityType propertyType, b
string[] joinColumns = GetColumns();
string joinPath = Path;

if ( impliedJoin && Walker.IsInFrom )
if (forceLeftJoin)
{
_joinType = JoinType.LeftOuterJoin;
}
else if (impliedJoin && Walker.IsInFrom)
{
_joinType = Walker.ImpliedJoinType;
}
Expand Down Expand Up @@ -524,7 +523,7 @@ private void DereferenceEntityJoin(string classAlias, EntityType propertyType, b
// If this is an implied join in a from element, then use the impled join type which is part of the
// tree parser's state (set by the gramamar actions).
JoinSequence joinSequence = SessionFactoryHelper
.CreateJoinSequence( impliedJoin, propertyType, tableAlias, _joinType, joinColumns );
.CreateJoinSequence(!forceLeftJoin && impliedJoin, propertyType, tableAlias, _joinType, joinColumns);

var factory = new FromElementFactory(
currentFromClause,
Expand All @@ -545,6 +544,10 @@ private void DereferenceEntityJoin(string classAlias, EntityType propertyType, b
}
else
{
if (forceLeftJoin)
{
elem.JoinSequence.SetJoinType(_joinType);
}
currentFromClause.AddDuplicateAlias(classAlias, elem);
}

Expand Down