Skip to content

Commit

Permalink
Fix case where the from clause is misidentified.
Browse files Browse the repository at this point in the history
Add a zero-width word boundary to the regex that identifies the from clause. This is used in alias detection.

See #2508, #2260.
  • Loading branch information
darinmanica authored and gregturn committed May 5, 2022
1 parent 0549fcc commit e549391
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public abstract class QueryUtils {
static {

StringBuilder builder = new StringBuilder();
builder.append("(?<=from)"); // from as starting delimiter
builder.append("(?<=\\bfrom)"); // from as starting delimiter
builder.append("(?:\\s)+"); // at least one space separating
builder.append(IDENTIFIER_GROUP); // Entity name, can be qualified (any
builder.append("(?:\\sas)*"); // exclude possible "as" keyword
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ void detectsAliasCorrectly() {
assertThat(detectAlias(
"(from Foo f max(f) ((((select * from Foo f2 (from Foo f3) max(*)) (from Foo f4)) max(f5)) (f6)) (from Foo f7))"))
.isEqualTo("f");
assertThat(detectAlias(
"SELECT e FROM DbEvent e WHERE (CAST(:modifiedFrom AS date) IS NULL OR e.modificationDate >= :modifiedFrom)"))
.isEqualTo("e");
assertThat(detectAlias("from User u where (cast(:effective as date) is null) OR :effective >= u.createdAt"))
.isEqualTo("u");
assertThat(detectAlias("from User u where (cast(:effectiveDate as date) is null) OR :effectiveDate >= u.createdAt"))
.isEqualTo("u");
assertThat(detectAlias("from User u where (cast(:effectiveFrom as date) is null) OR :effectiveFrom >= u.createdAt"))
.isEqualTo("u");
assertThat(
detectAlias("from User u where (cast(:e1f2f3ectiveFrom as date) is null) OR :effectiveFrom >= u.createdAt"))
.isEqualTo("u");
}

@Test // GH-2260
Expand Down

0 comments on commit e549391

Please sign in to comment.