|
29 | 29 | import org.elasticsearch.xpack.esql.core.type.DataType;
|
30 | 30 |
|
31 | 31 | import java.io.IOException;
|
32 |
| -import java.util.Collections; |
| 32 | +import java.util.ArrayList; |
33 | 33 | import java.util.Iterator;
|
34 | 34 | import java.util.List;
|
35 | 35 | import java.util.Objects;
|
@@ -122,7 +122,7 @@ static EsqlQueryResponse deserialize(BlockStreamInput in) throws IOException {
|
122 | 122 | long documentsFound = in.getTransportVersion().onOrAfter(ESQL_DOCUMENTS_FOUND_AND_VALUES_LOADED) ? in.readVLong() : 0;
|
123 | 123 | long valuesLoaded = in.getTransportVersion().onOrAfter(ESQL_DOCUMENTS_FOUND_AND_VALUES_LOADED) ? in.readVLong() : 0;
|
124 | 124 | if (in.getTransportVersion().onOrAfter(TransportVersions.V_8_12_0)) {
|
125 |
| - profile = in.readOptionalWriteable(Profile::new); |
| 125 | + profile = in.readOptionalWriteable(Profile::readFrom); |
126 | 126 | }
|
127 | 127 | boolean columnar = in.readBoolean();
|
128 | 128 | EsqlExecutionInfo executionInfo = null;
|
@@ -224,75 +224,66 @@ public EsqlExecutionInfo getExecutionInfo() {
|
224 | 224 | return executionInfo;
|
225 | 225 | }
|
226 | 226 |
|
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()); |
228 | 235 | if (isAsync) {
|
229 |
| - return ChunkedToXContentHelper.chunk((builder, params) -> { |
| 236 | + content.add(ChunkedToXContentHelper.chunk((builder, p) -> { |
230 | 237 | if (asyncExecutionId != null) {
|
231 | 238 | builder.field("id", asyncExecutionId);
|
232 | 239 | }
|
233 | 240 | builder.field("is_running", isRunning);
|
234 | 241 | return builder;
|
235 |
| - }); |
236 |
| - } else { |
237 |
| - return Collections.emptyIterator(); |
| 242 | + })); |
238 | 243 | }
|
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; |
247 | 244 | 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 | + ) |
251 | 251 | );
|
252 |
| - } else { |
253 |
| - tookTime = Collections.emptyIterator(); |
254 | 252 | }
|
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) |
266 | 258 | )
|
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() |
282 | 259 | );
|
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")); |
288 | 265 | }
|
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)); |
296 | 287 | }
|
297 | 288 |
|
298 | 289 | public boolean[] nullColumns() {
|
@@ -396,41 +387,15 @@ public EsqlResponse responseInternal() {
|
396 | 387 | return esqlResponse;
|
397 | 388 | }
|
398 | 389 |
|
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 { |
405 | 391 |
|
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)); |
408 | 394 | }
|
409 | 395 |
|
410 | 396 | @Override
|
411 | 397 | public void writeTo(StreamOutput out) throws IOException {
|
412 | 398 | out.writeCollection(drivers);
|
413 | 399 | }
|
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 |
| - } |
435 | 400 | }
|
436 | 401 | }
|
0 commit comments