in extensions-contrib/materialized-view-selection/src/main/java/org/apache/druid/query/materializedview/MaterializedViewUtils.java [101:232]
public static List<Interval> minus(List<Interval> interval2, List<Interval> interval1)
{
if (interval1.isEmpty() || interval2.isEmpty()) {
return interval1;
}
Iterator<Interval> it1 = JodaUtils.condenseIntervals(interval1).iterator();
Iterator<Interval> it2 = JodaUtils.condenseIntervals(interval2).iterator();
List<Interval> remaining = new ArrayList<>();
Interval currInterval1 = it1.next();
Interval currInterval2 = it2.next();
long start1 = currInterval1.getStartMillis();
long end1 = currInterval1.getEndMillis();
long start2 = currInterval2.getStartMillis();
long end2 = currInterval2.getEndMillis();
while (true) {
if (start2 < start1 && end2 <= start1) {
remaining.add(Intervals.utc(start2, end2));
if (it2.hasNext()) {
currInterval2 = it2.next();
start2 = currInterval2.getStartMillis();
end2 = currInterval2.getEndMillis();
} else {
break;
}
}
if (start2 < start1 && end2 > start1 && end2 < end1) {
remaining.add(Intervals.utc(start2, start1));
start1 = end2;
if (it2.hasNext()) {
currInterval2 = it2.next();
start2 = currInterval2.getStartMillis();
end2 = currInterval2.getEndMillis();
} else {
break;
}
}
if (start2 < start1 && end2 == end1) {
remaining.add(Intervals.utc(start2, start1));
if (it2.hasNext() && it1.hasNext()) {
currInterval2 = it2.next();
start2 = currInterval2.getStartMillis();
end2 = currInterval2.getEndMillis();
currInterval1 = it1.next();
start1 = currInterval1.getStartMillis();
end1 = currInterval1.getEndMillis();
} else {
break;
}
}
if (start2 < start1 && end2 > end1) {
remaining.add(Intervals.utc(start2, start1));
start2 = end1;
if (it1.hasNext()) {
currInterval1 = it1.next();
start1 = currInterval1.getStartMillis();
end1 = currInterval1.getEndMillis();
} else {
remaining.add(Intervals.utc(end1, end2));
break;
}
}
if (start2 == start1 && end2 >= start1 && end2 < end1) {
start1 = end2;
if (it2.hasNext()) {
currInterval2 = it2.next();
start2 = currInterval2.getStartMillis();
end2 = currInterval2.getEndMillis();
} else {
break;
}
}
if (start2 == start1 && end2 > end1) {
start2 = end1;
if (it1.hasNext()) {
currInterval1 = it1.next();
start1 = currInterval1.getStartMillis();
end1 = currInterval1.getEndMillis();
} else {
remaining.add(Intervals.utc(end1, end2));
break;
}
}
if (start2 > start1 && start2 < end1 && end2 < end1) {
start1 = end2;
if (it2.hasNext()) {
currInterval2 = it2.next();
start2 = currInterval2.getStartMillis();
end2 = currInterval2.getEndMillis();
} else {
break;
}
}
if (start2 > start1 && start2 < end1 && end2 > end1) {
start2 = end1;
if (it1.hasNext()) {
currInterval1 = it1.next();
start1 = currInterval1.getStartMillis();
end1 = currInterval1.getEndMillis();
} else {
remaining.add(Intervals.utc(end1, end2));
break;
}
}
if (start2 >= start1 && start2 <= end1 && end2 == end1) {
if (it2.hasNext() && it1.hasNext()) {
currInterval2 = it2.next();
start2 = currInterval2.getStartMillis();
end2 = currInterval2.getEndMillis();
currInterval1 = it1.next();
start1 = currInterval1.getStartMillis();
end1 = currInterval1.getEndMillis();
} else {
break;
}
}
if (start2 >= end1 && end2 > end1) {
if (it1.hasNext()) {
currInterval1 = it1.next();
start1 = currInterval1.getStartMillis();
end1 = currInterval1.getEndMillis();
} else {
remaining.add(Intervals.utc(start2, end2));
break;
}
}
}
while (it2.hasNext()) {
remaining.add(Intervals.of(it2.next().toString()));
}
return remaining;
}