Search in sources :

Example 1 with NotFilter

use of org.apache.druid.segment.filter.NotFilter 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 2 with NotFilter

use of org.apache.druid.segment.filter.NotFilter 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

ArrayList (java.util.ArrayList)2 BooleanFilter (org.apache.druid.query.filter.BooleanFilter)2 Filter (org.apache.druid.query.filter.Filter)2 AndFilter (org.apache.druid.segment.filter.AndFilter)2 NotFilter (org.apache.druid.segment.filter.NotFilter)2 OrFilter (org.apache.druid.segment.filter.OrFilter)2