Search in sources :

Example 1 with MetricManipulationFn

use of org.apache.druid.query.aggregation.MetricManipulationFn in project druid by druid-io.

the class GroupByQueryQueryToolChest method makePostComputeManipulatorFn.

@Override
public Function<ResultRow, ResultRow> makePostComputeManipulatorFn(final GroupByQuery query, final MetricManipulationFn fn) {
    final BitSet optimizedDims = extractionsToRewrite(query);
    final Function<ResultRow, ResultRow> preCompute = makePreComputeManipulatorFn(query, fn);
    if (optimizedDims.isEmpty()) {
        return preCompute;
    }
    // If we have optimizations that can be done at this level, we apply them here
    final List<DimensionSpec> dimensions = query.getDimensions();
    final List<ExtractionFn> extractionFns = new ArrayList<>(dimensions.size());
    for (int i = 0; i < dimensions.size(); i++) {
        final DimensionSpec dimensionSpec = dimensions.get(i);
        final ExtractionFn extractionFnToAdd;
        if (optimizedDims.get(i)) {
            extractionFnToAdd = dimensionSpec.getExtractionFn();
        } else {
            extractionFnToAdd = null;
        }
        extractionFns.add(extractionFnToAdd);
    }
    final int dimensionStart = query.getResultRowDimensionStart();
    return row -> {
        // preCompute.apply(row) will either return the original row, or create a copy.
        ResultRow newRow = preCompute.apply(row);
        // noinspection ObjectEquality (if preCompute made a copy, no need to make another copy)
        if (newRow == row) {
            newRow = row.copy();
        }
        for (int i = optimizedDims.nextSetBit(0); i >= 0; i = optimizedDims.nextSetBit(i + 1)) {
            newRow.set(dimensionStart + i, extractionFns.get(i).apply(newRow.get(dimensionStart + i)));
        }
        return newRow;
    };
}
Also used : QueryPlus(org.apache.druid.query.QueryPlus) ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) Inject(com.google.inject.Inject) DimensionHandlerUtils(org.apache.druid.segment.DimensionHandlerUtils) CacheKeyBuilder(org.apache.druid.query.cache.CacheKeyBuilder) Row(org.apache.druid.data.input.Row) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) Map(java.util.Map) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) QueryRunner(org.apache.druid.query.QueryRunner) SubqueryQueryRunner(org.apache.druid.query.SubqueryQueryRunner) TypeReference(com.fasterxml.jackson.core.type.TypeReference) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) GroupByStrategySelector(org.apache.druid.query.groupby.strategy.GroupByStrategySelector) GroupByStrategy(org.apache.druid.query.groupby.strategy.GroupByStrategy) Sequence(org.apache.druid.java.util.common.guava.Sequence) Function(com.google.common.base.Function) ImmutableMap(com.google.common.collect.ImmutableMap) DataSource(org.apache.druid.query.DataSource) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) MappedSequence(org.apache.druid.java.util.common.guava.MappedSequence) ISE(org.apache.druid.java.util.common.ISE) QueryContexts(org.apache.druid.query.QueryContexts) BinaryOperator(java.util.function.BinaryOperator) QueryDataSource(org.apache.druid.query.QueryDataSource) List(java.util.List) DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) Granularity(org.apache.druid.java.util.common.granularity.Granularity) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) MetricManipulatorFns(org.apache.druid.query.aggregation.MetricManipulatorFns) GroupByQueryResource(org.apache.druid.query.groupby.resource.GroupByQueryResource) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Query(org.apache.druid.query.Query) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) MetricManipulationFn(org.apache.druid.query.aggregation.MetricManipulationFn) Sequences(org.apache.druid.java.util.common.guava.Sequences) Functions(com.google.common.base.Functions) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser) Iterator(java.util.Iterator) ResponseContext(org.apache.druid.query.context.ResponseContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) QueryToolChest(org.apache.druid.query.QueryToolChest) IOException(java.io.IOException) CacheStrategy(org.apache.druid.query.CacheStrategy) TreeMap(java.util.TreeMap) RowSignature(org.apache.druid.segment.column.RowSignature) VisibleForTesting(com.google.common.annotations.VisibleForTesting) BitSet(java.util.BitSet) Comparator(java.util.Comparator) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) ExtractionFn(org.apache.druid.query.extraction.ExtractionFn) BitSet(java.util.BitSet) ArrayList(java.util.ArrayList)

Example 2 with MetricManipulationFn

use of org.apache.druid.query.aggregation.MetricManipulationFn in project druid by druid-io.

the class MaterializedViewQueryQueryToolChestTest method testMakePostComputeManipulatorFn.

@Test
public void testMakePostComputeManipulatorFn() {
    TimeseriesQuery realQuery = Druids.newTimeseriesQueryBuilder().dataSource(QueryRunnerTestHelper.DATA_SOURCE).granularity(QueryRunnerTestHelper.DAY_GRAN).intervals(QueryRunnerTestHelper.FULL_ON_INTERVAL_SPEC).aggregators(QueryRunnerTestHelper.ROWS_COUNT).descending(true).build();
    MaterializedViewQuery materializedViewQuery = new MaterializedViewQuery(realQuery, null);
    QueryToolChest materializedViewQueryQueryToolChest = new MaterializedViewQueryQueryToolChest(new MapQueryToolChestWarehouse(ImmutableMap.<Class<? extends Query>, QueryToolChest>builder().put(TimeseriesQuery.class, new TimeseriesQueryQueryToolChest()).build()));
    Function postFn = materializedViewQueryQueryToolChest.makePostComputeManipulatorFn(materializedViewQuery, new MetricManipulationFn() {

        @Override
        public Object manipulate(AggregatorFactory factory, Object object) {
            return "metricvalue1";
        }
    });
    Result<TimeseriesResultValue> result = new Result<>(DateTimes.nowUtc(), new TimeseriesResultValue(ImmutableMap.of("dim1", "dimvalue1")));
    Result<TimeseriesResultValue> postResult = (Result<TimeseriesResultValue>) postFn.apply(result);
    Map<String, Object> postResultMap = postResult.getValue().getBaseObject();
    Assert.assertEquals(postResult.getTimestamp(), result.getTimestamp());
    Assert.assertEquals(postResultMap.size(), 2);
    Assert.assertEquals(postResultMap.get(QueryRunnerTestHelper.ROWS_COUNT.getName()), "metricvalue1");
    Assert.assertEquals(postResultMap.get("dim1"), "dimvalue1");
}
Also used : TimeseriesResultValue(org.apache.druid.query.timeseries.TimeseriesResultValue) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) Query(org.apache.druid.query.Query) GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) TimeseriesQueryQueryToolChest(org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest) QueryToolChest(org.apache.druid.query.QueryToolChest) GroupByQueryQueryToolChest(org.apache.druid.query.groupby.GroupByQueryQueryToolChest) TimeseriesQueryQueryToolChest(org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) Result(org.apache.druid.query.Result) Function(com.google.common.base.Function) MapQueryToolChestWarehouse(org.apache.druid.query.MapQueryToolChestWarehouse) MetricManipulationFn(org.apache.druid.query.aggregation.MetricManipulationFn) Test(org.junit.Test)

Example 3 with MetricManipulationFn

use of org.apache.druid.query.aggregation.MetricManipulationFn in project druid by druid-io.

the class FinalizeResultsQueryRunner method run.

@Override
public Sequence<T> run(final QueryPlus<T> queryPlus, ResponseContext responseContext) {
    final Query<T> query = queryPlus.getQuery();
    final boolean isBySegment = QueryContexts.isBySegment(query);
    final boolean shouldFinalize = QueryContexts.isFinalize(query, true);
    final Query<T> queryToRun;
    final Function<T, ?> finalizerFn;
    final MetricManipulationFn metricManipulationFn;
    if (shouldFinalize) {
        queryToRun = query.withOverriddenContext(ImmutableMap.of("finalize", false));
        metricManipulationFn = MetricManipulatorFns.finalizing();
    } else {
        queryToRun = query;
        metricManipulationFn = MetricManipulatorFns.identity();
    }
    if (isBySegment) {
        finalizerFn = new Function<T, Result<BySegmentResultValue<T>>>() {

            final Function<T, T> baseFinalizer = toolChest.makePostComputeManipulatorFn(query, metricManipulationFn);

            @Override
            public Result<BySegmentResultValue<T>> apply(T input) {
                // noinspection unchecked (input is not actually a T; see class-level javadoc)
                Result<BySegmentResultValueClass<T>> result = (Result<BySegmentResultValueClass<T>>) input;
                if (input == null) {
                    throw new ISE("Cannot have a null result!");
                }
                BySegmentResultValue<T> resultsClass = result.getValue();
                return new Result<>(result.getTimestamp(), new BySegmentResultValueClass<>(Lists.transform(resultsClass.getResults(), baseFinalizer), resultsClass.getSegmentId(), resultsClass.getInterval()));
            }
        };
    } else {
        finalizerFn = toolChest.makePostComputeManipulatorFn(query, metricManipulationFn);
    }
    // noinspection unchecked (Technically unsound, but see class-level javadoc for rationale)
    return (Sequence<T>) Sequences.map(baseRunner.run(queryPlus.withQuery(queryToRun), responseContext), finalizerFn);
}
Also used : Sequence(org.apache.druid.java.util.common.guava.Sequence) MetricManipulationFn(org.apache.druid.query.aggregation.MetricManipulationFn) ISE(org.apache.druid.java.util.common.ISE)

Aggregations

MetricManipulationFn (org.apache.druid.query.aggregation.MetricManipulationFn)3 Function (com.google.common.base.Function)2 ISE (org.apache.druid.java.util.common.ISE)2 Sequence (org.apache.druid.java.util.common.guava.Sequence)2 Query (org.apache.druid.query.Query)2 QueryToolChest (org.apache.druid.query.QueryToolChest)2 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Functions (com.google.common.base.Functions)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1