Search in sources :

Example 26 with InDimFilter

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

the class ConvertSelectorsToIns method process.

@Override
public DimFilter process(DimFilter filter) {
    if (filter instanceof OrDimFilter) {
        // Copy children list
        final List<DimFilter> children = Lists.newArrayList(((OrDimFilter) filter).getFields());
        // Group filters by dimension and extractionFn.
        final Map<BoundRefKey, List<SelectorDimFilter>> selectors = Maps.newHashMap();
        for (DimFilter child : children) {
            if (child instanceof SelectorDimFilter) {
                final SelectorDimFilter selector = (SelectorDimFilter) child;
                final BoundRefKey boundRefKey = BoundRefKey.from(selector, sourceRowSignature.naturalStringComparator(RowExtraction.of(selector.getDimension(), selector.getExtractionFn())));
                List<SelectorDimFilter> filterList = selectors.get(boundRefKey);
                if (filterList == null) {
                    filterList = Lists.newArrayList();
                    selectors.put(boundRefKey, filterList);
                }
                filterList.add(selector);
            }
        }
        // Emit IN filters for each group of size > 1.
        for (Map.Entry<BoundRefKey, List<SelectorDimFilter>> entry : selectors.entrySet()) {
            final List<SelectorDimFilter> filterList = entry.getValue();
            if (filterList.size() > 1) {
                // We found a simplification. Remove the old filters and add new ones.
                final List<String> values = Lists.newArrayList();
                for (final SelectorDimFilter selector : filterList) {
                    values.add(selector.getValue());
                    if (!children.remove(selector)) {
                        throw new ISE("WTF?! Tried to remove selector but couldn't?");
                    }
                }
                children.add(new InDimFilter(entry.getKey().getDimension(), values, entry.getKey().getExtractionFn()));
            }
        }
        if (!children.equals(((OrDimFilter) filter).getFields())) {
            return children.size() == 1 ? children.get(0) : new OrDimFilter(children);
        } else {
            return filter;
        }
    } else {
        return filter;
    }
}
Also used : SelectorDimFilter(io.druid.query.filter.SelectorDimFilter) OrDimFilter(io.druid.query.filter.OrDimFilter) InDimFilter(io.druid.query.filter.InDimFilter) List(java.util.List) ISE(io.druid.java.util.common.ISE) InDimFilter(io.druid.query.filter.InDimFilter) OrDimFilter(io.druid.query.filter.OrDimFilter) SelectorDimFilter(io.druid.query.filter.SelectorDimFilter) DimFilter(io.druid.query.filter.DimFilter) Map(java.util.Map)

Aggregations

InDimFilter (io.druid.query.filter.InDimFilter)26 SelectorDimFilter (io.druid.query.filter.SelectorDimFilter)20 Test (org.junit.Test)20 BoundDimFilter (io.druid.query.filter.BoundDimFilter)19 JavaScriptDimFilter (io.druid.query.filter.JavaScriptDimFilter)12 RegexDimFilter (io.druid.query.filter.RegexDimFilter)12 SearchQueryDimFilter (io.druid.query.filter.SearchQueryDimFilter)12 ContainsSearchQuerySpec (io.druid.query.search.search.ContainsSearchQuerySpec)11 DimFilter (io.druid.query.filter.DimFilter)10 ArrayList (java.util.ArrayList)10 DefaultDimensionSpec (io.druid.query.dimension.DefaultDimensionSpec)8 Row (io.druid.data.input.Row)6 AndDimFilter (io.druid.query.filter.AndDimFilter)6 OrDimFilter (io.druid.query.filter.OrDimFilter)6 LookupExtractionFn (io.druid.query.lookup.LookupExtractionFn)6 LongSumAggregatorFactory (io.druid.query.aggregation.LongSumAggregatorFactory)5 ListFilteredDimensionSpec (io.druid.query.dimension.ListFilteredDimensionSpec)5 RegexFilteredDimensionSpec (io.druid.query.dimension.RegexFilteredDimensionSpec)5 HashMap (java.util.HashMap)5 MapLookupExtractor (io.druid.query.extraction.MapLookupExtractor)4