Spring Data Couchbase- @Query SUM

I have this query :

public interface BlockRepository extends PagingAndSortingRepository<Block, String> {

    @Query("SELECT SUM(reward) AS rewards FROM #{#n1ql.bucket} WHERE `generatorPublicKey` = $1 AND `height` BETWEEN $2 AND $3 AND #{#n1ql.filter}")
    long sumOfRewardForDelegate(String delegatePubkey, int fromHeight, int toHeight);

This leads to an error saying I must have __id and __cas. If I add those, I have an error saying it cannot cast a Block into a long.
This is not documented in the spring data doc despite being a pretty standard operation.

Can you walk me through the proper way to do this ?

Versions :
spring-boot-starter-data-couchbase 2.5.3
spring-data-couchbase 4.2.3

This is not currently working.

There is a fix for methods returning types other than those derived from the repository entry (ie. cannot cast a block into a long) in 4.3.0-M3 (today) that is necessary, but not sufficient for this case. The query result is not decoded properly into the long (it complains about not having a satisfactory constructor). I’m looking into that.

As a hack, you can use the count() execution path if you (a) give your query a name starting with ‘count’, and include ‘count(’ in the string. And you’ll need 4.3.0-M3.

	@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
	@Query("SELECT SUM(reward) AS rewards FROM #{#n1ql.bucket} WHERE `generatorPublicKey` = $1 AND `height` BETWEEN $2 AND $3 AND #{#n1ql.filter} and 'count()' = 'count()'")
	Long countEm(String delegatePubkey, int fromHeight, int toHeight);

The fix for this is in 5.0.x - Upgrade Java baseline to Java 17 · Issue #1278 · spring-projects/spring-data-couchbase · GitHub