Skip to content

Commit dc89da4

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 f45f534 commit dc89da4

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
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
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.jpa.repository.query;
1717

18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.springframework.data.jpa.repository.query.QueryUtils.*;
1820
import static org.assertj.core.api.Assertions.assertThat;
1921
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2022
import static org.springframework.data.jpa.repository.query.QueryUtils.*;
@@ -886,4 +888,30 @@ void orderByShouldWorkWithSubSelectStatements() {
886888
+ ") as timestamp\n" //
887889
+ "from foo f", sort)).endsWith("order by f.age desc");
888890
}
891+
892+
@Test // GH-2884
893+
void functionAliasShouldSupportArgumentsWithCommasOrArgumentsWithSemiColons() {
894+
895+
assertThat(QueryUtils.getFunctionAliases("""
896+
select s.id as id, s.name as name, gp.points
897+
from specialist s
898+
left join (
899+
select q.specialist_id, listagg(q.points, ',') as points
900+
from qualification q
901+
group by q.specialist_id
902+
) gp on gp.specialist_id = s.id
903+
where name like :name
904+
""")).containsExactly("points");
905+
906+
assertThat(QueryUtils.getFunctionAliases("""
907+
select s.id as id, s.name as name, gp.points
908+
from specialist s
909+
left join (
910+
select q.specialist_id, listagg(q.points, ';') as points
911+
from qualification q
912+
group by q.specialist_id
913+
) gp on gp.specialist_id = s.id
914+
where name like :name
915+
""")).containsExactly("points");
916+
}
889917
}

0 commit comments

Comments
 (0)