Search in sources :

Example 16 with ExtractionFn

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

the class GroupByQueryRunnerTest method testGroupByFloatColumnWithExFn.

@Test
public void testGroupByFloatColumnWithExFn() {
    // Cannot vectorize due to extraction dimension spec.
    cannotVectorize();
    if (config.getDefaultStrategy().equals(GroupByStrategySelector.STRATEGY_V1)) {
        expectedException.expect(UnsupportedOperationException.class);
        expectedException.expectMessage("GroupBy v1 does not support dimension selectors with unknown cardinality.");
    }
    String jsFn = "function(str) { return 'super-' + str; }";
    ExtractionFn jsExtractionFn = new JavaScriptExtractionFn(jsFn, false, JavaScriptConfig.getEnabledInstance());
    GroupByQuery query = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new ExtractionDimensionSpec("index", "index_alias", jsExtractionFn)).setDimFilter(new SelectorDimFilter("quality", "entertainment", null)).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("idx", "index")).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
    List<ResultRow> expectedResults;
    expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "index_alias", "super-158.747224", "rows", 1L, "idx", 158L), makeRow(query, "2011-04-02", "index_alias", "super-166.016049", "rows", 1L, "idx", 166L));
    Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
    TestHelper.assertExpectedObjects(expectedResults, results, "float");
}
Also used : RegexDimExtractionFn(org.apache.druid.query.extraction.RegexDimExtractionFn) StringFormatExtractionFn(org.apache.druid.query.extraction.StringFormatExtractionFn) LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) CascadeExtractionFn(org.apache.druid.query.extraction.CascadeExtractionFn) StrlenExtractionFn(org.apache.druid.query.extraction.StrlenExtractionFn) SubstringDimExtractionFn(org.apache.druid.query.extraction.SubstringDimExtractionFn) ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) DimExtractionFn(org.apache.druid.query.extraction.DimExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) SearchQuerySpecDimExtractionFn(org.apache.druid.query.extraction.SearchQuerySpecDimExtractionFn) TimeFormatExtractionFn(org.apache.druid.query.extraction.TimeFormatExtractionFn) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 17 with ExtractionFn

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

the class GroupByQueryRunnerTest method testGroupByLongTimeColumnWithExFn.

@Test
public void testGroupByLongTimeColumnWithExFn() {
    // Cannot vectorize due to extraction dimension spec.
    cannotVectorize();
    String jsFn = "function(str) { return 'super-' + str; }";
    ExtractionFn jsExtractionFn = new JavaScriptExtractionFn(jsFn, false, JavaScriptConfig.getEnabledInstance());
    GroupByQuery query = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new ExtractionDimensionSpec("__time", "time_alias", jsExtractionFn)).setDimFilter(new SelectorDimFilter("quality", "entertainment", null)).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("idx", "index")).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
    List<ResultRow> expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "time_alias", "super-1301616000000", "rows", 1L, "idx", 158L), makeRow(query, "2011-04-02", "time_alias", "super-1301702400000", "rows", 1L, "idx", 166L));
    Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
    TestHelper.assertExpectedObjects(expectedResults, results, "long-extraction");
}
Also used : RegexDimExtractionFn(org.apache.druid.query.extraction.RegexDimExtractionFn) StringFormatExtractionFn(org.apache.druid.query.extraction.StringFormatExtractionFn) LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) CascadeExtractionFn(org.apache.druid.query.extraction.CascadeExtractionFn) StrlenExtractionFn(org.apache.druid.query.extraction.StrlenExtractionFn) SubstringDimExtractionFn(org.apache.druid.query.extraction.SubstringDimExtractionFn) ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) DimExtractionFn(org.apache.druid.query.extraction.DimExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) SearchQuerySpecDimExtractionFn(org.apache.druid.query.extraction.SearchQuerySpecDimExtractionFn) TimeFormatExtractionFn(org.apache.druid.query.extraction.TimeFormatExtractionFn) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 18 with ExtractionFn

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

the class DimensionSelectorHavingSpecTest method testToString.

@Test
public void testToString() {
    ExtractionFn extractionFn = new RegexDimExtractionFn("^([^,]*),", false, "");
    String expected = "DimensionSelectorHavingSpec{" + "dimension='gender'," + " value='m'," + " extractionFn=regex(/^([^,]*),/, 1)}";
    Assert.assertEquals(expected, new DimensionSelectorHavingSpec("gender", "m", extractionFn).toString());
    expected = "DimensionSelectorHavingSpec{" + "dimension='gender'," + " value='m'," + " extractionFn=Identity}";
    Assert.assertEquals(expected, new DimensionSelectorHavingSpec("gender", "m", null).toString());
}
Also used : ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) RegexDimExtractionFn(org.apache.druid.query.extraction.RegexDimExtractionFn) RegexDimExtractionFn(org.apache.druid.query.extraction.RegexDimExtractionFn) Test(org.junit.Test)

Example 19 with ExtractionFn

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

the class DimensionSelectorHavingSpecTest method testDimensionFilterSpec.

@Test
public void testDimensionFilterSpec() {
    DimensionSelectorHavingSpec spec = new DimensionSelectorHavingSpec("dimension", "v", null);
    Assert.assertTrue(spec.eval(getTestRow("v")));
    Assert.assertTrue(spec.eval(getTestRow(ImmutableList.of("v", "v1"))));
    Assert.assertFalse(spec.eval(getTestRow(ImmutableList.of())));
    Assert.assertFalse(spec.eval(getTestRow("v1")));
    spec = new DimensionSelectorHavingSpec("dimension", null, null);
    Assert.assertTrue(spec.eval(getTestRow(ImmutableList.of())));
    Assert.assertTrue(spec.eval(getTestRow(ImmutableList.of(""))));
    Assert.assertFalse(spec.eval(getTestRow(ImmutableList.of("v"))));
    Assert.assertFalse(spec.eval(getTestRow(ImmutableList.of("v", "v1"))));
    spec = new DimensionSelectorHavingSpec("dimension", "", null);
    Assert.assertTrue(spec.eval(getTestRow(ImmutableList.of())));
    Assert.assertTrue(spec.eval(getTestRow(ImmutableList.of(""))));
    Assert.assertTrue(spec.eval(getTestRow(ImmutableList.of("v", "v1", ""))));
    Assert.assertFalse(spec.eval(getTestRow(ImmutableList.of("v"))));
    Assert.assertFalse(spec.eval(getTestRow(ImmutableList.of("v", "v1"))));
    ExtractionFn extractionFn = new RegexDimExtractionFn("^([^,]*),", true, "default");
    spec = new DimensionSelectorHavingSpec("dimension", "v", extractionFn);
    Assert.assertTrue(spec.eval(getTestRow(ImmutableList.of("v,v1", "v2,v3"))));
    Assert.assertFalse(spec.eval(getTestRow(ImmutableList.of("v1,v4"))));
    Assert.assertFalse(spec.eval(getTestRow(ImmutableList.of("v"))));
    Assert.assertFalse(spec.eval(getTestRow(ImmutableList.of("v1", "default"))));
    Assert.assertTrue(spec.eval(getTestRow(ImmutableList.of("v,default", "none"))));
    spec = new DimensionSelectorHavingSpec("dimension", "default", extractionFn);
    Assert.assertTrue(spec.eval(getTestRow(ImmutableList.of("v1,v2", "none"))));
}
Also used : ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) RegexDimExtractionFn(org.apache.druid.query.extraction.RegexDimExtractionFn) RegexDimExtractionFn(org.apache.druid.query.extraction.RegexDimExtractionFn) Test(org.junit.Test)

Example 20 with ExtractionFn

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

the class SearchQueryFilterTest method testSearchQueryWithExtractionFn.

@Test
public void testSearchQueryWithExtractionFn() {
    String nullJsFn = "function(str) { if (str === null) { return 'NOT_NULL_ANYMORE'; } else { return str;} }";
    ExtractionFn changeNullFn = new JavaScriptExtractionFn(nullJsFn, false, JavaScriptConfig.getEnabledInstance());
    if (NullHandling.replaceWithDefault()) {
        assertFilterMatches(new SearchQueryDimFilter("dim1", specForValue("ANYMORE"), changeNullFn), ImmutableList.of("0"));
        assertFilterMatches(new SearchQueryDimFilter("dim2", specForValue("ANYMORE"), changeNullFn), ImmutableList.of("1", "2", "5"));
    } else {
        assertFilterMatches(new SearchQueryDimFilter("dim1", specForValue("ANYMORE"), changeNullFn), ImmutableList.of());
        assertFilterMatches(new SearchQueryDimFilter("dim2", specForValue("ANYMORE"), changeNullFn), ImmutableList.of("1", "5"));
    }
    assertFilterMatches(new SearchQueryDimFilter("dim1", specForValue("ab"), changeNullFn), ImmutableList.of("4", "5"));
    assertFilterMatches(new SearchQueryDimFilter("dim1", specForValue("ab"), changeNullFn), ImmutableList.of("4", "5"));
    assertFilterMatches(new SearchQueryDimFilter("dim2", specForValue("a"), changeNullFn), ImmutableList.of("0", "3"));
    assertFilterMatches(new SearchQueryDimFilter("dim3", specForValue("ANYMORE"), changeNullFn), ImmutableList.of("0", "1", "2", "3", "4", "5"));
    assertFilterMatches(new SearchQueryDimFilter("dim3", specForValue("a"), changeNullFn), ImmutableList.of());
    assertFilterMatches(new SearchQueryDimFilter("dim4", specForValue("ANYMORE"), changeNullFn), ImmutableList.of("0", "1", "2", "3", "4", "5"));
    assertFilterMatches(new SearchQueryDimFilter("dim4", specForValue("a"), changeNullFn), ImmutableList.of());
}
Also used : ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) SearchQueryDimFilter(org.apache.druid.query.filter.SearchQueryDimFilter) Test(org.junit.Test)

Aggregations

ExtractionFn (org.apache.druid.query.extraction.ExtractionFn)40 Test (org.junit.Test)33 JavaScriptExtractionFn (org.apache.druid.query.extraction.JavaScriptExtractionFn)30 TimeFormatExtractionFn (org.apache.druid.query.extraction.TimeFormatExtractionFn)26 LookupExtractionFn (org.apache.druid.query.lookup.LookupExtractionFn)26 RegexDimExtractionFn (org.apache.druid.query.extraction.RegexDimExtractionFn)23 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)22 ExtractionDimensionSpec (org.apache.druid.query.dimension.ExtractionDimensionSpec)20 DimExtractionFn (org.apache.druid.query.extraction.DimExtractionFn)20 StringFormatExtractionFn (org.apache.druid.query.extraction.StringFormatExtractionFn)20 StrlenExtractionFn (org.apache.druid.query.extraction.StrlenExtractionFn)20 CascadeExtractionFn (org.apache.druid.query.extraction.CascadeExtractionFn)12 SearchQuerySpecDimExtractionFn (org.apache.druid.query.extraction.SearchQuerySpecDimExtractionFn)12 SubstringDimExtractionFn (org.apache.druid.query.extraction.SubstringDimExtractionFn)12 SelectorDimFilter (org.apache.druid.query.filter.SelectorDimFilter)11 Result (org.apache.druid.query.Result)9 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)9 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)8 ArrayList (java.util.ArrayList)6 DoubleMaxAggregatorFactory (org.apache.druid.query.aggregation.DoubleMaxAggregatorFactory)5