From 57b5cdf3927e85122fa86c9b396a9cd0402126a9 Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Fri, 1 Dec 2023 01:50:09 +0530 Subject: [PATCH] Revert Rounding API visibility changes Signed-off-by: Ankit Jain --- .../java/org/opensearch/common/Rounding.java | 74 ++++++++++++------- .../bucket/histogram/FilterRewriteHelper.java | 32 ++------ 2 files changed, 53 insertions(+), 53 deletions(-) diff --git a/server/src/main/java/org/opensearch/common/Rounding.java b/server/src/main/java/org/opensearch/common/Rounding.java index cf9f50f63752f..3136b35addfe2 100644 --- a/server/src/main/java/org/opensearch/common/Rounding.java +++ b/server/src/main/java/org/opensearch/common/Rounding.java @@ -55,6 +55,7 @@ import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZoneOffset; +import java.time.format.TextStyle; import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; import java.time.temporal.IsoFields; @@ -65,6 +66,7 @@ import java.util.List; import java.util.Locale; import java.util.Objects; +import java.util.OptionalLong; import java.util.concurrent.TimeUnit; /** @@ -99,7 +101,7 @@ long roundFloor(long utcMillis) { } @Override - public long extraLocalOffsetLookup() { + long extraLocalOffsetLookup() { return extraLocalOffsetLookup; } }, @@ -110,7 +112,7 @@ long roundFloor(long utcMillis) { return DateUtils.roundYear(utcMillis); } - public long extraLocalOffsetLookup() { + long extraLocalOffsetLookup() { return extraLocalOffsetLookup; } }, @@ -121,7 +123,7 @@ long roundFloor(long utcMillis) { return DateUtils.roundQuarterOfYear(utcMillis); } - public long extraLocalOffsetLookup() { + long extraLocalOffsetLookup() { return extraLocalOffsetLookup; } }, @@ -132,7 +134,7 @@ long roundFloor(long utcMillis) { return DateUtils.roundMonthOfYear(utcMillis); } - public long extraLocalOffsetLookup() { + long extraLocalOffsetLookup() { return extraLocalOffsetLookup; } }, @@ -141,7 +143,7 @@ long roundFloor(long utcMillis) { return DateUtils.roundFloor(utcMillis, this.ratio); } - public long extraLocalOffsetLookup() { + long extraLocalOffsetLookup() { return ratio; } }, @@ -150,7 +152,7 @@ long roundFloor(long utcMillis) { return DateUtils.roundFloor(utcMillis, ratio); } - public long extraLocalOffsetLookup() { + long extraLocalOffsetLookup() { return ratio; } }, @@ -165,7 +167,7 @@ long roundFloor(long utcMillis) { return DateUtils.roundFloor(utcMillis, ratio); } - public long extraLocalOffsetLookup() { + long extraLocalOffsetLookup() { return ratio; } }, @@ -217,7 +219,7 @@ public long extraLocalOffsetLookup() { * look up so that we can see transitions that we might have rounded * down beyond. */ - public abstract long extraLocalOffsetLookup(); + abstract long extraLocalOffsetLookup(); public byte getId() { return id; @@ -488,7 +490,7 @@ public double roundingSize(long utcMillis, DateTimeUnit timeUnit) { * * @opensearch.internal */ - public static class TimeUnitRounding extends Rounding { + static class TimeUnitRounding extends Rounding { static final byte ID = 1; private final DateTimeUnit unit; @@ -523,14 +525,6 @@ public byte id() { return ID; } - public DateTimeUnit getUnit() { - return this.unit; - } - - public ZoneId getTimeZone() { - return this.timeZone; - } - private LocalDateTime truncateLocalDateTime(LocalDateTime localDateTime) { switch (unit) { case SECOND_OF_MINUTE: @@ -961,7 +955,7 @@ public final long nextRoundingValue(long utcMillis) { * * @opensearch.internal */ - public static class TimeIntervalRounding extends Rounding { + static class TimeIntervalRounding extends Rounding { static final byte ID = 2; private final long interval; @@ -992,14 +986,6 @@ public byte id() { return ID; } - public long getInterval() { - return this.interval; - } - - public ZoneId getTimeZone() { - return this.timeZone; - } - @Override public Prepared prepare(long minUtcMillis, long maxUtcMillis) { long minLookup = minUtcMillis - interval; @@ -1399,4 +1385,40 @@ public static Rounding read(StreamInput in) throws IOException { throw new OpenSearchException("unknown rounding id [" + id + "]"); } } + + /** + * Extracts the interval value from the {@link Rounding} instance + * @param rounding {@link Rounding} instance + * @return the interval value from the {@link Rounding} instance or {@code OptionalLong.empty()} + * if the interval is not available + */ + public static OptionalLong getInterval(Rounding rounding) { + long interval = 0; + + if (rounding instanceof TimeUnitRounding) { + interval = (((TimeUnitRounding) rounding).unit).extraLocalOffsetLookup(); + if (!isUTCTimeZone(((TimeUnitRounding) rounding).timeZone)) { + // Fast filter aggregation cannot be used if it needs time zone rounding + return OptionalLong.empty(); + } + } else if (rounding instanceof TimeIntervalRounding) { + interval = ((TimeIntervalRounding) rounding).interval; + if (!isUTCTimeZone(((TimeIntervalRounding) rounding).timeZone)) { + // Fast filter aggregation cannot be used if it needs time zone rounding + return OptionalLong.empty(); + } + } else { + return OptionalLong.empty(); + } + + return OptionalLong.of(interval); + } + + /** + * Helper function for checking if the time zone requested for date histogram + * aggregation is utc or not + */ + private static boolean isUTCTimeZone(final ZoneId zoneId) { + return "Z".equals(zoneId.getDisplayName(TextStyle.FULL, Locale.ENGLISH)); + } } diff --git a/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/FilterRewriteHelper.java b/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/FilterRewriteHelper.java index c6f8296e29dc0..29cecd5b382cd 100644 --- a/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/FilterRewriteHelper.java +++ b/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/FilterRewriteHelper.java @@ -29,12 +29,10 @@ import org.opensearch.search.internal.SearchContext; import java.io.IOException; -import java.time.ZoneId; -import java.time.format.TextStyle; import java.util.HashMap; import java.util.List; -import java.util.Locale; import java.util.Map; +import java.util.OptionalLong; import java.util.function.BiConsumer; import java.util.function.Function; import java.util.function.Supplier; @@ -58,7 +56,7 @@ public FilterContext(DateFieldMapper.DateFieldType fieldType, Weight[] filters) } private static final int MAX_NUM_FILTER_BUCKETS = 1024; - private static final Map> queryWrappers; + private static final Map, Function> queryWrappers; // Initialize the wrappers map for unwrapping the query static { @@ -122,14 +120,6 @@ static long[] getAggregationBounds(final SearchContext context, final String fie return null; } - /** - * Helper function for checking if the time zone requested for date histogram - * aggregation is utc or not - */ - private static boolean isUTCTimeZone(final ZoneId zoneId) { - return "Z".equals(zoneId.getDisplayName(TextStyle.FULL, Locale.ENGLISH)); - } - /** * Creates the range query filters for aggregations using the interval, min/max * bounds and the rounding values @@ -143,24 +133,12 @@ private static Weight[] createFilterForAggregations( final long low, final long high ) throws IOException { - long interval; - if (rounding instanceof Rounding.TimeUnitRounding) { - interval = (((Rounding.TimeUnitRounding) rounding).getUnit()).extraLocalOffsetLookup(); - if (!isUTCTimeZone(((Rounding.TimeUnitRounding) rounding).getTimeZone())) { - // Fast filter aggregation cannot be used if it needs time zone rounding - return null; - } - } else if (rounding instanceof Rounding.TimeIntervalRounding) { - interval = ((Rounding.TimeIntervalRounding) rounding).getInterval(); - if (!isUTCTimeZone(((Rounding.TimeIntervalRounding) rounding).getTimeZone())) { - // Fast filter aggregation cannot be used if it needs time zone rounding - return null; - } - } else { - // Unexpected scenario, exit and fall back to original + final OptionalLong intervalOpt = Rounding.getInterval(rounding); + if (intervalOpt.isEmpty()) { return null; } + final long interval = intervalOpt.getAsLong(); // Calculate the number of buckets using range and interval long roundedLow = preparedRounding.round(fieldType.convertNanosToMillis(low)); long prevRounded = roundedLow;