Search in sources :

Example 1 with BooleanFilter

use of org.apache.druid.query.filter.BooleanFilter 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)

Aggregations

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