Skip to content

Fix Count Distinct on path with hql reserved words #3217

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 5 commits into from
Jan 4, 2023
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
64 changes: 64 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH3215/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3215
{
using System.Threading.Tasks;
[TestFixture]
public class ByCodeFixtureAsync : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();

mapper.Class<Member>(rc =>
{
rc.Id(x => x.Code);
rc.Property(x => x.Name);
rc.Property(x => x.Date);
});

mapper.Class<Request>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.ManyToOne(x => x.Member);
});
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override bool AppliesTo(Dialect.Dialect dialect)
{
return TestDialect.SupportsCountDistinct;
}

protected override void Configure(Configuration configuration)
{
base.Configure(configuration);
configuration.SetProperty(Environment.Hbm2ddlKeyWords, "auto-quote");
}

[Test]
public async Task CountDistinctWithReservedWordsAsync()
{
using (var session = OpenSession())
{
var hql = "select Count(DISTINCT r.Member.id), Count(DISTINCT Date(r.Member.Date)) from Request r";
var result = await (session.CreateQuery(hql).ListAsync());

Assert.That(result, Has.Count.EqualTo(1));
}
}
}
}
17 changes: 17 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3215/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH3215
{
class Member
{
public virtual int Code { get; set; }
public virtual string Name { get; set; }
public virtual DateTime Date { get; set; }
}

class Request
{
public virtual Guid Id { get; set; }
public virtual Member Member { get; set; }
}
}
53 changes: 53 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3215/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3215
{
[TestFixture]
public class ByCodeFixture : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();

mapper.Class<Member>(rc =>
{
rc.Id(x => x.Code);
rc.Property(x => x.Name);
rc.Property(x => x.Date);
});

mapper.Class<Request>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.ManyToOne(x => x.Member);
});
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override bool AppliesTo(Dialect.Dialect dialect)
{
return TestDialect.SupportsCountDistinct;
}

protected override void Configure(Configuration configuration)
{
base.Configure(configuration);
configuration.SetProperty(Environment.Hbm2ddlKeyWords, "auto-quote");
}

[Test]
public void CountDistinctWithReservedWords()
{
using (var session = OpenSession())
{
var hql = "select Count(DISTINCT r.Member.id), Count(DISTINCT Date(r.Member.Date)) from Request r";
var result = session.CreateQuery(hql).List();

Assert.That(result, Has.Count.EqualTo(1));
}
}
}
}
2 changes: 1 addition & 1 deletion src/NHibernate/Hql/Ast/ANTLR/Hql.g
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ vectorExpr
// NOTE: handleDotIdent() is called immediately after the first IDENT is recognized because
// the method looks a head to find keywords after DOT and turns them into identifiers.
identPrimary
: identifier { HandleDotIdent(); }
: identifier {{ HandleDotIdent(); }}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HandleDotIdent should be executed even while backtracking

( options {greedy=true;} : DOT^ ( identifier | o=OBJECT { $o.Type = IDENT; } ) )*
( ( op=OPEN^ { $op.Type = METHOD_CALL;} exprList CLOSE! )
)?
Expand Down