Search in sources :

Example 1 with AllGranularity

use of io.druid.java.util.common.granularity.AllGranularity in project druid by druid-io.

the class SearchBinaryFn method apply.

@Override
public Result<SearchResultValue> apply(Result<SearchResultValue> arg1, Result<SearchResultValue> arg2) {
    if (arg1 == null) {
        return arg2;
    }
    if (arg2 == null) {
        return arg1;
    }
    final int limit = gran instanceof AllGranularity ? this.limit : -1;
    SearchResultValue arg1Vals = arg1.getValue();
    SearchResultValue arg2Vals = arg2.getValue();
    Iterable<SearchHit> merged = Iterables.mergeSorted(Arrays.asList(arg1Vals, arg2Vals), searchSortSpec.getComparator());
    int maxSize = arg1Vals.getValue().size() + arg2Vals.getValue().size();
    if (limit > 0) {
        maxSize = Math.min(limit, maxSize);
    }
    List<SearchHit> results = Lists.newArrayListWithExpectedSize(maxSize);
    SearchHit prev = null;
    for (SearchHit searchHit : merged) {
        if (prev == null) {
            prev = searchHit;
            continue;
        }
        if (prev.equals(searchHit)) {
            if (prev.getCount() != null && searchHit.getCount() != null) {
                prev = new SearchHit(prev.getDimension(), prev.getValue(), prev.getCount() + searchHit.getCount());
            } else {
                prev = new SearchHit(prev.getDimension(), prev.getValue());
            }
        } else {
            results.add(prev);
            prev = searchHit;
            if (limit > 0 && results.size() >= limit) {
                break;
            }
        }
    }
    if (prev != null && (limit < 0 || results.size() < limit)) {
        results.add(prev);
    }
    final DateTime timestamp = gran instanceof AllGranularity ? arg1.getTimestamp() : gran.bucketStart(arg1.getTimestamp());
    return new Result<SearchResultValue>(timestamp, new SearchResultValue(results));
}
Also used : SearchHit(io.druid.query.search.search.SearchHit) AllGranularity(io.druid.java.util.common.granularity.AllGranularity) DateTime(org.joda.time.DateTime) Result(io.druid.query.Result)

Example 2 with AllGranularity

use of io.druid.java.util.common.granularity.AllGranularity in project druid by druid-io.

the class SelectBinaryFn method apply.

@Override
public Result<SelectResultValue> apply(Result<SelectResultValue> arg1, Result<SelectResultValue> arg2) {
    if (arg1 == null) {
        return arg2;
    }
    if (arg2 == null) {
        return arg1;
    }
    final List<EventHolder> arg1Val = arg1.getValue().getEvents();
    final List<EventHolder> arg2Val = arg2.getValue().getEvents();
    if (arg1Val == null || arg1Val.isEmpty()) {
        return arg2;
    }
    if (arg2Val == null || arg2Val.isEmpty()) {
        return arg1;
    }
    final DateTime timestamp = (gran instanceof AllGranularity) ? arg1.getTimestamp() : gran.bucketStart(arg1.getTimestamp());
    SelectResultValueBuilder builder = new SelectResultValueBuilder.MergeBuilder(timestamp, pagingSpec, descending);
    builder.addDimensions(mergeColumns(arg1.getValue().getDimensions(), arg2.getValue().getDimensions()));
    builder.addMetrics(mergeColumns(arg1.getValue().getMetrics(), arg2.getValue().getMetrics()));
    for (EventHolder event : arg1Val) {
        builder.addEntry(event);
    }
    for (EventHolder event : arg2Val) {
        builder.addEntry(event);
    }
    return builder.build();
}
Also used : AllGranularity(io.druid.java.util.common.granularity.AllGranularity) DateTime(org.joda.time.DateTime)

Example 3 with AllGranularity

use of io.druid.java.util.common.granularity.AllGranularity in project druid by druid-io.

the class TimeseriesBinaryFn method apply.

@Override
public Result<TimeseriesResultValue> apply(Result<TimeseriesResultValue> arg1, Result<TimeseriesResultValue> arg2) {
    if (arg1 == null) {
        return arg2;
    }
    if (arg2 == null) {
        return arg1;
    }
    TimeseriesResultValue arg1Val = arg1.getValue();
    TimeseriesResultValue arg2Val = arg2.getValue();
    Map<String, Object> retVal = new LinkedHashMap<String, Object>();
    for (AggregatorFactory factory : aggregations) {
        final String metricName = factory.getName();
        retVal.put(metricName, factory.combine(arg1Val.getMetric(metricName), arg2Val.getMetric(metricName)));
    }
    return (gran instanceof AllGranularity) ? new Result<TimeseriesResultValue>(arg1.getTimestamp(), new TimeseriesResultValue(retVal)) : new Result<TimeseriesResultValue>(gran.bucketStart(arg1.getTimestamp()), new TimeseriesResultValue(retVal));
}
Also used : AllGranularity(io.druid.java.util.common.granularity.AllGranularity) AggregatorFactory(io.druid.query.aggregation.AggregatorFactory) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with AllGranularity

use of io.druid.java.util.common.granularity.AllGranularity in project druid by druid-io.

the class TopNBinaryFn method apply.

@Override
public Result<TopNResultValue> apply(Result<TopNResultValue> arg1, Result<TopNResultValue> arg2) {
    if (arg1 == null) {
        return merger.getResult(arg2, comparator);
    }
    if (arg2 == null) {
        return merger.getResult(arg1, comparator);
    }
    Map<Object, DimensionAndMetricValueExtractor> retVals = new LinkedHashMap<>();
    TopNResultValue arg1Vals = arg1.getValue();
    TopNResultValue arg2Vals = arg2.getValue();
    for (DimensionAndMetricValueExtractor arg1Val : arg1Vals) {
        retVals.put(arg1Val.getDimensionValue(dimension), arg1Val);
    }
    for (DimensionAndMetricValueExtractor arg2Val : arg2Vals) {
        final Object dimensionValue = arg2Val.getDimensionValue(dimension);
        DimensionAndMetricValueExtractor arg1Val = retVals.get(dimensionValue);
        if (arg1Val != null) {
            // size of map = aggregator + topNDim + postAgg (If sorting is done on post agg field)
            Map<String, Object> retVal = new LinkedHashMap<>(aggregations.size() + 2);
            retVal.put(dimension, dimensionValue);
            for (AggregatorFactory factory : aggregations) {
                final String metricName = factory.getName();
                retVal.put(metricName, factory.combine(arg1Val.getMetric(metricName), arg2Val.getMetric(metricName)));
            }
            for (PostAggregator pf : postAggregations) {
                retVal.put(pf.getName(), pf.compute(retVal));
            }
            retVals.put(dimensionValue, new DimensionAndMetricValueExtractor(retVal));
        } else {
            retVals.put(dimensionValue, arg2Val);
        }
    }
    final DateTime timestamp;
    if (gran instanceof AllGranularity) {
        timestamp = arg1.getTimestamp();
    } else {
        timestamp = gran.bucketStart(arg1.getTimestamp());
    }
    TopNResultBuilder bob = topNMetricSpec.getResultBuilder(timestamp, dimSpec, threshold, comparator, aggregations, postAggregations);
    for (DimensionAndMetricValueExtractor extractor : retVals.values()) {
        bob.addEntry(extractor);
    }
    return bob.build();
}
Also used : PostAggregator(io.druid.query.aggregation.PostAggregator) AllGranularity(io.druid.java.util.common.granularity.AllGranularity) AggregatorFactory(io.druid.query.aggregation.AggregatorFactory) DateTime(org.joda.time.DateTime) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

AllGranularity (io.druid.java.util.common.granularity.AllGranularity)4 DateTime (org.joda.time.DateTime)3 AggregatorFactory (io.druid.query.aggregation.AggregatorFactory)2 LinkedHashMap (java.util.LinkedHashMap)2 Result (io.druid.query.Result)1 PostAggregator (io.druid.query.aggregation.PostAggregator)1 SearchHit (io.druid.query.search.search.SearchHit)1