@Override public DateTimeZone deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { String tzId = jp.getText(); return DateTimes.inferTzFromString(tzId); } }
@Override public DateTimeZone getSqlTimeZone() { return DateTimes.inferTzFromString("America/Los_Angeles"); } };
public static long T(final String timeString, final String timeZoneString) { final DateTimeZone timeZone = DateTimes.inferTzFromString(timeZoneString); return Calcites.jodaToCalciteTimestamp(new DateTime(timeString, timeZone), timeZone); }
public static DateTimeZone toTimeZone(final Expr timeZoneArg) { if (!timeZoneArg.isLiteral()) { throw new IAE("Time zone must be a literal"); } final Object literalValue = timeZoneArg.getLiteralValue(); return literalValue == null ? DateTimeZone.UTC : DateTimes.inferTzFromString((String) literalValue); }
timeZone = DateTimes.inferTzFromString(String.valueOf(tzParam)); } else { timeZone = plannerConfig.getSqlTimeZone();
@Test public void testCommonDateTimePattern() { DateTime dt1 = DateTimes.nowUtc(); DateTime dt2 = new DateTime(System.currentTimeMillis(), DateTimes.inferTzFromString("IST")); DateTime dt3 = new DateTime(System.currentTimeMillis(), DateTimeZone.forOffsetHoursMinutes(1, 30)); for (DateTime dt : new DateTime[] {dt1, dt2, dt3}) { Assert.assertTrue(DateTimes.COMMON_DATE_TIME_PATTERN.matcher(dt.toString()).matches()); } } }
: "yyyy-MM-dd'T'HH:mm:ss.SSSZZ"; final DateTimeZone timeZone = call.getOperands().size() > 2 && !RexLiteral.isNullLiteral(call.getOperands().get(2)) ? DateTimes.inferTzFromString(RexLiteral.stringValue(call.getOperands().get(2))) : plannerContext.getTimeZone();
@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)))) ); }
@Override public DruidExpression toDruidExpression( final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode ) { final RexCall call = (RexCall) rexNode; final RexNode timeArg = call.getOperands().get(0); final DruidExpression timeExpression = Expressions.toDruidExpression(plannerContext, rowSignature, timeArg); if (timeExpression == null) { return null; } final TimestampExtractExprMacro.Unit unit = TimestampExtractExprMacro.Unit.valueOf( StringUtils.toUpperCase(RexLiteral.stringValue(call.getOperands().get(1))) ); final DateTimeZone timeZone = call.getOperands().size() > 2 && !RexLiteral.isNullLiteral(call.getOperands().get(2)) ? DateTimes.inferTzFromString(RexLiteral.stringValue(call.getOperands().get(2))) : plannerContext.getTimeZone(); return applyTimeExtract(timeExpression, unit, timeZone); } }
} else { final String value = timeZoneArg.eval(bindings).asString(); timeZone = value != null ? DateTimes.inferTzFromString(value) : null;
@Test public void testTimestampsInResponseLosAngelesTimeZone() throws Exception { final ResultSet resultSet = clientLosAngeles.createStatement().executeQuery( "SELECT __time, CAST(__time AS DATE) AS t2 FROM druid.foo LIMIT 1" ); final DateTimeZone timeZone = DateTimes.inferTzFromString("America/Los_Angeles"); final DateTime localDateTime = new DateTime("2000-01-01T00Z", timeZone); final List<Map<String, Object>> resultRows = getRows(resultSet); Assert.assertEquals( ImmutableList.of( ImmutableMap.of( "__time", new Timestamp(Calcites.jodaToCalciteTimestamp(localDateTime, timeZone)), "t2", new Date(Calcites.jodaToCalciteTimestamp(localDateTime.dayOfMonth().roundFloorCopy(), timeZone)) ) ), resultRows ); }
@Test public void testIterableWeek() { 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-10-29T00:00:00.000-07:00", tz), new DateTime("2012-11-05T00:00:00.000-08:00", tz), new DateTime("2012-11-12T00:00:00.000-08:00", tz), new DateTime("2012-11-19T00:00:00.000-08:00", tz) ), new PeriodGranularity(new Period("P1W"), null, tz) .getIterable(new Interval(baseTime, baseTime.plus(Weeks.weeks(3)))) ); assertSameInterval( Lists.newArrayList( new DateTime("2012-11-03T10:00:00.000-07:00", tz), new DateTime("2012-11-10T10:00:00.000-08:00", tz), new DateTime("2012-11-17T10:00:00.000-08:00", tz) ), new PeriodGranularity(new Period("P1W"), baseTime, tz) .getIterable(new Interval(baseTime, baseTime.plus(Weeks.weeks(3)))) ); }
@Test public void testTimeFilterWithTimeFormatExtractionFn() { ExtractionFn exfn = new TimeFormatExtractionFn( "EEEE", DateTimes.inferTzFromString("America/New_York"), "en", null, false ); assertFilterMatches( new SelectorDimFilter(ColumnHolder.TIME_COLUMN_NAME, "Wednesday", exfn), ImmutableList.of("0", "1", "2", "3", "4", "5") ); }
@Test public void testComputeOffset() { { final DateTime t = DateTimes.of("2014-01-23"); final DateTime tOffset = DateTimes.of("2014-01-09"); Assert.assertEquals(tOffset, t.plus(testOperator.computeOffset(t.getMillis(), DateTimeZone.UTC))); } { final DateTime t = DateTimes.of("2014-08-02"); final DateTime tOffset = DateTimes.of("2014-01-11"); Assert.assertEquals(tOffset, t.plus(testOperator.computeOffset(t.getMillis(), DateTimeZone.UTC))); } { final DateTime t = DateTimes.of("2014-08-02T-07"); final DateTime tOffset = DateTimes.of("2014-01-11T-08"); Assert.assertEquals( tOffset, t.plus(testOperator.computeOffset(t.getMillis(), DateTimes.inferTzFromString("America/Los_Angeles"))) ); } }
new Period("P1D"), null, DateTimes.inferTzFromString("America/Los_Angeles") new DateTime("2011-03-31", DateTimes.inferTzFromString("America/Los_Angeles")), new TimeseriesResultValue( ImmutableMap.of("rows", 13L, "idx", 6619L) new DateTime("2011-04-01T", DateTimes.inferTzFromString("America/Los_Angeles")), new TimeseriesResultValue( ImmutableMap.of("rows", 13L, "idx", 5827L)
@Test public void testTimeZoneExtraction() throws Exception { TimeFormatExtractionFn fn = new TimeFormatExtractionFn( "'In Berlin ist es schon 'EEEE", DateTimes.inferTzFromString("Europe/Berlin"), "de", null, false ); Assert.assertEquals("In Berlin ist es schon Freitag", fn.apply(timestamps[0])); Assert.assertEquals("In Berlin ist es schon Samstag", fn.apply(timestamps[1])); Assert.assertEquals("In Berlin ist es schon Mittwoch", fn.apply(timestamps[2])); Assert.assertEquals("In Berlin ist es schon Donnerstag", fn.apply(timestamps[3])); Assert.assertEquals("In Berlin ist es schon Sonntag", fn.apply(timestamps[4])); Assert.assertEquals("In Berlin ist es schon Dienstag", fn.apply(timestamps[5])); testSerde(fn, "'In Berlin ist es schon 'EEEE", DateTimes.inferTzFromString("Europe/Berlin"), "de", Granularities.NONE); }
@Test public void testTimeseriesLosAngelesUsingTimeFloorConnectionUtc() throws Exception { testQuery( "SELECT SUM(cnt), gran FROM (\n" + " SELECT TIME_FLOOR(__time, 'P1M', CAST(NULL AS TIMESTAMP), 'America/Los_Angeles') AS gran,\n" + " cnt FROM druid.foo\n" + ") AS x\n" + "GROUP BY gran\n" + "ORDER BY gran", ImmutableList.of( Druids.newTimeseriesQueryBuilder() .dataSource(CalciteTests.DATASOURCE1) .intervals(QSS(Filtration.eternity())) .granularity(new PeriodGranularity(Period.months(1), null, DateTimes.inferTzFromString(LOS_ANGELES))) .aggregators(AGGS(new LongSumAggregatorFactory("a0", "cnt"))) .context(TIMESERIES_CONTEXT_DEFAULT) .build() ), ImmutableList.of( new Object[]{1L, T("1999-12-01T08")}, new Object[]{2L, T("2000-01-01T08")}, new Object[]{1L, T("2000-12-01T08")}, new Object[]{2L, T("2001-01-01T08")} ) ); }
@Test public void testTimeseriesLosAngelesViaPlannerConfig() throws Exception { testQuery( PLANNER_CONFIG_LOS_ANGELES, QUERY_CONTEXT_DEFAULT, "SELECT SUM(cnt), gran FROM (\n" + " SELECT FLOOR(__time TO MONTH) AS gran,\n" + " cnt FROM druid.foo\n" + ") AS x\n" + "GROUP BY gran\n" + "ORDER BY gran", CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of( Druids.newTimeseriesQueryBuilder() .dataSource(CalciteTests.DATASOURCE1) .intervals(QSS(Filtration.eternity())) .granularity(new PeriodGranularity(Period.months(1), null, DateTimes.inferTzFromString(LOS_ANGELES))) .aggregators(AGGS(new LongSumAggregatorFactory("a0", "cnt"))) .context(TIMESERIES_CONTEXT_DEFAULT) .build() ), ImmutableList.of( new Object[]{1L, T("1999-12-01", LOS_ANGELES)}, new Object[]{2L, T("2000-01-01", LOS_ANGELES)}, new Object[]{1L, T("2000-12-01", LOS_ANGELES)}, new Object[]{2L, T("2001-01-01", LOS_ANGELES)} ) ); }
@Test public void testTimeseriesLosAngelesViaQueryContext() throws Exception { testQuery( PLANNER_CONFIG_DEFAULT, QUERY_CONTEXT_LOS_ANGELES, "SELECT SUM(cnt), gran FROM (\n" + " SELECT FLOOR(__time TO MONTH) AS gran,\n" + " cnt FROM druid.foo\n" + ") AS x\n" + "GROUP BY gran\n" + "ORDER BY gran", CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of( Druids.newTimeseriesQueryBuilder() .dataSource(CalciteTests.DATASOURCE1) .intervals(QSS(Filtration.eternity())) .granularity(new PeriodGranularity(Period.months(1), null, DateTimes.inferTzFromString(LOS_ANGELES))) .aggregators(AGGS(new LongSumAggregatorFactory("a0", "cnt"))) .context(TIMESERIES_CONTEXT_LOS_ANGELES) .build() ), ImmutableList.of( new Object[]{1L, T("1999-12-01", LOS_ANGELES)}, new Object[]{2L, T("2000-01-01", LOS_ANGELES)}, new Object[]{1L, T("2000-12-01", LOS_ANGELES)}, new Object[]{2L, T("2001-01-01", LOS_ANGELES)} ) ); }
@Test public void testTimeseriesLosAngelesUsingTimeFloorConnectionLosAngeles() throws Exception { testQuery( PLANNER_CONFIG_DEFAULT, QUERY_CONTEXT_LOS_ANGELES, "SELECT SUM(cnt), gran FROM (\n" + " SELECT TIME_FLOOR(__time, 'P1M') AS gran,\n" + " cnt FROM druid.foo\n" + ") AS x\n" + "GROUP BY gran\n" + "ORDER BY gran", CalciteTests.REGULAR_USER_AUTH_RESULT, ImmutableList.of( Druids.newTimeseriesQueryBuilder() .dataSource(CalciteTests.DATASOURCE1) .intervals(QSS(Filtration.eternity())) .granularity(new PeriodGranularity(Period.months(1), null, DateTimes.inferTzFromString(LOS_ANGELES))) .aggregators(AGGS(new LongSumAggregatorFactory("a0", "cnt"))) .context(TIMESERIES_CONTEXT_LOS_ANGELES) .build() ), ImmutableList.of( new Object[]{1L, T("1999-12-01", LOS_ANGELES)}, new Object[]{2L, T("2000-01-01", LOS_ANGELES)}, new Object[]{1L, T("2000-12-01", LOS_ANGELES)}, new Object[]{2L, T("2001-01-01", LOS_ANGELES)} ) ); }