use of org.apache.druid.query.aggregation.JavaScriptAggregatorFactory in project druid by druid-io.
the class GroupByQueryRunnerTest method testGroupByWithAggsOnNumericDimensions.
@Test
public void testGroupByWithAggsOnNumericDimensions() {
// Cannot vectorize due to javascript aggregators.
cannotVectorize();
GroupByQuery query = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("quality", "alias")).setDimFilter(new SelectorDimFilter("quality", "technology", null)).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("qlLong", "qualityLong"), new DoubleSumAggregatorFactory("qlFloat", "qualityLong"), new JavaScriptAggregatorFactory("qlJs", ImmutableList.of("qualityLong"), "function(a,b) { return a + b; }", "function() { return 0; }", "function(a,b) { return a + b }", JavaScriptConfig.getEnabledInstance()), new DoubleSumAggregatorFactory("qfFloat", "qualityFloat"), new LongSumAggregatorFactory("qfLong", "qualityFloat"), new JavaScriptAggregatorFactory("qfJs", ImmutableList.of("qualityFloat"), "function(a,b) { return a + b; }", "function() { return 0; }", "function(a,b) { return a + b }", JavaScriptConfig.getEnabledInstance())).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
List<ResultRow> expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "alias", "technology", "rows", 1L, "qlLong", 1700L, "qlFloat", 1700.0, "qlJs", 1700.0, "qfFloat", 17000.0, "qfLong", 17000L, "qfJs", 17000.0), makeRow(query, "2011-04-02", "alias", "technology", "rows", 1L, "qlLong", 1700L, "qlFloat", 1700.0, "qlJs", 1700.0, "qfFloat", 17000.0, "qfLong", 17000L, "qfJs", 17000.0));
Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
TestHelper.assertExpectedObjects(expectedResults, results, "numeric-dims");
}
use of org.apache.druid.query.aggregation.JavaScriptAggregatorFactory in project druid by druid-io.
the class GroupByQueryRunnerTest method testSubqueryWithOuterJavascriptAggregators.
@Test
public void testSubqueryWithOuterJavascriptAggregators() {
final GroupByQuery subquery = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("market", "market"), new DefaultDimensionSpec("quality", "quality")).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("index", "index")).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
final GroupByQuery query = makeQueryBuilder().setDataSource(subquery).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("quality", "quality")).setAggregatorSpecs(new JavaScriptAggregatorFactory("js_agg", Arrays.asList("index", "rows"), "function(current, index, rows){return current + index + rows;}", "function(){return 0;}", "function(a,b){return a + b;}", JavaScriptConfig.getEnabledInstance())).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
List<ResultRow> expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "quality", "automotive", "js_agg", 136D), makeRow(query, "2011-04-01", "quality", "business", "js_agg", 119D), makeRow(query, "2011-04-01", "quality", "entertainment", "js_agg", 159D), makeRow(query, "2011-04-01", "quality", "health", "js_agg", 121D), makeRow(query, "2011-04-01", "quality", "mezzanine", "js_agg", 2873D), makeRow(query, "2011-04-01", "quality", "news", "js_agg", 122D), makeRow(query, "2011-04-01", "quality", "premium", "js_agg", 2903D), makeRow(query, "2011-04-01", "quality", "technology", "js_agg", 79D), makeRow(query, "2011-04-01", "quality", "travel", "js_agg", 120D), makeRow(query, "2011-04-02", "quality", "automotive", "js_agg", 148D), makeRow(query, "2011-04-02", "quality", "business", "js_agg", 113D), makeRow(query, "2011-04-02", "quality", "entertainment", "js_agg", 167D), makeRow(query, "2011-04-02", "quality", "health", "js_agg", 114D), makeRow(query, "2011-04-02", "quality", "mezzanine", "js_agg", 2450D), makeRow(query, "2011-04-02", "quality", "news", "js_agg", 115D), makeRow(query, "2011-04-02", "quality", "premium", "js_agg", 2508D), makeRow(query, "2011-04-02", "quality", "technology", "js_agg", 98D), makeRow(query, "2011-04-02", "quality", "travel", "js_agg", 127D));
Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
TestHelper.assertExpectedObjects(expectedResults, results, "subquery-javascript");
}
use of org.apache.druid.query.aggregation.JavaScriptAggregatorFactory in project druid by druid-io.
the class GroupByQueryRunnerTest method testSubqueryWithMultiColumnAggregators.
@Test
public void testSubqueryWithMultiColumnAggregators() {
// Cannot vectorize due to javascript functionality.
cannotVectorize();
final GroupByQuery subquery = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("quality", "alias")).setDimFilter(new JavaScriptDimFilter("market", "function(dim){ return true; }", null, JavaScriptConfig.getEnabledInstance())).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new DoubleSumAggregatorFactory("idx_subagg", "index"), new JavaScriptAggregatorFactory("js_agg", Arrays.asList("index", "market"), "function(current, index, dim){return current + index + dim.length;}", "function(){return 0;}", "function(a,b){return a + b;}", JavaScriptConfig.getEnabledInstance())).setPostAggregatorSpecs(Collections.singletonList(new ArithmeticPostAggregator("idx_subpostagg", "+", Arrays.asList(new FieldAccessPostAggregator("the_idx_subagg", "idx_subagg"), new ConstantPostAggregator("thousand", 1000))))).setHavingSpec(new HavingSpec() {
private GroupByQuery query;
@Override
public byte[] getCacheKey() {
return new byte[0];
}
@Override
public void setQuery(GroupByQuery query) {
this.query = query;
}
@Override
public boolean eval(ResultRow row) {
final String field = "idx_subpostagg";
final int p = query.getResultRowSignature().indexOf(field);
return (Rows.objectToNumber(field, row.get(p), true).floatValue() < 3800);
}
}).addOrderByColumn("alias").setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
final GroupByQuery query = makeQueryBuilder().setDataSource(subquery).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("alias", "alias")).setAggregatorSpecs(new LongSumAggregatorFactory("rows", "rows"), new LongSumAggregatorFactory("idx", "idx_subpostagg"), new DoubleSumAggregatorFactory("js_outer_agg", "js_agg")).setPostAggregatorSpecs(Collections.singletonList(new ArithmeticPostAggregator("idx_post", "+", Arrays.asList(new FieldAccessPostAggregator("the_idx_agg", "idx"), new ConstantPostAggregator("ten_thousand", 10000))))).setLimitSpec(new DefaultLimitSpec(Collections.singletonList(new OrderByColumnSpec("alias", OrderByColumnSpec.Direction.DESCENDING)), 5)).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
List<ResultRow> expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "alias", "travel", "rows", 1L, "idx_post", 11119.0, "idx", 1119L, "js_outer_agg", 123.92274475097656), makeRow(query, "2011-04-01", "alias", "technology", "rows", 1L, "idx_post", 11078.0, "idx", 1078L, "js_outer_agg", 82.62254333496094), makeRow(query, "2011-04-01", "alias", "news", "rows", 1L, "idx_post", 11121.0, "idx", 1121L, "js_outer_agg", 125.58358001708984), makeRow(query, "2011-04-01", "alias", "health", "rows", 1L, "idx_post", 11120.0, "idx", 1120L, "js_outer_agg", 124.13470458984375), makeRow(query, "2011-04-01", "alias", "entertainment", "rows", 1L, "idx_post", 11158.0, "idx", 1158L, "js_outer_agg", 162.74722290039062));
// Subqueries are handled by the ToolChest
Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
TestHelper.assertExpectedObjects(expectedResults, results, "subquery-multi-aggs");
}
use of org.apache.druid.query.aggregation.JavaScriptAggregatorFactory in project druid by druid-io.
the class GroupByQueryRunnerTest method testSubqueryWithOuterDimJavascriptAggregators.
@Test
public void testSubqueryWithOuterDimJavascriptAggregators() {
final GroupByQuery subquery = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("market", "market"), new DefaultDimensionSpec("quality", "quality")).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("index", "index")).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
final GroupByQuery query = makeQueryBuilder().setDataSource(subquery).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("quality", "quality")).setAggregatorSpecs(new JavaScriptAggregatorFactory("js_agg", Arrays.asList("index", "market"), "function(current, index, dim){return current + index + dim.length;}", "function(){return 0;}", "function(a,b){return a + b;}", JavaScriptConfig.getEnabledInstance())).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
List<ResultRow> expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "quality", "automotive", "js_agg", 139D), makeRow(query, "2011-04-01", "quality", "business", "js_agg", 122D), makeRow(query, "2011-04-01", "quality", "entertainment", "js_agg", 162D), makeRow(query, "2011-04-01", "quality", "health", "js_agg", 124D), makeRow(query, "2011-04-01", "quality", "mezzanine", "js_agg", 2893D), makeRow(query, "2011-04-01", "quality", "news", "js_agg", 125D), makeRow(query, "2011-04-01", "quality", "premium", "js_agg", 2923D), makeRow(query, "2011-04-01", "quality", "technology", "js_agg", 82D), makeRow(query, "2011-04-01", "quality", "travel", "js_agg", 123D), makeRow(query, "2011-04-02", "quality", "automotive", "js_agg", 151D), makeRow(query, "2011-04-02", "quality", "business", "js_agg", 116D), makeRow(query, "2011-04-02", "quality", "entertainment", "js_agg", 170D), makeRow(query, "2011-04-02", "quality", "health", "js_agg", 117D), makeRow(query, "2011-04-02", "quality", "mezzanine", "js_agg", 2470D), makeRow(query, "2011-04-02", "quality", "news", "js_agg", 118D), makeRow(query, "2011-04-02", "quality", "premium", "js_agg", 2528D), makeRow(query, "2011-04-02", "quality", "technology", "js_agg", 101D), makeRow(query, "2011-04-02", "quality", "travel", "js_agg", 130D));
Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
TestHelper.assertExpectedObjects(expectedResults, results, "subquery-javascript");
}
use of org.apache.druid.query.aggregation.JavaScriptAggregatorFactory in project druid by druid-io.
the class IncrementalIndexIngestionTest method testMultithreadAddFactsUsingExpressionAndJavaScript.
@Test
public void testMultithreadAddFactsUsingExpressionAndJavaScript() throws Exception {
final IncrementalIndex indexExpr = indexCreator.createIndex(new IncrementalIndexSchema.Builder().withQueryGranularity(Granularities.MINUTE).withMetrics(new LongSumAggregatorFactory("oddnum", null, "if(value%2==1,1,0)", TestExprMacroTable.INSTANCE)).withRollup(true).build());
final IncrementalIndex indexJs = indexCreator.createIndex(new IncrementalIndexSchema.Builder().withQueryGranularity(Granularities.MINUTE).withMetrics(new JavaScriptAggregatorFactory("oddnum", ImmutableList.of("value"), "function(current, value) { if (value%2==1) current = current + 1; return current;}", "function() {return 0;}", "function(a, b) { return a + b;}", JavaScriptConfig.getEnabledInstance())).withRollup(true).build());
final int addThreadCount = 2;
Thread[] addThreads = new Thread[addThreadCount];
for (int i = 0; i < addThreadCount; ++i) {
addThreads[i] = new Thread(new Runnable() {
@Override
public void run() {
final Random random = ThreadLocalRandom.current();
try {
for (int j = 0; j < MAX_ROWS / addThreadCount; ++j) {
int randomInt = random.nextInt(100000);
MapBasedInputRow mapBasedInputRowExpr = new MapBasedInputRow(0, Collections.singletonList("billy"), ImmutableMap.of("billy", randomInt % 3, "value", randomInt));
MapBasedInputRow mapBasedInputRowJs = new MapBasedInputRow(0, Collections.singletonList("billy"), ImmutableMap.of("billy", randomInt % 3, "value", randomInt));
indexExpr.add(mapBasedInputRowExpr);
indexJs.add(mapBasedInputRowJs);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
addThreads[i].start();
}
for (int i = 0; i < addThreadCount; ++i) {
addThreads[i].join();
}
long exprSum = 0;
long jsSum = 0;
for (IncrementalIndexRow row : indexExpr.getFacts().keySet()) {
exprSum += indexExpr.getMetricLongValue(row.getRowIndex(), 0);
}
for (IncrementalIndexRow row : indexJs.getFacts().keySet()) {
jsSum += indexJs.getMetricLongValue(row.getRowIndex(), 0);
}
Assert.assertEquals(exprSum, jsSum);
}
Aggregations