You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2022/09/23 09:12:57 UTC

[GitHub] [pinot] Jackie-Jiang opened a new pull request, #9453: Fix the range check for range index on raw column

Jackie-Jiang opened a new pull request, #9453:
URL: https://github.com/apache/pinot/pull/9453

   When range index is applied to a raw (no-dictionary) column, we need to check `max >= _min` before reading the range bitmap. Failed to do so will result in matching all docs instead of no docs as expected (note that negative `max - _min` will be treated as a huge unsigned long).
   
   This PR fixes the range check, and also some minor fix/enhancement:
   - Handle the case of `_max` value not available from the metadata
   - Do not read range bitmap when not needed
   
   During this bug fix, found a bug in RangeIndex and tracked under the issue: https://github.com/RoaringBitmap/RoaringBitmap/issues/586


-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #9453: Fix the range check for range index on raw column

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #9453:
URL: https://github.com/apache/pinot/pull/9453#discussion_r978444007


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/readers/BitSlicedRangeIndexReader.java:
##########
@@ -49,49 +48,85 @@ public BitSlicedRangeIndexReader(PinotDataBuffer dataBuffer,
     _min = dataBuffer.getLong(offset);
     offset += Long.BYTES;
     _offset = offset;
-    _max = metadata.hasDictionary()
-        ? metadata.getCardinality() - 1
-        : ((Number) metadata.getMaxValue()).longValue();
+    // TODO: Read max from header to prevent cases where max value is not available in the column metadata
+    if (metadata.hasDictionary()) {
+      _max = metadata.getCardinality() - 1;
+    } else {
+      Number maxValue = (Number) metadata.getMaxValue();
+      _max = maxValue != null ? maxValue.longValue() : Long.MAX_VALUE;
+    }
     _numDocs = metadata.getTotalDocs();
   }
 
   @Override
   public int getNumMatchingDocs(int min, int max) {
+    // TODO: Handle this before reading the range index
+    if (min > max || min > _max || max < _min) {
+      return 0;
+    }

Review Comment:
   The check for `min > _max || max < _min` is critical and can only be performed here for raw columns. However, ranges with `min > max` making it down to this level is a problem with the query planner and should be fixed there (even on the broker)



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] Jackie-Jiang commented on a diff in pull request #9453: Fix the range check for range index on raw column

Posted by GitBox <gi...@apache.org>.
Jackie-Jiang commented on code in PR #9453:
URL: https://github.com/apache/pinot/pull/9453#discussion_r978447120


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/readers/BitSlicedRangeIndexReader.java:
##########
@@ -122,35 +157,39 @@ public ImmutableRoaringBitmap getPartiallyMatchingDocIds(double min, double max)
   }
 
   private ImmutableRoaringBitmap queryRangeBitmap(long min, long max, long columnMax) {
-    RangeBitmap rangeBitmap = mapRangeBitmap();
-    if (Long.compareUnsigned(max, columnMax) < 0) {
-      if (Long.compareUnsigned(min, 0) > 0) {
-        return rangeBitmap.between(min, max).toMutableRoaringBitmap();
-      }
-      return rangeBitmap.lte(max).toMutableRoaringBitmap();
-    } else {
-      if (Long.compareUnsigned(min, 0) > 0) {
-        return rangeBitmap.gte(min).toMutableRoaringBitmap();
-      }
+    // Compare unsigned
+    boolean lowerUnbounded = min + Long.MIN_VALUE <= Long.MIN_VALUE;
+    boolean upperUnbounded = max + Long.MIN_VALUE >= columnMax + Long.MIN_VALUE;
+    if (lowerUnbounded && upperUnbounded) {
       MutableRoaringBitmap all = new MutableRoaringBitmap();
-      all.add(0, _numDocs);
+      all.add(0L, _numDocs);
       return all;
     }
+    RangeBitmap rangeBitmap = mapRangeBitmap();
+    if (lowerUnbounded) {
+      return rangeBitmap.lte(max).toMutableRoaringBitmap();
+    }
+    if (upperUnbounded) {
+      return rangeBitmap.gte(min).toMutableRoaringBitmap();
+    }
+    return rangeBitmap.between(min, max).toMutableRoaringBitmap();

Review Comment:
   It will improve the case of full match, where we don't need to read the range bitmap. But agree we can focus on just the bugfix in this PR



##########
pinot-segment-local/src/test/java/org/apache/pinot/segment/local/segment/index/creator/BitSlicedIndexCreatorTest.java:
##########
@@ -250,24 +276,37 @@ private void testDouble(Dataset<double[]> dataset)
     File rangeIndexFile = new File(INDEX_DIR, metadata.getColumnName() + BITMAP_RANGE_INDEX_FILE_EXTENSION);
     try (PinotDataBuffer dataBuffer = PinotDataBuffer.mapReadOnlyBigEndianFile(rangeIndexFile)) {
       BitSlicedRangeIndexReader reader = new BitSlicedRangeIndexReader(dataBuffer, metadata);
-      double prev = Double.NEGATIVE_INFINITY;
-      for (double quantile : dataset.quantiles()) {
-        ImmutableRoaringBitmap reference = dataset.scan(prev, quantile);
-        ImmutableRoaringBitmap result = reader.getMatchingDocIds(prev, quantile);
-        int resultCount = reader.getNumMatchingDocs(prev, quantile);
-        assertEquals(result, reference);
-        assertEquals(resultCount, result == null ? 0 : result.getCardinality());
+      double[] quantiles = dataset.quantiles();
+      // TODO: RangeBitmap has a bug in version 0.9.28 which gives wrong result computing between for 2 doubles with
+      //       different sign. The bug is tracked here: https://github.com/RoaringBitmap/RoaringBitmap/issues/586.
+      //       Uncomment this line after the bug is fixed.
+//      double prev = quantiles[0] - 1;

Review Comment:
   Sounds good



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #9453: Fix the range check for range index on raw column

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #9453:
URL: https://github.com/apache/pinot/pull/9453#discussion_r978437739


##########
pinot-segment-local/src/test/java/org/apache/pinot/segment/local/segment/index/creator/BitSlicedIndexCreatorTest.java:
##########
@@ -250,24 +276,37 @@ private void testDouble(Dataset<double[]> dataset)
     File rangeIndexFile = new File(INDEX_DIR, metadata.getColumnName() + BITMAP_RANGE_INDEX_FILE_EXTENSION);
     try (PinotDataBuffer dataBuffer = PinotDataBuffer.mapReadOnlyBigEndianFile(rangeIndexFile)) {
       BitSlicedRangeIndexReader reader = new BitSlicedRangeIndexReader(dataBuffer, metadata);
-      double prev = Double.NEGATIVE_INFINITY;
-      for (double quantile : dataset.quantiles()) {
-        ImmutableRoaringBitmap reference = dataset.scan(prev, quantile);
-        ImmutableRoaringBitmap result = reader.getMatchingDocIds(prev, quantile);
-        int resultCount = reader.getNumMatchingDocs(prev, quantile);
-        assertEquals(result, reference);
-        assertEquals(resultCount, result == null ? 0 : result.getCardinality());
+      double[] quantiles = dataset.quantiles();
+      // TODO: RangeBitmap has a bug in version 0.9.28 which gives wrong result computing between for 2 doubles with
+      //       different sign. The bug is tracked here: https://github.com/RoaringBitmap/RoaringBitmap/issues/586.
+      //       Uncomment this line after the bug is fixed.
+//      double prev = quantiles[0] - 1;

Review Comment:
   A temporary workaround is provided on the issue, I suggest applying the suggestion in this PR and revisit this when the bug is fixed (though that should be soon).



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #9453: Fix the range check for range index on raw column

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #9453:
URL: https://github.com/apache/pinot/pull/9453#discussion_r978569589


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/readers/BitSlicedRangeIndexReader.java:
##########
@@ -122,35 +157,39 @@ public ImmutableRoaringBitmap getPartiallyMatchingDocIds(double min, double max)
   }
 
   private ImmutableRoaringBitmap queryRangeBitmap(long min, long max, long columnMax) {
-    RangeBitmap rangeBitmap = mapRangeBitmap();
-    if (Long.compareUnsigned(max, columnMax) < 0) {
-      if (Long.compareUnsigned(min, 0) > 0) {
-        return rangeBitmap.between(min, max).toMutableRoaringBitmap();
-      }
-      return rangeBitmap.lte(max).toMutableRoaringBitmap();
-    } else {
-      if (Long.compareUnsigned(min, 0) > 0) {
-        return rangeBitmap.gte(min).toMutableRoaringBitmap();
-      }
+    // Compare unsigned
+    boolean lowerUnbounded = min + Long.MIN_VALUE <= Long.MIN_VALUE;
+    boolean upperUnbounded = max + Long.MIN_VALUE >= columnMax + Long.MIN_VALUE;
+    if (lowerUnbounded && upperUnbounded) {
       MutableRoaringBitmap all = new MutableRoaringBitmap();
-      all.add(0, _numDocs);
+      all.add(0L, _numDocs);
       return all;
     }
+    RangeBitmap rangeBitmap = mapRangeBitmap();
+    if (lowerUnbounded) {
+      return rangeBitmap.lte(max).toMutableRoaringBitmap();
+    }
+    if (upperUnbounded) {
+      return rangeBitmap.gte(min).toMutableRoaringBitmap();
+    }
+    return rangeBitmap.between(min, max).toMutableRoaringBitmap();

Review Comment:
   The data structure already detects these cases and skips them, the mapping should take ~10ns and eliminating that should be balanced against needing to read the code



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #9453: Fix the range check for range index on raw column

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #9453:
URL: https://github.com/apache/pinot/pull/9453#discussion_r978570681


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/readers/BitSlicedRangeIndexReader.java:
##########
@@ -49,49 +48,85 @@ public BitSlicedRangeIndexReader(PinotDataBuffer dataBuffer,
     _min = dataBuffer.getLong(offset);
     offset += Long.BYTES;
     _offset = offset;
-    _max = metadata.hasDictionary()
-        ? metadata.getCardinality() - 1
-        : ((Number) metadata.getMaxValue()).longValue();
+    // TODO: Read max from header to prevent cases where max value is not available in the column metadata
+    if (metadata.hasDictionary()) {
+      _max = metadata.getCardinality() - 1;
+    } else {
+      Number maxValue = (Number) metadata.getMaxValue();
+      _max = maxValue != null ? maxValue.longValue() : Long.MAX_VALUE;
+    }
     _numDocs = metadata.getTotalDocs();
   }
 
   @Override
   public int getNumMatchingDocs(int min, int max) {
+    // TODO: Handle this before reading the range index
+    if (min > max || min > _max || max < _min) {
+      return 0;
+    }

Review Comment:
   Indeed, I don’t think the index should need to do these checks at all, but if you need a quick fix maybe this is the quick solution.



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] Jackie-Jiang merged pull request #9453: Fix the range check for range index on raw column

Posted by GitBox <gi...@apache.org>.
Jackie-Jiang merged PR #9453:
URL: https://github.com/apache/pinot/pull/9453


-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] Jackie-Jiang commented on a diff in pull request #9453: Fix the range check for range index on raw column

Posted by GitBox <gi...@apache.org>.
Jackie-Jiang commented on code in PR #9453:
URL: https://github.com/apache/pinot/pull/9453#discussion_r978449717


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/readers/BitSlicedRangeIndexReader.java:
##########
@@ -49,49 +48,85 @@ public BitSlicedRangeIndexReader(PinotDataBuffer dataBuffer,
     _min = dataBuffer.getLong(offset);
     offset += Long.BYTES;
     _offset = offset;
-    _max = metadata.hasDictionary()
-        ? metadata.getCardinality() - 1
-        : ((Number) metadata.getMaxValue()).longValue();
+    // TODO: Read max from header to prevent cases where max value is not available in the column metadata
+    if (metadata.hasDictionary()) {
+      _max = metadata.getCardinality() - 1;
+    } else {
+      Number maxValue = (Number) metadata.getMaxValue();
+      _max = maxValue != null ? maxValue.longValue() : Long.MAX_VALUE;
+    }
     _numDocs = metadata.getTotalDocs();
   }
 
   @Override
   public int getNumMatchingDocs(int min, int max) {
+    // TODO: Handle this before reading the range index
+    if (min > max || min > _max || max < _min) {
+      return 0;
+    }

Review Comment:
   Ideally we should also handle `min > _max || max < _min` using the column metadata during the planning



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #9453: Fix the range check for range index on raw column

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #9453:
URL: https://github.com/apache/pinot/pull/9453#discussion_r978436827


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/readers/BitSlicedRangeIndexReader.java:
##########
@@ -122,35 +157,39 @@ public ImmutableRoaringBitmap getPartiallyMatchingDocIds(double min, double max)
   }
 
   private ImmutableRoaringBitmap queryRangeBitmap(long min, long max, long columnMax) {
-    RangeBitmap rangeBitmap = mapRangeBitmap();
-    if (Long.compareUnsigned(max, columnMax) < 0) {
-      if (Long.compareUnsigned(min, 0) > 0) {
-        return rangeBitmap.between(min, max).toMutableRoaringBitmap();
-      }
-      return rangeBitmap.lte(max).toMutableRoaringBitmap();
-    } else {
-      if (Long.compareUnsigned(min, 0) > 0) {
-        return rangeBitmap.gte(min).toMutableRoaringBitmap();
-      }
+    // Compare unsigned
+    boolean lowerUnbounded = min + Long.MIN_VALUE <= Long.MIN_VALUE;
+    boolean upperUnbounded = max + Long.MIN_VALUE >= columnMax + Long.MIN_VALUE;
+    if (lowerUnbounded && upperUnbounded) {
       MutableRoaringBitmap all = new MutableRoaringBitmap();
-      all.add(0, _numDocs);
+      all.add(0L, _numDocs);
       return all;
     }
+    RangeBitmap rangeBitmap = mapRangeBitmap();
+    if (lowerUnbounded) {
+      return rangeBitmap.lte(max).toMutableRoaringBitmap();
+    }
+    if (upperUnbounded) {
+      return rangeBitmap.gte(min).toMutableRoaringBitmap();
+    }
+    return rangeBitmap.between(min, max).toMutableRoaringBitmap();
   }
 
   private int queryRangeBitmapCardinality(long min, long max, long columnMax) {
+    // Compare unsigned
+    boolean lowerUnbounded = min + Long.MIN_VALUE <= Long.MIN_VALUE;
+    boolean upperUnbounded = max + Long.MIN_VALUE >= columnMax + Long.MIN_VALUE;
+    if (lowerUnbounded && upperUnbounded) {
+      return _numDocs;
+    }
     RangeBitmap rangeBitmap = mapRangeBitmap();
-    if (Long.compareUnsigned(max, columnMax) < 0) {
-      if (Long.compareUnsigned(min, 0) > 0) {
-        return (int) rangeBitmap.betweenCardinality(min, max);
-      }
+    if (lowerUnbounded) {
       return (int) rangeBitmap.lteCardinality(max);
-    } else {
-      if (Long.compareUnsigned(min, 0) > 0) {
-        return (int) rangeBitmap.gteCardinality(min);
-      }
-      return (int) _numDocs;
     }
+    if (upperUnbounded) {
+      return (int) rangeBitmap.gteCardinality(min);
+    }
+    return (int) rangeBitmap.betweenCardinality(min, max);

Review Comment:
   These are non-functional changes and I don't think they are improvements, I suggest removing them to remove noise in the PR (which is an important bug-fix)



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org


[GitHub] [pinot] richardstartin commented on a diff in pull request #9453: Fix the range check for range index on raw column

Posted by GitBox <gi...@apache.org>.
richardstartin commented on code in PR #9453:
URL: https://github.com/apache/pinot/pull/9453#discussion_r978436582


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/readers/BitSlicedRangeIndexReader.java:
##########
@@ -122,35 +157,39 @@ public ImmutableRoaringBitmap getPartiallyMatchingDocIds(double min, double max)
   }
 
   private ImmutableRoaringBitmap queryRangeBitmap(long min, long max, long columnMax) {
-    RangeBitmap rangeBitmap = mapRangeBitmap();
-    if (Long.compareUnsigned(max, columnMax) < 0) {
-      if (Long.compareUnsigned(min, 0) > 0) {
-        return rangeBitmap.between(min, max).toMutableRoaringBitmap();
-      }
-      return rangeBitmap.lte(max).toMutableRoaringBitmap();
-    } else {
-      if (Long.compareUnsigned(min, 0) > 0) {
-        return rangeBitmap.gte(min).toMutableRoaringBitmap();
-      }
+    // Compare unsigned
+    boolean lowerUnbounded = min + Long.MIN_VALUE <= Long.MIN_VALUE;
+    boolean upperUnbounded = max + Long.MIN_VALUE >= columnMax + Long.MIN_VALUE;
+    if (lowerUnbounded && upperUnbounded) {
       MutableRoaringBitmap all = new MutableRoaringBitmap();
-      all.add(0, _numDocs);
+      all.add(0L, _numDocs);
       return all;
     }
+    RangeBitmap rangeBitmap = mapRangeBitmap();
+    if (lowerUnbounded) {
+      return rangeBitmap.lte(max).toMutableRoaringBitmap();
+    }
+    if (upperUnbounded) {
+      return rangeBitmap.gte(min).toMutableRoaringBitmap();
+    }
+    return rangeBitmap.between(min, max).toMutableRoaringBitmap();

Review Comment:
   These are non-functional changes and I don't think they are improvements, I suggest removing them to remove noise in the PR (which is an important bug-fix)



-- 
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: commits-unsubscribe@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org