Search in sources :

Example 6 with BitmapIndexSelector

use of io.druid.query.filter.BitmapIndexSelector in project druid by druid-io.

the class AutoStrategy method getExecutionPlan.

@Override
public List<SearchQueryExecutor> getExecutionPlan(SearchQuery query, Segment segment) {
    final QueryableIndex index = segment.asQueryableIndex();
    if (index != null) {
        final BitmapIndexSelector selector = new ColumnSelectorBitmapIndexSelector(index.getBitmapFactoryForDimensions(), VirtualColumns.EMPTY, index);
        // from the non-bitmap-support filters, and then use it to compute the filtered result by intersecting bitmaps.
        if (filter == null || filter.supportsSelectivityEstimation(index, selector)) {
            final List<DimensionSpec> dimsToSearch = getDimsToSearch(index.getAvailableDimensions(), query.getDimensions());
            // Choose a search query execution strategy depending on the query.
            // The costs of index-only plan and cursor-based plan can be computed like below.
            //
            // c_index = (total cardinality of all search dimensions) * (bitmap intersection cost)
            //            * (search predicate processing cost)
            // c_cursor = (# of rows in a segment) * (filter selectivity) * (# of dimensions)
            //            * (search predicate processing cost)
            final SearchQueryDecisionHelper helper = getDecisionHelper(index);
            final double useIndexStrategyCost = helper.getBitmapIntersectCost() * computeTotalCard(index, dimsToSearch);
            final double cursorOnlyStrategyCost = (filter == null ? 1. : filter.estimateSelectivity(selector)) * selector.getNumRows() * dimsToSearch.size();
            log.debug("Use-index strategy cost: %f, cursor-only strategy cost: %f", useIndexStrategyCost, cursorOnlyStrategyCost);
            if (useIndexStrategyCost < cursorOnlyStrategyCost) {
                log.debug("Use-index execution strategy is selected, query id [%s]", query.getId());
                return UseIndexesStrategy.of(query).getExecutionPlan(query, segment);
            } else {
                log.debug("Cursor-only execution strategy is selected, query id [%s]", query.getId());
                return CursorOnlyStrategy.of(query).getExecutionPlan(query, segment);
            }
        } else {
            log.debug("Filter doesn't support bitmap index. Fall back to cursor-only execution strategy, query id [%s]", query.getId());
            return CursorOnlyStrategy.of(query).getExecutionPlan(query, segment);
        }
    } else {
        log.debug("Index doesn't exist. Fall back to cursor-only execution strategy, query id [%s]", query.getId());
        return CursorOnlyStrategy.of(query).getExecutionPlan(query, segment);
    }
}
Also used : ColumnSelectorBitmapIndexSelector(io.druid.segment.ColumnSelectorBitmapIndexSelector) DimensionSpec(io.druid.query.dimension.DimensionSpec) QueryableIndex(io.druid.segment.QueryableIndex) ColumnSelectorBitmapIndexSelector(io.druid.segment.ColumnSelectorBitmapIndexSelector) BitmapIndexSelector(io.druid.query.filter.BitmapIndexSelector)

Example 7 with BitmapIndexSelector

use of io.druid.query.filter.BitmapIndexSelector in project druid by druid-io.

the class BaseFilterTest method selectColumnValuesMatchingFilterUsingPostFiltering.

private List<String> selectColumnValuesMatchingFilterUsingPostFiltering(final DimFilter filter, final String selectColumn) {
    final Filter theFilter = makeFilter(filter);
    final Filter postFilteringFilter = new Filter() {

        @Override
        public ImmutableBitmap getBitmapIndex(BitmapIndexSelector selector) {
            throw new UnsupportedOperationException();
        }

        @Override
        public ValueMatcher makeMatcher(ColumnSelectorFactory factory) {
            return theFilter.makeMatcher(factory);
        }

        @Override
        public boolean supportsBitmapIndex(BitmapIndexSelector selector) {
            return false;
        }

        @Override
        public boolean supportsSelectivityEstimation(ColumnSelector columnSelector, BitmapIndexSelector indexSelector) {
            return false;
        }

        @Override
        public double estimateSelectivity(BitmapIndexSelector indexSelector) {
            return 1.0;
        }
    };
    final Sequence<Cursor> cursors = makeCursorSequence(postFilteringFilter);
    Sequence<List<String>> seq = Sequences.map(cursors, new Function<Cursor, List<String>>() {

        @Override
        public List<String> apply(Cursor input) {
            final DimensionSelector selector = input.makeDimensionSelector(new DefaultDimensionSpec(selectColumn, selectColumn));
            final List<String> values = Lists.newArrayList();
            while (!input.isDone()) {
                IndexedInts row = selector.getRow();
                Preconditions.checkState(row.size() == 1);
                values.add(selector.lookupName(row.get(0)));
                input.advance();
            }
            return values;
        }
    });
    return Sequences.toList(seq, new ArrayList<List<String>>()).get(0);
}
Also used : DimensionSelector(io.druid.segment.DimensionSelector) RowBasedColumnSelectorFactory(io.druid.query.groupby.RowBasedColumnSelectorFactory) ColumnSelectorFactory(io.druid.segment.ColumnSelectorFactory) ArrayList(java.util.ArrayList) Cursor(io.druid.segment.Cursor) DefaultDimensionSpec(io.druid.query.dimension.DefaultDimensionSpec) DimFilter(io.druid.query.filter.DimFilter) Filter(io.druid.query.filter.Filter) ColumnSelector(io.druid.segment.ColumnSelector) IndexedInts(io.druid.segment.data.IndexedInts) BitmapIndexSelector(io.druid.query.filter.BitmapIndexSelector) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

BitmapIndexSelector (io.druid.query.filter.BitmapIndexSelector)7 ImmutableBitmap (io.druid.collections.bitmap.ImmutableBitmap)5 BitmapFactory (io.druid.collections.bitmap.BitmapFactory)4 MutableBitmap (io.druid.collections.bitmap.MutableBitmap)4 Function (com.google.common.base.Function)3 RoaringBitmapFactory (io.druid.collections.bitmap.RoaringBitmapFactory)3 ImmutableRTree (io.druid.collections.spatial.ImmutableRTree)3 ColumnSelectorBitmapIndexSelector (io.druid.segment.ColumnSelectorBitmapIndexSelector)3 BitmapIndex (io.druid.segment.column.BitmapIndex)3 BitmapSerdeFactory (io.druid.segment.data.BitmapSerdeFactory)3 GenericIndexed (io.druid.segment.data.GenericIndexed)3 Indexed (io.druid.segment.data.Indexed)3 RoaringBitmapSerdeFactory (io.druid.segment.data.RoaringBitmapSerdeFactory)3 BitmapIndexColumnPartSupplier (io.druid.segment.serde.BitmapIndexColumnPartSupplier)3 Setup (org.openjdk.jmh.annotations.Setup)3 ImmutableList (com.google.common.collect.ImmutableList)2 DimensionSpec (io.druid.query.dimension.DimensionSpec)2 QueryableIndex (io.druid.segment.QueryableIndex)2 List (java.util.List)2 DefaultDimensionSpec (io.druid.query.dimension.DefaultDimensionSpec)1