|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.test.esql; |
| 11 | + |
| 12 | +import org.apache.http.util.EntityUtils; |
| 13 | +import org.elasticsearch.client.Request; |
| 14 | +import org.elasticsearch.client.Response; |
| 15 | +import org.elasticsearch.client.ResponseException; |
| 16 | +import org.elasticsearch.common.settings.Settings; |
| 17 | +import org.elasticsearch.test.cluster.ElasticsearchCluster; |
| 18 | +import org.elasticsearch.test.cluster.local.distribution.DistributionType; |
| 19 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 20 | +import org.junit.ClassRule; |
| 21 | + |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import static org.hamcrest.Matchers.containsString; |
| 28 | +import static org.hamcrest.Matchers.equalTo; |
| 29 | +import static org.hamcrest.Matchers.lessThanOrEqualTo; |
| 30 | + |
| 31 | +public class EsqlPartialResultsIT extends ESRestTestCase { |
| 32 | + @ClassRule |
| 33 | + public static ElasticsearchCluster cluster = ElasticsearchCluster.local() |
| 34 | + .distribution(DistributionType.DEFAULT) |
| 35 | + .module("test-error-query") |
| 36 | + .setting("xpack.security.enabled", "false") |
| 37 | + .setting("xpack.license.self_generated.type", "trial") |
| 38 | + .setting("esql.query.allow_partial_results", "true") |
| 39 | + .build(); |
| 40 | + |
| 41 | + @Override |
| 42 | + protected String getTestRestCluster() { |
| 43 | + return cluster.getHttpAddresses(); |
| 44 | + } |
| 45 | + |
| 46 | + public Set<String> populateIndices() throws Exception { |
| 47 | + int nextId = 0; |
| 48 | + { |
| 49 | + createIndex("failing-index", Settings.EMPTY, """ |
| 50 | + { |
| 51 | + "runtime": { |
| 52 | + "fail_me": { |
| 53 | + "type": "long", |
| 54 | + "script": { |
| 55 | + "source": "", |
| 56 | + "lang": "failing_field" |
| 57 | + } |
| 58 | + } |
| 59 | + }, |
| 60 | + "properties": { |
| 61 | + "v": { |
| 62 | + "type": "long" |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + """); |
| 67 | + int numDocs = between(1, 50); |
| 68 | + for (int i = 0; i < numDocs; i++) { |
| 69 | + String id = Integer.toString(nextId++); |
| 70 | + Request doc = new Request("PUT", "failing-index/_doc/" + id); |
| 71 | + doc.setJsonEntity("{\"v\": " + id + "}"); |
| 72 | + client().performRequest(doc); |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + Set<String> okIds = new HashSet<>(); |
| 77 | + { |
| 78 | + createIndex("ok-index", Settings.EMPTY, """ |
| 79 | + { |
| 80 | + "properties": { |
| 81 | + "v": { |
| 82 | + "type": "long" |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + """); |
| 87 | + int numDocs = between(1, 50); |
| 88 | + for (int i = 0; i < numDocs; i++) { |
| 89 | + String id = Integer.toString(nextId++); |
| 90 | + okIds.add(id); |
| 91 | + Request doc = new Request("PUT", "ok-index/_doc/" + id); |
| 92 | + doc.setJsonEntity("{\"v\": " + id + "}"); |
| 93 | + client().performRequest(doc); |
| 94 | + } |
| 95 | + } |
| 96 | + refresh(client(), "failing-index,ok-index"); |
| 97 | + return okIds; |
| 98 | + } |
| 99 | + |
| 100 | + public void testPartialResult() throws Exception { |
| 101 | + Set<String> okIds = populateIndices(); |
| 102 | + String query = """ |
| 103 | + { |
| 104 | + "query": "FROM ok-index,failing-index | LIMIT 100 | KEEP fail_me,v" |
| 105 | + } |
| 106 | + """; |
| 107 | + // allow_partial_results = true |
| 108 | + { |
| 109 | + Request request = new Request("POST", "/_query"); |
| 110 | + request.setJsonEntity(query); |
| 111 | + if (randomBoolean()) { |
| 112 | + request.addParameter("allow_partial_results", "true"); |
| 113 | + } |
| 114 | + Response resp = client().performRequest(request); |
| 115 | + Map<String, Object> results = entityAsMap(resp); |
| 116 | + assertThat(results.get("is_partial"), equalTo(true)); |
| 117 | + List<?> columns = (List<?>) results.get("columns"); |
| 118 | + assertThat(columns, equalTo(List.of(Map.of("name", "fail_me", "type", "long"), Map.of("name", "v", "type", "long")))); |
| 119 | + List<?> values = (List<?>) results.get("values"); |
| 120 | + assertThat(values.size(), lessThanOrEqualTo(okIds.size())); |
| 121 | + } |
| 122 | + // allow_partial_results = false |
| 123 | + { |
| 124 | + Request request = new Request("POST", "/_query"); |
| 125 | + request.setJsonEntity(""" |
| 126 | + { |
| 127 | + "query": "FROM ok-index,failing-index | LIMIT 100" |
| 128 | + } |
| 129 | + """); |
| 130 | + request.addParameter("allow_partial_results", "false"); |
| 131 | + var error = expectThrows(ResponseException.class, () -> client().performRequest(request)); |
| 132 | + Response resp = error.getResponse(); |
| 133 | + assertThat(resp.getStatusLine().getStatusCode(), equalTo(500)); |
| 134 | + assertThat(EntityUtils.toString(resp.getEntity()), containsString("Accessing failing field")); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments