Skip to content

Commit d3f381d

Browse files
Bug 47910.count query text block (#47911)
* RED: isCountQuery() should return true for queries in text blocks * GREEN: isCountQuery() should return true for queries in text blocks * RED: isSumQuery() should return true for sum query in java text block * GREEN: isSumQuery() should return true for sum query in java text block --------- Co-authored-by: Kushagra Thapar <kuthapar@microsoft.com>
1 parent 55a84bd commit d3f381d

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

sdk/spring/azure-spring-data-cosmos/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
#### Bugs Fixed
1010

11+
* Fixing bug where count query defined in a Java text block in `@Query` causes a class cast exception - See [Bug #47910](https://github.com/Azure/azure-sdk-for-java/issues/47910).
12+
* Also fixed the same bug for sum query.
13+
1114
#### Other Changes
1215

1316
### 7.0.0 (2026-02-03)

sdk/spring/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/repository/support/StringBasedCosmosQuery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
* Cosmos query class to handle the annotated queries. This overrides the execution and runs the query directly
3131
*/
3232
public class StringBasedCosmosQuery extends AbstractCosmosQuery {
33-
private static final Pattern COUNT_QUERY_PATTERN = Pattern.compile("^\\s*select\\s+value\\s+count.*", Pattern.CASE_INSENSITIVE);
34-
private static final Pattern SUM_QUERY_PATTERN = Pattern.compile("^\\s*select\\s+value\\s+sum.*", Pattern.CASE_INSENSITIVE);
33+
private static final Pattern COUNT_QUERY_PATTERN = Pattern.compile("^\\s*select\\s+value\\s+count.*", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
34+
private static final Pattern SUM_QUERY_PATTERN = Pattern.compile("^\\s*select\\s+value\\s+sum.*", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
3535

3636
private final String query;
3737

sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/repository/support/StringBasedCosmosQueryUnitTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,28 @@ public void testStripExtraWhitespaceFromString() throws NoSuchMethodException, I
4848
String result3 = (String) method.invoke(sbcq, args3);
4949
assertThat(result3).isEqualTo(expectedResult);
5050
}
51+
52+
@Test
53+
public void testTextBlockCountQuery() {
54+
String countQuery = """
55+
SELECT VALUE COUNT(1)
56+
FROM a
57+
WHERE a.city = @city
58+
AND a.state = @state
59+
""";
60+
boolean result = StringBasedCosmosQuery.isCountQuery(countQuery, long.class);
61+
assertThat(result).isTrue();
62+
}
63+
64+
@Test
65+
public void testTextBlockSumQuery() {
66+
String sumQuery = """
67+
SELECT VALUE SUM(a.population)
68+
FROM a
69+
WHERE a.city = @city
70+
AND a.state = @state
71+
""";
72+
boolean result = StringBasedCosmosQuery.isSumQuery(sumQuery, long.class);
73+
assertThat(result).isTrue();
74+
}
5175
}

0 commit comments

Comments
 (0)