From 7705edb27a601630ef79a74db4c3f37b3131f194 Mon Sep 17 00:00:00 2001 From: Nabarun Date: Fri, 12 Dec 2025 11:06:11 +0530 Subject: [PATCH] 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