Table names beyond the first in Formula queries incorrectly get the table name alias prefixed to them

Description

I am encountering what I believe to be a bug with how formula queries get rendered into sql. Specifically, any table names beyond the first table in a from clause get prefixed with the root table alias when they should instead be left alone.

Test case attached:

The following is what my investigation of the issue found

( Line numbers are referencing [Template.java on GitHub](https://github.com/hibernate/hibernate-orm/blob/e5dc635a52362f69b69acb8d5b166b69b165dbbd/hibernate-core/src/main/java/org/hibernate/sql/Template.java) )

Given some formula template of the form: "(SELECT ... FROM A, B ...)"

Assume everything up to the FROM is rendered normally and without error. The following steps then occur.

BEFORE_TABLE_KEYWORDS contains "FROM", so conditional at line 311 changes state to

The token is then appended and the loop continues...

The token is then appended and the loop continues...

beforeTable is true, so the conditional branch at line 290 is executed, changing state to:

The token is then appended and the loop continues...

afterFromTable is true, so the conditional branch at 295 is executed
The token is not "as", so the conditional branch at 296 is executed, changing state to:

The token is then appended and the loop continues...

The token is then appended and the loop continues...

the token is a valid identifier, so the conditional branch at 304 is executed, appending the placeholder, a period, and the token

However, there appears to be a conditional at line 315 intended to handle the case of a comma in a from clause, which would set beforeTable to true, but because the conditional at line 295 matches that comma, beforeTable never gets set to true.

It appears to me that either the conditional at line 315 is dead code, or that there is a bug in how the if/else chain is implemented.

Environment

Test case is against Hibernate 5.3.7.Final, but my production environment experiencing the issue is 3.5.6 (quite old, but the bug appears to be present since at least then)

Assignee

Unassigned

Reporter

Benjamin DeLillo

Fix versions

Labels

None

backPortable

Backport?

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure