Skip to content

Commit 259bc6c

Browse files
committed
ESQL: Make _tsid presentable
- Add an ability to present the TSID_DATA_TYPE as KEYWORD. - Add tests for the TopN optimization with METADATA _tsid
1 parent 2c63c9a commit 259bc6c

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,21 @@ FROM employees
168168
avg_worked_seconds:long | birth_date:date | emp_no:i | gender:k | height:d | height.float:d | height.half_float:d | height.scaled_float:d | hire_date:date | is_rehired:bool | job_positions:k | languages:i | languages.byte:i | languages.long:l | languages.short:short | salary:i | salary_change:d | salary_change.int:i | salary_change.keyword:k | salary_change.long:l | still_hired:bool | name:k | first_name:k | last_name:k
169169
349086555 | 1961-09-01T00:00:00Z | 10056 | F | 1.57 | 1.5700000524520874 | 1.5703125 | 1.57 | 1990-02-01T00:00:00Z | [false, false, true] | [Senior Team Lead] | 2 | 2 | 2 | 2 | 33370 | [-5.17, 10.99] | [-5, 10] | [-5.17, 10.99] | [-5, 10] | true | Brendon | Bernini | Brendon
170170
;
171+
172+
sortingByTsidMetadataField
173+
required_capability: ts_command_v0
174+
required_capability: metadata_tsid_field
175+
176+
TS k8s METADATA _tsid
177+
| SORT events_received ASC
178+
| LIMIT 5
179+
| KEEP events_received, @timestamp, cluster, pod
180+
;
181+
182+
events_received:long | @timestamp:datetime | cluster:keyword | pod:keyword
183+
1 | 2024-05-10T00:00:00.000Z | qa | one
184+
1 | 2024-05-10T00:00:00.000Z | qa | two
185+
1 | 2024-05-10T00:00:00.000Z | qa | three
186+
1 | 2024-05-10T00:00:00.000Z | prod | one
187+
1 | 2024-05-10T00:00:00.000Z | prod | two
188+
;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.xcontent.XContentParser;
2929
import org.elasticsearch.xcontent.XContentParserConfiguration;
3030
import org.elasticsearch.xcontent.XContentType;
31+
import org.elasticsearch.xpack.esql.core.expression.MetadataAttribute;
3132

3233
import java.io.IOException;
3334

@@ -108,6 +109,10 @@ protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Pa
108109
// https://github.com/FasterXML/jackson-dataformats-binary/issues/366
109110
val = BytesRef.deepCopyOf(scratch);
110111
}
112+
113+
if (columnInfo.name().equalsIgnoreCase(MetadataAttribute.TSID_FIELD)) {
114+
return builder.value(TimeSeriesIdFieldMapper.encodeTsid(val));
115+
}
111116
return builder.utf8Value(val.bytes, val.offset, val.length);
112117
}
113118
};

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/action/EsqlQueryResponseTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.elasticsearch.geometry.utils.Geohash;
5050
import org.elasticsearch.h3.H3;
5151
import org.elasticsearch.index.mapper.BlockLoader;
52+
import org.elasticsearch.index.mapper.TimeSeriesIdFieldMapper;
5253
import org.elasticsearch.rest.action.RestActions;
5354
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils;
5455
import org.elasticsearch.test.AbstractChunkedSerializingTestCase;
@@ -64,6 +65,7 @@
6465
import org.elasticsearch.xcontent.XContentType;
6566
import org.elasticsearch.xcontent.json.JsonXContent;
6667
import org.elasticsearch.xpack.esql.EsqlTestUtils;
68+
import org.elasticsearch.xpack.esql.core.expression.MetadataAttribute;
6769
import org.elasticsearch.xpack.esql.core.type.DataType;
6870
import org.elasticsearch.xpack.esql.planner.PlannerUtils;
6971
import org.elasticsearch.xpack.esql.type.UnsupportedEsFieldTests;
@@ -1086,6 +1088,36 @@ public void testProfileXContent() {
10861088
}
10871089
}
10881090

1091+
public void testKeywordTsidFieldEncoding() {
1092+
BytesRef tsidValue = (BytesRef) EsqlTestUtils.randomLiteral(DataType.TSID_DATA_TYPE).value();
1093+
Object expectedEncoded = TimeSeriesIdFieldMapper.encodeTsid(tsidValue);
1094+
1095+
for (String columnName : List.of(
1096+
MetadataAttribute.TSID_FIELD.toLowerCase(Locale.ROOT),
1097+
MetadataAttribute.TSID_FIELD.toUpperCase(Locale.ROOT)
1098+
)) {
1099+
for (String dataType : List.of(DataType.KEYWORD.esType(), DataType.TEXT.esType())) {
1100+
try (
1101+
BytesRefBlock.Builder refBlockBuilder = blockFactory.newBytesRefBlockBuilder(1);
1102+
1103+
EsqlQueryResponse response = new EsqlQueryResponse(
1104+
List.of(new ColumnInfoImpl(columnName, dataType, null)),
1105+
List.of(new Page(refBlockBuilder.appendBytesRef(tsidValue).build())),
1106+
1,
1107+
1,
1108+
null,
1109+
false,
1110+
false,
1111+
null
1112+
)
1113+
) {
1114+
String json = Strings.toString(wrapAsToXContent(response), true, false);
1115+
assertThat(json, org.hamcrest.Matchers.containsString("\"" + expectedEncoded + "\""));
1116+
}
1117+
}
1118+
}
1119+
}
1120+
10891121
@Override
10901122
protected void dispose(EsqlQueryResponse esqlQueryResponse) {
10911123
esqlQueryResponse.close();

0 commit comments

Comments
 (0)