We're updating the issue view to help you get more done. 

ANTLR parser should fail when providing an extra parenthesis

Description

Assuming we have a query like this one where the end parenthesis does not have a matching start parenthesis:

1 2 3 4 5 6 session.createQuery( "select a from Product a " + "where " + "coalesce(a.description, :description) = :description)", Product.class) .setParameter( "description", "desc" ) .getResultList();

The end parenthesis is discarded instead of throwing an exception:

1 2 3 4 5 6 7 8 9 select product0_.productId as productI1_35_, product0_.description as descript2_35_, product0_.cost as cost3_35_, product0_.numberAvailable as numberAv4_35_ from Product product0_ where coalesce(product0_.description, ?)=?

Now, this can lead to very weird situations where any clause following the extra round bracket is simply discarded.

So, if you have an ORDER BY clause after the extra parenthesis:

1 2 3 4 5 6 7 session.createQuery( "select a from Product a " + "where " + "coalesce(a.description, :description) = :description ) " + "order by a.description ", Product.class) .setParameter( "description", "desc" ) .getResultList();

The ORDER BY vanishes from the SQL statement:

1 2 3 4 5 6 7 8 9 select product0_.productId as productI1_35_, product0_.description as descript2_35_, product0_.cost as cost3_35_, product0_.numberAvailable as numberAv4_35_ from Product product0_ where coalesce(product0_.description, ?)=?

You can find a replicating test case in hibernate-core:

ASTParserLoadingTest#testOrderByExtraParenthesis

Environment

None

Status

Assignee

Vlad Mihalcea

Reporter

Vlad Mihalcea

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

backportDecision

None

Components

Affects versions

5.2.10

Priority

Major