use of io.druid.query.filter.BoundDimFilter 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 io.druid.query.filter.BoundDimFilter in project druid by druid-io.
the class TimeFilteringTest method testTimeFilterWithExtractionFn.
@Test
public void testTimeFilterWithExtractionFn() {
final Map<String, String> stringMap = new HashMap<>();
stringMap.put("0", "Monday");
stringMap.put("1", "Tuesday");
stringMap.put("2", "Wednesday");
stringMap.put("3", "Thursday");
stringMap.put("4", "Friday");
stringMap.put("5", "Saturday");
LookupExtractor mapExtractor = new MapLookupExtractor(stringMap, false);
LookupExtractionFn exfn = new LookupExtractionFn(mapExtractor, false, "UNKNOWN", false, true);
assertFilterMatches(new SelectorDimFilter(Column.TIME_COLUMN_NAME, "Monday", exfn), ImmutableList.<String>of("0"));
assertFilterMatches(new SelectorDimFilter(Column.TIME_COLUMN_NAME, "Notaday", exfn), ImmutableList.<String>of());
assertFilterMatches(new BoundDimFilter(Column.TIME_COLUMN_NAME, "Fridax", "Fridaz", false, false, null, exfn, StringComparators.ALPHANUMERIC), ImmutableList.<String>of("4"));
assertFilterMatches(new BoundDimFilter(Column.TIME_COLUMN_NAME, "Friday", "Friday", true, true, null, exfn, StringComparators.ALPHANUMERIC), ImmutableList.<String>of());
assertFilterMatches(new InDimFilter(Column.TIME_COLUMN_NAME, Arrays.asList("Caturday", "Saturday", "Tuesday"), exfn), ImmutableList.<String>of("1", "5"));
// test InFilter HashSet implementation
List<String> bigList = Arrays.asList("Saturday", "Tuesday", "Caturday", "Xanaday", "Vojuday", "Gribaday", "Kipoday", "Dheferday", "Fakeday", "Qeearaday", "Hello", "World", "1", "2", "3", "4", "5", "6", "7");
assertFilterMatches(new InDimFilter(Column.TIME_COLUMN_NAME, bigList, exfn), ImmutableList.<String>of("1", "5"));
String jsFn = "function(x) { return(x === 'Wednesday' || x === 'Thursday') }";
assertFilterMatches(new JavaScriptDimFilter(Column.TIME_COLUMN_NAME, jsFn, exfn, JavaScriptConfig.getEnabledInstance()), ImmutableList.<String>of("2", "3"));
assertFilterMatches(new RegexDimFilter(Column.TIME_COLUMN_NAME, ".*day", exfn), ImmutableList.<String>of("0", "1", "2", "3", "4", "5"));
assertFilterMatches(new SearchQueryDimFilter(Column.TIME_COLUMN_NAME, new ContainsSearchQuerySpec("s", true), exfn), ImmutableList.<String>of("1", "2", "3"));
}
use of io.druid.query.filter.BoundDimFilter in project druid by druid-io.
the class BoundFilterTest method testMatchWithExtractionFn.
@Test
public void testMatchWithExtractionFn() {
String extractionJsFn = "function(str) { return 'super-' + str; }";
ExtractionFn superFn = new JavaScriptExtractionFn(extractionJsFn, false, JavaScriptConfig.getEnabledInstance());
String nullJsFn = "function(str) { return null; }";
ExtractionFn makeNullFn = new JavaScriptExtractionFn(nullJsFn, false, JavaScriptConfig.getEnabledInstance());
assertFilterMatches(new BoundDimFilter("dim0", "", "", false, false, false, makeNullFn, StringComparators.LEXICOGRAPHIC), ImmutableList.of("0", "1", "2", "3", "4", "5", "6", "7"));
assertFilterMatches(new BoundDimFilter("dim1", "super-ab", "super-abd", true, true, false, superFn, StringComparators.LEXICOGRAPHIC), ImmutableList.of("5"));
assertFilterMatches(new BoundDimFilter("dim1", "super-0", "super-10", false, false, true, superFn, StringComparators.ALPHANUMERIC), ImmutableList.of("1", "2", "3"));
assertFilterMatches(new BoundDimFilter("dim2", "super-", "super-zzzzzz", false, false, false, superFn, StringComparators.LEXICOGRAPHIC), ImmutableList.of("0", "1", "2", "3", "4", "5", "6", "7"));
assertFilterMatches(new BoundDimFilter("dim2", "super-null", "super-null", false, false, false, superFn, StringComparators.LEXICOGRAPHIC), ImmutableList.of("1", "2", "5"));
assertFilterMatches(new BoundDimFilter("dim3", "super-null", "super-null", false, false, false, superFn, StringComparators.LEXICOGRAPHIC), ImmutableList.of("0", "1", "2", "3", "4", "5", "6", "7"));
assertFilterMatches(new BoundDimFilter("dim4", "super-null", "super-null", false, false, false, superFn, StringComparators.LEXICOGRAPHIC), ImmutableList.of("0", "1", "2", "3", "4", "5", "6", "7"));
assertFilterMatches(new BoundDimFilter("dim2", "super-null", "super-null", false, false, false, superFn, StringComparators.NUMERIC), ImmutableList.of("1", "2", "5"));
assertFilterMatches(new BoundDimFilter("dim4", "super-null", "super-null", false, false, false, superFn, StringComparators.NUMERIC), ImmutableList.of("0", "1", "2", "3", "4", "5", "6", "7"));
}
use of io.druid.query.filter.BoundDimFilter in project druid by druid-io.
the class BoundFilterTest method testLexicographicMatchTooStrict.
@Test
public void testLexicographicMatchTooStrict() {
assertFilterMatches(new BoundDimFilter("dim1", "abc", "abc", true, false, false, null, StringComparators.LEXICOGRAPHIC), ImmutableList.<String>of());
assertFilterMatches(new BoundDimFilter("dim1", "abc", "abc", true, true, false, null, StringComparators.LEXICOGRAPHIC), ImmutableList.<String>of());
assertFilterMatches(new BoundDimFilter("dim1", "abc", "abc", false, true, false, null, StringComparators.LEXICOGRAPHIC), ImmutableList.<String>of());
}
use of io.druid.query.filter.BoundDimFilter in project druid by druid-io.
the class TimeFilteringTest method testTimeFilterAsLong.
@Test
public void testTimeFilterAsLong() {
assertFilterMatches(new SelectorDimFilter(Column.TIME_COLUMN_NAME, "0", null), ImmutableList.<String>of("0"));
assertFilterMatches(new SelectorDimFilter(Column.TIME_COLUMN_NAME, "9000", null), ImmutableList.<String>of());
assertFilterMatches(new BoundDimFilter(Column.TIME_COLUMN_NAME, "0", "4", false, false, null, null, StringComparators.NUMERIC), ImmutableList.<String>of("0", "1", "2", "3", "4"));
assertFilterMatches(new BoundDimFilter(Column.TIME_COLUMN_NAME, "0", "4", true, true, null, null, StringComparators.NUMERIC), ImmutableList.<String>of("1", "2", "3"));
assertFilterMatches(new InDimFilter(Column.TIME_COLUMN_NAME, Arrays.asList("2", "4", "8"), null), ImmutableList.<String>of("2", "4"));
// cross the hashing threshold to test hashset implementation, filter on even values
List<String> infilterValues = new ArrayList<>(InDimFilter.NUMERIC_HASHING_THRESHOLD * 2);
for (int i = 0; i < InDimFilter.NUMERIC_HASHING_THRESHOLD * 2; i++) {
infilterValues.add(String.valueOf(i * 2));
}
assertFilterMatches(new InDimFilter(Column.TIME_COLUMN_NAME, infilterValues, null), ImmutableList.<String>of("0", "2", "4"));
String jsFn = "function(x) { return(x === 3 || x === 5) }";
assertFilterMatches(new JavaScriptDimFilter(Column.TIME_COLUMN_NAME, jsFn, null, JavaScriptConfig.getEnabledInstance()), ImmutableList.<String>of("3", "5"));
assertFilterMatches(new RegexDimFilter(Column.TIME_COLUMN_NAME, "4", null), ImmutableList.<String>of("4"));
assertFilterMatches(new SearchQueryDimFilter(Column.TIME_COLUMN_NAME, new ContainsSearchQuerySpec("2", true), null), ImmutableList.<String>of("2"));
}
Aggregations