Skip to content

Commit 8f8f43a

Browse files
committed
[ES|QL] Refactor AggregateMetricDoubleLiteral and adjust tests
1 parent a1f4ac3 commit 8f8f43a

File tree

30 files changed

+396
-309
lines changed

30 files changed

+396
-309
lines changed

x-pack/plugin/downsample/src/internalClusterTest/java/org/elasticsearch/xpack/downsample/DownsampleIT.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ public void testAggMetricInEsqlTSAfterDownsampling() throws Exception {
347347
}
348348
// tests on non-downsampled index
349349
// TODO: combine with above when support for aggregate_metric_double + implicit casting is added
350-
// TODO: add to counter tests below when support for counters is added
351350
for (String innerCommand : List.of("first_over_time", "last_over_time")) {
352351
String command = outerCommand + " (" + innerCommand + "(cpu))";
353352
try (var resp = esqlCommand("TS " + secondIndex + " | STATS " + command + " by cluster, bucket(@timestamp, 1 hour)")) {
@@ -368,7 +367,7 @@ public void testAggMetricInEsqlTSAfterDownsampling() throws Exception {
368367
}
369368

370369
// tests on counter types
371-
for (String innerCommand : List.of("rate")) {
370+
for (String innerCommand : List.of("rate", "first_over_time", "last_over_time")) {
372371
String command = outerCommand + " (" + innerCommand + "(request))";
373372
String esqlQuery = "TS " + dataStreamName + " | STATS " + command + " by cluster, bucket(@timestamp, 1 hour)";
374373
try (

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/AggregateMetricDoubleBlock.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,28 @@ static int hash(AggregateMetricDoubleBlock block) {
105105
IntBlock countBlock();
106106

107107
Block getMetricBlock(int index);
108+
109+
default AggregateMetricDoubleLiteral getAggregateMetricDoubleLiteral(int index) {
110+
boolean minAvailable = minBlock().isNull(index) == false;
111+
boolean maxAvailable = maxBlock().isNull(index) == false;
112+
boolean sumAvailable = sumBlock().isNull(index) == false;
113+
boolean countAvailable = countBlock().isNull(index) == false;
114+
double min = 0.0;
115+
double max = 0.0;
116+
double sum = 0.0;
117+
int count = 0;
118+
if (minAvailable) {
119+
min = minBlock().getDouble(index);
120+
}
121+
if (maxAvailable) {
122+
max = maxBlock().getDouble(index);
123+
}
124+
if (sumAvailable) {
125+
sum = sumBlock().getDouble(index);
126+
}
127+
if (countAvailable) {
128+
count = countBlock().getInt(index);
129+
}
130+
return new AggregateMetricDoubleLiteral(min, max, sum, count, minAvailable, maxAvailable, sumAvailable, countAvailable);
131+
}
108132
}

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/AggregateMetricDoubleBlockBuilder.java

Lines changed: 8 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,9 @@
77

88
package org.elasticsearch.compute.data;
99

10-
import org.elasticsearch.TransportVersion;
11-
import org.elasticsearch.common.io.stream.GenericNamedWriteable;
12-
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
13-
import org.elasticsearch.common.io.stream.StreamInput;
14-
import org.elasticsearch.common.io.stream.StreamOutput;
1510
import org.elasticsearch.core.Releasables;
1611
import org.elasticsearch.index.mapper.BlockLoader;
1712

18-
import java.io.IOException;
19-
2013
public class AggregateMetricDoubleBlockBuilder extends AbstractBlockBuilder implements BlockLoader.AggregateMetricDoubleBuilder {
2114

2215
private DoubleBlockBuilder minBuilder;
@@ -205,75 +198,24 @@ public String getLabel() {
205198
}
206199
}
207200

208-
/**
209-
* Literal to represent AggregateMetricDouble and primarily used for testing and during folding.
210-
* For all other purposes it is preferred to use the individual builders over the literal for generating blocks when possible.
211-
*/
212-
public record AggregateMetricDoubleLiteral(Double min, Double max, Double sum, Integer count) implements GenericNamedWriteable {
213-
214-
private static final TransportVersion ESQL_AGGREGATE_METRIC_DOUBLE_LITERAL = TransportVersion.fromName(
215-
"esql_aggregate_metric_double_literal"
216-
);
217-
218-
public AggregateMetricDoubleLiteral {
219-
min = (min == null || min.isNaN()) ? null : min;
220-
max = (max == null || max.isNaN()) ? null : max;
221-
sum = (sum == null || sum.isNaN()) ? null : sum;
222-
}
223-
224-
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
225-
GenericNamedWriteable.class,
226-
"AggregateMetricDoubleLiteral",
227-
AggregateMetricDoubleLiteral::new
228-
);
229-
230-
@Override
231-
public String getWriteableName() {
232-
return "AggregateMetricDoubleLiteral";
233-
}
234-
235-
public AggregateMetricDoubleLiteral(StreamInput input) throws IOException {
236-
this(input.readOptionalDouble(), input.readOptionalDouble(), input.readOptionalDouble(), input.readOptionalInt());
237-
}
238-
239-
@Override
240-
public void writeTo(StreamOutput out) throws IOException {
241-
out.writeOptionalDouble(min);
242-
out.writeOptionalDouble(max);
243-
out.writeOptionalDouble(sum);
244-
out.writeOptionalInt(count);
245-
}
246-
247-
@Override
248-
public boolean supportsVersion(TransportVersion version) {
249-
return version.supports(ESQL_AGGREGATE_METRIC_DOUBLE_LITERAL);
250-
}
251-
252-
@Override
253-
public TransportVersion getMinimalSupportedVersion() {
254-
assert false : "must not be called when overriding supportsVersion";
255-
throw new UnsupportedOperationException("must not be called when overriding supportsVersion");
256-
}
257-
}
258-
259201
public AggregateMetricDoubleBlockBuilder appendLiteral(AggregateMetricDoubleLiteral literal) {
260-
if (literal.min != null) {
261-
minBuilder.appendDouble(literal.min);
202+
if (literal.isMinAvailable()) {
203+
minBuilder.appendDouble(literal.getMin());
262204
} else {
263205
minBuilder.appendNull();
264206
}
265-
if (literal.max != null) {
266-
maxBuilder.appendDouble(literal.max);
207+
if (literal.isMaxAvailable()) {
208+
maxBuilder.appendDouble(literal.getMax());
267209
} else {
268210
maxBuilder.appendNull();
269211
}
270-
if (literal.sum != null) {
271-
sumBuilder.appendDouble(literal.sum);
212+
if (literal.isSumAvailable()) {
213+
sumBuilder.appendDouble(literal.getSum());
272214
} else {
273215
sumBuilder.appendNull();
274216
}
275-
if (literal.count != null) {
276-
countBuilder.appendInt(literal.count);
217+
if (literal.isCountAvailable()) {
218+
countBuilder.appendInt(literal.getCount());
277219
} else {
278220
countBuilder.appendNull();
279221
}
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.compute.data;
9+
10+
import org.elasticsearch.TransportVersion;
11+
import org.elasticsearch.common.io.stream.GenericNamedWriteable;
12+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
13+
import org.elasticsearch.common.io.stream.StreamInput;
14+
import org.elasticsearch.common.io.stream.StreamOutput;
15+
16+
import java.io.IOException;
17+
import java.util.Objects;
18+
19+
/**
20+
* Java Object representation of AggregateMetricDouble in ES|QL.
21+
*/
22+
public class AggregateMetricDoubleLiteral implements GenericNamedWriteable {
23+
private static final TransportVersion ESQL_AGGREGATE_METRIC_DOUBLE_LITERAL = TransportVersion.fromName(
24+
"esql_aggregate_metric_double_literal"
25+
);
26+
27+
private final double min;
28+
private final double max;
29+
private final double sum;
30+
private final int count;
31+
private final boolean minAvailable;
32+
private final boolean maxAvailable;
33+
private final boolean sumAvailable;
34+
private final boolean countAvailable;
35+
36+
public AggregateMetricDoubleLiteral(
37+
double min,
38+
double max,
39+
double sum,
40+
int count,
41+
boolean minAvailable,
42+
boolean maxAvailable,
43+
boolean sumAvailable,
44+
boolean countAvailable
45+
) {
46+
this.min = min;
47+
this.max = max;
48+
this.sum = sum;
49+
this.count = count;
50+
this.minAvailable = minAvailable;
51+
this.maxAvailable = maxAvailable;
52+
this.sumAvailable = sumAvailable;
53+
this.countAvailable = countAvailable;
54+
}
55+
56+
public AggregateMetricDoubleLiteral(double min, double max, double sum, int count) {
57+
this.min = min;
58+
this.max = max;
59+
this.sum = sum;
60+
this.count = count;
61+
this.minAvailable = true;
62+
this.maxAvailable = true;
63+
this.sumAvailable = true;
64+
this.countAvailable = true;
65+
}
66+
67+
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
68+
GenericNamedWriteable.class,
69+
"AggregateMetricDoubleLiteral",
70+
AggregateMetricDoubleLiteral::new
71+
);
72+
73+
@Override
74+
public String getWriteableName() {
75+
return "AggregateMetricDoubleLiteral";
76+
}
77+
78+
public AggregateMetricDoubleLiteral(StreamInput input) throws IOException {
79+
if (input.readBoolean()) {
80+
this.min = input.readDouble();
81+
this.minAvailable = true;
82+
} else {
83+
this.min = 0.0;
84+
this.minAvailable = false;
85+
}
86+
if (input.readBoolean()) {
87+
this.max = input.readDouble();
88+
this.maxAvailable = true;
89+
} else {
90+
this.max = 0.0;
91+
this.maxAvailable = false;
92+
}
93+
if (input.readBoolean()) {
94+
this.sum = input.readDouble();
95+
this.sumAvailable = true;
96+
} else {
97+
this.sum = 0.0;
98+
this.sumAvailable = false;
99+
}
100+
if (input.readBoolean()) {
101+
this.count = input.readInt();
102+
this.countAvailable = true;
103+
} else {
104+
this.count = 0;
105+
this.countAvailable = false;
106+
}
107+
}
108+
109+
@Override
110+
public void writeTo(StreamOutput out) throws IOException {
111+
if (minAvailable) {
112+
out.writeBoolean(true);
113+
out.writeDouble(min);
114+
} else {
115+
out.writeBoolean(false);
116+
}
117+
if (maxAvailable) {
118+
out.writeBoolean(true);
119+
out.writeDouble(max);
120+
} else {
121+
out.writeBoolean(false);
122+
}
123+
if (sumAvailable) {
124+
out.writeBoolean(true);
125+
out.writeDouble(sum);
126+
} else {
127+
out.writeBoolean(false);
128+
}
129+
if (countAvailable) {
130+
out.writeBoolean(true);
131+
out.writeInt(count);
132+
} else {
133+
out.writeBoolean(false);
134+
}
135+
}
136+
137+
@Override
138+
public boolean supportsVersion(TransportVersion version) {
139+
return version.supports(ESQL_AGGREGATE_METRIC_DOUBLE_LITERAL);
140+
}
141+
142+
@Override
143+
public TransportVersion getMinimalSupportedVersion() {
144+
assert false : "must not be called when overriding supportsVersion";
145+
throw new UnsupportedOperationException("must not be called when overriding supportsVersion");
146+
}
147+
148+
public double getMin() {
149+
return min;
150+
}
151+
152+
public double getMax() {
153+
return max;
154+
}
155+
156+
public double getSum() {
157+
return sum;
158+
}
159+
160+
public int getCount() {
161+
return count;
162+
}
163+
164+
public boolean isMinAvailable() {
165+
return minAvailable;
166+
}
167+
168+
public boolean isMaxAvailable() {
169+
return maxAvailable;
170+
}
171+
172+
public boolean isSumAvailable() {
173+
return sumAvailable;
174+
}
175+
176+
public boolean isCountAvailable() {
177+
return countAvailable;
178+
}
179+
180+
@Override
181+
public String toString() {
182+
return "AggregateMetricDoubleLiteral("
183+
+ (minAvailable ? "min: " + min + ", " : "")
184+
+ (maxAvailable ? "max: " + max + ", " : "")
185+
+ (sumAvailable ? "sum: " + sum + ", " : "")
186+
+ (countAvailable ? "count: " + count : "")
187+
+ ")";
188+
}
189+
190+
@Override
191+
public boolean equals(Object o) {
192+
if (o instanceof AggregateMetricDoubleLiteral == false) {
193+
return false;
194+
}
195+
var that = (AggregateMetricDoubleLiteral) o;
196+
return Double.compare(min, that.min) == 0
197+
&& Double.compare(max, that.max) == 0
198+
&& Double.compare(sum, that.sum) == 0
199+
&& count == that.count
200+
&& minAvailable == that.minAvailable
201+
&& maxAvailable == that.maxAvailable
202+
&& sumAvailable == that.sumAvailable
203+
&& countAvailable == that.countAvailable;
204+
}
205+
206+
@Override
207+
public int hashCode() {
208+
return Objects.hash(min, max, sum, count, minAvailable, maxAvailable, sumAvailable, countAvailable);
209+
}
210+
}

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/BlockFactory.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -436,32 +436,11 @@ public AggregateMetricDoubleBlockBuilder newAggregateMetricDoubleBlockBuilder(in
436436
return new AggregateMetricDoubleBlockBuilder(estimatedSize, this);
437437
}
438438

439-
public final AggregateMetricDoubleBlock newConstantAggregateMetricDoubleBlock(
440-
AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleLiteral value,
441-
int positions
442-
) {
439+
public final AggregateMetricDoubleBlock newConstantAggregateMetricDoubleBlock(AggregateMetricDoubleLiteral value, int positions) {
443440
try (AggregateMetricDoubleBlockBuilder builder = newAggregateMetricDoubleBlockBuilder(positions)) {
441+
// TODO: make ConstantAggregateMetricDoubleBlock
444442
for (int i = 0; i < positions; i++) {
445-
if (value.min() != null) {
446-
builder.min().appendDouble(value.min());
447-
} else {
448-
builder.min().appendNull();
449-
}
450-
if (value.max() != null) {
451-
builder.max().appendDouble(value.max());
452-
} else {
453-
builder.max().appendNull();
454-
}
455-
if (value.sum() != null) {
456-
builder.sum().appendDouble(value.sum());
457-
} else {
458-
builder.sum().appendNull();
459-
}
460-
if (value.count() != null) {
461-
builder.count().appendInt(value.count());
462-
} else {
463-
builder.count().appendNull();
464-
}
443+
builder.appendLiteral(value);
465444
}
466445
return builder.build();
467446
}

0 commit comments

Comments
 (0)