Skip to content

Commit 0ce8217

Browse files
committed
Enforce limit constraint for queries with more than 1 DNF term.
1 parent aaf3d4f commit 0ce8217

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/IndexedQueryEngine.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,27 @@ private ImmutableSortedMap<DocumentKey, Document> performCollectionQuery(Query q
9292
result = result.insert(entry.getKey(), entry.getValue());
9393
}
9494
}
95-
return result;
95+
96+
// If there's no limit constraint, we can return all the results.
97+
// If the DNF contained only 1 term, the SQLite query has enforced the limit, and we can return
98+
// all the results.
99+
if (target.getLimit() == -1 || target.getDnf().size() == 1) {
100+
return result;
101+
}
102+
103+
// If there's a limit constraint, we should perform all branches of the query (as done above),
104+
// collect the results in a sorted map (as done above), and we need to limit the results here.
105+
long counter = 0;
106+
ImmutableSortedMap<DocumentKey, Document> limitedResult =
107+
ImmutableSortedMap.Builder.emptyMap(DocumentKey.comparator());
108+
for (Map.Entry<DocumentKey, Document> entry : result) {
109+
if (counter == target.getLimit()) {
110+
break;
111+
}
112+
limitedResult = limitedResult.insert(entry.getKey(), entry.getValue());
113+
counter++;
114+
}
115+
return limitedResult;
96116
}
97117

98118
/**

0 commit comments

Comments
 (0)