Skip to content

Commit 9fc784e

Browse files
authored
Support count distinct on function in hql (#2502)
Fixes #1285
1 parent 008760c commit 9fc784e

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

src/NHibernate.Test/Async/Hql/Ast/HqlFixture.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,5 +306,14 @@ public async Task UnaryMinusBeforeParenthesesHandledCorrectlyAsync()
306306
Assert.That(actualWorkaround, Is.EqualTo(-2));
307307
}
308308
}
309+
310+
//NH-3249 (GH-1285)
311+
[Test]
312+
public async Task CountDistinctOnFunctionAsync()
313+
{
314+
var hql = @"SELECT COUNT(DISTINCT DATE(m.birthdate)) FROM Mammal m";
315+
using(var s = OpenSession())
316+
await (s.CreateQuery(hql).ListAsync());
317+
}
309318
}
310319
}

src/NHibernate.Test/Async/Linq/ByMethod/CountTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ public async Task CountDistinctProperty_ReturnsNumberOfDistinctEntriesForThatPro
4040
Assert.That(result, Is.EqualTo(387));
4141
}
4242

43+
//NH-3249 (GH-1285)
44+
[Test]
45+
public async Task CountDistinctFunc_ReturnsNumberOfDistinctEntriesForThatFuncAsync()
46+
{
47+
if (!TestDialect.SupportsCountDistinct)
48+
Assert.Ignore("Dialect does not support count distinct");
49+
50+
var result = await (db.Orders
51+
.Select(x => x.ShippingDate.Value.Date)
52+
.Distinct()
53+
.CountAsync());
54+
55+
Assert.That(result, Is.EqualTo(387));
56+
}
57+
4358
[Test]
4459
public async Task CountProperty_ReturnsNumberOfNonNullEntriesForThatPropertyAsync()
4560
{

src/NHibernate.Test/Hql/Ast/HqlFixture.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,5 +337,14 @@ public void UnaryMinusBeforeParenthesesHandledCorrectly()
337337
Assert.That(actualWorkaround, Is.EqualTo(-2));
338338
}
339339
}
340+
341+
//NH-3249 (GH-1285)
342+
[Test]
343+
public void CountDistinctOnFunction()
344+
{
345+
var hql = @"SELECT COUNT(DISTINCT DATE(m.birthdate)) FROM Mammal m";
346+
using(var s = OpenSession())
347+
s.CreateQuery(hql).List();
348+
}
340349
}
341350
}

src/NHibernate.Test/Linq/ByMethod/CountTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ public void CountDistinctProperty_ReturnsNumberOfDistinctEntriesForThatProperty(
2828
Assert.That(result, Is.EqualTo(387));
2929
}
3030

31+
//NH-3249 (GH-1285)
32+
[Test]
33+
public void CountDistinctFunc_ReturnsNumberOfDistinctEntriesForThatFunc()
34+
{
35+
if (!TestDialect.SupportsCountDistinct)
36+
Assert.Ignore("Dialect does not support count distinct");
37+
38+
var result = db.Orders
39+
.Select(x => x.ShippingDate.Value.Date)
40+
.Distinct()
41+
.Count();
42+
43+
Assert.That(result, Is.EqualTo(387));
44+
}
45+
3146
[Test]
3247
public void CountProperty_ReturnsNumberOfNonNullEntriesForThatProperty()
3348
{

src/NHibernate/Hql/Ast/ANTLR/Hql.g

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,14 @@ aggregateArgument
627627
;
628628
629629
aggregateDistinctAll
630-
: ( ( DISTINCT | ALL )? ( path | collectionExpr ) )
630+
: ( distinctAll aggregateArgument ) => (distinctAll aggregateArgument)
631+
| aggregateArgument
631632
;
632-
633+
634+
distinctAll
635+
: ( DISTINCT | ALL )
636+
;
637+
633638
//## collection: ( OPEN query CLOSE ) | ( 'elements'|'indices' OPEN path CLOSE );
634639
635640
collectionExpr

0 commit comments

Comments
 (0)