Skip to content

Commit e549391

Browse files
darinmanicagregturn
authored andcommitted
Fix case where the from clause is misidentified.
Add a zero-width word boundary to the regex that identifies the from clause. This is used in alias detection. See #2508, #2260.
1 parent 0549fcc commit e549391

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public abstract class QueryUtils {
138138
static {
139139

140140
StringBuilder builder = new StringBuilder();
141-
builder.append("(?<=from)"); // from as starting delimiter
141+
builder.append("(?<=\\bfrom)"); // from as starting delimiter
142142
builder.append("(?:\\s)+"); // at least one space separating
143143
builder.append(IDENTIFIER_GROUP); // Entity name, can be qualified (any
144144
builder.append("(?:\\sas)*"); // exclude possible "as" keyword

src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ void detectsAliasCorrectly() {
128128
assertThat(detectAlias(
129129
"(from Foo f max(f) ((((select * from Foo f2 (from Foo f3) max(*)) (from Foo f4)) max(f5)) (f6)) (from Foo f7))"))
130130
.isEqualTo("f");
131+
assertThat(detectAlias(
132+
"SELECT e FROM DbEvent e WHERE (CAST(:modifiedFrom AS date) IS NULL OR e.modificationDate >= :modifiedFrom)"))
133+
.isEqualTo("e");
134+
assertThat(detectAlias("from User u where (cast(:effective as date) is null) OR :effective >= u.createdAt"))
135+
.isEqualTo("u");
136+
assertThat(detectAlias("from User u where (cast(:effectiveDate as date) is null) OR :effectiveDate >= u.createdAt"))
137+
.isEqualTo("u");
138+
assertThat(detectAlias("from User u where (cast(:effectiveFrom as date) is null) OR :effectiveFrom >= u.createdAt"))
139+
.isEqualTo("u");
140+
assertThat(
141+
detectAlias("from User u where (cast(:e1f2f3ectiveFrom as date) is null) OR :effectiveFrom >= u.createdAt"))
142+
.isEqualTo("u");
131143
}
132144

133145
@Test // GH-2260

0 commit comments

Comments
 (0)