You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "jpountz (via GitHub)" <gi...@apache.org> on 2025/03/28 20:30:19 UTC

[PR] Fix HistogramCollector to not create zero-count buckets. [lucene]

jpountz opened a new pull request, #14421:
URL: https://github.com/apache/lucene/pull/14421

   If a bucket in the middle of the range doesn't match docs, it would be returned with a count of zero. Better not return it at all.


-- 
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-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Fix HistogramCollector to not create zero-count buckets. [lucene]

Posted by "jainankitk (via GitHub)" <gi...@apache.org>.
jainankitk commented on code in PR #14421:
URL: https://github.com/apache/lucene/pull/14421#discussion_r2019647007


##########
lucene/sandbox/src/test/org/apache/lucene/sandbox/facet/plain/histograms/TestHistogramCollectorManager.java:
##########
@@ -137,6 +140,23 @@ private void doTestSkipIndex(IndexWriterConfig cfg) throws IOException {
         IllegalStateException.class,
         () -> searcher.search(new MatchAllDocsQuery(), new HistogramCollectorManager("f", 4, 1)));
 
+    // Create a query so that bucket "1" (values from 4 to 8), which is in the middle of the range,
+    // doesn't match any docs. HistogramCollector should not add an entry with a count of 0 in this
+    // case.
+    Query query =
+        new BooleanQuery.Builder()
+            .add(NumericDocValuesField.newSlowRangeQuery("f", Long.MIN_VALUE, 2), Occur.SHOULD)
+            .add(NumericDocValuesField.newSlowRangeQuery("f", 10, Long.MAX_VALUE), Occur.SHOULD)
+            .build();
+    actualCounts = searcher.search(query, new HistogramCollectorManager("f", 4));

Review Comment:
   Initially, I was wondering if `checkMaxBuckets(collectorCounts.size(), maxBuckets)` might cause some of the tests earlier expecting exception to fail. But, all the tests with `counts[i] == 0`, use `1024` as `maxBuckets` which is comfortably over `collectorCounts.size()`
   
   Maybe, we can specify 3 as `maxBuckets` for even stronger condition:
   
   ```
   searcher.search(query, new HistogramCollectorManager("f", 4, 3));
   ```



##########
lucene/sandbox/src/java/org/apache/lucene/sandbox/facet/plain/histograms/HistogramCollector.java:
##########
@@ -279,7 +279,9 @@ public void collect(DocIdStream stream) throws IOException {
     public void finish() throws IOException {
       // Put counts that we computed in the int[] back into the hash map.
       for (int i = 0; i < counts.length; ++i) {
-        collectorCounts.addTo(leafMinBucket + i, counts[i]);
+        if (counts[i] != 0) {
+          collectorCounts.addTo(leafMinBucket + i, counts[i]);
+        }
       }
       checkMaxBuckets(collectorCounts.size(), maxBuckets);

Review Comment:
   While unrelated to this change, I am wondering if we should check eagerly to prevent unnecessary iterations:
   
   ```
   if (counts[i] != 0) {
     collectorCounts.addTo(leafMinBucket + i, counts[i]);
     checkMaxBuckets(collectorCounts.size(), maxBuckets);
   }
   ```
   
   We are doing similar validation in other places:
   
   ```
   if (bucket != prevBucket) {
     counts.addTo(bucket, 1);
     checkMaxBuckets(counts.size(), maxBuckets);
     prevBucket = bucket;
   }
   ```
   
   



-- 
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-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Fix HistogramCollector to not create zero-count buckets. [lucene]

Posted by "jpountz (via GitHub)" <gi...@apache.org>.
jpountz merged PR #14421:
URL: https://github.com/apache/lucene/pull/14421


-- 
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-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Fix HistogramCollector to not create zero-count buckets. [lucene]

Posted by "jpountz (via GitHub)" <gi...@apache.org>.
jpountz commented on PR #14421:
URL: https://github.com/apache/lucene/pull/14421#issuecomment-2766465560

   I backported to branch_10_2 since this is a bugfix cc @iverase 


-- 
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-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Fix HistogramCollector to not create zero-count buckets. [lucene]

Posted by "jpountz (via GitHub)" <gi...@apache.org>.
jpountz commented on code in PR #14421:
URL: https://github.com/apache/lucene/pull/14421#discussion_r2019755410


##########
lucene/sandbox/src/java/org/apache/lucene/sandbox/facet/plain/histograms/HistogramCollector.java:
##########
@@ -279,7 +279,9 @@ public void collect(DocIdStream stream) throws IOException {
     public void finish() throws IOException {
       // Put counts that we computed in the int[] back into the hash map.
       for (int i = 0; i < counts.length; ++i) {
-        collectorCounts.addTo(leafMinBucket + i, counts[i]);
+        if (counts[i] != 0) {
+          collectorCounts.addTo(leafMinBucket + i, counts[i]);
+        }
       }
       checkMaxBuckets(collectorCounts.size(), maxBuckets);

Review Comment:
   I think it's ok the way it is. The end goal is to prevent unbounded heap allocation. In this case, the amount of excess heap we may allocate is bounded by 1024 entries, so I'd err on the side of simplicity by not checking the number of buckets in the loop?



-- 
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-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org


Re: [PR] Fix HistogramCollector to not create zero-count buckets. [lucene]

Posted by "jainankitk (via GitHub)" <gi...@apache.org>.
jainankitk commented on code in PR #14421:
URL: https://github.com/apache/lucene/pull/14421#discussion_r2020439736


##########
lucene/sandbox/src/java/org/apache/lucene/sandbox/facet/plain/histograms/HistogramCollector.java:
##########
@@ -279,7 +279,9 @@ public void collect(DocIdStream stream) throws IOException {
     public void finish() throws IOException {
       // Put counts that we computed in the int[] back into the hash map.
       for (int i = 0; i < counts.length; ++i) {
-        collectorCounts.addTo(leafMinBucket + i, counts[i]);
+        if (counts[i] != 0) {
+          collectorCounts.addTo(leafMinBucket + i, counts[i]);
+        }
       }
       checkMaxBuckets(collectorCounts.size(), maxBuckets);

Review Comment:
   Sounds fair. I was expecting low bound like 1024, just wanted to confirm!



-- 
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-unsubscribe@lucene.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org