Search in sources :

Example 16 with MapLookupExtractor

use of org.apache.druid.query.extraction.MapLookupExtractor in project druid by druid-io.

the class LookupExtractionFnExpectationsTest method testNullValue.

@Test
public void testNullValue() {
    final LookupExtractionFn lookupExtractionFn = new LookupExtractionFn(new MapLookupExtractor(ImmutableMap.of("foo", ""), false), false, "REPLACE", false, false);
    Assert.assertEquals("REPLACE", lookupExtractionFn.apply(null));
}
Also used : MapLookupExtractor(org.apache.druid.query.extraction.MapLookupExtractor) Test(org.junit.Test)

Example 17 with MapLookupExtractor

use of org.apache.druid.query.extraction.MapLookupExtractor in project druid by druid-io.

the class ScanQueryRunnerTest method testSelectWithFilterLookupExtractionFn.

@Test
public void testSelectWithFilterLookupExtractionFn() {
    Map<String, String> extractionMap = new HashMap<>();
    extractionMap.put("total_market", "replaced");
    MapLookupExtractor mapLookupExtractor = new MapLookupExtractor(extractionMap, false);
    LookupExtractionFn lookupExtractionFn = new LookupExtractionFn(mapLookupExtractor, false, null, true, true);
    ScanQuery query = newTestQuery().intervals(I_0112_0114).filters(new SelectorDimFilter(QueryRunnerTestHelper.MARKET_DIMENSION, "replaced", lookupExtractionFn)).columns(QueryRunnerTestHelper.QUALITY_DIMENSION, QueryRunnerTestHelper.INDEX_METRIC).build();
    Iterable<ScanResultValue> results = runner.run(QueryPlus.wrap(query)).toList();
    Iterable<ScanResultValue> resultsOptimize = TOOL_CHEST.postMergeQueryDecoration(TOOL_CHEST.mergeResults(TOOL_CHEST.preMergeQueryDecoration(runner))).run(QueryPlus.wrap(query)).toList();
    final List<List<Map<String, Object>>> events = toEvents(new String[] { legacy ? getTimestampName() + ":TIME" : null, null, QueryRunnerTestHelper.QUALITY_DIMENSION + ":STRING", null, null, QueryRunnerTestHelper.INDEX_METRIC + ":DOUBLE" }, // filtered values with day granularity
    new String[] { "2011-01-12T00:00:00.000Z\ttotal_market\tmezzanine\tpreferred\tmpreferred\t1000.000000", "2011-01-12T00:00:00.000Z\ttotal_market\tpremium\tpreferred\tppreferred\t1000.000000" }, new String[] { "2011-01-13T00:00:00.000Z\ttotal_market\tmezzanine\tpreferred\tmpreferred\t1040.945505", "2011-01-13T00:00:00.000Z\ttotal_market\tpremium\tpreferred\tppreferred\t1689.012875" });
    List<ScanResultValue> expectedResults = toExpected(events, legacy ? Lists.newArrayList(getTimestampName(), QueryRunnerTestHelper.QUALITY_DIMENSION, QueryRunnerTestHelper.INDEX_METRIC) : Lists.newArrayList(QueryRunnerTestHelper.QUALITY_DIMENSION, QueryRunnerTestHelper.INDEX_METRIC), 0, 3);
    verify(expectedResults, results);
    verify(expectedResults, resultsOptimize);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) MapLookupExtractor(org.apache.druid.query.extraction.MapLookupExtractor) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 18 with MapLookupExtractor

use of org.apache.druid.query.extraction.MapLookupExtractor in project druid by druid-io.

the class LookupExtractionFnTest method testSimpleSerDe.

@Test
public void testSimpleSerDe() throws IOException {
    if (retainMissing && !NullHandling.isNullOrEquivalent(replaceMissing)) {
        // skip
        return;
    }
    final LookupExtractionFn lookupExtractionFn = new LookupExtractionFn(new MapLookupExtractor(ImmutableMap.of("foo", "bar"), false), retainMissing, replaceMissing, injective, false);
    final String str1 = OBJECT_MAPPER.writeValueAsString(lookupExtractionFn);
    final LookupExtractionFn lookupExtractionFn2 = OBJECT_MAPPER.readValue(str1, LookupExtractionFn.class);
    Assert.assertEquals(retainMissing, lookupExtractionFn2.isRetainMissingValue());
    Assert.assertEquals(replaceMissing, lookupExtractionFn2.getReplaceMissingValueWith());
    if (injective == null) {
        Assert.assertEquals(lookupExtractionFn2.getLookup().isOneToOne(), lookupExtractionFn2.isInjective());
    } else {
        Assert.assertEquals(injective, lookupExtractionFn2.isInjective());
    }
    Assert.assertArrayEquals(lookupExtractionFn.getCacheKey(), lookupExtractionFn2.getCacheKey());
    Assert.assertEquals(str1, OBJECT_MAPPER.writeValueAsString(lookupExtractionFn2));
}
Also used : MapLookupExtractor(org.apache.druid.query.extraction.MapLookupExtractor) Test(org.junit.Test)

Example 19 with MapLookupExtractor

use of org.apache.druid.query.extraction.MapLookupExtractor in project druid by druid-io.

the class GroupByQueryRunnerTest method testGroupByWithRebucketRename.

@Test
public void testGroupByWithRebucketRename() {
    // Cannot vectorize due to extraction dimension spec.
    cannotVectorize();
    Map<String, String> map = new HashMap<>();
    map.put("automotive", "automotive0");
    map.put("business", "business0");
    map.put("entertainment", "entertainment0");
    map.put("health", "health0");
    map.put("mezzanine", "mezzanine0");
    map.put("news", "news0");
    map.put("premium", "premium0");
    map.put("technology", "technology0");
    map.put("travel", "travel0");
    GroupByQuery query = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new ExtractionDimensionSpec("quality", "alias", new LookupExtractionFn(new MapLookupExtractor(map, false), false, null, false, false))).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("idx", "index")).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
    List<ResultRow> expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "alias", "automotive0", "rows", 1L, "idx", 135L), makeRow(query, "2011-04-01", "alias", "business0", "rows", 1L, "idx", 118L), makeRow(query, "2011-04-01", "alias", "entertainment0", "rows", 1L, "idx", 158L), makeRow(query, "2011-04-01", "alias", "health0", "rows", 1L, "idx", 120L), makeRow(query, "2011-04-01", "alias", "mezzanine0", "rows", 3L, "idx", 2870L), makeRow(query, "2011-04-01", "alias", "news0", "rows", 1L, "idx", 121L), makeRow(query, "2011-04-01", "alias", "premium0", "rows", 3L, "idx", 2900L), makeRow(query, "2011-04-01", "alias", "technology0", "rows", 1L, "idx", 78L), makeRow(query, "2011-04-01", "alias", "travel0", "rows", 1L, "idx", 119L), makeRow(query, "2011-04-02", "alias", "automotive0", "rows", 1L, "idx", 147L), makeRow(query, "2011-04-02", "alias", "business0", "rows", 1L, "idx", 112L), makeRow(query, "2011-04-02", "alias", "entertainment0", "rows", 1L, "idx", 166L), makeRow(query, "2011-04-02", "alias", "health0", "rows", 1L, "idx", 113L), makeRow(query, "2011-04-02", "alias", "mezzanine0", "rows", 3L, "idx", 2447L), makeRow(query, "2011-04-02", "alias", "news0", "rows", 1L, "idx", 114L), makeRow(query, "2011-04-02", "alias", "premium0", "rows", 3L, "idx", 2505L), makeRow(query, "2011-04-02", "alias", "technology0", "rows", 1L, "idx", 97L), makeRow(query, "2011-04-02", "alias", "travel0", "rows", 1L, "idx", 126L));
    Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
    TestHelper.assertExpectedObjects(expectedResults, results, "rebucket-rename");
}
Also used : LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) HashMap(java.util.HashMap) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) MapLookupExtractor(org.apache.druid.query.extraction.MapLookupExtractor) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 20 with MapLookupExtractor

use of org.apache.druid.query.extraction.MapLookupExtractor in project druid by druid-io.

the class GroupByQueryRunnerTest method testGroupByWithExtractionDimFilterCaseMappingValueIsNullOrEmpty.

@Test
public void testGroupByWithExtractionDimFilterCaseMappingValueIsNullOrEmpty() {
    Map<String, String> extractionMap = new HashMap<>();
    extractionMap.put("automotive", "automotive0");
    extractionMap.put("business", "business0");
    extractionMap.put("entertainment", "entertainment0");
    extractionMap.put("health", "health0");
    extractionMap.put("mezzanine", null);
    extractionMap.put("news", "");
    extractionMap.put("premium", "premium0");
    extractionMap.put("technology", "technology0");
    extractionMap.put("travel", "travel0");
    MapLookupExtractor mapLookupExtractor = new MapLookupExtractor(extractionMap, false);
    LookupExtractionFn lookupExtractionFn = new LookupExtractionFn(mapLookupExtractor, false, null, true, false);
    GroupByQuery query = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("quality", "alias")).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("idx", "index")).setGranularity(QueryRunnerTestHelper.DAY_GRAN).setDimFilter(new ExtractionDimFilter("quality", "", lookupExtractionFn, null)).build();
    List<ResultRow> expectedResults;
    if (NullHandling.replaceWithDefault()) {
        expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "alias", "mezzanine", "rows", 3L, "idx", 2870L), makeRow(query, "2011-04-01", "alias", "news", "rows", 1L, "idx", 121L), makeRow(query, "2011-04-02", "alias", "mezzanine", "rows", 3L, "idx", 2447L), makeRow(query, "2011-04-02", "alias", "news", "rows", 1L, "idx", 114L));
    } else {
        // Only empty string should match, nulls will not match
        expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "alias", "news", "rows", 1L, "idx", 121L), makeRow(query, "2011-04-02", "alias", "news", "rows", 1L, "idx", 114L));
    }
    Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
    TestHelper.assertExpectedObjects(expectedResults, results, "dim-extraction");
}
Also used : LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) HashMap(java.util.HashMap) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) MapLookupExtractor(org.apache.druid.query.extraction.MapLookupExtractor) ExtractionDimFilter(org.apache.druid.query.filter.ExtractionDimFilter) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Aggregations

MapLookupExtractor (org.apache.druid.query.extraction.MapLookupExtractor)58 Test (org.junit.Test)56 LookupExtractionFn (org.apache.druid.query.lookup.LookupExtractionFn)40 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)30 HashMap (java.util.HashMap)25 ExtractionDimensionSpec (org.apache.druid.query.dimension.ExtractionDimensionSpec)20 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)18 SelectorDimFilter (org.apache.druid.query.filter.SelectorDimFilter)14 Result (org.apache.druid.query.Result)13 ExtractionDimFilter (org.apache.druid.query.filter.ExtractionDimFilter)13 ImmutableList (com.google.common.collect.ImmutableList)11 List (java.util.List)11 NullHandlingTest (org.apache.druid.common.config.NullHandlingTest)11 PreJoinableClause (org.apache.druid.query.planning.PreJoinableClause)11 InDimFilter (org.apache.druid.query.filter.InDimFilter)10 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)8 BoundDimFilter (org.apache.druid.query.filter.BoundDimFilter)8 LookupExtractor (org.apache.druid.query.lookup.LookupExtractor)8 JavaScriptDimFilter (org.apache.druid.query.filter.JavaScriptDimFilter)6 RegexDimFilter (org.apache.druid.query.filter.RegexDimFilter)6