AmatyaAvadhanula commented on code in PR #13369:
URL: https://github.com/apache/druid/pull/13369#discussion_r1034384415
##########
core/src/main/java/org/apache/druid/java/util/common/Intervals.java:
##########
@@ -68,6 +72,50 @@ public static boolean isEternity(final Interval interval)
return ETERNITY.equals(interval);
}
+ /**
+ * Finds an interval from the given set of sortedIntervals which overlaps
with
+ * the searchInterval. If multiple candidate intervals overlap with the
+ * searchInterval, the "smallest" interval based on the
+ * {@link Comparators#intervalsByStartThenEnd()} is returned.
+ *
+ * @param searchInterval Interval which should overlap with the result
+ * @param sortedIntervals Candidate overlapping intervals, sorted in
ascending
+ * order, using {@link
Comparators#intervalsByStartThenEnd()}.
+ * @return The first overlapping interval, if one exists, otherwise null.
+ */
+ @Nullable
+ public static Interval findOverlappingInterval(Interval searchInterval,
Interval[] sortedIntervals)
+ {
+ Arrays.sort(sortedIntervals, Comparators.intervalsByStartThenEnd());
+ int index = Arrays.binarySearch(
+ sortedIntervals,
+ searchInterval,
+ Comparators.intervalsByStartThenEnd()
+ );
+ if (index >= 0) {
+ return sortedIntervals[index];
+ }
+
+ // Key was not found, index returned from binarySearch is
(-(insertionPoint) - 1)
+ index = -(index + 1);
+
+ // If the interval at (index - 1) doesn't overlap, (index - 2) wouldn't
overlap either
Review Comment:
This seems like an incorrect assumption with segments of different
granularities.
Consider the sorted intervals [ (2022-01-01 / 2023-01-01), (2022-05-01 /
2022-06-01) ]
If we were to take target interval [2022-07-01 / 2022-08-01], index = 1
would not overlap but index = 0 would.
If the intervals to this method are all of the same granularity, could you
please add it to the javadoc?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]