Search in sources :

Example 16 with Filter

use of io.druid.query.filter.Filter 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)

Example 17 with Filter

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

the class FilterPartitionTest method testDistributeOrCNFExtractionFn.

@Test
public void testDistributeOrCNFExtractionFn() {
    DimFilter dimFilter1 = new OrDimFilter(Arrays.<DimFilter>asList(new SelectorDimFilter("dim0", "super-6", JS_EXTRACTION_FN), new AndDimFilter(Arrays.<DimFilter>asList(new NoBitmapSelectorDimFilter("dim1", "super-def", JS_EXTRACTION_FN), new SelectorDimFilter("dim2", "super-c", JS_EXTRACTION_FN)))));
    Filter filter1 = dimFilter1.toFilter();
    Filter filter1CNF = Filters.convertToCNF(filter1);
    Assert.assertEquals(AndFilter.class, filter1CNF.getClass());
    Assert.assertEquals(2, ((AndFilter) filter1CNF).getFilters().size());
    assertFilterMatches(dimFilter1, ImmutableList.of("4", "6"));
    DimFilter dimFilter2 = new OrDimFilter(Arrays.<DimFilter>asList(new SelectorDimFilter("dim0", "super-2", JS_EXTRACTION_FN), new SelectorDimFilter("dim0", "super-3", JS_EXTRACTION_FN), new AndDimFilter(Arrays.<DimFilter>asList(new NoBitmapSelectorDimFilter("dim1", "super-HELLO", JS_EXTRACTION_FN), new SelectorDimFilter("dim2", "super-foo", JS_EXTRACTION_FN)))));
    assertFilterMatches(dimFilter2, ImmutableList.of("2", "3", "7"));
    DimFilter dimFilter3 = new OrDimFilter(Arrays.<DimFilter>asList(dimFilter1, dimFilter2, new AndDimFilter(Arrays.<DimFilter>asList(new NoBitmapSelectorDimFilter("dim1", "super-1", JS_EXTRACTION_FN), new SelectorDimFilter("dim2", "super-foo", JS_EXTRACTION_FN)))));
    Filter filter3 = dimFilter3.toFilter();
    Filter filter3CNF = Filters.convertToCNF(dimFilter3.toFilter());
    assertFilterMatches(dimFilter3, ImmutableList.of("2", "3", "4", "6", "7", "9"));
}
Also used : AndDimFilter(io.druid.query.filter.AndDimFilter) OrDimFilter(io.druid.query.filter.OrDimFilter) SelectorDimFilter(io.druid.query.filter.SelectorDimFilter) DimFilter(io.druid.query.filter.DimFilter) Filter(io.druid.query.filter.Filter) AndDimFilter(io.druid.query.filter.AndDimFilter) SelectorDimFilter(io.druid.query.filter.SelectorDimFilter) OrDimFilter(io.druid.query.filter.OrDimFilter) OrDimFilter(io.druid.query.filter.OrDimFilter) SelectorDimFilter(io.druid.query.filter.SelectorDimFilter) DimFilter(io.druid.query.filter.DimFilter) AndDimFilter(io.druid.query.filter.AndDimFilter) Test(org.junit.Test)

Example 18 with Filter

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

the class ExtractionDimFilterTest method testNull.

@Test
public void testNull() {
    Filter extractionFilter = new SelectorDimFilter("FDHJSFFHDS", "extractDimVal", DIM_EXTRACTION_FN).toFilter();
    ImmutableBitmap immutableBitmap = extractionFilter.getBitmapIndex(BITMAP_INDEX_SELECTOR);
    Assert.assertEquals(0, immutableBitmap.size());
}
Also used : SelectorDimFilter(io.druid.query.filter.SelectorDimFilter) ExtractionDimFilter(io.druid.query.filter.ExtractionDimFilter) Filter(io.druid.query.filter.Filter) ImmutableBitmap(io.druid.collections.bitmap.ImmutableBitmap) SelectorDimFilter(io.druid.query.filter.SelectorDimFilter) Test(org.junit.Test)

Example 19 with Filter

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

the class ExtractionDimFilterTest method testEmpty.

@Test
public void testEmpty() {
    Filter extractionFilter = new SelectorDimFilter("foo", "NFDJUKFNDSJFNS", DIM_EXTRACTION_FN).toFilter();
    ImmutableBitmap immutableBitmap = extractionFilter.getBitmapIndex(BITMAP_INDEX_SELECTOR);
    Assert.assertEquals(0, immutableBitmap.size());
}
Also used : SelectorDimFilter(io.druid.query.filter.SelectorDimFilter) ExtractionDimFilter(io.druid.query.filter.ExtractionDimFilter) Filter(io.druid.query.filter.Filter) ImmutableBitmap(io.druid.collections.bitmap.ImmutableBitmap) SelectorDimFilter(io.druid.query.filter.SelectorDimFilter) Test(org.junit.Test)

Example 20 with Filter

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

the class Filters method convertToCNFInternal.

// CNF conversion functions were adapted from Apache Hive, see:
// https://github.com/apache/hive/blob/branch-2.0/storage-api/src/java/org/apache/hadoop/hive/ql/io/sarg/SearchArgumentImpl.java
private static Filter convertToCNFInternal(Filter current) {
    if (current instanceof NotFilter) {
        return new NotFilter(convertToCNFInternal(((NotFilter) current).getBaseFilter()));
    }
    if (current instanceof AndFilter) {
        List<Filter> children = Lists.newArrayList();
        for (Filter child : ((AndFilter) current).getFilters()) {
            children.add(convertToCNFInternal(child));
        }
        return new AndFilter(children);
    }
    if (current instanceof OrFilter) {
        // a list of leaves that weren't under AND expressions
        List<Filter> nonAndList = new ArrayList<Filter>();
        // a list of AND expressions that we need to distribute
        List<Filter> andList = new ArrayList<Filter>();
        for (Filter child : ((OrFilter) current).getFilters()) {
            if (child instanceof AndFilter) {
                andList.add(child);
            } else if (child instanceof OrFilter) {
                // pull apart the kids of the OR expression
                for (Filter grandChild : ((OrFilter) child).getFilters()) {
                    nonAndList.add(grandChild);
                }
            } else {
                nonAndList.add(child);
            }
        }
        if (!andList.isEmpty()) {
            List<Filter> result = Lists.newArrayList();
            generateAllCombinations(result, andList, nonAndList);
            return new AndFilter(result);
        }
    }
    return current;
}
Also used : DimFilter(io.druid.query.filter.DimFilter) BooleanFilter(io.druid.query.filter.BooleanFilter) Filter(io.druid.query.filter.Filter) ArrayList(java.util.ArrayList)

Aggregations

Filter (io.druid.query.filter.Filter)25 ArrayList (java.util.ArrayList)14 DimFilter (io.druid.query.filter.DimFilter)13 SelectorDimFilter (io.druid.query.filter.SelectorDimFilter)11 Cursor (io.druid.segment.Cursor)11 List (java.util.List)10 AndDimFilter (io.druid.query.filter.AndDimFilter)8 OrDimFilter (io.druid.query.filter.OrDimFilter)8 StorageAdapter (io.druid.segment.StorageAdapter)8 BooleanFilter (io.druid.query.filter.BooleanFilter)7 AndFilter (io.druid.segment.filter.AndFilter)7 ImmutableBitmap (io.druid.collections.bitmap.ImmutableBitmap)6 BoundDimFilter (io.druid.query.filter.BoundDimFilter)6 QueryableIndexStorageAdapter (io.druid.segment.QueryableIndexStorageAdapter)6 Interval (org.joda.time.Interval)6 BoundFilter (io.druid.segment.filter.BoundFilter)5 DimensionPredicateFilter (io.druid.segment.filter.DimensionPredicateFilter)5 OrFilter (io.druid.segment.filter.OrFilter)5 SelectorFilter (io.druid.segment.filter.SelectorFilter)5 Test (org.junit.Test)5