Skip to content

Commit a229fd2

Browse files
authored
[ES|QL] Register AggregateMetricDoubleLiteral (#135054)
This literal was not previously being registered because it was primarily being used in testing code, but when aggregate_metric_double is folded it is used as the java object representation, so it requires registering.
1 parent 9b9e665 commit a229fd2

File tree

8 files changed

+88
-6
lines changed

8 files changed

+88
-6
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ public String getLabel() {
206206
}
207207
}
208208

209+
/**
210+
* Literal to represent AggregateMetricDouble and primarily used for testing and during folding.
211+
* For all other purposes it is preferred to use the individual builders over the literal for generating blocks when possible.
212+
*/
209213
public record AggregateMetricDoubleLiteral(Double min, Double max, Double sum, Integer count) implements GenericNamedWriteable {
210214
public AggregateMetricDoubleLiteral {
211215
min = (min == null || min.isNaN()) ? null : min;
@@ -247,4 +251,28 @@ public TransportVersion getMinimalSupportedVersion() {
247251
throw new UnsupportedOperationException("must not be called when overriding supportsVersion");
248252
}
249253
}
254+
255+
public AggregateMetricDoubleBlockBuilder appendLiteral(AggregateMetricDoubleLiteral literal) {
256+
if (literal.min != null) {
257+
minBuilder.appendDouble(literal.min);
258+
} else {
259+
minBuilder.appendNull();
260+
}
261+
if (literal.max != null) {
262+
maxBuilder.appendDouble(literal.max);
263+
} else {
264+
maxBuilder.appendNull();
265+
}
266+
if (literal.sum != null) {
267+
sumBuilder.appendDouble(literal.sum);
268+
} else {
269+
sumBuilder.appendNull();
270+
}
271+
if (literal.count != null) {
272+
countBuilder.appendInt(literal.count);
273+
} else {
274+
countBuilder.appendNull();
275+
}
276+
return this;
277+
}
250278
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ public static void appendValue(Block.Builder builder, Object val, ElementType ty
217217
case FLOAT -> ((FloatBlock.Builder) builder).appendFloat((Float) val);
218218
case DOUBLE -> ((DoubleBlock.Builder) builder).appendDouble((Double) val);
219219
case BOOLEAN -> ((BooleanBlock.Builder) builder).appendBoolean((Boolean) val);
220+
case AGGREGATE_METRIC_DOUBLE -> ((AggregateMetricDoubleBlockBuilder) builder).appendLiteral((AggregateMetricDoubleLiteral) val);
220221
default -> throw new UnsupportedOperationException("unsupported element type [" + type + "]");
221222
}
222223
}

x-pack/plugin/esql/qa/testFixtures/src/main/java/org/elasticsearch/xpack/esql/CsvTestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void append(String stringValue) {
143143
return;
144144
}
145145
stringValue = mvStrings[0].replace(ESCAPED_COMMA_SEQUENCE, ",");
146-
} else if (stringValue.contains(",")) {// multi-value field
146+
} else if (stringValue.contains(",") && type != Type.AGGREGATE_METRIC_DOUBLE) {// multi-value field
147147
builderWrapper().builder().beginPositionEntry();
148148

149149
String[] arrayOfValues = delimitedListToStringArray(stringValue, ",");

x-pack/plugin/esql/qa/testFixtures/src/main/resources/inlinestats.csv-spec

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3913,3 +3913,26 @@ from employees
39133913
10003 |0 |null
39143914
;
39153915

3916+
inlineStatsOnAggregateMetricDouble
3917+
required_capability: inline_stats
3918+
required_capability: aggregate_metric_double_literal_registered
3919+
required_capability: aggregate_metric_double_convert_to
3920+
FROM k8s-downsampled
3921+
| EVAL a = TO_AGGREGATE_METRIC_DOUBLE(1) // Temporary workaround to enable aggregate_metric_double
3922+
| INLINE STATS tx_max = MAX(network.eth0.tx) BY pod
3923+
| SORT @timestamp, cluster, pod
3924+
| KEEP @timestamp, cluster, pod, network.eth0.tx, tx_max
3925+
| LIMIT 9
3926+
;
3927+
3928+
@timestamp:datetime | cluster:keyword | pod:keyword | network.eth0.tx:aggregate_metric_double | tx_max:double
3929+
2024-05-09T23:30:00.000Z | prod | one | {"min":565.0,"max":829.0,"sum":7290.0,"value_count":10} | 1060.0
3930+
2024-05-09T23:30:00.000Z | prod | three | {"min":201.0,"max":582.0,"sum":1794.0,"value_count":6} | 824.0
3931+
2024-05-09T23:30:00.000Z | prod | two | {"min":20.0,"max":190.0,"sum":370.0,"value_count":10} | 1419.0
3932+
2024-05-09T23:30:00.000Z | qa | one | {"min":346.0,"max":356.0,"sum":1765.0,"value_count":5} | 1060.0
3933+
2024-05-09T23:30:00.000Z | qa | three | {"min":605.0,"max":605.0,"sum":605.0,"value_count":1} | 824.0
3934+
2024-05-09T23:30:00.000Z | qa | two | {"min":304.0,"max":1148.0,"sum":8590.0,"value_count":10} | 1419.0
3935+
2024-05-09T23:30:00.000Z | staging | one | {"min":263.0,"max":740.0,"sum":5390.0,"value_count":10} | 1060.0
3936+
2024-05-09T23:30:00.000Z | staging | three | {"min":341.0,"max":592.0,"sum":1956.0,"value_count":5} | 824.0
3937+
2024-05-09T23:30:00.000Z | staging | two | {"min":442.0,"max":1011.0,"sum":3850.0,"value_count":7} | 1419.0
3938+
;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,11 @@ public enum Cap {
976976
*/
977977
AGGREGATE_METRIC_DOUBLE_MV_EXPAND(AGGREGATE_METRIC_DOUBLE_FEATURE_FLAG),
978978

979+
/**
980+
* Registering AggregateMetricDoubleLiteral as a NamedWritable.
981+
*/
982+
AGGREGATE_METRIC_DOUBLE_LITERAL_REGISTERED(AGGREGATE_METRIC_DOUBLE_FEATURE_FLAG),
983+
979984
/**
980985
* Support change point detection "CHANGE_POINT".
981986
*/

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/ExpressionWritables.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
package org.elasticsearch.xpack.esql.expression;
99

1010
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
11+
import org.elasticsearch.compute.data.AggregateMetricDoubleBlockBuilder;
12+
import org.elasticsearch.plugins.SearchPlugin;
1113
import org.elasticsearch.xpack.esql.action.EsqlCapabilities;
1214
import org.elasticsearch.xpack.esql.core.expression.ExpressionCoreWritables;
1315
import org.elasticsearch.xpack.esql.expression.function.UnsupportedAttribute;
@@ -124,6 +126,12 @@ public static List<NamedWriteableRegistry.Entry> getNamedWriteables() {
124126
return entries;
125127
}
126128

129+
public static List<SearchPlugin.GenericNamedWriteableSpec> getGenericNamedWriteables() {
130+
List<SearchPlugin.GenericNamedWriteableSpec> entries = new ArrayList<>();
131+
entries.addAll(literals());
132+
return entries;
133+
}
134+
127135
public static List<NamedWriteableRegistry.Entry> attributes() {
128136
List<NamedWriteableRegistry.Entry> entries = new ArrayList<>();
129137
entries.addAll(ExpressionCoreWritables.attributes());
@@ -267,4 +275,13 @@ private static List<NamedWriteableRegistry.Entry> fullText() {
267275
private static List<NamedWriteableRegistry.Entry> vector() {
268276
return VectorWritables.getNamedWritables();
269277
}
278+
279+
public static List<SearchPlugin.GenericNamedWriteableSpec> literals() {
280+
return List.of(
281+
new SearchPlugin.GenericNamedWriteableSpec(
282+
"AggregateMetricDoubleLiteral",
283+
AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleLiteral::new
284+
)
285+
);
286+
}
270287
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plugin/EsqlPlugin.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.elasticsearch.plugins.ActionPlugin;
4343
import org.elasticsearch.plugins.ExtensiblePlugin;
4444
import org.elasticsearch.plugins.Plugin;
45+
import org.elasticsearch.plugins.SearchPlugin;
4546
import org.elasticsearch.rest.RestController;
4647
import org.elasticsearch.rest.RestHandler;
4748
import org.elasticsearch.threadpool.ExecutorBuilder;
@@ -90,7 +91,7 @@
9091
import java.util.function.Predicate;
9192
import java.util.function.Supplier;
9293

93-
public class EsqlPlugin extends Plugin implements ActionPlugin, ExtensiblePlugin {
94+
public class EsqlPlugin extends Plugin implements ActionPlugin, ExtensiblePlugin, SearchPlugin {
9495

9596
public static final String ESQL_WORKER_THREAD_POOL_NAME = "esql_worker";
9697

@@ -374,4 +375,9 @@ public List<ExecutorBuilder<?>> getExecutorBuilders(Settings settings) {
374375
public void loadExtensions(ExtensionLoader loader) {
375376
extraCheckerProviders.addAll(loader.loadExtensions(PlanCheckerProvider.class));
376377
}
378+
379+
@Override
380+
public List<SearchPlugin.GenericNamedWriteableSpec> getGenericNamedWriteables() {
381+
return ExpressionWritables.getGenericNamedWriteables();
382+
}
377383
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/type/EsqlDataTypeConverter.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -813,22 +813,24 @@ public static AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleLiteral str
813813
Double max = null;
814814
Double sum = null;
815815
Integer count = null;
816+
817+
s = s.replace("\\,", ",");
816818
String[] values = s.substring(1, s.length() - 1).split(",");
817819
for (String v : values) {
818820
var pair = v.split(":");
819821
String type = pair[0];
820822
String number = pair[1];
821823
switch (type) {
822-
case "min":
824+
case "min", "\"min\"":
823825
min = Double.parseDouble(number);
824826
break;
825-
case "max":
827+
case "max", "\"max\"":
826828
max = Double.parseDouble(number);
827829
break;
828-
case "sum":
830+
case "sum", "\"sum\"":
829831
sum = Double.parseDouble(number);
830832
break;
831-
case "value_count":
833+
case "value_count", "\"value_count\"":
832834
count = Integer.parseInt(number);
833835
break;
834836
default:

0 commit comments

Comments
 (0)