Merge pull request #2704 from navis/simple-optimize

optimize single elemented and/or filter
This commit is contained in:
Himanshu 2016-03-24 16:13:48 -05:00
commit 56343c6cdc
4 changed files with 46 additions and 24 deletions

View File

@ -21,15 +21,12 @@ package io.druid.query.filter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import io.druid.query.Druids;
import io.druid.segment.filter.AndFilter;
import io.druid.segment.filter.Filters;
import java.util.Collections;
import java.util.List;
/**
@ -45,7 +42,7 @@ public class AndDimFilter implements DimFilter
@JsonProperty("fields") List<DimFilter> fields
)
{
fields.removeAll(Collections.singletonList(null));
fields = DimFilters.filterNulls(fields);
Preconditions.checkArgument(fields.size() > 0, "AND operator requires at least one field");
this.fields = fields;
}
@ -65,14 +62,8 @@ public class AndDimFilter implements DimFilter
@Override
public DimFilter optimize()
{
return Druids.newAndDimFilterBuilder().fields(Lists.transform(this.getFields(), new Function<DimFilter, DimFilter>()
{
@Override
public DimFilter apply(DimFilter input)
{
return input.optimize();
}
})).build();
List<DimFilter> elements = DimFilters.optimize(fields);
return elements.size() == 1 ? elements.get(0) : Druids.newAndDimFilterBuilder().fields(elements).build();
}
@Override

View File

@ -20,6 +20,9 @@
package io.druid.query.filter;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import java.util.Arrays;
@ -80,4 +83,25 @@ public class DimFilters
)
);
}
public static List<DimFilter> optimize(List<DimFilter> filters)
{
return filterNulls(
Lists.transform(
filters, new Function<DimFilter, DimFilter>()
{
@Override
public DimFilter apply(DimFilter input)
{
return input.optimize();
}
}
)
);
}
public static List<DimFilter> filterNulls(List<DimFilter> optimized)
{
return Lists.newArrayList(Iterables.filter(optimized, Predicates.notNull()));
}
}

View File

@ -21,15 +21,12 @@ package io.druid.query.filter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import io.druid.query.Druids;
import io.druid.segment.filter.Filters;
import io.druid.segment.filter.OrFilter;
import java.util.Collections;
import java.util.List;
/**
@ -45,7 +42,7 @@ public class OrDimFilter implements DimFilter
@JsonProperty("fields") List<DimFilter> fields
)
{
fields.removeAll(Collections.singletonList(null));
fields = DimFilters.filterNulls(fields);
Preconditions.checkArgument(fields.size() > 0, "OR operator requires at least one field");
this.fields = fields;
}
@ -65,14 +62,8 @@ public class OrDimFilter implements DimFilter
@Override
public DimFilter optimize()
{
return Druids.newOrDimFilterBuilder().fields(Lists.transform(this.getFields(), new Function<DimFilter, DimFilter>()
{
@Override
public DimFilter apply(DimFilter input)
{
return input.optimize();
}
})).build();
List<DimFilter> elements = DimFilters.optimize(fields);
return elements.size() == 1 ? elements.get(0) : Druids.newOrDimFilterBuilder().fields(elements).build();
}
@Override

View File

@ -33,4 +33,20 @@ public class SelectorDimFilterTest
SelectorDimFilter selectorDimFilter2 = new SelectorDimFilter("ab", "cd");
Assert.assertFalse(Arrays.equals(selectorDimFilter.getCacheKey(), selectorDimFilter2.getCacheKey()));
}
@Test
public void testSimpleOptimize()
{
SelectorDimFilter selectorDimFilter = new SelectorDimFilter("abc", "d");
DimFilter filter = new AndDimFilter(
Arrays.<DimFilter>asList(
new OrDimFilter(
Arrays.<DimFilter>asList(
new AndDimFilter(Arrays.<DimFilter>asList(selectorDimFilter, null))
)
)
)
);
Assert.assertEquals(selectorDimFilter, filter.optimize());
}
}