Tan-JiaLiang commented on code in PR #6028: URL: https://github.com/apache/paimon/pull/6028#discussion_r2269130818
########## paimon-common/src/main/java/org/apache/paimon/fileindex/rangebitmap/BitSliceIndexBitmap.java: ########## @@ -151,7 +151,84 @@ public RoaringBitmap32 gte(int code) { return gt(code - 1); } + public RoaringBitmap32 topK(int k, @Nullable RoaringBitmap32 foundSet) { + if (k == 0 || (foundSet != null && foundSet.isEmpty())) { + return new RoaringBitmap32(); + } + + if (k < 0) { + throw new IllegalArgumentException("the k param can not be negative in topK, k=" + k); + } + + RoaringBitmap32 g = new RoaringBitmap32(); + RoaringBitmap32 e = isNotNull(foundSet); + if (e.getCardinality() <= k) { + return e; + } + + loadSlices(0, slices.length); + for (int i = slices.length - 1; i >= 0; i--) { + RoaringBitmap32 x = RoaringBitmap32.or(g, RoaringBitmap32.and(e, getSlice(i))); + long n = x.getCardinality(); + if (n > k) { + e = RoaringBitmap32.and(e, getSlice(i)); + } else if (n < k) { + g = x; + e = RoaringBitmap32.andNot(e, getSlice(i)); + } else { + e = RoaringBitmap32.and(e, getSlice(i)); + break; + } + } + + // only k results should be returned + RoaringBitmap32 f = RoaringBitmap32.or(g, e); + f.remove(e, f.getCardinality() - k); Review Comment: RoaringBitmap32#remove will return directly if the `f.getCardinality() - k` is negative. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@paimon.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org