From 03b2b5e2bed08cdaa0090c393cba49678e6fff49 Mon Sep 17 00:00:00 2001 From: Nabarun Date: Thu, 11 Dec 2025 17:59:48 +0530 Subject: [PATCH 1/2] test: add assertion for collection filtered with pagination offset --- types/query/collections_pagination_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/types/query/collections_pagination_test.go b/types/query/collections_pagination_test.go index 87cfc549a9..fc2f905823 100644 --- a/types/query/collections_pagination_test.go +++ b/types/query/collections_pagination_test.go @@ -154,6 +154,23 @@ func TestCollectionPagination(t *testing.T) { {Key: 295, Value: 295}, }, }, + "filtered with offset": { + req: &PageRequest{ + Offset: 3, + Limit: 3, + }, + expResp: &PageResponse{ + NextKey: encodeKey(12), + }, + filter: func(key, value uint64) (bool, error) { + return key%2 == 0, nil + }, + expResults: []collections.KeyValue[uint64, uint64]{ + {Key: 6, Value: 6}, + {Key: 8, Value: 8}, + {Key: 10, Value: 10}, + }, + }, "filtered no key with error": { req: &PageRequest{ Limit: 3, -- 2.45.2 From 50d50e6c2a7bbeea8990d1d5b316376b12ee6bc3 Mon Sep 17 00:00:00 2001 From: Nabarun Date: Fri, 12 Dec 2025 11:06:11 +0530 Subject: [PATCH 2/2] fix: pagination offset when filtering without key --- types/query/collections_pagination.go | 30 +++++++++------------- types/query/collections_pagination_test.go | 6 +++-- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/types/query/collections_pagination.go b/types/query/collections_pagination.go index 1c9babcc6a..4edea99794 100644 --- a/types/query/collections_pagination.go +++ b/types/query/collections_pagination.go @@ -128,13 +128,10 @@ func collFilteredPaginateNoKey[K, V any, C Collection[K, V], T any]( return nil, nil, err } defer iterator.Close() - // we advance the iter equal to the provided offset - if !advanceIter(iterator, offset) { - return nil, nil, collections.ErrInvalidIterator - } var ( count uint64 + skipped uint64 nextKey []byte results []T ) @@ -149,6 +146,11 @@ func collFilteredPaginateNoKey[K, V any, C Collection[K, V], T any]( } // if no predicate function is specified then we just include the result if predicateFunc == nil { + if skipped < offset { + skipped++ + continue + } + transformed, err := transformFunc(kv.Key, kv.Value) if err != nil { return nil, nil, err @@ -163,6 +165,12 @@ func collFilteredPaginateNoKey[K, V any, C Collection[K, V], T any]( return nil, nil, err } if include { + // Item matches filter - check if we need to skip it for offset + if skipped < offset { + skipped++ + continue + } + transformed, err := transformFunc(kv.Key, kv.Value) if err != nil { return nil, nil, err @@ -224,20 +232,6 @@ func collFilteredPaginateNoKey[K, V any, C Collection[K, V], T any]( return results, resp, nil } -func advanceIter[I interface { - Next() - Valid() bool -}](iter I, offset uint64, -) bool { - for i := uint64(0); i < offset; i++ { - if !iter.Valid() { - return false - } - iter.Next() - } - return true -} - // collFilteredPaginateByKey paginates a collection when a starting key // is provided in the PageRequest. Predicate is applied only if not nil. func collFilteredPaginateByKey[K, V any, C Collection[K, V], T any]( diff --git a/types/query/collections_pagination_test.go b/types/query/collections_pagination_test.go index fc2f905823..3455776132 100644 --- a/types/query/collections_pagination_test.go +++ b/types/query/collections_pagination_test.go @@ -156,11 +156,13 @@ func TestCollectionPagination(t *testing.T) { }, "filtered with offset": { req: &PageRequest{ - Offset: 3, - Limit: 3, + Offset: 3, + Limit: 3, + CountTotal: true, }, expResp: &PageResponse{ NextKey: encodeKey(12), + Total: 150, }, filter: func(key, value uint64) (bool, error) { return key%2 == 0, nil -- 2.45.2