Search in sources :

Example 1 with HavingSpec

use of org.apache.druid.query.groupby.having.HavingSpec in project druid by druid-io.

the class GroupByQueryRunnerTest method testSubqueryWithPostAggregatorsAndHaving.

@Test
public void testSubqueryWithPostAggregatorsAndHaving() {
    final GroupByQuery subquery = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("quality", "alias")).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("idx_subagg", "index")).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 void setQuery(GroupByQuery query) {
            this.query = query;
        }

        @Override
        public byte[] getCacheKey() {
            return new byte[0];
        }

        @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")).setPostAggregatorSpecs(Collections.singletonList(new ArithmeticPostAggregator("idx_post", "+", Arrays.asList(new FieldAccessPostAggregator("the_idx_agg", "idx"), new ConstantPostAggregator("ten_thousand", 10000))))).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
    List<ResultRow> expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "alias", "automotive", "rows", 1L, "idx_post", 11135.0, "idx", 1135L), makeRow(query, "2011-04-01", "alias", "business", "rows", 1L, "idx_post", 11118.0, "idx", 1118L), makeRow(query, "2011-04-01", "alias", "entertainment", "rows", 1L, "idx_post", 11158.0, "idx", 1158L), makeRow(query, "2011-04-01", "alias", "health", "rows", 1L, "idx_post", 11120.0, "idx", 1120L), makeRow(query, "2011-04-01", "alias", "news", "rows", 1L, "idx_post", 11121.0, "idx", 1121L), makeRow(query, "2011-04-01", "alias", "technology", "rows", 1L, "idx_post", 11078.0, "idx", 1078L), makeRow(query, "2011-04-01", "alias", "travel", "rows", 1L, "idx_post", 11119.0, "idx", 1119L), makeRow(query, "2011-04-02", "alias", "automotive", "rows", 1L, "idx_post", 11147.0, "idx", 1147L), makeRow(query, "2011-04-02", "alias", "business", "rows", 1L, "idx_post", 11112.0, "idx", 1112L), makeRow(query, "2011-04-02", "alias", "entertainment", "rows", 1L, "idx_post", 11166.0, "idx", 1166L), makeRow(query, "2011-04-02", "alias", "health", "rows", 1L, "idx_post", 11113.0, "idx", 1113L), makeRow(query, "2011-04-02", "alias", "mezzanine", "rows", 3L, "idx_post", 13447.0, "idx", 3447L), makeRow(query, "2011-04-02", "alias", "news", "rows", 1L, "idx_post", 11114.0, "idx", 1114L), makeRow(query, "2011-04-02", "alias", "premium", "rows", 3L, "idx_post", 13505.0, "idx", 3505L), makeRow(query, "2011-04-02", "alias", "technology", "rows", 1L, "idx_post", 11097.0, "idx", 1097L), makeRow(query, "2011-04-02", "alias", "travel", "rows", 1L, "idx_post", 11126.0, "idx", 1126L));
    // Subqueries are handled by the ToolChest
    Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
    TestHelper.assertExpectedObjects(expectedResults, results, "subquery-postaggs");
}
Also used : ArithmeticPostAggregator(org.apache.druid.query.aggregation.post.ArithmeticPostAggregator) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) 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) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) ConstantPostAggregator(org.apache.druid.query.aggregation.post.ConstantPostAggregator) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 2 with HavingSpec

use of org.apache.druid.query.groupby.having.HavingSpec 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 3 with HavingSpec

use of org.apache.druid.query.groupby.having.HavingSpec in project druid by druid-io.

the class GroupByQueryQueryToolChestTest method testResultLevelCacheKeyWithAndHavingSpec.

@Test
public void testResultLevelCacheKeyWithAndHavingSpec() {
    final List<HavingSpec> havings = Arrays.asList(new GreaterThanHavingSpec("agg", Double.valueOf(1.3)), new OrHavingSpec(Arrays.asList(new LessThanHavingSpec("lessAgg", Long.valueOf(1L)), new NotHavingSpec(new EqualToHavingSpec("equalAgg", Double.valueOf(2))))));
    final HavingSpec andHavingSpec = new AndHavingSpec(havings);
    final List<HavingSpec> havings2 = Arrays.asList(new GreaterThanHavingSpec("agg", Double.valueOf(13.0)), new OrHavingSpec(Arrays.asList(new LessThanHavingSpec("lessAgg", Long.valueOf(1L)), new NotHavingSpec(new EqualToHavingSpec("equalAgg", Double.valueOf(22))))));
    final HavingSpec andHavingSpec2 = new AndHavingSpec(havings2);
    final GroupByQuery query1 = GroupByQuery.builder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("quality", "alias")).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("idx", "index")).setPostAggregatorSpecs(ImmutableList.of(new ExpressionPostAggregator("post", "alias + 'x'", null, TestExprMacroTable.INSTANCE))).setGranularity(QueryRunnerTestHelper.DAY_GRAN).setLimitSpec(new DefaultLimitSpec(ImmutableList.of(new OrderByColumnSpec("post", OrderByColumnSpec.Direction.DESCENDING)), Integer.MAX_VALUE)).setHavingSpec(andHavingSpec).build();
    final GroupByQuery query2 = GroupByQuery.builder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("quality", "alias")).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("idx", "index")).setPostAggregatorSpecs(ImmutableList.of(new ExpressionPostAggregator("post", "alias + 'x'", null, TestExprMacroTable.INSTANCE))).setGranularity(QueryRunnerTestHelper.DAY_GRAN).setLimitSpec(new DefaultLimitSpec(ImmutableList.of(new OrderByColumnSpec("post", OrderByColumnSpec.Direction.DESCENDING)), Integer.MAX_VALUE)).setHavingSpec(andHavingSpec2).build();
    final CacheStrategy<ResultRow, Object, GroupByQuery> strategy1 = new GroupByQueryQueryToolChest(null).getCacheStrategy(query1);
    final CacheStrategy<ResultRow, Object, GroupByQuery> strategy2 = new GroupByQueryQueryToolChest(null).getCacheStrategy(query2);
    Assert.assertTrue(Arrays.equals(strategy1.computeCacheKey(query1), strategy2.computeCacheKey(query2)));
    Assert.assertFalse(Arrays.equals(strategy1.computeResultLevelCacheKey(query1), strategy2.computeResultLevelCacheKey(query2)));
}
Also used : EqualToHavingSpec(org.apache.druid.query.groupby.having.EqualToHavingSpec) OrHavingSpec(org.apache.druid.query.groupby.having.OrHavingSpec) DefaultLimitSpec(org.apache.druid.query.groupby.orderby.DefaultLimitSpec) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) LessThanHavingSpec(org.apache.druid.query.groupby.having.LessThanHavingSpec) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) OrderByColumnSpec(org.apache.druid.query.groupby.orderby.OrderByColumnSpec) AndHavingSpec(org.apache.druid.query.groupby.having.AndHavingSpec) DimFilterHavingSpec(org.apache.druid.query.groupby.having.DimFilterHavingSpec) NotHavingSpec(org.apache.druid.query.groupby.having.NotHavingSpec) LessThanHavingSpec(org.apache.druid.query.groupby.having.LessThanHavingSpec) HavingSpec(org.apache.druid.query.groupby.having.HavingSpec) OrHavingSpec(org.apache.druid.query.groupby.having.OrHavingSpec) AndHavingSpec(org.apache.druid.query.groupby.having.AndHavingSpec) EqualToHavingSpec(org.apache.druid.query.groupby.having.EqualToHavingSpec) GreaterThanHavingSpec(org.apache.druid.query.groupby.having.GreaterThanHavingSpec) ExpressionPostAggregator(org.apache.druid.query.aggregation.post.ExpressionPostAggregator) GreaterThanHavingSpec(org.apache.druid.query.groupby.having.GreaterThanHavingSpec) NotHavingSpec(org.apache.druid.query.groupby.having.NotHavingSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Aggregations

LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)3 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)3 DimFilterHavingSpec (org.apache.druid.query.groupby.having.DimFilterHavingSpec)3 EqualToHavingSpec (org.apache.druid.query.groupby.having.EqualToHavingSpec)3 GreaterThanHavingSpec (org.apache.druid.query.groupby.having.GreaterThanHavingSpec)3 HavingSpec (org.apache.druid.query.groupby.having.HavingSpec)3 OrHavingSpec (org.apache.druid.query.groupby.having.OrHavingSpec)3 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)3 Test (org.junit.Test)3 ArithmeticPostAggregator (org.apache.druid.query.aggregation.post.ArithmeticPostAggregator)2 ConstantPostAggregator (org.apache.druid.query.aggregation.post.ConstantPostAggregator)2 FieldAccessPostAggregator (org.apache.druid.query.aggregation.post.FieldAccessPostAggregator)2 DimensionSelectorHavingSpec (org.apache.druid.query.groupby.having.DimensionSelectorHavingSpec)2 DefaultLimitSpec (org.apache.druid.query.groupby.orderby.DefaultLimitSpec)2 OrderByColumnSpec (org.apache.druid.query.groupby.orderby.OrderByColumnSpec)2 DoubleSumAggregatorFactory (org.apache.druid.query.aggregation.DoubleSumAggregatorFactory)1 JavaScriptAggregatorFactory (org.apache.druid.query.aggregation.JavaScriptAggregatorFactory)1 ExpressionPostAggregator (org.apache.druid.query.aggregation.post.ExpressionPostAggregator)1 JavaScriptDimFilter (org.apache.druid.query.filter.JavaScriptDimFilter)1 AndHavingSpec (org.apache.druid.query.groupby.having.AndHavingSpec)1