@@ -107,20 +107,46 @@ private void prepareLogicalFilter(LogicalFilter logicalFilter) {
107107 }
108108
109109 private DocumentCursor createCursor (FindPlan findPlan ) {
110- RecordStream <Pair <NitriteId , Document >> recordStream = findSuitableStream (findPlan );
110+ // -1 means "not an index scan"; the index branch records the exact id-set size here.
111+ long [] indexedIdCount = { -1 };
112+ RecordStream <Pair <NitriteId , Document >> recordStream = findSuitableStream (findPlan , indexedIdCount );
111113 DocumentStream cursor = new DocumentStream (recordStream , processorChain );
112114 cursor .setFindPlan (findPlan );
115+ cursor .setCoveredCount (computeCoveredCount (findPlan , indexedIdCount [0 ]));
113116 return cursor ;
114117 }
115118
116- private RecordStream <Pair <NitriteId , Document >> findSuitableStream (FindPlan findPlan ) {
119+ /**
120+ * Returns the exact match count when the query is fully answered without fetching documents,
121+ * or {@code null} when the cursor must be drained to count. The count is exact only when
122+ * nothing downstream drops or changes cardinality (a post-filter, skip, or limit); sort does
123+ * not change the count, and an OR-union needs de-duplication so its count cannot be derived.
124+ */
125+ private Long computeCoveredCount (FindPlan findPlan , long indexedIdCount ) {
126+ if (!findPlan .getSubPlans ().isEmpty ()
127+ || findPlan .getCollectionScanFilter () != null
128+ || findPlan .getSkip () != null
129+ || findPlan .getLimit () != null
130+ || findPlan .getByIdFilter () != null ) {
131+ return null ;
132+ }
133+ if (findPlan .getIndexDescriptor () != null ) {
134+ // the index supplied the exact matching id set
135+ return indexedIdCount >= 0 ? indexedIdCount : null ;
136+ }
137+ // pure full scan over the whole collection
138+ return nitriteMap .size ();
139+ }
140+
141+ private RecordStream <Pair <NitriteId , Document >> findSuitableStream (FindPlan findPlan , long [] indexedIdCount ) {
117142 RecordStream <Pair <NitriteId , Document >> rawStream ;
118143
119144 if (!findPlan .getSubPlans ().isEmpty ()) {
120145 // or filters get all sub stream by finding suitable stream of all sub plans
121146 List <RecordStream <Pair <NitriteId , Document >>> subStreams = new ArrayList <>();
122147 for (FindPlan subPlan : findPlan .getSubPlans ()) {
123- RecordStream <Pair <NitriteId , Document >> suitableStream = findSuitableStream (subPlan );
148+ // a sub-plan's own id count cannot answer the union's count (dedup), so discard it
149+ RecordStream <Pair <NitriteId , Document >> suitableStream = findSuitableStream (subPlan , new long []{ -1 });
124150 subStreams .add (suitableStream );
125151 }
126152
@@ -148,6 +174,10 @@ private RecordStream<Pair<NitriteId, Document>> findSuitableStream(FindPlan find
148174 NitriteIndexer indexer = nitriteConfig .findIndexer (indexDescriptor .getIndexType ());
149175 LinkedHashSet <NitriteId > nitriteIds = indexer .findByFilter (findPlan , nitriteConfig );
150176
177+ // the index supplied the exact matching id set; record its size so a size()
178+ // with no row-dropping step downstream can answer from it without fetching
179+ indexedIdCount [0 ] = nitriteIds .size ();
180+
151181 // create indexed stream from optimized filter
152182 rawStream = new IndexedStream (nitriteIds , nitriteMap );
153183 } else {
0 commit comments