Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package org.elasticsearch.search.aggregations.metrics.percentiles;

public abstract class ParsedPercentileRanks extends AbstractParsedPercentiles implements PercentileRanks {
public abstract class ParsedPercentileRanks extends ParsedPercentiles implements PercentileRanks {

@Override
public double percent(double value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.LinkedHashMap;
import java.util.Map;

public abstract class AbstractParsedPercentiles extends ParsedAggregation implements Iterable<Percentile> {
public abstract class ParsedPercentiles extends ParsedAggregation implements Iterable<Percentile> {

private final Map<Double, Double> percentiles = new LinkedHashMap<>();
private final Map<Double, String> percentilesAsString = new HashMap<>();
Expand All @@ -46,14 +46,14 @@ void addPercentileAsString(Double key, String valueAsString) {
percentilesAsString.put(key, valueAsString);
}

Double getPercentile(double percent) {
protected Double getPercentile(double percent) {
if (percentiles.isEmpty()) {
return Double.NaN;
}
return percentiles.get(percent);
}

String getPercentileAsString(double percent) {
protected String getPercentileAsString(double percent) {
String valueAsString = percentilesAsString.get(percent);
if (valueAsString != null) {
return valueAsString;
Expand Down Expand Up @@ -119,7 +119,7 @@ protected XContentBuilder doXContentBody(XContentBuilder builder, Params params)
return builder;
}

protected static void declarePercentilesFields(ObjectParser<? extends AbstractParsedPercentiles, Void> objectParser) {
protected static void declarePercentilesFields(ObjectParser<? extends ParsedPercentiles, Void> objectParser) {
ParsedAggregation.declareAggregationFields(objectParser);

objectParser.declareField((parser, aggregation, context) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.aggregations.metrics.percentiles.AbstractParsedPercentiles;
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentiles;
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentileRanks;
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile;

Expand Down Expand Up @@ -55,7 +55,7 @@ public Percentile next() {
private static ObjectParser<ParsedHDRPercentileRanks, Void> PARSER =
new ObjectParser<>(ParsedHDRPercentileRanks.class.getSimpleName(), true, ParsedHDRPercentileRanks::new);
static {
AbstractParsedPercentiles.declarePercentilesFields(PARSER);
ParsedPercentiles.declarePercentilesFields(PARSER);
}

public static ParsedHDRPercentileRanks fromXContent(XContentParser parser, String name) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.search.aggregations.metrics.percentiles.hdr;

import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentiles;
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles;

import java.io.IOException;

public class ParsedHDRPercentiles extends ParsedPercentiles implements Percentiles {

@Override
protected String getType() {
return InternalHDRPercentiles.NAME;
}

@Override
public double percentile(double percent) {
return getPercentile(percent);
}

@Override
public String percentileAsString(double percent) {
return getPercentileAsString(percent);
}

private static ObjectParser<ParsedHDRPercentiles, Void> PARSER =
new ObjectParser<>(ParsedHDRPercentiles.class.getSimpleName(), true, ParsedHDRPercentiles::new);
static {
ParsedPercentiles.declarePercentilesFields(PARSER);
}

public static ParsedHDRPercentiles fromXContent(XContentParser parser, String name) throws IOException {
ParsedHDRPercentiles aggregation = PARSER.parse(parser, null);
aggregation.setName(name);
return aggregation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.aggregations.metrics.percentiles.AbstractParsedPercentiles;
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentiles;
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentileRanks;
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile;

Expand Down Expand Up @@ -55,7 +55,7 @@ public Percentile next() {
private static ObjectParser<ParsedTDigestPercentileRanks, Void> PARSER =
new ObjectParser<>(ParsedTDigestPercentileRanks.class.getSimpleName(), true, ParsedTDigestPercentileRanks::new);
static {
AbstractParsedPercentiles.declarePercentilesFields(PARSER);
ParsedPercentiles.declarePercentilesFields(PARSER);
}

public static ParsedTDigestPercentileRanks fromXContent(XContentParser parser, String name) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.search.aggregations.metrics.percentiles.tdigest;

import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.aggregations.metrics.percentiles.ParsedPercentiles;
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles;

import java.io.IOException;

public class ParsedTDigestPercentiles extends ParsedPercentiles implements Percentiles {

@Override
protected String getType() {
return InternalTDigestPercentiles.NAME;
}

@Override
public double percentile(double percent) {
return getPercentile(percent);
}

@Override
public String percentileAsString(double percent) {
return getPercentileAsString(percent);
}

private static ObjectParser<ParsedTDigestPercentiles, Void> PARSER =
new ObjectParser<>(ParsedTDigestPercentiles.class.getSimpleName(), true, ParsedTDigestPercentiles::new);
static {
ParsedPercentiles.declarePercentilesFields(PARSER);
}

public static ParsedTDigestPercentiles fromXContent(XContentParser parser, String name) throws IOException {
ParsedTDigestPercentiles aggregation = PARSER.parse(parser, null);
aggregation.setName(name);
return aggregation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.elasticsearch.rest.action.search.RestSearchAction;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.SearchModule;
import org.elasticsearch.search.aggregations.metrics.avg.AvgAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.avg.ParsedAvg;
Expand All @@ -44,9 +43,13 @@
import org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.min.ParsedMin;
import org.elasticsearch.search.aggregations.metrics.percentiles.hdr.InternalHDRPercentileRanks;
import org.elasticsearch.search.aggregations.metrics.percentiles.hdr.InternalHDRPercentiles;
import org.elasticsearch.search.aggregations.metrics.percentiles.hdr.ParsedHDRPercentileRanks;
import org.elasticsearch.search.aggregations.metrics.percentiles.hdr.ParsedHDRPercentiles;
import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.InternalTDigestPercentileRanks;
import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.InternalTDigestPercentiles;
import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.ParsedTDigestPercentileRanks;
import org.elasticsearch.search.aggregations.metrics.percentiles.tdigest.ParsedTDigestPercentiles;
import org.elasticsearch.search.aggregations.metrics.sum.ParsedSum;
import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.valuecount.ParsedValueCount;
Expand Down Expand Up @@ -82,7 +85,9 @@ public abstract class InternalAggregationTestCase<T extends InternalAggregation>
static List<NamedXContentRegistry.Entry> getNamedXContents() {
Map<String, ContextParser<Object, ? extends Aggregation>> namedXContents = new HashMap<>();
namedXContents.put(CardinalityAggregationBuilder.NAME, (p, c) -> ParsedCardinality.fromXContent(p, (String) c));
namedXContents.put(InternalHDRPercentiles.NAME, (p, c) -> ParsedHDRPercentiles.fromXContent(p, (String) c));
namedXContents.put(InternalHDRPercentileRanks.NAME, (p, c) -> ParsedHDRPercentileRanks.fromXContent(p, (String) c));
namedXContents.put(InternalTDigestPercentiles.NAME, (p, c) -> ParsedTDigestPercentiles.fromXContent(p, (String) c));
namedXContents.put(InternalTDigestPercentileRanks.NAME, (p, c) -> ParsedTDigestPercentileRanks.fromXContent(p, (String) c));
namedXContents.put(MinAggregationBuilder.NAME, (p, c) -> ParsedMin.fromXContent(p, (String) c));
namedXContents.put(MaxAggregationBuilder.NAME, (p, c) -> ParsedMax.fromXContent(p, (String) c));
Expand Down Expand Up @@ -189,7 +194,6 @@ protected NamedXContentRegistry xContentRegistry() {
}

public final void testFromXContent() throws IOException {
final NamedXContentRegistry xContentRegistry = xContentRegistry();
final T aggregation = createTestInstance();

//norelease Remove this assumption when all aggregations can be parsed back.
Expand All @@ -201,8 +205,33 @@ public final void testFromXContent() throws IOException {
final XContentType xContentType = randomFrom(XContentType.values());
final BytesReference originalBytes = toShuffledXContent(aggregation, xContentType, params, humanReadable);

final Aggregation parsedAggregation = parse(aggregation, xContentType, humanReadable, randomBoolean());

final BytesReference parsedBytes = toXContent((ToXContent) parsedAggregation, xContentType, params, humanReadable);
assertToXContentEquivalent(originalBytes, parsedBytes, xContentType);
assertFromXContent(aggregation, (ParsedAggregation) parsedAggregation);
}

//norelease TODO make abstract
protected void assertFromXContent(T aggregation, ParsedAggregation parsedAggregation) {
}

@SuppressWarnings("unchecked")
protected <P extends ParsedAggregation> P parse(final InternalAggregation aggregation,
final XContentType xContentType,
final boolean humanReadable,
final boolean shuffled) throws IOException {

final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
final BytesReference originalBytes;
if (shuffled) {
originalBytes = toShuffledXContent(aggregation, xContentType, params, humanReadable);
} else {
originalBytes = toXContent(aggregation, xContentType, params, humanReadable);
}

Aggregation parsedAggregation;
try (XContentParser parser = xContentType.xContent().createParser(xContentRegistry, originalBytes)) {
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());

Expand All @@ -222,15 +251,8 @@ public final void testFromXContent() throws IOException {

assertTrue(parsedAggregation instanceof ParsedAggregation);
assertEquals(aggregation.getType(), ((ParsedAggregation) parsedAggregation).getType());

final BytesReference parsedBytes = toXContent((ToXContent) parsedAggregation, xContentType, params, humanReadable);
assertToXContentEquivalent(originalBytes, parsedBytes, xContentType);
assertFromXContent(aggregation, (ParsedAggregation) parsedAggregation);
}
}

//norelease TODO make abstract
protected void assertFromXContent(T aggregation, ParsedAggregation parsedAggregation) {
return (P) parsedAggregation;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.search.aggregations.metrics.percentiles;

import com.carrotsearch.randomizedtesting.annotations.Repeat;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.InternalAggregation;
import org.elasticsearch.search.aggregations.InternalAggregationTestCase;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
import org.junit.Before;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public abstract class AbstractPercentilesTestCase<T extends InternalAggregation & Iterable<Percentile>>
extends InternalAggregationTestCase<T> {

private double[] percents;
private boolean keyed;
private DocValueFormat docValueFormat;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason these three fields need to be test instance members be randomized in the init() method? Otherwise I would prfer making them local in createTestIntstance().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When reducing percentiles aggregations it is important that all reduced aggregations have the same set of percentiles otherwise the reduce phase will just fail. Before adding the parsing method, the test used a fixed 0.5 percent but I think it's better to have one or more randomized percentiles.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I find it more rationale that all aggregations have the same format and keyed too, because it shouldn't be different from one or the other during the aggregation reducing phase.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that makes sense. I didn't think about the reducing tests.


@Before
public void init() {
percents = randomPercents();
keyed = randomBoolean();
docValueFormat = randomNumericDocValueFormat();
}

@Override
protected T createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
int numValues = randomInt(100);
double[] values = new double[numValues];
for (int i = 0; i < numValues; ++i) {
values[i] = randomDouble();
}
return createTestInstance(name, pipelineAggregators, metaData, keyed, docValueFormat, percents, values);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer percents/keyed/docValueFormat to be declared and instantiated here if there is no other reason I am missing atm.

}

protected abstract T createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData,
boolean keyed, DocValueFormat format, double[] percents, double[] values);

protected abstract Class<? extends ParsedPercentiles> implementationClass();

@Repeat(iterations = 1000)
public void testPercentilesIterators() throws IOException {
final T aggregation = createTestInstance();
final Iterable<Percentile> parsedAggregation = parse(aggregation, randomFrom(XContentType.values()), randomBoolean(), false);

Iterator<Percentile> it = aggregation.iterator();
Iterator<Percentile> parsedIt = parsedAggregation.iterator();
while (it.hasNext()) {
assertEquals(it.next(), parsedIt.next());
}
}

private static double[] randomPercents() {
List<Double> randomCdfValues = randomSubsetOf(randomIntBetween(1, 7), 0.01d, 0.05d, 0.25d, 0.50d, 0.75d, 0.95d, 0.99d);
double[] percents = new double[randomCdfValues.size()];
for (int i = 0; i < randomCdfValues.size(); i++) {
percents[i] = randomCdfValues.get(i);
}
return percents;
}
}
Loading