Use hasValue() methods from aggregations' InspectionHelpers (#44745)

Use InspectionHelper classes to decide if the aggregations should return null (in case there is no value) or the value itself.

(cherry picked from commit dafd7b039b0da072750e8f57e7572d24f7aad44a)
This commit is contained in:
Andrei Stefan 2019-07-24 15:55:20 +03:00 committed by Andrei Stefan
parent 0418eb35a7
commit 04cb3aebd5
1 changed files with 32 additions and 16 deletions

View File

@ -10,12 +10,16 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.search.aggregations.InternalAggregation;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
import org.elasticsearch.search.aggregations.bucket.filter.InternalFilter;
import org.elasticsearch.search.aggregations.matrix.stats.MatrixStats;
import org.elasticsearch.search.aggregations.matrix.stats.InternalMatrixStats;
import org.elasticsearch.search.aggregations.metrics.InternalAvg;
import org.elasticsearch.search.aggregations.metrics.InternalCardinality;
import org.elasticsearch.search.aggregations.metrics.InternalMax;
import org.elasticsearch.search.aggregations.metrics.InternalMin;
import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation;
import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation.SingleValue;
import org.elasticsearch.search.aggregations.metrics.InternalStats;
import org.elasticsearch.search.aggregations.metrics.PercentileRanks;
import org.elasticsearch.search.aggregations.metrics.Percentiles;
import org.elasticsearch.search.aggregations.metrics.InternalSum;
import org.elasticsearch.search.aggregations.metrics.InternalTDigestPercentileRanks;
import org.elasticsearch.search.aggregations.metrics.InternalTDigestPercentiles;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.querydsl.agg.Aggs;
import org.elasticsearch.xpack.sql.util.DateUtils;
@ -25,6 +29,9 @@ import java.time.ZoneId;
import java.util.Map;
import java.util.Objects;
import static org.elasticsearch.search.aggregations.matrix.stats.MatrixAggregationInspectionHelper.hasValue;
import static org.elasticsearch.search.aggregations.support.AggregationInspectionHelper.hasValue;
public class MetricAggExtractor implements BucketExtractor {
static final String NAME = "m";
@ -123,26 +130,35 @@ public class MetricAggExtractor implements BucketExtractor {
/**
* Check if the given aggregate has been executed and has computed values
* or not (the bucket is null).
*
* Waiting on https://github.com/elastic/elasticsearch/issues/34903
*/
private static boolean containsValues(InternalAggregation agg) {
// Stats & ExtendedStats
if (agg instanceof InternalStats) {
return ((InternalStats) agg).getCount() != 0;
return hasValue((InternalStats) agg);
}
if (agg instanceof MatrixStats) {
return ((MatrixStats) agg).getDocCount() != 0;
if (agg instanceof InternalMatrixStats) {
return hasValue((InternalMatrixStats) agg);
}
// sum returns 0 even for null; since that's a common case, we return it as such
if (agg instanceof SingleValue) {
return Double.isFinite(((SingleValue) agg).value());
if (agg instanceof InternalMax) {
return hasValue((InternalMax) agg);
}
if (agg instanceof PercentileRanks) {
return Double.isFinite(((PercentileRanks) agg).percent(0));
if (agg instanceof InternalMin) {
return hasValue((InternalMin) agg);
}
if (agg instanceof Percentiles) {
return Double.isFinite(((Percentiles) agg).percentile(0));
if (agg instanceof InternalAvg) {
return hasValue((InternalAvg) agg);
}
if (agg instanceof InternalCardinality) {
return hasValue((InternalCardinality) agg);
}
if (agg instanceof InternalSum) {
return hasValue((InternalSum) agg);
}
if (agg instanceof InternalTDigestPercentileRanks) {
return hasValue((InternalTDigestPercentileRanks) agg);
}
if (agg instanceof InternalTDigestPercentiles) {
return hasValue((InternalTDigestPercentiles) agg);
}
return true;
}