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;
}
Aggregations