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");
}
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");
}
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());
}
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"))));
}
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());
}
Aggregations