Skip to content

Commit 9da3366

Browse files
committed
[ES|QL] ToAggregateMetricDouble function (elastic#124595)
This commit adds a conversion function from numerics (and aggregate metric doubles) to aggregate metric doubles. It is most useful when you have multiple indices, where one index uses aggregate metric double (e.g. a downsampled index) and another uses a normal numeric type like long or double (e.g. an index prior to downsampling).
1 parent 016b367 commit 9da3366

File tree

19 files changed

+934
-8
lines changed

19 files changed

+934
-8
lines changed

docs/changelog/124595.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 124595
2+
summary: '`ToAggregateMetricDouble` function'
3+
area: "ES|QL"
4+
type: enhancement
5+
issues: []
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
% This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.
2+
3+
## `TO_AGGREGATE_METRIC_DOUBLE` [esql-to_aggregate_metric_double]
4+
5+
**Syntax**
6+
7+
:::{image} ../../../images/functions/to_aggregate_metric_double.svg
8+
:alt: Embedded
9+
:class: text-center
10+
:::
11+
12+
13+
:::{include} ../parameters/to_aggregate_metric_double.md
14+
:::
15+
16+
:::{include} ../description/to_aggregate_metric_double.md
17+
:::
18+
19+
:::{include} ../types/to_aggregate_metric_double.md
20+
:::
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
% This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.
2+
3+
**Parameters**
4+
5+
`number`
6+
: Input value. The input can be a single-valued column or an expression.
7+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
% This is generated by ESQL's AbstractFunctionTestCase. Do no edit it. See ../README.md for how to regenerate it.
2+
3+
**Supported types**
4+
5+
| number | result |
6+
| --- | --- |
7+
aggregate_metric_double
8+
Lines changed: 1 addition & 0 deletions
Loading

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
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.common.regex.Regex;
2222
import org.elasticsearch.common.settings.Settings;
2323
import org.elasticsearch.common.util.BigArrays;
24+
import org.elasticsearch.compute.data.AggregateMetricDoubleBlockBuilder;
2425
import org.elasticsearch.compute.data.BlockFactory;
2526
import org.elasticsearch.compute.data.BlockUtils;
2627
import org.elasticsearch.compute.data.BytesRefBlock;
@@ -786,6 +787,12 @@ public static Literal randomLiteral(DataType type) {
786787
case CARTESIAN_POINT -> CARTESIAN.asWkb(ShapeTestUtils.randomPoint());
787788
case GEO_SHAPE -> GEO.asWkb(GeometryTestUtils.randomGeometry(randomBoolean()));
788789
case CARTESIAN_SHAPE -> CARTESIAN.asWkb(ShapeTestUtils.randomGeometry(randomBoolean()));
790+
case AGGREGATE_METRIC_DOUBLE -> new AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleLiteral(
791+
randomDouble(),
792+
randomDouble(),
793+
randomDouble(),
794+
randomInt()
795+
);
789796
case NULL -> null;
790797
case SOURCE -> {
791798
try {
@@ -796,8 +803,9 @@ public static Literal randomLiteral(DataType type) {
796803
throw new UncheckedIOException(e);
797804
}
798805
}
799-
case UNSUPPORTED, OBJECT, DOC_DATA_TYPE, TSID_DATA_TYPE, PARTIAL_AGG, AGGREGATE_METRIC_DOUBLE ->
800-
throw new IllegalArgumentException("can't make random values for [" + type.typeName() + "]");
806+
case UNSUPPORTED, OBJECT, DOC_DATA_TYPE, TSID_DATA_TYPE, PARTIAL_AGG -> throw new IllegalArgumentException(
807+
"can't make random values for [" + type.typeName() + "]"
808+
);
801809
}, type);
802810
}
803811

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
@@ -772,7 +772,12 @@ public enum Cap {
772772
/**
773773
* Supercedes {@link Cap#MAKE_NUMBER_OF_CHANNELS_CONSISTENT_WITH_LAYOUT}.
774774
*/
775-
FIX_REPLACE_MISSING_FIELD_WITH_NULL_DUPLICATE_NAME_ID_IN_LAYOUT;
775+
FIX_REPLACE_MISSING_FIELD_WITH_NULL_DUPLICATE_NAME_ID_IN_LAYOUT,
776+
777+
/**
778+
* Support for to_aggregate_metric_double function
779+
*/
780+
AGGREGATE_METRIC_DOUBLE_CONVERT_TO(AGGREGATE_METRIC_DOUBLE_FEATURE_FLAG);
776781

777782
private final boolean enabled;
778783

0 commit comments

Comments
 (0)