Search in sources :

Example 1 with BlockDocIdSet

use of com.linkedin.pinot.core.common.BlockDocIdSet in project pinot by linkedin.

the class AndBlockDocIdSet method slowIterator.

public BlockDocIdIterator slowIterator() {
    List<BlockDocIdIterator> rawIterators = new ArrayList<>();
    boolean useBitmapBasedIntersection = false;
    for (BlockDocIdSet docIdSet : blockDocIdSets) {
        if (docIdSet instanceof BitmapDocIdSet) {
            useBitmapBasedIntersection = true;
        }
    }
    BlockDocIdIterator[] docIdIterators;
    if (useBitmapBasedIntersection) {
        List<ImmutableRoaringBitmap> allBitmaps = new ArrayList<ImmutableRoaringBitmap>();
        for (BlockDocIdSet docIdSet : blockDocIdSets) {
            if (docIdSet instanceof SortedDocIdSet) {
                MutableRoaringBitmap bitmap = new MutableRoaringBitmap();
                SortedDocIdSet sortedDocIdSet = (SortedDocIdSet) docIdSet;
                List<IntPair> pairs = sortedDocIdSet.getRaw();
                for (IntPair pair : pairs) {
                    // add takes [start, end) i.e inclusive
                    bitmap.add(pair.getLeft(), pair.getRight() + 1);
                // start, exclusive end.
                }
                allBitmaps.add(bitmap);
            } else if (docIdSet instanceof BitmapDocIdSet) {
                BitmapDocIdSet bitmapDocIdSet = (BitmapDocIdSet) docIdSet;
                ImmutableRoaringBitmap childBitmap = bitmapDocIdSet.getRaw();
                allBitmaps.add(childBitmap);
            } else {
                BlockDocIdIterator iterator = docIdSet.iterator();
                rawIterators.add(iterator);
            }
        }
        IntIterator intIterator;
        if (allBitmaps.size() > 1) {
            MutableRoaringBitmap answer = (MutableRoaringBitmap) allBitmaps.get(0).clone();
            for (int i = 1; i < allBitmaps.size(); i++) {
                answer.and(allBitmaps.get(i));
            }
            intIterator = answer.getIntIterator();
        } else {
            intIterator = allBitmaps.get(0).getIntIterator();
        }
        BitmapDocIdIterator singleBitmapBlockIdIterator = new BitmapDocIdIterator(intIterator);
        singleBitmapBlockIdIterator.setStartDocId(minDocId);
        singleBitmapBlockIdIterator.setEndDocId(maxDocId);
        rawIterators.add(0, singleBitmapBlockIdIterator);
        docIdIterators = new BlockDocIdIterator[rawIterators.size()];
        rawIterators.toArray(docIdIterators);
    } else {
        docIdIterators = new BlockDocIdIterator[blockDocIdSets.size()];
        for (int srcId = 0; srcId < blockDocIdSets.size(); srcId++) {
            docIdIterators[srcId] = blockDocIdSets.get(srcId).iterator();
        }
    }
    return new AndDocIdIterator(docIdIterators);
}
Also used : IntIterator(org.roaringbitmap.IntIterator) MutableRoaringBitmap(org.roaringbitmap.buffer.MutableRoaringBitmap) ArrayList(java.util.ArrayList) IntPair(com.linkedin.pinot.common.utils.Pairs.IntPair) BlockDocIdIterator(com.linkedin.pinot.core.common.BlockDocIdIterator) BlockDocIdSet(com.linkedin.pinot.core.common.BlockDocIdSet) AndDocIdIterator(com.linkedin.pinot.core.operator.dociditerators.AndDocIdIterator) ImmutableRoaringBitmap(org.roaringbitmap.buffer.ImmutableRoaringBitmap) RangelessBitmapDocIdIterator(com.linkedin.pinot.core.operator.dociditerators.RangelessBitmapDocIdIterator) BitmapDocIdIterator(com.linkedin.pinot.core.operator.dociditerators.BitmapDocIdIterator)

Example 2 with BlockDocIdSet

use of com.linkedin.pinot.core.common.BlockDocIdSet in project pinot by linkedin.

the class AndBlockDocIdSet method fastIterator.

public BlockDocIdIterator fastIterator() {
    long start = System.currentTimeMillis();
    List<List<IntPair>> sortedRangeSets = new ArrayList<>();
    List<ImmutableRoaringBitmap> childBitmaps = new ArrayList<ImmutableRoaringBitmap>();
    List<FilterBlockDocIdSet> scanBasedDocIdSets = new ArrayList<>();
    List<BlockDocIdIterator> remainingIterators = new ArrayList<>();
    for (BlockDocIdSet docIdSet : blockDocIdSets) {
        if (docIdSet instanceof SortedDocIdSet) {
            SortedDocIdSet sortedDocIdSet = (SortedDocIdSet) docIdSet;
            List<IntPair> pairs = sortedDocIdSet.getRaw();
            sortedRangeSets.add(pairs);
        } else if (docIdSet instanceof BitmapDocIdSet) {
            BitmapDocIdSet bitmapDocIdSet = (BitmapDocIdSet) docIdSet;
            ImmutableRoaringBitmap childBitmap = bitmapDocIdSet.getRaw();
            childBitmaps.add(childBitmap);
        } else if (docIdSet instanceof ScanBasedSingleValueDocIdSet) {
            scanBasedDocIdSets.add((ScanBasedSingleValueDocIdSet) docIdSet);
        } else if (docIdSet instanceof ScanBasedMultiValueDocIdSet) {
            scanBasedDocIdSets.add((ScanBasedMultiValueDocIdSet) docIdSet);
        } else {
            // TODO:handle child OR/AND as bitmap if possible
            remainingIterators.add(docIdSet.iterator());
        }
    }
    if (childBitmaps.size() == 0 && sortedRangeSets.size() == 0) {
        // When one or more of the operands are operators themselves, then we don't have a sorted or
        // bitmap index. In that case, just use the AndDocIdIterator to iterate over all of of the subtree.
        BlockDocIdIterator[] docIdIterators = new BlockDocIdIterator[blockDocIdSets.size()];
        for (int srcId = 0; srcId < blockDocIdSets.size(); srcId++) {
            docIdIterators[srcId] = blockDocIdSets.get(srcId).iterator();
        }
        return new AndDocIdIterator(docIdIterators);
    } else {
        // TODO: will be nice to re-order sorted and bitmap index based on size
        if (sortedRangeSets.size() > 0) {
            List<IntPair> pairList;
            pairList = SortedRangeIntersection.intersectSortedRangeSets(sortedRangeSets);
            answer = new MutableRoaringBitmap();
            for (IntPair pair : pairList) {
                // end is exclusive
                answer.add(pair.getLeft(), pair.getRight() + 1);
            }
        }
        // handle bitmaps
        if (childBitmaps.size() > 0) {
            if (answer == null) {
                answer = childBitmaps.get(0).toMutableRoaringBitmap();
                for (int i = 1; i < childBitmaps.size(); i++) {
                    answer.and(childBitmaps.get(i));
                }
            } else {
                for (int i = 0; i < childBitmaps.size(); i++) {
                    answer.and(childBitmaps.get(i));
                }
            }
        }
        // At this point, we must have 'answer' to be non-null.
        assert (answer != null) : "sortedRangeSets=" + sortedRangeSets.size() + ",childBitmaps=" + childBitmaps.size();
        // handle raw iterators
        for (FilterBlockDocIdSet scanBasedDocIdSet : scanBasedDocIdSets) {
            ScanBasedDocIdIterator iterator = (ScanBasedDocIdIterator) scanBasedDocIdSet.iterator();
            MutableRoaringBitmap scanAnswer = iterator.applyAnd(answer);
            answer.and(scanAnswer);
        }
        long end = System.currentTimeMillis();
        LOGGER.debug("Time to evaluate and Filter:{}", (end - start));
        // if other iterators exists resort to iterator style intersection
        BlockDocIdIterator answerDocIdIterator = new RangelessBitmapDocIdIterator(answer.getIntIterator());
        if (remainingIterators.size() == 0) {
            return answerDocIdIterator;
        } else {
            BlockDocIdIterator[] docIdIterators = new BlockDocIdIterator[remainingIterators.size() + 1];
            docIdIterators[0] = answerDocIdIterator;
            for (int i = 0; i < remainingIterators.size(); i++) {
                docIdIterators[i + 1] = remainingIterators.get(i);
            }
            return new AndDocIdIterator(docIdIterators);
        }
    }
}
Also used : RangelessBitmapDocIdIterator(com.linkedin.pinot.core.operator.dociditerators.RangelessBitmapDocIdIterator) MutableRoaringBitmap(org.roaringbitmap.buffer.MutableRoaringBitmap) ArrayList(java.util.ArrayList) ScanBasedDocIdIterator(com.linkedin.pinot.core.operator.dociditerators.ScanBasedDocIdIterator) IntPair(com.linkedin.pinot.common.utils.Pairs.IntPair) BlockDocIdIterator(com.linkedin.pinot.core.common.BlockDocIdIterator) BlockDocIdSet(com.linkedin.pinot.core.common.BlockDocIdSet) AndDocIdIterator(com.linkedin.pinot.core.operator.dociditerators.AndDocIdIterator) ImmutableRoaringBitmap(org.roaringbitmap.buffer.ImmutableRoaringBitmap) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with BlockDocIdSet

use of com.linkedin.pinot.core.common.BlockDocIdSet in project pinot by linkedin.

the class ChunkIndexCreationDriverImplTest method test4.

@Test(enabled = false)
public void test4() throws Exception {
    final IndexSegmentImpl segment = (IndexSegmentImpl) Loaders.IndexSegment.load(INDEX_DIR.listFiles()[0], ReadMode.mmap);
    final ImmutableDictionaryReader d = segment.getDictionaryFor("column1");
    final List<String> rhs = new ArrayList<String>();
    rhs.add(d.get(new Random().nextInt(d.length())).toString());
    final Predicate p = new EqPredicate("column1", rhs);
    final DataSource ds = segment.getDataSource("column1", p);
    final Block bl = ds.nextBlock();
    final BlockDocIdSet idSet = bl.getBlockDocIdSet();
    final BlockDocIdIterator it = idSet.iterator();
    int docId = it.next();
    final StringBuilder b = new StringBuilder();
    while (docId != Constants.EOF) {
        b.append(docId + ",");
        docId = it.next();
    }
//    System.out.println(b.toString());
}
Also used : ImmutableDictionaryReader(com.linkedin.pinot.core.segment.index.readers.ImmutableDictionaryReader) ArrayList(java.util.ArrayList) EqPredicate(com.linkedin.pinot.core.common.predicate.EqPredicate) EqPredicate(com.linkedin.pinot.core.common.predicate.EqPredicate) Predicate(com.linkedin.pinot.core.common.Predicate) DataSource(com.linkedin.pinot.core.common.DataSource) BlockDocIdIterator(com.linkedin.pinot.core.common.BlockDocIdIterator) IndexSegmentImpl(com.linkedin.pinot.core.segment.index.IndexSegmentImpl) Random(java.util.Random) BlockDocIdSet(com.linkedin.pinot.core.common.BlockDocIdSet) Block(com.linkedin.pinot.core.common.Block) Test(org.testng.annotations.Test)

Example 4 with BlockDocIdSet

use of com.linkedin.pinot.core.common.BlockDocIdSet in project pinot by linkedin.

the class AndOperatorTest method testIntersectionForThreeLists.

@Test
public void testIntersectionForThreeLists() {
    int[] list1 = new int[] { 2, 3, 6, 10, 15, 16, 28 };
    int[] list2 = new int[] { 3, 6, 8, 20, 28 };
    int[] list3 = new int[] { 1, 2, 3, 6, 30 };
    List<BaseFilterOperator> operators = new ArrayList<BaseFilterOperator>();
    operators.add(makeFilterOperator(list1));
    operators.add(makeFilterOperator(list2));
    operators.add(makeFilterOperator(list3));
    final AndOperator andOperator = new AndOperator(operators);
    andOperator.open();
    BaseFilterBlock block;
    while ((block = andOperator.getNextBlock()) != null) {
        final BlockDocIdSet blockDocIdSet = block.getBlockDocIdSet();
        final BlockDocIdIterator iterator = blockDocIdSet.iterator();
        int docId;
        while ((docId = iterator.next()) != Constants.EOF) {
        //        System.out.println(docId);
        }
    }
    andOperator.close();
}
Also used : BaseFilterOperator(com.linkedin.pinot.core.operator.filter.BaseFilterOperator) AndOperator(com.linkedin.pinot.core.operator.filter.AndOperator) BlockDocIdSet(com.linkedin.pinot.core.common.BlockDocIdSet) ArrayList(java.util.ArrayList) BaseFilterBlock(com.linkedin.pinot.core.operator.blocks.BaseFilterBlock) BlockDocIdIterator(com.linkedin.pinot.core.common.BlockDocIdIterator) Test(org.testng.annotations.Test)

Example 5 with BlockDocIdSet

use of com.linkedin.pinot.core.common.BlockDocIdSet in project pinot by linkedin.

the class AndOperatorTest method testComplexWithOr.

@Test
public void testComplexWithOr() {
    int[] list1 = new int[] { 2, 3, 6, 10, 15, 16, 28 };
    int[] list2 = new int[] { 3, 6, 8, 20, 28 };
    int[] list3 = new int[] { 1, 2, 3, 6, 30 };
    List<BaseFilterOperator> operators = new ArrayList<>();
    operators.add(makeFilterOperator(list3));
    operators.add(makeFilterOperator(list2));
    final OrOperator orOperator = new OrOperator(operators);
    List<BaseFilterOperator> operators1 = new ArrayList<>();
    operators1.add(orOperator);
    operators1.add(makeFilterOperator(list1));
    final AndOperator andOperator = new AndOperator(operators1);
    andOperator.open();
    BaseFilterBlock block;
    while ((block = andOperator.getNextBlock()) != null) {
        final BlockDocIdSet blockDocIdSet = block.getBlockDocIdSet();
        final BlockDocIdIterator iterator = blockDocIdSet.iterator();
        int docId;
        while ((docId = iterator.next()) != Constants.EOF) {
        //        System.out.println(docId);
        }
    }
    andOperator.close();
}
Also used : OrOperator(com.linkedin.pinot.core.operator.filter.OrOperator) BaseFilterOperator(com.linkedin.pinot.core.operator.filter.BaseFilterOperator) AndOperator(com.linkedin.pinot.core.operator.filter.AndOperator) BlockDocIdSet(com.linkedin.pinot.core.common.BlockDocIdSet) ArrayList(java.util.ArrayList) BaseFilterBlock(com.linkedin.pinot.core.operator.blocks.BaseFilterBlock) BlockDocIdIterator(com.linkedin.pinot.core.common.BlockDocIdIterator) Test(org.testng.annotations.Test)

Aggregations

BlockDocIdSet (com.linkedin.pinot.core.common.BlockDocIdSet)15 BlockDocIdIterator (com.linkedin.pinot.core.common.BlockDocIdIterator)14 ArrayList (java.util.ArrayList)13 Test (org.testng.annotations.Test)10 BaseFilterBlock (com.linkedin.pinot.core.operator.blocks.BaseFilterBlock)8 BaseFilterOperator (com.linkedin.pinot.core.operator.filter.BaseFilterOperator)8 Block (com.linkedin.pinot.core.common.Block)6 AndOperator (com.linkedin.pinot.core.operator.filter.AndOperator)4 OrOperator (com.linkedin.pinot.core.operator.filter.OrOperator)4 DataSource (com.linkedin.pinot.core.common.DataSource)3 Predicate (com.linkedin.pinot.core.common.Predicate)3 EqPredicate (com.linkedin.pinot.core.common.predicate.EqPredicate)3 IndexSegmentImpl (com.linkedin.pinot.core.segment.index.IndexSegmentImpl)3 TreeSet (java.util.TreeSet)3 ImmutableRoaringBitmap (org.roaringbitmap.buffer.ImmutableRoaringBitmap)3 MutableRoaringBitmap (org.roaringbitmap.buffer.MutableRoaringBitmap)3 IntPair (com.linkedin.pinot.common.utils.Pairs.IntPair)2 AndDocIdIterator (com.linkedin.pinot.core.operator.dociditerators.AndDocIdIterator)2 BitmapDocIdIterator (com.linkedin.pinot.core.operator.dociditerators.BitmapDocIdIterator)2 RangelessBitmapDocIdIterator (com.linkedin.pinot.core.operator.dociditerators.RangelessBitmapDocIdIterator)2