Search in sources :

Example 1 with DefaultBitmapResultFactory

use of org.apache.druid.query.DefaultBitmapResultFactory in project druid by druid-io.

the class QueryableIndexStorageAdapter method analyzeFilter.

@VisibleForTesting
public FilterAnalysis analyzeFilter(@Nullable final Filter filter, ColumnSelectorBitmapIndexSelector indexSelector, @Nullable QueryMetrics queryMetrics) {
    final int totalRows = index.getNumRows();
    /*
     * Filters can be applied in two stages:
     * pre-filtering: Use bitmap indexes to prune the set of rows to be scanned.
     * post-filtering: Iterate through rows and apply the filter to the row values
     *
     * The pre-filter and post-filter step have an implicit AND relationship. (i.e., final rows are those that
     * were not pruned AND those that matched the filter during row scanning)
     *
     * An AND filter can have its subfilters partitioned across the two steps. The subfilters that can be
     * processed entirely with bitmap indexes (subfilter returns true for supportsBitmapIndex())
     * will be moved to the pre-filtering stage.
     *
     * Any subfilters that cannot be processed entirely with bitmap indexes will be moved to the post-filtering stage.
     */
    final List<Filter> preFilters;
    final List<Filter> postFilters = new ArrayList<>();
    int preFilteredRows = totalRows;
    if (filter == null) {
        preFilters = Collections.emptyList();
    } else {
        preFilters = new ArrayList<>();
        if (filter instanceof AndFilter) {
            // If we get an AndFilter, we can split the subfilters across both filtering stages
            for (Filter subfilter : ((AndFilter) filter).getFilters()) {
                if (subfilter.supportsBitmapIndex(indexSelector) && subfilter.shouldUseBitmapIndex(indexSelector)) {
                    preFilters.add(subfilter);
                } else {
                    postFilters.add(subfilter);
                }
            }
        } else {
            // If we get an OrFilter or a single filter, handle the filter in one stage
            if (filter.supportsBitmapIndex(indexSelector) && filter.shouldUseBitmapIndex(indexSelector)) {
                preFilters.add(filter);
            } else {
                postFilters.add(filter);
            }
        }
    }
    final ImmutableBitmap preFilterBitmap;
    if (preFilters.isEmpty()) {
        preFilterBitmap = null;
    } else {
        if (queryMetrics != null) {
            BitmapResultFactory<?> bitmapResultFactory = queryMetrics.makeBitmapResultFactory(indexSelector.getBitmapFactory());
            long bitmapConstructionStartNs = System.nanoTime();
            // Use AndFilter.getBitmapResult to intersect the preFilters to get its short-circuiting behavior.
            preFilterBitmap = AndFilter.getBitmapIndex(indexSelector, bitmapResultFactory, preFilters);
            preFilteredRows = preFilterBitmap.size();
            queryMetrics.reportBitmapConstructionTime(System.nanoTime() - bitmapConstructionStartNs);
        } else {
            BitmapResultFactory<?> bitmapResultFactory = new DefaultBitmapResultFactory(indexSelector.getBitmapFactory());
            preFilterBitmap = AndFilter.getBitmapIndex(indexSelector, bitmapResultFactory, preFilters);
        }
    }
    if (queryMetrics != null) {
        queryMetrics.preFilters(new ArrayList<>(preFilters));
        queryMetrics.postFilters(postFilters);
        queryMetrics.reportSegmentRows(totalRows);
        queryMetrics.reportPreFilteredRows(preFilteredRows);
    }
    return new FilterAnalysis(preFilterBitmap, Filters.maybeAnd(postFilters).orElse(null));
}
Also used : AndFilter(org.apache.druid.segment.filter.AndFilter) AndFilter(org.apache.druid.segment.filter.AndFilter) Filter(org.apache.druid.query.filter.Filter) ImmutableBitmap(org.apache.druid.collections.bitmap.ImmutableBitmap) ArrayList(java.util.ArrayList) DefaultBitmapResultFactory(org.apache.druid.query.DefaultBitmapResultFactory) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ArrayList (java.util.ArrayList)1 ImmutableBitmap (org.apache.druid.collections.bitmap.ImmutableBitmap)1 DefaultBitmapResultFactory (org.apache.druid.query.DefaultBitmapResultFactory)1 Filter (org.apache.druid.query.filter.Filter)1 AndFilter (org.apache.druid.segment.filter.AndFilter)1