diff --git a/processing/src/main/java/io/druid/query/filter/AndDimFilter.java b/processing/src/main/java/io/druid/query/filter/AndDimFilter.java index 0364fa2e412..645db17ab3d 100644 --- a/processing/src/main/java/io/druid/query/filter/AndDimFilter.java +++ b/processing/src/main/java/io/druid/query/filter/AndDimFilter.java @@ -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 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() - { - @Override - public DimFilter apply(DimFilter input) - { - return input.optimize(); - } - })).build(); + List elements = DimFilters.optimize(fields); + return elements.size() == 1 ? elements.get(0) : Druids.newAndDimFilterBuilder().fields(elements).build(); } @Override diff --git a/processing/src/main/java/io/druid/query/filter/DimFilters.java b/processing/src/main/java/io/druid/query/filter/DimFilters.java index fe8808a8601..8c084fbb09c 100644 --- a/processing/src/main/java/io/druid/query/filter/DimFilters.java +++ b/processing/src/main/java/io/druid/query/filter/DimFilters.java @@ -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 optimize(List filters) + { + return filterNulls( + Lists.transform( + filters, new Function() + { + @Override + public DimFilter apply(DimFilter input) + { + return input.optimize(); + } + } + ) + ); + } + + public static List filterNulls(List optimized) + { + return Lists.newArrayList(Iterables.filter(optimized, Predicates.notNull())); + } } diff --git a/processing/src/main/java/io/druid/query/filter/OrDimFilter.java b/processing/src/main/java/io/druid/query/filter/OrDimFilter.java index 5016e6e41ad..52cbdb89210 100644 --- a/processing/src/main/java/io/druid/query/filter/OrDimFilter.java +++ b/processing/src/main/java/io/druid/query/filter/OrDimFilter.java @@ -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 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() - { - @Override - public DimFilter apply(DimFilter input) - { - return input.optimize(); - } - })).build(); + List elements = DimFilters.optimize(fields); + return elements.size() == 1 ? elements.get(0) : Druids.newOrDimFilterBuilder().fields(elements).build(); } @Override diff --git a/processing/src/test/java/io/druid/query/filter/SelectorDimFilterTest.java b/processing/src/test/java/io/druid/query/filter/SelectorDimFilterTest.java index d146b1bc4d7..577297214dc 100644 --- a/processing/src/test/java/io/druid/query/filter/SelectorDimFilterTest.java +++ b/processing/src/test/java/io/druid/query/filter/SelectorDimFilterTest.java @@ -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.asList( + new OrDimFilter( + Arrays.asList( + new AndDimFilter(Arrays.asList(selectorDimFilter, null)) + ) + ) + ) + ); + Assert.assertEquals(selectorDimFilter, filter.optimize()); + } }