Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert Rounding API visibility changes #11392

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 48 additions & 26 deletions server/src/main/java/org/opensearch/common/Rounding.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -98,7 +100,7 @@
}

@Override
public long extraLocalOffsetLookup() {
long extraLocalOffsetLookup() {
return extraLocalOffsetLookup;
}
},
Expand All @@ -109,7 +111,7 @@
return DateUtils.roundYear(utcMillis);
}

public long extraLocalOffsetLookup() {
long extraLocalOffsetLookup() {
return extraLocalOffsetLookup;
}
},
Expand All @@ -120,7 +122,7 @@
return DateUtils.roundQuarterOfYear(utcMillis);
}

public long extraLocalOffsetLookup() {
long extraLocalOffsetLookup() {
return extraLocalOffsetLookup;
}
},
Expand All @@ -131,7 +133,7 @@
return DateUtils.roundMonthOfYear(utcMillis);
}

public long extraLocalOffsetLookup() {
long extraLocalOffsetLookup() {
return extraLocalOffsetLookup;
}
},
Expand All @@ -140,7 +142,7 @@
return DateUtils.roundFloor(utcMillis, this.ratio);
}

public long extraLocalOffsetLookup() {
long extraLocalOffsetLookup() {
return ratio;
}
},
Expand All @@ -149,7 +151,7 @@
return DateUtils.roundFloor(utcMillis, ratio);
}

public long extraLocalOffsetLookup() {
long extraLocalOffsetLookup() {
return ratio;
}
},
Expand All @@ -164,7 +166,7 @@
return DateUtils.roundFloor(utcMillis, ratio);
}

public long extraLocalOffsetLookup() {
long extraLocalOffsetLookup() {
return ratio;
}
},
Expand Down Expand Up @@ -216,7 +218,7 @@
* 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;
Expand Down Expand Up @@ -487,7 +489,7 @@
*
* @opensearch.internal
*/
public static class TimeUnitRounding extends Rounding {
static class TimeUnitRounding extends Rounding {
static final byte ID = 1;

private final DateTimeUnit unit;
Expand Down Expand Up @@ -515,14 +517,6 @@
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:
Expand Down Expand Up @@ -953,7 +947,7 @@
*
* @opensearch.internal
*/
public static class TimeIntervalRounding extends Rounding {
static class TimeIntervalRounding extends Rounding {
static final byte ID = 2;

private final long interval;
Expand All @@ -980,14 +974,6 @@
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;
Expand Down Expand Up @@ -1384,4 +1370,40 @@
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();

Check warning on line 1393 in server/src/main/java/org/opensearch/common/Rounding.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/common/Rounding.java#L1393

Added line #L1393 was not covered by tests
}
} else {
return OptionalLong.empty();

Check warning on line 1396 in server/src/main/java/org/opensearch/common/Rounding.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/common/Rounding.java#L1396

Added line #L1396 was not covered by tests
}

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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -58,7 +56,7 @@ public FilterContext(DateFieldMapper.DateFieldType fieldType, Weight[] filters)
}

private static final int MAX_NUM_FILTER_BUCKETS = 1024;
private static final Map<Class, Function<Query, Query>> queryWrappers;
private static final Map<Class<?>, Function<Query, Query>> queryWrappers;

// Initialize the wrappers map for unwrapping the query
static {
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
Loading