-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
MetricAggExtractor.java
145 lines (123 loc) · 4.81 KB
/
MetricAggExtractor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.execution.search.extractor;
import org.elasticsearch.common.io.stream.StreamInput;
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.matrix.stats.MatrixStats;
import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation;
import org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation.SingleValue;
import org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks;
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles;
import org.elasticsearch.search.aggregations.metrics.stats.InternalStats;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.querydsl.agg.Aggs;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
public class MetricAggExtractor implements BucketExtractor {
static final String NAME = "m";
private final String name;
private final String property;
private final String innerKey;
public MetricAggExtractor(String name, String property, String innerKey) {
this.name = name;
this.property = property;
this.innerKey = innerKey;
}
MetricAggExtractor(StreamInput in) throws IOException {
name = in.readString();
property = in.readString();
innerKey = in.readOptionalString();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(name);
out.writeString(property);
out.writeOptionalString(innerKey);
}
String name() {
return name;
}
String property() {
return property;
}
String innerKey() {
return innerKey;
}
@Override
public String getWriteableName() {
return NAME;
}
@Override
public Object extract(Bucket bucket) {
InternalAggregation agg = bucket.getAggregations().get(name);
if (agg == null) {
throw new SqlIllegalArgumentException("Cannot find an aggregation named {}", name);
}
if (!containsValues(agg)) {
return null;
}
if (agg instanceof InternalNumericMetricsAggregation.MultiValue) {
//TODO: need to investigate when this can be not-null
//if (innerKey == null) {
// throw new SqlIllegalArgumentException("Invalid innerKey {} specified for aggregation {}", innerKey, name);
//}
return ((InternalNumericMetricsAggregation.MultiValue) agg).value(property);
}
Object v = agg.getProperty(property);
return innerKey != null && v instanceof Map ? ((Map<?, ?>) v).get(innerKey) : v;
}
/**
* 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;
}
if (agg instanceof MatrixStats) {
return ((MatrixStats) agg).getDocCount() != 0;
}
// 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 PercentileRanks) {
return Double.isFinite(((PercentileRanks) agg).percent(0));
}
if (agg instanceof Percentiles) {
return Double.isFinite(((Percentiles) agg).percentile(0));
}
return true;
}
@Override
public int hashCode() {
return Objects.hash(name, property, innerKey);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
MetricAggExtractor other = (MetricAggExtractor) obj;
return Objects.equals(name, other.name)
&& Objects.equals(property, other.property)
&& Objects.equals(innerKey, other.innerKey);
}
@Override
public String toString() {
String i = innerKey != null ? "[" + innerKey + "]" : "";
return Aggs.ROOT_GROUP_NAME + ">" + name + "." + property + i;
}
}