From 50e3666414b7f73c40f41960177e8098071550b9 Mon Sep 17 00:00:00 2001 From: Andriy Redko Date: Wed, 29 Nov 2023 16:27:16 -0500 Subject: [PATCH] Revert Rounding API visibility changes (#11392) Signed-off-by: Andriy Redko --- .../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 002d3924a6324..6f5f1e4328758 100644 --- a/server/src/main/java/org/opensearch/common/Rounding.java +++ b/server/src/main/java/org/opensearch/common/Rounding.java @@ -54,6 +54,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; @@ -64,6 +65,7 @@ import java.util.List; import java.util.Locale; import java.util.Objects; +import java.util.OptionalLong; import java.util.concurrent.TimeUnit; /** @@ -98,7 +100,7 @@ long roundFloor(long utcMillis) { } @Override - public long extraLocalOffsetLookup() { + long extraLocalOffsetLookup() { return extraLocalOffsetLookup; } }, @@ -109,7 +111,7 @@ long roundFloor(long utcMillis) { return DateUtils.roundYear(utcMillis); } - public long extraLocalOffsetLookup() { + long extraLocalOffsetLookup() { return extraLocalOffsetLookup; } }, @@ -120,7 +122,7 @@ long roundFloor(long utcMillis) { return DateUtils.roundQuarterOfYear(utcMillis); } - public long extraLocalOffsetLookup() { + long extraLocalOffsetLookup() { return extraLocalOffsetLookup; } }, @@ -131,7 +133,7 @@ long roundFloor(long utcMillis) { return DateUtils.roundMonthOfYear(utcMillis); } - public long extraLocalOffsetLookup() { + long extraLocalOffsetLookup() { return extraLocalOffsetLookup; } }, @@ -140,7 +142,7 @@ long roundFloor(long utcMillis) { return DateUtils.roundFloor(utcMillis, this.ratio); } - public long extraLocalOffsetLookup() { + long extraLocalOffsetLookup() { return ratio; } }, @@ -149,7 +151,7 @@ long roundFloor(long utcMillis) { return DateUtils.roundFloor(utcMillis, ratio); } - public long extraLocalOffsetLookup() { + long extraLocalOffsetLookup() { return ratio; } }, @@ -164,7 +166,7 @@ long roundFloor(long utcMillis) { return DateUtils.roundFloor(utcMillis, ratio); } - public long extraLocalOffsetLookup() { + long extraLocalOffsetLookup() { return ratio; } }, @@ -216,7 +218,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; @@ -487,7 +489,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; @@ -515,14 +517,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: @@ -953,7 +947,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; @@ -980,14 +974,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; @@ -1384,4 +1370,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;