Search in sources :

Example 1 with JavaScriptExtractionFn

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

the class InFilterTest method testMatchWithExtractionFn.

@Test
public void testMatchWithExtractionFn() {
    String extractionJsFn = "function(str) { return 'super-' + str; }";
    ExtractionFn superFn = new JavaScriptExtractionFn(extractionJsFn, false, JavaScriptConfig.getEnabledInstance());
    String nullJsFn = "function(str) { if (str === null) { return 'YES'; } else { return 'NO';} }";
    ExtractionFn yesNullFn = new JavaScriptExtractionFn(nullJsFn, false, JavaScriptConfig.getEnabledInstance());
    if (NullHandling.replaceWithDefault()) {
        assertFilterMatches(toInFilterWithFn("dim2", superFn, "super-null", "super-a", "super-b"), ImmutableList.of("a", "b", "c", "d", "f"));
        assertFilterMatches(toInFilterWithFn("dim1", superFn, "super-null", "super-10", "super-def"), ImmutableList.of("a", "b", "e"));
        assertFilterMatches(toInFilterWithFn("dim2", yesNullFn, "YES"), ImmutableList.of("b", "c", "f"));
        assertFilterMatches(toInFilterWithFn("dim1", yesNullFn, "NO"), ImmutableList.of("b", "c", "d", "e", "f"));
    } else {
        assertFilterMatches(toInFilterWithFn("dim2", superFn, "super-null", "super-a", "super-b"), ImmutableList.of("a", "b", "d", "f"));
        assertFilterMatches(toInFilterWithFn("dim1", superFn, "super-null", "super-10", "super-def"), ImmutableList.of("b", "e"));
        assertFilterMatches(toInFilterWithFn("dim2", yesNullFn, "YES"), ImmutableList.of("b", "f"));
        assertFilterMatches(toInFilterWithFn("dim1", yesNullFn, "NO"), ImmutableList.of("a", "b", "c", "d", "e", "f"));
    }
    assertFilterMatches(toInFilterWithFn("dim3", yesNullFn, "NO"), ImmutableList.of());
    assertFilterMatches(toInFilterWithFn("dim3", yesNullFn, "YES"), ImmutableList.of("a", "b", "c", "d", "e", "f"));
}
Also used : ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) Test(org.junit.Test)

Example 2 with JavaScriptExtractionFn

use of org.apache.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());
    if (NullHandling.replaceWithDefault()) {
        assertFilterMatches(new RegexDimFilter("dim1", ".*ANYMORE", changeNullFn), ImmutableList.of("0"));
        assertFilterMatches(new RegexDimFilter("dim2", ".*ANYMORE", changeNullFn), ImmutableList.of("1", "2", "5"));
    } else {
        assertFilterMatches(new RegexDimFilter("dim1", ".*ANYMORE", changeNullFn), ImmutableList.of());
        assertFilterMatches(new RegexDimFilter("dim2", ".*ANYMORE", changeNullFn), ImmutableList.of("1", "5"));
    }
    assertFilterMatches(new RegexDimFilter("dim1", "ab.*", changeNullFn), ImmutableList.of("4", "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.of());
    assertFilterMatches(new RegexDimFilter("dim4", ".*ANYMORE", changeNullFn), ImmutableList.of("0", "1", "2", "3", "4", "5"));
    assertFilterMatches(new RegexDimFilter("dim4", "a.*", changeNullFn), ImmutableList.of());
}
Also used : ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) RegexDimFilter(org.apache.druid.query.filter.RegexDimFilter) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) Test(org.junit.Test)

Example 3 with JavaScriptExtractionFn

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

the class TopNQueryRunnerTest method testFullOnTopNDimExtractionAllNulls.

@Test
public void testFullOnTopNDimExtractionAllNulls() {
    String jsFn = "function(str) { return null; }";
    ExtractionFn jsExtractionFn = new JavaScriptExtractionFn(jsFn, false, JavaScriptConfig.getEnabledInstance());
    TopNQuery query = new TopNQueryBuilder().dataSource(QueryRunnerTestHelper.DATA_SOURCE).granularity(QueryRunnerTestHelper.ALL_GRAN).dimension(new ExtractionDimensionSpec(QueryRunnerTestHelper.MARKET_DIMENSION, QueryRunnerTestHelper.MARKET_DIMENSION, jsExtractionFn)).metric(QueryRunnerTestHelper.INDEX_METRIC).threshold(4).intervals(QueryRunnerTestHelper.FULL_ON_INTERVAL_SPEC).aggregators(Lists.newArrayList(Iterables.concat(commonAggregators, Lists.newArrayList(new DoubleMaxAggregatorFactory("maxIndex", "index"), new DoubleMinAggregatorFactory("minIndex", "index"))))).postAggregators(QueryRunnerTestHelper.ADD_ROWS_INDEX_CONSTANT).build();
    Map<String, Object> expectedMap = new HashMap<>();
    expectedMap.put(QueryRunnerTestHelper.MARKET_DIMENSION, null);
    expectedMap.put("rows", 1209L);
    expectedMap.put("index", 503332.5071372986D);
    expectedMap.put("addRowsIndexConstant", 504542.5071372986D);
    expectedMap.put("uniques", 9.019833517963864);
    expectedMap.put("maxIndex", 1870.061029D);
    expectedMap.put("minIndex", 59.02102279663086D);
    List<Result<TopNResultValue>> expectedResults = Collections.singletonList(new Result<>(DateTimes.of("2011-01-12T00:00:00.000Z"), new TopNResultValue(Collections.singletonList(expectedMap))));
    assertExpectedResults(expectedResults, query);
}
Also used : DoubleMaxAggregatorFactory(org.apache.druid.query.aggregation.DoubleMaxAggregatorFactory) HashMap(java.util.HashMap) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) DoubleMinAggregatorFactory(org.apache.druid.query.aggregation.DoubleMinAggregatorFactory) Result(org.apache.druid.query.Result) ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) DimExtractionFn(org.apache.druid.query.extraction.DimExtractionFn) RegexDimExtractionFn(org.apache.druid.query.extraction.RegexDimExtractionFn) StringFormatExtractionFn(org.apache.druid.query.extraction.StringFormatExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) TimeFormatExtractionFn(org.apache.druid.query.extraction.TimeFormatExtractionFn) StrlenExtractionFn(org.apache.druid.query.extraction.StrlenExtractionFn) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 4 with JavaScriptExtractionFn

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

the class TopNQueryRunnerTest method testFullOnTopNFloatColumnWithExFn.

@Test
public void testFullOnTopNFloatColumnWithExFn() {
    String jsFn = "function(str) { return 'super-' + str; }";
    ExtractionFn jsExtractionFn = new JavaScriptExtractionFn(jsFn, false, JavaScriptConfig.getEnabledInstance());
    TopNQuery query = new TopNQueryBuilder().dataSource(QueryRunnerTestHelper.DATA_SOURCE).granularity(QueryRunnerTestHelper.ALL_GRAN).dimension(new ExtractionDimensionSpec(QueryRunnerTestHelper.INDEX_METRIC, "index_alias", jsExtractionFn)).metric(QueryRunnerTestHelper.INDEX_METRIC).threshold(4).intervals(QueryRunnerTestHelper.FULL_ON_INTERVAL_SPEC).aggregators(Lists.newArrayList(Iterables.concat(commonAggregators, Lists.newArrayList(new DoubleMaxAggregatorFactory("maxIndex", "index"), new DoubleMinAggregatorFactory("minIndex", "index"))))).postAggregators(QueryRunnerTestHelper.ADD_ROWS_INDEX_CONSTANT).build();
    List<Result<TopNResultValue>> expectedResults = Collections.singletonList(new Result<>(DateTimes.of("2011-01-12T00:00:00.000Z"), new TopNResultValue(Arrays.<Map<String, Object>>asList(ImmutableMap.<String, Object>builder().put("index_alias", "super-1000").put(QueryRunnerTestHelper.INDEX_METRIC, 2000.0D).put("rows", 2L).put("addRowsIndexConstant", 2003.0D).put("uniques", QueryRunnerTestHelper.UNIQUES_2).put("maxIndex", 1000.0D).put("minIndex", 1000.0D).build(), ImmutableMap.<String, Object>builder().put("index_alias", "super-1870.061029").put(QueryRunnerTestHelper.INDEX_METRIC, 1870.061029D).put("rows", 1L).put("addRowsIndexConstant", 1872.06103515625D).put("uniques", QueryRunnerTestHelper.UNIQUES_1).put("maxIndex", 1870.061029D).put("minIndex", 1870.061029D).build(), ImmutableMap.<String, Object>builder().put("index_alias", "super-1862.737933").put(QueryRunnerTestHelper.INDEX_METRIC, 1862.737933D).put("rows", 1L).put("addRowsIndexConstant", 1864.7379150390625D).put("uniques", QueryRunnerTestHelper.UNIQUES_1).put("maxIndex", 1862.737933D).put("minIndex", 1862.737933D).build(), ImmutableMap.<String, Object>builder().put("index_alias", "super-1743.92175").put(QueryRunnerTestHelper.INDEX_METRIC, 1743.92175D).put("rows", 1L).put("addRowsIndexConstant", 1745.9217529296875D).put("uniques", QueryRunnerTestHelper.UNIQUES_1).put("maxIndex", 1743.92175D).put("minIndex", 1743.92175D).build()))));
    assertExpectedResults(expectedResults, query);
}
Also used : DoubleMaxAggregatorFactory(org.apache.druid.query.aggregation.DoubleMaxAggregatorFactory) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) DoubleMinAggregatorFactory(org.apache.druid.query.aggregation.DoubleMinAggregatorFactory) Result(org.apache.druid.query.Result) ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) DimExtractionFn(org.apache.druid.query.extraction.DimExtractionFn) RegexDimExtractionFn(org.apache.druid.query.extraction.RegexDimExtractionFn) StringFormatExtractionFn(org.apache.druid.query.extraction.StringFormatExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) TimeFormatExtractionFn(org.apache.druid.query.extraction.TimeFormatExtractionFn) StrlenExtractionFn(org.apache.druid.query.extraction.StrlenExtractionFn) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 5 with JavaScriptExtractionFn

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

the class TopNQueryRunnerTest method testTopNQueryCardinalityAggregatorWithExtractionFn.

@Test
public void testTopNQueryCardinalityAggregatorWithExtractionFn() {
    String helloJsFn = "function(str) { return 'hello' }";
    ExtractionFn helloFn = new JavaScriptExtractionFn(helloJsFn, false, JavaScriptConfig.getEnabledInstance());
    DimensionSpec dimSpec = new ExtractionDimensionSpec(QueryRunnerTestHelper.MARKET_DIMENSION, QueryRunnerTestHelper.MARKET_DIMENSION, helloFn);
    ImmutableList<DimensionSpec> aggregatorDimensionSpecs = ImmutableList.of(new ExtractionDimensionSpec(QueryRunnerTestHelper.QUALITY_DIMENSION, QueryRunnerTestHelper.QUALITY_DIMENSION, helloFn));
    TopNQuery query = new TopNQueryBuilder().dataSource(QueryRunnerTestHelper.DATA_SOURCE).granularity(QueryRunnerTestHelper.ALL_GRAN).dimension(dimSpec).metric(new NumericTopNMetricSpec("numVals")).threshold(10).intervals(QueryRunnerTestHelper.FIRST_TO_THIRD).aggregators(duplicateAggregators(new CardinalityAggregatorFactory("numVals", aggregatorDimensionSpecs, false), new CardinalityAggregatorFactory("numVals1", aggregatorDimensionSpecs, false))).build();
    List<Result<TopNResultValue>> expectedResults = Collections.singletonList(new Result<>(DateTimes.of("2011-04-01T00:00:00.000Z"), new TopNResultValue(withDuplicateResults(Collections.singletonList(ImmutableMap.of("market", "hello", "numVals", 1.0002442201269182d)), "numVals", "numVals1"))));
    assertExpectedResults(expectedResults, query);
}
Also used : DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) ListFilteredDimensionSpec(org.apache.druid.query.dimension.ListFilteredDimensionSpec) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) Result(org.apache.druid.query.Result) ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) DimExtractionFn(org.apache.druid.query.extraction.DimExtractionFn) RegexDimExtractionFn(org.apache.druid.query.extraction.RegexDimExtractionFn) StringFormatExtractionFn(org.apache.druid.query.extraction.StringFormatExtractionFn) JavaScriptExtractionFn(org.apache.druid.query.extraction.JavaScriptExtractionFn) LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) TimeFormatExtractionFn(org.apache.druid.query.extraction.TimeFormatExtractionFn) StrlenExtractionFn(org.apache.druid.query.extraction.StrlenExtractionFn) CardinalityAggregatorFactory(org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Aggregations

JavaScriptExtractionFn (org.apache.druid.query.extraction.JavaScriptExtractionFn)23 Test (org.junit.Test)23 ExtractionFn (org.apache.druid.query.extraction.ExtractionFn)21 LookupExtractionFn (org.apache.druid.query.lookup.LookupExtractionFn)17 TimeFormatExtractionFn (org.apache.druid.query.extraction.TimeFormatExtractionFn)16 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)16 ExtractionDimensionSpec (org.apache.druid.query.dimension.ExtractionDimensionSpec)14 DimExtractionFn (org.apache.druid.query.extraction.DimExtractionFn)12 RegexDimExtractionFn (org.apache.druid.query.extraction.RegexDimExtractionFn)12 StringFormatExtractionFn (org.apache.druid.query.extraction.StringFormatExtractionFn)12 StrlenExtractionFn (org.apache.druid.query.extraction.StrlenExtractionFn)12 Result (org.apache.druid.query.Result)8 CascadeExtractionFn (org.apache.druid.query.extraction.CascadeExtractionFn)7 SearchQuerySpecDimExtractionFn (org.apache.druid.query.extraction.SearchQuerySpecDimExtractionFn)7 SubstringDimExtractionFn (org.apache.druid.query.extraction.SubstringDimExtractionFn)7 SelectorDimFilter (org.apache.druid.query.filter.SelectorDimFilter)7 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)5 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)5 DoubleMaxAggregatorFactory (org.apache.druid.query.aggregation.DoubleMaxAggregatorFactory)4 DoubleMinAggregatorFactory (org.apache.druid.query.aggregation.DoubleMinAggregatorFactory)4