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;
}
});
}
};
}
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()));
}
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);
}
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));
}
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));
}
Aggregations