| 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: FormulaWithTwoFromTablesTest.java 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.
result: "(SELECT ... "
token: "FROM"
inFromClause: false
beforeTable: false
afterFromTable: false
BEFORE_TABLE_KEYWORDS contains "FROM", so conditional at line 311 changes state to
inFromClause: false -> true
beforeTable: false -> true
The token is then appended and the loop continues...
result: "(SELECT ... FROM"
token: " "
inFromClause: true
beforeTable: true
afterFromTable: false
The token is then appended and the loop continues...
result: "(SELECT ... FROM "
token: "A"
inFromClause: true
beforeTable: true
afterFromTable: false
beforeTable is true, so the conditional branch at line 290 is executed, changing state to:
beforeTable: true -> false
afterFromTable: false -> true
The token is then appended and the loop continues...
result: "(SELECT ... FROM A"
token: ","
inFromClause: true
beforeTable: false
afterFromTable: true
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:
afterFromTable: true -> false
The token is then appended and the loop continues...
result: "(SELECT ... FROM A,"
token: " "
inFromClause: true
beforeTable: false
afterFromTable: false
The token is then appended and the loop continues...
result: "(SELECT ... FROM A, "
token: "B"
inFromClause: true
beforeTable: false
afterFromTable: false
the token is a valid identifier, so the conditional branch at 304 is executed, appending the placeholder, a period, and the token
result: "(SELECT ... FROM A, $placeholder$.B"
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. |