Search in sources :

Example 1 with TimeBoundaryResultValue

use of io.druid.query.timeboundary.TimeBoundaryResultValue in project druid by druid-io.

the class TimewarpOperator method postProcess.

public QueryRunner<T> postProcess(final QueryRunner<T> baseRunner, final long now) {
    return new QueryRunner<T>() {

        @Override
        public Sequence<T> run(final Query<T> query, final Map<String, Object> responseContext) {
            final long offset = computeOffset(now);
            final Interval interval = query.getIntervals().get(0);
            final Interval modifiedInterval = new Interval(Math.min(interval.getStartMillis() + offset, now + offset), Math.min(interval.getEndMillis() + offset, now + offset));
            return Sequences.map(baseRunner.run(query.withQuerySegmentSpec(new MultipleIntervalSegmentSpec(Arrays.asList(modifiedInterval))), responseContext), new Function<T, T>() {

                @Override
                public T apply(T input) {
                    if (input instanceof Result) {
                        Result res = (Result) input;
                        Object value = res.getValue();
                        if (value instanceof TimeBoundaryResultValue) {
                            TimeBoundaryResultValue boundary = (TimeBoundaryResultValue) value;
                            DateTime minTime = null;
                            try {
                                minTime = boundary.getMinTime();
                            } catch (IllegalArgumentException e) {
                            }
                            final DateTime maxTime = boundary.getMaxTime();
                            return (T) ((TimeBoundaryQuery) query).buildResult(new DateTime(Math.min(res.getTimestamp().getMillis() - offset, now)), minTime != null ? minTime.minus(offset) : null, maxTime != null ? new DateTime(Math.min(maxTime.getMillis() - offset, now)) : null).iterator().next();
                        }
                        return (T) new Result(res.getTimestamp().minus(offset), value);
                    } else if (input instanceof MapBasedRow) {
                        MapBasedRow row = (MapBasedRow) input;
                        return (T) new MapBasedRow(row.getTimestamp().minus(offset), row.getEvent());
                    }
                    // default to noop for unknown result types
                    return input;
                }
            });
        }
    };
}
Also used : TimeBoundaryQuery(io.druid.query.timeboundary.TimeBoundaryQuery) MultipleIntervalSegmentSpec(io.druid.query.spec.MultipleIntervalSegmentSpec) TimeBoundaryQuery(io.druid.query.timeboundary.TimeBoundaryQuery) DateTime(org.joda.time.DateTime) MapBasedRow(io.druid.data.input.MapBasedRow) TimeBoundaryResultValue(io.druid.query.timeboundary.TimeBoundaryResultValue) Map(java.util.Map) Interval(org.joda.time.Interval)

Example 2 with TimeBoundaryResultValue

use of io.druid.query.timeboundary.TimeBoundaryResultValue in project druid by druid-io.

the class TimewarpOperatorTest method testPostProcess.

@Test
public void testPostProcess() throws Exception {
    QueryRunner<Result<TimeseriesResultValue>> queryRunner = testOperator.postProcess(new QueryRunner<Result<TimeseriesResultValue>>() {

        @Override
        public Sequence<Result<TimeseriesResultValue>> run(Query<Result<TimeseriesResultValue>> query, Map<String, Object> responseContext) {
            return Sequences.simple(ImmutableList.of(new Result<>(new DateTime(new DateTime("2014-01-09")), new TimeseriesResultValue(ImmutableMap.<String, Object>of("metric", 2))), new Result<>(new DateTime(new DateTime("2014-01-11")), new TimeseriesResultValue(ImmutableMap.<String, Object>of("metric", 3))), new Result<>(query.getIntervals().get(0).getEnd(), new TimeseriesResultValue(ImmutableMap.<String, Object>of("metric", 5)))));
        }
    }, new DateTime("2014-08-02").getMillis());
    final Query<Result<TimeseriesResultValue>> query = Druids.newTimeseriesQueryBuilder().dataSource("dummy").intervals("2014-07-31/2014-08-05").aggregators(Arrays.<AggregatorFactory>asList(new CountAggregatorFactory("count"))).build();
    Assert.assertEquals(Lists.newArrayList(new Result<>(new DateTime("2014-07-31"), new TimeseriesResultValue(ImmutableMap.<String, Object>of("metric", 2))), new Result<>(new DateTime("2014-08-02"), new TimeseriesResultValue(ImmutableMap.<String, Object>of("metric", 3))), new Result<>(new DateTime("2014-08-02"), new TimeseriesResultValue(ImmutableMap.<String, Object>of("metric", 5)))), Sequences.toList(queryRunner.run(query, CONTEXT), Lists.<Result<TimeseriesResultValue>>newArrayList()));
    TimewarpOperator<Result<TimeBoundaryResultValue>> timeBoundaryOperator = new TimewarpOperator<>(new Interval(new DateTime("2014-01-01"), new DateTime("2014-01-15")), new Period("P1W"), // align on Monday
    new DateTime("2014-01-06"));
    QueryRunner<Result<TimeBoundaryResultValue>> timeBoundaryRunner = timeBoundaryOperator.postProcess(new QueryRunner<Result<TimeBoundaryResultValue>>() {

        @Override
        public Sequence<Result<TimeBoundaryResultValue>> run(Query<Result<TimeBoundaryResultValue>> query, Map<String, Object> responseContext) {
            return Sequences.simple(ImmutableList.of(new Result<>(new DateTime("2014-01-12"), new TimeBoundaryResultValue(ImmutableMap.<String, Object>of("maxTime", new DateTime("2014-01-12"))))));
        }
    }, new DateTime("2014-08-02").getMillis());
    final Query<Result<TimeBoundaryResultValue>> timeBoundaryQuery = Druids.newTimeBoundaryQueryBuilder().dataSource("dummy").build();
    Assert.assertEquals(Lists.newArrayList(new Result<>(new DateTime("2014-08-02"), new TimeBoundaryResultValue(ImmutableMap.<String, Object>of("maxTime", new DateTime("2014-08-02"))))), Sequences.toList(timeBoundaryRunner.run(timeBoundaryQuery, CONTEXT), Lists.<Result<TimeBoundaryResultValue>>newArrayList()));
}
Also used : TimeseriesResultValue(io.druid.query.timeseries.TimeseriesResultValue) Period(org.joda.time.Period) Sequence(io.druid.java.util.common.guava.Sequence) AggregatorFactory(io.druid.query.aggregation.AggregatorFactory) CountAggregatorFactory(io.druid.query.aggregation.CountAggregatorFactory) DateTime(org.joda.time.DateTime) CountAggregatorFactory(io.druid.query.aggregation.CountAggregatorFactory) TimeBoundaryResultValue(io.druid.query.timeboundary.TimeBoundaryResultValue) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 3 with TimeBoundaryResultValue

use of io.druid.query.timeboundary.TimeBoundaryResultValue in project druid by druid-io.

the class SchemalessTestFullTest method testTimeBoundary.

private void testTimeBoundary(QueryRunner runner, List<Result<TimeBoundaryResultValue>> expectedResults, String failMsg) {
    TimeBoundaryQuery query = Druids.newTimeBoundaryQueryBuilder().dataSource("testing").build();
    failMsg += " timeBoundary ";
    HashMap<String, Object> context = new HashMap<>();
    Iterable<Result<TimeBoundaryResultValue>> actualResults = Sequences.toList(runner.run(query, context), Lists.<Result<TimeBoundaryResultValue>>newArrayList());
    TestHelper.assertExpectedResults(expectedResults, actualResults, failMsg);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TimeBoundaryResultValue(io.druid.query.timeboundary.TimeBoundaryResultValue) TimeBoundaryQuery(io.druid.query.timeboundary.TimeBoundaryQuery) Result(io.druid.query.Result)

Example 4 with TimeBoundaryResultValue

use of io.druid.query.timeboundary.TimeBoundaryResultValue in project druid by druid-io.

the class SchemalessTestSimpleTest method testTimeBoundary.

@Test
public void testTimeBoundary() {
    TimeBoundaryQuery query = Druids.newTimeBoundaryQueryBuilder().dataSource("testing").build();
    List<Result<TimeBoundaryResultValue>> expectedResults = Arrays.asList(new Result<TimeBoundaryResultValue>(new DateTime("2011-01-12T00:00:00.000Z"), new TimeBoundaryResultValue(ImmutableMap.of(TimeBoundaryQuery.MIN_TIME, new DateTime("2011-01-12T00:00:00.000Z"), TimeBoundaryQuery.MAX_TIME, new DateTime("2011-01-13T00:00:00.000Z")))));
    QueryRunner runner = TestQueryRunners.makeTimeBoundaryQueryRunner(segment);
    HashMap<String, Object> context = new HashMap<String, Object>();
    TestHelper.assertExpectedResults(expectedResults, runner.run(query, context));
}
Also used : HashMap(java.util.HashMap) TimeBoundaryResultValue(io.druid.query.timeboundary.TimeBoundaryResultValue) TimeBoundaryQuery(io.druid.query.timeboundary.TimeBoundaryQuery) DateTime(org.joda.time.DateTime) QueryRunner(io.druid.query.QueryRunner) Result(io.druid.query.Result) Test(org.junit.Test)

Example 5 with TimeBoundaryResultValue

use of io.druid.query.timeboundary.TimeBoundaryResultValue in project druid by druid-io.

the class AppendTest method testTimeBoundary2.

@Test
public void testTimeBoundary2() {
    List<Result<TimeBoundaryResultValue>> expectedResults = Arrays.asList(new Result<TimeBoundaryResultValue>(new DateTime("2011-01-12T00:00:00.000Z"), new TimeBoundaryResultValue(ImmutableMap.of(TimeBoundaryQuery.MIN_TIME, new DateTime("2011-01-12T00:00:00.000Z"), TimeBoundaryQuery.MAX_TIME, new DateTime("2011-01-15T00:00:00.000Z")))));
    TimeBoundaryQuery query = Druids.newTimeBoundaryQueryBuilder().dataSource(dataSource).build();
    QueryRunner runner = TestQueryRunners.makeTimeBoundaryQueryRunner(segment2);
    HashMap<String, Object> context = new HashMap<String, Object>();
    TestHelper.assertExpectedResults(expectedResults, runner.run(query, context));
}
Also used : HashMap(java.util.HashMap) TimeBoundaryResultValue(io.druid.query.timeboundary.TimeBoundaryResultValue) TimeBoundaryQuery(io.druid.query.timeboundary.TimeBoundaryQuery) DateTime(org.joda.time.DateTime) QueryRunner(io.druid.query.QueryRunner) Result(io.druid.query.Result) Test(org.junit.Test)

Aggregations

TimeBoundaryResultValue (io.druid.query.timeboundary.TimeBoundaryResultValue)6 TimeBoundaryQuery (io.druid.query.timeboundary.TimeBoundaryQuery)5 DateTime (org.joda.time.DateTime)5 Result (io.druid.query.Result)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 QueryRunner (io.druid.query.QueryRunner)3 Interval (org.joda.time.Interval)2 MapBasedRow (io.druid.data.input.MapBasedRow)1 Sequence (io.druid.java.util.common.guava.Sequence)1 AggregatorFactory (io.druid.query.aggregation.AggregatorFactory)1 CountAggregatorFactory (io.druid.query.aggregation.CountAggregatorFactory)1 MultipleIntervalSegmentSpec (io.druid.query.spec.MultipleIntervalSegmentSpec)1 TimeseriesResultValue (io.druid.query.timeseries.TimeseriesResultValue)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Period (org.joda.time.Period)1