|
6 | 6 | package org.elasticsearch.xpack.sql.qa.rest;
|
7 | 7 |
|
8 | 8 | import com.fasterxml.jackson.core.io.JsonStringEncoder;
|
9 |
| - |
10 | 9 | import org.apache.http.HttpEntity;
|
11 | 10 | import org.apache.http.entity.ContentType;
|
12 | 11 | import org.apache.http.entity.StringEntity;
|
|
15 | 14 | import org.elasticsearch.client.Response;
|
16 | 15 | import org.elasticsearch.client.ResponseException;
|
17 | 16 | import org.elasticsearch.common.CheckedSupplier;
|
| 17 | +import org.elasticsearch.common.Strings; |
18 | 18 | import org.elasticsearch.common.bytes.BytesArray;
|
19 | 19 | import org.elasticsearch.common.collect.Tuple;
|
20 | 20 | import org.elasticsearch.common.io.Streams;
|
| 21 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
21 | 22 | import org.elasticsearch.common.xcontent.XContentHelper;
|
22 | 23 | import org.elasticsearch.common.xcontent.json.JsonXContent;
|
23 | 24 | import org.elasticsearch.test.NotEqualMessageBuilder;
|
|
31 | 32 | import java.io.InputStreamReader;
|
32 | 33 | import java.nio.charset.StandardCharsets;
|
33 | 34 | import java.sql.JDBCType;
|
| 35 | +import java.time.Instant; |
| 36 | +import java.time.ZoneId; |
| 37 | +import java.time.ZonedDateTime; |
34 | 38 | import java.util.ArrayList;
|
35 | 39 | import java.util.Arrays;
|
36 | 40 | import java.util.Collections;
|
@@ -151,6 +155,70 @@ public void testNextPage() throws IOException {
|
151 | 155 | ContentType.APPLICATION_JSON), StringUtils.EMPTY, mode));
|
152 | 156 | }
|
153 | 157 |
|
| 158 | + public void testNextPageWithDatetimeAndTimezoneParam() throws IOException { |
| 159 | + Request request = new Request("PUT", "/test_date_timezone"); |
| 160 | + XContentBuilder createIndex = JsonXContent.contentBuilder().startObject(); |
| 161 | + createIndex.startObject("mappings"); |
| 162 | + { |
| 163 | + createIndex.startObject("properties"); |
| 164 | + { |
| 165 | + createIndex.startObject("date").field("type", "date").field("format", "epoch_millis"); |
| 166 | + createIndex.endObject(); |
| 167 | + } |
| 168 | + createIndex.endObject(); |
| 169 | + } |
| 170 | + createIndex.endObject().endObject(); |
| 171 | + request.setJsonEntity(Strings.toString(createIndex)); |
| 172 | + client().performRequest(request); |
| 173 | + |
| 174 | + request = new Request("PUT", "/test_date_timezone/_bulk"); |
| 175 | + request.addParameter("refresh", "true"); |
| 176 | + StringBuilder bulk = new StringBuilder(); |
| 177 | + long[] datetimes = new long[] { 1_000, 10_000, 100_000, 1_000_000, 10_000_000 }; |
| 178 | + for (long datetime : datetimes) { |
| 179 | + bulk.append("{\"index\":{}}\n"); |
| 180 | + bulk.append("{\"date\":").append(datetime).append("}\n"); |
| 181 | + } |
| 182 | + request.setJsonEntity(bulk.toString()); |
| 183 | + assertEquals(200, client().performRequest(request).getStatusLine().getStatusCode()); |
| 184 | + |
| 185 | + ZoneId zoneId = randomZone(); |
| 186 | + String mode = randomMode(); |
| 187 | + String sqlRequest = |
| 188 | + "{\"query\":\"SELECT DATE_PART('TZOFFSET', date) AS tz FROM test_date_timezone ORDER BY date\"," |
| 189 | + + "\"time_zone\":\"" + zoneId.getId() + "\", " |
| 190 | + + "\"mode\":\"" + mode + "\", " |
| 191 | + + "\"fetch_size\":2}"; |
| 192 | + |
| 193 | + String cursor = null; |
| 194 | + for (int i = 0; i <= datetimes.length; i += 2) { |
| 195 | + Map<String, Object> expected = new HashMap<>(); |
| 196 | + Map<String, Object> response; |
| 197 | + |
| 198 | + if (i == 0) { |
| 199 | + expected.put("columns", singletonList(columnInfo(mode, "tz", "integer", JDBCType.INTEGER, 11))); |
| 200 | + response = runSql(new StringEntity(sqlRequest, ContentType.APPLICATION_JSON), "", mode); |
| 201 | + } else { |
| 202 | + response = runSql(new StringEntity("{\"cursor\":\"" + cursor + "\"" + mode(mode) + "}", |
| 203 | + ContentType.APPLICATION_JSON), StringUtils.EMPTY, mode); |
| 204 | + } |
| 205 | + |
| 206 | + List<Object> values = new ArrayList<>(2); |
| 207 | + for (int j = 0; j < (i < datetimes.length - 1 ? 2 : 1); j++) { |
| 208 | + values.add(singletonList(ZonedDateTime.ofInstant(Instant.ofEpochMilli(datetimes[i + j]), zoneId) |
| 209 | + .getOffset().getTotalSeconds() / 60)); |
| 210 | + } |
| 211 | + expected.put("rows", values); |
| 212 | + cursor = (String) response.remove("cursor"); |
| 213 | + assertResponse(expected, response); |
| 214 | + assertNotNull(cursor); |
| 215 | + } |
| 216 | + Map<String, Object> expected = new HashMap<>(); |
| 217 | + expected.put("rows", emptyList()); |
| 218 | + assertResponse(expected, runSql(new StringEntity("{ \"cursor\":\"" + cursor + "\"" + mode(mode) + "}", |
| 219 | + ContentType.APPLICATION_JSON), StringUtils.EMPTY, mode)); |
| 220 | + } |
| 221 | + |
154 | 222 | @AwaitsFix(bugUrl = "Unclear status, https://github.com/elastic/x-pack-elasticsearch/issues/2074")
|
155 | 223 | public void testTimeZone() throws IOException {
|
156 | 224 | String mode = randomMode();
|
|
0 commit comments