Skip to content

Commit 1534c87

Browse files
gregturnmp911de
authored andcommitted
Support functions that have semicolons as arguments.
A native SQL function with ';' as an argument, e.g. listagg(a.b, ';'), will fail to find anything after it (like an alias). By adding ';' to the list of approved function tokens, queries with functional aliases will work properly. Resolves #2884. Original pull request: #2891
1 parent 8b07198 commit 1534c87

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public abstract class QueryUtils {
186186

187187
builder = new StringBuilder();
188188
// any function call including parameters within the brackets
189-
builder.append("\\w+\\s*\\([\\w\\.,\\s'=:\\\\?]+\\)");
189+
builder.append("\\w+\\s*\\([\\w\\.,\\s'=:;\\\\?]+\\)");
190190
// the potential alias
191191
builder.append("\\s+[as|AS]+\\s+(([\\w\\.]+))");
192192

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

+28-8
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,8 @@
1515
*/
1616
package org.springframework.data.jpa.repository.query;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
19-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20-
import static org.springframework.data.jpa.repository.query.QueryUtils.applySorting;
21-
import static org.springframework.data.jpa.repository.query.QueryUtils.createCountQueryFor;
22-
import static org.springframework.data.jpa.repository.query.QueryUtils.detectAlias;
23-
import static org.springframework.data.jpa.repository.query.QueryUtils.getOuterJoinAliases;
24-
import static org.springframework.data.jpa.repository.query.QueryUtils.hasConstructorExpression;
25-
import static org.springframework.data.jpa.repository.query.QueryUtils.removeSubqueries;
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.springframework.data.jpa.repository.query.QueryUtils.*;
2620

2721
import java.util.Collections;
2822
import java.util.Set;
@@ -891,4 +885,30 @@ void orderByShouldWorkWithSubSelectStatements() {
891885
+ ") as timestamp\n" //
892886
+ "from foo f", sort)).endsWith("order by f.age desc");
893887
}
888+
889+
@Test // GH-2884
890+
void functionAliasShouldSupportArgumentsWithCommasOrArgumentsWithSemiColons() {
891+
892+
assertThat(QueryUtils.getFunctionAliases("""
893+
select s.id as id, s.name as name, gp.points
894+
from specialist s
895+
left join (
896+
select q.specialist_id, listagg(q.points, ',') as points
897+
from qualification q
898+
group by q.specialist_id
899+
) gp on gp.specialist_id = s.id
900+
where name like :name
901+
""")).containsExactly("points");
902+
903+
assertThat(QueryUtils.getFunctionAliases("""
904+
select s.id as id, s.name as name, gp.points
905+
from specialist s
906+
left join (
907+
select q.specialist_id, listagg(q.points, ';') as points
908+
from qualification q
909+
group by q.specialist_id
910+
) gp on gp.specialist_id = s.id
911+
where name like :name
912+
""")).containsExactly("points");
913+
}
894914
}

0 commit comments

Comments
 (0)