Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,19 @@ public Result<TopNResultValue> apply(Object input)
Iterator<Object> inputIter = results.iterator();
DateTime timestamp = granularity.toDateTime(((Number) inputIter.next()).longValue());

// Need a value transformer to convert generic Jackson-deserialized type into the proper type.
final Function<Object, Object> dimValueTransformer = TopNMapFn.getValueTransformer(
query.getDimensionSpec().getOutputType()
);

while (inputIter.hasNext()) {
List<Object> result = (List<Object>) inputIter.next();
Map<String, Object> vals = Maps.newLinkedHashMap();

Iterator<AggregatorFactory> aggIter = aggs.iterator();
Iterator<Object> resultIter = result.iterator();

vals.put(query.getDimensionSpec().getOutputName(), resultIter.next());
vals.put(query.getDimensionSpec().getOutputName(), dimValueTransformer.apply(resultIter.next()));

while (aggIter.hasNext() && resultIter.hasNext()) {
final AggregatorFactory factory = aggIter.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@
import io.druid.segment.TestHelper;
import io.druid.segment.TestIndex;
import io.druid.segment.VirtualColumns;
import io.druid.segment.column.ValueType;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

Expand All @@ -61,76 +63,15 @@ public class TopNQueryQueryToolChestTest
@Test
public void testCacheStrategy() throws Exception
{
CacheStrategy<Result<TopNResultValue>, Object, TopNQuery> strategy =
new TopNQueryQueryToolChest(null, null).getCacheStrategy(
new TopNQuery(
new TableDataSource("dummy"),
VirtualColumns.EMPTY,
new DefaultDimensionSpec("test", "test"),
new NumericTopNMetricSpec("metric1"),
3,
new MultipleIntervalSegmentSpec(ImmutableList.of(Intervals.of("2015-01-01/2015-01-02"))),
null,
Granularities.ALL,
ImmutableList.<AggregatorFactory>of(new CountAggregatorFactory("metric1")),
ImmutableList.<PostAggregator>of(new ConstantPostAggregator("post", 10)),
null
)
);

final Result<TopNResultValue> result1 = new Result<>(
// test timestamps that result in integer size millis
DateTimes.utc(123L),
new TopNResultValue(
Arrays.asList(
ImmutableMap.<String, Object>of(
"test", "val1",
"metric1", 2
)
)
)
);

Object preparedValue = strategy.prepareForSegmentLevelCache().apply(
result1
);

ObjectMapper objectMapper = TestHelper.makeJsonMapper();
Object fromCacheValue = objectMapper.readValue(
objectMapper.writeValueAsBytes(preparedValue),
strategy.getCacheObjectClazz()
);

Result<TopNResultValue> fromCacheResult = strategy.pullFromSegmentLevelCache().apply(fromCacheValue);

Assert.assertEquals(result1, fromCacheResult);

final Result<TopNResultValue> result2 = new Result<>(
// test timestamps that result in integer size millis
DateTimes.utc(123L),
new TopNResultValue(
Arrays.asList(
ImmutableMap.<String, Object>of(
"test", "val1",
"metric1", 2,
"post", 10
)
)
)
);

Object preparedResultCacheValue = strategy.prepareForCache(true).apply(
result2
);

Object fromResultCacheValue = objectMapper.readValue(
objectMapper.writeValueAsBytes(preparedResultCacheValue),
strategy.getCacheObjectClazz()
);

Result<TopNResultValue> fromResultCacheResult = strategy.pullFromCache(true).apply(fromResultCacheValue);
Assert.assertEquals(result2, fromResultCacheResult);
doTestCacheStrategy(ValueType.STRING, "val1");
doTestCacheStrategy(ValueType.FLOAT, 2.1f);
doTestCacheStrategy(ValueType.DOUBLE, 2.1d);
doTestCacheStrategy(ValueType.LONG, 2L);
}

@Test
public void testCacheStrategyWithFloatDimension() throws Exception
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gianm this test is empty. Is this intended?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I must have included it by accident. I think it's not needed since I modified the testCacheStrategy test to check all four supported types.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'll remove this.

}

@Test
Expand Down Expand Up @@ -242,6 +183,79 @@ public void testMinTopNThreshold()
Assert.assertEquals(2000, mockRunner.query.getThreshold());
}

private void doTestCacheStrategy(final ValueType valueType, final Object dimValue) throws IOException
{
CacheStrategy<Result<TopNResultValue>, Object, TopNQuery> strategy =
new TopNQueryQueryToolChest(null, null).getCacheStrategy(
new TopNQuery(
new TableDataSource("dummy"),
VirtualColumns.EMPTY,
new DefaultDimensionSpec("test", "test", valueType),
new NumericTopNMetricSpec("metric1"),
3,
new MultipleIntervalSegmentSpec(ImmutableList.of(Intervals.of("2015-01-01/2015-01-02"))),
null,
Granularities.ALL,
ImmutableList.<AggregatorFactory>of(new CountAggregatorFactory("metric1")),
ImmutableList.<PostAggregator>of(new ConstantPostAggregator("post", 10)),
null
)
);

final Result<TopNResultValue> result1 = new Result<>(
// test timestamps that result in integer size millis
DateTimes.utc(123L),
new TopNResultValue(
Arrays.asList(
ImmutableMap.<String, Object>of(
"test", dimValue,
"metric1", 2
)
)
)
);

Object preparedValue = strategy.prepareForSegmentLevelCache().apply(
result1
);

ObjectMapper objectMapper = TestHelper.makeJsonMapper();
Object fromCacheValue = objectMapper.readValue(
objectMapper.writeValueAsBytes(preparedValue),
strategy.getCacheObjectClazz()
);

Result<TopNResultValue> fromCacheResult = strategy.pullFromSegmentLevelCache().apply(fromCacheValue);

Assert.assertEquals(result1, fromCacheResult);

final Result<TopNResultValue> result2 = new Result<>(
// test timestamps that result in integer size millis
DateTimes.utc(123L),
new TopNResultValue(
Arrays.asList(
ImmutableMap.<String, Object>of(
"test", dimValue,
"metric1", 2,
"post", 10
)
)
)
);

Object preparedResultCacheValue = strategy.prepareForCache(true).apply(
result2
);

Object fromResultCacheValue = objectMapper.readValue(
objectMapper.writeValueAsBytes(preparedResultCacheValue),
strategy.getCacheObjectClazz()
);

Result<TopNResultValue> fromResultCacheResult = strategy.pullFromCache(true).apply(fromResultCacheValue);
Assert.assertEquals(result2, fromResultCacheResult);
}

static class MockQueryRunner implements QueryRunner<Result<TopNResultValue>>
{
private final QueryRunner<Result<TopNResultValue>> runner;
Expand Down