diff --git a/sdk/spring/azure-spring-data-cosmos/CHANGELOG.md b/sdk/spring/azure-spring-data-cosmos/CHANGELOG.md index 72d01300e8fa..a1413ed65d9f 100644 --- a/sdk/spring/azure-spring-data-cosmos/CHANGELOG.md +++ b/sdk/spring/azure-spring-data-cosmos/CHANGELOG.md @@ -8,6 +8,9 @@ #### Bugs Fixed +* 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). +* Also fixed the same bug for sum query. + #### Other Changes ### 7.0.0 (2026-02-03) diff --git a/sdk/spring/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/repository/support/StringBasedCosmosQuery.java b/sdk/spring/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/repository/support/StringBasedCosmosQuery.java index 713a7bf1a486..80b3ca48e591 100644 --- a/sdk/spring/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/repository/support/StringBasedCosmosQuery.java +++ b/sdk/spring/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/repository/support/StringBasedCosmosQuery.java @@ -30,8 +30,8 @@ * Cosmos query class to handle the annotated queries. This overrides the execution and runs the query directly */ public class StringBasedCosmosQuery extends AbstractCosmosQuery { - private static final Pattern COUNT_QUERY_PATTERN = Pattern.compile("^\\s*select\\s+value\\s+count.*", Pattern.CASE_INSENSITIVE); - private static final Pattern SUM_QUERY_PATTERN = Pattern.compile("^\\s*select\\s+value\\s+sum.*", Pattern.CASE_INSENSITIVE); + private static final Pattern COUNT_QUERY_PATTERN = Pattern.compile("^\\s*select\\s+value\\s+count.*", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); + private static final Pattern SUM_QUERY_PATTERN = Pattern.compile("^\\s*select\\s+value\\s+sum.*", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); private final String query; diff --git a/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/repository/support/StringBasedCosmosQueryUnitTest.java b/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/repository/support/StringBasedCosmosQueryUnitTest.java index 081296b18a7e..8de4ade0e9c0 100644 --- a/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/repository/support/StringBasedCosmosQueryUnitTest.java +++ b/sdk/spring/azure-spring-data-cosmos/src/test/java/com/azure/spring/data/cosmos/repository/support/StringBasedCosmosQueryUnitTest.java @@ -48,4 +48,28 @@ public void testStripExtraWhitespaceFromString() throws NoSuchMethodException, I String result3 = (String) method.invoke(sbcq, args3); assertThat(result3).isEqualTo(expectedResult); } + + @Test + public void testTextBlockCountQuery() { + String countQuery = """ + SELECT VALUE COUNT(1) + FROM a + WHERE a.city = @city + AND a.state = @state + """; + boolean result = StringBasedCosmosQuery.isCountQuery(countQuery, long.class); + assertThat(result).isTrue(); + } + + @Test + public void testTextBlockSumQuery() { + String sumQuery = """ + SELECT VALUE SUM(a.population) + FROM a + WHERE a.city = @city + AND a.state = @state + """; + boolean result = StringBasedCosmosQuery.isSumQuery(sumQuery, long.class); + assertThat(result).isTrue(); + } }