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.
This commit is contained in:
Darin Manica
2022-05-05 06:22:39 -06:00
committed by Greg L. Turnquist
parent 28a22830bd
commit cd27f03915
2 changed files with 13 additions and 1 deletions

View File

@@ -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

View File

@@ -130,6 +130,18 @@ class QueryUtilsUnitTests {
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