BasicFormatterImpl throws a NPE if native SQL begins with a parentheses

Description

If a native query starts with a parentheses and Hibernate is logging queries, BasicFormatterImpl throws a NPE because it is assuming that the previous token was not null.

An example of SQL that may begin with parentheses is a UNION:

(SELECT ....)
UNION
(SELECT ...)

Below is the stacktrace.

java.lang.NullPointerException
at org.hibernate.engine.jdbc.internal.BasicFormatterImpl$FormatProcess.isFunctionName(BasicFormatterImpl.java:359)
at org.hibernate.engine.jdbc.internal.BasicFormatterImpl$FormatProcess.openParen(BasicFormatterImpl.java:340)
at org.hibernate.engine.jdbc.internal.BasicFormatterImpl$FormatProcess.perform(BasicFormatterImpl.java:141)
at org.hibernate.engine.jdbc.internal.BasicFormatterImpl.format(BasicFormatterImpl.java:75)
at org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement(SqlStatementLogger.java:89)
at org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement(SqlStatementLogger.java:77)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:167)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:148)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1985)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1915)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1893)
at org.hibernate.loader.Loader.doQuery(Loader.java:938)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
at org.hibernate.loader.Loader.doList(Loader.java:2692)
at org.hibernate.loader.Loader.doList(Loader.java:2675)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2507)
at org.hibernate.loader.Loader.list(Loader.java:2502)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:335)
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2162)
at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1016)
at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:152)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414)
at org.hibernate.query.Query.getResultList(Query.java:146)

Environment

All

Status

Assignee

Unassigned

Reporter

Andrew Geery

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

backportDecision

None

Components

Affects versions

Priority

Trivial
Configure