Search in sources :

Example 1 with PeriodGranularity

use of org.apache.druid.java.util.common.granularity.PeriodGranularity in project druid by druid-io.

the class QueryGranularityTest method testPeriodDaylightSaving.

@Test
public void testPeriodDaylightSaving() {
    final DateTimeZone tz = DateTimes.inferTzFromString("America/Los_Angeles");
    final DateTime baseTime = new DateTime("2012-11-04T00:00:00", tz);
    assertSameInterval(Lists.newArrayList(new DateTime("2012-11-04T00:00:00.000-07:00", tz), new DateTime("2012-11-05T00:00:00.000-08:00", tz), new DateTime("2012-11-06T00:00:00.000-08:00", tz)), new PeriodGranularity(new Period("P1D"), null, tz).getIterable(new Interval(baseTime, baseTime.plus(Days.days(3)))));
    assertSameInterval(Lists.newArrayList(new DateTime("2012-11-04T00:00:00.000-07:00", tz), new DateTime("2012-11-04T01:00:00.000-07:00", tz), new DateTime("2012-11-04T01:00:00.000-08:00", tz), new DateTime("2012-11-04T02:00:00.000-08:00", tz), new DateTime("2012-11-04T03:00:00.000-08:00", tz)), new PeriodGranularity(new Period("PT1H"), null, tz).getIterable(new Interval(baseTime, baseTime.plus(Hours.hours(5)))));
    final PeriodGranularity hour = new PeriodGranularity(new Period("PT1H"), null, tz);
    assertSameDateTime(Lists.newArrayList(new DateTime("2012-11-04T00:00:00.000-07:00", tz), new DateTime("2012-11-04T01:00:00.000-07:00", tz), new DateTime("2012-11-04T01:00:00.000-08:00", tz), new DateTime("2012-11-04T02:00:00.000-08:00", tz), new DateTime("2012-11-04T03:00:00.000-08:00", tz)), Lists.newArrayList(hour.bucketStart(DateTimes.of("2012-11-04T00:30:00-07:00")), hour.bucketStart(DateTimes.of("2012-11-04T01:30:00-07:00")), hour.bucketStart(DateTimes.of("2012-11-04T01:30:00-08:00")), hour.bucketStart(DateTimes.of("2012-11-04T02:30:00-08:00")), hour.bucketStart(DateTimes.of("2012-11-04T03:30:00-08:00"))));
}
Also used : PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) Period(org.joda.time.Period) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 2 with PeriodGranularity

use of org.apache.druid.java.util.common.granularity.PeriodGranularity in project druid by druid-io.

the class QueryGranularityTest method testIterableMonth.

@Test
public void testIterableMonth() {
    final DateTimeZone tz = DateTimes.inferTzFromString("America/Los_Angeles");
    final DateTime baseTime = new DateTime("2012-11-03T10:00:00", tz);
    assertSameInterval(Lists.newArrayList(new DateTime("2012-11-01T00:00:00.000-07:00", tz), new DateTime("2012-12-01T00:00:00.000-08:00", tz), new DateTime("2013-01-01T00:00:00.000-08:00", tz), new DateTime("2013-02-01T00:00:00.000-08:00", tz)), new PeriodGranularity(new Period("P1M"), null, tz).getIterable(new Interval(baseTime, baseTime.plus(Months.months(3)))));
}
Also used : PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) Period(org.joda.time.Period) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 3 with PeriodGranularity

use of org.apache.druid.java.util.common.granularity.PeriodGranularity in project druid by druid-io.

the class QueryGranularityTest method testSerializePeriod.

@Test
public void testSerializePeriod() throws Exception {
    final ObjectMapper mapper = new DefaultObjectMapper();
    String json = "{ \"type\": \"period\", \"period\": \"P1D\" }";
    Granularity gran = mapper.readValue(json, Granularity.class);
    Assert.assertEquals(new PeriodGranularity(new Period("P1D"), null, null), gran);
    // Nonstandard period
    json = "{ \"type\": \"period\", \"period\": \"P2D\" }";
    gran = mapper.readValue(json, Granularity.class);
    Assert.assertEquals(new PeriodGranularity(new Period("P2D"), null, null), gran);
    // Set timeZone, origin
    json = "{ \"type\": \"period\", \"period\": \"P1D\"," + "\"timeZone\": \"America/Los_Angeles\", \"origin\": \"1970-01-01T00:00:00Z\"}";
    gran = mapper.readValue(json, Granularity.class);
    Assert.assertEquals(new PeriodGranularity(new Period("P1D"), DateTimes.EPOCH, DateTimes.inferTzFromString("America/Los_Angeles")), gran);
    PeriodGranularity expected = new PeriodGranularity(new Period("P1D"), DateTimes.of("2012-01-01"), DateTimes.inferTzFromString("America/Los_Angeles"));
    String jsonOut = mapper.writeValueAsString(expected);
    Assert.assertEquals(expected, mapper.readValue(jsonOut, Granularity.class));
    String illegalJson = "{ \"type\": \"period\", \"period\": \"P0D\" }";
    try {
        mapper.readValue(illegalJson, Granularity.class);
        Assert.fail();
    } catch (JsonMappingException e) {
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) Period(org.joda.time.Period) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) Granularity(org.apache.druid.java.util.common.granularity.Granularity) PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) DurationGranularity(org.apache.druid.java.util.common.granularity.DurationGranularity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) Test(org.junit.Test)

Example 4 with PeriodGranularity

use of org.apache.druid.java.util.common.granularity.PeriodGranularity in project druid by druid-io.

the class QueryGranularityTest method testTruncateKathmandu.

@Test
public void testTruncateKathmandu() {
    final DateTimeZone tz = DateTimeZone.forTimeZone(TimeZone.getTimeZone("Asia/Kathmandu"));
    final DateTime date = new DateTime("2011-03-15T21:42:23.898+05:45", tz);
    final PeriodGranularity year = new PeriodGranularity(new Period("P1Y"), null, tz);
    final PeriodGranularity hour = new PeriodGranularity(new Period("PT1H"), null, tz);
    final PeriodGranularity twoHour = new PeriodGranularity(new Period("PT2H"), null, tz);
    assertBucketStart(year, date, new DateTime("2011-01-01T00:00:00.000+05:45", tz));
    assertBucketStart(hour, date, new DateTime("2011-03-15T21:00:00.000+05:45", tz));
    assertBucketStart(twoHour, date, new DateTime("2011-03-15T20:00:00.000+05:45", tz));
}
Also used : PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) Period(org.joda.time.Period) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 5 with PeriodGranularity

use of org.apache.druid.java.util.common.granularity.PeriodGranularity in project druid by druid-io.

the class QueryGranularityTest method testPeriodTruncateMinutes.

@Test
public void testPeriodTruncateMinutes() {
    final DateTime origin = DateTimes.of("2012-01-02T00:05:00.000Z");
    PeriodGranularity periodOrigin = new PeriodGranularity(new Period("PT15M"), origin, null);
    assertSameDateTime(Lists.newArrayList(DateTimes.of("2012-01-01T04:50:00.000Z"), DateTimes.of("2012-01-02T07:05:00.000Z"), DateTimes.of("2012-01-04T00:20:00.000Z")), Lists.newArrayList(periodOrigin.bucketStart(DateTimes.of("2012-01-01T05:00:04.123Z")), periodOrigin.bucketStart(DateTimes.of("2012-01-02T07:08:04.123Z")), periodOrigin.bucketStart(DateTimes.of("2012-01-04T00:20:04.123Z"))));
    PeriodGranularity periodNoOrigin = new PeriodGranularity(new Period("PT15M"), null, null);
    assertSameDateTime(Lists.newArrayList(DateTimes.of("2012-01-01T05:00:00.000Z"), DateTimes.of("2012-01-02T07:00:00.000Z"), DateTimes.of("2012-01-04T00:15:00.000Z")), Lists.newArrayList(periodNoOrigin.bucketStart(DateTimes.of("2012-01-01T05:00:04.123Z")), periodNoOrigin.bucketStart(DateTimes.of("2012-01-02T07:00:04.123Z")), periodNoOrigin.bucketStart(DateTimes.of("2012-01-04T00:20:04.123Z"))));
}
Also used : PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) Period(org.joda.time.Period) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

PeriodGranularity (org.apache.druid.java.util.common.granularity.PeriodGranularity)65 Test (org.junit.Test)57 Period (org.joda.time.Period)52 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)33 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)27 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)24 DateTime (org.joda.time.DateTime)13 QueryRunner (org.apache.druid.query.QueryRunner)11 Result (org.apache.druid.query.Result)11 ArrayList (java.util.ArrayList)10 FinalizeResultsQueryRunner (org.apache.druid.query.FinalizeResultsQueryRunner)10 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)10 ResponseContext (org.apache.druid.query.context.ResponseContext)9 DateTimeZone (org.joda.time.DateTimeZone)9 ChainedExecutionQueryRunner (org.apache.druid.query.ChainedExecutionQueryRunner)8 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)8 Sequence (org.apache.druid.java.util.common.guava.Sequence)7 QueryPlus (org.apache.druid.query.QueryPlus)7 Interval (org.joda.time.Interval)7 QueryDataSource (org.apache.druid.query.QueryDataSource)6