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

Support count distinct on function in hql #2502

Merged
merged 1 commit into from
Aug 30, 2020
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
9 changes: 9 additions & 0 deletions src/NHibernate.Test/Async/Hql/Ast/HqlFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,14 @@ public async Task UnaryMinusBeforeParenthesesHandledCorrectlyAsync()
Assert.That(actualWorkaround, Is.EqualTo(-2));
}
}

//NH-3249 (GH-1285)
[Test]
public async Task CountDistinctOnFunctionAsync()
{
var hql = @"SELECT COUNT(DISTINCT DATE(m.birthdate)) FROM Mammal m";
using(var s = OpenSession())
await (s.CreateQuery(hql).ListAsync());
}
}
}
15 changes: 15 additions & 0 deletions src/NHibernate.Test/Async/Linq/ByMethod/CountTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ public async Task CountDistinctProperty_ReturnsNumberOfDistinctEntriesForThatPro
Assert.That(result, Is.EqualTo(387));
}

//NH-3249 (GH-1285)
[Test]
public async Task CountDistinctFunc_ReturnsNumberOfDistinctEntriesForThatFuncAsync()
{
if (!TestDialect.SupportsCountDistinct)
Assert.Ignore("Dialect does not support count distinct");

var result = await (db.Orders
.Select(x => x.ShippingDate.Value.Date)
.Distinct()
.CountAsync());

Assert.That(result, Is.EqualTo(387));
}

[Test]
public async Task CountProperty_ReturnsNumberOfNonNullEntriesForThatPropertyAsync()
{
Expand Down
9 changes: 9 additions & 0 deletions src/NHibernate.Test/Hql/Ast/HqlFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,5 +337,14 @@ public void UnaryMinusBeforeParenthesesHandledCorrectly()
Assert.That(actualWorkaround, Is.EqualTo(-2));
}
}

//NH-3249 (GH-1285)
[Test]
public void CountDistinctOnFunction()
{
var hql = @"SELECT COUNT(DISTINCT DATE(m.birthdate)) FROM Mammal m";
using(var s = OpenSession())
s.CreateQuery(hql).List();
}
}
}
15 changes: 15 additions & 0 deletions src/NHibernate.Test/Linq/ByMethod/CountTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ public void CountDistinctProperty_ReturnsNumberOfDistinctEntriesForThatProperty(
Assert.That(result, Is.EqualTo(387));
}

//NH-3249 (GH-1285)
[Test]
public void CountDistinctFunc_ReturnsNumberOfDistinctEntriesForThatFunc()
{
if (!TestDialect.SupportsCountDistinct)
Assert.Ignore("Dialect does not support count distinct");

var result = db.Orders
.Select(x => x.ShippingDate.Value.Date)
.Distinct()
.Count();

Assert.That(result, Is.EqualTo(387));
}

[Test]
public void CountProperty_ReturnsNumberOfNonNullEntriesForThatProperty()
{
Expand Down
9 changes: 7 additions & 2 deletions src/NHibernate/Hql/Ast/ANTLR/Hql.g
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,14 @@ aggregateArgument
;

aggregateDistinctAll
: ( ( DISTINCT | ALL )? ( path | collectionExpr ) )
: ( distinctAll aggregateArgument ) => (distinctAll aggregateArgument)
| aggregateArgument
Comment on lines +630 to +631
Copy link
Member Author

Choose a reason for hiding this comment

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

An obvious ( ( DISTINCT | ALL )? aggregateArgument ) gives:

[fatal] rule aggregate has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.

So had to use this syntactic predicate rule with code duplication..

;


distinctAll
: ( DISTINCT | ALL )
;

//## collection: ( OPEN query CLOSE ) | ( 'elements'|'indices' OPEN path CLOSE );

collectionExpr
Expand Down