From f4b45eb5a9085577ecacbfe82a2bc5eb31470863 Mon Sep 17 00:00:00 2001 From: Lukasz Antoniak Date: Fri, 1 Feb 2013 15:17:30 +0100 Subject: [PATCH] HHH-2045 - HQL empty IN list --- hibernate-core/src/main/antlr/hql.g | 1 + .../ast/tree/InLogicOperatorNode.java | 6 ++-- .../test/hql/ASTParserLoadingTest.java | 29 +++++++++++++++++++ .../java/org/hibernate/test/hql/HQLTest.java | 2 +- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/antlr/hql.g b/hibernate-core/src/main/antlr/hql.g index 7ebc79e4ac..90aca2aedd 100644 --- a/hibernate-core/src/main/antlr/hql.g +++ b/hibernate-core/src/main/antlr/hql.g @@ -758,6 +758,7 @@ collectionExpr compoundExpr : collectionExpr | path + | { LA(1) == OPEN && LA(2) == CLOSE }? OPEN! CLOSE! | (OPEN! ( (expression (COMMA! expression)*) | subQuery ) CLOSE!) | parameter ; diff --git a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/InLogicOperatorNode.java b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/InLogicOperatorNode.java index 6bb5e12421..f17dbada6e 100644 --- a/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/InLogicOperatorNode.java +++ b/hibernate-core/src/main/java/org/hibernate/hql/internal/ast/tree/InLogicOperatorNode.java @@ -79,7 +79,9 @@ public class InLogicOperatorNode extends BinaryLogicOperatorNode implements Bina if ( !isNodeAcceptable( rhsNode ) ) return; int rhsColumnSpan = 0; - if ( rhsNode.getType() == HqlTokenTypes.VECTOR_EXPR ) { + if ( rhsNode == null ) { + return; // early exit for empty IN list + } else if ( rhsNode.getType() == HqlTokenTypes.VECTOR_EXPR ) { rhsColumnSpan = rhsNode.getNumberOfChildren(); } else { Type rhsType = extractDataType( rhsNode ); @@ -96,7 +98,7 @@ public class InLogicOperatorNode extends BinaryLogicOperatorNode implements Bina * this is possible for parameter lists and explicit lists. It is completely unreasonable for sub-queries. */ private boolean isNodeAcceptable( Node rhsNode ) { - return rhsNode instanceof LiteralNode + return rhsNode == null /* empty IN list */ || rhsNode instanceof LiteralNode || rhsNode instanceof ParameterNode || rhsNode.getType() == HqlTokenTypes.VECTOR_EXPR; } diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingTest.java b/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingTest.java index e886792144..49c94e6cd0 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingTest.java @@ -49,6 +49,7 @@ import org.hibernate.TypeMismatchException; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.dialect.DB2Dialect; +import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.HSQLDialect; import org.hibernate.dialect.IngresDialect; import org.hibernate.dialect.MySQLDialect; @@ -77,6 +78,7 @@ import org.hibernate.test.cid.Order; import org.hibernate.test.cid.Product; import org.hibernate.testing.DialectChecks; import org.hibernate.testing.FailureExpected; +import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.RequiresDialectFeature; import org.hibernate.testing.SkipForDialect; import org.hibernate.testing.TestForIssue; @@ -485,6 +487,33 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase { s.close(); } + @Test + @TestForIssue( jiraKey = "HHH-2045" ) + @RequiresDialect( H2Dialect.class ) + public void testEmptyInList() { + Session session = openSession(); + session.beginTransaction(); + Human human = new Human(); + human.setName( new Name( "Lukasz", null, "Antoniak" ) ); + human.setNickName( "NONE" ); + session.save( human ); + session.getTransaction().commit(); + session.close(); + + session = openSession(); + session.beginTransaction(); + List results = session.createQuery( "from Human h where h.nickName in ()" ).list(); + assertEquals( 0, results.size() ); + session.getTransaction().commit(); + session.close(); + + session = openSession(); + session.beginTransaction(); + session.delete( human ); + session.getTransaction().commit(); + session.close(); + } + @Test public void testComponentNullnessChecks() { Session s = openSession(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/HQLTest.java b/hibernate-core/src/test/java/org/hibernate/test/hql/HQLTest.java index ddda76c903..7896818b19 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/hql/HQLTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/hql/HQLTest.java @@ -245,7 +245,7 @@ public class HQLTest extends QueryTranslatorTestCase { } @Test - @FailureExpected( jiraKey = "N/A" ) + @TestForIssue( jiraKey = "HHH-2045" ) public void testEmptyInList() { assertTranslation( "select a from Animal a where a.description in ()" ); }