Skip to content

Commit

Permalink
Enable Not node handling in HqlParser.NegateNode (#3390)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahusoid authored Aug 1, 2023
1 parent d32a9d9 commit d6fd1dc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH3327/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,21 @@ WHERE NOT (
)");
Assert.That((await (q.ListAsync()))[0], Is.EqualTo(0));
}

[Test]
public async Task NotNotExistsNegatedAsync()
{
using var log = new SqlLogSpy();
using var session = OpenSession();
var results = await (session.CreateQuery(
@"SELECT COUNT(ROOT.Id)
FROM Entity AS ROOT
WHERE NOT (
NOT EXISTS (FROM ChildEntity AS CHILD WHERE CHILD.Parent = ROOT)
AND NOT ROOT.Name = 'Parent'
)").ListAsync());
Assert.That(log.GetWholeLog(), Does.Not.Contains(" NOT ").IgnoreCase);
Assert.That(results.Count, Is.EqualTo(1));
}
}
}
16 changes: 16 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3327/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,21 @@ WHERE NOT (
)");
Assert.That(q.List()[0], Is.EqualTo(0));
}

[Test]
public void NotNotExistsNegated()
{
using var log = new SqlLogSpy();
using var session = OpenSession();
var results = session.CreateQuery(
@"SELECT COUNT(ROOT.Id)
FROM Entity AS ROOT
WHERE NOT (
NOT EXISTS (FROM ChildEntity AS CHILD WHERE CHILD.Parent = ROOT)
AND NOT ROOT.Name = 'Parent'
)").List();
Assert.That(log.GetWholeLog(), Does.Not.Contains(" NOT ").IgnoreCase);
Assert.That(results.Count, Is.EqualTo(1));
}
}
}
6 changes: 2 additions & 4 deletions src/NHibernate/Hql/Ast/ANTLR/HqlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,8 @@ public IASTNode NegateNode(IASTNode node)
node.Type = BETWEEN;
node.Text = "{not}" + node.Text;
return node; // (NOT (NOT_BETWEEN a b) ) => (BETWEEN a b)
/* This can never happen because this rule will always eliminate the child NOT.
case NOT:
return x.getFirstChild(); // (NOT (NOT x) ) => (x)
*/
case NOT:
return node.GetChild(0); // (NOT (NOT x) ) => (x)
default:
IASTNode not = (IASTNode) TreeAdaptor.Create(NOT, "not");
not.AddChild(node);
Expand Down

0 comments on commit d6fd1dc

Please sign in to comment.