Skip to content

Commit

Permalink
optimize single elemented and/or filter
Browse files Browse the repository at this point in the history
  • Loading branch information
navis committed Mar 23, 2016
1 parent 451c0bc commit 91f6be4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
15 changes: 3 additions & 12 deletions processing/src/main/java/io/druid/query/filter/AndDimFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@

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;

/**
Expand All @@ -43,7 +40,7 @@ public AndDimFilter(
@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;
}
Expand All @@ -63,14 +60,8 @@ public byte[] getCacheKey()
@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
Expand Down
24 changes: 24 additions & 0 deletions processing/src/main/java/io/druid/query/filter/DimFilters.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,4 +83,25 @@ public DimFilter apply(String input)
)
);
}

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()));
}
}
15 changes: 3 additions & 12 deletions processing/src/main/java/io/druid/query/filter/OrDimFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@

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;

/**
Expand All @@ -43,7 +40,7 @@ public OrDimFilter(
@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;
}
Expand All @@ -63,14 +60,8 @@ public byte[] getCacheKey()
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,20 @@ public void testGetCacheKey()
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());
}
}

0 comments on commit 91f6be4

Please sign in to comment.