use of org.apache.druid.data.input.MapBasedRow in project druid by druid-io.
the class MovingAverageQueryTest method consistentTypeCasting.
/**
* converts Int to Long, Float to Double in the actual and expected result
*/
private List<MapBasedRow> consistentTypeCasting(List<MapBasedRow> result) {
List<MapBasedRow> newResult = new ArrayList<>();
for (MapBasedRow row : result) {
final Map<String, Object> event = Maps.newLinkedHashMap((row).getEvent());
event.forEach((key, value) -> {
if (value instanceof Integer) {
event.put(key, ((Integer) value).longValue());
}
if (value instanceof Float) {
event.put(key, ((Float) value).doubleValue());
}
});
newResult.add(new MapBasedRow(row.getTimestamp(), event));
}
return newResult;
}
use of org.apache.druid.data.input.MapBasedRow in project druid by druid-io.
the class PostAveragerAggregatorCalculator method apply.
@Override
public Row apply(final Row row) {
if (postAveragers.isEmpty()) {
return row;
}
final Map<String, Object> newMap;
newMap = Maps.newLinkedHashMap(((MapBasedRow) row).getEvent());
for (PostAggregator postAverager : postAveragers) {
boolean allColsPresent = postAverager.getDependentFields().stream().allMatch(c -> newMap.get(c) != null);
newMap.put(postAverager.getName(), allColsPresent ? postAverager.compute(newMap) : null);
}
return new MapBasedRow(row.getTimestamp(), newMap);
}
use of org.apache.druid.data.input.MapBasedRow in project druid by druid-io.
the class MaterializedViewQueryQueryToolChestTest method testDecorateObjectMapperMaterializedViewQuery.
@Test
public void testDecorateObjectMapperMaterializedViewQuery() throws IOException {
GroupByQuery realQuery = GroupByQuery.builder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("quality", "alias")).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("idx", "index")).setGranularity(QueryRunnerTestHelper.DAY_GRAN).setContext(ImmutableMap.of(GroupByQueryConfig.CTX_KEY_ARRAY_RESULT_ROWS, false)).build();
MaterializedViewQuery materializedViewQuery = new MaterializedViewQuery(realQuery, null);
QueryToolChest materializedViewQueryQueryToolChest = new MaterializedViewQueryQueryToolChest(new MapQueryToolChestWarehouse(ImmutableMap.<Class<? extends Query>, QueryToolChest>builder().put(GroupByQuery.class, new GroupByQueryQueryToolChest(null)).build()));
ObjectMapper objectMapper = materializedViewQueryQueryToolChest.decorateObjectMapper(JSON_MAPPER, materializedViewQuery);
List<ResultRow> results = Arrays.asList(GroupByQueryRunnerTestHelper.createExpectedRow(realQuery, "2011-04-01", "alias", "automotive", "rows", 1L, "idx", 135L), GroupByQueryRunnerTestHelper.createExpectedRow(realQuery, "2011-04-01", "alias", "business", "rows", 1L, "idx", 118L));
List<MapBasedRow> expectedResults = results.stream().map(resultRow -> resultRow.toMapBasedRow(realQuery)).collect(Collectors.toList());
Assert.assertEquals("decorate-object-mapper", JSON_MAPPER.writerFor(new TypeReference<List<MapBasedRow>>() {
}).writeValueAsString(expectedResults), objectMapper.writeValueAsString(results));
}
use of org.apache.druid.data.input.MapBasedRow in project druid by druid-io.
the class MovingAverageQueryToolChest method makePostComputeManipulatorFn.
@Override
public Function<Row, Row> makePostComputeManipulatorFn(MovingAverageQuery query, MetricManipulationFn fn) {
return new Function<Row, Row>() {
@Override
public Row apply(Row result) {
MapBasedRow mRow = (MapBasedRow) result;
final Map<String, Object> values = new HashMap<>(mRow.getEvent());
for (AggregatorFactory agg : query.getAggregatorSpecs()) {
Object aggVal = values.get(agg.getName());
if (aggVal != null) {
values.put(agg.getName(), fn.manipulate(agg, aggVal));
} else {
values.put(agg.getName(), null);
}
}
for (AveragerFactory<?, ?> avg : query.getAveragerSpecs()) {
Object aggVal = values.get(avg.getName());
if (aggVal != null) {
values.put(avg.getName(), fn.manipulate(new AveragerFactoryWrapper<>(avg, avg.getName() + "_"), aggVal));
} else {
values.put(avg.getName(), null);
}
}
return new MapBasedRow(result.getTimestamp(), values);
}
};
}
use of org.apache.druid.data.input.MapBasedRow in project druid by druid-io.
the class SketchAggregationTest method testSketchDataIngestAndGpByQuery.
@Test
public void testSketchDataIngestAndGpByQuery() throws Exception {
final GroupByQuery groupByQuery = readQueryFromClasspath("sketch_test_data_group_by_query.json", helper.getObjectMapper(), vectorize);
final Sequence<ResultRow> seq = helper.createIndexAndRunQueryOnSegment(new File(SketchAggregationTest.class.getClassLoader().getResource("sketch_test_data.tsv").getFile()), readFileFromClasspathAsString("sketch_test_data_record_parser.json"), readFileFromClasspathAsString("sketch_test_data_aggregators.json"), 0, Granularities.NONE, 1000, groupByQuery);
final String expectedSummary = "\n### HeapCompactSketch SUMMARY: \n" + " Estimate : 50.0\n" + " Upper Bound, 95% conf : 50.0\n" + " Lower Bound, 95% conf : 50.0\n" + " Theta (double) : 1.0\n" + " Theta (long) : 9223372036854775807\n" + " Theta (long) hex : 7fffffffffffffff\n" + " EstMode? : false\n" + " Empty? : false\n" + " Ordered? : true\n" + " Retained Entries : 50\n" + " Seed Hash : 93cc | 37836\n" + "### END SKETCH SUMMARY\n";
List<ResultRow> results = seq.toList();
Assert.assertEquals(1, results.size());
Assert.assertEquals(ResultRow.fromLegacyRow(new MapBasedRow(DateTimes.of("2014-10-19T00:00:00.000Z"), ImmutableMap.<String, Object>builder().put("sids_sketch_count", 50.0).put("sids_sketch_count_with_err", new SketchEstimateWithErrorBounds(50.0, 50.0, 50.0, 2)).put("sketchEstimatePostAgg", 50.0).put("sketchEstimatePostAggWithErrorBounds", new SketchEstimateWithErrorBounds(50.0, 50.0, 50.0, 2)).put("sketchUnionPostAggEstimate", 50.0).put("sketchSummary", expectedSummary).put("sketchIntersectionPostAggEstimate", 50.0).put("sketchAnotBPostAggEstimate", 0.0).put("non_existing_col_validation", 0.0).build()), groupByQuery), results.get(0));
}
Aggregations