Search in sources :

Example 1 with JavaScriptExtractionFn

use of io.druid.query.extraction.JavaScriptExtractionFn in project druid by druid-io.

the class TimeFilteringTest method testIntervalFilterOnStringDimension.

@Test
public void testIntervalFilterOnStringDimension() {
    assertFilterMatches(new IntervalDimFilter("dim0", Arrays.asList(Interval.parse("1970-01-01T00:00:00.001Z/1970-01-01T00:00:00.005Z")), null), ImmutableList.<String>of("1", "2", "3", "4"));
    assertFilterMatches(new IntervalDimFilter("dim0", Arrays.asList(Interval.parse("1970-01-01T00:00:00.000Z/1970-01-01T00:00:00.003Z"), Interval.parse("1970-01-01T00:00:00.004Z/1970-01-01T00:00:00.006Z")), null), ImmutableList.<String>of("0", "1", "2", "4", "5"));
    assertFilterMatches(new IntervalDimFilter("dim0", Arrays.asList(Interval.parse("1970-01-01T00:00:00.000Z/1970-01-01T00:00:00.001Z"), Interval.parse("1970-01-01T00:00:00.003Z/1970-01-01T00:00:00.006Z"), Interval.parse("1970-01-01T00:00:00.002Z/1970-01-01T00:00:00.005Z")), null), ImmutableList.<String>of("0", "2", "3", "4", "5"));
    assertFilterMatches(new IntervalDimFilter("dim1", Arrays.asList(Interval.parse("1970-01-01T00:00:00.002Z/1970-01-01T00:00:00.011Z")), null), ImmutableList.<String>of("1", "2"));
    // increment timestamp by 2 hours
    String timeBoosterJsFn = "function(x) { return(Number(x) + 7200000) }";
    ExtractionFn exFn = new JavaScriptExtractionFn(timeBoosterJsFn, true, JavaScriptConfig.getEnabledInstance());
    assertFilterMatches(new IntervalDimFilter("dim0", Arrays.asList(Interval.parse("1970-01-01T02:00:00.001Z/1970-01-01T02:00:00.005Z")), exFn), ImmutableList.<String>of("1", "2", "3", "4"));
}
Also used : LookupExtractionFn(io.druid.query.lookup.LookupExtractionFn) JavaScriptExtractionFn(io.druid.query.extraction.JavaScriptExtractionFn) ExtractionFn(io.druid.query.extraction.ExtractionFn) TimeFormatExtractionFn(io.druid.query.extraction.TimeFormatExtractionFn) JavaScriptExtractionFn(io.druid.query.extraction.JavaScriptExtractionFn) IntervalDimFilter(io.druid.query.filter.IntervalDimFilter) Test(org.junit.Test)

Example 2 with JavaScriptExtractionFn

use of io.druid.query.extraction.JavaScriptExtractionFn in project druid by druid-io.

the class RegexFilterTest method testRegexWithExtractionFn.

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

Example 3 with JavaScriptExtractionFn

use of io.druid.query.extraction.JavaScriptExtractionFn in project druid by druid-io.

the class FilteredAggregatorTest method testAggregateWithExtractionFns.

@Test
public void testAggregateWithExtractionFns() {
    final float[] values = { 0.15f, 0.27f };
    TestFloatColumnSelector selector;
    FilteredAggregatorFactory factory;
    String extractionJsFn = "function(str) { return str + 'AARDVARK'; }";
    ExtractionFn extractionFn = new JavaScriptExtractionFn(extractionJsFn, false, JavaScriptConfig.getEnabledInstance());
    factory = new FilteredAggregatorFactory(new DoubleSumAggregatorFactory("billy", "value"), new SelectorDimFilter("dim", "aAARDVARK", extractionFn));
    selector = new TestFloatColumnSelector(values);
    validateFilteredAggs(factory, values, selector);
    factory = new FilteredAggregatorFactory(new DoubleSumAggregatorFactory("billy", "value"), new InDimFilter("dim", Arrays.asList("NOT-aAARDVARK", "FOOBAR", "aAARDVARK"), extractionFn));
    selector = new TestFloatColumnSelector(values);
    validateFilteredAggs(factory, values, selector);
    factory = new FilteredAggregatorFactory(new DoubleSumAggregatorFactory("billy", "value"), new BoundDimFilter("dim", "aAARDVARK", "aAARDVARK", false, false, true, extractionFn, StringComparators.ALPHANUMERIC));
    selector = new TestFloatColumnSelector(values);
    validateFilteredAggs(factory, values, selector);
    factory = new FilteredAggregatorFactory(new DoubleSumAggregatorFactory("billy", "value"), new RegexDimFilter("dim", "aAARDVARK", extractionFn));
    selector = new TestFloatColumnSelector(values);
    validateFilteredAggs(factory, values, selector);
    factory = new FilteredAggregatorFactory(new DoubleSumAggregatorFactory("billy", "value"), new SearchQueryDimFilter("dim", new ContainsSearchQuerySpec("aAARDVARK", true), extractionFn));
    selector = new TestFloatColumnSelector(values);
    validateFilteredAggs(factory, values, selector);
    String jsFn = "function(x) { return(x === 'aAARDVARK') }";
    factory = new FilteredAggregatorFactory(new DoubleSumAggregatorFactory("billy", "value"), new JavaScriptDimFilter("dim", jsFn, extractionFn, JavaScriptConfig.getEnabledInstance()));
    selector = new TestFloatColumnSelector(values);
    validateFilteredAggs(factory, values, selector);
}
Also used : RegexDimFilter(io.druid.query.filter.RegexDimFilter) BoundDimFilter(io.druid.query.filter.BoundDimFilter) ContainsSearchQuerySpec(io.druid.query.search.search.ContainsSearchQuerySpec) JavaScriptExtractionFn(io.druid.query.extraction.JavaScriptExtractionFn) JavaScriptExtractionFn(io.druid.query.extraction.JavaScriptExtractionFn) ExtractionFn(io.druid.query.extraction.ExtractionFn) SelectorDimFilter(io.druid.query.filter.SelectorDimFilter) InDimFilter(io.druid.query.filter.InDimFilter) SearchQueryDimFilter(io.druid.query.filter.SearchQueryDimFilter) JavaScriptDimFilter(io.druid.query.filter.JavaScriptDimFilter) Test(org.junit.Test)

Example 4 with JavaScriptExtractionFn

use of io.druid.query.extraction.JavaScriptExtractionFn in project druid by druid-io.

the class GroupByQueryRunnerTest method testGroupByCardinalityAggWithExtractionFn.

@Test
public void testGroupByCardinalityAggWithExtractionFn() {
    String helloJsFn = "function(str) { return 'hello' }";
    ExtractionFn helloFn = new JavaScriptExtractionFn(helloJsFn, false, JavaScriptConfig.getEnabledInstance());
    GroupByQuery query = GroupByQuery.builder().setDataSource(QueryRunnerTestHelper.dataSource).setQuerySegmentSpec(QueryRunnerTestHelper.firstToThird).setDimensions(Lists.<DimensionSpec>newArrayList(new DefaultDimensionSpec("market", "alias"))).setAggregatorSpecs(Arrays.asList(QueryRunnerTestHelper.rowsCount, new CardinalityAggregatorFactory("numVals", ImmutableList.<DimensionSpec>of(new ExtractionDimensionSpec(QueryRunnerTestHelper.qualityDimension, QueryRunnerTestHelper.qualityDimension, helloFn)), false))).setGranularity(QueryRunnerTestHelper.dayGran).build();
    List<Row> expectedResults = Arrays.asList(GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "spot", "rows", 9L, "numVals", 1.0002442201269182d), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "total_market", "rows", 2L, "numVals", 1.0002442201269182d), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-01", "alias", "upfront", "rows", 2L, "numVals", 1.0002442201269182d), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "spot", "rows", 9L, "numVals", 1.0002442201269182d), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "total_market", "rows", 2L, "numVals", 1.0002442201269182d), GroupByQueryRunnerTestHelper.createExpectedRow("2011-04-02", "alias", "upfront", "rows", 2L, "numVals", 1.0002442201269182d));
    Iterable<Row> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
    TestHelper.assertExpectedObjects(expectedResults, results, "");
}
Also used : LookupExtractionFn(io.druid.query.lookup.LookupExtractionFn) StrlenExtractionFn(io.druid.query.extraction.StrlenExtractionFn) JavaScriptExtractionFn(io.druid.query.extraction.JavaScriptExtractionFn) CascadeExtractionFn(io.druid.query.extraction.CascadeExtractionFn) TimeFormatExtractionFn(io.druid.query.extraction.TimeFormatExtractionFn) ExtractionFn(io.druid.query.extraction.ExtractionFn) RegexDimExtractionFn(io.druid.query.extraction.RegexDimExtractionFn) DimExtractionFn(io.druid.query.extraction.DimExtractionFn) DefaultDimensionSpec(io.druid.query.dimension.DefaultDimensionSpec) RegexFilteredDimensionSpec(io.druid.query.dimension.RegexFilteredDimensionSpec) ExtractionDimensionSpec(io.druid.query.dimension.ExtractionDimensionSpec) ListFilteredDimensionSpec(io.druid.query.dimension.ListFilteredDimensionSpec) DimensionSpec(io.druid.query.dimension.DimensionSpec) JavaScriptExtractionFn(io.druid.query.extraction.JavaScriptExtractionFn) Row(io.druid.data.input.Row) CardinalityAggregatorFactory(io.druid.query.aggregation.cardinality.CardinalityAggregatorFactory) DefaultDimensionSpec(io.druid.query.dimension.DefaultDimensionSpec) ExtractionDimensionSpec(io.druid.query.dimension.ExtractionDimensionSpec) Test(org.junit.Test)

Example 5 with JavaScriptExtractionFn

use of io.druid.query.extraction.JavaScriptExtractionFn in project druid by druid-io.

the class SelectQueryRunnerTest method testFullOnSelectWithLongAndFloatWithExFn.

@Test
public void testFullOnSelectWithLongAndFloatWithExFn() {
    String jsFn = "function(str) { return 'super-' + str; }";
    ExtractionFn jsExtractionFn = new JavaScriptExtractionFn(jsFn, false, JavaScriptConfig.getEnabledInstance());
    List<DimensionSpec> dimSpecs = Arrays.<DimensionSpec>asList(new ExtractionDimensionSpec(QueryRunnerTestHelper.indexMetric, "floatIndex", jsExtractionFn), new ExtractionDimensionSpec(Column.TIME_COLUMN_NAME, "longTime", jsExtractionFn));
    SelectQuery query = newTestQuery().dimensionSpecs(dimSpecs).metrics(Arrays.asList(Column.TIME_COLUMN_NAME, "index")).intervals(I_0112_0114).build();
    HashMap<String, Object> context = new HashMap<String, Object>();
    Iterable<Result<SelectResultValue>> results = Sequences.toList(runner.run(query, context), Lists.<Result<SelectResultValue>>newArrayList());
    List<Result<SelectResultValue>> expectedResultsAsc = Arrays.asList(new Result<SelectResultValue>(new DateTime("2011-01-12T00:00:00.000Z"), new SelectResultValue(ImmutableMap.of(QueryRunnerTestHelper.segmentId, 2), Sets.newHashSet("null_column", "floatIndex", "longTime"), Sets.newHashSet("__time", "index"), Arrays.asList(new EventHolder(QueryRunnerTestHelper.segmentId, 0, new ImmutableMap.Builder<String, Object>().put(EventHolder.timestampKey, new DateTime("2011-01-12T00:00:00.000Z")).put("longTime", "super-1294790400000").put("floatIndex", "super-100").put(QueryRunnerTestHelper.indexMetric, 100.000000F).put(Column.TIME_COLUMN_NAME, 1294790400000L).build()), new EventHolder(QueryRunnerTestHelper.segmentId, 1, new ImmutableMap.Builder<String, Object>().put(EventHolder.timestampKey, new DateTime("2011-01-12T00:00:00.000Z")).put("longTime", "super-1294790400000").put("floatIndex", "super-100").put(QueryRunnerTestHelper.indexMetric, 100.000000F).put(Column.TIME_COLUMN_NAME, 1294790400000L).build()), new EventHolder(QueryRunnerTestHelper.segmentId, 2, new ImmutableMap.Builder<String, Object>().put(EventHolder.timestampKey, new DateTime("2011-01-12T00:00:00.000Z")).put("longTime", "super-1294790400000").put("floatIndex", "super-100").put(QueryRunnerTestHelper.indexMetric, 100.000000F).put(Column.TIME_COLUMN_NAME, 1294790400000L).build())))));
    List<Result<SelectResultValue>> expectedResultsDsc = Arrays.asList(new Result<SelectResultValue>(new DateTime("2011-01-12T00:00:00.000Z"), new SelectResultValue(ImmutableMap.of(QueryRunnerTestHelper.segmentId, -3), Sets.newHashSet("null_column", "floatIndex", "longTime"), Sets.newHashSet("__time", "index"), Arrays.asList(new EventHolder(QueryRunnerTestHelper.segmentId, -1, new ImmutableMap.Builder<String, Object>().put(EventHolder.timestampKey, new DateTime("2011-01-13T00:00:00.000Z")).put("longTime", "super-1294876800000").put("floatIndex", "super-1564.61767578125").put(QueryRunnerTestHelper.indexMetric, 1564.6177f).put(Column.TIME_COLUMN_NAME, 1294876800000L).build()), new EventHolder(QueryRunnerTestHelper.segmentId, -2, new ImmutableMap.Builder<String, Object>().put(EventHolder.timestampKey, new DateTime("2011-01-13T00:00:00.000Z")).put("longTime", "super-1294876800000").put("floatIndex", "super-826.0601806640625").put(QueryRunnerTestHelper.indexMetric, 826.0602f).put(Column.TIME_COLUMN_NAME, 1294876800000L).build()), new EventHolder(QueryRunnerTestHelper.segmentId, -3, new ImmutableMap.Builder<String, Object>().put(EventHolder.timestampKey, new DateTime("2011-01-13T00:00:00.000Z")).put("longTime", "super-1294876800000").put("floatIndex", "super-1689.0128173828125").put(QueryRunnerTestHelper.indexMetric, 1689.0128f).put(Column.TIME_COLUMN_NAME, 1294876800000L).build())))));
    verify(descending ? expectedResultsDsc : expectedResultsAsc, populateNullColumnAtLastForQueryableIndexCase(results, "null_column"));
}
Also used : DimensionSpec(io.druid.query.dimension.DimensionSpec) ExtractionDimensionSpec(io.druid.query.dimension.ExtractionDimensionSpec) DefaultDimensionSpec(io.druid.query.dimension.DefaultDimensionSpec) HashMap(java.util.HashMap) JavaScriptExtractionFn(io.druid.query.extraction.JavaScriptExtractionFn) DateTime(org.joda.time.DateTime) ImmutableMap(com.google.common.collect.ImmutableMap) Result(io.druid.query.Result) LookupExtractionFn(io.druid.query.lookup.LookupExtractionFn) JavaScriptExtractionFn(io.druid.query.extraction.JavaScriptExtractionFn) ExtractionFn(io.druid.query.extraction.ExtractionFn) ExtractionDimensionSpec(io.druid.query.dimension.ExtractionDimensionSpec) Test(org.junit.Test)

Aggregations

JavaScriptExtractionFn (io.druid.query.extraction.JavaScriptExtractionFn)22 Test (org.junit.Test)22 ExtractionFn (io.druid.query.extraction.ExtractionFn)21 LookupExtractionFn (io.druid.query.lookup.LookupExtractionFn)17 TimeFormatExtractionFn (io.druid.query.extraction.TimeFormatExtractionFn)15 ExtractionDimensionSpec (io.druid.query.dimension.ExtractionDimensionSpec)14 DimExtractionFn (io.druid.query.extraction.DimExtractionFn)11 RegexDimExtractionFn (io.druid.query.extraction.RegexDimExtractionFn)11 StrlenExtractionFn (io.druid.query.extraction.StrlenExtractionFn)11 Result (io.druid.query.Result)8 DateTime (org.joda.time.DateTime)8 CascadeExtractionFn (io.druid.query.extraction.CascadeExtractionFn)6 SelectorDimFilter (io.druid.query.filter.SelectorDimFilter)6 Row (io.druid.data.input.Row)5 LongSumAggregatorFactory (io.druid.query.aggregation.LongSumAggregatorFactory)5 PostAggregator (io.druid.query.aggregation.PostAggregator)5 HyperUniqueFinalizingPostAggregator (io.druid.query.aggregation.hyperloglog.HyperUniqueFinalizingPostAggregator)5 DefaultDimensionSpec (io.druid.query.dimension.DefaultDimensionSpec)5 DoubleMaxAggregatorFactory (io.druid.query.aggregation.DoubleMaxAggregatorFactory)4 DoubleMinAggregatorFactory (io.druid.query.aggregation.DoubleMinAggregatorFactory)4