From 3ca4883f916f46b6afb2b97e1201071c0e6647f1 Mon Sep 17 00:00:00 2001 From: jimczi Date: Mon, 30 Mar 2020 11:36:25 +0200 Subject: [PATCH] Fixed rewrite of time zone without DST We try to rewrite time zones to fixed offsets in the date histogram aggregation if the data in the shard is within a single transition. However this optimization is not applied on time zones that don't apply daylight saving changes but had some random transitions in the past (e.g. Australia/Brisbane or Asia/Katmandu). This changes fixes the rewrite of such time zones to fixed offsets. --- .../histogram/DateHistogramAggregationBuilder.java | 4 ++-- .../bucket/histogram/DateHistogramTests.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregationBuilder.java index 5df239093c5ae..f767840d8a3da 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregationBuilder.java @@ -467,7 +467,7 @@ ZoneId rewriteTimeZone(QueryShardContext context) throws IOException { ZoneOffsetTransition prevOffsetTransition = tz.getRules().previousTransition(instant); final long prevTransition; - if (prevOffsetTransition != null) { + if (prevOffsetTransition != null) { prevTransition = prevOffsetTransition.getInstant().toEpochMilli(); } else { prevTransition = instant.toEpochMilli(); @@ -477,7 +477,7 @@ ZoneId rewriteTimeZone(QueryShardContext context) throws IOException { if (nextOffsetTransition != null) { nextTransition = nextOffsetTransition.getInstant().toEpochMilli(); } else { - nextTransition = instant.toEpochMilli(); + nextTransition = Long.MAX_VALUE; // fixed time-zone after prevTransition } // We need all not only values but also rounded values to be within diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java index 373df3e5f26a0..06ea9a37ba223 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java @@ -170,6 +170,18 @@ public void testRewriteTimeZone() throws IOException { assertSame(tz, builder.rewriteTimeZone(shardContextThatDoesntCross)); assertSame(tz, builder.rewriteTimeZone(shardContextThatCrosses)); + // timeZone without DST => always rewrite + tz = ZoneId.of("Australia/Brisbane"); + builder.timeZone(tz); + assertSame(ZoneOffset.ofHours(10), builder.rewriteTimeZone(shardContextThatDoesntCross)); + assertSame(ZoneOffset.ofHours(10), builder.rewriteTimeZone(shardContextThatCrosses)); + + // another timeZone without DST => always rewrite + tz = ZoneId.of("Asia/Katmandu"); + builder.timeZone(tz); + assertSame(ZoneOffset.ofHoursMinutes(5, 45), builder.rewriteTimeZone(shardContextThatDoesntCross)); + assertSame(ZoneOffset.ofHoursMinutes(5, 45), builder.rewriteTimeZone(shardContextThatCrosses)); + // daylight-saving-times => rewrite if doesn't cross tz = ZoneId.of("Europe/Paris"); builder.timeZone(tz);