use of org.apache.druid.query.dimension.DefaultDimensionSpec in project druid by druid-io.
the class GroupByQueryRunnerTest method testGroupByWithFirstLast.
@Test
public void testGroupByWithFirstLast() {
// Cannot vectorize due to "first", "last" aggregators.
cannotVectorize();
GroupByQuery query = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FULL_ON_INTERVAL_SPEC).setDimensions(new DefaultDimensionSpec("market", "market")).setAggregatorSpecs(new LongFirstAggregatorFactory("first", "index", null), new LongLastAggregatorFactory("last", "index", null)).setGranularity(QueryRunnerTestHelper.MONTH_GRAN).build();
List<ResultRow> expectedResults = Arrays.asList(makeRow(query, "2011-01-01", "market", "spot", "first", 100L, "last", 155L), makeRow(query, "2011-01-01", "market", "total_market", "first", 1000L, "last", 1127L), makeRow(query, "2011-01-01", "market", "upfront", "first", 800L, "last", 943L), makeRow(query, "2011-02-01", "market", "spot", "first", 132L, "last", 114L), makeRow(query, "2011-02-01", "market", "total_market", "first", 1203L, "last", 1292L), makeRow(query, "2011-02-01", "market", "upfront", "first", 1667L, "last", 1101L), makeRow(query, "2011-03-01", "market", "spot", "first", 153L, "last", 125L), makeRow(query, "2011-03-01", "market", "total_market", "first", 1124L, "last", 1366L), makeRow(query, "2011-03-01", "market", "upfront", "first", 1166L, "last", 1063L), makeRow(query, "2011-04-01", "market", "spot", "first", 135L, "last", 120L), makeRow(query, "2011-04-01", "market", "total_market", "first", 1314L, "last", 1029L), makeRow(query, "2011-04-01", "market", "upfront", "first", 1447L, "last", 780L));
Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
TestHelper.assertExpectedObjects(expectedResults, results, "first-last-aggs");
}
use of org.apache.druid.query.dimension.DefaultDimensionSpec in project druid by druid-io.
the class GroupByQueryRunnerTest method testSubqueryWithOuterMaxOnDiskStorageContextOverride.
@Test
public void testSubqueryWithOuterMaxOnDiskStorageContextOverride() {
final GroupByQuery subquery = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FULL_ON_INTERVAL_SPEC).setDimensions(new DefaultDimensionSpec("quality", "alias")).setGranularity(QueryRunnerTestHelper.DAY_GRAN).setLimitSpec(new DefaultLimitSpec(ImmutableList.of(new OrderByColumnSpec("alias", OrderByColumnSpec.Direction.ASCENDING)), null)).overrideContext(ImmutableMap.of("maxOnDiskStorage", Integer.MAX_VALUE, "bufferGrouperMaxSize", Integer.MAX_VALUE)).build();
final GroupByQuery query = makeQueryBuilder().setDataSource(subquery).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new ArrayList<>()).setAggregatorSpecs(new CountAggregatorFactory("count")).setGranularity(QueryRunnerTestHelper.ALL_GRAN).overrideContext(ImmutableMap.of("maxOnDiskStorage", 0, "bufferGrouperMaxSize", 0)).build();
if (config.getDefaultStrategy().equals(GroupByStrategySelector.STRATEGY_V1)) {
expectedException.expect(ISE.class);
expectedException.expectMessage("Unknown column in order clause");
GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
} else {
expectedException.expect(ResourceLimitExceededException.class);
expectedException.expectMessage("Not enough aggregation buffer space to execute this query");
GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
}
}
use of org.apache.druid.query.dimension.DefaultDimensionSpec 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");
}
use of org.apache.druid.query.dimension.DefaultDimensionSpec 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.dimension.DefaultDimensionSpec in project druid by druid-io.
the class GroupByQueryRunnerTest method testPostAggMergedHavingSpec.
@Test
public void testPostAggMergedHavingSpec() {
GroupByQuery.Builder builder = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setInterval("2011-04-02/2011-04-04").setDimensions(new DefaultDimensionSpec("quality", "alias")).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("index", "index")).setPostAggregatorSpecs(ImmutableList.of(QueryRunnerTestHelper.ADD_ROWS_INDEX_CONSTANT)).setGranularity(new PeriodGranularity(new Period("P1M"), null, null)).setHavingSpec(new OrHavingSpec(ImmutableList.of(new GreaterThanHavingSpec(QueryRunnerTestHelper.ADD_ROWS_INDEX_CONSTANT_METRIC, 1000L))));
final GroupByQuery fullQuery = builder.build();
List<ResultRow> expectedResults = Arrays.asList(makeRow(fullQuery, "2011-04-01", "alias", "mezzanine", "rows", 6L, "index", 4420L, QueryRunnerTestHelper.ADD_ROWS_INDEX_CONSTANT_METRIC, (double) (6L + 4420L + 1L)), makeRow(fullQuery, "2011-04-01", "alias", "premium", "rows", 6L, "index", 4416L, QueryRunnerTestHelper.ADD_ROWS_INDEX_CONSTANT_METRIC, (double) (6L + 4416L + 1L)));
QueryRunner mergedRunner = factory.getToolchest().mergeResults(new QueryRunner<ResultRow>() {
@Override
public Sequence<ResultRow> run(QueryPlus<ResultRow> queryPlus, ResponseContext responseContext) {
// simulate two daily segments
final QueryPlus queryPlus1 = queryPlus.withQuery(queryPlus.getQuery().withQuerySegmentSpec(new MultipleIntervalSegmentSpec(Collections.singletonList(Intervals.of("2011-04-02/2011-04-03")))));
final QueryPlus queryPlus2 = queryPlus.withQuery(queryPlus.getQuery().withQuerySegmentSpec(new MultipleIntervalSegmentSpec(Collections.singletonList(Intervals.of("2011-04-03/2011-04-04")))));
return new MergeSequence(queryPlus.getQuery().getResultOrdering(), Sequences.simple(Arrays.asList(runner.run(queryPlus1, responseContext), runner.run(queryPlus2, responseContext))));
}
});
TestHelper.assertExpectedObjects(expectedResults, mergedRunner.run(QueryPlus.wrap(fullQuery)), "merged");
}
Aggregations