Skip to content

Commit 450648a

Browse files
authored
Simplify EsqlQueryResponse (#129031)
1 parent 154496a commit 450648a

File tree

2 files changed

+53
-88
lines changed

2 files changed

+53
-88
lines changed

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

Lines changed: 52 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.elasticsearch.xpack.esql.core.type.DataType;
3030

3131
import java.io.IOException;
32-
import java.util.Collections;
32+
import java.util.ArrayList;
3333
import java.util.Iterator;
3434
import java.util.List;
3535
import java.util.Objects;
@@ -122,7 +122,7 @@ static EsqlQueryResponse deserialize(BlockStreamInput in) throws IOException {
122122
long documentsFound = in.getTransportVersion().onOrAfter(ESQL_DOCUMENTS_FOUND_AND_VALUES_LOADED) ? in.readVLong() : 0;
123123
long valuesLoaded = in.getTransportVersion().onOrAfter(ESQL_DOCUMENTS_FOUND_AND_VALUES_LOADED) ? in.readVLong() : 0;
124124
if (in.getTransportVersion().onOrAfter(TransportVersions.V_8_12_0)) {
125-
profile = in.readOptionalWriteable(Profile::new);
125+
profile = in.readOptionalWriteable(Profile::readFrom);
126126
}
127127
boolean columnar = in.readBoolean();
128128
EsqlExecutionInfo executionInfo = null;
@@ -224,75 +224,66 @@ public EsqlExecutionInfo getExecutionInfo() {
224224
return executionInfo;
225225
}
226226

227-
private Iterator<? extends ToXContent> asyncPropertiesOrEmpty() {
227+
@Override
228+
@SuppressWarnings("unchecked")
229+
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
230+
boolean dropNullColumns = params.paramAsBoolean(DROP_NULL_COLUMNS_OPTION, false);
231+
boolean[] nullColumns = dropNullColumns ? nullColumns() : null;
232+
233+
var content = new ArrayList<Iterator<? extends ToXContent>>(25);
234+
content.add(ChunkedToXContentHelper.startObject());
228235
if (isAsync) {
229-
return ChunkedToXContentHelper.chunk((builder, params) -> {
236+
content.add(ChunkedToXContentHelper.chunk((builder, p) -> {
230237
if (asyncExecutionId != null) {
231238
builder.field("id", asyncExecutionId);
232239
}
233240
builder.field("is_running", isRunning);
234241
return builder;
235-
});
236-
} else {
237-
return Collections.emptyIterator();
242+
}));
238243
}
239-
}
240-
241-
@Override
242-
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params) {
243-
boolean dropNullColumns = params.paramAsBoolean(DROP_NULL_COLUMNS_OPTION, false);
244-
boolean[] nullColumns = dropNullColumns ? nullColumns() : null;
245-
246-
Iterator<ToXContent> tookTime;
247244
if (executionInfo != null && executionInfo.overallTook() != null) {
248-
tookTime = ChunkedToXContentHelper.chunk(
249-
(builder, p) -> builder.field("took", executionInfo.overallTook().millis())
250-
.field(EsqlExecutionInfo.IS_PARTIAL_FIELD.getPreferredName(), executionInfo.isPartial())
245+
content.add(
246+
ChunkedToXContentHelper.chunk(
247+
(builder, p) -> builder //
248+
.field("took", executionInfo.overallTook().millis())
249+
.field(EsqlExecutionInfo.IS_PARTIAL_FIELD.getPreferredName(), executionInfo.isPartial())
250+
)
251251
);
252-
} else {
253-
tookTime = Collections.emptyIterator();
254252
}
255-
256-
Iterator<ToXContent> meta = ChunkedToXContentHelper.chunk((builder, p) -> {
257-
builder.field("documents_found", documentsFound);
258-
builder.field("values_loaded", valuesLoaded);
259-
return builder;
260-
});
261-
262-
Iterator<? extends ToXContent> columnHeadings = dropNullColumns
263-
? Iterators.concat(
264-
ResponseXContentUtils.allColumns(columns, "all_columns"),
265-
ResponseXContentUtils.nonNullColumns(columns, nullColumns, "columns")
253+
content.add(
254+
ChunkedToXContentHelper.chunk(
255+
(builder, p) -> builder //
256+
.field("documents_found", documentsFound)
257+
.field("values_loaded", valuesLoaded)
266258
)
267-
: ResponseXContentUtils.allColumns(columns, "columns");
268-
Iterator<? extends ToXContent> valuesIt = ResponseXContentUtils.columnValues(this.columns, this.pages, columnar, nullColumns);
269-
Iterator<ToXContent> executionInfoRender = executionInfo != null && executionInfo.hasMetadataToReport()
270-
? ChunkedToXContentHelper.field("_clusters", executionInfo, params)
271-
: Collections.emptyIterator();
272-
return Iterators.concat(
273-
ChunkedToXContentHelper.startObject(),
274-
asyncPropertiesOrEmpty(),
275-
tookTime,
276-
meta,
277-
columnHeadings,
278-
ChunkedToXContentHelper.array("values", valuesIt),
279-
executionInfoRender,
280-
profileRenderer(params),
281-
ChunkedToXContentHelper.endObject()
282259
);
283-
}
284-
285-
private Iterator<ToXContent> profileRenderer(ToXContent.Params params) {
286-
if (profile == null) {
287-
return Collections.emptyIterator();
260+
if (dropNullColumns) {
261+
content.add(ResponseXContentUtils.allColumns(columns, "all_columns"));
262+
content.add(ResponseXContentUtils.nonNullColumns(columns, nullColumns, "columns"));
263+
} else {
264+
content.add(ResponseXContentUtils.allColumns(columns, "columns"));
288265
}
289-
return Iterators.concat(ChunkedToXContentHelper.startObject("profile"), ChunkedToXContentHelper.chunk((b, p) -> {
290-
if (executionInfo != null) {
291-
b.field("query", executionInfo.overallTimeSpan());
292-
b.field("planning", executionInfo.planningTimeSpan());
293-
}
294-
return b;
295-
}), ChunkedToXContentHelper.array("drivers", profile.drivers.iterator(), params), ChunkedToXContentHelper.endObject());
266+
content.add(
267+
ChunkedToXContentHelper.array("values", ResponseXContentUtils.columnValues(this.columns, this.pages, columnar, nullColumns))
268+
);
269+
if (executionInfo != null && executionInfo.hasMetadataToReport()) {
270+
content.add(ChunkedToXContentHelper.field("_clusters", executionInfo, params));
271+
}
272+
if (profile != null) {
273+
content.add(ChunkedToXContentHelper.startObject("profile"));
274+
content.add(ChunkedToXContentHelper.chunk((b, p) -> {
275+
if (executionInfo != null) {
276+
b.field("query", executionInfo.overallTimeSpan());
277+
b.field("planning", executionInfo.planningTimeSpan());
278+
}
279+
return b;
280+
}));
281+
content.add(ChunkedToXContentHelper.array("drivers", profile.drivers.iterator(), params));
282+
content.add(ChunkedToXContentHelper.endObject());
283+
}
284+
content.add(ChunkedToXContentHelper.endObject());
285+
286+
return Iterators.concat(content.toArray(Iterator[]::new));
296287
}
297288

298289
public boolean[] nullColumns() {
@@ -396,41 +387,15 @@ public EsqlResponse responseInternal() {
396387
return esqlResponse;
397388
}
398389

399-
public static class Profile implements Writeable {
400-
private final List<DriverProfile> drivers;
401-
402-
public Profile(List<DriverProfile> drivers) {
403-
this.drivers = drivers;
404-
}
390+
public record Profile(List<DriverProfile> drivers) implements Writeable {
405391

406-
public Profile(StreamInput in) throws IOException {
407-
this.drivers = in.readCollectionAsImmutableList(DriverProfile::readFrom);
392+
public static Profile readFrom(StreamInput in) throws IOException {
393+
return new Profile(in.readCollectionAsImmutableList(DriverProfile::readFrom));
408394
}
409395

410396
@Override
411397
public void writeTo(StreamOutput out) throws IOException {
412398
out.writeCollection(drivers);
413399
}
414-
415-
@Override
416-
public boolean equals(Object o) {
417-
if (this == o) {
418-
return true;
419-
}
420-
if (o == null || getClass() != o.getClass()) {
421-
return false;
422-
}
423-
Profile profile = (Profile) o;
424-
return Objects.equals(drivers, profile.drivers);
425-
}
426-
427-
@Override
428-
public int hashCode() {
429-
return Objects.hash(drivers);
430-
}
431-
432-
List<DriverProfile> drivers() {
433-
return drivers;
434-
}
435400
}
436401
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public class EsqlQueryResponseProfileTests extends AbstractWireSerializingTestCase<EsqlQueryResponse.Profile> {
2222
@Override
2323
protected Writeable.Reader<EsqlQueryResponse.Profile> instanceReader() {
24-
return EsqlQueryResponse.Profile::new;
24+
return EsqlQueryResponse.Profile::readFrom;
2525
}
2626

2727
@Override

0 commit comments

Comments
 (0)