Search in sources :

Example 41 with TimeseriesQuery

use of org.apache.druid.query.timeseries.TimeseriesQuery in project druid by druid-io.

the class SchemaEvolutionTest method testHyperUniqueEvolutionTimeseries.

@Test
@Parameters(method = "doVectorize")
public void testHyperUniqueEvolutionTimeseries(boolean doVectorize) {
    final TimeseriesQueryRunnerFactory factory = QueryRunnerTestHelper.newTimeseriesQueryRunnerFactory();
    final TimeseriesQuery query = Druids.newTimeseriesQueryBuilder().dataSource(DATA_SOURCE).intervals("1000/3000").aggregators(ImmutableList.of(new HyperUniquesAggregatorFactory("uniques", "uniques"))).context(ImmutableMap.of(QueryContexts.VECTORIZE_KEY, doVectorize)).build();
    // index1 has no "uniques" column
    Assert.assertEquals(timeseriesResult(ImmutableMap.of("uniques", 0d)), runQuery(query, factory, ImmutableList.of(index1)));
    // index1 (no uniques) + index2 and index3 (yes uniques); we should be able to combine
    Assert.assertEquals(timeseriesResult(ImmutableMap.of("uniques", 4.003911343725148d)), runQuery(query, factory, ImmutableList.of(index1, index2, index3)));
}
Also used : TimeseriesQueryRunnerFactory(org.apache.druid.query.timeseries.TimeseriesQueryRunnerFactory) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) HyperUniquesAggregatorFactory(org.apache.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 42 with TimeseriesQuery

use of org.apache.druid.query.timeseries.TimeseriesQuery in project druid by druid-io.

the class SchemaEvolutionTest method testNumericEvolutionFiltering.

@Test
@Parameters(method = "doVectorize")
public void testNumericEvolutionFiltering(boolean doVectorize) {
    final TimeseriesQueryRunnerFactory factory = QueryRunnerTestHelper.newTimeseriesQueryRunnerFactory();
    // "c1" changes from string(1) -> long(2) -> float(3) -> nonexistent(4)
    // test behavior of filtering
    final TimeseriesQuery query = Druids.newTimeseriesQueryBuilder().dataSource(DATA_SOURCE).intervals("1000/3000").filters(new BoundDimFilter("c1", "9", "11", false, false, null, null, StringComparators.NUMERIC)).aggregators(ImmutableList.of(new LongSumAggregatorFactory("a", "c1"), new DoubleSumAggregatorFactory("b", "c1"), new FloatSumAggregatorFactory("d", "c1"), new LongMinAggregatorFactory("e", "c1"), new CountAggregatorFactory("c"))).context(ImmutableMap.of(QueryContexts.VECTORIZE_KEY, doVectorize)).build();
    // Only string(1) -- which we can filter but not aggregate
    Assert.assertEquals(timeseriesResult(ImmutableMap.of("a", 19L, "b", 19.1, "c", 2L, "d", 19.1f, "e", 9L)), runQuery(query, factory, ImmutableList.of(index1)));
    // Only long(2) -- which we can filter and aggregate
    Assert.assertEquals(timeseriesResult(ImmutableMap.of("a", 19L, "b", 19.0, "c", 2L, "d", 19.0f, "e", 9L)), runQuery(query, factory, ImmutableList.of(index2)));
    // Only float(3) -- which we can't filter, but can aggregate
    Assert.assertEquals(timeseriesResult(ImmutableMap.of("a", 19L, "b", 19.1, "c", 2L, "d", 19.1f, "e", 9L)), runQuery(query, factory, ImmutableList.of(index3)));
    // Only nonexistent(4)
    Assert.assertEquals(timeseriesResult(TestHelper.createExpectedMap("a", NullHandling.defaultLongValue(), "b", NullHandling.defaultDoubleValue(), "c", 0L, "d", NullHandling.defaultFloatValue(), "e", NullHandling.sqlCompatible() ? null : Long.MAX_VALUE)), runQuery(query, factory, ImmutableList.of(index4)));
    // string(1) + long(2) + float(3) + nonexistent(4)
    Assert.assertEquals(timeseriesResult(ImmutableMap.of("a", 57L, "b", 57.2, "c", 6L, "d", 57.20000076293945, "e", 9L)), runQuery(query, factory, ImmutableList.of(index1, index2, index3, index4)));
}
Also used : TimeseriesQueryRunnerFactory(org.apache.druid.query.timeseries.TimeseriesQueryRunnerFactory) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) FloatSumAggregatorFactory(org.apache.druid.query.aggregation.FloatSumAggregatorFactory) LongMinAggregatorFactory(org.apache.druid.query.aggregation.LongMinAggregatorFactory) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 43 with TimeseriesQuery

use of org.apache.druid.query.timeseries.TimeseriesQuery in project druid by druid-io.

the class SchemaEvolutionTest method testNumericEvolutionTimeseriesAggregation.

@Test
@Parameters(method = "doVectorize")
public void testNumericEvolutionTimeseriesAggregation(boolean doVectorize) {
    final TimeseriesQueryRunnerFactory factory = QueryRunnerTestHelper.newTimeseriesQueryRunnerFactory();
    // "c1" changes from string(1) -> long(2) -> float(3) -> nonexistent(4)
    // test behavior of longSum/doubleSum with/without expressions
    final TimeseriesQuery query = Druids.newTimeseriesQueryBuilder().dataSource(DATA_SOURCE).intervals("1000/3000").aggregators(ImmutableList.of(new LongSumAggregatorFactory("a", "c1"), new DoubleSumAggregatorFactory("b", "c1"), new LongSumAggregatorFactory("c", null, "c1 * 1", TestExprMacroTable.INSTANCE), new DoubleSumAggregatorFactory("d", null, "c1 * 1", TestExprMacroTable.INSTANCE))).context(ImmutableMap.of(QueryContexts.VECTORIZE_KEY, doVectorize)).build();
    // Only string(1)
    // Note: Expressions implicitly cast strings to numbers, leading to the a/b vs c/d difference.
    Assert.assertEquals(timeseriesResult(ImmutableMap.of("a", 31L, "b", THIRTY_ONE_POINT_ONE, "c", 31L, "d", THIRTY_ONE_POINT_ONE)), runQuery(query, factory, ImmutableList.of(index1)));
    // Only long(2)
    Assert.assertEquals(timeseriesResult(ImmutableMap.of("a", 31L, "b", 31.0, "c", 31L, "d", 31.0)), runQuery(query, factory, ImmutableList.of(index2)));
    // Only float(3)
    Assert.assertEquals(timeseriesResult(ImmutableMap.of("a", 31L, "b", THIRTY_ONE_POINT_ONE, "c", 31L, "d", THIRTY_ONE_POINT_ONE)), runQuery(query, factory, ImmutableList.of(index3)));
    // Only nonexistent(4)
    Map<String, Object> result = new HashMap<>();
    result.put("a", NullHandling.defaultLongValue());
    result.put("b", NullHandling.defaultDoubleValue());
    result.put("c", NullHandling.defaultLongValue());
    result.put("d", NullHandling.defaultDoubleValue());
    Assert.assertEquals(timeseriesResult(result), runQuery(query, factory, ImmutableList.of(index4)));
    // string(1) + long(2) + float(3) + nonexistent(4)
    // Note: Expressions implicitly cast strings to numbers, leading to the a/b vs c/d difference.
    Assert.assertEquals(timeseriesResult(ImmutableMap.of("a", 31L * 3, "b", THIRTY_ONE_POINT_ONE * 2 + 31, "c", 31L * 3, "d", THIRTY_ONE_POINT_ONE * 2 + 31)), runQuery(query, factory, ImmutableList.of(index1, index2, index3, index4)));
    // long(2) + float(3) + nonexistent(4)
    Assert.assertEquals(timeseriesResult(ImmutableMap.of("a", 31L * 2, "b", THIRTY_ONE_POINT_ONE + 31, "c", 31L * 2, "d", THIRTY_ONE_POINT_ONE + 31)), runQuery(query, factory, ImmutableList.of(index2, index3, index4)));
}
Also used : TimeseriesQueryRunnerFactory(org.apache.druid.query.timeseries.TimeseriesQueryRunnerFactory) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) HashMap(java.util.HashMap) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 44 with TimeseriesQuery

use of org.apache.druid.query.timeseries.TimeseriesQuery in project druid by druid-io.

the class VarianceAggregatorFactoryTest method testResultArraySignature.

@Test
public void testResultArraySignature() {
    final TimeseriesQuery query = Druids.newTimeseriesQueryBuilder().dataSource("dummy").intervals("2000/3000").granularity(Granularities.HOUR).aggregators(new CountAggregatorFactory("count"), new VarianceAggregatorFactory("variance", "col"), new VarianceFoldingAggregatorFactory("varianceFold", "col", null)).postAggregators(new FieldAccessPostAggregator("variance-access", "variance"), new FinalizingFieldAccessPostAggregator("variance-finalize", "variance"), new FieldAccessPostAggregator("varianceFold-access", "varianceFold"), new FinalizingFieldAccessPostAggregator("varianceFold-finalize", "varianceFold")).build();
    Assert.assertEquals(RowSignature.builder().addTimeColumn().add("count", ColumnType.LONG).add("variance", null).add("varianceFold", null).add("variance-access", VarianceAggregatorFactory.TYPE).add("variance-finalize", ColumnType.DOUBLE).add("varianceFold-access", VarianceAggregatorFactory.TYPE).add("varianceFold-finalize", ColumnType.DOUBLE).build(), new TimeseriesQueryQueryToolChest().resultArraySignature(query));
}
Also used : FinalizingFieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FinalizingFieldAccessPostAggregator) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) FinalizingFieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FinalizingFieldAccessPostAggregator) TimeseriesQueryQueryToolChest(org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 45 with TimeseriesQuery

use of org.apache.druid.query.timeseries.TimeseriesQuery in project druid by druid-io.

the class AppenderatorDriverRealtimeIndexTaskTest method sumMetric.

@Nullable
public Long sumMetric(final Task task, final DimFilter filter, final String metric) {
    // Do a query.
    TimeseriesQuery query = Druids.newTimeseriesQueryBuilder().dataSource("test_ds").filters(filter).aggregators(ImmutableList.of(new LongSumAggregatorFactory(metric, metric))).granularity(Granularities.ALL).intervals("2000/3000").build();
    List<Result<TimeseriesResultValue>> results = task.getQueryRunner(query).run(QueryPlus.wrap(query)).toList();
    if (results.isEmpty()) {
        return 0L;
    } else {
        return results.get(0).getValue().getLongMetric(metric);
    }
}
Also used : TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) Result(org.apache.druid.query.Result) SegmentPublishResult(org.apache.druid.indexing.overlord.SegmentPublishResult) Nullable(javax.annotation.Nullable)

Aggregations

TimeseriesQuery (org.apache.druid.query.timeseries.TimeseriesQuery)130 Test (org.junit.Test)109 TimeseriesQueryQueryToolChest (org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest)58 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)50 Result (org.apache.druid.query.Result)39 TimeseriesResultValue (org.apache.druid.query.timeseries.TimeseriesResultValue)35 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)29 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)25 QueryRunner (org.apache.druid.query.QueryRunner)22 FinalizeResultsQueryRunner (org.apache.druid.query.FinalizeResultsQueryRunner)21 FieldAccessPostAggregator (org.apache.druid.query.aggregation.post.FieldAccessPostAggregator)17 Interval (org.joda.time.Interval)17 TimeseriesQueryEngine (org.apache.druid.query.timeseries.TimeseriesQueryEngine)15 TimeseriesQueryRunnerFactory (org.apache.druid.query.timeseries.TimeseriesQueryRunnerFactory)15 ArrayList (java.util.ArrayList)13 FinalizingFieldAccessPostAggregator (org.apache.druid.query.aggregation.post.FinalizingFieldAccessPostAggregator)13 DateTime (org.joda.time.DateTime)12 Sequence (org.apache.druid.java.util.common.guava.Sequence)11 IOException (java.io.IOException)10 SpatialDimFilter (org.apache.druid.query.filter.SpatialDimFilter)9