Search in sources :

Example 1 with FalseDimFilter

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

the class CombineAndSimplifyBounds method doSimplify.

/**
 * Simplify BoundDimFilters that are children of an OR or an AND.
 *
 * @param children    the filters
 * @param disjunction true for OR, false for AND
 *
 * @return simplified filters
 */
private static DimFilter doSimplify(final List<DimFilter> children, boolean disjunction) {
    // Copy the list of child filters. We'll modify the copy and eventually return it.
    final List<DimFilter> newChildren = Lists.newArrayList(children);
    // Group Bound filters by dimension, extractionFn, and comparator and compute a RangeSet for each one.
    final Map<BoundRefKey, List<BoundDimFilter>> bounds = new HashMap<>();
    // all and/or filters have at least 1 child
    boolean allFalse = true;
    for (final DimFilter child : newChildren) {
        if (child instanceof BoundDimFilter) {
            final BoundDimFilter bound = (BoundDimFilter) child;
            final BoundRefKey boundRefKey = BoundRefKey.from(bound);
            final List<BoundDimFilter> filterList = bounds.computeIfAbsent(boundRefKey, k -> new ArrayList<>());
            filterList.add(bound);
            allFalse = false;
        } else {
            allFalse &= child instanceof FalseDimFilter;
        }
    }
    // short circuit if can never be true
    if (allFalse) {
        return Filtration.matchNothing();
    }
    // Try to simplify filters within each group.
    for (Map.Entry<BoundRefKey, List<BoundDimFilter>> entry : bounds.entrySet()) {
        final BoundRefKey boundRefKey = entry.getKey();
        final List<BoundDimFilter> filterList = entry.getValue();
        // Create a RangeSet for this group.
        final RangeSet<BoundValue> rangeSet = disjunction ? RangeSets.unionRanges(Bounds.toRanges(filterList)) : RangeSets.intersectRanges(Bounds.toRanges(filterList));
        if (rangeSet.asRanges().size() < filterList.size()) {
            // We found a simplification. Remove the old filters and add new ones.
            for (final BoundDimFilter bound : filterList) {
                if (!newChildren.remove(bound)) {
                    // Don't expect this to happen, but include it as a sanity check.
                    throw new ISE("Tried to remove bound, but couldn't");
                }
            }
            if (rangeSet.asRanges().isEmpty()) {
                // range set matches nothing, equivalent to FALSE
                newChildren.add(Filtration.matchNothing());
            }
            for (final Range<BoundValue> range : rangeSet.asRanges()) {
                if (!range.hasLowerBound() && !range.hasUpperBound()) {
                    // range matches all, equivalent to TRUE
                    newChildren.add(Filtration.matchEverything());
                } else {
                    newChildren.add(Bounds.toFilter(boundRefKey, range));
                }
            }
        }
    }
    // Finally: Go through newChildren, removing or potentially exiting early based on TRUE / FALSE marker filters.
    Preconditions.checkState(newChildren.size() > 0, "newChildren.size > 0");
    final Iterator<DimFilter> iterator = newChildren.iterator();
    while (iterator.hasNext()) {
        final DimFilter newChild = iterator.next();
        if (Filtration.matchNothing().equals(newChild)) {
            // AND with FALSE => always false, short circuit
            if (disjunction) {
                iterator.remove();
            } else {
                return Filtration.matchNothing();
            }
        } else if (Filtration.matchEverything().equals(newChild)) {
            // AND with TRUE => ignore
            if (disjunction) {
                return Filtration.matchEverything();
            } else {
                iterator.remove();
            }
        }
    }
    if (newChildren.isEmpty()) {
        // If "newChildren" is empty at this point, it must have consisted entirely of TRUE / FALSE marker filters.
        if (disjunction) {
            // Must have been all FALSE filters (the only kind we would have removed above).
            return Filtration.matchNothing();
        } else {
            // Must have been all TRUE filters (the only kind we would have removed above).
            return Filtration.matchEverything();
        }
    } else if (newChildren.size() == 1) {
        return newChildren.get(0);
    } else {
        return disjunction ? new OrDimFilter(newChildren) : new AndDimFilter(newChildren);
    }
}
Also used : BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) AndDimFilter(org.apache.druid.query.filter.AndDimFilter) HashMap(java.util.HashMap) FalseDimFilter(org.apache.druid.query.filter.FalseDimFilter) OrDimFilter(org.apache.druid.query.filter.OrDimFilter) ArrayList(java.util.ArrayList) List(java.util.List) ISE(org.apache.druid.java.util.common.ISE) AndDimFilter(org.apache.druid.query.filter.AndDimFilter) NotDimFilter(org.apache.druid.query.filter.NotDimFilter) FalseDimFilter(org.apache.druid.query.filter.FalseDimFilter) DimFilter(org.apache.druid.query.filter.DimFilter) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) OrDimFilter(org.apache.druid.query.filter.OrDimFilter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with FalseDimFilter

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

the class CombineAndSimplifyBounds method process.

@Override
public DimFilter process(DimFilter filter) {
    if (filter instanceof FalseDimFilter) {
        // we might sometimes come into here with just a false from optimizing impossible conditions
        return filter;
    } else if (filter instanceof AndDimFilter) {
        final List<DimFilter> children = getAndFilterChildren((AndDimFilter) filter);
        final DimFilter one = doSimplifyAnd(children);
        final DimFilter two = negate(doSimplifyOr(negateAll(children)));
        return computeCost(one) <= computeCost(two) ? one : two;
    } else if (filter instanceof OrDimFilter) {
        final List<DimFilter> children = getOrFilterChildren((OrDimFilter) filter);
        final DimFilter one = doSimplifyOr(children);
        final DimFilter two = negate(doSimplifyAnd(negateAll(children)));
        return computeCost(one) <= computeCost(two) ? one : two;
    } else if (filter instanceof NotDimFilter) {
        final DimFilter field = ((NotDimFilter) filter).getField();
        final DimFilter candidate;
        if (field instanceof OrDimFilter) {
            candidate = doSimplifyAnd(negateAll(getOrFilterChildren((OrDimFilter) field)));
        } else if (field instanceof AndDimFilter) {
            candidate = doSimplifyOr(negateAll(getAndFilterChildren((AndDimFilter) field)));
        } else {
            candidate = negate(field);
        }
        return computeCost(filter) <= computeCost(candidate) ? filter : candidate;
    } else {
        return filter;
    }
}
Also used : NotDimFilter(org.apache.druid.query.filter.NotDimFilter) AndDimFilter(org.apache.druid.query.filter.AndDimFilter) OrDimFilter(org.apache.druid.query.filter.OrDimFilter) ArrayList(java.util.ArrayList) List(java.util.List) FalseDimFilter(org.apache.druid.query.filter.FalseDimFilter) AndDimFilter(org.apache.druid.query.filter.AndDimFilter) NotDimFilter(org.apache.druid.query.filter.NotDimFilter) FalseDimFilter(org.apache.druid.query.filter.FalseDimFilter) DimFilter(org.apache.druid.query.filter.DimFilter) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) OrDimFilter(org.apache.druid.query.filter.OrDimFilter)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 AndDimFilter (org.apache.druid.query.filter.AndDimFilter)2 BoundDimFilter (org.apache.druid.query.filter.BoundDimFilter)2 DimFilter (org.apache.druid.query.filter.DimFilter)2 FalseDimFilter (org.apache.druid.query.filter.FalseDimFilter)2 NotDimFilter (org.apache.druid.query.filter.NotDimFilter)2 OrDimFilter (org.apache.druid.query.filter.OrDimFilter)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ISE (org.apache.druid.java.util.common.ISE)1