Skip to content

Commit 2f64c49

Browse files
committed
ToAggregateMetricDouble function
1 parent 734dd07 commit 2f64c49

File tree

13 files changed

+642
-8
lines changed

13 files changed

+642
-8
lines changed

server/src/main/java/org/elasticsearch/TransportVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ static TransportVersion def(int id) {
185185
public static final TransportVersion ESQL_THREAD_NAME_IN_DRIVER_PROFILE = def(9_027_0_00);
186186
public static final TransportVersion INFERENCE_CONTEXT = def(9_028_0_00);
187187
public static final TransportVersion ML_INFERENCE_DEEPSEEK = def(9_029_00_0);
188+
public static final TransportVersion ESQL_AGGREGATE_METRIC_DOUBLE_LITERAL = def(9_030_0_00);
188189

189190
/*
190191
* STOP! READ THIS FIRST! No, really,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,6 @@ public static boolean isRepresentable(DataType t) {
557557
&& t != SOURCE
558558
&& t != HALF_FLOAT
559559
&& t != PARTIAL_AGG
560-
&& t != AGGREGATE_METRIC_DOUBLE
561560
&& t.isCounter() == false;
562561
}
563562

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@
77

88
package org.elasticsearch.compute.data;
99

10+
import org.elasticsearch.TransportVersion;
11+
import org.elasticsearch.TransportVersions;
12+
import org.elasticsearch.common.io.stream.GenericNamedWriteable;
13+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
14+
import org.elasticsearch.common.io.stream.StreamInput;
15+
import org.elasticsearch.common.io.stream.StreamOutput;
1016
import org.elasticsearch.core.Releasables;
1117
import org.elasticsearch.index.mapper.BlockLoader;
1218

19+
import java.io.IOException;
20+
1321
public class AggregateMetricDoubleBlockBuilder extends AbstractBlockBuilder implements BlockLoader.AggregateMetricDoubleBuilder {
1422

1523
private DoubleBlockBuilder minBuilder;
@@ -161,11 +169,40 @@ public String getLabel() {
161169
}
162170
}
163171

164-
public record AggregateMetricDoubleLiteral(Double min, Double max, Double sum, Integer count) {
172+
public record AggregateMetricDoubleLiteral(Double min, Double max, Double sum, Integer count) implements GenericNamedWriteable {
165173
public AggregateMetricDoubleLiteral {
166174
min = min.isNaN() ? null : min;
167175
max = max.isNaN() ? null : max;
168176
sum = sum.isNaN() ? null : sum;
169177
}
178+
179+
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
180+
GenericNamedWriteable.class,
181+
"AggregateMetricDoubleLiteral",
182+
AggregateMetricDoubleLiteral::new
183+
);
184+
185+
@Override
186+
public String getWriteableName() {
187+
return "AggregateMetricDoubleLiteral";
188+
}
189+
190+
public AggregateMetricDoubleLiteral(StreamInput input) throws IOException {
191+
this(input.readOptionalDouble(), input.readOptionalDouble(), input.readOptionalDouble(), input.readOptionalInt());
192+
}
193+
194+
@Override
195+
public void writeTo(StreamOutput out) throws IOException {
196+
out.writeOptionalDouble(min);
197+
out.writeOptionalDouble(max);
198+
out.writeOptionalDouble(sum);
199+
out.writeOptionalInt(count);
200+
}
201+
202+
@Override
203+
public TransportVersion getMinimalSupportedVersion() {
204+
return TransportVersions.ESQL_AGGREGATE_METRIC_DOUBLE_LITERAL;
205+
}
206+
170207
}
171208
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,19 @@ private static Object valueAtOffset(Block block, int offset) {
285285
DocVector v = ((DocBlock) block).asVector();
286286
yield new Doc(v.shards().getInt(offset), v.segments().getInt(offset), v.docs().getInt(offset));
287287
}
288-
case COMPOSITE -> throw new IllegalArgumentException("can't read values from composite blocks");
288+
case COMPOSITE -> {
289+
CompositeBlock compositeBlock = (CompositeBlock) block;
290+
var minBlock = (DoubleBlock) compositeBlock.getBlock(AggregateMetricDoubleBlockBuilder.Metric.MIN.getIndex());
291+
var maxBlock = (DoubleBlock) compositeBlock.getBlock(AggregateMetricDoubleBlockBuilder.Metric.MAX.getIndex());
292+
var sumBlock = (DoubleBlock) compositeBlock.getBlock(AggregateMetricDoubleBlockBuilder.Metric.SUM.getIndex());
293+
var countBlock = (IntBlock) compositeBlock.getBlock(AggregateMetricDoubleBlockBuilder.Metric.COUNT.getIndex());
294+
yield new AggregateMetricDoubleLiteral(
295+
minBlock.getDouble(offset),
296+
maxBlock.getDouble(offset),
297+
sumBlock.getDouble(offset),
298+
countBlock.getInt(offset)
299+
);
300+
}
289301
case UNKNOWN -> throw new IllegalArgumentException("can't read values from [" + block + "]");
290302
};
291303
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.elasticsearch.common.regex.Regex;
2121
import org.elasticsearch.common.settings.Settings;
2222
import org.elasticsearch.common.util.BigArrays;
23+
import org.elasticsearch.compute.data.AggregateMetricDoubleBlockBuilder;
2324
import org.elasticsearch.compute.data.BlockFactory;
2425
import org.elasticsearch.compute.data.BlockUtils;
2526
import org.elasticsearch.compute.data.BytesRefBlock;
@@ -788,6 +789,12 @@ public static Literal randomLiteral(DataType type) {
788789
case CARTESIAN_POINT -> CARTESIAN.asWkb(ShapeTestUtils.randomPoint());
789790
case GEO_SHAPE -> GEO.asWkb(GeometryTestUtils.randomGeometry(randomBoolean()));
790791
case CARTESIAN_SHAPE -> CARTESIAN.asWkb(ShapeTestUtils.randomGeometry(randomBoolean()));
792+
case AGGREGATE_METRIC_DOUBLE -> new AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleLiteral(
793+
randomDouble(),
794+
randomDouble(),
795+
randomDouble(),
796+
randomInt()
797+
);
791798
case NULL -> null;
792799
case SOURCE -> {
793800
try {
@@ -798,8 +805,9 @@ public static Literal randomLiteral(DataType type) {
798805
throw new UncheckedIOException(e);
799806
}
800807
}
801-
case UNSUPPORTED, OBJECT, DOC_DATA_TYPE, TSID_DATA_TYPE, PARTIAL_AGG, AGGREGATE_METRIC_DOUBLE ->
802-
throw new IllegalArgumentException("can't make random values for [" + type.typeName() + "]");
808+
case UNSUPPORTED, OBJECT, DOC_DATA_TYPE, TSID_DATA_TYPE, PARTIAL_AGG -> throw new IllegalArgumentException(
809+
"can't make random values for [" + type.typeName() + "]"
810+
);
803811
}, type);
804812
}
805813

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,12 @@ public enum Cap {
875875
/**
876876
* Full text functions can be scored when being part of a disjunction
877877
*/
878-
FULL_TEXT_FUNCTIONS_DISJUNCTIONS_SCORE;
878+
FULL_TEXT_FUNCTIONS_DISJUNCTIONS_SCORE,
879+
880+
/**
881+
* Support for to_aggregate_metric_double function
882+
*/
883+
AGGREGATE_METRIC_DOUBLE_CONVERT_TO(AGGREGATE_METRIC_DOUBLE_FEATURE_FLAG);
879884

880885
private final boolean enabled;
881886

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.xpack.esql.expression.function.fulltext.FullTextWritables;
1515
import org.elasticsearch.xpack.esql.expression.function.scalar.ScalarFunctionWritables;
1616
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.FromBase64;
17+
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToAggregateMetricDouble;
1718
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToBase64;
1819
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToBoolean;
1920
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToCartesianPoint;
@@ -180,6 +181,7 @@ public static List<NamedWriteableRegistry.Entry> unaryScalars() {
180181
entries.add(StY.ENTRY);
181182
entries.add(Tan.ENTRY);
182183
entries.add(Tanh.ENTRY);
184+
entries.add(ToAggregateMetricDouble.ENTRY);
183185
entries.add(ToBase64.ENTRY);
184186
entries.add(ToBoolean.ENTRY);
185187
entries.add(ToCartesianPoint.ENTRY);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.elasticsearch.xpack.esql.expression.function.scalar.conditional.Greatest;
4343
import org.elasticsearch.xpack.esql.expression.function.scalar.conditional.Least;
4444
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.FromBase64;
45+
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToAggregateMetricDouble;
4546
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToBase64;
4647
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToBoolean;
4748
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToCartesianPoint;
@@ -376,6 +377,7 @@ private static FunctionDefinition[][] functions() {
376377
// conversion functions
377378
new FunctionDefinition[] {
378379
def(FromBase64.class, FromBase64::new, "from_base64"),
380+
def(ToAggregateMetricDouble.class, ToAggregateMetricDouble::new, "to_aggregate_metric_double", "to_aggregatemetricdouble"),
379381
def(ToBase64.class, ToBase64::new, "to_base64"),
380382
def(ToBoolean.class, ToBoolean::new, "to_boolean", "to_bool"),
381383
def(ToCartesianPoint.class, ToCartesianPoint::new, "to_cartesianpoint"),

0 commit comments

Comments
 (0)