optimize single elemented and/or filter

This commit is contained in:
navis.ryu 2016-03-23 09:25:15 +09:00
parent 451c0bc6d8
commit 91f6be4884
4 changed files with 46 additions and 24 deletions

View File

@ -21,13 +21,10 @@ 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 java.util.Collections;
import java.util.List;
/**
@ -43,7 +40,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;
}
@ -63,14 +60,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,13 +21,10 @@ 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 java.util.Collections;
import java.util.List;
/**
@ -43,7 +40,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;
}
@ -63,14 +60,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());
}
}