Search in sources :

Example 11 with MapBasedRow

use of io.druid.data.input.MapBasedRow in project druid by druid-io.

the class TimewarpOperator method postProcess.

public QueryRunner<T> postProcess(final QueryRunner<T> baseRunner, final long now) {
    return new QueryRunner<T>() {

        @Override
        public Sequence<T> run(final Query<T> query, final Map<String, Object> responseContext) {
            final long offset = computeOffset(now);
            final Interval interval = query.getIntervals().get(0);
            final Interval modifiedInterval = new Interval(Math.min(interval.getStartMillis() + offset, now + offset), Math.min(interval.getEndMillis() + offset, now + offset));
            return Sequences.map(baseRunner.run(query.withQuerySegmentSpec(new MultipleIntervalSegmentSpec(Arrays.asList(modifiedInterval))), responseContext), new Function<T, T>() {

                @Override
                public T apply(T input) {
                    if (input instanceof Result) {
                        Result res = (Result) input;
                        Object value = res.getValue();
                        if (value instanceof TimeBoundaryResultValue) {
                            TimeBoundaryResultValue boundary = (TimeBoundaryResultValue) value;
                            DateTime minTime = null;
                            try {
                                minTime = boundary.getMinTime();
                            } catch (IllegalArgumentException e) {
                            }
                            final DateTime maxTime = boundary.getMaxTime();
                            return (T) ((TimeBoundaryQuery) query).buildResult(new DateTime(Math.min(res.getTimestamp().getMillis() - offset, now)), minTime != null ? minTime.minus(offset) : null, maxTime != null ? new DateTime(Math.min(maxTime.getMillis() - offset, now)) : null).iterator().next();
                        }
                        return (T) new Result(res.getTimestamp().minus(offset), value);
                    } else if (input instanceof MapBasedRow) {
                        MapBasedRow row = (MapBasedRow) input;
                        return (T) new MapBasedRow(row.getTimestamp().minus(offset), row.getEvent());
                    }
                    // default to noop for unknown result types
                    return input;
                }
            });
        }
    };
}
Also used : TimeBoundaryQuery(io.druid.query.timeboundary.TimeBoundaryQuery) MultipleIntervalSegmentSpec(io.druid.query.spec.MultipleIntervalSegmentSpec) TimeBoundaryQuery(io.druid.query.timeboundary.TimeBoundaryQuery) DateTime(org.joda.time.DateTime) MapBasedRow(io.druid.data.input.MapBasedRow) TimeBoundaryResultValue(io.druid.query.timeboundary.TimeBoundaryResultValue) Map(java.util.Map) Interval(org.joda.time.Interval)

Example 12 with MapBasedRow

use of io.druid.data.input.MapBasedRow in project druid by druid-io.

the class HyperUniquesAggregationTest method testIngestAndQuery.

@Test
public void testIngestAndQuery() throws Exception {
    AggregationTestHelper helper = AggregationTestHelper.createGroupByQueryAggregationTestHelper(Lists.newArrayList(new AggregatorsModule()), config, tempFolder);
    String metricSpec = "[{" + "\"type\": \"hyperUnique\"," + "\"name\": \"index_hll\"," + "\"fieldName\": \"market\"" + "}]";
    String parseSpec = "{" + "\"type\" : \"string\"," + "\"parseSpec\" : {" + "    \"format\" : \"tsv\"," + "    \"timestampSpec\" : {" + "        \"column\" : \"timestamp\"," + "        \"format\" : \"auto\"" + "}," + "    \"dimensionsSpec\" : {" + "        \"dimensions\": []," + "        \"dimensionExclusions\" : []," + "        \"spatialDimensions\" : []" + "    }," + "    \"columns\": [\"timestamp\", \"market\", \"quality\", \"placement\", \"placementish\", \"index\"]" + "  }" + "}";
    String query = "{" + "\"queryType\": \"groupBy\"," + "\"dataSource\": \"test_datasource\"," + "\"granularity\": \"ALL\"," + "\"dimensions\": []," + "\"aggregations\": [" + "  { \"type\": \"hyperUnique\", \"name\": \"index_hll\", \"fieldName\": \"index_hll\" }" + "]," + "\"postAggregations\": [" + "  { \"type\": \"hyperUniqueCardinality\", \"name\": \"index_unique_count\", \"fieldName\": \"index_hll\" }" + "]," + "\"intervals\": [ \"1970/2050\" ]" + "}";
    Sequence seq = helper.createIndexAndRunQueryOnSegment(new File(this.getClass().getClassLoader().getResource("druid.sample.tsv").getFile()), parseSpec, metricSpec, 0, Granularities.NONE, 50000, query);
    MapBasedRow row = (MapBasedRow) Sequences.toList(seq, Lists.newArrayList()).get(0);
    Assert.assertEquals(3.0, row.getFloatMetric("index_hll"), 0.1);
    Assert.assertEquals(3.0, row.getFloatMetric("index_unique_count"), 0.1);
}
Also used : AggregatorsModule(io.druid.jackson.AggregatorsModule) MapBasedRow(io.druid.data.input.MapBasedRow) AggregationTestHelper(io.druid.query.aggregation.AggregationTestHelper) Sequence(io.druid.java.util.common.guava.Sequence) File(java.io.File) Test(org.junit.Test) GroupByQueryRunnerTest(io.druid.query.groupby.GroupByQueryRunnerTest)

Example 13 with MapBasedRow

use of io.druid.data.input.MapBasedRow in project druid by druid-io.

the class HyperUniquesAggregationTest method testIngestAndQueryPrecomputedHll.

@Test
public void testIngestAndQueryPrecomputedHll() throws Exception {
    AggregationTestHelper helper = AggregationTestHelper.createGroupByQueryAggregationTestHelper(Lists.newArrayList(new AggregatorsModule()), config, tempFolder);
    String metricSpec = "[{" + "\"type\": \"hyperUnique\"," + "\"name\": \"index_hll\"," + "\"fieldName\": \"preComputedHll\"," + "\"isInputHyperUnique\": true" + "}]";
    String parseSpec = "{" + "\"type\" : \"string\"," + "\"parseSpec\" : {" + "    \"format\" : \"tsv\"," + "    \"timestampSpec\" : {" + "        \"column\" : \"timestamp\"," + "        \"format\" : \"auto\"" + "}," + "    \"dimensionsSpec\" : {" + "        \"dimensions\": []," + "        \"dimensionExclusions\" : []," + "        \"spatialDimensions\" : []" + "    }," + "    \"columns\": [\"timestamp\", \"market\", \"preComputedHll\"]" + "  }" + "}";
    String query = "{" + "\"queryType\": \"groupBy\"," + "\"dataSource\": \"test_datasource\"," + "\"granularity\": \"ALL\"," + "\"dimensions\": []," + "\"aggregations\": [" + "  { \"type\": \"hyperUnique\", \"name\": \"index_hll\", \"fieldName\": \"index_hll\" }" + "]," + "\"postAggregations\": [" + "  { \"type\": \"hyperUniqueCardinality\", \"name\": \"index_unique_count\", \"fieldName\": \"index_hll\" }" + "]," + "\"intervals\": [ \"1970/2050\" ]" + "}";
    Sequence seq = helper.createIndexAndRunQueryOnSegment(new File(this.getClass().getClassLoader().getResource("druid.hll.sample.tsv").getFile()), parseSpec, metricSpec, 0, Granularities.DAY, 50000, query);
    MapBasedRow row = (MapBasedRow) Sequences.toList(seq, Lists.newArrayList()).get(0);
    Assert.assertEquals(4.0, row.getFloatMetric("index_hll"), 0.1);
    Assert.assertEquals(4.0, row.getFloatMetric("index_unique_count"), 0.1);
}
Also used : AggregatorsModule(io.druid.jackson.AggregatorsModule) MapBasedRow(io.druid.data.input.MapBasedRow) AggregationTestHelper(io.druid.query.aggregation.AggregationTestHelper) Sequence(io.druid.java.util.common.guava.Sequence) File(java.io.File) Test(org.junit.Test) GroupByQueryRunnerTest(io.druid.query.groupby.GroupByQueryRunnerTest)

Example 14 with MapBasedRow

use of io.druid.data.input.MapBasedRow in project druid by druid-io.

the class GroupByTimeseriesQueryRunnerTest method constructorFeeder.

@SuppressWarnings("unchecked")
@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> constructorFeeder() throws IOException {
    GroupByQueryConfig config = new GroupByQueryConfig();
    config.setMaxIntermediateRows(10000);
    final GroupByQueryRunnerFactory factory = GroupByQueryRunnerTest.makeQueryRunnerFactory(config);
    return QueryRunnerTestHelper.transformToConstructionFeeder(Lists.transform(QueryRunnerTestHelper.makeQueryRunners(factory), new Function<QueryRunner<Row>, Object>() {

        @Nullable
        @Override
        public Object apply(final QueryRunner<Row> input) {
            return new QueryRunner() {

                @Override
                public Sequence run(Query query, Map responseContext) {
                    TimeseriesQuery tsQuery = (TimeseriesQuery) query;
                    QueryRunner<Row> newRunner = factory.mergeRunners(MoreExecutors.sameThreadExecutor(), ImmutableList.<QueryRunner<Row>>of(input));
                    QueryToolChest toolChest = factory.getToolchest();
                    newRunner = new FinalizeResultsQueryRunner<>(toolChest.mergeResults(toolChest.preMergeQueryDecoration(newRunner)), toolChest);
                    return Sequences.map(newRunner.run(GroupByQuery.builder().setDataSource(tsQuery.getDataSource()).setQuerySegmentSpec(tsQuery.getQuerySegmentSpec()).setGranularity(tsQuery.getGranularity()).setDimFilter(tsQuery.getDimensionsFilter()).setAggregatorSpecs(tsQuery.getAggregatorSpecs()).setPostAggregatorSpecs(tsQuery.getPostAggregatorSpecs()).setVirtualColumns(tsQuery.getVirtualColumns()).build(), responseContext), new Function<Row, Result<TimeseriesResultValue>>() {

                        @Override
                        public Result<TimeseriesResultValue> apply(final Row input) {
                            MapBasedRow row = (MapBasedRow) input;
                            return new Result<TimeseriesResultValue>(row.getTimestamp(), new TimeseriesResultValue(row.getEvent()));
                        }
                    });
                }

                @Override
                public String toString() {
                    return input.toString();
                }
            };
        }
    }));
}
Also used : TimeseriesResultValue(io.druid.query.timeseries.TimeseriesResultValue) TimeseriesQuery(io.druid.query.timeseries.TimeseriesQuery) Query(io.druid.query.Query) TimeseriesQuery(io.druid.query.timeseries.TimeseriesQuery) QueryToolChest(io.druid.query.QueryToolChest) FinalizeResultsQueryRunner(io.druid.query.FinalizeResultsQueryRunner) QueryRunner(io.druid.query.QueryRunner) Result(io.druid.query.Result) MapBasedRow(io.druid.data.input.MapBasedRow) Function(com.google.common.base.Function) Row(io.druid.data.input.Row) MapBasedRow(io.druid.data.input.MapBasedRow) Map(java.util.Map)

Example 15 with MapBasedRow

use of io.druid.data.input.MapBasedRow in project druid by druid-io.

the class BufferGrouperTest method testGrowing.

@Test
public void testGrowing() {
    final TestColumnSelectorFactory columnSelectorFactory = GrouperTestUtil.newColumnSelectorFactory();
    final Grouper<Integer> grouper = makeGrouper(columnSelectorFactory, 10000, 2);
    final int expectedMaxSize = 219;
    columnSelectorFactory.setRow(new MapBasedRow(0, ImmutableMap.<String, Object>of("value", 10L)));
    for (int i = 0; i < expectedMaxSize; i++) {
        Assert.assertTrue(String.valueOf(i), grouper.aggregate(i));
    }
    Assert.assertFalse(grouper.aggregate(expectedMaxSize));
    // Aggregate slightly different row
    columnSelectorFactory.setRow(new MapBasedRow(0, ImmutableMap.<String, Object>of("value", 11L)));
    for (int i = 0; i < expectedMaxSize; i++) {
        Assert.assertTrue(String.valueOf(i), grouper.aggregate(i));
    }
    Assert.assertFalse(grouper.aggregate(expectedMaxSize));
    final List<Grouper.Entry<Integer>> expected = Lists.newArrayList();
    for (int i = 0; i < expectedMaxSize; i++) {
        expected.add(new Grouper.Entry<>(i, new Object[] { 21L, 2L }));
    }
    Assert.assertEquals(expected, Lists.newArrayList(grouper.iterator(true)));
}
Also used : MapBasedRow(io.druid.data.input.MapBasedRow) Test(org.junit.Test)

Aggregations

MapBasedRow (io.druid.data.input.MapBasedRow)34 Test (org.junit.Test)21 Row (io.druid.data.input.Row)16 GroupByQueryRunnerTest (io.druid.query.groupby.GroupByQueryRunnerTest)12 DateTime (org.joda.time.DateTime)11 File (java.io.File)9 Sequence (io.druid.java.util.common.guava.Sequence)7 Function (com.google.common.base.Function)6 MapBasedInputRow (io.druid.data.input.MapBasedInputRow)4 AggregationTestHelper (io.druid.query.aggregation.AggregationTestHelper)4 LongSumAggregatorFactory (io.druid.query.aggregation.LongSumAggregatorFactory)4 DimensionSpec (io.druid.query.dimension.DimensionSpec)4 List (java.util.List)4 Map (java.util.Map)4 Interval (org.joda.time.Interval)4 AggregatorsModule (io.druid.jackson.AggregatorsModule)3 AggregatorFactory (io.druid.query.aggregation.AggregatorFactory)3 SelectorDimFilter (io.druid.query.filter.SelectorDimFilter)3 GroupByQueryEngine (io.druid.query.groupby.GroupByQueryEngine)3 ISE (io.druid.java.util.common.ISE)2