|
@@ -6,7 +6,6 @@
|
|
|
package org.elasticsearch.xpack.sql.qa.rest;
|
|
|
|
|
|
import com.fasterxml.jackson.core.io.JsonStringEncoder;
|
|
|
-
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
@@ -15,9 +14,11 @@ import org.elasticsearch.client.RequestOptions;
|
|
|
import org.elasticsearch.client.Response;
|
|
|
import org.elasticsearch.client.ResponseException;
|
|
|
import org.elasticsearch.common.CheckedSupplier;
|
|
|
+import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.bytes.BytesArray;
|
|
|
import org.elasticsearch.common.collect.Tuple;
|
|
|
import org.elasticsearch.common.io.Streams;
|
|
|
+import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.common.xcontent.XContentHelper;
|
|
|
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
|
|
import org.elasticsearch.test.NotEqualMessageBuilder;
|
|
@@ -31,6 +32,9 @@ import java.io.InputStream;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.sql.JDBCType;
|
|
|
+import java.time.Instant;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.ZonedDateTime;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collections;
|
|
@@ -151,6 +155,70 @@ public abstract class RestSqlTestCase extends BaseRestSqlTestCase implements Err
|
|
|
ContentType.APPLICATION_JSON), StringUtils.EMPTY, mode));
|
|
|
}
|
|
|
|
|
|
+ public void testNextPageWithDatetimeAndTimezoneParam() throws IOException {
|
|
|
+ Request request = new Request("PUT", "/test_date_timezone");
|
|
|
+ XContentBuilder createIndex = JsonXContent.contentBuilder().startObject();
|
|
|
+ createIndex.startObject("mappings");
|
|
|
+ {
|
|
|
+ createIndex.startObject("properties");
|
|
|
+ {
|
|
|
+ createIndex.startObject("date").field("type", "date").field("format", "epoch_millis");
|
|
|
+ createIndex.endObject();
|
|
|
+ }
|
|
|
+ createIndex.endObject();
|
|
|
+ }
|
|
|
+ createIndex.endObject().endObject();
|
|
|
+ request.setJsonEntity(Strings.toString(createIndex));
|
|
|
+ client().performRequest(request);
|
|
|
+
|
|
|
+ request = new Request("PUT", "/test_date_timezone/_bulk");
|
|
|
+ request.addParameter("refresh", "true");
|
|
|
+ StringBuilder bulk = new StringBuilder();
|
|
|
+ long[] datetimes = new long[] { 1_000, 10_000, 100_000, 1_000_000, 10_000_000 };
|
|
|
+ for (long datetime : datetimes) {
|
|
|
+ bulk.append("{\"index\":{}}\n");
|
|
|
+ bulk.append("{\"date\":").append(datetime).append("}\n");
|
|
|
+ }
|
|
|
+ request.setJsonEntity(bulk.toString());
|
|
|
+ assertEquals(200, client().performRequest(request).getStatusLine().getStatusCode());
|
|
|
+
|
|
|
+ ZoneId zoneId = randomZone();
|
|
|
+ String mode = randomMode();
|
|
|
+ String sqlRequest =
|
|
|
+ "{\"query\":\"SELECT DATE_PART('TZOFFSET', date) AS tz FROM test_date_timezone ORDER BY date\","
|
|
|
+ + "\"time_zone\":\"" + zoneId.getId() + "\", "
|
|
|
+ + "\"mode\":\"" + mode + "\", "
|
|
|
+ + "\"fetch_size\":2}";
|
|
|
+
|
|
|
+ String cursor = null;
|
|
|
+ for (int i = 0; i <= datetimes.length; i += 2) {
|
|
|
+ Map<String, Object> expected = new HashMap<>();
|
|
|
+ Map<String, Object> response;
|
|
|
+
|
|
|
+ if (i == 0) {
|
|
|
+ expected.put("columns", singletonList(columnInfo(mode, "tz", "integer", JDBCType.INTEGER, 11)));
|
|
|
+ response = runSql(new StringEntity(sqlRequest, ContentType.APPLICATION_JSON), "", mode);
|
|
|
+ } else {
|
|
|
+ response = runSql(new StringEntity("{\"cursor\":\"" + cursor + "\"" + mode(mode) + "}",
|
|
|
+ ContentType.APPLICATION_JSON), StringUtils.EMPTY, mode);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Object> values = new ArrayList<>(2);
|
|
|
+ for (int j = 0; j < (i < datetimes.length - 1 ? 2 : 1); j++) {
|
|
|
+ values.add(singletonList(ZonedDateTime.ofInstant(Instant.ofEpochMilli(datetimes[i + j]), zoneId)
|
|
|
+ .getOffset().getTotalSeconds() / 60));
|
|
|
+ }
|
|
|
+ expected.put("rows", values);
|
|
|
+ cursor = (String) response.remove("cursor");
|
|
|
+ assertResponse(expected, response);
|
|
|
+ assertNotNull(cursor);
|
|
|
+ }
|
|
|
+ Map<String, Object> expected = new HashMap<>();
|
|
|
+ expected.put("rows", emptyList());
|
|
|
+ assertResponse(expected, runSql(new StringEntity("{ \"cursor\":\"" + cursor + "\"" + mode(mode) + "}",
|
|
|
+ ContentType.APPLICATION_JSON), StringUtils.EMPTY, mode));
|
|
|
+ }
|
|
|
+
|
|
|
@AwaitsFix(bugUrl = "Unclear status, https://github.com/elastic/x-pack-elasticsearch/issues/2074")
|
|
|
public void testTimeZone() throws IOException {
|
|
|
String mode = randomMode();
|