HHH-4150 - Fix and test

This commit is contained in:
Lukasz Antoniak 2013-02-19 13:45:12 +01:00 committed by Brett Meyer
parent dc311bb027
commit dcccc6c4b0
2 changed files with 70 additions and 1 deletions

View File

@ -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
;

View File

@ -556,6 +556,75 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase {
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();