Search in sources :

Example 1 with BooleanFilter

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

the class Filters method flatten.

// CNF conversion functions were 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 Filter flatten(Filter root) {
    if (root instanceof BooleanFilter) {
        List<Filter> children = new ArrayList<>();
        children.addAll(((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 = ((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(io.druid.query.filter.BooleanFilter) DimFilter(io.druid.query.filter.DimFilter) BooleanFilter(io.druid.query.filter.BooleanFilter) Filter(io.druid.query.filter.Filter) ArrayList(java.util.ArrayList)

Aggregations

BooleanFilter (io.druid.query.filter.BooleanFilter)1 DimFilter (io.druid.query.filter.DimFilter)1 Filter (io.druid.query.filter.Filter)1 ArrayList (java.util.ArrayList)1