Search in sources :

Example 11 with OrFilter

use of org.apache.druid.segment.filter.OrFilter in project druid by druid-io.

the class HiveCnfHelper method generateAllCombinations.

// A helper function 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 void generateAllCombinations(List<Filter> result, List<Filter> andList, List<Filter> nonAndList) {
    List<Filter> children = new ArrayList<>(((AndFilter) andList.get(0)).getFilters());
    if (result.isEmpty()) {
        for (Filter child : children) {
            List<Filter> a = new ArrayList<>(nonAndList);
            a.add(child);
            // Result must receive an actual OrFilter, so wrap if Filters.or managed to un-OR it.
            result.add(idempotentOr(Filters.or(a)));
        }
    } else {
        List<Filter> work = new ArrayList<>(result);
        result.clear();
        for (Filter child : children) {
            for (Filter or : work) {
                List<Filter> a = new ArrayList<>((((OrFilter) or).getFilters()));
                a.add(child);
                // Result must receive an actual OrFilter.
                result.add(idempotentOr(Filters.or(a)));
            }
        }
    }
    if (andList.size() > 1) {
        generateAllCombinations(result, andList.subList(1, andList.size()), nonAndList);
    }
}
Also used : BooleanFilter(org.apache.druid.query.filter.BooleanFilter) AndFilter(org.apache.druid.segment.filter.AndFilter) OrFilter(org.apache.druid.segment.filter.OrFilter) NotFilter(org.apache.druid.segment.filter.NotFilter) Filter(org.apache.druid.query.filter.Filter) ArrayList(java.util.ArrayList) OrFilter(org.apache.druid.segment.filter.OrFilter)

Example 12 with OrFilter

use of org.apache.druid.segment.filter.OrFilter in project druid by druid-io.

the class HiveCnfHelper method flatten.

public static Filter flatten(Filter root) {
    if (root instanceof BooleanFilter) {
        List<Filter> children = new ArrayList<>(((BooleanFilter) root).getFilters());
        // they don't get re-visited
        for (int i = 0; i < children.size(); ++i) {
            Filter child = flatten(children.get(i));
            // do we need to flatten?
            if (child.getClass() == root.getClass() && !(child instanceof NotFilter)) {
                boolean first = true;
                List<Filter> grandKids = new ArrayList<>(((BooleanFilter) child).getFilters());
                for (Filter grandkid : grandKids) {
                    // for the first grandkid replace the original parent
                    if (first) {
                        first = false;
                        children.set(i, grandkid);
                    } else {
                        children.add(++i, grandkid);
                    }
                }
            } else {
                children.set(i, child);
            }
        }
        // if we have a singleton AND or OR, just return the child
        if (children.size() == 1 && (root instanceof AndFilter || root instanceof OrFilter)) {
            return children.get(0);
        }
        if (root instanceof AndFilter) {
            return new AndFilter(children);
        } else if (root instanceof OrFilter) {
            return new OrFilter(children);
        }
    }
    return root;
}
Also used : BooleanFilter(org.apache.druid.query.filter.BooleanFilter) AndFilter(org.apache.druid.segment.filter.AndFilter) BooleanFilter(org.apache.druid.query.filter.BooleanFilter) AndFilter(org.apache.druid.segment.filter.AndFilter) OrFilter(org.apache.druid.segment.filter.OrFilter) NotFilter(org.apache.druid.segment.filter.NotFilter) Filter(org.apache.druid.query.filter.Filter) NotFilter(org.apache.druid.segment.filter.NotFilter) ArrayList(java.util.ArrayList) OrFilter(org.apache.druid.segment.filter.OrFilter)

Example 13 with OrFilter

use of org.apache.druid.segment.filter.OrFilter in project druid by druid-io.

the class HiveCnfHelper method convertToCnf.

public static Filter convertToCnf(Filter current) {
    if (current instanceof NotFilter) {
        return new NotFilter(convertToCnf(((NotFilter) current).getBaseFilter()));
    }
    if (current instanceof AndFilter) {
        List<Filter> children = new ArrayList<>();
        for (Filter child : ((AndFilter) current).getFilters()) {
            children.add(convertToCnf(child));
        }
        return Filters.and(children);
    }
    if (current instanceof OrFilter) {
        // a list of leaves that weren't under AND expressions
        List<Filter> nonAndList = new ArrayList<>();
        // a list of AND expressions that we need to distribute
        List<Filter> andList = new ArrayList<>();
        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
                nonAndList.addAll(((OrFilter) child).getFilters());
            } else {
                nonAndList.add(child);
            }
        }
        if (!andList.isEmpty()) {
            List<Filter> result = new ArrayList<>();
            generateAllCombinations(result, andList, nonAndList);
            return Filters.and(result);
        }
    }
    return current;
}
Also used : AndFilter(org.apache.druid.segment.filter.AndFilter) BooleanFilter(org.apache.druid.query.filter.BooleanFilter) AndFilter(org.apache.druid.segment.filter.AndFilter) OrFilter(org.apache.druid.segment.filter.OrFilter) NotFilter(org.apache.druid.segment.filter.NotFilter) Filter(org.apache.druid.query.filter.Filter) NotFilter(org.apache.druid.segment.filter.NotFilter) ArrayList(java.util.ArrayList) OrFilter(org.apache.druid.segment.filter.OrFilter)

Aggregations

Filter (org.apache.druid.query.filter.Filter)13 OrFilter (org.apache.druid.segment.filter.OrFilter)13 AndFilter (org.apache.druid.segment.filter.AndFilter)11 SelectorFilter (org.apache.druid.segment.filter.SelectorFilter)10 BoundDimFilter (org.apache.druid.query.filter.BoundDimFilter)8 BoundFilter (org.apache.druid.segment.filter.BoundFilter)8 InDimFilter (org.apache.druid.query.filter.InDimFilter)7 FalseFilter (org.apache.druid.segment.filter.FalseFilter)7 ExpressionDimFilter (org.apache.druid.query.filter.ExpressionDimFilter)6 JoinFilterPreAnalysis (org.apache.druid.segment.join.filter.JoinFilterPreAnalysis)6 JoinFilterSplit (org.apache.druid.segment.join.filter.JoinFilterSplit)6 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 BooleanFilter (org.apache.druid.query.filter.BooleanFilter)3 NotFilter (org.apache.druid.segment.filter.NotFilter)3 IndexedTableJoinable (org.apache.druid.segment.join.table.IndexedTableJoinable)3 AndDimFilter (org.apache.druid.query.filter.AndDimFilter)2 DimFilter (org.apache.druid.query.filter.DimFilter)2 OrDimFilter (org.apache.druid.query.filter.OrDimFilter)2 SelectorDimFilter (org.apache.druid.query.filter.SelectorDimFilter)2