Search in sources :

Example 41 with MapBasedRow

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;
}
Also used : MapBasedRow(org.apache.druid.data.input.MapBasedRow) ArrayList(java.util.ArrayList)

Example 42 with MapBasedRow

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);
}
Also used : MapBasedRow(org.apache.druid.data.input.MapBasedRow) PostAggregator(org.apache.druid.query.aggregation.PostAggregator)

Example 43 with MapBasedRow

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));
}
Also used : ResultRow(org.apache.druid.query.groupby.ResultRow) Arrays(java.util.Arrays) MapBasedRow(org.apache.druid.data.input.MapBasedRow) TimeseriesResultValue(org.apache.druid.query.timeseries.TimeseriesResultValue) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Druids(org.apache.druid.query.Druids) Query(org.apache.druid.query.Query) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) Map(java.util.Map) GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) TypeReference(com.fasterxml.jackson.core.type.TypeReference) MetricManipulationFn(org.apache.druid.query.aggregation.MetricManipulationFn) DateTimes(org.apache.druid.java.util.common.DateTimes) Function(com.google.common.base.Function) ImmutableMap(com.google.common.collect.ImmutableMap) ResultRow(org.apache.druid.query.groupby.ResultRow) TimeseriesQueryQueryToolChest(org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) GroupByQueryConfig(org.apache.druid.query.groupby.GroupByQueryConfig) MapQueryToolChestWarehouse(org.apache.druid.query.MapQueryToolChestWarehouse) QueryToolChest(org.apache.druid.query.QueryToolChest) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) GroupByQueryRunnerTestHelper(org.apache.druid.query.groupby.GroupByQueryRunnerTestHelper) Result(org.apache.druid.query.Result) List(java.util.List) QueryRunnerTestHelper(org.apache.druid.query.QueryRunnerTestHelper) GroupByQueryQueryToolChest(org.apache.druid.query.groupby.GroupByQueryQueryToolChest) Assert(org.junit.Assert) Query(org.apache.druid.query.Query) GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) TimeseriesQueryQueryToolChest(org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest) QueryToolChest(org.apache.druid.query.QueryToolChest) GroupByQueryQueryToolChest(org.apache.druid.query.groupby.GroupByQueryQueryToolChest) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) GroupByQueryQueryToolChest(org.apache.druid.query.groupby.GroupByQueryQueryToolChest) MapBasedRow(org.apache.druid.data.input.MapBasedRow) GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) MapQueryToolChestWarehouse(org.apache.druid.query.MapQueryToolChestWarehouse) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) Test(org.junit.Test)

Example 44 with MapBasedRow

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);
        }
    };
}
Also used : MapBasedRow(org.apache.druid.data.input.MapBasedRow) Function(com.google.common.base.Function) HashMap(java.util.HashMap) MapBasedRow(org.apache.druid.data.input.MapBasedRow) Row(org.apache.druid.data.input.Row) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory)

Example 45 with MapBasedRow

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));
}
Also used : ResultRow(org.apache.druid.query.groupby.ResultRow) MapBasedRow(org.apache.druid.data.input.MapBasedRow) GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) File(java.io.File) GroupByQueryRunnerTest(org.apache.druid.query.groupby.GroupByQueryRunnerTest) Test(org.junit.Test)

Aggregations

MapBasedRow (org.apache.druid.data.input.MapBasedRow)65 Test (org.junit.Test)50 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)36 ArrayList (java.util.ArrayList)21 Row (org.apache.druid.data.input.Row)16 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)16 GroupByQueryRunnerTest (org.apache.druid.query.groupby.GroupByQueryRunnerTest)16 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)15 HashMap (java.util.HashMap)13 DimensionSpec (org.apache.druid.query.dimension.DimensionSpec)12 GroupByQuery (org.apache.druid.query.groupby.GroupByQuery)10 List (java.util.List)9 ResultRow (org.apache.druid.query.groupby.ResultRow)9 LongMeanAveragerFactory (org.apache.druid.query.movingaverage.averagers.LongMeanAveragerFactory)9 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)8 File (java.io.File)7 ByteBuffer (java.nio.ByteBuffer)6 GroupByQueryConfig (org.apache.druid.query.groupby.GroupByQueryConfig)6 TimeseriesResultValue (org.apache.druid.query.timeseries.TimeseriesResultValue)6 IOException (java.io.IOException)5