"select new ..." with CAST( NULL AS ...) fails with QuerySyntaxException: unexpected AST node: NULL

Description

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ... org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: NULL near line 1, column 59 [SELECT NEW support.hibernate.dto.EmployeeDTO(e.name, CAST(NULL as long), e.title) FROM support.hibernate.entity.Employee e WHERE e.name = :name] at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54) at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47) at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:79) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.generate(QueryTranslatorImpl.java:240) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:205) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:104) at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:79) at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168) at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:222) at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:200) at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1703) at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:301) ...

Executing a query like the below

1 final String hql = "SELECT NEW support.hibernate.dto.EmployeeDTO(e.name, CAST(NULL as long), e.title) FROM Employee e WHERE e.name = :name";

Looks like something changed between Hibernate 4.2.7.SP5-redhat-1 (EAP 6.2.4) and Hibernate 4.2.14.SP1-redhat-1 (EAP 6.3.0) with regard to antlr parsing so that the keyword 'NULL' causes a parse failure in the HQL query. The issue remains in 5.0.9 and 5.1.0

Replacing the 'CAST(NULL as long)' with 'CAST(12 as long)' works ...

If the query in question is a @NamedQuery the failure will be at the point the entity manager is created. The same issue can be seen with a simple dynamic HQL query, however, as the testcase attached shows. The test uses an H2 db and can be run using 'mvn clean test'

Environment

None

Status

Assignee

Gail Badner

Reporter

Stephen Fikes

Fix versions

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

4.2.21
5.1.0
5.0.9

Priority

Major
Configure