use of com.google.common.collect.RangeSet in project druid by druid-io.
the class GetDimensionRangeSetTest method testNotFilter.
@Test
public void testNotFilter() {
DimFilter not1 = new NotDimFilter(selector1);
RangeSet expected1 = rangeSet(ImmutableList.of(Range.lessThan("a"), Range.greaterThan("a")));
Assert.assertEquals(expected1, not1.getDimensionRangeSet("dim1"));
Assert.assertNull(not1.getDimensionRangeSet("dim2"));
DimFilter not2 = new NotDimFilter(in3);
RangeSet expected2 = rangeSet(ImmutableList.of(Range.lessThan(""), Range.open("", "null"), Range.greaterThan("null")));
Assert.assertEquals(expected2, not2.getDimensionRangeSet("dim1"));
DimFilter not3 = new NotDimFilter(bound1);
RangeSet expected3 = rangeSet(ImmutableList.of(Range.lessThan("from"), Range.greaterThan("to")));
Assert.assertEquals(expected3, not3.getDimensionRangeSet("dim1"));
DimFilter not4 = new NotDimFilter(not2);
RangeSet expected4 = rangeSet(ImmutableList.of(point(""), point("null")));
Assert.assertEquals(expected4, not4.getDimensionRangeSet("dim1"));
DimFilter or1 = new OrDimFilter(ImmutableList.of(selector1, selector2, bound1, bound3));
DimFilter not5 = new NotDimFilter(or1);
RangeSet expected5 = rangeSet(ImmutableList.of(Range.lessThan("a"), Range.open("a", "from")));
Assert.assertEquals(expected5, not5.getDimensionRangeSet("dim1"));
Assert.assertNull(not5.getDimensionRangeSet("dim2"));
DimFilter or2 = new OrDimFilter(ImmutableList.of(selector3, in3, bound2, bound4, other3));
DimFilter not6 = new NotDimFilter(or2);
RangeSet expected6a = rangeSet(ImmutableList.of(Range.greaterThan("tillend")));
RangeSet expected6b = rangeSet(ImmutableList.of(Range.atMost("again"), Range.atLeast("exclusive")));
Assert.assertEquals(expected6a, not6.getDimensionRangeSet("dim1"));
Assert.assertEquals(expected6b, not6.getDimensionRangeSet("dim2"));
DimFilter and1 = new AndDimFilter(ImmutableList.of(in1, bound1, bound2));
DimFilter not7 = new NotDimFilter(and1);
RangeSet expected7 = rangeSet(ImmutableList.of(Range.lessThan("testing"), Range.open("testing", "this"), Range.open("this", "tillend"), Range.greaterThan("tillend")));
Assert.assertEquals(expected7, not7.getDimensionRangeSet("dim1"));
Assert.assertNull(not7.getDimensionRangeSet("dim2"));
DimFilter and2 = new AndDimFilter(ImmutableList.of(bound1, bound2, bound3, bound4));
DimFilter not8 = new NotDimFilter(and2);
Assert.assertNull(not8.getDimensionRangeSet("dim1"));
Assert.assertNull(not8.getDimensionRangeSet("dim2"));
}
use of com.google.common.collect.RangeSet in project druid by druid-io.
the class GetDimensionRangeSetTest method testSimpleFilter.
@Test
public void testSimpleFilter() {
RangeSet expected1 = rangeSet(point("a"));
Assert.assertEquals(expected1, selector1.getDimensionRangeSet("dim1"));
Assert.assertNull(selector1.getDimensionRangeSet("dim2"));
RangeSet expected2 = rangeSet(point(""));
Assert.assertEquals(expected2, selector5.getDimensionRangeSet("dim1"));
RangeSet expected3 = rangeSet(ImmutableList.of(point("testing"), point("this"), point("filter"), point("tillend")));
Assert.assertEquals(expected3, in1.getDimensionRangeSet("dim1"));
RangeSet expected4 = rangeSet(ImmutableList.of(point("null"), point("")));
Assert.assertEquals(expected4, in3.getDimensionRangeSet("dim1"));
RangeSet expected5 = ImmutableRangeSet.of(Range.closed("from", "to"));
Assert.assertEquals(expected5, bound1.getDimensionRangeSet("dim1"));
RangeSet expected6 = ImmutableRangeSet.of(Range.atMost("tillend"));
Assert.assertEquals(expected6, bound2.getDimensionRangeSet("dim1"));
RangeSet expected7 = ImmutableRangeSet.of(Range.greaterThan("notincluded"));
Assert.assertEquals(expected7, bound3.getDimensionRangeSet("dim1"));
Assert.assertNull(other1.getDimensionRangeSet("someDim"));
Assert.assertNull(other2.getDimensionRangeSet("someOtherDim"));
Assert.assertNull(other3.getDimensionRangeSet("dim"));
Assert.assertNull(interval1.getDimensionRangeSet(Column.TIME_COLUMN_NAME));
Assert.assertNull(interval2.getDimensionRangeSet("dim1"));
}
use of com.google.common.collect.RangeSet in project druid by druid-io.
the class MoveTimeFiltersToIntervals method extractConvertibleTimeBounds.
/**
* Extract bound filters on __time that can be converted to query-level "intervals".
*
* @return pair of new dimFilter + RangeSet of __time that should be ANDed together. Either can be null but not both.
*/
private static Pair<DimFilter, RangeSet<Long>> extractConvertibleTimeBounds(final DimFilter filter) {
if (filter instanceof AndDimFilter) {
final List<DimFilter> children = ((AndDimFilter) filter).getFields();
final List<DimFilter> newChildren = Lists.newArrayList();
final List<RangeSet<Long>> rangeSets = Lists.newArrayList();
for (DimFilter child : children) {
final Pair<DimFilter, RangeSet<Long>> pair = extractConvertibleTimeBounds(child);
if (pair.lhs != null) {
newChildren.add(pair.lhs);
}
if (pair.rhs != null) {
rangeSets.add(pair.rhs);
}
}
final DimFilter newFilter;
if (newChildren.size() == 0) {
newFilter = null;
} else if (newChildren.size() == 1) {
newFilter = newChildren.get(0);
} else {
newFilter = new AndDimFilter(newChildren);
}
return Pair.of(newFilter, rangeSets.isEmpty() ? null : RangeSets.intersectRangeSets(rangeSets));
} else if (filter instanceof OrDimFilter) {
final List<DimFilter> children = ((OrDimFilter) filter).getFields();
final List<RangeSet<Long>> rangeSets = Lists.newArrayList();
boolean allCompletelyConverted = true;
boolean allHadIntervals = true;
for (DimFilter child : children) {
final Pair<DimFilter, RangeSet<Long>> pair = extractConvertibleTimeBounds(child);
if (pair.lhs != null) {
allCompletelyConverted = false;
}
if (pair.rhs != null) {
rangeSets.add(pair.rhs);
} else {
allHadIntervals = false;
}
}
if (allCompletelyConverted) {
return Pair.of(null, RangeSets.unionRangeSets(rangeSets));
} else {
return Pair.of(filter, allHadIntervals ? RangeSets.unionRangeSets(rangeSets) : null);
}
} else if (filter instanceof NotDimFilter) {
final DimFilter child = ((NotDimFilter) filter).getField();
final Pair<DimFilter, RangeSet<Long>> pair = extractConvertibleTimeBounds(child);
if (pair.rhs != null && pair.lhs == null) {
return Pair.of(null, pair.rhs.complement());
} else {
return Pair.of(filter, null);
}
} else if (filter instanceof BoundDimFilter) {
final BoundDimFilter bound = (BoundDimFilter) filter;
if (BoundRefKey.from(bound).equals(TIME_BOUND_REF_KEY)) {
return Pair.of(null, RangeSets.of(toLongRange(Bounds.toRange(bound))));
} else {
return Pair.of(filter, null);
}
} else {
return Pair.of(filter, null);
}
}
use of com.google.common.collect.RangeSet in project druid by druid-io.
the class DimFilterUtils method filterShards.
/**
* Filter the given iterable of objects by removing any object whose ShardSpec, obtained from the converter function,
* does not fit in the RangeSet of the dimFilter {@link DimFilter#getDimensionRangeSet(String)}. The returned set
* contains the filtered objects in the same order as they appear in input.
*
* DimensionRangedCache stores the RangeSets of different dimensions for the dimFilter. It should be re-used
* between calls with the same dimFilter to save redundant calls of {@link DimFilter#getDimensionRangeSet(String)}
* on same dimensions.
*
* @param dimFilter The filter to use
* @param input The iterable of objects to be filtered
* @param converter The function to convert T to ShardSpec that can be filtered by
* @param dimensionRangeCache The cache of RangeSets of different dimensions for the dimFilter
* @param <T> This can be any type, as long as transform function is provided to convert this to ShardSpec
* @return The set of filtered object, in the same order as input
*/
public static <T> Set<T> filterShards(DimFilter dimFilter, Iterable<T> input, Function<T, ShardSpec> converter, Map<String, Optional<RangeSet<String>>> dimensionRangeCache) {
Set<T> retSet = Sets.newLinkedHashSet();
for (T obj : input) {
ShardSpec shard = converter.apply(obj);
boolean include = true;
if (dimFilter != null && shard != null) {
Map<String, Range<String>> domain = shard.getDomain();
for (Map.Entry<String, Range<String>> entry : domain.entrySet()) {
Optional<RangeSet<String>> optFilterRangeSet = dimensionRangeCache.get(entry.getKey());
if (optFilterRangeSet == null) {
RangeSet<String> filterRangeSet = dimFilter.getDimensionRangeSet(entry.getKey());
optFilterRangeSet = Optional.fromNullable(filterRangeSet);
dimensionRangeCache.put(entry.getKey(), optFilterRangeSet);
}
if (optFilterRangeSet.isPresent() && optFilterRangeSet.get().subRangeSet(entry.getValue()).isEmpty()) {
include = false;
}
}
}
if (include) {
retSet.add(obj);
}
}
return retSet;
}
use of com.google.common.collect.RangeSet in project druid by druid-io.
the class GetDimensionRangeSetTest method testAndFilter.
@Test
public void testAndFilter() {
DimFilter and1 = new AndDimFilter(ImmutableList.of(selector1, selector2, in1));
Assert.assertEquals(empty, and1.getDimensionRangeSet("dim1"));
Assert.assertNull(and1.getDimensionRangeSet("dim2"));
DimFilter and2 = new AndDimFilter(ImmutableList.of(selector3, bound1, other1));
RangeSet expected2 = rangeSet(ImmutableList.of(Range.closed("from", "to")));
Assert.assertEquals(expected2, and2.getDimensionRangeSet("dim1"));
DimFilter and3 = new AndDimFilter(ImmutableList.of(in2, bound1, bound2, bound3, bound4));
RangeSet expected3 = rangeSet(Range.openClosed("notincluded", "tillend"));
Assert.assertEquals(expected3, and3.getDimensionRangeSet("dim1"));
Assert.assertEquals(empty, and3.getDimensionRangeSet("dim2"));
DimFilter and4 = new AndDimFilter(ImmutableList.of(in3, bound3));
RangeSet expected4 = rangeSet(point("null"));
Assert.assertEquals(expected4, and4.getDimensionRangeSet("dim1"));
DimFilter and5 = new AndDimFilter(ImmutableList.of(and3, in1));
RangeSet expected5 = rangeSet(ImmutableList.of(point("testing"), point("this"), point("tillend")));
Assert.assertEquals(expected5, and5.getDimensionRangeSet("dim1"));
}
Aggregations