use of org.apache.druid.query.groupby.orderby.DefaultLimitSpec in project druid by druid-io.
the class VarianceGroupByQueryTest method testGroupByZtestPostAgg.
@Test
public void testGroupByZtestPostAgg() {
// test postaggs from 'teststats' package in here since we've already gone to the trouble of setting up the test
GroupByQuery query = queryBuilder.setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("quality", "alias")).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, VarianceTestHelper.INDEX_VARIANCE_AGGR, new LongSumAggregatorFactory("idx", "index")).setPostAggregatorSpecs(ImmutableList.of(VarianceTestHelper.STD_DEV_OF_INDEX_POST_AGGR, // these inputs are totally nonsensical, i just want the code path to be executed
new ZtestPostAggregator("ztest", new FieldAccessPostAggregator("f1", "idx"), new ConstantPostAggregator("f2", 100000L), new FieldAccessPostAggregator("f3", "index_stddev"), new ConstantPostAggregator("f2", 100000L)))).setLimitSpec(new DefaultLimitSpec(OrderByColumnSpec.descending("ztest"), 1)).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
VarianceTestHelper.RowBuilder builder = new VarianceTestHelper.RowBuilder(new String[] { "alias", "rows", "idx", "index_stddev", "index_var", "ztest" });
List<ResultRow> expectedResults = builder.add("2011-04-01", "premium", 3L, 2900.0, 726.632270328514, 527994.4562827706, 36.54266309285626).build(query);
Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
TestHelper.assertExpectedObjects(expectedResults, results, "groupBy");
}
use of org.apache.druid.query.groupby.orderby.DefaultLimitSpec in project druid by druid-io.
the class VarianceGroupByQueryTest method testPostAggHavingSpec.
@Test
public void testPostAggHavingSpec() {
VarianceTestHelper.RowBuilder expect = new VarianceTestHelper.RowBuilder(new String[] { "alias", "rows", "index", "index_var", "index_stddev" });
GroupByQuery query = queryBuilder.setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setInterval("2011-04-02/2011-04-04").setDimensions(new DefaultDimensionSpec("quality", "alias")).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, QueryRunnerTestHelper.INDEX_LONG_SUM, VarianceTestHelper.INDEX_VARIANCE_AGGR).setPostAggregatorSpecs(ImmutableList.of(VarianceTestHelper.STD_DEV_OF_INDEX_POST_AGGR)).setGranularity(new PeriodGranularity(new Period("P1M"), null, null)).setHavingSpec(new OrHavingSpec(ImmutableList.of(// 3 rows
new GreaterThanHavingSpec(VarianceTestHelper.STD_DEV_OF_INDEX_METRIC, 15L)))).build();
List<ResultRow> expectedResults = expect.add("2011-04-01", "automotive", 2L, 269L, 299.0009819048282, 17.29164485827847).add("2011-04-01", "mezzanine", 6L, 4420L, 254083.76447001836, 504.06722217380724).add("2011-04-01", "premium", 6L, 4416L, 252279.2020389339, 502.27403082275106).build(query);
Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
TestHelper.assertExpectedObjects(expectedResults, results, "havingSpec");
query = query.withLimitSpec(new DefaultLimitSpec(Collections.singletonList(OrderByColumnSpec.asc(VarianceTestHelper.STD_DEV_OF_INDEX_METRIC)), 2));
expectedResults = expect.add("2011-04-01", "automotive", 2L, 269L, 299.0009819048282, 17.29164485827847).add("2011-04-01", "premium", 6L, 4416L, 252279.2020389339, 502.27403082275106).build(query);
results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
TestHelper.assertExpectedObjects(expectedResults, results, "limitSpec");
}
use of org.apache.druid.query.groupby.orderby.DefaultLimitSpec in project druid by druid-io.
the class GroupByQuery method canDoLimitPushDown.
private boolean canDoLimitPushDown(@Nullable LimitSpec limitSpec, @Nullable HavingSpec havingSpec, @Nullable List<List<String>> subtotalsSpec) {
if (subtotalsSpec != null && !subtotalsSpec.isEmpty()) {
return false;
}
if (limitSpec instanceof DefaultLimitSpec) {
DefaultLimitSpec limitSpecWithoutOffset = ((DefaultLimitSpec) limitSpec).withOffsetToLimit();
// If only applying an orderby without a limit, don't try to push down
if (!limitSpecWithoutOffset.isLimited()) {
return false;
}
if (!getApplyLimitPushDownFromContext()) {
return false;
}
if (havingSpec != null) {
return false;
}
// If the sorting order only uses columns in the grouping key, we can always push the limit down
// to the buffer grouper without affecting result accuracy
boolean sortHasNonGroupingFields = DefaultLimitSpec.sortingOrderHasNonGroupingFields((DefaultLimitSpec) limitSpec, getDimensions());
return !sortHasNonGroupingFields;
}
return false;
}
use of org.apache.druid.query.groupby.orderby.DefaultLimitSpec in project druid by druid-io.
the class ApproximateHistogramGroupByQueryTest method testGroupByWithSameNameComplexPostAgg.
@Test(expected = IllegalArgumentException.class)
public void testGroupByWithSameNameComplexPostAgg() {
ApproximateHistogramAggregatorFactory aggFactory = new ApproximateHistogramAggregatorFactory("quantile", "index", 10, 5, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, false);
GroupByQuery query = new GroupByQuery.Builder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setGranularity(QueryRunnerTestHelper.ALL_GRAN).setDimensions(new DefaultDimensionSpec(QueryRunnerTestHelper.MARKET_DIMENSION, "marketalias")).setInterval(QueryRunnerTestHelper.FULL_ON_INTERVAL_SPEC).setLimitSpec(new DefaultLimitSpec(Collections.singletonList(new OrderByColumnSpec("marketalias", OrderByColumnSpec.Direction.DESCENDING)), 1)).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, aggFactory).setPostAggregatorSpecs(Collections.singletonList(new QuantilePostAggregator("quantile", "quantile", 0.5f))).build();
List<ResultRow> expectedResults = Collections.singletonList(GroupByQueryRunnerTestHelper.createExpectedRow(query, "1970-01-01T00:00:00.000Z", "marketalias", "upfront", "rows", 186L, "quantile", 880.9881f));
Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
TestHelper.assertExpectedObjects(expectedResults, results, "approx-histo");
}
use of org.apache.druid.query.groupby.orderby.DefaultLimitSpec in project druid by druid-io.
the class ApproximateHistogramGroupByQueryTest method testGroupByWithApproximateHistogramAgg.
@Test
public void testGroupByWithApproximateHistogramAgg() {
ApproximateHistogramAggregatorFactory aggFactory = new ApproximateHistogramAggregatorFactory("apphisto", "index", 10, 5, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, false);
GroupByQuery query = new GroupByQuery.Builder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setGranularity(QueryRunnerTestHelper.ALL_GRAN).setDimensions(new DefaultDimensionSpec(QueryRunnerTestHelper.MARKET_DIMENSION, "marketalias")).setInterval(QueryRunnerTestHelper.FULL_ON_INTERVAL_SPEC).setLimitSpec(new DefaultLimitSpec(Collections.singletonList(new OrderByColumnSpec("marketalias", OrderByColumnSpec.Direction.DESCENDING)), 1)).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, aggFactory).setPostAggregatorSpecs(Collections.singletonList(new QuantilePostAggregator("quantile", "apphisto", 0.5f))).build();
List<ResultRow> expectedResults = Collections.singletonList(GroupByQueryRunnerTestHelper.createExpectedRow(query, "1970-01-01T00:00:00.000Z", "marketalias", "upfront", "rows", 186L, "quantile", 880.9881f, "apphisto", new Histogram(new float[] { 214.97299194335938f, 545.9906005859375f, 877.0081787109375f, 1208.0257568359375f, 1539.0433349609375f, 1870.06103515625f }, new double[] { 0.0, 67.53287506103516, 72.22068786621094, 31.984678268432617, 14.261756896972656 })));
Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
TestHelper.assertExpectedObjects(expectedResults, results, "approx-histo");
}
Aggregations