private static List<Double> generatePoints(int maxPoints, int rounding, Map<String, Object> attributes) { Number start = Utilities.getCasted(attributes, RANGE_START, Number.class); Number end = Utilities.getCasted(attributes, RANGE_END, Number.class); Number increment = Utilities.getCasted(attributes, RANGE_INCREMENT, Number.class); if (!areNumbersValid(start, end, increment)) { return Collections.emptyList(); } Double from = start.doubleValue(); Double to = end.doubleValue(); Double by = increment.doubleValue(); List<Double> points = new ArrayList<>(); for (int i = 0; i < maxPoints && from <= to; ++i) { points.add(Utilities.round(from, rounding)); from += by; } return points; }
private static QuantileSketch getSketch(int entries, int maxPoints, int rounding, Type type, Map<String, Object> attributes, BulletRecordProvider provider) { int equidistantPoints = getNumberOfEquidistantPoints(attributes); if (equidistantPoints > 0) { return new QuantileSketch(entries, rounding, type, Math.min(equidistantPoints, maxPoints), provider); } List<Double> points = getProvidedPoints(attributes); if (Utilities.isEmpty(points)) { points = generatePoints(maxPoints, rounding, attributes); } // If still not good, return null if (Utilities.isEmpty(points)) { return null; } // Sort and get first maxPoints distinct values double[] cleanedPoints = points.stream().distinct().sorted().limit(maxPoints) .mapToDouble(d -> d).toArray(); if (invalidBounds(type, cleanedPoints)) { return null; } return new QuantileSketch(entries, type, cleanedPoints, provider); }
@Override public Optional<List<BulletError>> initialize() { if (Utilities.isEmpty(fields) || fields.size() != 1) { return Optional.of(singletonList(REQUIRES_ONE_FIELD_ERROR)); } Map<String, Object> attributes = aggregation.getAttributes(); if (Utilities.isEmpty(attributes)) { return Optional.of(singletonList(REQUIRES_TYPE_ERROR)); } String typeString = Utilities.getCasted(attributes, TYPE, String.class); Type type = SUPPORTED_DISTRIBUTION_TYPES.get(typeString); if (type == null) { return Optional.of(singletonList(REQUIRES_TYPE_ERROR)); } // Try to initialize sketch now sketch = getSketch(entries, maxPoints, rounding, type, attributes, provider); if (sketch == null) { return Optional.of(type == Type.QUANTILE ? asList(REQUIRES_POINTS_ERROR, REQUIRES_POINTS_PROPER_RANGE) : singletonList(REQUIRES_POINTS_ERROR)); } // Initialize field since we have exactly 1 field = fields.get(0); return Optional.empty(); }
distribution.combine(rawData); List<BulletRecord> records = distribution.getRecords();
/** * Returns a new {@link Strategy} instance that can handle this aggregation. * * @param aggregation The non-null, initialized {@link Aggregation} instance. * @param config The {@link BulletConfig} containing configuration for the strategy. * * @return The created instance of a strategy that can implement the Aggregation. */ public static Strategy findStrategy(Aggregation aggregation, BulletConfig config) { // Guaranteed to be present. switch (aggregation.getType()) { case COUNT_DISTINCT: return new CountDistinct(aggregation, config); case DISTRIBUTION: return new Distribution(aggregation, config); case RAW: return new Raw(aggregation, config); case TOP_K: return new TopK(aggregation, config); } // If we have any fields -> GroupBy return Utilities.isEmpty(aggregation.getFields()) ? new GroupAll(aggregation, config) : new GroupBy(aggregation, config); } }