Search in sources :

Example 1 with DoublesSketch

use of org.apache.datasketches.quantiles.DoublesSketch in project druid by druid-io.

the class DoublesSketchAggregatorFactory method makeAggregateCombiner.

@Override
public AggregateCombiner makeAggregateCombiner() {
    return new ObjectAggregateCombiner<DoublesSketch>() {

        private final DoublesUnion union = DoublesUnion.builder().setMaxK(k).build();

        @Override
        public void reset(final ColumnValueSelector selector) {
            union.reset();
            fold(selector);
        }

        @Override
        public void fold(final ColumnValueSelector selector) {
            final DoublesSketch sketch = (DoublesSketch) selector.getObject();
            union.update(sketch);
        }

        @Nullable
        @Override
        public DoublesSketch getObject() {
            return union.getResult();
        }

        @Override
        public Class<DoublesSketch> classOfObject() {
            return DoublesSketch.class;
        }
    };
}
Also used : DoublesSketch(org.apache.datasketches.quantiles.DoublesSketch) DoublesUnion(org.apache.datasketches.quantiles.DoublesUnion) ObjectAggregateCombiner(org.apache.druid.query.aggregation.ObjectAggregateCombiner) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) NilColumnValueSelector(org.apache.druid.segment.NilColumnValueSelector) BaseDoubleColumnValueSelector(org.apache.druid.segment.BaseDoubleColumnValueSelector)

Example 2 with DoublesSketch

use of org.apache.datasketches.quantiles.DoublesSketch in project druid by druid-io.

the class DoublesSketchComplexMetricSerde method deserializeColumn.

@Override
public void deserializeColumn(final ByteBuffer buffer, final ColumnBuilder builder) {
    final GenericIndexed<DoublesSketch> column = GenericIndexed.read(buffer, STRATEGY, builder.getFileMapper());
    builder.setComplexColumnSupplier(new ComplexColumnPartSupplier(getTypeName(), column));
}
Also used : DoublesSketch(org.apache.datasketches.quantiles.DoublesSketch) UpdateDoublesSketch(org.apache.datasketches.quantiles.UpdateDoublesSketch) ComplexColumnPartSupplier(org.apache.druid.segment.serde.ComplexColumnPartSupplier)

Example 3 with DoublesSketch

use of org.apache.datasketches.quantiles.DoublesSketch in project druid by druid-io.

the class DoublesSketchMergeVectorAggregator method aggregate.

@Override
public void aggregate(final ByteBuffer buf, final int numRows, final int[] positions, @Nullable final int[] rows, final int positionOffset) {
    final Object[] vector = selector.getObjectVector();
    DoublesSketches.handleMaxStreamLengthLimit(() -> {
        for (int i = 0; i < numRows; i++) {
            final DoublesSketch sketch = (DoublesSketch) vector[rows != null ? rows[i] : i];
            if (sketch != null) {
                final int position = positions[i] + positionOffset;
                final DoublesUnion union = helper.getSketchAtPosition(buf, position);
                union.update(sketch);
            }
        }
    });
}
Also used : DoublesSketch(org.apache.datasketches.quantiles.DoublesSketch) DoublesUnion(org.apache.datasketches.quantiles.DoublesUnion)

Example 4 with DoublesSketch

use of org.apache.datasketches.quantiles.DoublesSketch in project druid by druid-io.

the class DoublesSketchToHistogramPostAggregator method compute.

@Override
public Object compute(final Map<String, Object> combinedAggregators) {
    final DoublesSketch sketch = (DoublesSketch) field.compute(combinedAggregators);
    final int numBins = splitPoints != null ? splitPoints.length + 1 : (this.numBins != null ? this.numBins.intValue() : DEFAULT_NUM_BINS);
    if (numBins < 2) {
        throw new IAE("at least 2 bins expected");
    }
    if (sketch.isEmpty()) {
        final double[] histogram = new double[numBins];
        Arrays.fill(histogram, Double.NaN);
        return histogram;
    }
    final double[] histogram = sketch.getPMF(splitPoints != null ? splitPoints : equallySpacedPoints(numBins, sketch.getMinValue(), sketch.getMaxValue()));
    for (int i = 0; i < histogram.length; i++) {
        // scale fractions to counts
        histogram[i] *= sketch.getN();
    }
    return histogram;
}
Also used : DoublesSketch(org.apache.datasketches.quantiles.DoublesSketch) IAE(org.apache.druid.java.util.common.IAE)

Example 5 with DoublesSketch

use of org.apache.datasketches.quantiles.DoublesSketch in project druid by druid-io.

the class DoublesSketchToQuantilesPostAggregator method compute.

@Override
public Object compute(final Map<String, Object> combinedAggregators) {
    final DoublesSketch sketch = (DoublesSketch) field.compute(combinedAggregators);
    if (sketch.isEmpty()) {
        final double[] quantiles = new double[fractions.length];
        Arrays.fill(quantiles, Double.NaN);
        return quantiles;
    }
    return sketch.getQuantiles(fractions);
}
Also used : DoublesSketch(org.apache.datasketches.quantiles.DoublesSketch)

Aggregations

DoublesSketch (org.apache.datasketches.quantiles.DoublesSketch)24 Test (org.junit.Test)17 File (java.io.File)11 GroupByQueryRunnerTest (org.apache.druid.query.groupby.GroupByQueryRunnerTest)11 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)11 ResultRow (org.apache.druid.query.groupby.ResultRow)9 MapBasedInputRow (org.apache.druid.data.input.MapBasedInputRow)5 ComplexMetricExtractor (org.apache.druid.segment.serde.ComplexMetricExtractor)5 DoublesUnion (org.apache.datasketches.quantiles.DoublesUnion)3 Result (org.apache.druid.query.Result)2 TimeseriesResultValue (org.apache.druid.query.timeseries.TimeseriesResultValue)2 Comparator (java.util.Comparator)1 UpdateDoublesSketch (org.apache.datasketches.quantiles.UpdateDoublesSketch)1 ArrayOfDoublesUpdatableSketch (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketch)1 ArrayOfDoublesUpdatableSketchBuilder (org.apache.datasketches.tuple.arrayofdoubles.ArrayOfDoublesUpdatableSketchBuilder)1 IAE (org.apache.druid.java.util.common.IAE)1 ObjectAggregateCombiner (org.apache.druid.query.aggregation.ObjectAggregateCombiner)1 PostAggregator (org.apache.druid.query.aggregation.PostAggregator)1 ConstantPostAggregator (org.apache.druid.query.aggregation.post.ConstantPostAggregator)1 BaseDoubleColumnValueSelector (org.apache.druid.segment.BaseDoubleColumnValueSelector)1