Skip to content

Commit 3f8a8cd

Browse files
committed
Fix HQL rendering of CTE with CYCLE clause.
Ensure SET identifier is an expression. Closes #4012
1 parent 041898e commit 3f8a8cd

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/HqlQueryRenderer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,22 @@ public QueryTokenStream visitCycleClause(HqlParser.CycleClauseContext ctx) {
195195
QueryRendererBuilder builder = QueryRenderer.builder();
196196

197197
builder.append(QueryTokens.expression(ctx.CYCLE().getText()));
198-
builder.append(visit(ctx.cteAttributes()));
198+
builder.appendExpression(visit(ctx.cteAttributes()));
199199
builder.append(QueryTokens.expression(ctx.SET().getText()));
200-
builder.append(visit(ctx.identifier(0)));
200+
builder.appendExpression(visit(ctx.identifier(0)));
201201

202202
if (ctx.TO() != null) {
203203

204204
builder.append(QueryTokens.expression(ctx.TO().getText()));
205205
builder.append(visit(ctx.literal(0)));
206206
builder.append(QueryTokens.expression(ctx.DEFAULT().getText()));
207-
builder.append(visit(ctx.literal(1)));
207+
builder.appendExpression(visit(ctx.literal(1)));
208208
}
209209

210210
if (ctx.USING() != null) {
211211

212212
builder.append(QueryTokens.expression(ctx.USING().getText()));
213-
builder.append(visit(ctx.identifier(1)));
213+
builder.appendExpression(visit(ctx.identifier(1)));
214214
}
215215

216216
return builder;

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/HqlQueryRendererTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,31 @@ WITH maxId AS (select max(sr.snapshot.id) snapshotId from SnapshotReference sr
17301730
""");
17311731
}
17321732

1733+
@Test // GH-4012
1734+
void cteWithSearch() {
1735+
1736+
assertQuery("""
1737+
WITH Tree AS (SELECT o.uuid AS test_uuid FROM DemoEntity o)
1738+
SEARCH BREADTH FIRST BY foo ASC NULLS FIRST, bar DESC NULLS LAST SET baz
1739+
SELECT test_uuid FROM Tree
1740+
""");
1741+
}
1742+
1743+
@Test // GH-4012
1744+
void cteWithCycle() {
1745+
1746+
assertQuery("""
1747+
WITH Tree AS (SELECT o.uuid AS test_uuid FROM DemoEntity o) CYCLE test_uuid SET circular TO true DEFAULT false
1748+
SELECT test_uuid FROM Tree
1749+
""");
1750+
1751+
assertQuery(
1752+
"""
1753+
WITH Tree AS (SELECT o.uuid AS test_uuid FROM DemoEntity o) CYCLE test_uuid SET circular TO true DEFAULT false USING bar
1754+
SELECT test_uuid FROM Tree
1755+
""");
1756+
}
1757+
17331758
@Test // GH-2982
17341759
void floorShouldBeValidEntityName() {
17351760

0 commit comments

Comments
 (0)