From 9223e06883080dc7335345caaf52b4bcec47fbfc Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Tue, 14 Nov 2023 10:00:08 +0100 Subject: [PATCH] Fix rendering of asterisk projection (`table.*`) for aliased tables. Closes #1651 Original pull request: #1662 --- .../core/sql/render/ExpressionVisitor.java | 12 ++++++++++-- .../core/sql/render/SelectRendererUnitTests.java | 11 +++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/ExpressionVisitor.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/ExpressionVisitor.java index c5b9f62c8f..b2a32c979f 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/ExpressionVisitor.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/ExpressionVisitor.java @@ -98,8 +98,16 @@ Delegation enterMatched(Expression segment) { } else { value = segment.toString(); } - } else if (segment instanceof AsteriskFromTable) { - value = NameRenderer.render(context, ((AsteriskFromTable) segment).getTable()) + ".*"; + } else if (segment instanceof AsteriskFromTable asteriskFromTable) { + + TableLike table = asteriskFromTable.getTable(); + CharSequence renderedTable; + if (table instanceof Aliased aliasedTable) { + renderedTable = NameRenderer.render(context, aliasedTable); + } else { + renderedTable = NameRenderer.render(context, table); + } + value = renderedTable + ".*"; } else if (segment instanceof Cast) { CastVisitor visitor = new CastVisitor(context); diff --git a/spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java b/spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java index 3ec3f0d054..177ea67102 100644 --- a/spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java +++ b/spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java @@ -638,6 +638,17 @@ void notOfNested() { assertThat(sql).isEqualTo("SELECT atable.* FROM atable WHERE NOT (atable.id = 1 AND atable.id = 2)"); } + @Test // GH-1651 + void asteriskOfAliasedTableUsesAlias() { + + Table employee = SQL.table("employee").as("e"); + Select select = Select.builder().select(employee.asterisk()).select(employee.column("id")).from(employee).build(); + + String rendered = SqlRenderer.toString(select); + + assertThat(rendered).isEqualTo("SELECT e.*, e.id FROM employee e"); + } + /** * Tests the rendering of analytic functions. */