add method getRequiredColumns for DimFilter (#5872)

* add method getRequiredColumns for DimFilter

* deal with the NullPointerException when DimFilter is null
This commit is contained in:
zhangxinyu 2018-06-28 06:45:46 +08:00 committed by Jonathan Wei
parent 0f429298cf
commit d857345b7d
26 changed files with 261 additions and 62 deletions

View File

@ -20,24 +20,12 @@
package io.druid.query.materializedview; package io.druid.query.materializedview;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import io.druid.java.util.common.Intervals; import io.druid.java.util.common.Intervals;
import io.druid.java.util.common.JodaUtils; import io.druid.java.util.common.JodaUtils;
import io.druid.query.Query; import io.druid.query.Query;
import io.druid.query.aggregation.AggregatorFactory; import io.druid.query.aggregation.AggregatorFactory;
import io.druid.query.aggregation.FilteredAggregatorFactory; import io.druid.query.aggregation.FilteredAggregatorFactory;
import io.druid.query.dimension.DimensionSpec; import io.druid.query.dimension.DimensionSpec;
import io.druid.query.filter.AndDimFilter;
import io.druid.query.filter.BoundDimFilter;
import io.druid.query.filter.DimFilter;
import io.druid.query.filter.InDimFilter;
import io.druid.query.filter.IntervalDimFilter;
import io.druid.query.filter.LikeDimFilter;
import io.druid.query.filter.NotDimFilter;
import io.druid.query.filter.OrDimFilter;
import io.druid.query.filter.RegexDimFilter;
import io.druid.query.filter.SearchQueryDimFilter;
import io.druid.query.filter.SelectorDimFilter;
import io.druid.query.groupby.GroupByQuery; import io.druid.query.groupby.GroupByQuery;
import io.druid.query.timeseries.TimeseriesQuery; import io.druid.query.timeseries.TimeseriesQuery;
import io.druid.query.topn.TopNQuery; import io.druid.query.topn.TopNQuery;
@ -60,10 +48,9 @@ public class MaterializedViewUtils
public static Set<String> getRequiredFields(Query query) public static Set<String> getRequiredFields(Query query)
{ {
Set<String> dimensions = new HashSet<>(); Set<String> dimensions = new HashSet<>();
Set<String> dimsInFilter = getDimensionsInFilter(query.getFilter()); Set<String> dimsInFilter = null == query.getFilter() ? new HashSet<String>() : query.getFilter().getRequiredColumns();
if (dimsInFilter != null) {
dimensions.addAll(dimsInFilter); dimensions.addAll(dimsInFilter);
}
if (query instanceof TopNQuery) { if (query instanceof TopNQuery) {
TopNQuery q = (TopNQuery) query; TopNQuery q = (TopNQuery) query;
dimensions.addAll(extractFieldsFromAggregations(q.getAggregatorSpecs())); dimensions.addAll(extractFieldsFromAggregations(q.getAggregatorSpecs()));
@ -90,58 +77,13 @@ public class MaterializedViewUtils
for (AggregatorFactory agg : aggs) { for (AggregatorFactory agg : aggs) {
if (agg instanceof FilteredAggregatorFactory) { if (agg instanceof FilteredAggregatorFactory) {
FilteredAggregatorFactory fagg = (FilteredAggregatorFactory) agg; FilteredAggregatorFactory fagg = (FilteredAggregatorFactory) agg;
ret.addAll(getDimensionsInFilter(fagg.getFilter())); ret.addAll(fagg.getFilter().getRequiredColumns());
} }
ret.addAll(agg.requiredFields()); ret.addAll(agg.requiredFields());
} }
return ret; return ret;
} }
private static Set<String> getDimensionsInFilter(DimFilter dimFilter)
{
if (dimFilter instanceof AndDimFilter) {
AndDimFilter d = (AndDimFilter) dimFilter;
Set<String> ret = new HashSet<>();
for (DimFilter filter : d.getFields()) {
ret.addAll(getDimensionsInFilter(filter));
}
return ret;
} else if (dimFilter instanceof OrDimFilter) {
OrDimFilter d = (OrDimFilter) dimFilter;
Set<String> ret = new HashSet<>();
for (DimFilter filter : d.getFields()) {
ret.addAll(getDimensionsInFilter(filter));
}
return ret;
} else if (dimFilter instanceof NotDimFilter) {
NotDimFilter d = (NotDimFilter) dimFilter;
return getDimensionsInFilter(d.getField());
} else if (dimFilter instanceof BoundDimFilter) {
BoundDimFilter d = (BoundDimFilter) dimFilter;
return Sets.newHashSet(d.getDimension());
} else if (dimFilter instanceof InDimFilter) {
InDimFilter d = (InDimFilter) dimFilter;
return Sets.newHashSet(d.getDimension());
} else if (dimFilter instanceof IntervalDimFilter) {
IntervalDimFilter d = (IntervalDimFilter) dimFilter;
return Sets.newHashSet(d.getDimension());
} else if (dimFilter instanceof LikeDimFilter) {
LikeDimFilter d = (LikeDimFilter) dimFilter;
return Sets.newHashSet(d.getDimension());
} else if (dimFilter instanceof RegexDimFilter) {
RegexDimFilter d = (RegexDimFilter) dimFilter;
return Sets.newHashSet(d.getDimension());
} else if (dimFilter instanceof SearchQueryDimFilter) {
SearchQueryDimFilter d = (SearchQueryDimFilter) dimFilter;
return Sets.newHashSet(d.getDimension());
} else if (dimFilter instanceof SelectorDimFilter) {
SelectorDimFilter d = (SelectorDimFilter) dimFilter;
return Sets.newHashSet(d.getDimension());
} else {
return null;
}
}
/** /**
* caculate the intervals which are covered by interval2, but not covered by interval1. * caculate the intervals which are covered by interval2, but not covered by interval1.
* result intervals = interval2 - interval1 interval2 * result intervals = interval2 - interval1 interval2

View File

@ -24,12 +24,14 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;
import com.google.common.collect.TreeRangeSet; import com.google.common.collect.TreeRangeSet;
import io.druid.java.util.common.StringUtils; import io.druid.java.util.common.StringUtils;
import io.druid.segment.filter.AndFilter; import io.druid.segment.filter.AndFilter;
import io.druid.segment.filter.Filters; import io.druid.segment.filter.Filters;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet;
import java.util.List; import java.util.List;
/** /**
@ -97,6 +99,15 @@ public class AndDimFilter implements DimFilter
return retSet; return retSet;
} }
@Override
public HashSet<String> getRequiredColumns()
{
HashSet<String> requiredColumns = Sets.newHashSet();
fields.stream()
.forEach(field -> requiredColumns.addAll(field.getRequiredColumns()));
return requiredColumns;
}
@Override @Override
public boolean equals(Object o) public boolean equals(Object o)
{ {

View File

@ -26,6 +26,7 @@ import com.google.common.base.Supplier;
import com.google.common.collect.BoundType; import com.google.common.collect.BoundType;
import com.google.common.collect.Range; import com.google.common.collect.Range;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;
import com.google.common.collect.TreeRangeSet; import com.google.common.collect.TreeRangeSet;
import com.google.common.primitives.Doubles; import com.google.common.primitives.Doubles;
import com.google.common.primitives.Floats; import com.google.common.primitives.Floats;
@ -40,6 +41,7 @@ import javax.annotation.Nullable;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
public class BoundDimFilter implements DimFilter public class BoundDimFilter implements DimFilter
@ -246,6 +248,12 @@ public class BoundDimFilter implements DimFilter
return retSet; return retSet;
} }
@Override
public HashSet<String> getRequiredColumns()
{
return Sets.newHashSet(dimension);
}
@Override @Override
public boolean equals(Object o) public boolean equals(Object o)
{ {

View File

@ -24,11 +24,14 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;
import io.druid.query.cache.CacheKeyBuilder; import io.druid.query.cache.CacheKeyBuilder;
import io.druid.query.dimension.DimensionSpec; import io.druid.query.dimension.DimensionSpec;
import io.druid.segment.filter.ColumnComparisonFilter; import io.druid.segment.filter.ColumnComparisonFilter;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
/** /**
*/ */
@ -103,6 +106,15 @@ public class ColumnComparisonDimFilter implements DimFilter
return null; return null;
} }
@Override
public HashSet<String> getRequiredColumns()
{
return Sets.newHashSet(dimensions.stream()
.map(DimensionSpec::getDimension)
.collect(Collectors.toSet())
);
}
@Override @Override
public int hashCode() public int hashCode()
{ {

View File

@ -24,6 +24,8 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import io.druid.java.util.common.Cacheable; import io.druid.java.util.common.Cacheable;
import java.util.HashSet;
/** /**
*/ */
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@ -75,4 +77,9 @@ public interface DimFilter extends Cacheable
* determine for this DimFilter. * determine for this DimFilter.
*/ */
RangeSet<String> getDimensionRangeSet(String dimension); RangeSet<String> getDimensionRangeSet(String dimension);
/**
* @return a HashSet that represents all columns' name which the DimFilter required to do filter.
*/
HashSet<String> getRequiredColumns();
} }

View File

@ -23,12 +23,14 @@ import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;
import io.druid.math.expr.Expr; import io.druid.math.expr.Expr;
import io.druid.math.expr.ExprMacroTable; import io.druid.math.expr.ExprMacroTable;
import io.druid.math.expr.Parser; import io.druid.math.expr.Parser;
import io.druid.query.cache.CacheKeyBuilder; import io.druid.query.cache.CacheKeyBuilder;
import io.druid.segment.filter.ExpressionFilter; import io.druid.segment.filter.ExpressionFilter;
import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
public class ExpressionDimFilter implements DimFilter public class ExpressionDimFilter implements DimFilter
@ -70,6 +72,12 @@ public class ExpressionDimFilter implements DimFilter
return null; return null;
} }
@Override
public HashSet<String> getRequiredColumns()
{
return Sets.newHashSet(Parser.findRequiredBindings(parsed));
}
@Override @Override
public byte[] getCacheKey() public byte[] getCacheKey()
{ {

View File

@ -23,10 +23,12 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;
import io.druid.java.util.common.StringUtils; import io.druid.java.util.common.StringUtils;
import io.druid.query.extraction.ExtractionFn; import io.druid.query.extraction.ExtractionFn;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.HashSet;
/** /**
* This class is deprecated, use SelectorDimFilter instead: {@link SelectorDimFilter} * This class is deprecated, use SelectorDimFilter instead: {@link SelectorDimFilter}
@ -110,6 +112,12 @@ public class ExtractionDimFilter implements DimFilter
return null; return null;
} }
@Override
public HashSet<String> getRequiredColumns()
{
return Sets.newHashSet(dimension);
}
@Override @Override
public String toString() public String toString()
{ {

View File

@ -30,6 +30,7 @@ import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Range; import com.google.common.collect.Range;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;
import com.google.common.collect.TreeRangeSet; import com.google.common.collect.TreeRangeSet;
import com.google.common.primitives.Doubles; import com.google.common.primitives.Doubles;
import com.google.common.primitives.Floats; import com.google.common.primitives.Floats;
@ -48,6 +49,7 @@ import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
@ -215,6 +217,12 @@ public class InDimFilter implements DimFilter
return retSet; return retSet;
} }
@Override
public HashSet<String> getRequiredColumns()
{
return Sets.newHashSet(dimension);
}
@Override @Override
public boolean equals(Object o) public boolean equals(Object o)
{ {

View File

@ -23,6 +23,7 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;
import com.google.common.primitives.Longs; import com.google.common.primitives.Longs;
import io.druid.java.util.common.JodaUtils; import io.druid.java.util.common.JodaUtils;
import io.druid.java.util.common.Pair; import io.druid.java.util.common.Pair;
@ -34,6 +35,7 @@ import org.joda.time.Interval;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.List; import java.util.List;
public class IntervalDimFilter implements DimFilter public class IntervalDimFilter implements DimFilter
@ -122,6 +124,12 @@ public class IntervalDimFilter implements DimFilter
return convertedFilter.getDimensionRangeSet(dimension); return convertedFilter.getDimensionRangeSet(dimension);
} }
@Override
public HashSet<String> getRequiredColumns()
{
return Sets.newHashSet(dimension);
}
@Override @Override
public boolean equals(Object o) public boolean equals(Object o)
{ {

View File

@ -25,6 +25,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;
import io.druid.java.util.common.StringUtils; import io.druid.java.util.common.StringUtils;
import io.druid.js.JavaScriptConfig; import io.druid.js.JavaScriptConfig;
import io.druid.query.extraction.ExtractionFn; import io.druid.query.extraction.ExtractionFn;
@ -34,6 +35,7 @@ import org.mozilla.javascript.Function;
import org.mozilla.javascript.ScriptableObject; import org.mozilla.javascript.ScriptableObject;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.HashSet;
public class JavaScriptDimFilter implements DimFilter public class JavaScriptDimFilter implements DimFilter
{ {
@ -134,6 +136,12 @@ public class JavaScriptDimFilter implements DimFilter
return null; return null;
} }
@Override
public HashSet<String> getRequiredColumns()
{
return Sets.newHashSet(dimension);
}
@Override @Override
public String toString() public String toString()
{ {

View File

@ -25,6 +25,7 @@ import com.google.common.base.Preconditions;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;
import com.google.common.io.BaseEncoding; import com.google.common.io.BaseEncoding;
import com.google.common.primitives.Chars; import com.google.common.primitives.Chars;
import io.druid.java.util.common.StringUtils; import io.druid.java.util.common.StringUtils;
@ -34,6 +35,7 @@ import io.druid.segment.filter.LikeFilter;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class LikeDimFilter implements DimFilter public class LikeDimFilter implements DimFilter
@ -306,6 +308,12 @@ public class LikeDimFilter implements DimFilter
return null; return null;
} }
@Override
public HashSet<String> getRequiredColumns()
{
return Sets.newHashSet(dimension);
}
@Override @Override
public boolean equals(Object o) public boolean equals(Object o)
{ {

View File

@ -22,6 +22,7 @@ package io.druid.query.filter;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.HashSet;
/** /**
*/ */
@ -50,4 +51,10 @@ public class NoopDimFilter implements DimFilter
{ {
return null; return null;
} }
@Override
public HashSet<String> getRequiredColumns()
{
return null;
}
} }

View File

@ -27,6 +27,7 @@ import com.google.common.collect.RangeSet;
import io.druid.segment.filter.NotFilter; import io.druid.segment.filter.NotFilter;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.List; import java.util.List;
/** /**
@ -100,6 +101,12 @@ public class NotDimFilter implements DimFilter
return rangeSet == null ? null : rangeSet.complement(); return rangeSet == null ? null : rangeSet.complement();
} }
@Override
public HashSet<String> getRequiredColumns()
{
return field.getRequiredColumns();
}
@Override @Override
public boolean equals(Object o) public boolean equals(Object o)
{ {

View File

@ -24,6 +24,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;
import com.google.common.collect.TreeRangeSet; import com.google.common.collect.TreeRangeSet;
import io.druid.java.util.common.StringUtils; import io.druid.java.util.common.StringUtils;
import io.druid.segment.filter.Filters; import io.druid.segment.filter.Filters;
@ -31,6 +32,7 @@ import io.druid.segment.filter.OrFilter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet;
import java.util.List; import java.util.List;
/** /**
@ -105,6 +107,15 @@ public class OrDimFilter implements DimFilter
return retSet; return retSet;
} }
@Override
public HashSet<String> getRequiredColumns()
{
HashSet<String> requiredColumns = Sets.newHashSet();
fields.stream()
.forEach(field -> requiredColumns.addAll(field.getRequiredColumns()));
return requiredColumns;
}
@Override @Override
public boolean equals(Object o) public boolean equals(Object o)
{ {

View File

@ -23,11 +23,13 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;
import io.druid.java.util.common.StringUtils; import io.druid.java.util.common.StringUtils;
import io.druid.query.extraction.ExtractionFn; import io.druid.query.extraction.ExtractionFn;
import io.druid.segment.filter.RegexFilter; import io.druid.segment.filter.RegexFilter;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
@ -108,6 +110,12 @@ public class RegexDimFilter implements DimFilter
return null; return null;
} }
@Override
public HashSet<String> getRequiredColumns()
{
return Sets.newHashSet(dimension);
}
@Override @Override
public String toString() public String toString()
{ {

View File

@ -22,12 +22,14 @@ package io.druid.query.filter;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;
import io.druid.java.util.common.StringUtils; import io.druid.java.util.common.StringUtils;
import io.druid.query.extraction.ExtractionFn; import io.druid.query.extraction.ExtractionFn;
import io.druid.query.search.SearchQuerySpec; import io.druid.query.search.SearchQuerySpec;
import io.druid.segment.filter.SearchQueryFilter; import io.druid.segment.filter.SearchQueryFilter;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.HashSet;
/** /**
*/ */
@ -104,6 +106,12 @@ public class SearchQueryDimFilter implements DimFilter
return null; return null;
} }
@Override
public HashSet<String> getRequiredColumns()
{
return Sets.newHashSet(dimension);
}
@Override @Override
public String toString() public String toString()
{ {

View File

@ -28,6 +28,7 @@ import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Range; import com.google.common.collect.Range;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;
import com.google.common.collect.TreeRangeSet; import com.google.common.collect.TreeRangeSet;
import com.google.common.primitives.Doubles; import com.google.common.primitives.Doubles;
import com.google.common.primitives.Floats; import com.google.common.primitives.Floats;
@ -38,6 +39,7 @@ import io.druid.segment.filter.DimensionPredicateFilter;
import io.druid.segment.filter.SelectorFilter; import io.druid.segment.filter.SelectorFilter;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
/** /**
@ -192,6 +194,12 @@ public class SelectorDimFilter implements DimFilter
return retSet; return retSet;
} }
@Override
public HashSet<String> getRequiredColumns()
{
return Sets.newHashSet(dimension);
}
@Override @Override
public int hashCode() public int hashCode()
{ {

View File

@ -22,11 +22,13 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.RangeSet; import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;
import io.druid.collections.spatial.search.Bound; import io.druid.collections.spatial.search.Bound;
import io.druid.java.util.common.StringUtils; import io.druid.java.util.common.StringUtils;
import io.druid.segment.filter.SpatialFilter; import io.druid.segment.filter.SpatialFilter;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.HashSet;
/** /**
*/ */
@ -92,6 +94,12 @@ public class SpatialDimFilter implements DimFilter
return null; return null;
} }
@Override
public HashSet<String> getRequiredColumns()
{
return Sets.newHashSet(dimension);
}
@Override @Override
public boolean equals(Object o) public boolean equals(Object o)
{ {

View File

@ -0,0 +1,40 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.druid.query.filter;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.junit.Assert;
import org.junit.Test;
public class AndDimFilterTest
{
@Test
public void testGetRequiredColumns()
{
AndDimFilter andDimFilter = new AndDimFilter(
Lists.newArrayList(
new SelectorDimFilter("a", "d", null),
new SelectorDimFilter("b", "d", null),
new SelectorDimFilter("c", "d", null)
)
);
Assert.assertEquals(andDimFilter.getRequiredColumns(), Sets.newHashSet("a", "b", "c"));
}
}

View File

@ -42,6 +42,7 @@ package io.druid.query.filter;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.google.inject.Key; import com.google.inject.Key;
import io.druid.guice.GuiceInjectors; import io.druid.guice.GuiceInjectors;
@ -128,4 +129,11 @@ public class BoundDimFilterTest
Assert.assertNotEquals(boundDimFilter.hashCode(), boundDimFilterWithExtract.hashCode()); Assert.assertNotEquals(boundDimFilter.hashCode(), boundDimFilterWithExtract.hashCode());
} }
@Test
public void testGetRequiredColumns()
{
BoundDimFilter boundDimFilter = new BoundDimFilter("dimension", "12", "15", null, null, true, null, StringComparators.ALPHANUMERIC);
Assert.assertEquals(boundDimFilter.getRequiredColumns(), Sets.newHashSet("dimension"));
}
} }

View File

@ -20,6 +20,7 @@
package io.druid.query.filter; package io.druid.query.filter;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import io.druid.query.dimension.DefaultDimensionSpec; import io.druid.query.dimension.DefaultDimensionSpec;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -87,4 +88,16 @@ public class ColumnComparisonDimFilterTest
columnComparisonDimFilter3.hashCode() columnComparisonDimFilter3.hashCode()
); );
} }
@Test
public void testGetRequiredColumns()
{
ColumnComparisonDimFilter columnComparisonDimFilter = new ColumnComparisonDimFilter(
ImmutableList.of(
DefaultDimensionSpec.of("abc"),
DefaultDimensionSpec.of("d")
)
);
Assert.assertEquals(columnComparisonDimFilter.getRequiredColumns(), Sets.newHashSet("abc", "d"));
}
} }

View File

@ -20,6 +20,7 @@
package io.druid.query.filter; package io.druid.query.filter;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Sets;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.google.inject.Key; import com.google.inject.Key;
import io.druid.guice.GuiceInjectors; import io.druid.guice.GuiceInjectors;
@ -230,4 +231,18 @@ public class IntervalDimFilterTest
); );
Assert.assertNotEquals(intervalFilter1, intervalFilter5); Assert.assertNotEquals(intervalFilter1, intervalFilter5);
} }
@Test
public void testGetRequiredColumns()
{
DimFilter intervalFilter = new IntervalDimFilter(
Column.TIME_COLUMN_NAME,
Arrays.asList(
Intervals.of("1970-01-01T00:00:00.001Z/1970-01-01T00:00:00.004Z"),
Intervals.of("1975-01-01T00:00:00.001Z/1980-01-01T00:00:00.004Z")
),
null
);
Assert.assertEquals(intervalFilter.getRequiredColumns(), Sets.newHashSet(Column.TIME_COLUMN_NAME));
}
} }

View File

@ -19,6 +19,7 @@
package io.druid.query.filter; package io.druid.query.filter;
import com.google.common.collect.Sets;
import io.druid.js.JavaScriptConfig; import io.druid.js.JavaScriptConfig;
import io.druid.query.extraction.RegexDimExtractionFn; import io.druid.query.extraction.RegexDimExtractionFn;
import io.druid.segment.filter.JavaScriptFilter; import io.druid.segment.filter.JavaScriptFilter;
@ -105,4 +106,11 @@ public class JavaScriptDimFilterTest
javaScriptDimFilter.toFilter(); javaScriptDimFilter.toFilter();
Assert.assertTrue(false); Assert.assertTrue(false);
} }
@Test
public void testGetRequiredColumns()
{
JavaScriptDimFilter javaScriptDimFilter = new JavaScriptDimFilter("dim", FN1, null, new JavaScriptConfig(false));
Assert.assertEquals(javaScriptDimFilter.getRequiredColumns(), Sets.newHashSet("dim"));
}
} }

View File

@ -20,6 +20,7 @@
package io.druid.query.filter; package io.druid.query.filter;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Sets;
import io.druid.jackson.DefaultObjectMapper; import io.druid.jackson.DefaultObjectMapper;
import io.druid.query.extraction.SubstringDimExtractionFn; import io.druid.query.extraction.SubstringDimExtractionFn;
import org.junit.Assert; import org.junit.Assert;
@ -60,4 +61,11 @@ public class LikeDimFilterTest
Assert.assertEquals(filter.hashCode(), filter2.hashCode()); Assert.assertEquals(filter.hashCode(), filter2.hashCode());
Assert.assertNotEquals(filter.hashCode(), filter3.hashCode()); Assert.assertNotEquals(filter.hashCode(), filter3.hashCode());
} }
@Test
public void testGetRequiredColumns()
{
final DimFilter filter = new LikeDimFilter("foo", "bar%", "@", new SubstringDimExtractionFn(1, 2));
Assert.assertEquals(filter.getRequiredColumns(), Sets.newHashSet("foo"));
}
} }

View File

@ -19,6 +19,7 @@
package io.druid.query.filter; package io.druid.query.filter;
import com.google.common.collect.Sets;
import io.druid.query.extraction.RegexDimExtractionFn; import io.druid.query.extraction.RegexDimExtractionFn;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -75,4 +76,11 @@ public class SelectorDimFilterTest
); );
Assert.assertEquals(selectorDimFilter, filter.optimize()); Assert.assertEquals(selectorDimFilter, filter.optimize());
} }
@Test
public void testGetRequiredColumns()
{
SelectorDimFilter selectorDimFilter = new SelectorDimFilter("abc", "d", null);
Assert.assertEquals(selectorDimFilter.getRequiredColumns(), Sets.newHashSet("abc"));
}
} }

View File

@ -22,6 +22,7 @@ package io.druid.segment.filter;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import io.druid.data.input.InputRow; import io.druid.data.input.InputRow;
import io.druid.data.input.impl.DimensionsSpec; import io.druid.data.input.impl.DimensionsSpec;
import io.druid.data.input.impl.FloatDimensionSchema; import io.druid.data.input.impl.FloatDimensionSchema;
@ -39,6 +40,7 @@ import io.druid.segment.IndexBuilder;
import io.druid.segment.StorageAdapter; import io.druid.segment.StorageAdapter;
import io.druid.segment.incremental.IncrementalIndexSchema; import io.druid.segment.incremental.IncrementalIndexSchema;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
@ -200,6 +202,18 @@ public class ExpressionFilterTest extends BaseFilterTest
assertFilterMatches(EDF("like(missing, '1%')"), ImmutableList.of()); assertFilterMatches(EDF("like(missing, '1%')"), ImmutableList.of());
} }
@Test
public void testGetRequiredColumn()
{
Assert.assertEquals(EDF("like(dim1, '1%')").getRequiredColumns(), Sets.newHashSet("dim1"));
Assert.assertEquals(EDF("dim2 == '1'").getRequiredColumns(), Sets.newHashSet("dim2"));
Assert.assertEquals(EDF("dim3 < '2'").getRequiredColumns(), Sets.newHashSet("dim3"));
Assert.assertEquals(EDF("dim4 == ''").getRequiredColumns(), Sets.newHashSet("dim4"));
Assert.assertEquals(EDF("1 + 1").getRequiredColumns(), Sets.newHashSet());
Assert.assertEquals(EDF("dim0 == dim3").getRequiredColumns(), Sets.newHashSet("dim0", "dim3"));
Assert.assertEquals(EDF("missing == ''").getRequiredColumns(), Sets.newHashSet("missing"));
}
private static ExpressionDimFilter EDF(final String expression) private static ExpressionDimFilter EDF(final String expression)
{ {
return new ExpressionDimFilter(expression, TestExprMacroTable.INSTANCE); return new ExpressionDimFilter(expression, TestExprMacroTable.INSTANCE);