use of io.druid.query.filter.ExtractionDimFilter in project druid by druid-io.
the class SelectorFilterTest method testSelectorWithLookupExtractionFn.
@Test
public void testSelectorWithLookupExtractionFn() {
final Map<String, String> stringMap = ImmutableMap.of("1", "HELLO", "a", "HELLO", "def", "HELLO", "abc", "UNKNOWN");
LookupExtractor mapExtractor = new MapLookupExtractor(stringMap, false);
LookupExtractionFn lookupFn = new LookupExtractionFn(mapExtractor, false, "UNKNOWN", false, true);
assertFilterMatches(new SelectorDimFilter("dim0", "HELLO", lookupFn), ImmutableList.of("1"));
assertFilterMatches(new SelectorDimFilter("dim0", "UNKNOWN", lookupFn), ImmutableList.of("0", "2", "3", "4", "5"));
assertFilterMatches(new SelectorDimFilter("dim1", "HELLO", lookupFn), ImmutableList.of("3", "4"));
assertFilterMatches(new SelectorDimFilter("dim1", "UNKNOWN", lookupFn), ImmutableList.of("0", "1", "2", "5"));
assertFilterMatches(new SelectorDimFilter("dim2", "HELLO", lookupFn), ImmutableList.of("0", "3"));
assertFilterMatches(new SelectorDimFilter("dim2", "UNKNOWN", lookupFn), ImmutableList.of("0", "1", "2", "4", "5"));
assertFilterMatches(new SelectorDimFilter("dim3", "HELLO", lookupFn), ImmutableList.<String>of());
assertFilterMatches(new SelectorDimFilter("dim3", "UNKNOWN", lookupFn), ImmutableList.of("0", "1", "2", "3", "4", "5"));
assertFilterMatches(new SelectorDimFilter("dim4", "HELLO", lookupFn), ImmutableList.<String>of());
assertFilterMatches(new SelectorDimFilter("dim4", "UNKNOWN", lookupFn), ImmutableList.of("0", "1", "2", "3", "4", "5"));
final Map<String, String> stringMap2 = ImmutableMap.of("2", "5");
LookupExtractor mapExtractor2 = new MapLookupExtractor(stringMap2, false);
LookupExtractionFn lookupFn2 = new LookupExtractionFn(mapExtractor2, true, null, false, true);
assertFilterMatches(new SelectorDimFilter("dim0", "5", lookupFn2), ImmutableList.of("2", "5"));
final Map<String, String> stringMap3 = ImmutableMap.of("1", "");
LookupExtractor mapExtractor3 = new MapLookupExtractor(stringMap3, false);
LookupExtractionFn lookupFn3 = new LookupExtractionFn(mapExtractor3, false, null, false, true);
assertFilterMatches(new SelectorDimFilter("dim0", null, lookupFn3), ImmutableList.of("0", "1", "2", "3", "4", "5"));
final Map<String, String> stringMap4 = ImmutableMap.of("9", "4");
LookupExtractor mapExtractor4 = new MapLookupExtractor(stringMap4, false);
LookupExtractionFn lookupFn4 = new LookupExtractionFn(mapExtractor4, true, null, false, true);
final Map<String, String> stringMap5 = ImmutableMap.of("5", "44");
LookupExtractor mapExtractor5 = new MapLookupExtractor(stringMap5, false);
LookupExtractionFn lookupFn5 = new LookupExtractionFn(mapExtractor5, true, null, false, true);
final Map<String, String> stringMap6 = ImmutableMap.of("5", "5");
LookupExtractor mapExtractor6 = new MapLookupExtractor(stringMap6, false);
LookupExtractionFn lookupFn6 = new LookupExtractionFn(mapExtractor6, true, null, false, true);
// optimize() tests, check that filter was converted to the proper form
SelectorDimFilter optFilter1 = new SelectorDimFilter("dim1", "UNKNOWN", lookupFn);
SelectorDimFilter optFilter2 = new SelectorDimFilter("dim0", "5", lookupFn2);
SelectorDimFilter optFilter3 = new SelectorDimFilter("dim0", null, lookupFn3);
SelectorDimFilter optFilter4 = new SelectorDimFilter("dim0", "5", lookupFn4);
SelectorDimFilter optFilter5 = new SelectorDimFilter("dim0", "5", lookupFn5);
SelectorDimFilter optFilter6 = new SelectorDimFilter("dim0", "5", lookupFn6);
InDimFilter optFilter2Optimized = new InDimFilter("dim0", Arrays.asList("2", "5"), null);
SelectorDimFilter optFilter4Optimized = new SelectorDimFilter("dim0", "5", null);
SelectorDimFilter optFilter6Optimized = new SelectorDimFilter("dim0", "5", null);
Assert.assertTrue(optFilter1.equals(optFilter1.optimize()));
Assert.assertTrue(optFilter2Optimized.equals(optFilter2.optimize()));
Assert.assertTrue(optFilter3.equals(optFilter3.optimize()));
Assert.assertTrue(optFilter4Optimized.equals(optFilter4.optimize()));
Assert.assertTrue(optFilter5.equals(optFilter5.optimize()));
Assert.assertTrue(optFilter6Optimized.equals(optFilter6.optimize()));
assertFilterMatches(optFilter1, ImmutableList.of("0", "1", "2", "5"));
assertFilterMatches(optFilter2, ImmutableList.of("2", "5"));
assertFilterMatches(optFilter3, ImmutableList.of("0", "1", "2", "3", "4", "5"));
assertFilterMatches(optFilter4, ImmutableList.of("5"));
assertFilterMatches(optFilter5, ImmutableList.<String>of());
assertFilterMatches(optFilter6, ImmutableList.of("5"));
// tests that ExtractionDimFilter (identical to SelectorDimFilter now) optimize() with lookup works
// remove these when ExtractionDimFilter is removed.
assertFilterMatches(new ExtractionDimFilter("dim1", "UNKNOWN", lookupFn, null), ImmutableList.of("0", "1", "2", "5"));
assertFilterMatches(new ExtractionDimFilter("dim0", "5", lookupFn2, null), ImmutableList.of("2", "5"));
assertFilterMatches(new ExtractionDimFilter("dim0", null, lookupFn3, null), ImmutableList.of("0", "1", "2", "3", "4", "5"));
}
use of io.druid.query.filter.ExtractionDimFilter in project druid by druid-io.
the class ExtractionDimFilterTest method testAnd.
@Test
public void testAnd() {
Assert.assertEquals(1, Filters.toFilter(DimFilters.or(new ExtractionDimFilter("foo", "extractDimVal", DIM_EXTRACTION_FN, null))).getBitmapIndex(BITMAP_INDEX_SELECTOR).size());
Assert.assertEquals(1, Filters.toFilter(DimFilters.and(new ExtractionDimFilter("foo", "extractDimVal", DIM_EXTRACTION_FN, null), new ExtractionDimFilter("foo", "extractDimVal", DIM_EXTRACTION_FN, null))).getBitmapIndex(BITMAP_INDEX_SELECTOR).size());
}
use of io.druid.query.filter.ExtractionDimFilter in project druid by druid-io.
the class ExtractionDimFilterTest method testOr.
@Test
public void testOr() {
Assert.assertEquals(1, Filters.toFilter(DimFilters.or(new ExtractionDimFilter("foo", "extractDimVal", DIM_EXTRACTION_FN, null))).getBitmapIndex(BITMAP_INDEX_SELECTOR).size());
Assert.assertEquals(1, Filters.toFilter(DimFilters.or(new ExtractionDimFilter("foo", "extractDimVal", DIM_EXTRACTION_FN, null), new ExtractionDimFilter("foo", "DOES NOT EXIST", DIM_EXTRACTION_FN, null))).getBitmapIndex(BITMAP_INDEX_SELECTOR).size());
}
use of io.druid.query.filter.ExtractionDimFilter in project druid by druid-io.
the class GroupByQueryRunnerTest method testGroupByWithExtractionDimFilterWhenSearchValueNotInTheMap.
@Test
public void testGroupByWithExtractionDimFilterWhenSearchValueNotInTheMap() {
Map<String, String> extractionMap = new HashMap<>();
MapLookupExtractor mapLookupExtractor = new MapLookupExtractor(extractionMap, false);
LookupExtractionFn lookupExtractionFn = new LookupExtractionFn(mapLookupExtractor, false, null, true, false);
GroupByQuery query = GroupByQuery.builder().setDataSource(QueryRunnerTestHelper.dataSource).setQuerySegmentSpec(QueryRunnerTestHelper.firstToThird).setDimensions(Lists.<DimensionSpec>newArrayList(new DefaultDimensionSpec("quality", "alias"))).setAggregatorSpecs(Arrays.asList(QueryRunnerTestHelper.rowsCount, new LongSumAggregatorFactory("idx", "index"))).setGranularity(QueryRunnerTestHelper.dayGran).setDimFilter(new ExtractionDimFilter("quality", "NOT_THERE", lookupExtractionFn, null)).build();
List<Row> expectedResults = Arrays.asList();
Iterable<Row> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
TestHelper.assertExpectedObjects(expectedResults, results, "");
}
use of io.druid.query.filter.ExtractionDimFilter in project druid by druid-io.
the class GroupByQueryRunnerTest method testGroupByWithExtractionDimFilter.
// Extraction Filters testing
@Test
public void testGroupByWithExtractionDimFilter() {
Map<String, String> extractionMap = new HashMap<>();
extractionMap.put("automotive", "automotiveAndBusinessAndNewsAndMezzanine");
extractionMap.put("business", "automotiveAndBusinessAndNewsAndMezzanine");
extractionMap.put("mezzanine", "automotiveAndBusinessAndNewsAndMezzanine");
extractionMap.put("news", "automotiveAndBusinessAndNewsAndMezzanine");
MapLookupExtractor mapLookupExtractor = new MapLookupExtractor(extractionMap, false);
LookupExtractionFn lookupExtractionFn = new LookupExtractionFn(mapLookupExtractor, false, null, true, false);
List<DimFilter> dimFilters = Lists.<DimFilter>newArrayList(new ExtractionDimFilter("quality", "automotiveAndBusinessAndNewsAndMezzanine", lookupExtractionFn, null), new SelectorDimFilter("quality", "entertainment", null), new SelectorDimFilter("quality", "health", null), new SelectorDimFilter("quality", "premium", null), new SelectorDimFilter("quality", "technology", null), new SelectorDimFilter("quality", "travel", null));
GroupByQuery query = GroupByQuery.builder().setDataSource(QueryRunnerTestHelper.dataSource).setQuerySegmentSpec(QueryRunnerTestHelper.firstToThird).setDimensions(Lists.<DimensionSpec>newArrayList(new DefaultDimensionSpec("quality", "alias"))).setAggregatorSpecs(Arrays.asList(QueryRunnerTestHelper.rowsCount, new LongSumAggregatorFactory("idx", "index"))).setGranularity(QueryRunnerTestHelper.dayGran).setDimFilter(Druids.newOrDimFilterBuilder().fields(dimFilters).build()).build();
List<Row> expectedResults = Arrays.asList(GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "automotive", "rows", 1L, "idx", 135L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "business", "rows", 1L, "idx", 118L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "entertainment", "rows", 1L, "idx", 158L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "health", "rows", 1L, "idx", 120L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "mezzanine", "rows", 3L, "idx", 2870L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "news", "rows", 1L, "idx", 121L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "premium", "rows", 3L, "idx", 2900L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "technology", "rows", 1L, "idx", 78L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "travel", "rows", 1L, "idx", 119L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "automotive", "rows", 1L, "idx", 147L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "business", "rows", 1L, "idx", 112L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "entertainment", "rows", 1L, "idx", 166L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "health", "rows", 1L, "idx", 113L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "mezzanine", "rows", 3L, "idx", 2447L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "news", "rows", 1L, "idx", 114L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "premium", "rows", 3L, "idx", 2505L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "technology", "rows", 1L, "idx", 97L), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "travel", "rows", 1L, "idx", 126L));
Iterable<Row> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
TestHelper.assertExpectedObjects(expectedResults, results, "");
}
Aggregations