Skip to content

Commit e93448c

Browse files
committed
address comments
1 parent a82dc92 commit e93448c

File tree

5 files changed

+85
-40
lines changed

5 files changed

+85
-40
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToStringFromAggregateMetricDoubleEvaluator.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,10 @@ public Block evalBlock(Block b) {
4343
int positionCount = block.getPositionCount();
4444
try (BytesRefBlock.Builder builder = driverContext.blockFactory().newBytesRefBlockBuilder(positionCount)) {
4545
for (int p = 0; p < positionCount; p++) {
46-
int valueCount = block.getValueCount(p);
47-
int start = block.getFirstValueIndex(p);
48-
int end = start + valueCount;
49-
boolean positionOpened = false;
50-
boolean valuesAppended = false;
51-
for (int i = start; i < end; i++) {
52-
BytesRef value = evalValue(block, i);
53-
if (positionOpened == false && valueCount > 1) {
54-
builder.beginPositionEntry();
55-
positionOpened = true;
56-
}
57-
builder.appendBytesRef(value);
58-
valuesAppended = true;
59-
}
60-
if (valuesAppended == false) {
46+
if (block.isNull(p)) {
6147
builder.appendNull();
62-
} else if (positionOpened) {
63-
builder.endPositionEntry();
48+
} else {
49+
builder.appendBytesRef(evalValue(block, p));
6450
}
6551
}
6652
return builder.build();

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package org.elasticsearch.xpack.esql.type;
99

1010
import org.apache.lucene.util.BytesRef;
11+
import org.elasticsearch.common.Strings;
1112
import org.elasticsearch.common.io.stream.StreamInput;
1213
import org.elasticsearch.common.io.stream.StreamOutput;
1314
import org.elasticsearch.common.logging.LoggerMessageFormat;
@@ -16,10 +17,14 @@
1617
import org.elasticsearch.common.time.DateFormatters;
1718
import org.elasticsearch.common.time.DateUtils;
1819
import org.elasticsearch.compute.data.AggregateMetricDoubleBlockBuilder;
20+
import org.elasticsearch.compute.data.AggregateMetricDoubleBlockBuilder.Metric;
1921
import org.elasticsearch.compute.data.CompositeBlock;
2022
import org.elasticsearch.compute.data.DoubleBlock;
2123
import org.elasticsearch.compute.data.IntBlock;
2224
import org.elasticsearch.search.DocValueFormat;
25+
import org.elasticsearch.xcontent.XContentBuilder;
26+
import org.elasticsearch.xcontent.json.JsonXContent;
27+
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
2328
import org.elasticsearch.xpack.esql.core.InvalidArgumentException;
2429
import org.elasticsearch.xpack.esql.core.QlIllegalArgumentException;
2530
import org.elasticsearch.xpack.esql.core.expression.Expression;
@@ -62,7 +67,6 @@
6267
import java.util.List;
6368
import java.util.Locale;
6469
import java.util.Map;
65-
import java.util.StringJoiner;
6670
import java.util.function.BiFunction;
6771
import java.util.function.Function;
6872

@@ -670,24 +674,23 @@ public static long booleanToUnsignedLong(boolean number) {
670674
}
671675

672676
public static String aggregateMetricDoubleBlockToString(CompositeBlock compositeBlock, int index) {
673-
var minBlock = compositeBlock.getBlock(AggregateMetricDoubleBlockBuilder.Metric.MIN.getIndex());
674-
var maxBlock = compositeBlock.getBlock(AggregateMetricDoubleBlockBuilder.Metric.MAX.getIndex());
675-
var sumBlock = compositeBlock.getBlock(AggregateMetricDoubleBlockBuilder.Metric.SUM.getIndex());
676-
var countBlock = compositeBlock.getBlock(AggregateMetricDoubleBlockBuilder.Metric.COUNT.getIndex());
677-
final StringJoiner joiner = new StringJoiner(", ");
678-
if (maxBlock.isNull(index) == false) {
679-
joiner.add("\"max\": " + ((DoubleBlock) maxBlock).getDouble(index));
680-
}
681-
if (minBlock.isNull(index) == false) {
682-
joiner.add("\"min\": " + ((DoubleBlock) minBlock).getDouble(index));
683-
}
684-
if (sumBlock.isNull(index) == false) {
685-
joiner.add("\"sum\": " + ((DoubleBlock) sumBlock).getDouble(index));
686-
}
687-
if (countBlock.isNull(index) == false) {
688-
joiner.add("\"value_count\": " + ((IntBlock) countBlock).getInt(index));
677+
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
678+
builder.startObject();
679+
for (Metric metric : List.of(Metric.MIN, Metric.MAX, Metric.SUM)) {
680+
var block = compositeBlock.getBlock(metric.getIndex());
681+
if (block.isNull(index) == false) {
682+
builder.field(metric.name().toLowerCase(), ((DoubleBlock) block).getDouble(index));
683+
}
684+
}
685+
var countBlock = compositeBlock.getBlock(AggregateMetricDoubleBlockBuilder.Metric.COUNT.getIndex());
686+
if (countBlock.isNull(index) == false) {
687+
builder.field("value_count", ((IntBlock) countBlock).getInt(index));
688+
}
689+
builder.endObject();
690+
return Strings.toString(builder);
691+
} catch (IOException e) {
692+
throw new EsqlIllegalArgumentException("error rendering aggregate metric double", e);
689693
}
690-
return "{ " + joiner + " }";
691694
}
692695

693696
public enum EsqlConverter implements Converter {

x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/esql/40_tsdb.yml

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ setup:
141141
- '{"@timestamp": "2021-04-28T19:50:24.467Z", "agg_metric": {"max": 10, "min": 3}, "k8s": {"pod": {"uid":"947e4ced-1786-4e53-9e0c-5c447e959507"}}}'
142142
- '{"index": {}}'
143143
- '{"@timestamp": "2021-04-28T19:50:44.467Z", "agg_metric": {"max": 17, "min": 2}, "k8s": {"pod": {"uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9"}}}'
144+
- '{"index": {}}'
145+
- '{"@timestamp": "2021-04-28T19:51:04.467Z", "k8s": {"pod": {"uid":"df3145b3-0563-4d3b-a0f7-897eb2876ea9"}}}'
144146

145147
- do:
146148
indices.create:
@@ -466,7 +468,32 @@ render aggregate_metric_double when missing min and max:
466468
- length: {values.0: 1}
467469
- match: {columns.0.name: "agg_metric"}
468470
- match: {columns.0.type: "aggregate_metric_double"}
469-
- match: {values.0.0: '{ "sum": 1.0, "value_count": 10 }'}
471+
- match: {values.0.0: '{"sum":1.0,"value_count":10}'}
472+
473+
474+
---
475+
render aggregate_metric_double when missing value:
476+
- requires:
477+
test_runner_features: [ capabilities ]
478+
capabilities:
479+
- method: POST
480+
path: /_query
481+
parameters: [ ]
482+
capabilities: [ aggregate_metric_double_rendering ]
483+
reason: "Support for rendering aggregate_metric_doubles"
484+
- do:
485+
allowed_warnings_regex:
486+
- "No limit defined, adding default limit of \\[.*\\]"
487+
esql.query:
488+
body:
489+
query: 'FROM test3 | WHERE @timestamp == "2021-04-28T19:51:04.467Z" | KEEP agg_metric'
490+
491+
- length: {values: 1}
492+
- length: {values.0: 1}
493+
- match: {columns.0.name: "agg_metric"}
494+
- match: {columns.0.type: "aggregate_metric_double"}
495+
- match: {values.0.0: null}
496+
470497

471498
---
472499
to_string aggregate_metric_double:
@@ -489,7 +516,7 @@ to_string aggregate_metric_double:
489516
- length: {values.0: 1}
490517
- match: {columns.0.name: "agg"}
491518
- match: {columns.0.type: "keyword"}
492-
- match: {values.0.0: '{ "sum": 1.0, "value_count": 10 }'}
519+
- match: {values.0.0: '{"sum":1.0,"value_count":10}'}
493520

494521
---
495522
from index pattern unsupported counter:
@@ -526,7 +553,7 @@ from index pattern unsupported counter:
526553
- match: {columns.7.type: "keyword"}
527554
- match: {columns.8.name: "metricset"}
528555
- match: {columns.8.type: "keyword"}
529-
- length: {values: 15}
556+
- length: {values: 16}
530557

531558
---
532559
from index pattern explicit counter use:
@@ -547,7 +574,7 @@ from index pattern explicit counter use:
547574
query: 'FROM test* | keep *.tx'
548575
- match: {columns.0.name: "k8s.pod.network.tx"}
549576
- match: {columns.0.type: "unsupported"}
550-
- length: {values: 15}
577+
- length: {values: 16}
551578

552579
---
553580
_source:

x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/esql/40_unsupported_types.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ unsupported:
190190
- match: { columns.28.type: integer }
191191

192192
- length: { values: 1 }
193-
- match: { values.0.0: '{ "max": 3.0, "min": 1.0, "sum": 10.1, "value_count": 5 }' }
193+
- match: { values.0.0: '{"min":1.0,"max":3.0,"sum":10.1,"value_count":5}' }
194194
- match: { values.0.1: null }
195195
- match: { values.0.2: null }
196196
- match: { values.0.3: "2015-01-01T12:10:30.123456789Z" }

x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/esql/46_downsample.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,32 @@ setup:
117117
- match: {values.0.2: 5332018.0}
118118
- match: {values.0.3: 8}
119119

120+
---
121+
"Render stats from downsampled index":
122+
- requires:
123+
test_runner_features: [capabilities]
124+
capabilities:
125+
- method: POST
126+
path: /_query
127+
parameters: []
128+
capabilities: [aggregate_metric_double_rendering]
129+
reason: "Support for rendering aggregate_metric_doubles"
130+
- do:
131+
indices.downsample:
132+
index: test
133+
target_index: test-downsample
134+
body: >
135+
{
136+
"fixed_interval": "1h"
137+
}
138+
- is_true: acknowledged
139+
140+
- do:
141+
esql.query:
142+
body:
143+
query: "FROM test-downsample | WHERE @timestamp == \"2021-04-28T19:00:00.000Z\" | KEEP k8s.pod.network.rx | LIMIT 100"
144+
- length: {values: 1}
145+
- length: {values.0: 1}
146+
- match: {columns.0.name: "k8s.pod.network.rx"}
147+
- match: {columns.0.type: "aggregate_metric_double"}
148+
- match: {values.0.0: '{"min":530604.0,"max":530605.0,"sum":1061209.0,"value_count":2}'}

0 commit comments

Comments
 (0)