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