Jackie-Jiang commented on a change in pull request #8148:
URL: https://github.com/apache/pinot/pull/8148#discussion_r806346742



##########
File path: 
pinot-core/src/test/java/org/apache/pinot/core/operator/dociditerators/NotDocIdIteratorTest.java
##########
@@ -0,0 +1,103 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.core.operator.dociditerators;
+
+import org.apache.pinot.core.common.BlockDocIdIterator;
+import org.apache.pinot.segment.spi.Constants;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+
+
+public class NotDocIdIteratorTest {
+  @Test
+  public void testNotDocIdIterator() {
+    // OR result: [0, 1, 2, 4, 5, 6, 8, 10, 13, 15, 16, 17, 18, 19, 20]
+    int[] docIds1 = new int[]{1, 4, 6, 10, 15, 17, 18, 20};
+    int[] docIds2 = new int[]{0, 1, 5, 8, 15, 18};
+    int[] docIds3 = new int[]{1, 2, 6, 13, 16, 19};
+    int[] docIds4 = new int[]{0, 1, 2, 3, 4, 5};
+
+    MutableRoaringBitmap bitmap1 = new MutableRoaringBitmap();
+    bitmap1.add(docIds1);
+    MutableRoaringBitmap bitmap2 = new MutableRoaringBitmap();
+    bitmap2.add(docIds2);
+    MutableRoaringBitmap bitmap3 = new MutableRoaringBitmap();
+    bitmap3.add(docIds3);
+    MutableRoaringBitmap bitmap4 = new MutableRoaringBitmap();
+    bitmap4.add(docIds4);
+    OrDocIdIterator orDocIdIterator = new OrDocIdIterator(new 
BlockDocIdIterator[]{

Review comment:
       Why do we need to build this `OrDocIdIterator`? We don't want to test 
`OrDocIdIterator` in this test

##########
File path: 
pinot-core/src/test/java/org/apache/pinot/core/operator/dociditerators/NotDocIdIteratorTest.java
##########
@@ -0,0 +1,103 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.core.operator.dociditerators;
+
+import org.apache.pinot.core.common.BlockDocIdIterator;
+import org.apache.pinot.segment.spi.Constants;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+
+
+public class NotDocIdIteratorTest {
+  @Test
+  public void testNotDocIdIterator() {
+    // OR result: [0, 1, 2, 4, 5, 6, 8, 10, 13, 15, 16, 17, 18, 19, 20]
+    int[] docIds1 = new int[]{1, 4, 6, 10, 15, 17, 18, 20};
+    int[] docIds2 = new int[]{0, 1, 5, 8, 15, 18};
+    int[] docIds3 = new int[]{1, 2, 6, 13, 16, 19};
+    int[] docIds4 = new int[]{0, 1, 2, 3, 4, 5};
+
+    MutableRoaringBitmap bitmap1 = new MutableRoaringBitmap();
+    bitmap1.add(docIds1);
+    MutableRoaringBitmap bitmap2 = new MutableRoaringBitmap();
+    bitmap2.add(docIds2);
+    MutableRoaringBitmap bitmap3 = new MutableRoaringBitmap();
+    bitmap3.add(docIds3);
+    MutableRoaringBitmap bitmap4 = new MutableRoaringBitmap();
+    bitmap4.add(docIds4);
+    OrDocIdIterator orDocIdIterator = new OrDocIdIterator(new 
BlockDocIdIterator[]{
+        new RangelessBitmapDocIdIterator(bitmap1), new 
RangelessBitmapDocIdIterator(
+        bitmap2), new RangelessBitmapDocIdIterator(bitmap3)
+    });
+    NotDocIdIterator notDocIdIterator = new NotDocIdIterator(new 
RangelessBitmapDocIdIterator(bitmap1), 25);
+
+    assertEquals(notDocIdIterator.advance(1), 2);
+    assertEquals(notDocIdIterator.next(), 3);
+    assertEquals(notDocIdIterator.next(), 5);
+    assertEquals(notDocIdIterator.advance(7), 8);

Review comment:
       (major) Per the contract, this should return 7

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/operator/dociditerators/NotDocIdIterator.java
##########
@@ -0,0 +1,97 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.core.operator.dociditerators;
+
+import org.apache.pinot.core.common.BlockDocIdIterator;
+import org.apache.pinot.segment.spi.Constants;
+
+
+/**
+ * The iterator performs a linear pass through the underlying child iterator 
and returns
+ * the complement of the result set.
+ */
+public class NotDocIdIterator implements BlockDocIdIterator {
+  private final BlockDocIdIterator _childDocIdIterator;
+  private final int _numDocs;
+  private int _nextDocId;
+  private int _nextNonMatchingDocId;
+
+  public NotDocIdIterator(BlockDocIdIterator childDocIdIterator, int numDocs) {
+    _childDocIdIterator = childDocIdIterator;
+    _nextDocId = 0;
+
+    int currentDocIdFromChildIterator = childDocIdIterator.next();
+    _nextNonMatchingDocId = currentDocIdFromChildIterator == Constants.EOF ? 
numDocs : currentDocIdFromChildIterator;
+    _numDocs = numDocs;
+  }
+
+  @Override
+  public int next() {
+    while (_nextDocId == _nextNonMatchingDocId) {
+      _nextDocId = _nextNonMatchingDocId + 1;
+
+      int nextMatchingDocId = _childDocIdIterator.next();
+
+      if (nextMatchingDocId == Constants.EOF) {
+        _nextNonMatchingDocId = _numDocs;
+      } else {
+        _nextNonMatchingDocId = nextMatchingDocId;
+      }
+    }
+
+    if (_nextDocId >= _numDocs) {
+      return Constants.EOF;
+    }
+
+    return _nextDocId++;
+  }
+
+  @Override
+  public int advance(int targetDocId) {
+    if (targetDocId == Constants.EOF || targetDocId > _numDocs) {
+      return Constants.EOF;
+    }
+
+    if (targetDocId < _nextDocId) {
+      return _nextDocId;
+    }
+
+    _nextDocId = targetDocId + 1;
+
+    int upperLimit = findUpperLimitGreaterThanDocId(targetDocId);
+
+    if (upperLimit == Constants.EOF) {
+      _nextNonMatchingDocId = _numDocs;
+    } else {
+      _nextNonMatchingDocId = upperLimit;
+    }
+
+    return _nextDocId++;
+  }
+
+  private int findUpperLimitGreaterThanDocId(int currentDocId) {
+    int result = _childDocIdIterator.advance(currentDocId);

Review comment:
       (major) need to check `_nextNonMatchingDocId < currentDocId` before 
advancing the pointer

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/operator/dociditerators/NotDocIdIterator.java
##########
@@ -0,0 +1,97 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.core.operator.dociditerators;
+
+import org.apache.pinot.core.common.BlockDocIdIterator;
+import org.apache.pinot.segment.spi.Constants;
+
+
+/**
+ * The iterator performs a linear pass through the underlying child iterator 
and returns
+ * the complement of the result set.
+ */
+public class NotDocIdIterator implements BlockDocIdIterator {
+  private final BlockDocIdIterator _childDocIdIterator;
+  private final int _numDocs;
+  private int _nextDocId;
+  private int _nextNonMatchingDocId;
+
+  public NotDocIdIterator(BlockDocIdIterator childDocIdIterator, int numDocs) {
+    _childDocIdIterator = childDocIdIterator;
+    _nextDocId = 0;
+
+    int currentDocIdFromChildIterator = childDocIdIterator.next();
+    _nextNonMatchingDocId = currentDocIdFromChildIterator == Constants.EOF ? 
numDocs : currentDocIdFromChildIterator;
+    _numDocs = numDocs;
+  }
+
+  @Override
+  public int next() {
+    while (_nextDocId == _nextNonMatchingDocId) {
+      _nextDocId = _nextNonMatchingDocId + 1;
+
+      int nextMatchingDocId = _childDocIdIterator.next();
+
+      if (nextMatchingDocId == Constants.EOF) {
+        _nextNonMatchingDocId = _numDocs;
+      } else {
+        _nextNonMatchingDocId = nextMatchingDocId;
+      }
+    }
+
+    if (_nextDocId >= _numDocs) {
+      return Constants.EOF;
+    }
+
+    return _nextDocId++;
+  }
+
+  @Override
+  public int advance(int targetDocId) {
+    if (targetDocId == Constants.EOF || targetDocId > _numDocs) {
+      return Constants.EOF;
+    }
+
+    if (targetDocId < _nextDocId) {
+      return _nextDocId;
+    }

Review comment:
       These checks are redundant because by contract, `targetDocId >= 
_nextDocId && targetDocId < _numDocs`

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/operator/dociditerators/NotDocIdIterator.java
##########
@@ -0,0 +1,97 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.core.operator.dociditerators;
+
+import org.apache.pinot.core.common.BlockDocIdIterator;
+import org.apache.pinot.segment.spi.Constants;
+
+
+/**
+ * The iterator performs a linear pass through the underlying child iterator 
and returns
+ * the complement of the result set.
+ */
+public class NotDocIdIterator implements BlockDocIdIterator {
+  private final BlockDocIdIterator _childDocIdIterator;
+  private final int _numDocs;
+  private int _nextDocId;
+  private int _nextNonMatchingDocId;
+
+  public NotDocIdIterator(BlockDocIdIterator childDocIdIterator, int numDocs) {
+    _childDocIdIterator = childDocIdIterator;
+    _nextDocId = 0;
+
+    int currentDocIdFromChildIterator = childDocIdIterator.next();
+    _nextNonMatchingDocId = currentDocIdFromChildIterator == Constants.EOF ? 
numDocs : currentDocIdFromChildIterator;
+    _numDocs = numDocs;
+  }
+
+  @Override
+  public int next() {
+    while (_nextDocId == _nextNonMatchingDocId) {
+      _nextDocId = _nextNonMatchingDocId + 1;
+
+      int nextMatchingDocId = _childDocIdIterator.next();
+
+      if (nextMatchingDocId == Constants.EOF) {
+        _nextNonMatchingDocId = _numDocs;
+      } else {
+        _nextNonMatchingDocId = nextMatchingDocId;
+      }
+    }
+
+    if (_nextDocId >= _numDocs) {
+      return Constants.EOF;
+    }
+
+    return _nextDocId++;
+  }
+
+  @Override
+  public int advance(int targetDocId) {
+    if (targetDocId == Constants.EOF || targetDocId > _numDocs) {
+      return Constants.EOF;
+    }
+
+    if (targetDocId < _nextDocId) {
+      return _nextDocId;
+    }
+
+    _nextDocId = targetDocId + 1;

Review comment:
       (major) This will skip the `targetDocId`

##########
File path: 
pinot-core/src/test/java/org/apache/pinot/core/operator/dociditerators/NotDocIdIteratorTest.java
##########
@@ -0,0 +1,103 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.core.operator.dociditerators;
+
+import org.apache.pinot.core.common.BlockDocIdIterator;
+import org.apache.pinot.segment.spi.Constants;
+import org.roaringbitmap.buffer.MutableRoaringBitmap;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+
+
+public class NotDocIdIteratorTest {
+  @Test
+  public void testNotDocIdIterator() {
+    // OR result: [0, 1, 2, 4, 5, 6, 8, 10, 13, 15, 16, 17, 18, 19, 20]
+    int[] docIds1 = new int[]{1, 4, 6, 10, 15, 17, 18, 20};
+    int[] docIds2 = new int[]{0, 1, 5, 8, 15, 18};
+    int[] docIds3 = new int[]{1, 2, 6, 13, 16, 19};
+    int[] docIds4 = new int[]{0, 1, 2, 3, 4, 5};
+
+    MutableRoaringBitmap bitmap1 = new MutableRoaringBitmap();
+    bitmap1.add(docIds1);
+    MutableRoaringBitmap bitmap2 = new MutableRoaringBitmap();
+    bitmap2.add(docIds2);
+    MutableRoaringBitmap bitmap3 = new MutableRoaringBitmap();
+    bitmap3.add(docIds3);
+    MutableRoaringBitmap bitmap4 = new MutableRoaringBitmap();
+    bitmap4.add(docIds4);
+    OrDocIdIterator orDocIdIterator = new OrDocIdIterator(new 
BlockDocIdIterator[]{
+        new RangelessBitmapDocIdIterator(bitmap1), new 
RangelessBitmapDocIdIterator(
+        bitmap2), new RangelessBitmapDocIdIterator(bitmap3)
+    });
+    NotDocIdIterator notDocIdIterator = new NotDocIdIterator(new 
RangelessBitmapDocIdIterator(bitmap1), 25);
+
+    assertEquals(notDocIdIterator.advance(1), 2);
+    assertEquals(notDocIdIterator.next(), 3);
+    assertEquals(notDocIdIterator.next(), 5);
+    assertEquals(notDocIdIterator.advance(7), 8);
+    assertEquals(notDocIdIterator.advance(13), 14);

Review comment:
       (major) This should return 13, same for other places




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to