Skip to content
Closed
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
2 changes: 1 addition & 1 deletion hibernate-core/src/main/antlr/hql.g
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ castedIdentPrimaryBase
aggregate
: ( SUM^ | AVG^ | MAX^ | MIN^ ) OPEN! additiveExpression CLOSE! { #aggregate.setType(AGGREGATE); }
// Special case for count - It's 'parameters' can be keywords.
| COUNT^ OPEN! ( STAR { #STAR.setType(ROW_STAR); } | ( ( DISTINCT | ALL )? ( path | collectionExpr | NUM_INT ) ) ) CLOSE!
| COUNT^ OPEN! ( STAR { #STAR.setType(ROW_STAR); } | ( ( DISTINCT | ALL )? ( path | collectionExpr | NUM_INT | caseExpression ) ) ) CLOSE!
| collectionExpr
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,75 @@ public void testComponentNullnessChecks() {
s.close();
}

@Test
@TestForIssue( jiraKey = "HHH-4150" )
public void testSelectClauseCaseWithSum() {
Session s = openSession();
Transaction t = s.beginTransaction();

Human h1 = new Human();
h1.setBodyWeight( 74.0f );
h1.setDescription( "Me" );
s.persist( h1 );

Human h2 = new Human();
h2.setBodyWeight( 125.0f );
h2.setDescription( "big persion #1" );
s.persist( h2 );

Human h3 = new Human();
h3.setBodyWeight( 110.0f );
h3.setDescription( "big persion #2" );
s.persist( h3 );

s.flush();

Number count = (Number) s.createQuery( "select sum(case when bodyWeight > 100 then 1 else 0 end) from Human" ).uniqueResult();
assertEquals( 2, count.intValue() );
count = (Number) s.createQuery( "select sum(case when bodyWeight > 100 then bodyWeight else 0 end) from Human" ).uniqueResult();
assertEquals( h2.getBodyWeight() + h3.getBodyWeight(), count.floatValue(), 0.001 );

t.rollback();
s.close();
}

@Test
@TestForIssue( jiraKey = "HHH-4150" )
public void testSelectClauseCaseWithCountDistinct() {
Session s = openSession();
Transaction t = s.beginTransaction();

Human h1 = new Human();
h1.setBodyWeight( 74.0f );
h1.setDescription( "Me" );
h1.setNickName( "Oney" );
s.persist( h1 );

Human h2 = new Human();
h2.setBodyWeight( 125.0f );
h2.setDescription( "big persion" );
h2.setNickName( "big #1" );
s.persist( h2 );

Human h3 = new Human();
h3.setBodyWeight( 110.0f );
h3.setDescription( "big persion" );
h3.setNickName( "big #2" );
s.persist( h3 );

s.flush();

Number count = (Number) s.createQuery( "select count(distinct case when bodyWeight > 100 then description else null end) from Human" ).uniqueResult();
assertEquals( 1, count.intValue() );
count = (Number) s.createQuery( "select count(case when bodyWeight > 100 then description else null end) from Human" ).uniqueResult();
assertEquals( 2, count.intValue() );
count = (Number) s.createQuery( "select count(distinct case when bodyWeight > 100 then nickName else null end) from Human" ).uniqueResult();
assertEquals( 2, count.intValue() );

t.rollback();
s.close();
}

@Test
public void testInvalidCollectionDereferencesFail() {
Session s = openSession();
Expand Down