Skip to content

Commit

Permalink
Merge pull request apache#2644 from metamx/optimize-timeboundary
Browse files Browse the repository at this point in the history
optimize timeboundary for min or max bound
  • Loading branch information
b-slim committed Mar 13, 2016
2 parents cf7f6da + 6f0d6ef commit 8cc3582
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ public Iterable<Result<TimeBoundaryResultValue>> mergeResults(List<Result<TimeBo
final DateTime minTime;
final DateTime maxTime;

if (bound.equalsIgnoreCase(MIN_TIME)) {
if (isMinTime()) {
ts = min;
minTime = min;
maxTime = null;
} else if (bound.equalsIgnoreCase(MAX_TIME)) {
} else if (isMaxTime()) {
ts = max;
minTime = null;
maxTime = max;
Expand All @@ -193,6 +193,16 @@ public Iterable<Result<TimeBoundaryResultValue>> mergeResults(List<Result<TimeBo
return buildResult(ts, minTime, maxTime);
}

boolean isMinTime()
{
return bound.equalsIgnoreCase(MIN_TIME);
}

boolean isMaxTime()
{
return bound.equalsIgnoreCase(MAX_TIME);
}

@Override
public String toString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public <T extends LogicalSegment> List<T> filterSegments(TimeBoundaryQuery query
return segments;
}

final T min = segments.get(0);
final T max = segments.get(segments.size() - 1);
final T min = query.isMaxTime() ? null : segments.get(0);
final T max = query.isMinTime() ? null : segments.get(segments.size() - 1);

return Lists.newArrayList(
Iterables.filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,73 +40,131 @@
*/
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
public void testFilterSegments() throws Exception
{
List<LogicalSegment> segments = new TimeBoundaryQueryQueryToolChest().filterSegments(
null,
TIME_BOUNDARY_QUERY,
Arrays.asList(
new LogicalSegment()
{
@Override
public Interval getInterval()
{
return new Interval("2013-01-01/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");
}
}
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(segments.size(), 3);
Assert.assertEquals(6, segments.size());

List<LogicalSegment> expected = Arrays.asList(
new LogicalSegment()
{
@Override
public Interval getInterval()
{
return new Interval("2013-01-01/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");
}
}
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-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 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
public void testCacheStrategy() throws Exception
{
Expand Down

0 comments on commit 8cc3582

Please sign in to comment.