Join condition gets rendered wrongly if column has an alias [DATAJDBC-353]
Jens Schauder opened DATAJDBC-353 and commented
This test when added to SelectRendererUnitTest fails because the columns in the join condition get rendered with table-alias.column AS column-alias.
@Test
public void shouldRenderJoinWithAliases() {
Table a = SQL.table("a").as("a_alias"); Column aColumn = a.column("a_col").as("a_col_alias");
Table b = SQL.table("b").as("b_alias"); Column bColumn = b.column("a_col").as("a_col_alias");
Select select = Select.builder().select(aColumn, bColumn) //
.from(a) //
.join(b).on(aColumn).equals(bColumn) //
.build();
assertThat(SqlRenderer.toString(select)) //
.isEqualTo(
"SELECT a_alias.a_col AS a_col_alias, b_alias.b_col AS b_col_alias " //
+ "FROM a AS a_alias " //
+ "JOIN b AS b_alias ON a_alias.a_col = b_alias.b_col " //
);
}
Referenced from: pull request https://github.com/spring-projects/spring-data-jdbc/pull/246
Hebert Coelho de Oliveira commented
Hello, I want to help with this ticket. I have a question. I do understand that the join has a problem.
But, in the "isEqualTo" condition, on the select statement "b_alias.b_col" should not be "a_col"?
The "bColmun" attribute was created with "a_col". should not, the correct select statement be: b_alias.a_col AS a_col_alias that is how the alias for the B is created?
I will start looking at the join problem with alias for now.
Hebert Coelho de Oliveira commented
I also noticed in the join condition. Would not, in the expected query, the correct be "b_alias.a_col"?
Hebert Coelho de Oliveira commented
I created this PR: https://github.com/spring-projects/spring-data-jdbc/pull/246
Jens Schauder commented
You are right, the test I included in the issue is not correct.
The definition for the bColumn should be
Column bColumn = b.column("b_col").as("b_col_alias")
and the join condition should match that.
(and the column definition should be in its own line).
With this definition everything with a b prefix comes from the b table which should make the test easy to understand.
The problem can no longer be reproduced