Merge pull request #2644 from metamx/optimize-timeboundary

optimize timeboundary for min or max bound
This commit is contained in:
Slim 2016-03-13 13:16:24 -05:00
commit 8cc3582e70
3 changed files with 122 additions and 54 deletions

View File

@ -176,11 +176,11 @@ public class TimeBoundaryQuery extends BaseQuery<Result<TimeBoundaryResultValue>
final DateTime minTime; final DateTime minTime;
final DateTime maxTime; final DateTime maxTime;
if (bound.equalsIgnoreCase(MIN_TIME)) { if (isMinTime()) {
ts = min; ts = min;
minTime = min; minTime = min;
maxTime = null; maxTime = null;
} else if (bound.equalsIgnoreCase(MAX_TIME)) { } else if (isMaxTime()) {
ts = max; ts = max;
minTime = null; minTime = null;
maxTime = max; maxTime = max;
@ -193,6 +193,16 @@ public class TimeBoundaryQuery extends BaseQuery<Result<TimeBoundaryResultValue>
return buildResult(ts, minTime, maxTime); return buildResult(ts, minTime, maxTime);
} }
boolean isMinTime()
{
return bound.equalsIgnoreCase(MIN_TIME);
}
boolean isMaxTime()
{
return bound.equalsIgnoreCase(MAX_TIME);
}
@Override @Override
public String toString() public String toString()
{ {

View File

@ -65,8 +65,8 @@ public class TimeBoundaryQueryQueryToolChest
return segments; return segments;
} }
final T min = segments.get(0); final T min = query.isMaxTime() ? null : segments.get(0);
final T max = segments.get(segments.size() - 1); final T max = query.isMinTime() ? null : segments.get(segments.size() - 1);
return Lists.newArrayList( return Lists.newArrayList(
Iterables.filter( Iterables.filter(

View File

@ -40,66 +40,66 @@ import java.util.List;
*/ */
public class TimeBoundaryQueryQueryToolChestTest public class TimeBoundaryQueryQueryToolChestTest
{ {
private static final TimeBoundaryQuery TIME_BOUNDARY_QUERY = new TimeBoundaryQuery(
new TableDataSource("test"),
null,
null,
null
);
private static final TimeBoundaryQuery MAXTIME_BOUNDARY_QUERY = new TimeBoundaryQuery(
new TableDataSource("test"),
null,
TimeBoundaryQuery.MAX_TIME,
null
);
private static final TimeBoundaryQuery MINTIME_BOUNDARY_QUERY = new TimeBoundaryQuery(
new TableDataSource("test"),
null,
TimeBoundaryQuery.MIN_TIME,
null
);
private static LogicalSegment createLogicalSegment(final Interval interval)
{
return new LogicalSegment()
{
@Override
public Interval getInterval()
{
return interval;
}
};
}
@Test @Test
public void testFilterSegments() throws Exception public void testFilterSegments() throws Exception
{ {
List<LogicalSegment> segments = new TimeBoundaryQueryQueryToolChest().filterSegments( List<LogicalSegment> segments = new TimeBoundaryQueryQueryToolChest().filterSegments(
null, TIME_BOUNDARY_QUERY,
Arrays.asList( Arrays.asList(
new LogicalSegment() createLogicalSegment(new Interval("2013-01-01/P1D")),
{ createLogicalSegment(new Interval("2013-01-01T01/PT1H")),
@Override createLogicalSegment(new Interval("2013-01-01T02/PT1H")),
public Interval getInterval() createLogicalSegment(new Interval("2013-01-02/P1D")),
{ createLogicalSegment(new Interval("2013-01-03T01/PT1H")),
return new Interval("2013-01-01/P1D"); createLogicalSegment(new Interval("2013-01-03T02/PT1H")),
} createLogicalSegment(new Interval("2013-01-03/P1D"))
},
new LogicalSegment()
{
@Override
public Interval getInterval()
{
return new Interval("2013-01-01T01/PT1H");
}
},
new LogicalSegment()
{
@Override
public Interval getInterval()
{
return new Interval("2013-01-01T02/PT1H");
}
}
) )
); );
Assert.assertEquals(segments.size(), 3); Assert.assertEquals(6, segments.size());
List<LogicalSegment> expected = Arrays.asList( List<LogicalSegment> expected = Arrays.asList(
new LogicalSegment() createLogicalSegment(new Interval("2013-01-01/P1D")),
{ createLogicalSegment(new Interval("2013-01-01T01/PT1H")),
@Override createLogicalSegment(new Interval("2013-01-01T02/PT1H")),
public Interval getInterval() createLogicalSegment(new Interval("2013-01-03T01/PT1H")),
{ createLogicalSegment(new Interval("2013-01-03T02/PT1H")),
return new Interval("2013-01-01/P1D"); createLogicalSegment(new Interval("2013-01-03/P1D"))
}
},
new LogicalSegment()
{
@Override
public Interval getInterval()
{
return new Interval("2013-01-01T01/PT1H");
}
},
new LogicalSegment()
{
@Override
public Interval getInterval()
{
return new Interval("2013-01-01T02/PT1H");
}
}
); );
for (int i = 0; i < segments.size(); i++) { for (int i = 0; i < segments.size(); i++) {
@ -107,6 +107,64 @@ public class TimeBoundaryQueryQueryToolChestTest
} }
} }
@Test
public void testMaxTimeFilterSegments() throws Exception
{
List<LogicalSegment> segments = new TimeBoundaryQueryQueryToolChest().filterSegments(
MAXTIME_BOUNDARY_QUERY,
Arrays.asList(
createLogicalSegment(new Interval("2013-01-01/P1D")),
createLogicalSegment(new Interval("2013-01-01T01/PT1H")),
createLogicalSegment(new Interval("2013-01-01T02/PT1H")),
createLogicalSegment(new Interval("2013-01-02/P1D")),
createLogicalSegment(new Interval("2013-01-03T01/PT1H")),
createLogicalSegment(new Interval("2013-01-03T02/PT1H")),
createLogicalSegment(new Interval("2013-01-03/P1D"))
)
);
Assert.assertEquals(3, segments.size());
List<LogicalSegment> expected = Arrays.asList(
createLogicalSegment(new Interval("2013-01-03T01/PT1H")),
createLogicalSegment(new Interval("2013-01-03T02/PT1H")),
createLogicalSegment(new Interval("2013-01-03/P1D"))
);
for (int i = 0; i < segments.size(); i++) {
Assert.assertEquals(segments.get(i).getInterval(), expected.get(i).getInterval());
}
}
@Test
public void testMinTimeFilterSegments() throws Exception
{
List<LogicalSegment> segments = new TimeBoundaryQueryQueryToolChest().filterSegments(
MINTIME_BOUNDARY_QUERY,
Arrays.asList(
createLogicalSegment(new Interval("2013-01-01/P1D")),
createLogicalSegment(new Interval("2013-01-01T01/PT1H")),
createLogicalSegment(new Interval("2013-01-01T02/PT1H")),
createLogicalSegment(new Interval("2013-01-02/P1D")),
createLogicalSegment(new Interval("2013-01-03T01/PT1H")),
createLogicalSegment(new Interval("2013-01-03T02/PT1H")),
createLogicalSegment(new Interval("2013-01-03/P1D"))
)
);
Assert.assertEquals(3, segments.size());
List<LogicalSegment> expected = Arrays.asList(
createLogicalSegment(new Interval("2013-01-01/P1D")),
createLogicalSegment(new Interval("2013-01-01T01/PT1H")),
createLogicalSegment(new Interval("2013-01-01T02/PT1H"))
);
for (int i = 0; i < segments.size(); i++) {
Assert.assertEquals(segments.get(i).getInterval(), expected.get(i).getInterval());
}
}
@Test @Test
public void testCacheStrategy() throws Exception public void testCacheStrategy() throws Exception
{ {