Search in sources :

Example 1 with JavaScriptAggregatorFactory

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");
}
Also used : DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) JavaScriptAggregatorFactory(org.apache.druid.query.aggregation.JavaScriptAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 2 with JavaScriptAggregatorFactory

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");
}
Also used : LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) JavaScriptAggregatorFactory(org.apache.druid.query.aggregation.JavaScriptAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 3 with JavaScriptAggregatorFactory

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");
}
Also used : ArithmeticPostAggregator(org.apache.druid.query.aggregation.post.ArithmeticPostAggregator) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) DefaultLimitSpec(org.apache.druid.query.groupby.orderby.DefaultLimitSpec) ConstantPostAggregator(org.apache.druid.query.aggregation.post.ConstantPostAggregator) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) JavaScriptAggregatorFactory(org.apache.druid.query.aggregation.JavaScriptAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) OrderByColumnSpec(org.apache.druid.query.groupby.orderby.OrderByColumnSpec) DimFilterHavingSpec(org.apache.druid.query.groupby.having.DimFilterHavingSpec) EqualToHavingSpec(org.apache.druid.query.groupby.having.EqualToHavingSpec) DimensionSelectorHavingSpec(org.apache.druid.query.groupby.having.DimensionSelectorHavingSpec) HavingSpec(org.apache.druid.query.groupby.having.HavingSpec) OrHavingSpec(org.apache.druid.query.groupby.having.OrHavingSpec) GreaterThanHavingSpec(org.apache.druid.query.groupby.having.GreaterThanHavingSpec) JavaScriptDimFilter(org.apache.druid.query.filter.JavaScriptDimFilter) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 4 with JavaScriptAggregatorFactory

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");
}
Also used : LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) JavaScriptAggregatorFactory(org.apache.druid.query.aggregation.JavaScriptAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 5 with JavaScriptAggregatorFactory

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);
}
Also used : LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) JavaScriptAggregatorFactory(org.apache.druid.query.aggregation.JavaScriptAggregatorFactory) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) MapBasedInputRow(org.apache.druid.data.input.MapBasedInputRow) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Aggregations

JavaScriptAggregatorFactory (org.apache.druid.query.aggregation.JavaScriptAggregatorFactory)7 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)6 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)6 Test (org.junit.Test)6 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)4 DoubleSumAggregatorFactory (org.apache.druid.query.aggregation.DoubleSumAggregatorFactory)3 MapBasedInputRow (org.apache.druid.data.input.MapBasedInputRow)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ByteBuffer (java.nio.ByteBuffer)1 Random (java.util.Random)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 CloseableStupidPool (org.apache.druid.collections.CloseableStupidPool)1 MapBasedRow (org.apache.druid.data.input.MapBasedRow)1 Row (org.apache.druid.data.input.Row)1 ArithmeticPostAggregator (org.apache.druid.query.aggregation.post.ArithmeticPostAggregator)1 ConstantPostAggregator (org.apache.druid.query.aggregation.post.ConstantPostAggregator)1 FieldAccessPostAggregator (org.apache.druid.query.aggregation.post.FieldAccessPostAggregator)1 JavaScriptDimFilter (org.apache.druid.query.filter.JavaScriptDimFilter)1 SelectorDimFilter (org.apache.druid.query.filter.SelectorDimFilter)1 GroupByQueryConfig (org.apache.druid.query.groupby.GroupByQueryConfig)1